Path: blob/main/files/en-us/web/svg/attribute/d/index.md
6552 views
------{{SVGRef}}
The d attribute defines a path to be drawn.
A path definition is a list of path commands where each command is composed of a command letter and numbers that represent the command parameters. The commands are detailed below.
You can use this attribute with the following SVG elements: <path>, <glyph>, <missing-glyph>.
d is a presentation attribute, and hence can also be used as a CSS property.
Example
{{EmbedLiveSample('Example', '100%', 200)}}
path
For {{SVGElement('path')}}, d is a string containing a series of path commands that define the path to be drawn.
| Value | <string> |
|---|---|
| Default value | none |
| Animatable | Yes |
glyph
Warning: As of SVG2 {{SVGElement('glyph')}} is deprecated and shouldn't be used.
For {{SVGElement('glyph')}}, d is a string containing a series of path commands that define the outline shape of the glyph.
| Value | <string> |
|---|---|
| Default value | none |
| Animatable | Yes |
Note: The point of origin (the coordinate
0,0) is usually the upper left corner of the context. However the {{SVGElement("glyph")}} element has its origin in the bottom left corner of its letterbox.
missing-glyph
Warning: As of SVG2 {{SVGElement('missing-glyph')}} is deprecated and shouldn't be used.
For {{SVGElement('missing-glyph')}}, d is a string containing a series of path commands that define the outline shape of the glyph.
| Value | <string> |
|---|---|
| Default value | none |
| Animatable | Yes |
Using d as a CSS property
d is a presentation attribute, and hence can be also be modified using CSS. The property takes either path() or none.
The example below shows how you might apply a new path on hover over an element. The new path is the same as the old one, but adds a line across the heart.
{{EmbedLiveSample('Using d as a CSS Property', '100%', 200)}}
Path commands
Path commands are instructions that define a path to be drawn. Each command is composed of a command letter and numbers that represent the command parameters.
SVG defines 6 types of path commands, for a total of 20 commands:
MoveTo:
M,mLineTo:
L,l,H,h,V,vCubic Bézier Curve:
C,c,S,sQuadratic Bézier Curve:
Q,q,T,tElliptical Arc Curve:
A,aClosePath:
Z,z
Note: Commands are case-sensitive. An upper-case command specifies absolute coordinates, while a lower-case command specifies coordinates relative to the current position.
It is always possible to specify a negative value as an argument to a command:
negative angles will be anti-clockwise;
absolute negative x and y values are interpreted as negative coordinates;
relative negative x values move to the left, and relative negative y values move upwards.
MoveTo path commands
MoveTo instructions can be thought of as picking up the drawing instrument, and setting it down somewhere else—in other words, moving the current point (Po; {xo, yo}). There is no line drawn between Po and the new current point (Pn; {xn, yn}).
| Command | Parameters | Notes |
|---|---|---|
| M |
(x, y)+
|
Move the current point to the coordinate
Formula: Pn = { |
| m |
(dx, dy)+
|
Move the current point by shifting the last known position of
the path by
Formula: Pn = {xo
+ |
Examples
{{EmbedLiveSample('MoveTo_path_commands', '100%', 200)}}
LineTo path commands
LineTo instructions draw a straight line from the current point (Po; {xo, yo}) to the end point (Pn; {xn, yn}), based on the parameters specified. The end point (Pn) then becomes the current point for the next command (Po′).
| Command | Parameters | Notes |
|---|---|---|
| L | (x, y)+ |
Draw a line from the current point to the
end point specified by
Formula: Po′ =
Pn = { |
| l |
(dx, dy)+
|
Draw a line from the current point to the
end point, which is the current point shifted by
Formula: Po′ =
Pn = {xo +
|
| H |
x+
|
Draw a horizontal line from the current point to the
end point, which is specified by the
Formula: Po′ =
Pn = { |
| h |
dx+
|
Draw a horizontal line from the current point to the
end point, which is specified by the
current point shifted by
Formula: Po′ =
Pn = {xo +
|
| V |
y+
|
Draw a vertical line from the current point to the
end point, which is specified by the
Formula: Po′ =
Pn = {xo, |
| v |
dy+
|
Draw a vertical line from the current point to the
end point, which is specified by the
current point shifted by
Formula: Po′ =
Pn = {xo, yo + |
Examples
{{EmbedLiveSample('LineTo_path_commands', '100%', 200)}}
Cubic Bézier Curve
Cubic Bézier curves are smooth curve definitions using four points:
starting point (current point)
: (Po = {xo, yo})
end point
: (Pn = {xn, yn})
start control point
: (Pcs = {xcs, ycs}) (controls curvature near the start of the curve)
end control point
: (Pce = {xce, yce}) (controls curvature near the end of the curve)
After drawing, the end point (Pn) becomes the current point for the next command (Po′).
| Command | Parameters | Notes |
|---|---|---|
| C |
(x1,y1, x2,y2, x,y)+
|
Draw a cubic Bézier curve from the current point to the
end point specified by
|
| c |
(dx1,dy1, dx2,dy2, dx,dy)+
|
Draw a cubic Bézier curve from the current point to the
end point, which is the current point shifted by
|
| S |
(x2,y2, x,y)+
|
Draw a smooth cubic Bézier curve from the current point to the
end point specified by x,y. The end control point is specified by
x2,y2. The start control point is the reflection of the
end control point of the previous curve command about the current point. If the
previous command wasn't a cubic Bézier curve, the
start control point is the same as the curve starting point
(current point). Any subsequent pair(s) of coordinate pairs are
interpreted as parameter(s) for implicit absolute smooth cubic Bézier
curve (S) commands.
|
| s |
(dx2,dy2, dx,dy)+
|
Draw a smooth cubic Bézier curve from the current point to the
end point, which is the current point shifted by
dx along the x-axis and
dy along the y-axis. The
end control point is the current point (starting point
of the curve) shifted by dx2 along the x-axis
and dy2 along the y-axis. The
start control point is the reflection of the
end control point of the previous curve command about the current point. If the
previous command wasn't a cubic Bézier curve, the
start control point is the same as the curve starting point
(current point). Any subsequent pair(s) of coordinate pairs are
interpreted as parameter(s) for implicit relative smooth cubic Bézier
curve (s) commands.
|
Examples
{{EmbedLiveSample('Cubic_Bézier_Curve', '100%', 200)}}
Quadratic Bézier Curve
Quadratic Bézier curves are smooth curve definitions using three points:
starting point (current point)
: Po = {xo, yo}
end point
: Pn = {xn, yn}
control point
: Pc = {xc, yc} (controls curvature)
After drawing, the end point (Pn) becomes the current point for the next command (Po′).
| Command | Parameters | Notes |
|---|---|---|
| Q |
(x1,y1, x,y)+
|
Draw a quadratic Bézier curve from the current point to the
end point specified by
|
| q |
(dx1,dy1, dx,dy)+
|
Draw a quadratic Bézier curve from the current point to the
end point, which is the current point shifted by
|
| T |
(x,y)+
|
Draw a smooth quadratic Bézier curve from the
current point to the end point specified by
|
| t |
(dx,dy)+
|
Draw a smooth quadratic Bézier curve from the
current point to the end point, which is the
current point shifted by
|
Examples
{{EmbedLiveSample('Quadratic_Bézier_Curve', '100%', 200)}}
Elliptical Arc Curve
Elliptical arc curves are curves defined as a portion of an ellipse. It is sometimes easier to draw highly regular curves with an elliptical arc than with a Bézier curve.
| Command | Parameters | Notes |
|---|---|---|
| A |
(rx ry
angle large-arc-flag
sweep-flag x
y)+
|
Draw an Arc curve from the current point to the coordinate
x,y becomes the new current point for the next
command. All subsequent sets of parameters are considered implicit
absolute arc curve (A) commands.
|
| a |
(rx ry
angle large-arc-flag
sweep-flag dx
dy)+
|
Draw an Arc curve from the current point to a point for which
coordinates are those of the current point shifted by
dx and dy for the next
command. All subsequent sets of parameters are considered implicit
relative arc curve (a) commands.
|
Examples
{{EmbedLiveSample('Elliptical_Arc_Curve', '100%', 200)}}
ClosePath
ClosePath instructions draw a straight line from the current position to the first point in the path.
| Command | Parameters | Notes |
|---|---|---|
| Z, z | Close the current subpath by connecting the last point of the path with its initial point. If the two points are at different coordinates, a straight line is drawn between those two points. |
Note: The appearance of a shape closed with ClosePath may be different to that of one closed by drawing a line to the origin, using one of the other commands, because the line ends are joined together (according to the {{SVGAttr('stroke-linejoin')}} setting), rather than just being placed at the same coordinates.
Examples
{{EmbedLiveSample('ClosePath', '100%', 200)}}
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}