How To Create Paths in SVG

Creating basic shapes in SVG are described in previous post “How To Create Basic Shapes in SVG”, which are shorthand forms of the more general <path> element. The <path> element is more general then these shapes, using path element, you can draw the outline of any arbitrary shape by specifying a series of connected lines, arcs, and curves, which can be filled, stroked, used as a clipping path, or any combination of the three. This outline can be filled and drawn with a stroke, just as done on creating basic shapes.

<path> element

The <path> element consists of two attributes which are “path data” and “pathLength”. All of the data describing an outline of a shape is in the <path> element’s path data(d) attribute. Every path must begin with a moveto command. The path data consists of one-letter commands, such as M for moveto or L for lineto, followed by an x- and y-coordinate for that particular command separated by commas or white-space.The pathLength attribute used in <path> element includes numbers as its value for the computation of the total length of the path.

Path data commands

A path is defined by including a <path> element which contains path data(d) attribute, where the d attribute contains the moveto, line, curve, arc and closepath commands.Path data can contain newline characters and thus can be broken up into multiple lines to improve readability but they are only allowed at certain places within path data.

moveto commands

The “moveto” commands (M or m) establish a new current point. The effect is as if the “pen” were lifted and moved to a new location. A path data segment must begin with a “moveto” command. Subsequent “moveto” commands represent the start of a new subpath

closepath commands

The “closepath” (Z or z) ends the current subpath and causes an automatic straight line to be drawn from the current point to the initial point of the current subpath. If a “closepath” is followed immediately by a “moveto”, then the “moveto” identifies the start point of the next subpath. If a “closepath” is followed immediately by any other command, then the next subpath starts at the same initial point as the current subpath.

lineto commands

The various “lineto” commands draw straight lines from the current point to a new point. The commands L or l draws a line from the current point to the given (x,y) coordinate which becomes the new current point, where L indicates that absolute coordinates will follow and l indicates that relative coordinates will follow. A number of coordinates pairs may be specified to draw a polyline. At the end of the command, the new current point is set to the final set of coordinates provided.

Another set of commands H or h draws a horizontal line from the current point (cpx, cpy) to (x, cpy), where H indicates that absolute coordinates will follow and h indicates that relative coordinates will follow. Multiple x values can be provided. At the end of the command, the new current point becomes (x, cpy) for the final value of x.

The next set of commands V or v draws a vertical line from the current point (cpx, cpy) to (cpx, y), where V indicates that absolute coordinates will follow and v indicates that relative coordinates will follow. Multiple y values can be provided. At the end of the command, the new current point becomes (cpx, y) for the final value of y.

Example of Drawing Paths Using Moveto, Lineto and Closepath Commands

If you want to use a <path> to draw a triangle, you can draw all three lines, or you can draw the first two lines and then use the closepath command, denoted by a capital Z, to draw a straight line back to the beginning point of the current subpath. Following example shows the way to draw a triangle with closepath, and a path that draws a triangle by opening and closing subpaths.

Curve Commands

The Cubic Bézier commands (C, c, S and s) consists of a cubic Bézier segment, which is defined by a start point, an end point, and two control points. Quadratic Bézier commands (Q, q, T and t) includes a quadratic Bézier segment, which is defined by a start point, an end point, and one control point. And the Elliptical arc commands (A and a) includes an elliptical arc segment draws a segment of an ellipse.

Example of cubic Bézier curve commands

Using the C or c command, you can create cubic Bézier curve. The command is followed by three sets of coordinates that specify the control point for the start point, the control point for the end point and the end point. As with all the other path commands, an uppercase command implies absolute coordinates and lowercase implies relative coordinates. The curve in the following example was drawn from (20,80) to (200,120) with control points at (50,20) and (150,60).

Example of quadratic Bézier curve commands

You create a quadratic curve in a <path> data with the Q or q command. The command is followed by two sets of coordinates that specify a control point and an endpoint. The uppercase command implies absolute coordinates and lowercase implies relative coordinates. The curve in following example was drawn from (20, 75) to (230, 90) with the control point at (240, 30).

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 8 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.