CHAPTER 3: Basic Shapes

SVG PREDEFINED SHAPES
SVG has some predefined shapes elements that can be manipulated by developers:

Let us start with the basic shape, Rectangle.

RECTANGLE - The <rect> tag
The <rect> tag is used to create a rectangle and variations of a rectangle shape.

To understand how this works, copy the following code into Notepad and save the file as "rect1.svg". Place the file in your Web directory:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//
DTD SVG 20000303 Stylable//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="300" height="300">
<rect width="300" height="100"
style="fill:rgb(255,0,0);stroke-width:1;
stroke:rgb(0,0,0)"/>
</svg>

This creates a rectangle of width 300 pixels, height of 100 pixels, with a color fill of pure red,  stroke-width of 1 pixel, and stoke color of black.

In the previous code, the <rect> tag did not declare the location of the rectangle. To add its location you go to add it like this:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//
DTD SVG 20000303 Stylable//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="300" height="300">
<rect x="10" y="10" width="300" height="100"
style="fill:rgb(255,0,0);stroke-width:1;
stroke:rgb(0,0,0)"/>
</svg>

Take note that the y-coordinate is going down if positive in SVG.

Coordinates can be declared as percentage, inches, or pixels(default).

Code explanation in detail.

  • The width and height attributes of the rect element define the height and the width of the rectangle
  • The x and y attribute of the rect element define the location of the rectangle.
  • The style attribute is used to define CSS properties
  • The CSS fill property defines the fill color of the rectangle (either an rgb value, a color name, or a hexadecimal value)
  • The CSS stroke-width property defines the width of the border of the rectangle
  • The CSS stroke property defines the color of the border of the rectangle

Next, copy the following code into Notepad and save the file as "rect1.htm". Place the file in your Web directory:

<!DOCTYPE html PUBLIC "-//W3C//
DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/
xhtml1-transitional.dtd">
<html>
<body>
<object data="rect1.svg" width="100%"
height="100%" type="image/svg+xml">
<embed src="rect1.svg" width="100%"
height="100%" type="image/svg+xml" />
</object>
</body>
</html>

This applies to any .svg file to be embedded in XHTML.

Open the "rect1.htm" file in your browser to view your first SVG graphic!!!

View rect1.htm

You can also call an attribute called opacity to your stroke of the rectangle by adding in its style attribute:

stroke-opacity: <value>

where <value> must be replace by a number between 0 to 1. Decimals are accepted too by SVG. Remember, 1 is visible and 0 is hidden.

You can also change the fill opacity by adding in its style attribute:

opacity: <value>

where <value> must be replace by a number between 0 to 1. Decimals are accepted too by SVG. Remember, 1 is visible and 0 is hidden.

TOP

CIRCLE - The <circle> tag
The <circle> tag is used to create a circle.

Copy the following code into Notepad and save the file as "circle1.svg". Place the file in your Web directory:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//
DTD SVG 20000303 Stylable//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="300" height="300">
<circle cx="100" cy="50" r="40" stroke="black"
stroke-width="2" fill="red"/>
</svg>

Code explanation:

  • The cx and cy attributes define the x and y coordinates of the center of the circle. If cx and cy are omitted, the circle's center is set to (0, 0)
  • The r attribute defines the radius of the circle

TOP

ELLIPSE - The <ellipse> tag
The <ellipse> tag is used to create a ellipse.

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//
DTD SVG 20000303 Stylable//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="500" height="500">
<ellipse cx="300" cy="150" rx="200" ry="80"
style="fill:rgb(200,100,50);
stroke:rgb(0,0,100);stroke-width:2"/>
</svg>

It is basically similar to <circle> tag except that it has rx and ry as its horizonal and vertical radius value.

Code explanation:

  • The cx attribute defines the x coordinate of the center of the ellipse
  • The cy attribute defines the y coordinate of the center of the ellipse
  • The rx attribute defines the horizontal radius
  • The ry attribute defines the vertical radius

TOP

LINE - The <line> tag
The <line> tag is used to create a line.

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//
DTD SVG 20000303 Stylable//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="300" height="300">
<line x1="0" y1="0" x2="300" y2="300"
style="stroke:rgb(99,99,99);stroke-width:2"/>
</svg>

Code explanation:

  • The x1 attribute defines the start of the line on the x-axis
  • The y1 attribute defines the start of the line on the y-axis
  • The x2 attribute defines the end of the line on the x-axis
  • The y2 attribute defines the end of the line on the y-axis

Next, copy the following code into Notepad and save the file as "line1

TOP

POLYGON - The <polygon> tag
The <polygon> tag is used to create a graphic that contains at least three sides.
 

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//
DTD SVG 20000303 Stylable//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%">
<polygon points="220,100 300,210 170,250"
style="fill:#cccccc;
stroke:#000000;stroke-width:1"/>
</svg>

Code explanation:

  • The points attribute defines the x and y coordinates for each corner of the polygon

Therefore, the first coordinate of this is 220,100
then the second coordinate of this is 300,210
then the third coordinate of this is 170,250

This forms a triangle, but you are not limited to its number of points.

TOP

POLYLINE -The <polyline> tag

The <polyline> tag is used to create any shape that consists of only straight lines.

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//
DTD SVG 20000303 Stylable//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%">
<polyline points="0,0 0,20 20,20 20,40 40,40 40,60"
style="fill:white;stroke:red;stroke-width:2"/>
</svg>

Code explanation:

  • The points attribute defines the x and y coordinates for each corner of the polyline

TOP

PATH - The <path> tag

The <path> Tag
The <path> tag is used to define a path. This tag can make any type of shape, even spirals.

The following commands are available for path data:

  • M = moveto
  • L = lineto
  • H = horizontal lineto
  • V = vertical lineto
  • C = curveto
  • S = smooth curveto
  • Q = quadratic Belzier curve
  • T = smooth quadratic Belzier curveto
  • A = elliptical Arc
  • Z = closepath

Note: All of the commands above can also be expressed with lower letters. Capital letters means absolutely positioned, lower cases means relatively positioned.

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//
DTD SVG 20000303 Stylable//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%">
<path d="M250 150 L150 350 L350 350 Z" />
</svg>

The example above defines a path that starts at position 250 150 with a line to position 150 350 then from there a line to 350 350 and finally closing the path back to 250 150.

The following example creates a spiral:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//
DTD SVG 20000303 Stylable//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%">
<path d="M153 334
C153 334 151 334 151 334
C151 339 153 344 156 344
C164 344 171 339 171 334
C171 322 164 314 156 314
C142 314 131 322 131 334
C131 350 142 364 156 364
C175 364 191 350 191 334
C191 311 175 294 156 294
C131 294 111 311 111 334
C111 361 131 384 156 384
C186 384 211 361 211 334
C211 300 186 274 156 274"
style="fill:white;stroke:red;stroke-width:2"/>
</svg>

QUIZ: LEARN SVG - CHAPTER 3
TOP | CHAPTER 4: COLORS AND FILTERS


powered by BYC