The rect Element
The rect element defines a rectangular shape. The SVG rect element can be used to describe both a rectangle and a square. A square is simply a special case of a rectangleone in which the width and height of the rectangle are equal.
To fully describe a rectangle in SVG, we need to define the coordinates of its top-left corner together with its width and height. Those key pieces of information are specified in the x, y, width, and height attributes, respectively, of the rect element.
In addition to defining the position and dimensions of the rectangle, we need to define its appearance using the style attribute (or one of the alternative techniques described in Chapter 4, "Using CSS with SVG"). If we omit any style information, the rectangle will be rendered as a solid black shape. Like the line element, the rect element has a stroke property that defines the color of the outline of the rectangle. A rect element also may have a fill color that is defined by the value of its fill property.
In Listing 3.7, SimpleRects.svg, we produce two rectangular shapes. The upper shape appears onscreen to be a solid red rectangle, whereas the lower shape is a pale gray rectangle with a red outline. In fact, for both shapes, the stroke property (which defines the color of the outline) is defined separately from the color of the fill. In the first rectangle, which appears to have no outline, the stroke is simply the same color as the fill.
Listing 3.7 SimpleRects.svgTwo Rectangles Using the rect Element
<?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> <svg width="300" height="300"> <rect x="50" y="50" width="150" height="50" style="stroke:#FF0000; fill:#FF0000"/> <rect x="50" y="200" width="150" height="50" style="stroke:#FF0000; fill:#CCCCCC"/> </svg>
A common use of the rect element is in graphs, such as bar graphs. Listing 3.8, GraphAxes03.svg, shows how to use rect elements in a manner similar to that shown earlier to produce vertical bars in a bar chart.
Listing 3.8 GraphAxes03.svgUsing the rect Element in a Bar Graph
<?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> <svg width="600" height="400"> <!-- The vertical axis --> <line x1="40" y1="360" x2="40" y2="40" style="stroke:#000000; stroke-width:0.5;"/> <!-- The scale marks on the vertical axis --> <line x1="40" y1="110" x2="48" y2="110" style="stroke:#000000; stroke-width:0.5;"/> <line x1="40" y1="160" x2="48" y2="160" style="stroke:#000000; stroke-width:0.5;"/> <line x1="40" y1="210" x2="48" y2="210" style="stroke:#000000; stroke-width:0.5;"/> <line x1="40" y1="260" x2="48" y2="260" style="stroke:#000000; stroke-width:0.5;"/> <line x1="40" y1="310" x2="48" y2="310" style="stroke:#000000; stroke-width:0.5;"/> <!-- The horizontal axis --> <line x1="40" y1="360" x2="550" y2="360" style="stroke:#000000; stroke-width:0.5;"/> <!-- The scale marks on the horizontal axis --> <line x1="80" y1="360" x2="80" y2="368" style="stroke:#000000; stroke-width:0.5;"/> <line x1="120" y1="360" x2="120" y2="368" style="stroke:#000000; stroke-width:0.5;"/> <line x1="160" y1="360" x2="160" y2="368" style="stroke:#000000; stroke-width:0.5;"/> <line x1="200" y1="360" x2="200" y2="368" style="stroke:#000000; stroke-width:0.5;"/> <line x1="240" y1="360" x2="240" y2="368" style="stroke:#000000; stroke-width:0.5;"/> <line x1="280" y1="360" x2="280" y2="368" style="stroke:#000000; stroke-width:0.5;"/> <line x1="320" y1="360" x2="320" y2="368" style="stroke:#000000; stroke-width:0.5;"/> <line x1="360" y1="360" x2="360" y2="368" style="stroke:#000000; stroke-width:0.5;"/> <line x1="400" y1="360" x2="400" y2="368" style="stroke:#000000; stroke-width:0.5;"/> <line x1="440" y1="360" x2="440" y2="368" style="stroke:#000000; stroke-width:0.5;"/> <line x1="480" y1="360" x2="480" y2="368" style="stroke:#000000; stroke-width:0.5;"/> <line x1="520" y1="360" x2="520" y2="368" style="stroke:#000000; stroke-width:0.5;"/> <!-- The rectangles representing monthly sales --> <rect x="77" y="250" width="6" height="110" style="stroke:#FF0000; fill:#CCCCCC; stroke-width:1;"/> <rect x="117" y="240" width="6" height="120" style="stroke:#FF0000; fill:#CCCCCC; stroke-width:1;"/> <rect x="157" y="230" width="6" height="130" style="stroke:#FF0000; fill:#CCCCCC; stroke-width:1;"/> <rect x="197" y="240" width="6" height="120" style="stroke:#FF0000; fill:#CCCCCC; stroke-width:1;"/> <rect x="237" y="210" width="6" height="150" style="stroke:#FF0000; fill:#CCCCCC; stroke-width:1;"/> <rect x="277" y="220" width="6" height="140" style="stroke:#FF0000; fill:#CCCCCC; stroke-width:1;"/> <rect x="317" y="230" width="6" height="130" style="stroke:#FF0000; fill:#CCCCCC; stroke-width:1;"/> <rect x="357" y="220" width="6" height="140" style="stroke:#FF0000; fill:#CCCCCC; stroke-width:1;"/> <rect x="397" y="240" width="6" height="120" style="stroke:#FF0000; fill:#CCCCCC; stroke-width:1;"/> <rect x="437" y="190" width="6" height="170" style="stroke:#FF0000; fill:#CCCCCC; stroke-width:1;"/> <rect x="477" y="210" width="6" height="150" style="stroke:#FF0000; fill:#CCCCCC; stroke-width:1;"/> <rect x="517" y="200" width="6" height="160" style="stroke:#FF0000; fill:#CCCCCC; stroke-width:1;"/> </svg>
In Listing 3.8, the direction of the scale marks on the horizontal axis has been reversedthey now point down. The rect elements allow us to create bars that are two colors, having a red stroke and a pale gray fill, as you can see in Figure 3.7, or if you run the code onscreen.
Figure 3.7 A bar graph using rect and line elements.
An SVG rect element may be given rounded corners by means of the rx and ry attributes. Listing 3.9 shows how the rx and ry attributes can be used and also a limitation of the use of the use elementyou can only use the shape exactly as defined and cannot, for example, add rx and ry attributes.
Listing 3.9 RectRoundCorners.svgCreating Rectangles with Rounded Corners
<?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> <svg width="800px" height="600px"> <defs> <rect id="MyRect" x="0px" y="0px" width="100px" height="75px" style="fill:none; stroke:blue; stroke-width:3"/> </defs> <g transform="translate(25,25)"> <use xlink:href="#MyRect" rx="0px" ry="0px" /> </g> <g transform="translate(225,25)"> <use xlink:href="#MyRect" rx="5px" ry="5px" /> </g> <g transform="translate(425,25)"> <use xlink:href="#MyRect" rx="10px" ry="10px" /> </g> <rect id="MyRect" x="225px" y="150px" width="100px" height="75px" style="fill:none; stroke:blue; stroke-width:3" rx="5px" ry="5px"/> <rect id="MyRect" x="425px" y="150px" width="100px" height="75px" style="fill:none; stroke:blue; stroke-width:3" rx="10px" ry="10px"/> </svg>
The final two rect elements in Listing 3.9 produce the two rectangles in the lower row in Figure 3.8, each of which has rounded corners. Unfortunately, when you make use of the use element, you cannot modify the shape by, for example, adding rx and ry attributes.
TIP
If you want to apply unequal rx and ry values, you must be sure to specify a desired value, even zero, for both attributes. If you omit one of the two attributes, the SVG rendering engine will assign the same value to the other.
A rect element may be animated by any of the SVG animation elementsanimate, set, animateColor, animateMotion, or animateTransformwhich are described in Chapter 11.