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.
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!!!
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.
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:
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:
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:
Next, copy the following code into Notepad and save the file as "line1
<?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:
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.
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:
PATH - The <path> tag
The following commands are available for path data:
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