Smooth Wave in HTML5

Waves in HTML5

 
A sine wave is a mathematical function that is repeated at a regular interval of time. The function is used in many fields including mathematics, physics, and engineering. We can also say that a sine wave is a smooth wave.
 
It has the following properties:
  1. The sine wave is blue whenever the value is positive.
  2. The sine wave is red whenever the value is read.
  3. The thickness of the line is directly proportional to the absolute value of the magnitude of the wave. For example, where the sine value reaches 0, the wave is absent.
On the x-axis, we will map the angle Theta. Theta will vary from 0 degrees to 1040 degrees.
 
On the y-axis, we will map the sin (Theta). For this, we will use the Math function Math.sin. The Math.sin function takes angles in radians. So the angle is first multiplied by PI / 180.
 
Since sine waves have both positive and negative values, a sine wave varies from -1 to 1. Since the canvas has only positive values in the y axis, we will need to reconfigure the bitmap so that 0 and negative values can also be represented. To do this, in the sample above the 0 value is mapped to y = 100 on the bitmap. The waves are limited from y = 50 (when sine(theta) = 1) to y = 150 (when sine(theta) = -1).
 
When sineValue > 0, y = 100 - (sineValue-0) * 50; when sineValue < 0, y = 100 + (0 - sineValue) * 50; So when the sineValue is positive, the wave is drawn above y = 100. When it is negative, the wave is drawn below y = 100. The multiplication by 50 is to scale the wave, so that it falls between y = 50 and y = 150.
 
We will now create the "sineWave()" method.
 
Step 1
 
In this method, we will find the sine of the angle; see:
 
var y = Math.sin(x*Math.PI/180);
 
Step 2
 
If the sine value is positive then map it above y = 100 and change the color to blue, as in the following:
  1. if(y >=0)  
  2. {  
  3.     y = 100 - (y-0) * 50;  
  4.     ctx.fillStyle = "blue";  
Step 3
 
If the sine value is negative then map it below y = 100 and change the color to red, as in the following:
  1. if( y < 0 )  
  2. {  
  3.   y = 250 + (0-y) * 50;  
  4.   ctx.fillStyle = "red";  
Step 4
 
We will use the fillRect method to draw the actual wave.
 
ctx.fillRect(x, y, Math.sin(x * Math.PI/180) * 5, Math.sin(x * Math.PI/180 * 5);
 
After that we increment the angle, as in the following:
 
x+=1,
 
Step 5
 
When the angle reaches 1040, stop the animation, as in the following:
 
if(x > 1040)
  clearInterval (animFlag);
 
Example
  1. <!DOCTYPE html>  
  2.    
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     ]  
  7.     <script type="application/javascript">  
  8.    
  9.         var x = 0;  
  10.         var animFlag;  
  11.    
  12.         function init() {  
  13.             var canvas = document.getElementById("canvas");  
  14.             if (canvas.getContext) {  
  15.                 var ctx = canvas.getContext("2d");  
  16.    
  17.                 // Call the sineWave() function repeatedly every 1 microseconds  
  18.                 setInterval(function() {sineWave()}, 1)  
  19.             }  
  20.         }  
  21.    
  22.         function sineWave()  
  23.         {  
  24.             var canvas = document.getElementById("canvas");  
  25.             if (canvas.getContext) {  
  26.                 var ctx = canvas.getContext("2d");  
  27.    
  28.                 // Find the sine of the angle  
  29.                 var y = Math.sin(x*Math.PI/180);  
  30.    
  31.                 // If the sine value is positive, map it above y = 100 and change the colour to blue  
  32.                 if(y >=0)  
  33.                 {  
  34.                     y = 100 - (y-0) * 50;  
  35.                     ctx.fillStyle = "blue";  
  36.                 }  
  37.    
  38.                 // If the sine value is negative, map it below y = 100 and change the colour to red  
  39.                 if( y < 0 )  
  40.                 {  
  41.                     y = 250 + (0-y) * 50;  
  42.                     ctx.fillStyle = "red";  
  43.                 }  
  44.    
  45.                 // We will use the fillRect method to draw the actual wave. The length and breath of the  
  46.                 ctx.fillRect(x, y, Math.sin(x * Math.PI/180) * 5, Math.sin(x * Math.PI/180 * 5);  
  47.    
  48.                 // Increment the angle.  
  49.                 x+=1;  
  50.    
  51.                 // When the angle reaches 1040, stop the animation.  
  52.                 if(x > 1040)  
  53.                     clearInterval (animFlag);  
  54.             }}  
  55.     </script>  
  56.     <title>Animation - Sine Wave</title>  
  57. </head>  
  58. <body onload="init();">  
  59.     <canvas id="canvas" width="700" height="300"></canvas>  
  60. </body>  
  61. </html> 
Output
 
Image 1
 
 
swave1.jpg
 
Image 2
 
swave2.jpg
 
Image 3
 
swave3.jpg
 
Image 4
 
swave4.jpg