How to draw a simple circle in HTML5 Canvas using minimum JavaScript code?
The arc() method is used to create a circle in HTML5 with the canvas element. For a circle with arc() method, use the start angle as 0 and end angle to 2*Math. PI.
To draw arcs or circles, we use the arc() or arcTo() methods.
Use the JavaScript arc() method to draw an arc. Use the beginPath() method to begin the new arc. And use the stroke() and/or fill() method to stroke and fill the arc.
Here is how to draw a circle using JavaScript in HTML5:
const canvas = document.getElementById('myCanvas'); const context = canvas.getContext('2d'); const centerX = canvas.width / 2; const centerY = canvas.height / 2; const radius = 70; context.beginPath(); context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false); context.fillStyle = 'green'; context.fill(); context.lineWidth = 5; context.strokeStyle = '#003300'; context.stroke();
body { margin: 0px; padding: 0px; }
<canvas id="myCanvas" width="578" height="200"></canvas>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With