Canvas How to - Draw spiral curve








Question

We would like to know how to draw spiral curve.

Answer


<!--   w  w w .  ja  v a 2 s .  co  m-->
<html>
    <head>
        <script>
            window.onload = function(){
                var canvas = document.getElementById("myCanvas");
                var context = canvas.getContext("2d");
                
                var radius = 0;
                var angle = 0;
                
                context.lineWidth = 10;
                context.strokeStyle = "#0096FF"; // blue-ish color
                context.beginPath();
                context.moveTo(canvas.width / 2, canvas.height / 2);
                
                for (var n = 0; n < 150; n++) {
                    radius += 0.75;
                    // make a complete circle every 50 iterations
                    angle += (Math.PI * 2) / 50;
                    var x = canvas.width / 2 + radius * Math.cos(angle);
                    var 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>

The code above is rendered as follows: