| Getting started |
Defining objects, such as text and shapes, in an SVG image is relatively straightforward. As an SVG developer, you could hand-code a lot of this (or develop programs to dynamically generate image objects), but there are alternatives. For example, Adobe® Illustrator® 9.0 lets you generate complex images easily by simply exporting your Illustrator files as SVG. However, a general understanding of how SVG objects are defined, and what the various coordinates and elements refer to, can be particularly valuable when you are considering animation and interactivity.
Let's take a look at some typical SVG code. As XML, SVG is written much as HTML, with opening and closing tags, and nested tags within tags. Every SVG file is contained within an <svg> outermost element, and as such, can be embedded inline as a fragment within a parent document (an XML Web page) or as a standalone, self-contained graphics file. For example, the following shows the outermost structure of an SVG document as a standalone file:
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20000303 Stylable//EN" "http://www.w3.org/TR/2000/03/WD-SVG-20000303/DTD/svg-20000303-stylable.dtd"> <svg xml:space="preserve" width="5.5in" height=".5in"> ... </svg>
The first line establishes the code that follows as XML. The second line provides a reference for the DTD or Document Type Definition. The DTD is a set of rules that defines elements and attributes, and specifies how to form valid documents and structures.
Within the <svg> element, there can be three basic types of drawing elements: text, shapes, and paths. These are defined in a straightforward fashion. For example, the following description:
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20000303 Stylable//EN" "http://www.w3.org/TR/2000/03/WD-SVG-20000303/DTD/svg-20000303-stylable.dtd"> <svg xml:space="preserve" width="5.5in" height=".5in"> <text style="fill:red;" y="15">This is SVG.</text> </svg>
produces this result:
The text is positioned in an image area that is 5.5 inches wide by .5 inches tall, with its baseline 15 pixels down from the top.
The following example produces a blue rectangle with its top left corner aligning with the top left corner of the image area (the default when no coordinates are specified).
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 03December 1999//EN" "http://www.w3.org/Graphics/SVG/SVG-19991203.dtd"> <svg xml:space="preserve" width="5.5in" height="2in"> <rect style="fill:blue;" width="250" height="100"/> </svg>