Canvas Draw Arc

Last Updated:

How to Draw a Arc

Your browser does not support the canvas element.

To draw a line, follow these steps

  1. canvas - Assigning the canvas element to a variable.
  2. ctx - Assigning the 2d rendering context to another variable by calling the getContext('2d') method.
  3. beginPath() - Use beginPath() canvas method to start a new path by emptying the list of sub-paths.
  4. arc() - Use arc() canvas method to add a circular arc to the current sub-path.
  5. stroke() - Use stroke() canvas method to stroke (outline) the current or given path with the current strokeStyle. The default strokeStyle 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

ValueTypeExplanation
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

Your browser does not support the canvas element.

To fill the arc, follow these steps

  1. canvas - Assigning the canvas element to a variable.
  2. ctx - Assigning the 2d rendering context to another variable by calling the getContext('2d') method.
  3. beginPath() - Use beginPath() canvas method to start a new path by emptying the list of sub-paths.
  4. arc() - Use arc() canvas method to add a circular arc to the current sub-path.
  5. fill() - Use fill() canvas method to fill the object with the current fillStyle. The default fillStyle color is 'black'.
  6. stroke() - Use stroke() canvas method to stroke (outline) the current or given path with the current strokeStyle. The default strokeStyle 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

Your browser does not support the canvas element.

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>

Share this Page

Meet the Author