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:
-
pointsThe attribute defines the coordinates of each vertex of the polygon; multiple vertex coordinates are separated by spaces or commas, and each pair of coordinates is separated by a comma. -
fillThe attribute defines the fill color of the polygon. -
strokeThe attribute defines the border color of the polygon. -
stroke-widthThe attribute defines the border width of the polygon.
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
A polygon is made up of straight lines, and its shape is "closed" (all the lines connect together).
polygon 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:
- The points attribute defines the x and y coordinates of each corner of the polygon
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
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
.