Canvas Draw Arc
How to Draw a Arc
To draw a arc, follow these steps
- canvas - Assigning the canvas element to a variable.
- ctx - Assigning the 2d rendering context to another variable by calling the
getContext('2d')
method. - beginPath() - Use
beginPath()
canvas method to start a new path by emptying the list of sub-paths. - arc() - Use
arc()
canvas method to add a circular arc to the current sub-path. - stroke() - Use
stroke()
canvas method to stroke (outline) the current or given path with the currentstrokeStyle
. The defaultstrokeStyle
is 'black'.
Example
HTML Online Editor
<!DOCTYPE html>
<html lang="en-US">
<body>
<canvas width="120" height="120" id="point">
Your browser does not support the canvas element.
</canvas>
<script>
var canvas = document.getElementById('point');
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(60, 60, 50, 0, 2 * Math.PI);
ctx.stroke();
</script>
</body>
</html>
Syntax
ctx.arc(x, y, radius, startAngle, endAngle [, anticlockwise])
Attributes Value
Value | Type | Explanation |
---|---|---|
x | Required | Specifies the x-axis coordinate of the arc's center. |
y | Required | Specifies the y-axis coordinate of the arc's center. |
radius | Required | Specifies the arc's radius. Should be positive vale. |
startAngle | Required | Specifies the angle at which the arc starts. |
endAngle | Required | Specifies the angle at which the arc ends. |
anticlockwise | Optional | Specifies the arc to be drawn counter-clockwise between the start and end angles. Default value is false (clockwise). |
How to fill the Arc
To fill the arc, follow these steps
- canvas - Assigning the canvas element to a variable.
- ctx - Assigning the 2d rendering context to another variable by calling the
getContext('2d')
method. - beginPath() - Use
beginPath()
canvas method to start a new path by emptying the list of sub-paths. - arc() - Use
arc()
canvas method to add a circular arc to the current sub-path. - fill() - Use
fill()
canvas method to fill the object with the currentfillStyle
. The defaultfillStyle
color is 'black'. - stroke() - Use
stroke()
canvas method to stroke (outline) the current or given path with the currentstrokeStyle
. The defaultstrokeStyle
is 'black'.
Example
HTML Online Editor
<!DOCTYPE html>
<html lang="en-US">
<body>
<canvas width="120" height="120" id="point">
Your browser does not support the canvas element.
</canvas>
<script>
var canvas = document.getElementById('point');
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(60, 60, 50, 0, 2 * Math.PI);
ctx.fill();
ctx.stroke();
</script>
</body>
</html>
How to Custom Color the Arc
To custom color the arc, you have to overwrite the fillStyle
and strokeStyle
canvas properties before calling the fill()
and stroke()
canvas methods.
Example
HTML Online Editor
<!DOCTYPE html>
<html lang="en-US">
<body>
<canvas width="120" height="120" id="point">
Your browser does not support the canvas element.
</canvas>
<script>
var canvas = document.getElementById('point');
var ctx = canvas.getContext('2d');
ctx.fillStyle = "#339933";
ctx.strokeStyle = "red";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.arc(60, 60, 50, 0, 2 * Math.PI);
ctx.fill();
ctx.stroke();
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 93% of Canvas Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in Canvas.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.