81
Introduction to SVG (Part 2) Prepared by K.M.SO Department of Computer Science ver. 2 last update: June 2004

Introduction to SVG (Part 2)

  • Upload
    slone

  • View
    30

  • Download
    1

Embed Size (px)

DESCRIPTION

Introduction to SVG (Part 2). Prepared by K.M.SO Department of Computer Science ver. 2 last update: June 2004. Document Structure – Grouping and referencing objects. Grouping Element(1). The 'g' element is the element for grouping and naming collections of drawing elements. - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to SVG  (Part 2)

Introduction to SVG (Part 2)

Prepared by K.M.SODepartment of Computer Science

ver. 2

last update: June 2004

Page 2: Introduction to SVG  (Part 2)

Document Structure –Grouping and referencing objects

Page 3: Introduction to SVG  (Part 2)

Grouping Element(1)

The 'g' element is the element for grouping and naming collections of drawing elements.

Enables to execute same operation on all items in the group.

Can be used in conjunction with <desc> and <title> elements to provide description and semantics about the group

Page 4: Introduction to SVG  (Part 2)

Grouping Element(2)

Each group can be given the id attribute for purposes of animation and re-usabilty.

Any ‘g’ element can contain other ‘g’ elements nested within it to an arbitrary depth.

Drawing element that is not contained in a ‘g’ element can be considered as a group of its own.

Page 5: Introduction to SVG  (Part 2)

Simple Grouping<svg width="5cm" height="5cm">

<g id="group1" style="fill:red"><rect x="1cm" y="1cm" width="1cm" height="1cm"/><rect x="3cm" y="1cm" width="1cm" height="1cm" /></g><g id="group2" style="fill:blue"><rect x="1cm" y="3cm" width="1cm" height="1cm" /><rect x="3cm" y="3cm" width="1cm" height="1cm" /></g></svg>

Page 6: Introduction to SVG  (Part 2)

Another Example (1/3)<svg width="240px" height="240px" viewBox="0 0 240 240"><g id="house" style="fill: none; stroke: black;"> <desc>House with door</desc> <rect x="6" y="50" width="60" height="60"/> <polyline points="6 50, 36 9, 66 50"/> <polyline points="36 110, 36 80, 50 80, 50 110"/></g><g id="man" style="fill: none; stroke: black;"> <desc>Male human</desc> <circle cx="85" cy="56" r="10"/> <line x1="85" y1="66" x2="85" y2="80"/> <polyline points="76 104, 85 80, 94 104" /> <polyline points="76 70, 85 76, 94 70" /></g><g id="woman" style="fill: none; stroke: black;"> <desc>Female human</desc> <circle cx="110" cy="56" r="10"/> <polyline points="110 66, 110 80, 100 90, 120 90, 110 80"/> <line x1="104" y1="104" x2="108" y2="90"/> <line x1="112" y1="90" x2="116" y2="104"/> <polyline points="101 70, 110 76, 119 70" /></g></svg>

Page 7: Introduction to SVG  (Part 2)

Another Example (2/3)<svg width="240px" height="240px" viewBox="0 0 240 240"><g id="house" style="fill: none; stroke: black;"> <desc>House with door</desc> <rect x="6" y="50" width="60" height="60"/> <polyline points="6 50, 36 9, 66 50"/> <polyline points="36 110, 36 80, 50 80, 50 110"/></g>

<g id="man" style="fill: none; stroke: black;"> <desc>Male human</desc> <circle cx="85" cy="56" r="10"/> <line x1="85" y1="66" x2="85" y2="80"/> <polyline points="76 104, 85 80, 94 104" /> <polyline points="76 70, 85 76, 94 70" /></g><g id="woman" style="fill: none; stroke: black;"> <desc>Female human</desc> <circle cx="110" cy="56" r="10"/> <polyline points="110 66, 110 80, 100 90, 120 90, 110 80"/> <line x1="104" y1="104" x2="108" y2="90"/> <line x1="112" y1="90" x2="116" y2="104"/> <polyline points="101 70, 110 76, 119 70" /></g></svg>

Page 8: Introduction to SVG  (Part 2)

Another Example (3/3)<svg width="240px" height="240px" viewBox="0 0 240 240"><g id="house" style="fill: none; stroke: black;"> <desc>House with door</desc> <rect x="6" y="50" width="60" height="60"/> <polyline points="6 50, 36 9, 66 50"/> <polyline points="36 110, 36 80, 50 80, 50 110"/></g><g id="man" style="fill: none; stroke: black;"> <desc>Male human</desc> <circle cx="85" cy="56" r="10"/> <line x1="85" y1="66" x2="85" y2="80"/> <polyline points="76 104, 85 80, 94 104" /> <polyline points="76 70, 85 76, 94 70" /></g>

<g id="woman" style="fill: none; stroke: black;"> <desc>Female human</desc> <circle cx="110" cy="56" r="10"/> <polyline points="110 66, 110 80, 100 90, 120 90,

110 80"/> <line x1="104" y1="104" x2="108" y2="90"/> <line x1="112" y1="90" x2="116" y2="104"/> <polyline points="101 70, 110 76, 119 70" /></g></svg>

Page 9: Introduction to SVG  (Part 2)

The use element(1)

The <use>’s purpose is to reuse defined container elements like <svg> and <g>

The 'use' element references another element and indicates that the graphical contents of that element is included/drawn at that given point in the document.

The ‘use’ element cannot reference entire files.

Page 10: Introduction to SVG  (Part 2)

The use element(2)

The 'use' element has the attributes x, y, width and height which are used to map the graphical contents of the referenced element onto a rectangular region within the current coordinate system.

Page 11: Introduction to SVG  (Part 2)

The use element(3)<svg width="240px" height="240px" viewBox="0 0 240 240">

<g id="house" style="fill: none; stroke: black;">

<rect x="6" y="50" width="60" height="60"/> <polyline points="6 50, 36 9, 66 50"/> <polyline points="36 110, 36 80, 50 80, 50 110"/></g>

<g id="man" style="fill: none; stroke: black;"><circle cx="85" cy="56" r="10"/><line x1="85" y1="66" x2="85" y2="80"/>

<polyline points="76 104, 85 80, 94 104" /><polyline points="76 70, 85 76, 94 70" /></g>

<g id="woman" style="fill: none; stroke: black;"> <circle cx="110" cy="56" r="10"/>

<polyline points="110 66, 110 80, 100 90, 120 90, 110 80"/><line x1="104" y1="104" x2="108" y2="90"/><line x1="112" y1="90" x2="116" y2="104"/><polyline points="101 70, 110 76, 119 70" />

</g>

<use xlink:href="#house" x="70" y="100"/>

<use xlink:href="#woman" x="-80" y="100"/>

<use xlink:href="#man" x="-30" y="100"/></svg>

Page 12: Introduction to SVG  (Part 2)

The use element(4)

The <use> element has an important advantage over a simple reference to an element:

It can apply transformations and style sheets over the used element by using the matching attributes of the ‘use’ element.

Page 13: Introduction to SVG  (Part 2)

The defs element(1)

The defs element is a container element for referenced elements.

When SVG elements are drawn into a visual media, the <defs> is not drawn with them.

Page 14: Introduction to SVG  (Part 2)

The defs element(2)<svg>

<defs><g id="house"> ... </g><g id="man"> ... </g><g id="woman"> ... </g>

<g id="couple"> <use xlink:href="#man" x="0" y="0"/> <use xlink:href="#woman" x="25" y="0"/></g></defs>……

<use xlink:href="#house" x="0" y="0" style="fill: #cfc;"/><use xlink:href="#couple" x="70" y="40"/><use xlink:href="#house" x="120" y="0" style="fill: #99f;"/><use xlink:href="#couple" x="190" y="40"/><use xlink:href="#woman" x="0" y="145"/><use xlink:href="#man" x="25" y="145"/><use xlink:href="#house" x="65" y="105" style="fill: #c00;"/></svg>

Page 15: Introduction to SVG  (Part 2)

References in SVG

SVG supports two types of references: Local URI reference – the referenced object is

found in the same document.

*Non-Local URI reference – the referenced

object is found in another document.

Page 16: Introduction to SVG  (Part 2)

Reuse elements from file

<use xlink:href=“AnotherFile.svg#woman”

x=“100” y=“200” /> (not supported in newest viewer)

Instead, you may try this one:

<image xlink:href=“AnotherFile.svg”

x=“100” y=“200” width=“100” height=“100”/>

Page 17: Introduction to SVG  (Part 2)

The Symbol element

Unlike the <g> element, a <symbol> is never display.

Symbols can specify viewBox and preserveAspectRatio attributes.

Page 18: Introduction to SVG  (Part 2)

Groups vs Symbols<svg width="200px" height="200px" viewBox="0 0 200 200"><defs><g id="octagon" style="stroke: black;"><polygon points=" 36 25, 25 36, 11 36, 0 25, 0 11, 11 0, 25 0, 36 11"/></g><symbol id="sym-octagon" style="stroke: black;" preserveAspectRatio="xMidYMid slice" viewBox="0 0 40 40"><polygon points="36 25, 25 36, 11 36, 0 25, 0 11, 11 0, 25 0, 36 11"/></symbol></defs><use xlink:href="#octagon" x="40" y="40" width="30" height="30“ style="fill: #c00;"/><use xlink:href="#octagon" x="80" y="40" width="40" height="60“ style="fill: #cc0;"/><use xlink:href="#sym-octagon" x="40" y="80" width="30" height="30" style="fill: #cfc;"/><use xlink:href="#sym-octagon" x="80" y="80" width="40" height="60" style="fill: #699;"/></svg>

Page 19: Introduction to SVG  (Part 2)

The image element(1)

The <image> indicates that the contents of a complete file are to be rendered into a given rectangle within the current user coordinate system

The <image> can refer to raster image files such as PNG or JPEG or to an SVG file.

Main attributes: “x”, “y”, “width”, “height” and “xlink:href”

Page 20: Introduction to SVG  (Part 2)

The image element(2)

If <image> references a raster image, then the raster image is fitted into the region specified by the x, y, width and height attributes such that the top/left corner of the raster image exactly aligns with coordinate (x,y), and the bottom/right corner of the raster image exactly aligns with coordinate (x+width,y+height).

When <image> references an SVG file, then the 'image' element establishes a new viewport - specified by x,y,width,height.

Page 21: Introduction to SVG  (Part 2)

The image element(3) Unlike <use>, <image> cannot reference

elements inside the SVG document. One can apply transformations and styles to

the <image> element through the matching attributes.

– nested transformations– clipping paths– alpha masks– filter effects– template objects and extensibility

Page 22: Introduction to SVG  (Part 2)

Include a JPEG image with SVG

(72,92)

Page 23: Introduction to SVG  (Part 2)

Include a JPEG image with SVG

<svg width="310px" height="310px"

viewBox="0 0 310 310">

<ellipse cx="154" cy="154" rx="150" ry="120" style="fill: #999999;"/>

<ellipse cx="152" cy="152" rx="150" ry="120" style="fill: #cceeff;"/>

<image xlink:href="kwanghwamun.jpg"

x="72" y="92" width="160" height="120"/> </svg>

Page 24: Introduction to SVG  (Part 2)

Jasc webDraw– A free 30-day trial copy is available at – www.jasc.com

(Time: <10mins)

Activity

SVG drawing tools

Page 25: Introduction to SVG  (Part 2)

Transformations(1)

A new user coordinate system can be established by specifying transformations in the form of a transform attribute on a group of graphical elements.

The transform attribute transforms all user space coordinates and lengths on the given element and all of its ancestors.

Transformations maybe nested and so have cummulative effect.

Page 26: Introduction to SVG  (Part 2)

Transformations(2)

2D transformations are represented using 3X3 matrices.

Common transformations:

translate(x,y) - establish a new coordinate system whose origin is at (x,y).

rotate(a) – rotates the coordinate system by a degrees around the origin.

scale(a,b) – scales the coordinate system – x axis by a and y axis by b.

Page 27: Introduction to SVG  (Part 2)

Transformations(3)

Translation Matrix:

Rotation Matrix:

Scaling Matrix:

Page 28: Introduction to SVG  (Part 2)

Transformations(4)

Page 29: Introduction to SVG  (Part 2)

Transformations(5)

SVG allows both to apply transformations as translate, rotate, scale or to apply a detailed transformation matrix.

Transformations in SVG are applied by the ‘transform’ element.

Page 30: Introduction to SVG  (Part 2)

Linear Transformations

<svg width="200px" height="200px" viewBox="0 0 200 200">

<g id="square">

<rect x="10" y="10" width="20" height="20"

style="fill: none; stroke:black; stroke-width: 2;"/>

</g>

<use xlink:href="#square" transform="translate(50,50)"/>

</svg>

Page 31: Introduction to SVG  (Part 2)

Scale Transformations (1)

<svg width="300" height="300">

<g id="square">

<rect x="10" y="10" width="20" height="20"

style="fill: none; stroke: black;"/>

</g>

<use xlink:href="#square" transform="scale(2)"/>

</svg>

Page 32: Introduction to SVG  (Part 2)

Scale Transformations (2)

<svg width="200px" height="200px"

viewBox="0 0 200 200">

<g id="square">

<rect x="10" y="10" width="20" height="20"

style="fill: none; stroke: black;"/>

</g>

<use xlink:href="#square" transform="scale(3, 1.5)"/>

</svg>

Page 33: Introduction to SVG  (Part 2)

Sequences of Transformations

tranformation (A, B) != tranformation (B, A)

tansform=“scale(2) translate(30, 20)”

is NOT equal to

tansform=“translate(30, 20) scale(2)”

Page 34: Introduction to SVG  (Part 2)

Rotation Transformations

Rotate the user coordinate by 45 degree. The center of rotation is the origin (0,0)

transform=“rotate(45)”

Rotate the user coordinate by 45 degree. The center of rotation is the origin (50,50)

transform=“rotate(45, 50, 50 )”

Page 35: Introduction to SVG  (Part 2)

Rotation Transformations

<svg width="200px" height="200px" viewBox="0 0 200 200">

<!-- axes -->

<polyline points="100 0, 0 0, 0 100" style="stroke: black; fill: none;"/>

<!-- normal and rotated square -->

<rect x="70" y="30" width="20" height="20" style="fill: gray;"/>

<rect x="70" y="30" width="20" height="20"

transform="rotate(45)" style="fill: black;"/>;

</svg>

Page 36: Introduction to SVG  (Part 2)

Skew Transformations<svg width="200px" height="200px" viewBox="0 0 200 200">

<!-- guide lines -->

<g style="stroke: gray; stroke-dasharray: 4 4;">

<line x1="0" y1="0" x2="200" y2="0"/><line x1="20" y1="0" x2="20" y2="90"/> <line x1="120" y1="0" x2="120" y2="90"/> </g>

<g transform="translate(20, 0)">

<g transform="skewX(30)">

<polyline points="50 0, 0 0, 0 50"

style="fill: none; stroke: black; stroke-width: 2;"/>

<text x="0" y="60">skewX</text>

</g></g>

<g transform="translate(120, 0)">

<g transform="skewY(30)"> <polyline points="50 0, 0 0, 0 50"

style="fill: none; stroke: black; stroke-width: 2;"/>

<text x="0" y="60">skewY</text>

</g></g>

</svg>

Page 37: Introduction to SVG  (Part 2)

RapidSVG– A professional SVG web site includes RapidSVG

download, demo and gallery– http://xstreamsoftware.com/demos_rsvg.htm

(Time: <15mins)

Activity

SVG drawing tools

Page 38: Introduction to SVG  (Part 2)

Paths(1)

Paths represent the outline of a shape which can be filled, stroked, used as a clipping path, or any combination of the three.

Paths are described by a set of points. The geometry of the path is defined in

terms of moveto (M m) , lineto (L l), and closepath (Z).

Path is represented in SVG by the ‘path’ element.

Page 39: Introduction to SVG  (Part 2)

Paths(2) <svg width="4cm" height="4cm" viewBox="0 0 400 400">

<rect x="1" y="1" width="398" height="398" style="fill:none; stroke:blue"/> <path d="M 100 100 L 300 100 L 200 300 z" style="fill:red; stroke:blue; stroke-width:3"/> </svg>

Page 40: Introduction to SVG  (Part 2)

Paths (move to and line to)<svg width="200px" height="200px"

viewBox="0 0 200 200">

<g transform="translate(20,20)">

<g style="stroke: black; fill: none;">

<path d="M 10 10 L 100 10"/>

<path d="M 10, 20 L 100, 20 L 100,50"/>

<path d="M 40 60, L 10 60, L 40 42.68,

M 60 60, L 90 60, L 60 42.68"/>

</g></g>

</svg>

Page 41: Introduction to SVG  (Part 2)

Paths (close path)<svg width="200px" height="200px"

viewBox="0 0 200 200">

<g transform="translate(20,20)">

<g style="stroke: black; fill: none;">

<path d="M 10 10, L 40 10, L 40 30, L 10 30, L 10 10"/>

<path d="M 60 10, L 90 10, L 90 30, L 60 30, Z"/>

<path d="M 40 60, L 10 60, L 40 42.68, Z

M 60 60, L 90 60, L 60 42.68, Z"/>

</g></g>

</svg>

Page 42: Introduction to SVG  (Part 2)

Paths shortcuts

Horizontal line (H h) Vertical line (V v) Multiple sets of coordinate<!– 6 paths draw the same diamond -->

<path d="M 30 30 L 55 5 L 80 30 L 55 55 Z"/>

<path d="M 30 30 L 55 5 80 30 55 55 Z"/>

<path d="M 30 30 55 5 80 30 55 55 Z"/>

<path d="m 30 30 l 25 -25 l 25 25 l -25 25 z"/>

<path d="m 30 30 l 25 -25 25 25 -25 25 z"/>

<path d="m 30 30 25 -25 25 25 -25 25 z"/>

(30,30)

Page 43: Introduction to SVG  (Part 2)

Elliptical Arc

Specify the elliptical Arc: (a) x- y-radii two point two possible ellipse

(b) two arcs < 180 degree;

(c) two arcs > 180 degree,

(d) drawn in the direction of incresing negative angle

(e) drawn in the direction of incresing postive angle

(f) x-axis-rotation of ellipse

Page 44: Introduction to SVG  (Part 2)
Page 45: Introduction to SVG  (Part 2)

Elliptical Arc

the paths used to draw the elliptical arcs in section (b) to (e)

<path d="M 125, 75 A100,50 0 0,0 225, 125"/><!– b -->

<path d="M 125, 75 A100,50 0 0,1 225, 125"/><!– c -->

<path d="M 125, 75 A100,50 0 1,0 225, 125"/><!– d -->

<path d="M 125, 75 A100,50 0 1,1 225, 125"/><!– e -->

Page 46: Introduction to SVG  (Part 2)

Sodipodi A free (GPL) vector-based drawing program

create native svg file format.– http://www.sodipodi.com/– GTK+ for Windows is required – http://www.dropline.net/gtk/index.php

(Time: <15mins)

Activity

SVG drawing tools

Page 47: Introduction to SVG  (Part 2)

Bezier Curves (1)

Quaratic Bezier Curve: Q x1 y1 x y

Q or q : command x1 y1 : control point x y : end point

<path d="M30 100 Q 80 30, 100 100, 130 65, 200 80"/>

(x,y)

(x1,y1)

current position

Page 48: Introduction to SVG  (Part 2)

Bezier Curves (2) Smooth Quaratic Bezier Curve: T x y

T or t : command (Reflection of previous control point )

x y : end point

<path d="M30 100 Q 80 30, 100 100 T 200 80"/>

(x,y)

Reflection of control point

Page 49: Introduction to SVG  (Part 2)

Bezier Curves (3)

Cubic Bezier Curve: C x1 y1 x2 y2 x y

C or c : command x1 y1 x2 y2 : control points x y : end point

<path d="M40 50 C 10 10, 140 10, 110 50"/>

Page 50: Introduction to SVG  (Part 2)
Page 51: Introduction to SVG  (Part 2)

Bezier Curves (4)

Smooth Cubic Bezier Curve: S x2 y2 x y

C or c : command x2 y2 : control points (x1 y1 from reflection of

previous control point )

x y : end point

<path d="M30 100 C 50 30, 70 50, 100 100

S 150 40, 200 80“ />

x2,y2

x,y

Reflection of control point

Page 52: Introduction to SVG  (Part 2)

SVGEditor– A online SVG web drawing tool demonstration– http://www.resource-solutions.de/svgeditor/

SVG viewBox– An example explores zooming with viewbox.– http://www.resource-solutions.de/svg/index.html

(Time: <10mins)

Activity

SVG drawing tools

Page 53: Introduction to SVG  (Part 2)

Patterns Pattern is a graphic object that is replicated

horizontally and vertically to fill another object. patternUnits

objectBoundingBox

userSpaceOnUse

patternContentUnits

Page 54: Introduction to SVG  (Part 2)

Tiles Pattern spaced by objectBoundingBox

<svg width="400px" height="200px" viewBox="0 0 400 200">

<pattern id="tile" x="0" y="0" width="20%" height="20%"

patternUnits="objectBoundingBox"><path d="M 0 0 Q 5 20 10 10 T 20 20"

style="stroke: black; fill: none;"/>

<path d="M 0 0 h 20 v 20 h -20 z"

style="stroke: gray; fill: none;"/>

</pattern>

</defs>

<rect x="20" y="20" width="100" height="100"

style="fill: url(#tile); stroke: black;"/>

<rect x="135" y="20" width="70" height="80"

style="fill: url(#tile); stroke: black;"/>

<rect x="220" y="20" width="150" height="130"

style="fill: url(#tile); stroke: black;"/>

</svg>

Page 55: Introduction to SVG  (Part 2)

Tiles Pattern spaced by userSpaceOnUse

<svg width="400px" height="200px" viewBox="0 0 400 200">

<pattern id="tile" x="0" y="0" width="20%" height="20%"

patternUnits="userSpaceOnUse"><path d="M 0 0 Q 5 20 10 10 T 20 20"

style="stroke: black; fill: none;"/>

<path d="M 0 0 h 20 v 20 h -20 z"

style="stroke: gray; fill: none;"/>

</pattern>

</defs>

<rect x="20" y="20" width="100" height="100"

style="fill: url(#tile); stroke: black;"/>

<rect x="135" y="20" width="70" height="80"

style="fill: url(#tile); stroke: black;"/>

<rect x="220" y="20" width="150" height="130"

style="fill: url(#tile); stroke: black;"/>

</svg>

Page 56: Introduction to SVG  (Part 2)

Tiles Pattern spaced by patternContentUnits

<svg width="400px" height="200px" viewBox="0 0 400 200"><defs>

<pattern id="tile” patternUnits="objectBoundingBox”

patternContentUnits="objectBoundingBox"

x="0" y="0" width=".2" height=".2"><path d="M 0 0 Q .05 .20 .10 .10 T .20 .20"

style="stroke: black; fill: none; stroke-width: 0.01;"/>

<path d="M 0 0 h 0.2 v 0.2 h-0.2z"

style="stroke: black; fill: none; stroke-width: 0.01;"/>

</pattern></defs>

<g transform="translate(20,20)">

<rect x="0" y="0" width="100" height="100"

style="fill: url(#tile); stroke: black;"/></g>

<g transform="translate(135,20)">

<rect x="0" y="0" width="70" height="80"

style="fill: url(#tile); stroke: black;"/></g>

<g transform="translate(220,20)">

<rect x="0" y="0" width="150" height="130"

style="fill: url(#tile); stroke: black;"/></g>

</svg>

Page 57: Introduction to SVG  (Part 2)

Tiles Pattern scaled with viewBox

<svg width="400px" height="200px" viewBox="0 0 400 200">

<defs>

<pattern id="tile" patternUnits="userSpaceOnUse"

x="0" y="0" width="20" height="20"

viewBox="0 0 150 150"><path d="M30 100 C 50 50, 70 20, 100 100, 110, 130, 45, 150, 65, 100"

style="stroke: black; stroke-width: 5; fill: none;"/>

</pattern>

</defs>

<rect x="20" y="20" width="100" height="100"

style="fill: url(#tile); stroke: black;"/></svg>

Page 58: Introduction to SVG  (Part 2)

Linear Gradient:

Radial Gradient:

Gradients

Page 59: Introduction to SVG  (Part 2)

Gradients LinearGradient (two-color)

<svg width="400px" height="200px" viewBox="0 0 400 200">

preserveAspectRatio="xMinYMin meet">

<defs>

<linearGradient id="two_hues">

<stop offset="0%" style="stop-color: #ffcc00;"/>

<stop offset="100%" style="stop-color: #0099cc;"/>

</linearGradient>

</defs>

<rect x="20" y="20" width="200" height="100"

style="fill: url(#two_hues); stroke: black;"/>

</svg>

Page 60: Introduction to SVG  (Part 2)

Gradients LinearGradient (three-color)

<svg width="400px" height="200px" viewBox="0 0 400 200">

preserveAspectRatio="xMinYMin meet">

<defs>

<linearGradient id="two_hues">

<stop offset="0%" style="stop-color: #ffcc00;"/>

<stop offset=”33.3%" style="stop-color: #cc6699;"/>

<stop offset="100%" style="stop-color: #6699cc;"/>

</linearGradient>

</defs>

<rect x="20" y="20" width="200" height="100"

style="fill: url(#three_hues); stroke: black;"/>

</svg>

Page 61: Introduction to SVG  (Part 2)

Gradients LinearGradient (w/ direction x1 y1 x2 y2)

Page 62: Introduction to SVG  (Part 2)

….

<linearGradient id="three_stops">

<stop offset="0%" style="stop-color: #ffcc00;"/>

<stop offset="33.3%" style="stop-color: #cc6699"/>

<stop offset="100%" style="stop-color: #66cc99;"/>

</linearGradient>

<linearGradient id="right_to_left” xlink:href="#three_stops"

x1="100%" y1="0%" x2="0%" y2="0%"/>

<linearGradient id="down" xlink:href="#three_stops"

x1="0%" y1="0%" x2="0%" y2="100%"/>

<linearGradient id="up" xlink:href="#three_stops"

x1="0%" y1="100%" x2="0%" y2="0%"/>

<linearGradient id="diagonal" xlink:href="#three_stops"

x1="0%" y1="0%" x2="100%" y2="100%"/>…...

Gradients

Page 63: Introduction to SVG  (Part 2)

Gradients radialGradient

<defs>

<radialGradient id="three_stops">

<stop offset="0%" style="stop-color: #f96;"/>

<stop offset="50%" style="stop-color: #9c9;"/>

<stop offset="100%" style="stop-color: #906;"/>

</radialGradient>

</defs>

<rect x="20" y="20" width="100" height="100"

style="fill: url(#three_stops); stroke: black;"/>

Page 64: Introduction to SVG  (Part 2)

Gradients Setting limits for a radialGradient

<defs>

<radialGradient id=”center_origin”

cx=“0” cy=“0” r=“100%”>

<stop offset="0%" style="stop-color: #f96;"/>

<stop offset="50%" style="stop-color: #9c9;"/>

<stop offset="100%" style="stop-color: #906;"/>

</radialGradient> </defs>

<rect x="20" y="20" width="100" height="100"

style="fill: url(# center_origin); stroke: black;"/>

Page 65: Introduction to SVG  (Part 2)

Gradients Setting focal point for a radialGradient

<defs>

<radialGradient id=”focal_set”

cx=“0” cy=“0” fx=“30%” fy=“30%” r=“100%”>

<stop offset="0%" style="stop-color: #f96;"/>

<stop offset="50%" style="stop-color: #9c9;"/>

<stop offset="100%" style="stop-color: #906;"/>

</radialGradient> </defs>

<rect x="20" y="20" width="100" height="100"

style="fill: url(#focal_set); stroke: black;"/>

Page 66: Introduction to SVG  (Part 2)

Gradients speadMethod ( pad, repeat, reflect )<svg width="400px" height="200px" viewBox="0 0 400 200"> preserveAspectRatio="xMinYMin meet">

<defs>

<radialGradient id="three_stops" cx="0%" cy="0%" r="70%"><stop offset="0%" style="stop-color: #f96;"/>

<stop offset="50%" style="stop-color: #9c9;"/>

<stop offset="100%" style="stop-color: #906;"/>

</radialGradient>

<radialGradient id="padded" xlink:href="#three_stops"

spreadMethod="pad"/>

<radialGradient id="repeated" xlink:href="#three_stops" spreadMethod="repeat"/>

<radialGradient id="reflected" xlink:href="#three_stops" spreadMethod="reflect"/>

</defs>

<rect x="20" y="20" width="100" height="100" style="fill: url(#padded); stroke: black;"/>

<rect x="130" y="20" width="100" height="100" style="fill: url(#repeated); stroke: black;"/>

<rect x="240" y="20" width="100" height="100" style="fill: url(#reflected); stroke: black;"/></svg>

Page 67: Introduction to SVG  (Part 2)

CGUI toolkit– object oriented ECMAScript library to create SVG based Custom

GUI elements inside a web browser

– http://homepage.usask.ca/~ctl271/cgui/index.shtml

(Time: <10mins)

Activity

SVG tools

Page 68: Introduction to SVG  (Part 2)

The text element

<text> is used to specify text that is rendered with other graphical elements.

This means that we can apply transformation, clipping, masking, and so to text.

Fonts are as specified in CSS2.

Page 69: Introduction to SVG  (Part 2)

<text> example

<svg width="10cm" height="3cm">

<desc>Example text01 - 'Hello, out there' in blue</desc>

<text x="2.5cm" y="1.5cm" style="font-family:Verdana;

font-size:16pt; fill:blue"> Hello, out there </text>

</svg>

Page 70: Introduction to SVG  (Part 2)

Text placement and outlining

<svg width="200px" height="200px"

viewBox="0 0 200 200">

<!-- guide lines -->

<path d="M 20 10, 20 120 M 10 30 100 30

M 10 70 100 70 M 10 110 100 110"

style="stroke: gray;"/>

<text x="20" y="30">Simplest Text</text>

<text x="20" y="70" style="stroke: black;"> Outlined/filled</text>

<text x="20" y="110" style="stroke: black;

stroke-width: 0.5; fill: none;">Outlined only</text></svg>

Page 71: Introduction to SVG  (Part 2)

Text weight, style, decoration and spacing

<svg width="350px" height="200px"

viewBox="0 0 350 200">

<g font-size="18pt">

<text x="20" y="20" style="font-weight:bold;">bold</text>

<text x="120" y="20" style="font-style:italic;">italic</text>

<text x="20" y="60" style="text-decoration:underline;">under</text>

<text x="120" y="60" style="text-decoration:overline;">over</text>

<text x="200" y="60" style="text-decoration:line-through;">through</text>

<text x="20" y="90" style="word-spacing: 10pt;">more word space</text>

<text x="20" y="120" style="word-spacing: -3pt;">less word space</text>

<text x="20" y="150" style="letter-spacing: 5pt;">wide letter space</text>

<text x="20" y="180" style="letter-spacing: -2pt;">narrow letter space</text>

</g>

</svg>

Page 72: Introduction to SVG  (Part 2)

Text Alignment

<svg width="200px" height="200px" viewBox="0 0 200 200">

<g style="font-size: 14pt;">

<path d="M 100 10 100 100" style="stroke: gray; fill: none;"/>

<text x="100" y="30" style="text-anchor: start">Start</text>

<text x="100" y="60" style="text-anchor: middle">Middle</text>

<text x="100" y="90" style="text-anchor: end">End</text></g>

</svg>

Page 73: Introduction to SVG  (Part 2)

The tspan element

<svg width="600px" height="100px" viewBox="0 0 600 100">

<text x="10" y="30" style="font-size:18pt;">

Switch among

<tspan style="font-style:italic">italic</tspan>

, normal, and

<tspan style="font-weight:bold">bold</tspan>

text.

</text></svg>

Page 74: Introduction to SVG  (Part 2)

Text on a Path

<defs>

<path id="curvepath"

d="M30 40 C 50 10, 70 10, 120 40 S 150 0, 200 40"

style="stroke: gray; fill: none;"/>

</defs>

<text style="font-size: 12;">

<textPath xlink:href="#curvepath">

Following a cubic B&#xe9;zier curve.

</textPath>

</text>

Page 75: Introduction to SVG  (Part 2)

Clipping to a Path<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN“ "http://www.w3.org/TR/2001/REC-SVG- 20010904/DTD/svg10.dtd"><svg width="200" height="200" viewBox="0 0 200 200"><defs><g id="cat"><circle cx="42" cy="57" r="30" style="stroke: black; fill: none;"/><circle cx="33" cy="48" r="3" style="stroke:black; fill:#999999;"/><circle cx="51" cy="48" r="3" style="stroke:black; fill:#999999;"/> <g id="whiskers"><line x1="45" y1="57" x2="81" y2="51" style="stroke: black;"/><line x1="45" y1="57" x2="81" y2="62" style="stroke: black;"/> </g><use xlink:href="#whiskers" transform="scale(-1 1) translate(-84 0)"/><!-- ears --><polyline points="64.8 37.2, 54 6, 42 27, 30 6, 19.2 37.2“ style="stroke: black; fill: none;"/><!-- mouth --><polyline points="21 66, 27 72, 57 72, 62 66“ style="stroke: black; fill: none;"/><!-- nose --> <path d="M 45 54 L 39 54 A 3 6 0 0 0 45 54“ style="stroke: black; fill: #cccccc;"/></g>

<clipPath id="rectClip"> <rect id="rect1" x="15" y="15" width="40" height="45" style="stroke: gray; fill: none;"/></clipPath></defs><!-- clip to rectangle -->

<use xlink:href="#cat" style="clip-path: url(#rectClip);"/><!-- for reference, show entire picture with clipping area outlined --><g transform="translate(100,0)"> <use xlink:href="#rect1"/> <!-- show clip rectangle --> <use xlink:href="#cat"/></g></svg>

Page 76: Introduction to SVG  (Part 2)

Masking a Graphic<svg width="350" height="200" viewBox="0 0 350 200"><defs>

<mask id="fullmask" x="0" y="0“ width="1“ height="1“ maskContentUnits="objectBoundingBox"> <rect x="0" y="0" width="1" height="1“ style="fill-opacity: 1.0; fill: white;"/></mask><mask id="three-fourths" x="0" y="0" width="1" height="1“ maskContentUnits="objectBoundingBox"><rect x="0" y="0" width="1" height="1“ style="fill-opacity: 0.75; fill: white;"/></mask><mask id="one-half" x="0" y="0" width="1" height="1“ maskContentUnits="objectBoundingBox"><rect x="0" y="0" width="1" height="1“ style="fill-opacity: 0.5; fill: white;"/></mask><mask id="one-fourth" x="0" y="0" width="1" height="1“ maskContentUnits="objectBoundingBox"><rect x="0" y="0" width="1" height="1“ style="fill-opacity: 0.25; fill: white;"/></mask></defs><g style="font-size: 14pt; text-anchor:middle; fill:black;">

<g style="mask: url(#fullmask);"><circle cx="35" cy="35" r="25"/><text x="35" y="80">100%</text></g><g style="mask: url(#three-fourths);"><circle cx="95" cy="35" r="25"/> <text x="95" y="80">75%</text></g><g style="mask: url(#one-half);"><circle cx="155" cy="35" r="25"/> <text x="155" y="80">50%</text></g><g style="mask: url(#one-fourth);"><circle cx="215" cy="35" r="25"/> <text x="215" y="80">25%</text></g></g></svg>

Page 77: Introduction to SVG  (Part 2)

Masking a Graphic (2)

Unmasked image Masked image

Page 78: Introduction to SVG  (Part 2)

Masking a Graphic (3)<svg width="350" height="350" viewBox="0 0 350 350">

<defs>

<radialGradient id="fade"><stop offset="0%" style="stop-color: white; stop-opacity: 1.0;"/>

<stop offset="85%" style="stop-color: white; stop-opacity: 0.5;"/>

<stop offset="100%" style="stop-color: white; stop-opacity: 0.0;"/>

</radialGradient>

<mask id="fademask">

<rect x="72" y="92" width="160" height="120"

style="fill: url(#fade);"/>

</mask></defs>

<image xlink:href="kwanghwamun.jpg" x="72" y="92“ width="160" height="120"

style="mask: url(#fademask);"/></svg>

Page 79: Introduction to SVG  (Part 2)

Treebuilder.de– pipes Game

– SVG CrashDown

– Connect

– http://www.treebuilder.de/games.asp

(Time: <10mins)

Activity

SVG Games

Page 80: Introduction to SVG  (Part 2)

Summary

Grouping and Use elements Image Transformations:

- translation, rotation, scaling, skew, sequences

Paths, Elliptical Arc, Bezier Curves Patterns and Gradients Text weight, style, placement and outlining, decoration and spacing Text on a Path Clipping to a Path Masking

Page 81: Introduction to SVG  (Part 2)

end of day 2