Lesson 7
Objective: Learn to draw, position and style a polyline.
<polyline x1="0" y1="0" x2="100" y2="100"></polyline>
<polyline> ... </polyline> The full line element with open and closing tags.
<polyline /> Because this does not contain any content it can also be used as a self-closing element.
Polylines are are groups of connected straight lines. All the points are included in one attribute.
points a set of points setting the layout points of the polyline. Each number must be separated by a space, comma, EOL or line feed character. Each point must have two numbers an x coordinate and a y coordinate.
The point list can therefore be written in a number of ways depending on your preference. Eg: all the following describe the same points:
1 1, 2 2, 3 3, 4 4
1,1 2,2 3,3 4,4 << This is probably the clearest way to write points in a line.
1 1 2 2 3 3 4 4
1 1
2 2
3 3
4 4
Now that we are drawing lines with multiple points there are additional styles that enhance the presentation of the line.
stroke-linejoin="miter" The value options for this property are miter, round and bevel.
fill="white" Note that polylines can use the fill attribute. You will have to experiment with various shapes to see how this is applied and how it appears.
All other stroke properties can be used with polyline.This first example uses full SVG attributes to define the stroke colour and size of the rectangle. This example uses a comma to separate x and y coordinates.
<polyline x="100" y="100" points="200,200 300,200 300,100 100,100 100,300 350,300 350,50" stroke="green" stroke-width="4" fill="none"> </polyline>
It is easy to construct complex polylines if you draw the shape on graph paper and plot the points first. You can use the IGP-SVG Primitive design sheet for this. This example only uses spaces between points.
<polyline points="10 200 60 100 120 300 200 40 240 200 280 100 360 310 380 200" stroke="red" stroke-width="4" stroke-linejoin="miter" fill="white"> </polyline>
You can create geometric shapes with polylines by having the same starting and ending points. Cut and paste this SVG into the SVG Editor. This was designed on the SVG Primitive design sheet first. Notice how each polyline has a title to make it easy to understand.
<!--Wall. Points with line feed--> <polyline title="Wall" points=" 80,200 80,360 320,360 320,200" stroke="orange" stroke-width="4" stroke-linecap="round" stroke-linejoin="miter" fill="yellow"></polyline> <!--roof. Points with only spaces--> <polyline title="Roof" points="40 200 200 80 360 200" stroke="darkred" stroke-width="6" stroke-linejoin="bevel" fill="red"></polyline> <!--Door. Points with comma linked xy's--> <polyline title="Door" points="160,360 160,240, 240,240 240,360" stroke="darkbrown" stroke-width="4" stroke-linejoin="miter" fill="brown"></polyline> <!--Left Window. Points with comma separated xy's and some extra line properties--> <polyline title="Left Window" points="100 240,100 300,140 300,140 240,100 240" stroke="brown" stroke-width="12" stroke-linejoin="round" stroke-linecap="round" fill="brown"></polyline> <!--Right Window. Points with line feed again--> <polyline title="Right Window" points=" 260,240 260,300 300,300 300,240 260,240" stroke-width="none" stroke-linejoin="miter" fill="brown"></polyline>
Here is the SVG for those who are too lazy to type the SVG. View the SVG Markup
Here are our standard SVG Cheatsheets you can download, print out and have ready when needed. There is also a text file containing annotated quickstarts for all major shapes and techniques.
SVG Editor. Access a full SVG Editor online at any time.