Get the App
SLTechnology News&Howtos  ›  Development  › 

How to achieve interesting wave effect with HTML5

Shulou Source: shulou.com Published: 2022-06-01 06:10:09 09月30日 Update

Today, I would like to share with you the relevant knowledge points about how to achieve interesting wave effects in HTML5. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

1. Create trigger condition (button)

= "Beisizer ()" / / trigger the JS event when clicked

JS event nm mouse over JS event = "bcd ()" / /

"nm" useleave= "out ()" / / JS event when the mouse leaves

Id= "Anniu" > accurate & nbsp?     ?   & nbsp determination

2. Create a canvas Canvas

3. Create a JS event (property setting) / / get the canvas

Var?beisizer?=?document.getElementById ("Theback");? var?ContenofBeisizer?=?beisizer.getContext ("2d"); / / set wave area (wave width, height)

Var?beisizerwidth?=?beisizer.width;?var?beisizerheight?=?beisizer.height;?var?beisizerlinewidth?=?2;// curve

Var?sinX?=?0;?var?sinY?=?beisizerheight/2.0;// axis length

Var?axisLenght?=?beisizerwidth;// Radian width

Var?waveWidth?=?0.014;// wave height

Var?waveHeight?=?beisizerheight?/?15.0

ContenofBeisizer.lineWidth?=?beisizerlinewidth

4. Draw Bessel curve var?drawSin?=?function (xofspeed). {

ContenofBeisizer.save (); / / stores the points of the Bezier curve

Var?points?=? []

ContenofBeisizer.beginPath (); / / create a Bezier point

For? (var?x?=?sinX;x?

Var?y?=?-Math.sin ((sinX+x) * waveWidth+xofspeed); / /? points.push ([x _ thime). For the test, please uncomment and comment on the next line

Points.push ([x _ rem _ r _ r _ r

/ / ContenofBeisizer.lineTo, please uncomment and comment on the next line for the test

ContenofBeisizer.lineTo (x, women, girls, boys, girls, girls, boys, girls, boys, girls, boys, girls, girls, boys, girls, girls,

}

ContenofBeisizer.lineTo (axisLenght,beisizerheight)

ContenofBeisizer.lineTo (sinX,beisizerheight)

ContenofBeisizer.lineTo (points [0] [0], points [0] [1])

ContenofBeisizer.stroke ()

ContenofBeisizer.restore ();? / / Bezier curve style setting

ContenofBeisizer.strokeStyle?=? "# 3bc777"

ContenofBeisizer.fillStyle?=? "# 3bc777"

ContenofBeisizer.fill ()

This code has finished drawing the static Bessel curve, so you can try to run it by unlocking the following statement.

Var y =-Math.sin ((sinX+x) * waveWidth)

Points.push ([x _ rem _ r _ r _ r

Var y =-Math.sin ((sinX+x) * waveWidth+xofspeed)

Run the method to execute drawSin ()

Static Bezier curve. Png

5. The realization of wave effect.

You need to add the following code to the property to set the rate.

Var speed = 0.1; the higher the number, the faster the rate.

Var xofspeed = 0;? Lateral offset of waves

Var rand = beisizerheight; wave height var?rendY?=?function? {

ContenofBeisizer.clearRect / / controls the height of the waves

Var?tmp?=?0.1

Rand-=tmp;var?b?=?beisizerheight?-?rand

/ / Control of cyclic high tide

If? (parseInt (b) = = beisizerheight) {

Rand?=?beisizerheight

}

DrawSin (xofspeed)

DrawSinl (xofspeed)

Xofspeed?+=?speed

RequestAnimationFrame (rendY)

By calling itself to produce different heights, to produce a wave effect. The attribute values set here are understood together with the Bezier curve drawn in step 4.

Operation mode rendY ()

These are all the contents of the article "how to achieve interesting Wave effects in HTML5". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Waves curves Bezier effects height events comments knowledge articles properties waves running interesting different one line code content width canvas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MySQL Huawei Redmi NVidia