Cracking the SVG Code

Photo by jcapaldi on Flickr used under the Creative Commons License.

Me

You

What we'll cover...

Why SVG?

Browser Support

Can I Use?

Inline Browser Support

Our Example

What is familiar from HTML?

Development History (abbreviated)

SVG Tag Attributes

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="209px" height="110px" x="0" y="0" viewBox="0 0 209 110">
  <title>A Simple Rectangle</title>
  <desc>A gray rectangle with a black border that is wider than it is tall.</desc>

  <a xlink:href="index.html">
    <rect x="20" y="20" height="100" width="200" rx="5" ry="5" style="fill: turquoise; stroke: #333333;"/>
  </a>

</svg>

Name Spacing

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"></svg>

Title & Description

<svg>
  <title>A Simple Rectangle</title>
  <desc>A gray rectangle with a black border that is wider than it is tall.</desc>
</svg>

Viewport

<svg width="209px" height="110px" viewBox="0 0 209 110"></svg>

SVG Coordinate System

<svg viewBox="0 0 209 110"></svg>

The rest of those attributes...

Everything else before the shapes is pretty much useless.

Shapes!

Stroke = Border
Fill = Background Color

Rectangle

<svg viewBox="-1 -1 202 102">
  <rect width="200" height="100" rx="5" ry="5" fill="turquoise" stroke="#333333">
</svg>

Circle

<svg viewBox="0 0 102 102">
  <circle cx="51" cy="51" r="50" fill="turquoise" stroke="#333333">
</svg>

Ellipse

<svg viewBox="0 25 102 52">
  <ellipse cx="51" cy="51" rx="50" ry="25" fill="turquoise" stroke="#333333">
</svg>

Line

<svg viewBox="0 0 60 30">
  <line x1="5" y1="5" x2="50" y2="25" stroke="#333333">
</svg>

Polyline

<svg viewBox="-1 -1 52 52">
  <polyline points="0,0 50,0 50,50" fill="turquoise" stroke="#333333">
</svg>

Polygon

<svg viewBox="-1 -1 52 52">
  <polygon points="0,0 50,0 50,50" fill="turquoise" stroke="#333333">
</svg>

The Virtual Pen

Path

<svg viewBox="0 -2 139 243">
  <path d="M50,50
          L100,100
          M110,110
          L100,0
          A30,50 0 1,0 100,100
          Z"
        fill="none" stroke="#333333">
</svg>

Path

M or m = moveto (x,y)
L or l = lineto (x,y)
A or a = arc (rx,ry x-axis-rotation large-arc-flag,sweep-flag)
C or c = curveto (Cubic Bezier Curve)
S or s = curveto (Smooth or Shorthand Cubic Bezier Curve)
Z or z = closepath

Arc, An elliptical curve

A50,50 0 0,1 100,100
rx,ry x-axis-rotation large-arc-flag,sweep-flag x,y

Cubic Bezier Curve

C x1,y1 x2,y2 x,y
S x2,y2 x,y

Draw Time!

What’s up with the decimals and dashes?

Linear Gradients

<svg width="600" height="200" >
  <defs>
    <linearGradient id="cool-gradient"
                    x1="0%" y1="0%"
                    x2="0%" y2="100%"
                    spreadMethod="pad">
      <stop offset="0%" stop-color="black" stop-opacity="1"/>
      <stop offset="100%" stop-color="turquoise" stop-opacity="1"/>
    </linearGradient>
  </defs>
  <rect x="10" y="10" width="600" height="200" fill="url(#cool-gradient)"/>
</svg>

Gradient Attributes

Gradient Attributes (cont.)

Radial Gradients

<svg width="500" height="500" viewBox="0 0 120 120">
  <defs>
    <radialGradient id="radial-gradient"
                      fx="5%" fy="5%" r="65%"
                      spreadMethod="pad">
      <stop offset="0%" stop-color="turquoise"/>
      <stop offset="100%" stop-color="black"/>
    </radialGradient>
  </defs>

  <circle fill="url(#radial-gradient)" cx="60" cy="60" r="50"/>
</svg>

Radial Gradient Unique Attributes

Stroke attributes

Animating Stroke Dash

In Browser Action!

Resources