Canvas Draw Arc

How to Draw a Arc

To draw a 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. 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
xRequiredSpecifies the x-axis coordinate of the arc's center.
yRequiredSpecifies the y-axis coordinate of the arc's center.
radiusRequiredSpecifies the arc's radius. Should be positive vale.
startAngleRequiredSpecifies the angle at which the arc starts.
endAngleRequiredSpecifies the angle at which the arc ends.
anticlockwiseOptionalSpecifies 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

  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

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.

Meet the Author