SVG for Javascript developers

A short intro for scripting use



Author: Tim Meadowcroft (@schmerg)



Presentation written with Slidy2



Hit the space bar for next slide

Introduction - Overview

What a javascript developer should know about SVG today

Quick history - Standards

What are all these versions, what should I target

Quick history - Browser Support

Where will this work

Basic characteristics

What does it offer me

Features

Intro to supported concepts and some idea of depth of functionality

Quick examples #1

Basic composition: shapes and text

<svg version="1.1" width='700px' height='600px' 
     xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <g id="textBox">
    <circle id='textCirc' cx='100' cy='100' r='75px' fill='yellow' opacity='0.25'
            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='100px' y='300px' style='fill: blue; font-weight: bold; font-size:20px;'>
      <tspan>Text under transparency</tspan>
    </text>
    <rect x='95px' y='275px' width='150px' height='50px' style='opacity: 0.5;' fill='yellow'/>
  </g>

  <use x='400' y='0' xlink:href='#textBox' transform="rotate(20)"/>

  <rect x="1" y="1" width="699" height="599" fill="none" stroke="blue" />

</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"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="12cm" height="3.6cm" viewBox="0 0 1000 300" 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>
  <desc>Example toap01 - simple text on a path</desc>
 
  <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>
 
  <!-- Show outline of canvas using 'rect' element -->
  <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>
  <g>
    <defs>
      <linearGradient id="MyGradient">
        <stop offset="5%" stop-color="#F60" />
        <stop offset="95%" stop-color="#FF6" />
      </linearGradient>
    </defs>
 
    <!-- Outline the drawing area in blue -->
    <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="100" width="600" height="200"/>
  </g>
</svg>
Example lingrad01 - fill a rectangle using a linear gradient paint server

Declarative building blocks

Constructs for composition within SVG

Scripting the DOM

Changing the document

Comparison to Canvas (pt1)

What about HTML5 and Canvas?

Comparison to canvas (pt2)

What does SVG do for you over and above canvas

Techniques

Some tricks of doing basic stuff in Canvas and 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

References

Forget the reference tomes, where's the meat