Here's an example of how to draw a 2D sine wave in JavaScript using HTML5 canvas.
881 chars38 lines
In this code, we first set up the canvas and get its 2D graphics context. We then define the starting position of the wave and set up an animation loop using requestAnimationFrame(). Each frame of the animation, we clear the canvas and draw the sine wave by iterating over each x-coordinate from 0 to the width of the canvas and calculating the corresponding y-coordinate using the sine function. Finally, we set the stroke style to blue and draw the line using ctx.stroke().
gistlibby LogSnag