<path>
: Arcs
SVG has dedicated
<circle>
and
<ellipse>
elements, but if you only want to draw a section of a circular or elliptical arc, you need to use a
<path>
element with an arc command. The arc command defines the shape of the ellipse that should be used to connect two points. But it also has to define which of
four possible
arcs (all following the shape of that ellipse) should be used to connect those points.
The arc format is "
[A|a]rx,ry, x-axis-rotation, large-arc-flag, sweep-flag, x,y
". As with the other path directions, the arc starts at the end-point of the previous direction (which might be simply a "move" command to start a path).
A
or
a
means "arc" (as opposed to line, curve or any of the other path directions);
a capital letter means that coordinates are absolute, lowercase means relative to the last point on the path ( as with all the other path direction tags );
rx
and
ry
are the elliptical radii; they should be equal for a circular arc;
x-axis-rotation
is the rotation of the ellipse relative to the local coordinate system;
large-arc-flag
and
sweep-flag
are each either
1
or
0
, and define which of the four arcs to draw;
x,y
is the coordinate of the end-point of the arc.
If you're used to SVG
<circle>
and
<ellipse>
elements, you'll be struck by the fact that the
centre
of the shape is never specified. Instead, it's calculated to fit the other requirements. The example shows how this can get confusing.
All of the curves in the drawing start at
(20,50)
and end at
(80,50)
.
All are based on an ellipse with x-radius of 40 and y-radius of 30, where the x-axis of the shape is rotated 20 degrees from horizontal.
The dashed lines have
large-arc-flag
set to 1
;
the dotted lines have it set to zero
.
The purple lines have
sweep-flag
set to 1
;
.
The
large-arc-flag
is easy to understand: it tells the path to take the long way around the ellipse. The
sweep-flag
is less intuitively named, but can be understood as the "clockwise" flag; it forces the arc to be positioned such that the chosen (long or short) arc makes a clockwise curve between the start and end points.
One final point: Arc notation falls apart if you try to draw a
complete
circle or ellipse, where the end-point is the same as the start-point. The
large-arc-flag
will be ignored and the segment interpretted as a zero-length line. That's because there are
infinite
possible ways to position an ellipse (not just four) when only a single point is defined — that point could be anywhere on the shape. If you do want to draw a complete circle as part of a path, you'll have to break it down into two arcs:
"M0,50 A50,50,0 1 1 100,50 A50,50,0 1 1 0,50 Z"
is the circle that exactly fills a 100×100 square.