The <path> element is
the most flexible of the drawing primitives in SVG. It is so flexible,
that technically speaking, one would not need any of the others! Why
have the others then? we might ask. Well it is easier for we humans to
specify a circle than to use the particular subcommand of <path>
that would accomplish the same result.
Since <path> is so powerful and flexible, that means it also has a bit more complexity and is, accordingly, a bit trickier to learn.
Like the other drawing primitives such as <rect>
and <ellipse>, <path> can take
attributes like fill, stroke, and dash array. On the other hand it uses
a special syntax for describing the way it actually visits points in
the plane. It borrows some of its origin (at least ideologically) from turtle
graphics (used in the Logo programming language to help
introduced younger children to the basics of computer programming).
That is, we move the pen (or drawing point) from position to position,
raise it and lower it, and make strokes of varying types. These
instructions within the path syntax are considered to be the
"subcommands" of the path object and are to be found in its d
attribute.
We see from the Primer's treatment of the subject, that a path typically begins with the M subcommand instructing the drawing to begin at a specific (x,y) point such as the point (100,100):
d = "M 100,100 ...
From there, we continue adding points, that is, (x,y) pairs, describing lines segments to be joined along the path.
Here's how it works:
We begin by specifying where the drawing will begin by inserting as the first element of "d" a notation such as "M x y" for numbers x and y. We might think of "M x y" as meaning "move pen to the coordinate x y." From there, we have options of moving (with pen still down on the canvas) linearly (L), quadratically (Q), cubically (C) or through an elliptic arc (A). For example, d="M 100 100 L 200 200" would succeed in drawing a diagonal line from the point (100,100) to the point (200,200), as shown.
| SVG code | Illustration |
|---|---|
<path stroke="black" |
|
The pen-down and line modes stay in effect until turned off, so we might concatenate yet other pairs of coordinates into the path.
| SVG code | Illustration |
|---|---|
<path d="M 100 100 |
|
A couple of things should be noted. First, in the above example, we did not specify a stroke since, by default, the figure is filled with black. Second, if we specify that a path has no fill (using fill="none") then the path will not appear to loop back to the beginning. Third, we might, for sake of legibility, be tempted to add commas, between pairs of coordinates. This is just fine, in the general case, though a few cases have been reported in which certain browsers seem to be troubled by large numbers of commas as coordinate delimiters. Fourth, we may assume that L (or line) is the default way of moving to the next point, and it need not be specified. That is d="M 100 100 L 200 200 100 150" should be equivalent to d="M 100,100 200,200 100,150" . These observations are illustrated as follows. Note that once we specify fill="none" the figure will be invisible, unless we specify a stroke.
| SVG code | Illustration |
|---|---|
<path d="M 50,100 150,200
50,150"/> |
|
The path will also be unclosed — that is, the two endpoints will not be connected unless we specify that they should be. If we wish a path to be closed, we modify it with the z flag at the end of the path as follows:
open:
<path d="M 100,50 200,150 100,100" fill="none" stroke="black"/>
closed:
<path d="M 100,50 200,150 100,100 z" fill="none" stroke="black"/>
Since paths are, by default, filled with black, it is natural to wonder what happens when the path crosses itself. By default, the union of the regions traversed by the path is filled, unless we specify otherwise.
| SVG code | Illustration |
|---|---|
<path d="M 70,290 L 150,150 200,250
40,250 100,150 170,290"/> |
|
<path d="M 70,290 L 150,150 200,250
40,250 100,150 170,290" fill-rule="evenodd"/> |
|
Here we show the default fill technique as well as the "even-odd" fill rule on a shape which intersects itself on more than one occasion. The points are labeled just to make it easier to read what might seem a long list of six coordinate pairs.
Another interesting aspect of <path> is that we might combine multiple path segments into a common path definition. That is, a path may have multiple components by having more than one pen-down operation. Note in the figure below that the two path segments are indeed treated as one since the orange fill is applied to the entire figure rather than to the two separate triangular components. The interior of the figure is also transparent, as illustrated by the rotated and reduced version of the image appearing partly inside and partly outside the foreground figure.
| SVG code | Illustration |
|---|---|
<path fill="orange" |
|
Let us begin with the point (100,200) move over and up to (200, 100) and then over and back down to (300, 200). The d attribute of such a path would be as shown here:
<path d="M 100,200 200,100 300,200 " />
If we include such code in a document, though, we would merely see a black triangle, since by default, the fill value of drawn objects is 'black.'
If we turn off the fill as follows:
<path d="M 100,200 200,100 300,200 " fill="none" />
then nothing will show up since the default value of the stroke is 'none.' Adjusting both attributes at the same time makes the most sense:
<path d="M 100,200 200,100 300,200" fill="none" stroke="black" stroke-width="24"/>
as shown in the illustration below:
We are now part of the way to drawing something that looks like an M. Another up and another down and we are getting closer!
<path d="M 100,200 200,100 300,200 400,100 500,200" fill="none" stroke="black" stroke-width="24"/>
Sure enough, as seen below, the path goes up and down like an 'M'!
We might observe, though, that this particular M is not quite like the M most of us might visualize. First of all, an uppercase M typically has its peaks closer to the end point than to the middle. This can be accomplished by moving the 2nd pair further to the left (changing it from (200,100) to (140, 100) for example) and moving the 4th point further to the right (from (400,100) to say 460,100)). We might also wish to expand the height of the shape, relative to its width.
<path d="M 100,300 140,100 300,300 460,100 500,300" fill="none" stroke="black" stroke-width="24"/>
The above path looks like this:
Moving the peaks further from the center in an M
Still, we might realize that the center of Ms (at least in some fonts) is sometimes higher than its feet (in this case it is a bit lower since the stroke is centered about the defined point and the slopes of the associated lines differ), so we might raise that a bit:, and perhaps adjust the thickness of the stroke:
<path d="M 100,400 140,100 300,250 460,100 500,400" fill="none" stroke="black" stroke-width="34"/>
as is shown here:
Raising the center of the M"
Since SVG allows the definition of one's own font, it is possible to define a series of characters as path elements, though, in truth, real world fonts as well as SVG's power for defining them are far more sophisticated than this very simple example.
With slight modifications to the 'M' shape developed earlier, we may make two of them next to each other by simply moving all the x coordinates to the right:
<path d="M 100,350 100,100
200,225 300,100 300,350" fill="none" stroke="black"
stroke-width="30"/>
<path d="M 400,350 400,100 500,225 600,100 600,350" fill="none"
stroke="black" stroke-width="30"/>
Now let us introduce a slight variation by using a second 'pen-down' command:
<path d="M 100,350 100,100
200,225 300,100 300,350" fill="none" stroke="black"
stroke-width="30"/>
<path d="M 400,350 400,100 500,225 M 600,100 600,350"
fill="none" stroke="black" stroke-width="30"/>
Note that the stroke between the third and fourth coordinate pairs is discontinued but resumed, again, between the fourth and fifth pairs.
Two M's, one further right, but discontinuous
Now, let's do one more experiment with the pen-up and pen-down to make more complex shapes with <path>.
Following are two paths drawn with one apparently "inside" the other (in the sense that the coordinates of one are contained inside the polygon defined by the other):
<path d="M 100,350 300,100
500,350" fill="none" stroke="black" stroke-width="20"/>
<path d="M 250,320 250,220 350,220 350,320" fill="none"
stroke="black" stroke-width="20"/>
Note how the rectangle (which could also be drawn as a <rect>) is encompassed by the triangle.
Two paths, one with 3 pts the other with 4
Next, note that by adding the simple 'z' subcommand (shown below in red) at the end of each of the strings, the paths are closed rather than left open between endpoints.
<path d="M 100,350 300,100
500,350 z"
fill="none" stroke="black" stroke-width="20"/>
<path d="M 250,320 250,220 350,220 350,320 z"
fill="none" stroke="black" stroke-width="20"/>
The rendered SVG image looks like this:
Adding 'z' to the paths to close them.
Observe that the above appearance can alternatively be accomplished with a single path object as follows:
<path d="M 100,350 300,100
500,350 z
M 250,320 250,220 350,220 350,320 z"
fill="none" stroke="black" stroke-width="20"/>
This can save a bit on markup, with the possible problem of increased cognitive complexity and decreased semantic accessibility of the resultant object. However, there are some additional benefits to this approach that are worth considering.
By combining the two shapes above into one complex path, the fill-rule of that path may be defined as "even-odd." The net effect of this is that whatever color is used as the fill of the shape will not be applied to the interior region (though it would be to regions inside the interior region).
<path d="M 100,350 300,100
500,350 z
M 250,320 250,220 350,220 350,320 z"
fill="#ff8" stroke="black" stroke-width="15"
fill-rule="evenodd"/>
The advantage here can be seen in this example, in which rectangles that underly the triangle may be seen through the rectangular hole in the shape. This effect would be difficult to produce if the two parts of this compound path were viewed as separate paths, since in order to be visible the rectangle would have to be atop the triangle, but in that case nothing inside it, other than the triangle itself would be visible.
Yellow triangle with rectangular hole showing pink and green rectangles underneath
I became aware of Bézier curves in the mid 1980s when I discovered that Adobe Illustrator had the ability to draw amazing curves quickly. I did not know what sort of crazy-fast mathematics would be able to solve all those equations so quickly. A good treatment of the subject may be found at Wikipedia.
Here's basically how a quadratic Bézier works in SVG. We define an initial point (say 100,200) with a pen-down. From there, we set a course heading toward the next point. Instead of going to the next point, we just aim that direction. So, for example, while "M 100 200 L 200 400" actually arrives at the point "200,400", "M 100 200 Q 200 400 … " merely heads that way. Ultimately, in addition to a "heading" we also have a final "destination" and that is the final coordinate pair required of the quadratic Bézier. In the illustration we see that.
"M 100,200 L 200,400 300,200"
draws a red path between (and reaching each of) the three points indicated. Simply replacing the "L" with a "Q" to draw
"M 100,200 Q 200,400 300,200"
produces a curve passing through both endpoints, and becoming tangent to the associated lines of the allied line-path at the endpoints to the segments.
| SVG code | Illustration |
|---|---|
<path d="M 100 200 Q 200,400
300,200" fill="none" stroke="blue" /> |
|
While there is an infinite family of curves tangent both to the line "M 100 200 L 200 300" at (100, 200) and to "M 200 400 L 300 200" at (300,200), there is only one quadratic that shares these properties, even if we allow for rotations (in the sense of parametric equations) of the quadratic. That is, the curve is uniquely defined by those three points in the plane. Likewise, any three non-collinear points in the plane determine one quadratic Bézier curve.
Revisiting the earlier example in which the fill-rule was modified to produce an empty space in the middle of the curve, we may draw the same curve with quadratic splines instead of lines to see the effect.
| SVG code | Illustration |
|---|---|
<path fill-rule="evenodd
|
|
Note how the above example:
<path id="H"
fill="#bbb" fill-rule="evenodd"
d="M 70 140 L 150,0 200,100 L 40,100 100,0 L 170,140 70
140"/>
Can have its L's modified to Q's:
<path id="X" fill="#b42"
fill-rule="evenodd"
d="M 70 140 Q 150,0 200,100 Q 40,100 100,0 Q 170,140 70
140"/>
That produces a shape like this (we've changed the colors and
added in identifiers to the paths for easy reference in the text here).
Two paths with same points: one quadratic (id="X") the other
linear (id="H"). See code above.
Observe that the angles of the reddish shape (X) at which the curves actually meet are sharp rather than rounded. Let's look more closely. If you're familiar with a trefoil knot, then that is the sort of shape we'll be aiming toward.
First we may observe that if the desired shape were to pass through any of the six points of the linear path, H, then in order for the parts of the curve that meet there to be smooth and for any of them to be tangent to lines of H, then the new curve would have to extend beyond the bounds of X. We could extend the lines of X into a larger equilateral triangle and then work on building our trefoil knot. This could be done with cubic Bézier curves by defining a curve that passes through the same three endpoints it already does but to be "guided" by the control points consisting of the three points of the circumscribed triangle, shown below as the light green line.
<path fill="#c53"
fill-rule="evenodd" opacity=".5"
d="M 70 140 C 17.5 ,140 150,0 200,100 C 220, 140 40,100 100,0 C 127,-47
170,140 70 140"/>
cubic trefoil guided by circumscribed triangle
Another approach, that would be more symmetric, would be to form a triangle from the three midpoints of the outermost edges of the six sided polygon H, and to let the curve pass through those three points with the control points of the cubic curves being the six original points of H.
Here we see that by defining those three midpoints {(70,50), (175,50), and (120,140)}and allowing the original points of H to act as control points, we can define a variety of curves that move through the midpoints and which are tangent to the curve at those points.
Passing cubic Bézier curves through the midpoints
Another solution is similarly given by
<path fill="#ff8"
fill-rule="evenodd" stroke="black" stroke-width="2" opacity=".65"
d="M 120 140 C 70 140 150,0 175, 50 C 200,100 40,100 70, 50 C 100,0
170,140 120,140"/>
as shown below and here
Trefoil guided by vertices and passing through midpoints