Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a circle sector on an html5 canvas?

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)

like image 843
dsavi Avatar asked Jun 03 '11 17:06

dsavi


People also ask

How do you make a circle on canvas?

The arc() method creates an arc/curve (used to create circles, or parts of circles). Tip: To create a circle with arc(): Set start angle to 0 and end angle to 2*Math. PI. Tip: Use the stroke() or the fill() method to actually draw the arc on the canvas.

Which method is used to draw a circle on a canvas?

To draw arcs or circles, we use the arc() or arcTo() methods.

How do you make an arc in HTML?

arc Method: arc(x, y, radius, startAngle, endAngle, direction) The x-coordinate (in pixels), for the center point of the arc in relation to the upper-left corner of the canvas rectangle. The y-coordinate ( in pixels), for the center point of the arc in relation to the upper-left corner of the canvas rectangle.


1 Answers

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.

like image 138
MartinStettner Avatar answered Sep 17 '22 19:09

MartinStettner