0
0
HtmlHow-ToBeginner · 3 min read

How to Draw a Circle on Canvas in HTML

To draw a circle on a canvas, use the arc() method of the canvas 2D context. First, get the canvas context with getContext('2d'), then call beginPath(), arc(x, y, radius, 0, 2 * Math.PI), and finally stroke() or fill() to render the circle.
📐

Syntax

The main method to draw a circle on canvas is arc(). It has these parts:

  • x: The horizontal center of the circle.
  • y: The vertical center of the circle.
  • radius: How big the circle is.
  • startAngle: Where the circle starts, in radians (0 is the right side).
  • endAngle: Where the circle ends, in radians (use 2 * Math.PI for a full circle).
  • anticlockwise (optional): Direction to draw the arc, default is clockwise.

Use beginPath() before drawing and stroke() or fill() after to show the circle.

javascript
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.stroke();
💻

Example

This example draws a blue circle with a radius of 50 pixels centered at (100, 100) on the canvas.

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Draw Circle on Canvas</title>
  <style>
    canvas {
      border: 1px solid #ccc;
      display: block;
      margin: 20px auto;
    }
  </style>
</head>
<body>
  <canvas id="myCanvas" width="200" height="200" aria-label="Canvas showing a blue circle"></canvas>
  <script>
    const canvas = document.getElementById('myCanvas');
    const ctx = canvas.getContext('2d');
    ctx.beginPath();
    ctx.arc(100, 100, 50, 0, 2 * Math.PI);
    ctx.fillStyle = 'blue';
    ctx.fill();
  </script>
</body>
</html>
Output
A 200x200 pixel canvas with a solid blue circle centered in the middle.
⚠️

Common Pitfalls

Common mistakes when drawing circles on canvas include:

  • Not calling beginPath() before arc(), which can cause shapes to connect unexpectedly.
  • Using degrees instead of radians for angles; canvas uses radians (full circle is 2 * Math.PI).
  • Forgetting to call stroke() or fill() to actually draw the circle.
  • Setting the radius to zero or a negative number, which will not draw a visible circle.
javascript
/* Wrong: missing beginPath() */
ctx.arc(100, 100, 50, 0, 2 * Math.PI);
ctx.stroke();

/* Right: include beginPath() */
ctx.beginPath();
ctx.arc(100, 100, 50, 0, 2 * Math.PI);
ctx.stroke();
📊

Quick Reference

  • ctx.beginPath(): Start a new shape.
  • ctx.arc(x, y, radius, startAngle, endAngle): Draw circle or arc.
  • ctx.stroke(): Draw the outline.
  • ctx.fill(): Fill the shape with color.
  • Angles: Use radians, not degrees.

Key Takeaways

Use ctx.arc() with 0 to 2*Math.PI radians to draw a full circle.
Always call ctx.beginPath() before drawing a new shape.
Call ctx.stroke() or ctx.fill() to render the circle on canvas.
Angles must be in radians, not degrees.
Set a positive radius to see the circle clearly.