Looking to Reprint this content?
Cover | Table of Contents | Colophon
DOCTYPE
declaration. The root <svg> element
defines the width and
height of the finished graphic
in pixels. The <title> element's
content is available to a viewing program for use in a title bar or
as a tooltip pointer, and the <desc>
element lets you give a full description of the image.
<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="140" height="170">
<title>Cat</title>
<desc>Stick Figure of a Cat</desc>
<!— the drawing will go here —>
</svg>
<circle> element. The
element's attributes
specify the center x-coordinate,
center y-coordinate, and radius.
The (0,0) point is the upper left corner of the picture.
x coordinates
increase as you move horizontally to the right;
y coordinates increase
as you move vertically downwards.
style attribute. Its
value will be a series of presentation properties and values,
as described in Appendix B,
in Section B.1. We'll use a stroke color
of black for the outline, and a fill color
of none to make the face transparent.
The SVG is shown in Example 1-2, and
its result in Figure 1-8.
width and
height attributes on
the <svg> element. The values of these
attributes can be simply a number, which is presumed to be in pixels;
this is said to be specified in user coordinates.
You may also specify width and
height as a
number followed by a unit identifier, which can be one of the
following:
em
ex
px
pt
pc
cm
mm
in
width and
height attributes on
the <svg> element. The values of these
attributes can be simply a number, which is presumed to be in pixels;
this is said to be specified in user coordinates.
You may also specify width and
height as a
number followed by a unit identifier, which can be one of the
following:
em
ex
px
pt
pc
cm
mm
in
<svg width="200" height="150">
<svg width="200px" height="200px">
<svg> element, the viewer sets up a
coordinate system where the horizontal, or x-coordinate,
increases as you go to the right, and the vertical, or
y-coordinate, increases as you
move vertically downward. The upper left corner of the viewport is
defined to have an x- and
y-coordinate of
zero. This point, written as (0, 0) is also
called the origin. The coordinate system
is a pure geometric system;
points have neither width nor height, and the grid lines are
considered infinitely thin. We'll return to this subject in
Chapter 3.
<svg width="200" height="200">
<rect x="10" y="10" width="50" height="30"
style="stroke: black; fill: none;"/>
</svg>
<svg width="200" height="200">
<rect x="10mm" y="10mm" width="15mm" height="10mm"
style="stroke:black; fill:none;"/>
</svg>
<svg> element does not
affect coordinates given without units in other elements.
Example 2-3
shows a viewport set up in millimeters, but the rectangle is still drawn
at pixel (user) coordinates, as you see in Figure 2-3.
viewBox attribute on the <svg> element. The value of this attribute consists of
four numbers that represent the minimum
x-coordinate,
minimum y-coordinate,
width, and height of the user coordinate system that you want to superimpose on
the viewport.
<svg width="4cm" height="5cm" viewBox="0 0 64 80">
<svg width="4cm" height="5cm" viewBox="0 0 64 80">
<rect x="10" y="35" width="40" height="40"
style="stroke: black; fill: none;"/>
<!-- roof -->
<polyline points="10 35, 30 7.68, 50 35"
style="stroke:black; fill: none;"/>
<!-- door -->
<polyline points="30 75, 30 55, 40 55, 40 75"
style="stroke:black; fill: none;"/>
</svg>
viewBox attribute may be separated by commas or
whitespace. If either the width or height is zero, none of your graphic will
display. It is an error to specify a negative value for the
viewBox were identical
(4/5 = 64/80). What happens, though, if the aspect ratio of the viewport and
the viewBox are not the same, as in this example, where
viewBox has an aspect ratio of one to one,
but the viewport has an aspect ratio of one to three?
<svg width="45px" height="135px" viewBox="0 0 90 90">
<svg> element into your
document. The effect is to create a "mini-canvas" upon which
you can draw. We used this technique to create illustrations such as
Figure 2-5. Rather than drawing the rectangles, then rescaling and positioning the cat inside each one
(the brute force approach), we took these steps:
<svg> element with the
appropriate preserveAspectRatio
attribute<use>), and let SVG do the
heavy lifting
<svg width="200px" height="200px" viewBox="0 0 200 200">
<circle cx="25" cy="25" r="25" style="stroke: black; fill: none;"/>
</svg>
<svg width="200px" height="200px" viewBox="0 0 200 200">
<circle cx="25" cy="25" r="25" style="stroke: black; fill: none;"/>
<rect x="100" y="5" width="30" height="80"
style="stroke: blue; fill: none;"/>
</svg>
<svg> tag, you are ready to begin
drawing. In this chapter, we will show the basic shapes you can
use to create the major elements of most drawings: lines, rectangles,
polygons, circles, and ellipses.
<line> element. Just specify the
x- and y-coordinates of
the line's endpoints. Coordinates may be specified
without units, in which case they are considered to be user coordinates,
or with units such as em,
in, etc. as described in
Chapter 2,
in Section 2.1. The SVG in
Example 3-1 draws several lines; the reference grid
in Figure 3-1 is not part of the SVG that you see here.
<line x1="start-x" y1="start-y" x2="end-x" y2="end-y">
<svg width="200px" height="200px" viewBox="0 0 200 200"> <!-- horizontal line --> <line x1="40" y1="20" x2="80" y2="20" style="stroke: black;"/> <!-- vertical line --> <line x1="0.7cm" y1="1cm" x2="0.7cm" y2="2.0cm" style="stroke: black;"/> <!-- diagonal line --> <line x1="30" y1="30" x2="85" y2="85" style="stroke: black;"/> </svg>
style attribute.
<line> element. Just specify the
x- and y-coordinates of
the line's endpoints. Coordinates may be specified
without units, in which case they are considered to be user coordinates,
or with units such as em,
in, etc. as described in
Chapter 2,
in Section 2.1. The SVG in
Example 3-1 draws several lines; the reference grid
in Figure 3-1 is not part of the SVG that you see here.
<line x1="start-x" y1="start-y" x2="end-x" y2="end-y">
<svg width="200px" height="200px" viewBox="0 0 200 200"> <!-- horizontal line --> <line x1="40" y1="20" x2="80" y2="20" style="stroke: black;"/> <!-- vertical line --> <line x1="0.7cm" y1="1cm" x2="0.7cm" y2="2.0cm" style="stroke: black;"/> <!-- diagonal line --> <line x1="30" y1="30" x2="85" y2="85" style="stroke: black;"/> </svg>
style attribute.
<svg width="200px" height="200px" viewBox="0 0 200 200">
<!-- horizontal line -->
<line x1="30" y1="10" x2="80" y2="10"
style="stroke-width: 10; stroke: black;"/>
<!-- vertical line -->
<line x1="10" y1="30" x2="10" y2="80"
style="stroke-width: 10; stroke: black;"/>
<!-- diagonal line -->
<line x1="25" y1="25" x2="75" y2="75"
style="stroke-width: 10; stroke: black;"/>
</svg>
aqua,
black,
blue,
fuchsia,
gray,
green,
lime,
maroon,
navy,
olive,
purple,
red,
silver,
teal,
white, and
yellow.
#
rr
gg
bb, where
rr is the red component,
gg is the green component, and
bb is the blue component in the range
0-ff.
width, and its
height. The interior of the
rectangle is filled with the fill
color you specify. If you do not specify a
fill color, the interior of the shape is filled with black.
The fill color may be specified in any of the ways described
in Section 3.2.2, or it may take
the value none to leave the
interior unfilled and thus transparent. You may also specify a
fill-opacity in the same
format as you did in Section 3.2.3.
Both fill
and fill-opacity are
presentation properties, and belong in the
style attribute.
none is presumed, and
no outline is drawn.
Example 3-6 draws several variations
of the <rect> element.
Figure 3-6 shows
the result, with a grid for reference.
<svg width="200px" height="200px" viewBox="0 0 200 200">
<!-- black interior, no outline -->
<rect x="10" y="10" width="30" height="50"/>
<!-- no interior, black outline -->
<rect x="50" y="10" width="20" height="40"
style="fill: none; stroke: black;"/>
<!-- blue interior, thick semi-transparent red outline -->
<rect x="10" y="70" width="25" height="30"
style="fill: #0000ff;
stroke: red; stroke-width: 7; stroke-opacity: 0.5;"/>
<!-- semi-transparent yellow interior, dashed green outline -->
<rect x="50" y="70" width="35" height="20"
style="fill: yellow; fill-opacity: 0.5;
stroke: green; stroke-width: 2; stroke-dasharray: 5 2"/>
</svg>
<circle> element
and specify the center x-coordinate,
center y-coordinate, and radius with
the cx,
cy, and
r attributes.
As with a rectangle, the default is to fill the circle with black and
draw no outline unless you specify some other combination of
fill and
stroke.
rx and
ry.
cx or
cy is omitted, it is presumed to be
zero. If the radius is zero, no shape will be displayed;
it is an error to provide a negative radius.
Example 3-8 draws some
circles and ellipses which are shown in
Figure 3-9.
<svg width="200px" height="200px" viewBox="0 0 200 200">
<circle cx="30" cy="30" r="20" style="stroke: black; fill: none;"/>
<circle cx="80" cy="30" r="20"
style="stroke-width: 5; stroke: black; fill: none;"/>
<ellipse cx="30" cy="80" rx="10" ry="20"
style="stroke: black; fill: none;"/>
<ellipse cx="80" cy="80" rx="20" ry="10"
style="stroke: black; fill: none;"/>
</svg>
<polygon> element
lets you specify a series of points
that describe a geometric area to be filled and outlined
as described earlier. The points
attribute consists of a series of x- and
y-coordinate pairs
separated by commas or whitespace. You must give an even number of
entries in the series of numbers. You don't have to return to the
starting point; the shape will automatically be closed.
Example 3-9 uses the
<polygon> element to draw a
parallelogram, a star, and an irregular shape.
<svg width="200px" height="200px" viewBox="0 0 200 200">
<!-- parallelogram -->
<polygon points="15,10 55, 10 45, 20 5, 20"
style="fill: red; stroke: black;"/>
<!-- star -->
<polygon
points="35,37.5 37.9,46.1 46.9,46.1 39.7,51.5
42.3,60.1 35,55 27.7,60.1 30.3,51.5
23.1,46.1 32.1,46.1"
style="fill: #ccffcc; stroke: green;"/>
<!-- weird shape -->
<polygon
points="60 60, 65 72, 80 60, 90 90, 72 80, 72 85, 50 95"
style="fill: yellow; fill-opacity: 0.5; stroke: black;
stroke-width: 2;"/>
</svg>
<line> elements, but if there are many
lines it might be easier to use the
<polyline>
element. It has the same attributes as
<polygon>, except that the shape is not
closed. Example 3-12 draws the symbol for an
electrical resistor, shown in Figure 3-13.
<svg width="200px" height="200px" viewBox="0 0 200 200">
<polyline
points="5 20, 20 20, 25 10, 35 30, 45 10,
55 30, 65 10, 75 30, 80 20, 95 20"
style="stroke: black; stroke-width: 3; fill: none;"/>
</svg>
fill property
to none when using
<polyline>; otherwise, the SVG viewer
attempts to fill the shape, sometimes with startling results like
those in Figure 3-14.
<line> or
<polyline>, you may specify the
shape of the endpoints of the lines by setting the
stroke-linecap
style property
to one of the values
butt,
round, or
square.
Example 3-13 shows these three values,
with gray guide lines showing the actual endpoints of the lines.
You can see in
Figure 3-15 that
round and
square extend beyond the
end coordinates; butt, the
default, ends exactly at the specified endpoint.
<line x1="10" y1="15" x2="50" y2="15"
style="stroke-linecap: butt; stroke-width: 15;"/>
<line x1="10" y1="45" x2="50" y2="45"
style="stroke-linecap: round; stroke-width: 15;"/>
<line x1="10" y1="75" x2="50" y2="75"
style="stroke-linecap: square; stroke-width: 15;"/>
<!-- guide lines -->
<line x1="10" y1="0" x2="10" y2="100" style="stroke: #999;"/>
<line x1="50" y1="0" x2="50" y2="100" style="stroke: #999;"/>
stroke-linejoin
style property,
which may have the values
miter (pointed),
round (round -- what did you
expect?), or
bevel (flat).
Example 3-14 produces the result shown in
Figure 3-16.
<polyline
style="stroke-linejoin: miter; stroke: black; stroke-width: 12;
fill: none;"
points="30 30, 45 15, 60 30"/>
<polyline
style="stroke-linejoin: round; stroke: black; stroke-width: 12;
fill: none;"
points="90 30, 105 15, 120 30"/>
<polyline
style="stroke-linejoin: bevel; stroke-width: 12; stroke: black;
fill: none;"
points="150 30, 165 15, 180 30"/>
| Shape | Description |
|---|---|
<line x1="start-x"
y1="start-y"
x2="end-x"
y2="end-y"/>
|
Draws a line from the starting point at coordinates
(start-x, start-y)
to the ending point at coordinates (end-x, end-y).
|
<rect x="left-x"
y="top-y" width="width"
height="height"/>
|
Draws a rectangle whose upper left corner is at
(left-x, top-y)
with the given width and
height.
|
<circle cx="center-x"
cy="center-y"
r="radius"/>
|
Draws a circle with the given radius,
centered at
(center-x, center-y).
|
<ellipse cx="center-x"
cy="center-y"
rx="x-radius"
ry="y-radius"/>
|
Draws an ellipse with the given x-radius
and y-radius
centered at
(center-x, center-y).
|
<polygon
points="points-specifications"/>
|
Draws an arbitrary closed polygon whose outline is described by the
points-specification. The points
are specified as pairs of x- and
y-coordinates.
These are user coordinates only; you may not add a length unit
specifier.
|
<style> attribute
to a series of visual properties and their values as described
in Appendix B, in
Section B.1.
<circle cx="20" cy="20" r="10"
style="stroke: black; stroke-width: 1.5; fill: blue; fill-opacity: 0.6"/>
<defs> element, which we will
discuss later in this chapter.
<svg width="200px" height="200px" viewBox="0 0 200 200">
<defs>
<style type="text/css"><![CDATA[
circle {
fill: #ffc;
stroke: blue;
stroke-width: 2;
stroke-dasharray: 5 3
}
]]></style>
</defs>
<circle cx="20" cy="20" r="10"/>
<circle cx="60" cy="20" r="15"/>
<circle cx="20" cy="60" r="10" style="fill: #cfc"/>
<circle cx="60" cy="60" r="15" style="stroke-width: 1;
stroke-dasharray: none;"/>
</svg>
<g> element gathers all of its child elements
as a group, and often has an id attribute
to give that group a unique name. Furthermore, each group may have
its own <title> and
<desc> to identify it for text-based
XML applications or to aid in accessibility for visually-impaired users.
In addition to the conceptual
clarity that comes from the ability to group and document objects, the
<g> element also provides notational
convenience.
Any styles you
specify in the starting <g>
tag will apply to all the child elements in the group. In
Example 4-5,
this saves us from having to duplicate the
style="fill:none; stroke:black;"
on every element shown in Figure 4-4.
It is also possible to nest groups within one another, although we won't
show any examples of this until
Chapter 5.
<g> element as analogous to the
Group Objects function in programs such as
Adobe Illustrator. It also serves a similar function to the
concept of layers in such programs; a layer
is also a grouping of related objects.
<svg width="240px" height="240px" viewBox="0 0 240 240">
<title>Grouped Drawing</title>
<desc>Stick-figure drawings of a house and people</desc>
<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, 12