Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a circle in HTML5 Canvas using JavaScript?

How to draw a simple circle in HTML5 Canvas using minimum JavaScript code?

like image 799
Humayun Shabbir Avatar asked Aug 02 '14 13:08

Humayun Shabbir


People also ask

How do I draw a circle in HTML5?

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.

Which JavaScript method is used to draw a circle on a canvas?

To draw arcs or circles, we use the arc() or arcTo() methods.

How do you draw an arc in JavaScript?

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.


1 Answers

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>
like image 172
Humayun Shabbir Avatar answered Sep 28 '22 02:09

Humayun Shabbir