SVG (Scalable Vector Graphics) polygon
elements are used to draw closed shapes by specifying a set of points. Each point is defined by its x
(horizontal) and y
(vertical) coordinates within the SVG coordinate system. The points
attribute of the polygon
element is used to define the coordinates of the vertices of the polygon.
The points
attribute contains a list of x and y coordinate pairs separated by spaces. Each coordinate pair is separated by a comma or space. For example:
html<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<polygon points="10,10 50,10 50,50 10,50" fill="blue" />
</svg>
In this example, we draw a blue square with the polygon
element. The points
attribute contains four coordinate pairs: (10, 10), (50, 10), (50, 50), and (10, 50). The square is closed, meaning it connects the last point back to the first point, creating a closed shape.
To draw more complex shapes, you can add more coordinate pairs to the points
attribute, separated by spaces. For example, to draw a triangle, you need three coordinate pairs, and for a pentagon, you need five coordinate pairs, and so on.
Here's an example of a triangle:
html<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<polygon points="10,10 50,10 30,50" fill="green" />
</svg>
In this example, we draw a green triangle with the polygon
element. The points
attribute contains three coordinate pairs: (10, 10), (50, 10), and (30, 50).
SVG's polygon
elements are a powerful way to create custom shapes and graphics, and you can use a combination of polygon
elements and other SVG elements (such as rect
, circle
, etc.) to build complex visuals.