- Add links to JSFiddle
I am revisiting the Fourier transform so I thought it might be nice to come up with some visualizations. My first stop was to plot a sine wave in JavaScript, then to animate it. I am using the window.requestAnimationFrame() method.
function plotSine(ctx, xOffset, yOffset) {
var width = ctx.canvas.width;
var height = ctx.canvas.height;
var scale = 20;
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = "rgb(66,44,255)";
var x = 4;
var y = 0;
var amplitude = 40;
var frequency = 20;
ctx.moveTo(x, 50);
while (x < width) {
y = height/2 + amplitude * Math.sin((x+xOffset)/frequency);
ctx.lineTo(x, y);
x++;
}
ctx.stroke();
ctx.save();
drawPoint(ctx, y);
ctx.stroke();
ctx.restore();
}function plotSine(ctx) {
var width = ctx.canvas.width;
var height = ctx.canvas.height;
var scale = 20;
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = "rgb(66,44,255)";
var x = 0;
var y = 0;
var amplitude = 40;
var frequency = 20;
//ctx.moveTo(x, y);
while (x < width) {
y = height/2 + amplitude * Math.sin(x/frequency);
ctx.lineTo(x, y);
x = x + 1;
console.log("x="+x+" y="+y);
}
ctx.stroke();
}If we uncomment ctx.moveTo(x, y) just before the loop we get a vertical line.





