Created
August 22, 2014 06:42
-
-
Save reecer/e083bd3f960b3d868089 to your computer and use it in GitHub Desktop.
Sine wave using canvas.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var canvas = document.getElementById("canvas"); | |
var ctx = canvas.getContext("2d"); | |
var points = {}; | |
var x = 0; | |
var len = 500; | |
animate(); | |
function animate() { | |
requestAnimationFrame( animate ); | |
draw(); | |
} | |
function y() { | |
var mag = 20; | |
var angle = 0.15; | |
return mag * Math.sin(angle * x) + len/2; | |
} | |
function draw(){ | |
ctx.clearRect(0, 0, len, len); // Clear the canvas | |
ctx.beginPath(); | |
ctx.setLineWidth(3); | |
ctx.setStrokeColor("blue"); | |
points[x] = y(); | |
x += 1; | |
for(var i = 0; i < len; i++) { | |
ctx.lineTo(i, points[i + x - len]); | |
} | |
ctx.stroke(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment