Scalable Vector Graphics (SVG)
An Introduction
Filip van Laenen
Chief Engineer
Computas
Agenda
• Part I – Introduction
  – What's SVG?
  – Why use SVG?
  – Tools
  – Sample art work
• Part II – Code-along
  – Examples and exercises




                             2
About Me
Filip.van.Laenen@computas.com
          f.a.vanlaenen@ieee.org
                   @filipvanlaenen
My Background
• Java, (Smalltalk)
• (Perl), Ruby
• XML
 – XSLT
 – XSD
 – Namespaces
• HTML
 – CSS



                      4
Introduction to SVG
                  History of SVG
                    What's SVG?
                 Why use SVG?
 Related and Competing Standards
History of SVG
• 1999: Version 1.0
 – PGML (Precision Graphics Markup Language)
 – VML (Vector Markup Language)
• 2001: Version 1.0 recommendation
• 2003: Version 1.1 recommendation
 – SVG Mobile: SVG Basic & SVG Tiny
• 2008: SVG Tiny 1.2 recommendation
• 2011: SVG 1.1 SE recommendation
• Version 1.2 working draft
• SVG 2.0 in progress
                                               6
What's SVG?
• Scalable Vector Graphics
 – Vector based graphics on the net
 – No quality loss after scaling
• XML based
 – Integrates with DOM and XSL
• Animation on every element




                                      7
Why SVG?
• Scaling




            8
Why SVG? (cont'd)
• Smaller files
 – Better compression than JPEG or PNG
 – Even better with SVGZ
• Open standard, part of HTML 5
 – Pure XML
 – Can be read and manipulated by many tools
 – Extensible
• Text based
 – Text manipulation
 – Indexing
                                               9
Why SVG? (cont'd)
• In-line in HTML possible
 – Shared CSS
• Animation and interaction
 – SMIL
 – ECMAScript
 – Other scripting languages




                               10
Why not SVG?
• Take a guess…




                  11
Browser Support for SVG
• Konqueror
• Google Chrome
• Opera
• Safari
• Firefox (since version 4.0)
• Internet Explorer 9




                                12
Related and Competing Standards
• Flash
• VML  †


• XAML   †


• Silverlight (†)


• JPEG
  – Photos
• PNG (GIF )    †

  – Pixel based drawings


                              13
Competing Non-Standards
• Visio diagrammes
• Powerpoint slides




                          14
Creating SVG Files
                Manually
         Programmatically
Manual Production
• Drawing tools
 – Inkscape
   •http://www.inkscape.org/
 – Export from other drawing tools
• Any XML editor
• Any text editor
• Any browser to view/check the result




                                         16
Programmatical Production
• API
 – SVG library based
 – XML
 – Text
• Structure
 – Template based
 – From scratch




                            17
Sample Art Work
Learn More about SVG
• W3C Recommendation
 – http://www.w3.org/TR/SVG/Overview.html
• SVG Basics Tutorial
 – http://www.svgbasics.com/index.html


• Open Clip Art Library
 – http://www.openclipart.org/
• Inkscape
 – http://www.inkscape.org/

                                            23
Questions?
Code-along
Tools
• Text editor
 – (XML editor)
• Browser




                  26
Topics
• Circle, ellipse, rect, opacity, rx, ry
• Line, polygon
• Polyline, path, marker
• Text, tspan
• Stroke
• Linear and radial gradient
• Patterns
• Groups
• Filters
• Animation                                27
Code-along
Part 1 – Circle, ellipse, rect, opacity, rx, ry
Circle
<?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="150" height="100" version="1.1"
      xmlns="http://www.w3.org/2000/svg">

  <circle cx="100" cy="50" r="40" stroke="black"
     stroke-width="2" fill="red"/>

</svg>



                                                       29
Ellipse
<?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="150" height="100" version="1.1"
      xmlns="http://www.w3.org/2000/svg">

  <ellipse cx="100" cy="50" rx="40" ry="30"
     stroke="black" stroke-width="2" fill="red"/>

</svg>



                                                       30
Rectangle
<?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="100%" height="100%" version="1.1"
      xmlns="http://www.w3.org/2000/svg">

   <rect width="300" height="100"
      style="fill:rgb(0,0,255);stroke-width:1;
             stroke:rgb(0,0,0)"/>

</svg>




                                                       31
Opacity
<?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="100%" height="100%" version="1.1"
      xmlns="http://www.w3.org/2000/svg">

   <rect x="20" y="20" width="250" height="250"
      style="fill:blue;stroke:pink;
             stroke-width:5;
             fill-opacity:0.1;stroke-opacity:0.9"/>

</svg>


                                                       32
Rounded Corners
<?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="100%" height="100%" version="1.1"
      xmlns="http://www.w3.org/2000/svg">

   <rect x="20" y="20" rx="20" ry="20" width="250"
      height="100"
      style="fill:red;stroke:black;
             stroke-width:5;opacity:0.5"/>

</svg>


                                                       33
Excercise #1
• Yellow circle, green border
• Blue rectangle, 50% opaque
• Red rectangle, rounded corners, black border




                                                 34
Code-along
Part 2 – Line, polygon
Line
<?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="100%" height="100%" version="1.1"
      xmlns="http://www.w3.org/2000/svg">

   <line x1="0" y1="0" x2="300" y2="300"
      stroke="red" stroke-width="2"/>

</svg>




                                                       36
Polygon
<?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="100%" height="100%" version="1.1"
      xmlns="http://www.w3.org/2000/svg">

  <polygon points="220,100 300,210 170,250"
     fill="#cccccc" stroke="#000000"
     stroke-width="1"/>

</svg>



                                                       37
Excercise #2
• Cumulative diagramme
• X and Y axis
• Blue, yellow and red data




                              38
Code-along
Part 3 – Polyline, path, marker
Polyline
<?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="100%" height="100%" version="1.1"
      xmlns="http://www.w3.org/2000/svg">

   <polyline
      points="0,0 0,20 20,20 20,40 40,40 40,60"
      fill="none" stroke="red" stroke-width="2"/>

</svg>




                                                       40
Polyline (cont'd)
<?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="100%" height="100%" version="1.1"
      xmlns="http://www.w3.org/2000/svg">

   <polyline
      points="0,0 0,20 20,20 20,40 40,40 40,60"
      fill="blue" stroke="red" stroke-width="2"/>

</svg>




                                                       41
Path
<?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="400" height="400" version="1.1"
      xmlns="http://www.w3.org/2000/svg">

   <path d="M250 150 L150 350 L350 350 Z" />

</svg>




                                                       42
Path Commands
• M/m: Moveto
• L/l: Lineto
• H/h: Horizontal lineto
• V/v: Vertical lineto
• C/c: Curveto
• S/s: Smooth curveto
• Q/q: Quadratic Bézier curve
• T/t: Smooth quadratic Bézier curveto
• A/a: Elliptical arc
• Z/z: Closepath                         43
Elliptic Arc
• Rx and ry: radius
• X-axis-rotation: Rotation of the X axis
• Large-arc-flag:
 – 0 if less than 180°
 – 1 if more than 180°
• Sweep-flag:
 – 0 if negative direction
 – 1 if positive direction
• X and y: end points

                                            44
Eliptic Arc (cont'd)
<svg width="700" height="500" version="1.1"
       xmlns="http://www.w3.org/2000/svg">
   <path d="M300 200 A200 150 30 0 0 400 300"
       fill="none" stroke="black" stroke-width="2"/>
   <path d="M300 200 A200 150 30 0 1 400 300"
       fill="none" stroke="red" stroke-width="2"/>
   <path d="M300 200 A200 150 30 1 0 400 300"
       fill="none" stroke="green" stroke-width="2"/>
   <path d="M300 200 A200 150 30 1 1 400 300"
       fill="none" stroke="blue" stroke-width="2"/>
</svg>




                                                       45
Marker
<svg width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg">
…
    <path d="M 200 250 L 700 100 L 900 350 L 1200 400"
       fill="none" stroke="black" stroke-width="50"
       marker-start="url(#StartMarker)"
       marker-mid="url(#MidMarker)"
       marker-end="url(#EndMarker)"/>
</svg>




                                                    46
Marker Definition
<svg width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg">
    <defs>
       <marker id="StartMarker" viewBox="0 0 12 12"
         refX="12" refY="6"
         markerWidth="3" markerHeight="3"
         stroke="green" stroke-width="2"
         fill="none" orient="auto">
              <circle cx="6" cy="6" r="5"/>
      </marker>
        …
    </defs>
…
                                                  47
Excercise #3.1
• Same as exercise #2, but line diagramme




                                            48
Excercise #3.2
• Same as exercise #3.1, but with markers




                                            49
Excercise #3.3
• Pie diagramme




                  50
Code-along
 Part 4 – Text, tspan
Text
<svg width="150" height="60" version="1.1"
         xmlns="http://www.w3.org/2000/svg">
   <text x="10" y="25" fill="navy" font-size="15">
         Lorem ipsum
   </text>
   <text x="10" y="25" dx="5" dy="15" fill="red"
            font-size="18">
         Dolor sit amet
   </text>
</svg>



                                                   52
Tspan
<svg width="150" height="60" version="1.1"
         xmlns="http://www.w3.org/2000/svg">
   <text x="30" y="25" fill="navy" font-size="15">
         <tspan>
            Lorem ipsum
         </tspan>
         <tspan dx="-50" dy="15" font-style="italic">
            Dolor sit amet
         </tspan>
   </text>
</svg>

                                                   53
Text Style
• Font-family
• Font-size
• Font-style: normal, italic or oblique
• Font-variant: normal, small-caps
• Font-weight: normal, bold, bolder, lighter, …
• Text-anchor: start, middle, end
• Text-decoration: none, underline, overline, …



                                                  54
Excercise #4
• Pie diagramme with labels




                              55
Code-along
   Part 5 – Stroke
Stroke
• Stroke-width
• Stroke-linecap: butt, round, square
• Stroke-linejoin: miter, round, bevel
• Stroke-miterlimit
• Stroke-dasharray
• Stroke-dashoffset
• Stroke-opacity



                                         58
Excercise #5
• Same as exercise #3.1
 – Blue line dashed
 – Red line half transparent




                               59
Code-along
Part 6 – Linear and radial gradients
Linear Gradient
<svg width="600" height="400" version="1.1"
         xmlns="http://www.w3.org/2000/svg">
   <defs>
         <linearGradient id="MyGradient">
            <stop offset="0%" stop-color="orange"/>
            <stop offset="100%" stop-color="yellow"/>
         </linearGradient>
   </defs>
   <rect x="50" y="50" width="500" height="300"
         fill="url(#MyGradient)" stroke="black"
         stroke-width="2"/>
</svg>
                                                      62
Linear Gradient (cont'd)
<svg width="600" height="400" version="1.1"
         xmlns="http://www.w3.org/2000/svg">
   <defs>
         <linearGradient id="MyGradient">
            <stop offset="0%" stop-color="orange"/>
            <stop offset="50%" stop-color="yellow"/>
            <stop offset="100%" stop-color="green"/>
         </linearGradient>
   </defs>
   <rect x="50" y="50" width="500" height="300"
         fill="url(#MyGradient)" stroke="black"
         stroke-width="2"/>
</svg>                                                 63
Linear Gradient (cont'd)
<svg width="600" height="400" version="1.1"
         xmlns="http://www.w3.org/2000/svg">
   <defs>
         <linearGradient id="MyGradient">
            <stop offset="20%" stop-color="orange"/>
            <stop offset="50%" stop-color="yellow"/>
            <stop offset="80%" stop-color="green"/>
         </linearGradient>
   </defs>
   <rect x="50" y="50" width="500" height="300"
         fill="url(#MyGradient)" stroke="black"
         stroke-width="2"/>
</svg>                                                 64
Linear Gradient (cont'd)
<svg width="600" height="400" version="1.1"
      xmlns="http://www.w3.org/2000/svg">
   <defs>
      <linearGradient id="MyGradient"
               x1="0%" y1="0%" x2="100%" y2="100%">
            <stop offset="0%" stop-color="orange"/>
            <stop offset="50%" stop-color="yellow"/>
            <stop offset="100%" stop-color="green"/>
      </linearGradient>
   </defs>



                                                       65
Linear Gradient (cont'd)
<svg width="600" height="400" version="1.1"
      xmlns="http://www.w3.org/2000/svg">
   <defs>
      <linearGradient id="MyGradient"
               x1="0%" y1="0%" x2="100%" y2="50%">
            <stop offset="0%" stop-color="orange"/>
            <stop offset="50%" stop-color="yellow"/>
            <stop offset="100%" stop-color="green"/>
      </linearGradient>
   </defs>



                                                       66
Linear Gradient (cont'd)
<svg width="600" height="400" version="1.1"
      xmlns="http://www.w3.org/2000/svg">
   <defs>
      <linearGradient id="MyGradient"
               x1="0%" y1="0%" x2="100%" y2="50%">
            <stop offset="0%" stop-color="orange"/>
            <stop offset="50%" stop-color="yellow"/>
            <stop offset="100%" stop-color="green"
               stop-opacity=".3"/>
      </linearGradient>
   </defs>

                                                       67
Excercise #6.1
• Same as exercise #2, but with gradients




                                            68
Radial Gradient
<svg width="600" height="400" version="1.1"
         xmlns="http://www.w3.org/2000/svg">
   <defs>
         <radialGradient id="MyGradient"
               cx="50%" cy="50%" r="50%">
            <stop offset="0%" stop-color="orange"/>
            <stop offset="100%" stop-color="yellow"/>
         </radialGradient>
   </defs>
   <circle cx="300" cy="200" r="180"
stroke="black"
         stroke-width="2" fill="url(#MyGradient)"/>
</svg>                                                69
Radial Gradient (cont'd)
<svg width="600" height="400" version="1.1"
         xmlns="http://www.w3.org/2000/svg">
   <defs>
         <radialGradient id="MyGradient" cx="50%"
               cy="50%" r="50%" fx="25%" fy="25%">
            <stop offset="0%" stop-color="orange"/>
            <stop offset="100%" stop-color="yellow"/>
         </radialGradient>
   </defs>
   <circle cx="300" cy="200" r="180"
stroke="black"
         stroke-width="2" fill="url(#MyGradient)"/>
</svg>                                                70
Excercise #6.2
• Same as exercise #4, but with gradients
 – Use gradientUnits="userSpaceOnUse"




                                            71
Just Mentioning
    Patterns and Groups
Patterns




           73
Groups
• Logical unit
• Common attributes
 – Color
 – Opacity
 – Fill, stroke, gradients
• Attributes can be overriden a lower level




                                              74
Code-Along
   Part 7 – Filters
Filters
• Primitives
 – Lighting
 – Blur
 – Color tranformations
 – Displacement
 – Turbulence
• Filter mathematics
 – Composition
 – Blending
 – Addition
                          76
Filter
<svg width="600" height="400" version="1.1"
         xmlns="http://www.w3.org/2000/svg">
   <filter id="f1" width="150%" height="150%">
         <feOffset result="offOut" in="SourceGraphic"
            dx="10" dy="10"/>
         <feBlend in="SourceGraphic" in2="offOut"
            mode="normal"/>
   </filter>
   <rect filter="url(#f1)" x="40" y="40"
         rx="40" ry="40" width="400" height="200"
         style="fill:red;stroke:black;
            stroke-width:5;opacity:0.5"/>
</svg>                                              77
Filter (cont'd)
  <filter id="f2" width="150%" height="150%">
     <feOffset result="offOut" in="SourceGraphic"
        dx="10" dy="10"/>
     <feGaussianBlur result="blurOut" in="offOut"
        stdDeviation="5"/>
     <feBlend in="SourceGraphic" in2="blurOut"
        mode="normal"/>
  </filter>




                                                 78
Filter Sources
• Result from another filter
• SourceGraphic
• SourceAlpha
• BackgroundImage
• BackgroundAlpha
• FillPaint
• StrokePaint



                               79
Filter (cont'd)
  <filter id="f3" width="150%" height="150%">
     <feOffset result="offOut" in="SourceAlpha"
        dx="10" dy="10"/>
     <feGaussianBlur result="blurOut" in="offOut"
        stdDeviation="5"/>
     <feBlend in="SourceGraphic" in2="blurOut"
        mode="normal"/>
  </filter>




                                                  80
Filter (cont'd)
  <filter id="f4" width="150%" height="150%">
     <feOffset result="offOut" in="SourceGraphic"
        dx="10" dy="10"/>
     <feColorMatrix result="matrixOut" in="offOut"
        type="matrix"
        values="0.2 0 0 0 0
                0 0.2 0 0 0
                0 0 0.2 0 0
                0 0 0 1 0"/>
     <feGaussianBlur result="blurOut"
        in="matrixOut" stdDeviation="5"/>
     <feBlend in="SourceGraphic" in2="blurOut"
        mode="normal"/>
  </filter>                                          81
Color Transformations
• Matrix:
 – Transformation per color channel (r, g, b and α)
 – 0 is black (no color)




• Alternatives:
 – Saturate, HueRotate, LuminanceToAlpha
                                                      82
Code-Along
 Part 8 – Animation
Animation
• Animation elements
• Scripting
 – ECMAScript a.o.
• SMIL
 – Synchronized Multimedia Integration Language




                                                  84
Animate
<circle cx="100" cy="50" stroke="green"
      stroke-width="2" fill="yellow">
   <animate attributeName="r" attributeType="XML"
      begin="0s" dur="9s" from="20" to="60"
      fill="freeze"/>
</circle>
<rect x="20" y="30" width="200" height="100"
      fill="blue" opacity="0.5">
   <animate attributeName="opacity"
      attributeType="XML" begin="0s" dur="9s"
      fill="remove" from="0.2" to="1"/>
</rect>
                                                    85
AnimateTransform
<rect x="20" y="30" width="200" height="100"
      fill="blue">
   <animateTransform attributeName="transform"
      attributeType="XML" type="scale"
      from="1" to="2" dur="5s" fill="freeze"/>
</rect>
<rect x="10" y="70" width="100" height="100"
      fill="red">
   <animateTransform attributeName="transform"
      attributeType="XML" type="rotate"
      from="0" to="360" dur="5s"
      repeatCount="indefinite"/>
</rect>                                          86
OnClick
<rect x="10" y="70" width="100" height="100"
     fill="red"
     onclick="evt.target.setAttribute('fill','green')"
/>




                                                         87
OnLoad
<svg … onload="StartAnimation(evt)">
  <script type="application/ecmascript"><![CDATA[
     …
     function StartAnimation(evt) {
         blue_rect =
evt.target.ownerDocument.getElementById("BlueRect");
         blue_rect.setAttribute("transform",
           "scale(" + scalefactor + ")");
     …
     }
  ]]></script>
  <rect id="BlueRect" x="20" y="30"
     width="20" height="10" fill="blue"/>

                                                       88
Topics Not Covered
Topics Not Covered
• Transform and viewBox
• Clipping, masking and compositing
• Fonts
• Glyphs
• Linking
• Metadata
• Extensibility



                                      90
Learn More about SVG
• W3C Recommendation
 – http://www.w3.org/TR/SVG/Overview.html
• SVG Basics Tutorial
 – http://www.svgbasics.com/index.html


• Open Clip Art Library
 – http://www.openclipart.org/
• Inkscape
 – http://www.inkscape.org/

                                            91
Questions?
           Comments?
Filip.van.Laenen@computas.com
          f.a.vanlaenen@ieee.org
                   @filipvanlaenen
Thanks!
Filip.van.Laenen@computas.com
          f.a.vanlaenen@ieee.org
                   @filipvanlaenen

SVG (Devoxx 2011, 2011-NOV-14)

  • 1.
    Scalable Vector Graphics(SVG) An Introduction Filip van Laenen Chief Engineer Computas
  • 2.
    Agenda • Part I– Introduction – What's SVG? – Why use SVG? – Tools – Sample art work • Part II – Code-along – Examples and exercises 2
  • 3.
    About Me Filip.van.Laenen@computas.com f.a.vanlaenen@ieee.org @filipvanlaenen
  • 4.
    My Background • Java,(Smalltalk) • (Perl), Ruby • XML – XSLT – XSD – Namespaces • HTML – CSS 4
  • 5.
    Introduction to SVG History of SVG What's SVG? Why use SVG? Related and Competing Standards
  • 6.
    History of SVG •1999: Version 1.0 – PGML (Precision Graphics Markup Language) – VML (Vector Markup Language) • 2001: Version 1.0 recommendation • 2003: Version 1.1 recommendation – SVG Mobile: SVG Basic & SVG Tiny • 2008: SVG Tiny 1.2 recommendation • 2011: SVG 1.1 SE recommendation • Version 1.2 working draft • SVG 2.0 in progress 6
  • 7.
    What's SVG? • ScalableVector Graphics – Vector based graphics on the net – No quality loss after scaling • XML based – Integrates with DOM and XSL • Animation on every element 7
  • 8.
  • 9.
    Why SVG? (cont'd) •Smaller files – Better compression than JPEG or PNG – Even better with SVGZ • Open standard, part of HTML 5 – Pure XML – Can be read and manipulated by many tools – Extensible • Text based – Text manipulation – Indexing 9
  • 10.
    Why SVG? (cont'd) •In-line in HTML possible – Shared CSS • Animation and interaction – SMIL – ECMAScript – Other scripting languages 10
  • 11.
    Why not SVG? •Take a guess… 11
  • 12.
    Browser Support forSVG • Konqueror • Google Chrome • Opera • Safari • Firefox (since version 4.0) • Internet Explorer 9 12
  • 13.
    Related and CompetingStandards • Flash • VML † • XAML † • Silverlight (†) • JPEG – Photos • PNG (GIF ) † – Pixel based drawings 13
  • 14.
    Competing Non-Standards • Visiodiagrammes • Powerpoint slides 14
  • 15.
    Creating SVG Files Manually Programmatically
  • 16.
    Manual Production • Drawingtools – Inkscape •http://www.inkscape.org/ – Export from other drawing tools • Any XML editor • Any text editor • Any browser to view/check the result 16
  • 17.
    Programmatical Production • API – SVG library based – XML – Text • Structure – Template based – From scratch 17
  • 18.
  • 23.
    Learn More aboutSVG • W3C Recommendation – http://www.w3.org/TR/SVG/Overview.html • SVG Basics Tutorial – http://www.svgbasics.com/index.html • Open Clip Art Library – http://www.openclipart.org/ • Inkscape – http://www.inkscape.org/ 23
  • 24.
  • 25.
  • 26.
    Tools • Text editor – (XML editor) • Browser 26
  • 27.
    Topics • Circle, ellipse,rect, opacity, rx, ry • Line, polygon • Polyline, path, marker • Text, tspan • Stroke • Linear and radial gradient • Patterns • Groups • Filters • Animation 27
  • 28.
    Code-along Part 1 –Circle, ellipse, rect, opacity, rx, ry
  • 29.
    Circle <?xml version="1.0" standalone="no"?> <!DOCTYPEsvg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="150" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg"> <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red"/> </svg> 29
  • 30.
    Ellipse <?xml version="1.0" standalone="no"?> <!DOCTYPEsvg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="150" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg"> <ellipse cx="100" cy="50" rx="40" ry="30" stroke="black" stroke-width="2" fill="red"/> </svg> 30
  • 31.
    Rectangle <?xml version="1.0" standalone="no"?> <!DOCTYPEsvg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"> <rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:1; stroke:rgb(0,0,0)"/> </svg> 31
  • 32.
    Opacity <?xml version="1.0" standalone="no"?> <!DOCTYPEsvg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"> <rect x="20" y="20" width="250" height="250" style="fill:blue;stroke:pink; stroke-width:5; fill-opacity:0.1;stroke-opacity:0.9"/> </svg> 32
  • 33.
    Rounded Corners <?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="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"> <rect x="20" y="20" rx="20" ry="20" width="250" height="100" style="fill:red;stroke:black; stroke-width:5;opacity:0.5"/> </svg> 33
  • 34.
    Excercise #1 • Yellowcircle, green border • Blue rectangle, 50% opaque • Red rectangle, rounded corners, black border 34
  • 35.
    Code-along Part 2 –Line, polygon
  • 36.
    Line <?xml version="1.0" standalone="no"?> <!DOCTYPEsvg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"> <line x1="0" y1="0" x2="300" y2="300" stroke="red" stroke-width="2"/> </svg> 36
  • 37.
    Polygon <?xml version="1.0" standalone="no"?> <!DOCTYPEsvg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"> <polygon points="220,100 300,210 170,250" fill="#cccccc" stroke="#000000" stroke-width="1"/> </svg> 37
  • 38.
    Excercise #2 • Cumulativediagramme • X and Y axis • Blue, yellow and red data 38
  • 39.
    Code-along Part 3 –Polyline, path, marker
  • 40.
    Polyline <?xml version="1.0" standalone="no"?> <!DOCTYPEsvg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"> <polyline points="0,0 0,20 20,20 20,40 40,40 40,60" fill="none" stroke="red" stroke-width="2"/> </svg> 40
  • 41.
    Polyline (cont'd) <?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="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"> <polyline points="0,0 0,20 20,20 20,40 40,40 40,60" fill="blue" stroke="red" stroke-width="2"/> </svg> 41
  • 42.
    Path <?xml version="1.0" standalone="no"?> <!DOCTYPEsvg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="400" height="400" version="1.1" xmlns="http://www.w3.org/2000/svg"> <path d="M250 150 L150 350 L350 350 Z" /> </svg> 42
  • 43.
    Path Commands • M/m:Moveto • L/l: Lineto • H/h: Horizontal lineto • V/v: Vertical lineto • C/c: Curveto • S/s: Smooth curveto • Q/q: Quadratic Bézier curve • T/t: Smooth quadratic Bézier curveto • A/a: Elliptical arc • Z/z: Closepath 43
  • 44.
    Elliptic Arc • Rxand ry: radius • X-axis-rotation: Rotation of the X axis • Large-arc-flag: – 0 if less than 180° – 1 if more than 180° • Sweep-flag: – 0 if negative direction – 1 if positive direction • X and y: end points 44
  • 45.
    Eliptic Arc (cont'd) <svgwidth="700" height="500" version="1.1" xmlns="http://www.w3.org/2000/svg"> <path d="M300 200 A200 150 30 0 0 400 300" fill="none" stroke="black" stroke-width="2"/> <path d="M300 200 A200 150 30 0 1 400 300" fill="none" stroke="red" stroke-width="2"/> <path d="M300 200 A200 150 30 1 0 400 300" fill="none" stroke="green" stroke-width="2"/> <path d="M300 200 A200 150 30 1 1 400 300" fill="none" stroke="blue" stroke-width="2"/> </svg> 45
  • 46.
    Marker <svg width="100%" height="100%"version="1.1" xmlns="http://www.w3.org/2000/svg"> … <path d="M 200 250 L 700 100 L 900 350 L 1200 400" fill="none" stroke="black" stroke-width="50" marker-start="url(#StartMarker)" marker-mid="url(#MidMarker)" marker-end="url(#EndMarker)"/> </svg> 46
  • 47.
    Marker Definition <svg width="100%"height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"> <defs> <marker id="StartMarker" viewBox="0 0 12 12" refX="12" refY="6" markerWidth="3" markerHeight="3" stroke="green" stroke-width="2" fill="none" orient="auto"> <circle cx="6" cy="6" r="5"/> </marker> … </defs> … 47
  • 48.
    Excercise #3.1 • Sameas exercise #2, but line diagramme 48
  • 49.
    Excercise #3.2 • Sameas exercise #3.1, but with markers 49
  • 50.
  • 51.
    Code-along Part 4– Text, tspan
  • 52.
    Text <svg width="150" height="60"version="1.1" xmlns="http://www.w3.org/2000/svg"> <text x="10" y="25" fill="navy" font-size="15"> Lorem ipsum </text> <text x="10" y="25" dx="5" dy="15" fill="red" font-size="18"> Dolor sit amet </text> </svg> 52
  • 53.
    Tspan <svg width="150" height="60"version="1.1" xmlns="http://www.w3.org/2000/svg"> <text x="30" y="25" fill="navy" font-size="15"> <tspan> Lorem ipsum </tspan> <tspan dx="-50" dy="15" font-style="italic"> Dolor sit amet </tspan> </text> </svg> 53
  • 54.
    Text Style • Font-family •Font-size • Font-style: normal, italic or oblique • Font-variant: normal, small-caps • Font-weight: normal, bold, bolder, lighter, … • Text-anchor: start, middle, end • Text-decoration: none, underline, overline, … 54
  • 55.
    Excercise #4 • Piediagramme with labels 55
  • 57.
    Code-along Part 5 – Stroke
  • 58.
    Stroke • Stroke-width • Stroke-linecap:butt, round, square • Stroke-linejoin: miter, round, bevel • Stroke-miterlimit • Stroke-dasharray • Stroke-dashoffset • Stroke-opacity 58
  • 59.
    Excercise #5 • Sameas exercise #3.1 – Blue line dashed – Red line half transparent 59
  • 61.
    Code-along Part 6 –Linear and radial gradients
  • 62.
    Linear Gradient <svg width="600"height="400" version="1.1" xmlns="http://www.w3.org/2000/svg"> <defs> <linearGradient id="MyGradient"> <stop offset="0%" stop-color="orange"/> <stop offset="100%" stop-color="yellow"/> </linearGradient> </defs> <rect x="50" y="50" width="500" height="300" fill="url(#MyGradient)" stroke="black" stroke-width="2"/> </svg> 62
  • 63.
    Linear Gradient (cont'd) <svgwidth="600" height="400" version="1.1" xmlns="http://www.w3.org/2000/svg"> <defs> <linearGradient id="MyGradient"> <stop offset="0%" stop-color="orange"/> <stop offset="50%" stop-color="yellow"/> <stop offset="100%" stop-color="green"/> </linearGradient> </defs> <rect x="50" y="50" width="500" height="300" fill="url(#MyGradient)" stroke="black" stroke-width="2"/> </svg> 63
  • 64.
    Linear Gradient (cont'd) <svgwidth="600" height="400" version="1.1" xmlns="http://www.w3.org/2000/svg"> <defs> <linearGradient id="MyGradient"> <stop offset="20%" stop-color="orange"/> <stop offset="50%" stop-color="yellow"/> <stop offset="80%" stop-color="green"/> </linearGradient> </defs> <rect x="50" y="50" width="500" height="300" fill="url(#MyGradient)" stroke="black" stroke-width="2"/> </svg> 64
  • 65.
    Linear Gradient (cont'd) <svgwidth="600" height="400" version="1.1" xmlns="http://www.w3.org/2000/svg"> <defs> <linearGradient id="MyGradient" x1="0%" y1="0%" x2="100%" y2="100%"> <stop offset="0%" stop-color="orange"/> <stop offset="50%" stop-color="yellow"/> <stop offset="100%" stop-color="green"/> </linearGradient> </defs> 65
  • 66.
    Linear Gradient (cont'd) <svgwidth="600" height="400" version="1.1" xmlns="http://www.w3.org/2000/svg"> <defs> <linearGradient id="MyGradient" x1="0%" y1="0%" x2="100%" y2="50%"> <stop offset="0%" stop-color="orange"/> <stop offset="50%" stop-color="yellow"/> <stop offset="100%" stop-color="green"/> </linearGradient> </defs> 66
  • 67.
    Linear Gradient (cont'd) <svgwidth="600" height="400" version="1.1" xmlns="http://www.w3.org/2000/svg"> <defs> <linearGradient id="MyGradient" x1="0%" y1="0%" x2="100%" y2="50%"> <stop offset="0%" stop-color="orange"/> <stop offset="50%" stop-color="yellow"/> <stop offset="100%" stop-color="green" stop-opacity=".3"/> </linearGradient> </defs> 67
  • 68.
    Excercise #6.1 • Sameas exercise #2, but with gradients 68
  • 69.
    Radial Gradient <svg width="600"height="400" version="1.1" xmlns="http://www.w3.org/2000/svg"> <defs> <radialGradient id="MyGradient" cx="50%" cy="50%" r="50%"> <stop offset="0%" stop-color="orange"/> <stop offset="100%" stop-color="yellow"/> </radialGradient> </defs> <circle cx="300" cy="200" r="180" stroke="black" stroke-width="2" fill="url(#MyGradient)"/> </svg> 69
  • 70.
    Radial Gradient (cont'd) <svgwidth="600" height="400" version="1.1" xmlns="http://www.w3.org/2000/svg"> <defs> <radialGradient id="MyGradient" cx="50%" cy="50%" r="50%" fx="25%" fy="25%"> <stop offset="0%" stop-color="orange"/> <stop offset="100%" stop-color="yellow"/> </radialGradient> </defs> <circle cx="300" cy="200" r="180" stroke="black" stroke-width="2" fill="url(#MyGradient)"/> </svg> 70
  • 71.
    Excercise #6.2 • Sameas exercise #4, but with gradients – Use gradientUnits="userSpaceOnUse" 71
  • 72.
    Just Mentioning Patterns and Groups
  • 73.
  • 74.
    Groups • Logical unit •Common attributes – Color – Opacity – Fill, stroke, gradients • Attributes can be overriden a lower level 74
  • 75.
    Code-Along Part 7 – Filters
  • 76.
    Filters • Primitives –Lighting – Blur – Color tranformations – Displacement – Turbulence • Filter mathematics – Composition – Blending – Addition 76
  • 77.
    Filter <svg width="600" height="400"version="1.1" xmlns="http://www.w3.org/2000/svg"> <filter id="f1" width="150%" height="150%"> <feOffset result="offOut" in="SourceGraphic" dx="10" dy="10"/> <feBlend in="SourceGraphic" in2="offOut" mode="normal"/> </filter> <rect filter="url(#f1)" x="40" y="40" rx="40" ry="40" width="400" height="200" style="fill:red;stroke:black; stroke-width:5;opacity:0.5"/> </svg> 77
  • 78.
    Filter (cont'd) <filter id="f2" width="150%" height="150%"> <feOffset result="offOut" in="SourceGraphic" dx="10" dy="10"/> <feGaussianBlur result="blurOut" in="offOut" stdDeviation="5"/> <feBlend in="SourceGraphic" in2="blurOut" mode="normal"/> </filter> 78
  • 79.
    Filter Sources • Resultfrom another filter • SourceGraphic • SourceAlpha • BackgroundImage • BackgroundAlpha • FillPaint • StrokePaint 79
  • 80.
    Filter (cont'd) <filter id="f3" width="150%" height="150%"> <feOffset result="offOut" in="SourceAlpha" dx="10" dy="10"/> <feGaussianBlur result="blurOut" in="offOut" stdDeviation="5"/> <feBlend in="SourceGraphic" in2="blurOut" mode="normal"/> </filter> 80
  • 81.
    Filter (cont'd) <filter id="f4" width="150%" height="150%"> <feOffset result="offOut" in="SourceGraphic" dx="10" dy="10"/> <feColorMatrix result="matrixOut" in="offOut" type="matrix" values="0.2 0 0 0 0 0 0.2 0 0 0 0 0 0.2 0 0 0 0 0 1 0"/> <feGaussianBlur result="blurOut" in="matrixOut" stdDeviation="5"/> <feBlend in="SourceGraphic" in2="blurOut" mode="normal"/> </filter> 81
  • 82.
    Color Transformations • Matrix: – Transformation per color channel (r, g, b and α) – 0 is black (no color) • Alternatives: – Saturate, HueRotate, LuminanceToAlpha 82
  • 83.
    Code-Along Part 8– Animation
  • 84.
    Animation • Animation elements •Scripting – ECMAScript a.o. • SMIL – Synchronized Multimedia Integration Language 84
  • 85.
    Animate <circle cx="100" cy="50"stroke="green" stroke-width="2" fill="yellow"> <animate attributeName="r" attributeType="XML" begin="0s" dur="9s" from="20" to="60" fill="freeze"/> </circle> <rect x="20" y="30" width="200" height="100" fill="blue" opacity="0.5"> <animate attributeName="opacity" attributeType="XML" begin="0s" dur="9s" fill="remove" from="0.2" to="1"/> </rect> 85
  • 86.
    AnimateTransform <rect x="20" y="30"width="200" height="100" fill="blue"> <animateTransform attributeName="transform" attributeType="XML" type="scale" from="1" to="2" dur="5s" fill="freeze"/> </rect> <rect x="10" y="70" width="100" height="100" fill="red"> <animateTransform attributeName="transform" attributeType="XML" type="rotate" from="0" to="360" dur="5s" repeatCount="indefinite"/> </rect> 86
  • 87.
    OnClick <rect x="10" y="70"width="100" height="100" fill="red" onclick="evt.target.setAttribute('fill','green')" /> 87
  • 88.
    OnLoad <svg … onload="StartAnimation(evt)"> <script type="application/ecmascript"><![CDATA[ … function StartAnimation(evt) { blue_rect = evt.target.ownerDocument.getElementById("BlueRect"); blue_rect.setAttribute("transform", "scale(" + scalefactor + ")"); … } ]]></script> <rect id="BlueRect" x="20" y="30" width="20" height="10" fill="blue"/> 88
  • 89.
  • 90.
    Topics Not Covered •Transform and viewBox • Clipping, masking and compositing • Fonts • Glyphs • Linking • Metadata • Extensibility 90
  • 91.
    Learn More aboutSVG • W3C Recommendation – http://www.w3.org/TR/SVG/Overview.html • SVG Basics Tutorial – http://www.svgbasics.com/index.html • Open Clip Art Library – http://www.openclipart.org/ • Inkscape – http://www.inkscape.org/ 91
  • 92.
    Questions? Comments? Filip.van.Laenen@computas.com f.a.vanlaenen@ieee.org @filipvanlaenen
  • 93.
    Thanks! Filip.van.Laenen@computas.com f.a.vanlaenen@ieee.org @filipvanlaenen