Drawing Arcs and Circles
You'll learn how to use the arc and arcTo methods to draw circles, arcs, and rounded corners on the HTML canvas.
Now that you understand canvas paths, it's time to master curves. While straight lines create angular shapes, arcs let you draw circular paths—everything from perfect circles to pie slices to smoothly rounded corners.
The arc() Method
The arc() method draws a circular arc. Think of it like a compass: you specify a center point, radius, and which portion of the circle to draw.
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
ctx.beginPath();
// arc(x, y, radius, startAngle, endAngle, counterclockwise)
ctx.arc(100, 100, 50, 0, Math.PI * 2);
ctx.stroke(); // Draws a complete circle
The angles are in radians (not degrees). 0 is 3 o'clock, Math.PI / 2 is 6 o'clock, and Math.PI * 2 completes the circle. The optional boolean controls direction.
For a half-circle or arc:
ctx.beginPath();
ctx.arc(200, 100, 40, 0, Math.PI); // Half circle (top half)
ctx.stroke();
The arcTo() Method
The arcTo() method draws an arc that connects two lines smoothly—perfect for rounded corners. Imagine you're driving and need to turn: instead of a sharp corner, you follow a gentle curve.
ctx.beginPath();
ctx.moveTo(50, 50);
// arcTo(x1, y1, x2, y2, radius)
ctx.arcTo(150, 50, 150, 150, 20); // Rounded corner
ctx.lineTo(150, 150);
ctx.stroke();
It creates a tangent arc between your current point and the line defined by the control points.
Key Takeaway: Use arc() for circles and circular arcs by specifying center, radius, and angle range; use arcTo() to create smooth, rounded corners between two lines with a given radius.