Drawing SVGs

Learn about the basic coordinate system and elements to draw your SVGs.

Once you have a firm grasp of Understanding SVGs...

Table of Contents

  • The SVG Drawing Space
    • The <svg> element
    • SVG Namespace
    • Coordinate System
    • The viewBox
  • Drawing Elements
    • <line>
    • <circle>
    • <ellipse>
    • <rect>
    • <polygon>
    • <path>
      • arc
      • beziér
    • transform Attribute
  • Object Elements
    • <text>
    • <image>
    • <symbol>
    • <use>
    • <foreignObject>

The SVG Drawing Space

The <svg> element

Different ways to describe the drawing area.

Using height and width.

<svg width="100" height="100">
</svg>

using viewBox

<svg viewBox="0,0 100,100">
</svg>
  • Within the SVG it’s more accurate to describe units instead of pixels. Each of the three following SVGs all have the same stroke width (2).

The SVG Namespace

All SVG image documents are required to have the SVG namespace in the <svg> element.

xmlns="http://www.w3.org/2000/svg"

Coordinate System

Simple cartesian coordinates.

Cartesian Coordinates

The viewBox

The viewBox is the visible area of the artwork in the SVG that will be displayed. Elements that exist outside of the viewBox will not be seen.

<svg viewBox="0 0 10 6">
<svg viewBox="0,0 10,6">
A viewBox set to origin 0,0 with a width of 10 and a height of 6.

You can think of the viewBox as an analog to the artboard that can be found in Adobe Illustrator or other vector-based drawing applications. When Illustrator exports SVG files the artboard defines the viewBox.

The Artboard in Adobe Illustrator

Drawing Elements

Drawing elements can describe straight and curved paths or entire shape primitives.

The <line> element

<line x1="10" y1="20" x2="70" y2="40" />
<line x1="25" y1="25" x2="75" y2="75" />
<line x1="25" y1="75" x2="75" y2="25" />

The <circle> element

<circle cx="50" cy="50" r="25" />

The <ellipse> element

<ellipse cx="50" cy="50" rx="45" ry="25" />

The <rect> element

<rect x="25" y="25" width="50" height="50" />

rounded rect

<rect x="25" y="25" width="50" height="50" rx="8" ry="8" />

The <polygon> element

<polygon points="50,33 75,75 25,75" />

The <path> element

The <path> element has the capacity to be the most confusing, and yet the most flexible, SVG element that can be used to draw paths.

<path d="M50 95c26 0 21-48 47-48 0-11-19-21-32-4 0-18-1-26-15-38-14 12-15 20-15 38-13-17-32-7-32 4 26 0 21 48 47 48z" />

You’ll discover that once you begin to understand the path commands the element won’t seem nearly as frightening.

Path Commands
AbsoluteRelativeNameDescription
MmMoveTo
LlLineTo
VvVertical
HhHorizontal
AaArcTo
CcCubic Bézier
QqQuadratic Bézier
ZzClosePath

Absolute and Relative Path Commands

Any shape could be drawn using absolute or relative path commands.

Absolute Commands (the commands using the uppercase letters) move in relation to the origin coordinates (0,0).

<path d="M50,33 75,75 25,75Z" />

Relative commands (the commands using the lowercase letters) move in relation to the preceding point’s coordinates.

<path d="m50,33 25,42 -50,0z" />

MoveTo

The MoveTo command (M or m) tells the SVG to start drawing. Paths always start with a MoveTo command starting from the SVG origin point (0,0).

<path d="M25,33 77,25" />

If you can imagine tha path as a line being drawn by a pen, the MoveTo command is an analog to lifting up the pen, and putting it back down somewhere else on the paper.

<path d="M25,33 77,25 M75,75 25,73" />

LineTo

The LineTo command (L or l) draws a straight line to that point.

<path d="M25,75 L50,25 L75,75" />

Vertical

The Vertical command (V or v) draws a vertical line to the point.

<path d="M25,33 V77" />

Horizontal

The Horizontal command (H or h)

<path d="M25,33 H77" />

Close Path

The Close path command (Z or z) closes the path by drawing a line directly to the first point after the preceding MoveTo command.

An open path:

<path d="M45,15 75,30 75,60 45,75 15,60 15,30" />

Closing a path:

<path d="M45,15 75,30 75,60 45,75 15,60 15,30z" />

path: arc

The ArcTo command lets you draw an Arc in the following formula:

A rx,ry x-axis-rotation large-arc-flag sweep-flag x,y
<path d="M25,75 A10,10 0 0 0 75,25" />

Arc and Sweep flags

When describing an arc path there are several directions that the path could follow, these directions are controlled with the arc flag and the sweep flag.

An example of the 4 directions an arc path could follow.

No flags set

<path d="M33,33 A33,33 0 0 0 67,67" />

large-arc-flag

<path d="M33,33 A33,33 0 1 0 67,67" />

sweep-flag

<path d="M33,33 A33,33 0 0 1 67,67" />

large-arc-flag + sweep-flag

<path d="M33,33 A33,33 0 1 1 67,67" />

x-axis-rotation

An arc path rotated at 0°, 30° and 60°

path: cubic bézier

Commands

  • CurveTo (Cubic)
  • Mirror Handle (Cubic S)
<path d="M3,4 C1,1 7,1 5,4" />
<path d="M10,60 C-10,90 110,90 90,60" />
<path d="M50,90 C-15,40 10,5 50,30 C90,5 110,40 50,90z" />

path: quadratic bézier

Commands

  • CurveTo (Quadratic)
  • Mirror Handle (Quadratic T)
<path d="M3,4 Q4,1 5,4" />
<path d="M5,80 Q50,5 95,80" />

Mirror handles

<path d="M5,40 C20,5 32.5,5 50,40 S82.5,75 95,40" />
<path d="M5,40 Q25,-15 50,40 T95,40" />

Transform

Transforms are...

Original

<rect x="25" y="25" width="50" height="50" />

Translate

<rect x="25" y="25" width="50" height="50" transform="translate(20,0)" />

Rotate

<rect x="25" y="25" width="50" height="50" transform="rotate(35)" />
<rect x="25" y="25" width="50" height="50" transform="rotate(35 50,50)" />

Skew

<rect x="25" y="25" width="50" height="50" transform="skewX(20)" />
<rect x="25" y="25" width="50" height="50" transform="skewY(20)" />
<rect x="25" y="25" width="50" height="50" transform="skewX(20) skewY(20)" />
<rect x="25" y="25" width="50" height="50" transform="skewY(20) skewX(20)" />

Scale

<rect x="25" y="25" width="50" height="50" transform="scale(1.5)" />

Matrix

To tranform: matrix(scaleX, skewX, skewY, scaleY, translateX, translateY)

<rect x="25" y="25" width="50" height="50" transform="matrix(-1,-.2,.5,1,75,10)" />

To rotate: matrix(cos, sin, -sin, cos, translateX, translateY)

<rect x="25" y="25" width="50" height="50" transform="matrix(.707,.707,-.707,.707,50,-20)" />

Object Elements

A range of elements to put objects on your page.

The <text> element

<text> allows you to add text in a single line into the SVG. You can apply the same SVG styles, transformations, and filters to text as any other SVG element.

<text x="25" y="75"&gt;I Love SVG!&lt;/text>
I Love SVG!

<tspan>

<tspan> allows you to change text styles within the line of text, much like a <span> element in HTML.

<tspan fill="red" stroke="none"&gt;&amp;#10084;&lt;/tspan>
I SVG!

There is no native line-wrapping in SVG text.

The <image> element

<image href="lion_cracker.webp" x="10" y="10" width="80" height="80" />
<image href="data:image/png;base64,..." x="42" y="42" width="16" height="16" />

The <symbol> element

<symbol id="card" viewBox="0 0 100 100"&gt;
            &lt;rect x="0" y="0" width="75" height="75" /&gt;
            &lt;path d="M50 90 C -15 40, 10 5, 50 30 C 90 5, 110 40, 50 90z" fill="rgba(255,0,0,1)" /&gt;
          &lt;/symbol&gt;
          &lt;symbol id="nested" viewBox="0 0 100 100"&gt;
            &lt;circle class="go" cx="50" cy="50" r="25" /&gt;
            &lt;use href="#card" x="50" y="50" width="15" height="15" /&gt;
            &lt;text x="33" y="48" font-size="12" stroke="none" &gt;nested&lt;/text&gt;
          &lt;/symbol>
nested

The <use> element

Once the symbol is in your document, you can reuse it anywhere else in your document with the <use> element.

<svg height="200" width="200"&gt;
            &lt;use href="#card" x="10%" y="10%" width="80%" height="80%" /&gt;
          &lt;/svg>

The <foreignObject> element

Use the foreignObject tag to insert HTML elements into an SVG.

HTML→SVG

Web Development Invoice

itemunitsprice
HTML5$20.00
CSS40 ft2$15.00
SVG1$15.00
White space$4000.00
Total$4050.00
A foreignObject containing <h1>, <p>, <button>, and <table> HTML elements.

About

SVG ZOO was created by N.E. Lilly to explore the amazing and wonderful complexity that can be found in Scalable Vector Graphics. Last modified on May 29, 2023.

Additional technical details are provided on the Colophon.

Connect