Professional Documents
Culture Documents
Introduction to SVG
SVG is a language for describing 2D-graphics and graphical applications in XML.
What is SVG?
• SVG stands for Scalable Vector Graphics
• SVG is used to define vector-based graphics for the Web
• SVG defines the graphics in XML format
• SVG graphics do NOT lose any quality if they are zoomed or resized
• Every element and every attribute in SVG files can be animated
• SVG is a W3C recommendation
• SVG integrates with other W3C standards such as the DOM and XSL
• SVG is a W3C Recommendation
Advantages of using SVG over other image formats (like JPEG and GIF) are:
• SVG files can be read and modified by a large range of tools (e.g. notepad)
• SVG files are smaller and more compressible than JPEG and GIF images
• SVG images are scalable
• SVG images can be printed with high quality at any resolution
• SVG images are zoomable (and the image can be zoomed without degradation)
• Text in SVG is selectable and searchable (excellent for making maps)
• SVG works with Java technology
• SVG is an open standard
• SVG files are pure XML
SVG Example
SVG is written in XML.
SVG Example
The following example is an example of a simple SVG file. SVG files must be saved with an .svg
extension:<?xml version="1.0" standalone="no"?>
1
"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">
<circle cx="100" cy="50" r="40" stroke="black"
stroke-width="2" fill="red"/>
</svg>
Code explanation:
The first line contains the XML declaration. Notice the standalone attribute! This attribute specifies whether
the SVG file "stands alone", or contains a reference to an external file.
standalone="no" means that the SVG document has a reference to an external file - in this case, the DTD.
The second and the third line refer to the external SVG DTD. The DTD is located at
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd". The DTD resides at the W3C and it contains all
allowable SVG elements.
The SVG code begins with the <svg> element, which consists of the opening <svg> tag and the closing
</svg> tag. This is the root element. The width and height attributes set the width and height of the SVG
document. The version attribute defines the SVG version to be used and the xmlns attribute defines the
SVG namespace.
The SVG <circle> element is used to create a circle. The cx and cy attributes define the x and y coordinates
of the center of the circle. If cx and cy are omitted, the circle's center is set to (0, 0). The r attribute defines
the radius of the circle.
The stroke and stroke-width attributes control how the outline of a shape appears. We set the outline of the
circle to a 2px wide, black "border".
The fill attribute refers to the color inside a shape. We set the fill color to red.
The closing </svg> tag closes the root SVG element and the document.
Syntax:
<embed src="rect.svg" width="300" height="100"
type="image/svg+xml"
pluginspage="http://www.adobe.com/svg/viewer/install/" />
Note: The pluginspage attribute points to an URL for the plugin download.
Tip: Internet Explorer supports an additional attribute, wmode="transparent", that let the HTML page
background shine through.
The <object> tag is an HTML4 standard tag and is supported in all newer browsers. The disadvantage is
that it does not allow scripting.
Note: If you have installed the latest version of Adobe SVG Viewer, SVG files will not work when using the
<object> tag (at least not in Internet Explorer)!
Syntax:
2
<object data="rect.svg" width="300" height="100"
type="image/svg+xml"
codebase="http://www.adobe.com/svg/viewer/install/" />
Note: The codebase attribute points to an URL for the plugin download.
Syntax:
<iframe src="rect.svg" width="300" height="100">
</iframe>
I Wish....
It would be great if we could add SVG elements directly into the HTML code, only by referring to the SVG
namespace, like this:<html
xmlns:svg="http://www.w3.org/2000/svg">
<body>
<p>This is an HTML paragraph</p>
<svg:svg width="300" height="100" version="1.1" >
<svg:circle cx="100" cy="50" r="40" stroke="black"
stroke-width="2" fill="red" />
</svg:svg>
</body>
</html>
SVG <rect>
SVG has some predefined shape elements that can be used and manipulated by developers.
SVG Shapes
SVG has some predefined shape elements that can be used and manipulated by developers:
Rectangle <rect>
Circle <circle>
Ellipse <ellipse>
Line <line>
Polyline <polyline>
Polygon <polygon>
Path <path>
The following chapters will explain each element, starting with the rect element.
The <rect> Tag
The <rect> tag is used to create a rectangle and variations of a rectangle shape.
To understand how this works, copy the following code into Notepad and save the file as "rect1.svg". Place
the file in your Web directory:<?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>
Code explanation:
• The width and height attributes of the rect element define the height and the width of the rectangle
• The style attribute is used to define CSS properties
• The CSS fill property defines the fill color of the rectangle (either an rgb value, a color name, or a
hexadecimal value)
3
• The CSS stroke-width property defines the width of the border of the rectangle
• The CSS stroke property defines the color of the border of the rectangle
Let's look at another example that contains some new attributes:<?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>
Code explanation:
The x attribute defines the left position of the rectangle (e.g. x="0" places the rectangle 0 pixels from the left
of the browser window)
The y attribute defines the top position of the rectangle (e.g. y="0" places the rectangle 0 pixels from the top
of the browser window)
The CSS fill-opacity property defines the opacity of the fill color (legal range: 0 to 1)
The CSS stroke-opacity property defines the opacity of the stroke color (legal range: 0 to 1)
</svg>
Code explanation:
The CSS opacity property defines the opacity value for the whole element (legal range: 0 to 1)
Code explanation:
The rx and the ry attributes rounds the corners of the rectangle
SVG <circle>
• The <circle> tag is used to create a circle.
• The <circle> Tag
• The <circle> tag is used to create a circle.
Copy the following code into Notepad and save the file as "circle1.svg". Place the file in your Web
directory:<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
4
"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">
<circle cx="100" cy="50" r="40" stroke="black"
stroke-width="2" fill="red"/>
</svg>
Code explanation:
The cx and cy attributes define the x and y coordinates of the center of the circle. If cx and cy are omitted,
the circle's center is set to (0, 0)
The r attribute defines the radius of the circle
SVG <ellipse>
The <ellipse> tag is used to create an ellipse.
Copy the following code into Notepad and save the file as "ellipse1.svg". Place the file in your Web
directory:<?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">
<ellipse cx="300" cy="150" rx="200" ry="80"
style="fill:rgb(200,100,50);
stroke:rgb(0,0,100);stroke-width:2"/>
</svg>
Code explanation:
• The cx attribute defines the x coordinate of the center of the ellipse
• The cy attribute defines the y coordinate of the center of the ellipse
• The rx attribute defines the horizontal radius
• The ry attribute defines the vertical radius
The following example combines two ellipses (one yellow and one white):
<?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">
<ellipse cx="240" cy="100" rx="220" ry="30"
style="fill:yellow"/>
<ellipse cx="220" cy="100" rx="190" ry="20"
style="fill:white"/>
</svg>
5
SVG <line>
The <line> tag is used to create a line.
Code explanation:
• The x1 attribute defines the start of the line on the x-axis
• The y1 attribute defines the start of the line on the y-axis
• The x2 attribute defines the end of the line on the x-axis
• The y2 attribute defines the end of the line on the y-axis
SVG <polygon>
The <polygon> tag is used to create a graphic that contains at least three sides.
Code explanation:
The points attribute defines the x and y coordinates for each corner of the polygon
SVG <polyline>
The <polyline> tag is used to create any shape that consists of only straight lines.
6
The <polyline> tag is used to create any shape that consists of only straight lines.
Copy the following code into Notepad and save the file as "polyline1.svg". Place the file in your Web
directory:
<?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"
style="fill:white;stroke:red;stroke-width:2"/>
</svg>
SVG <path>
The <path> tag is used to define a path.
Note: All of the commands above can also be expressed with lower letters. Capital letters means absolutely
positioned, lower cases means relatively positioned.
Copy the following code into Notepad and save the file as "path1.svg". Place the file in your Web directory:
<?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">
<path d="M250 150 L150 350 L350 350 Z" />
</svg>
The example above defines a path that starts at position 250 150 with a line to position 150 350 then from
there a line to 350 350 and finally closing the path back to 250 150.
The following example creates a spiral:
<?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">
7
C211 300 186 274 156 274"
style="fill:white;stroke:red;stroke-width:2"/>
</svg>
Complex? YES!!!! Because of the complexity involved in drawing paths it is highly recommended to use an
SVG editor to create complex graphics.
SVG Filters
SVG Filters is used to add special effects to shapes and text.
Gaussian Blur
The <filter> tag is used to define an SVG filter. The <filter> tag has a required id attribute which identifies
which filter will be applied to the graphic.
The <filter> tag must be nested within a <defs> tag. The <defs> tag is short for definitions and it allows
definition of special elements such as filter.
Copy the following code into Notepad and save the file as "filter1.svg". Place the file in your Web directory:
<?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">
<defs>
<filter id="Gaussian_Blur">
<feGaussianBlur in="SourceGraphic" stdDeviation="3" />
</filter>
</defs>
Code explanation:
8
The id attribute of the <filter> tag defines a unique name for the filter (the same filter can be used by many
elements in the document)
The filter:url(#Gaussian_Blur) property is used to link an element to a filter. The # character must be used
when linking to the filters id
The filter effect is defined with the <feGaussianBlur> tag. The fe prefix is used for all filters
The stdDeviation attribute of the <feGaussianBlur> tag defines the amount of the blur
The in="SourceGraphic" part defines that the effect is created from the whole image
Copy the following code into Notepad and save the file as "linear1.svg". Place the file in your Web directory:
<?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">
<defs>
<linearGradient id="orange_red" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(255,255,0);
stop-opacity:1"/>
<stop offset="100%" style="stop-color:rgb(255,0,0);
stop-opacity:1"/>
</linearGradient>
</defs>
<ellipse cx="200" cy="190" rx="85" ry="55"
style="fill:url(#orange_red)"/>
</svg>
9
Code explanation:
The id attribute of the <linearGradient> tag defines a unique name for the gradient
The fill:url(#orange_red) property links the ellipse element to the gradient
The x1, x2, y1,y2 attributes of the <linearGradient> tag define the starting and ending position of the
gradient
The color range for a gradient can be composed of two or more colors. Each color is specified with a <stop>
tag. The offset attribute is used to define where the gradient color begin and end
View example (Horizontal gradient)
Another example:<?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">
<defs>
<linearGradient id="orange_red" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" style="stop-color:rgb(255,255,0);
stop-opacity:1"/>
<stop offset="100%" style="stop-color:rgb(255,0,0);
stop-opacity:1"/>
</linearGradient>
</defs>
<ellipse cx="200" cy="190" rx="85" ry="55"
style="fill:url(#orange_red)"/>
</svg>
VG Gradients - Radial
An SVG Gradient must be defined within a <defs> tag.
Radial Gradients
The <radialGradient> tag is used to define an SVG radial gradient.
The <radialGradient> tag must be nested within a <defs> tag. The <defs> tag is short for definitions and it
allows definition of special elements such as gradients.
Copy the following code into Notepad and save the file as "radial1.svg". Place the file in your Web
directory:<?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">
<defs>
<radialGradient id="grey_blue" cx="50%" cy="50%" r="50%"
fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(200,200,200);
stop-opacity:0"/>
<stop offset="100%" style="stop-color:rgb(0,0,255);
stop-opacity:1"/>
</radialGradient>
</defs>
<ellipse cx="230" cy="200" rx="110" ry="100"
style="fill:url(#grey_blue)"/>
</svg>
Code explanation:
The id attribute of the <radialGradient> tag defines a unique name for the gradient
The fill:url(#grey_blue) property links the ellipse element to the gradient
The cx, cy and r attributes define the outermost circle and the fx and fy define the innermost circle
The color range for a gradient can be composed of two or more colors. Each color is specified with a <stop>
tag. The offset attribute is used to define where the gradient color begin and end
View example
10
Another example:<?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">
<defs>
<radialGradient id="grey_blue" cx="20%" cy="40%" r="50%"
fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(200,200,200);
stop-opacity:0"/>
<stop offset="100%" style="stop-color:rgb(0,0,255);
stop-opacity:1"/>
</radialGradient>
</defs>
<ellipse cx="230" cy="200" rx="110" ry="100"
style="fill:url(#grey_blue)"/>
</svg>
SVG Examples
Viewing SVG Files
All browsers support SVG files nowadays, except for Internet Explorer, which needs a plug-in. Those are
available for free, as for example the Adobe SVG Viewer.
SVG Examples
SVG Shapes
A rectangle
A rectangle with opacity
A rectangle with opacity 2
A rectangle with rounded corners
A circle
An ellipse
Three ellipses on top of each other
Two ellipses
A line
A polygon with three sides
A polygon with four sides
A polyline
A path
A spiral
SVG Filters
A feGaussianBlur filter
Another feGaussianBlur filter
A feBlend filter
A feColorMatrix filter
A feComponentTransfer filter
A feOffset filter
feOffset, feFlood, feComposite, feMerge, and feMergeNode
A feMorphology filter
Filter 1
Filter 2
Filter 3
Filter 4
Filter 5
Filter 6
11
SVG Gradients
SVG Misc
Add an <a> element to a rectangle. The rectangle will act as a hyperlink
Use a JavaScript to create an a element when the user clicks on a circle
Rectangle that repeatedly fade away over 5 seconds
Animate the x, y, width, and height attributes of a rectangle and change the color of the rectangle
SVG Elements
The links in the "Element" column point to attributes and more useful information about the specific element.
Element Description
a Defines a hyperlink
altGlyph Allows control over glyphs used to render particular character data (e.g. for
music symbols or Asian text)
altGlyphDef Defines a set of glyph substitutions (e.g. for music symbols or Asian text)
altGlyphItem Defines a candidate set of glyph substitutions (e.g. for music symbols or
Asian text)
animate Animates an attribute or property over time
clipPath
12
desc A text-only description for elements in SVG - not displayed as part of the
graphics. User agents may display the text as a tooltip
ellipse Defines an ellipse
feBlend SVG filter. Composites two objects together using different blending modes
13
feTurbulence SVG filter.
font-face-format
font-face-name
font-face-src
font-face-uri
foreignObject
hkern
image
marker
mask
missing-glyph
mpath
pattern
polygon Defines a closed shape that consists of a set of connected straight lines
14
radialGradient Defines a radial gradient
stop
switch
symbol
text
textPath
title A text-only description for elements in SVG - not displayed as part of the
graphics. User agents may display the text as a tooltip
tref
tspan
use
tspan
use
view
vkern
By: DataIntegratedEntity22592
Source: http://w3schools.com/svg/default.asp
15