Circles, rectangles and more

SVG Shapes

XMLRSS feed

Basic SVG Shapes

SVG defines 5 basic shapes. These shapes all have attributes specific to that shape for positioning and sizing. They also each have presentation attributes which effect things like fill and stroke color, border width and more. Basic shapes (and complex shapes) have many common presentation attributes.

Presentation attributes are divided into groups and are described in the appendix rather than explaining them here.

Legend

BOLD means the attribute is required.
Green means the attribute is optional.
Italics denotes a presentation attribute of the shape like 'fill' which is both optional and shared with other SVG elements.

The 'rect' Element
<rect x="20" y="20" rx="0" ry="0" width="160" height="160" fill="blue" />
Code 1. Rectangle
Attribute
Explanation
Example
x
The x-axis top-left corner of the rectangle
y
The y-axis top-left corner of the rectangle
rx
For rounded rectangles. x-axis radius used to round the element (0 default)
ry
For rounded rectangles. y-axis radius used to round the element (0 default)
width
The width of the rectangle
height
The height of the rectangle
Presentation Attributes
The 'circle' Element
<circle cx="100" cy="100" r="90" fill="blue" />
Code 2. Circle
Attribute
Explanation
Example
cx
The x-axis center of the circle
cy
The y-axis center of the circle
r
The circle's radius
Presentation Attributes
The 'ellipse' Element
<ellipse cx="100" cy="100" rx="90" ry="80" fill="blue" />
Code 3. Ellipse
Attribute
Explanation
Example
cx
The x-axis center of the ellipse
cy
The y-axis center of the ellipse
rx
The length of the ellipse's radius along the x-axis
ry
The length of the ellipse's radius along the y-axis
Presentation Attributes
The 'line' Element
<line x1="10" y1="10" x2="190" y2="190" stroke="blue" stroke-width="4" />
Code 4. Line
Attribute
Explanation
Example
x1, y1
The start point the line
x2, y2
The end point the line
Presentation Attributes
The 'polyline' Element
<polyline points="10,190 20,190 20,150 50,150 50,190 80,190 80,110 110,110 110,190 140,190 140,70 170,70 170,190 190,190" stroke="blue" fill="darkblue" stroke-width="4" />
Code 5. Polyline
Attribute
Explanation
Example
points
The points on the polyline
Presentation Attributes
The 'polygon' Element
<polygon points="100,10 40,180 190,60 10,60 160,180 100,10" stroke="blue" fill="darkblue" stroke-width="4" fill-rule="nonzero" />
Code 6. Polygon
Attribute
Explanation
Example
points
The points of the polygon. The total number of points must be even.
fill-rule
See below. Part of the FillStroke presentation attributes.
Presentation Attributes

The difference between the polygon and the polyline is that polygons are a closed shape. If the last and first polygon points don't match then the shape is closed automatically.

Pike

Feel free to skip ahead to the Image element page if you're not interested in a long winded explanation on what fill and clip rules mean.

Clip and fill rules (clip-rule & fill-rule)

Polygons, polylines and clipping planes all use either the rule 'evenodd' or 'nonzero' to determine where the inside of an object is. Both rules work by starting with a count of 0 and fill sections based on this count. This count changes when a ray intersects with a line segment of the graphic element being filled.

The nonzero rule (default) works by taking a ray from inside the graphic element and going outside to infinity. At each intersection point of the ray and the line/curve segments of the graphic element you compute the slope and rotate it 90 degrees counterclockwise. A vector pointed in the same direction of the ray (dot product > 0) adds 1 to the count and one in the opposite direction (dot product < 0) adds -1. A final number which is not zero results in the filling of that point.

The evenodd rule works by taking a point in the shape and counting the number of times a ray from that point to infinity crosses the line/curve segments of the graphic element. An odd count means the point is inside the shape. In both cases, if the polyline or path is not closed (start and end points have the same x and y coordinates) it is automatically closed to compute the interior segments of the shape.

Example 1. Example shows fill-rule="nonzero" on the left and fill-rule="evenodd" on the right.
Last Updated: Sat, 17 Dec 2005 00:14:59 GMT
All rights reserved © 1999-2007 Matthew Bystedt