Curve with Math sin - Javascript Canvas

Javascript examples for Canvas:Example

Description

Curve with Math sin

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.6.3.js"></script> 
      <script type="text/javascript">
    $(window).load(function(){// w w  w .j  av a  2 s  .c  o m
    var canvas = document.getElementById('canvas'),
        ctx = canvas.getContext('2d'),
        x=0,y=0;
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    canvas.onmousemove= function(e){
        //clear the canvas
        ctx.clearRect(0,0,canvas.width,canvas.height);
        ctx.fillStyle = "blue";
        var width = window.innerWidth;

        ctx.beginPath();

        for(var i=0;i<=width;i++){
            y=Math.sin((i+x)*50)*12;
            ctx.arc(i, y+100, 4, 0, 2*Math.PI);
        }

        ctx.closePath();

        ctx.fill();
        x+=20;
    }
    });

      </script> 
   </head> 
   <body> 
      <canvas id="canvas" style="border:1px solid black;"></canvas>  
   </body>
</html>

Related Tutorials