The following code shows how to draw a spiral by connecting a series of short lines to form a spiral path.
<html> <head> <script> window.onload = function(){ let canvas = document.getElementById("myCanvas"); let context = canvas.getContext("2d"); //from w w w. j av a 2s . c o m let radius = 0; let angle = 0; context.lineWidth = 10; context.strokeStyle = "#0096FF"; context.beginPath(); context.moveTo(canvas.width / 2, canvas.height / 2); for (let n = 0; n < 180; n++) { radius += 0.75; // make a complete circle every 50 iterations angle += (Math.PI * 2) / 50; let x = canvas.width / 2 + radius * Math.cos(angle); let y = canvas.height / 2 + radius * Math.sin(angle); context.lineTo(x, y); } context.stroke(); }; </script> </head> <body> <canvas id="myCanvas" width="600" height="250" style="border:1px solid black;"> </canvas> </body> </html>
To draw a spiral, place our drawing cursor in the center of the canvas.
And then iteratively increase the radius and angle about the center.
Draw a short line from the previous point to the current point.