SVG 1.1 (Second Edition) – 16 August 2011Top ⋅ Contents ⋅ Previous ⋅ Next ⋅ Elements ⋅ Attributes ⋅ Properties
9 Basic Shapes
Contents
9.1 Introduction
9.2 The‘rect’element
9.3 The‘circle’element
9.4 The‘ellipse’element
9.5 The‘line’element
9.6 The‘polyline’element
9.7 The‘polygon’element
9.7.1 The grammar for points specifications in‘polyline’and‘polygon’elements
9.8 DOM interfaces
9.8.1 Interface SVGRectElement
9.8.2 Interface SVGCircleElement
9.8.3 Interface SVGEllipseElement
9.8.4 Interface SVGLineElement
9.8.5 Interface SVGAnimatedPoints
9.8.6 Interface SVGPolylineElement
9.8.7 Interface SVGPolygonElement
9.1 Introduction
SVG contains the following set of basic shape elements:
rectangles (including optional rounded corners), created with the‘rect’element,
circles, created with the‘circle’element,
ellipses, created with the‘ellipse’element,
straight lines, created with the‘line’element,
polylines, created with the‘polyline’element, and
polygons, created with the‘polygon’element.
Mathematically, these shape elements are equivalent to a‘path’element that would construct the same shape. The basic shapes may be stroked, filled and used as clip paths. All of the properties available for‘path’elements also apply to the basic shapes.
9.2 The‘rect’element
The‘rect’element defines a rectangle which is axis-aligned with the current user coordinate system. Rounded rectangles can be achieved by setting appropriate values for attributes‘rx’and‘ry’.
‘rect’
Categories:
Basic shape element, graphics element, shape element
Content model:
Any number of the following elements, in any order:
The x-axis coordinate of the side of the rectangle which has the smaller x-axis coordinate value in the current user coordinate system.
If the attribute is not specified, the effect is as if a value of "0" were specified.
Animatable: yes.
y= "
"
The y-axis coordinate of the side of the rectangle which has the smaller y-axis coordinate value in the current user coordinate system.
If the attribute is not specified, the effect is as if a value of "0" were specified.
Animatable: yes.
width= "
"
The width of the rectangle.
A negative value is an error (see Error processing). A value of zero disables rendering of the element.
Animatable: yes.
height= "
"
The height of the rectangle.
A negative value is an error (see Error processing). A value of zero disables rendering of the element.
Animatable: yes.
rx= "
"
For rounded rectangles, the x-axis radius of the ellipse used to round off the corners of the rectangle.
A negative value is an error (see Error processing).
See the notes below about what happens if the attribute is not specified.
Animatable: yes.
ry= "
"
For rounded rectangles, the y-axis radius of the ellipse used to round off the corners of the rectangle.
A negative value is an error (see Error processing).
See the notes below about what happens if the attribute is not specified.
Animatable: yes.
The values used for the x- and y-axis rounded corner radii are determined implicitly if the‘rx’or‘ry’attributes (or both) are not specified, or are specified but with invalid values. The values are also subject to clamping so that the lengths of the straight segments of the rectangle are never negative. The effective values for‘rx’and‘ry’are determined by following these steps in order:
Letrxandrybe length values.
If neither‘rx’nor‘ry’are properly specified, then set bothrxandryto 0. (This will result in square corners.)
Otherwise, if a properly specified value is provided for‘rx’, but not for‘ry’, then set bothrxandryto the value of‘rx’.
Otherwise, if a properly specified value is provided for‘ry’, but not for‘rx’, then set bothrxandryto the value of‘ry’.
Otherwise, both‘rx’and‘ry’were specified properly. Setrxto the value of‘rx’andryto the value of‘ry’.
Ifrxis greater than half of‘width’, then setrxto half of‘width’.
Ifryis greater than half of‘height’, then setryto half of‘height’.
The effective values of‘rx’and‘ry’arerxandry, respectively.
Mathematically, a‘rect’element can be mapped to an equivalent‘path’element as follows: (Note: all coordinate and length values are first converted into user space coordinates according to Units.)
perform an absolute moveto operation to location (x+rx,y), wherexis the value of the‘rect’element's‘x’attribute converted to user space,rxis the effective value of the‘rx’attribute converted to user space andyis the value of the‘y’attribute converted to user space
perform an absolute horizontal lineto operation to location (x+width-rx,y), wherewidthis the‘rect’element's‘width’attribute converted to user space
perform an absolute elliptical arc operation to coordinate (x+width,y+ry), where the effective values for the‘rx’and‘ry’attributes on the‘rect’element converted to user space are used as therxandryattributes on the elliptical arc command, respectively, thex-axis-rotationis set to zero, thelarge-arc-flagis set to zero, and thesweep-flagis set to one
perform a absolute verticallinetoto location (x+width,y+height-ry), whereheightis the‘rect’element's‘height’attribute converted to user space
perform an absolute elliptical arc operation to coordinate (x+width-rx,y+height)
perform an absolute horizontallinetoto location (x+rx,y+height)
perform an absolute elliptical arc operation to coordinate (x,y+height-ry)
perform an absolute absolute verticallinetoto location (x,y+ry)
perform an absolute elliptical arc operation to coordinate (x+rx,y)
Example rect01shows a rectangle with sharp corners. The‘rect’element is filled with yellow and stroked with navy.
Example rect01
View this example as SVG (SVG-enabled browsers only)
Example rect02shows two rounded rectangles. The‘rx’specifies how to round the corners of the rectangles. Note that since no value has been specified for the‘ry’attribute, it will be assigned the same value as the‘rx’attribute.
Example rect02
View this example as SVG (SVG-enabled browsers only)
9.3 The‘circle’element
The‘circle’element defines a circle based on a center point and a radius.
‘circle’
Categories:
Basic shape element, graphics element, shape element
Content model:
Any number of the following elements, in any order:
The x-axis coordinate of the center of the circle.
If the attribute is not specified, the effect is as if a value of "0" were specified.
Animatable: yes.
cy= "
"
The y-axis coordinate of the center of the circle.
If the attribute is not specified, the effect is as if a value of "0" were specified.
Animatable: yes.
r= "
"
The radius of the circle.
A negative value is an error (see Error processing). A value of zero disables rendering of the element.
Animatable: yes.
The arc of a‘circle’element begins at the "3 o'clock" point on the radius and progresses towards the "9 o'clock" point. The starting point and direction of the arc are affected by the user space transform in the same manner as the geometry of the element.
Example circle01consists of a‘circle’element that is filled with red and stroked with blue.
Example circle01
View this example as SVG (SVG-enabled browsers only)
9.4 The‘ellipse’element
The‘ellipse’element defines an ellipse which is axis-aligned with the current user coordinate system based on a center point and two radii.
‘ellipse’
Categories:
Basic shape element, graphics element, shape element
Content model:
Any number of the following elements, in any order:
The x-axis coordinate of the center of the ellipse.
If the attribute is not specified, the effect is as if a value of "0" were specified.
Animatable: yes.
cy= "
"
The y-axis coordinate of the center of the ellipse.
If the attribute is not specified, the effect is as if a value of "0" were specified.
Animatable: yes.
rx= "
"
The x-axis radius of the ellipse.
A negative value is an error (see Error processing). A value of zero disables rendering of the element.
Animatable: yes.
ry= "
"
The y-axis radius of the ellipse.
A negative value is an error (see Error processing). A value of zero disables rendering of the element.
Animatable: yes.
The arc of an‘ellipse’element begins at the "3 o'clock" point on the radius and progresses towards the "9 o'clock" point. The starting point and direction of the arc are affected by the user space transform in the same manner as the geometry of the element.
Example ellipse01below specifies the coordinates of the two ellipses in the user coordinate system established by the‘viewBox’attribute on the‘svg’element and the‘transform’attribute on the‘g’and‘ellipse’elements. Both ellipses use the default values of zero for the‘cx’and‘cy’attributes (the center of the ellipse). The second ellipse is rotated.
Example ellipse01
View this example as SVG (SVG-enabled browsers only)
9.5 The‘line’element
The‘line’element defines a line segment that starts at one point and ends at another.
‘line’
Categories:
Basic shape element, graphics element, shape element
Content model:
Any number of the following elements, in any order:
The x-axis coordinate of the start of the line.
If the attribute is not specified, the effect is as if a value of "0" were specified.
Animatable: yes.
y1= "
"
The y-axis coordinate of the start of the line.
If the attribute is not specified, the effect is as if a value of "0" were specified.
Animatable: yes.
x2= "
"
The x-axis coordinate of the end of the line.
If the attribute is not specified, the effect is as if a value of "0" were specified.
Animatable: yes.
y2= "
"
The y-axis coordinate of the end of the line.
If the attribute is not specified, the effect is as if a value of "0" were specified.
Animatable: yes.
Mathematically, a‘line’element can be mapped to an equivalent‘path’element as follows: (Note: all coordinate and length values are first converted into user space coordinates according to Units.)
perform an absolute moveto operation to absolute location (x1,y1), wherex1andy1are the values of the‘line’element's‘x1’and‘y1’attributes converted to user space, respectively
perform an absolute lineto operation to absolute location (x2,y2), wherex2andy2are the values of the‘line’element's‘x2’and‘y2’attributes converted to user space, respectively
Because‘line’elements are single lines and thus are geometrically one-dimensional, they have no interior; thus,‘line’elements are never filled (see the‘fill’property).
Example line01below specifies the coordinates of the five lines in the user coordinate system established by the‘viewBox’attribute on the‘svg’element. The lines have different thicknesses.
Example line01
View this example as SVG (SVG-enabled browsers only)
9.6 The‘polyline’element
The‘polyline’element defines a set of connected straight line segments. Typically,‘polyline’elements define open shapes.
‘polyline’
Categories:
Basic shape element, graphics element, shape element
Content model:
Any number of the following elements, in any order:
The points that make up the polyline. All coordinate values are in the user coordinate system.
Animatable: yes.
If an odd number of coordinates is provided, then the element is in error, with the same user agent behavior as occurs with an incorrectly specified‘path’element.
Mathematically, a‘polyline’element can be mapped to an equivalent‘path’element as follows:
perform an absolute moveto operation to the first coordinate pair in the list of points
for each subsequent coordinate pair, perform an absolute lineto operation to that coordinate pair.
Example polyline01below specifies a polyline in the user coordinate system established by the‘viewBox’attribute on the‘svg’element.
Example polyline01
View this example as SVG (SVG-enabled browsers only)
9.7 The‘polygon’element
The‘polygon’element defines a closed shape consisting of a set of connected straight line segments.
‘polygon’
Categories:
Basic shape element, graphics element, shape element
Content model:
Any number of the following elements, in any order:
The points that make up the polygon. All coordinate values are in the user coordinate system.
Animatable: yes.
If an odd number of coordinates is provided, then the element is in error, with the same user agent behavior as occurs with an incorrectly specified‘path’element.
Mathematically, a‘polygon’element can be mapped to an equivalent‘path’element as follows:
perform an absolute moveto operation to the first coordinate pair in the list of points
for each subsequent coordinate pair, perform an absolute lineto operation to that coordinate pair
perform a closepath command
Example polygon01below specifies two polygons (a star and a hexagon) in the user coordinate system established by the‘viewBox’attribute on the‘svg’element.
Example polygon01
View this example as SVG (SVG-enabled browsers only)
9.7.1 The grammar for points specifications in‘polyline’and‘polygon’elements
The following is the Extended Backus-Naur Form (EBNF) for points specifications in‘polyline’and‘polygon’elements. The following notation is used:
Corresponds to attribute
‘x1’on the given
‘line’element.
y1(readonly SVGAnimatedLength)
Corresponds to attribute
‘y1’on the given
‘line’element.
x2(readonly SVGAnimatedLength)
Corresponds to attribute
‘x2’on the given
‘line’element.
y2(readonly SVGAnimatedLength)
Corresponds to attribute
‘y2’on the given
‘line’element.
9.8.5 Interface SVGAnimatedPoints
The SVGAnimatedPoints interface supports elements which have a‘points’attribute which holds a list of coordinate values and which support the ability to animate that attribute.
Additionally, the‘points’attribute on the original element accessed via the XML DOM (e.g., using thegetAttribute()method call) will reflect any changes made to points.
Provides access to the base (i.e., static) contents of the
‘points’attribute.
animatedPoints(readonly SVGPointList)
Provides access to the current animated contents of the
‘points’attribute. If the given attribute or property is being animated, contains the current animated value of the attribute or property. If the given attribute or property is not currently being animated, contains the same value as points.
9.8.6 Interface SVGPolylineElement
The SVGPolylineElement interface corresponds to the
‘polyline’element.