# Draw a Sine Wave in JavaScript ### To Do - [ ] Add links to JSFiddle I am revisiting the [Fourier transform](https://en.wikipedia.org/wiki/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. I've documented the steps in reverse order. ## Simple Animation The end result is an oscillating sine wave. Nothing ground-breaking but it looks pretty cool to me. ![Animated Sine Wave](https://gist.github.com/gkhays/e264009c0832c73d5345847e673a64ab/raw/d22f89c7a42c33ee3ec553195c7d090ccd11877a/sinewave.gif) The JavaScript funtion to plot the sine wave uses the `Math.sin()` function, which is called repeatedly given a different starting point on the y-axis. See the simple plot below. In this case, the `draw()` function makes the repeat calls via `window.requestAnimationFrame(draw)`. ```js function draw() { var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); context.clearRect(0, 0, 500, 500); showAxes(context); context.save(); plotSine(context, step, 50); context.restore(); step += 4; window.requestAnimationFrame(draw); } ``` ## Superimposed Sine Waves After I learned to plot a sine wave, I visualized how to draw multiple sine waves in order to achieve the oscillation effect in the animation. It looked pretty cool, so I get it around. It reminds me of [Spirograph](https://en.wikipedia.org/wiki/Spirograph). :smile: ![Superimposed](https://gist.github.com/gkhays/e264009c0832c73d5345847e673a64ab/raw/33de4ef3245615c3fdb39e9878198fda88c0625f/Superimposed.png) The `step` advances the starting point along the y-axis. ```js var step = 4; for (var i = -4; i < canvas.height; i += step) { plotSine(context, i, 54 + i); } ``` ## Simple Plot My initial attempt to draw a sine wave on an `HTML5` canvas. ![Sine Wave](https://gist.github.com/gkhays/e264009c0832c73d5345847e673a64ab/raw/e4619775994d7f8d7584b9cd9fd561e6f3841821/Sine.png) ```html

Oscillating Sine Wave

``` ```js 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; } ctx.stroke(); } ``` Interesting tidbit: If we uncomment `ctx.moveTo(x, y)` just before the loop we get a vertical line. ![](https://gist.github.com/gkhays/e264009c0832c73d5345847e673a64ab/raw/b95ad7cb1a8beb644dd52ef414fa46eff6731381/Sine-Left-Bar.png) ## References * [Basic animations](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations) * [window.requestAnimationFrame()](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame) * [WindowTimers.setInterval()](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval) I found this animated gif in Reddit. ![Trigonometry](https://gist.github.com/gkhays/e264009c0832c73d5345847e673a64ab/raw/f73f0fa9236a85e8f1471aad358a4ce01dd84552/trig-aha.gif) See [This should be the first thing shown in any Trigonometry class](https://www.reddit.com/r/pics/comments/bnzgx/this_should_be_the_first_thing_shown_in_any/).