How to draw a circle sector on an html5 canvas?
Asked Answered
O

1

20

I'm trying to make a sort of pie-chart shape on a canvas element, however I can't seem to find any function that does this by itself. I only seem to be able to draw full circles and segments. Is there an easy way to do this?

(See also: Wikipedia on circle terminology)

Overby answered 3/6, 2011 at 17:11 Comment(0)
S
39

The following should work:

context.moveTo(cx,cy);
context.arc(cx,cy,radius,startangle,endangle);
context.lineTo(cx,cy);
context.stroke(); // or context.fill()

with cx, cy being the center of the arc.

Schuler answered 3/6, 2011 at 17:18 Comment(3)
Martin has the right idea, here's a working example if you're really lost: jsfiddle.net/3VB68Suggestion
Just a FYI, angles are in radians here.Tomasz
What about context.beginPath() / context.closePath() ? Some stuff like clearRect() will stop working without themShaunda

© 2022 - 2024 — McMap. All rights reserved.