Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SVG Polygon

SVG Polygon

In SVG, the The element is used to draw polygons; it is one of the commonly used basic shapes in SVG.

Use The element can create closed polygons, and you can control the vertex coordinates, fill color, border color, etc. by setting attributes.

Basic Syntax



  fill="fill-color"                

  stroke="stroke-color"            

  stroke-width="width"             

/>

Attribute Analysis:

The following code draws a polygon with orange fill, black stroke, and a width of 2 pixels; its vertex coordinates are (100, 10), (150, 190), and (50, 190), forming a closed triangle.



  



Example 1

The tag is used to create graphics with no fewer than three sides.

A polygon is made up of straight lines, and its shape is "closed" (all the lines connect together).

Remarkpolygon comes from Greek. "Poly" means "many", and "gon" means "angle".

Below is the SVG code:

Example

<

svg

height

=

"

210

"

width

=

"

500

"

<

polygon

points

=

"

200,10 250,190 160,210

"

style

=

"

fill:lime;stroke:purple;stroke-width:1

"

/>

svg

Try it »

Click to view: View SVG File.

Code analysis:

Example 2

The following example creates a four-sided polygon:

Below is the SVG code:

Example

<

svg

height

=

"

250

"

width

=

"

500

"

<

polygon

points

=

"

220,10 300,210 170,250 123,234

"

style

=

"

fill:lime;stroke:purple;stroke-width:1

"

/>

svg

Try it »

Click to view: View SVG File.

Example 3

Use The element creates a star:

Below is the SVG code:

Example

<

svg

height

=

"

210

"

width

=

"

500

"

<

polygon

points

=

"

100,10 40,198 190,78 10,78 160,198

"

style

=

"

fill:lime;stroke:purple;stroke-width:5;fill-rule:nonzero;

"

/>

svg

Try it »

Click to view: View the SVG file.


Example 4

Change the fill-rule attribute to "evenodd":

Below is the SVG code:

Example

<

svg

height

=

"

210

"

width

=

"

500

"

<

polygon

points

=

"

100,10 40,198 190,78 10,78 160,198

"

style

=

"

fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;

"

/>

svg

Try it »

Click to view:

View SVG File

.