SVG <polygon>

SVG 多邊形 - <polygon>

<polygon> 元素用于創建包含至少三個邊的圖形。

多邊形是由直線組成的,并且形狀是“封閉的”(所有線都連接起來)。

Polygon 一詞源自希臘語。"Poly" 意為“許多”,"gon" 意為“角度”。

例子 1

下例創建擁有三個邊的多邊形:

這是 SVG 代碼:

<svg height="210" width="500">
  <polygon points="200,10 250,190 160,210" style="fill:lime;stroke:purple;stroke-width:1" />
</svg>

親自試一試

代碼解釋:

  • points 屬性定義多邊形每個角的 x 和 y 坐標

例子 2

下例創建擁有四個邊的多邊形:

這是 SVG 代碼:

<svg height="250" width="500">
 <polygon points="220,10 300,210 170,250 123,234" style="fill:lime;stroke:purple;stroke-width:1" />
</svg>

親自試一試

例子 3

使用 <polygon> 元素創建一顆星:

這是 SVG 代碼:

<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>

親自試一試

例子 4

將 fill-rule 屬性更改為 "evenodd"

這是 SVG 代碼:

<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>

親自試一試