SVG Pros & Cons for Javascript developers

The good, bad and ugly of scripting SVG in the browser



Author: Tim Meadowcroft -- @schmerg


Extracted & expanded from a longer Intro to Scripting SVG presentation

Presentation written with Slidy2


Hit the space bar for next slide

Introduction - Overview

SVG Tips and Gotchas for javascript developers

Quick history - Standards

What are all these versions, what should I target

Just work to SVG 1.1 2nd Edition

Quick history - Browser Support

Where will this work

Desktop

Mobile / Tablet

After 10 years, OK on "modern" desktops (meaning not IE 6,7,8)

Mobile limited to iOS 4+, Android 4 or Chrome on Android (beta)

Basic characteristics

What does it offer me

Fully integrated DOM for raster & vector graphics including text

Static (Declarative) Features

Intro to supported concepts and some idea of depth of functionality

You can achieve a lot before you need to start scripting

Declarative building blocks

Constructs for composition within SVG

Build composable structures that can be manipulated as a single item

Quick examples #1

Basic composition: shapes and text

<svg version="1.1" width='700px' height='250px' 
     xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <rect x="1" y="1" width="699" height="249" style="fill: #eeffee; stroke: blue;" />
  <g id="textBox" style='cursor: pointer;'>
    <circle id='textCirc' cx='100' cy='100' r='75px' fill='yellow' opacity='0.5'
            onclick='document.getElementById("textCirc").setAttribute("fill", "orange")'/>
    <text x='100px' y='100px' style='fill: blue; font-weight: bold; font-size:20px;'>
      <tspan>Text on top</tspan>
    </text>
  </g>
  <g id="textBox2">
    <text x='450px' y='50px' style='fill: blue; font-weight: bold; font-size:20px;'>
      <tspan>Text under transparency</tspan>
    </text>
    <rect x='445px' y='25px' width='150px' height='50px' style='opacity: 0.5;' fill='yellow'/>
  </g>
  <use x='400' y='-25' xlink:href='#textBox' transform="rotate(20), scale(0.75,0.75)"/>
</svg>
Text on top Text under transparency

Quick examples #2

Text on a path from http://www.w3.org/TR/SVG/text.html

<?xml version="1.0" standalone="no"?>
<svg width="15cm" height="4.5cm" viewBox="0 0 1000 250" version="1.1"
     xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <path id="MyPath"
          d="M 100 200 
             C 200 100 300   0 400 100
             C 500 200 600 300 700 200
             C 800 100 900 100 900 100" />
  </defs>

  <use xlink:href="#MyPath" fill="none" stroke="red"  />

  <text font-family="Verdana" font-size="42.5" fill="blue" >
    <textPath xlink:href="#MyPath">We go up, then we go down, then up again</textPath>
  </text>

  <rect x="1" y="1" width="998" height="298" fill="none" stroke="blue" stroke-width="2" />
</svg>
Example toap01 - simple text on a path We go up, then we go down, then up again

Quick examples #3

Gradients from http://www.w3.org/TR/SVG/pservers.html

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="8cm" height="4cm" viewBox="0 0 800 400" version="1.1" xmlns="http://www.w3.org/2000/svg">
  <desc>Example lingrad01 - fill a rectangle using a linear gradient paint server</desc>
  <defs>
    <linearGradient id="MyGradient">
      <stop offset="5%"  stop-color="#F60" />
      <stop offset="50%" stop-color="#FFF" />
      <stop offset="95%" stop-color="#FF6" />
    </linearGradient>
    <filter xmlns="http://www.w3.org/2000/svg" id="desaturate">
      <feColorMatrix type='saturate' values='0.4' />
    </filter>
  </defs>
 
  <rect fill="none" stroke="blue" x="1" y="1" width="798" height="398"/>
 
  <!-- The rectangle is filled using a linear gradient paint server -->
  <rect fill="url(#MyGradient)" stroke="black" stroke-width="5"  
        x="100" y="50" width="600" height="100"/>
  <rect fill="url(#MyGradient)" stroke="black" stroke-width="5"  
        filter="url(#desaturate)" 
        x="100" y="200" width="600" height="100"/>
</svg>
Example lingrad01 - fill a rectangle using a linear gradient paint server

Scripting the DOM

Changing the document

Essentially the same but beware HTML DOM assumptions

Comparison to Canvas

What does SVG do for you compared to Canvas

Fixed-price all-you-can-eat is expensive if you're just snacking

Even free pizza is useless unless you like the pizza on offer

Techniques

Some tricks of doing basic stuff in Canvas and SVG

Fighting the SVG DOM / redraw-loop / events? » Canvas

Writing Canvas scene graphs / hit detection / sprite libraries? » SVG

Good bits

What features make life easy

Bad bits or missing bits

What should I be careful of

Hard/ugly bits

What will be frustrating

Libraries / wrappers

Making life easier

Personal Recommendations

Some basic guidance

Hybrids

Possibilities of hybrid solutions?

References

Forget the reference tomes, where's the meat