• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Polygon
2
3The **\<Polygon>** component is used to draw a polygon.
4
5>  **NOTE**
6>
7>  This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
8
9
10## Child Components
11
12Not supported
13
14
15## APIs
16
17Polygon(value?: {width?: string | number, height?: string | number})
18
19Since API version 9, this API is supported in ArkTS widgets.
20
21**Parameters**
22
23| Name| Type| Mandatory| Default Value| Description|
24| -------- | -------- | -------- | -------- | -------- |
25| width | string \| number | No| 0 | Width.<br>**NOTE**<br>An invalid value is handled as the default value.|
26| height | string \| number | No| 0 | Height.<br>**NOTE**<br>An invalid value is handled as the default value.|
27
28
29
30## Attributes
31
32In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
33
34| Name| Type| Default Value| Description|
35| -------- | -------- | -------- | -------- |
36| points | Array&lt;Point&gt; | [] | Vertex coordinates of the polygon.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>An invalid value is handled as the default value.|
37| fill | [ResourceColor](ts-types.md) | Color.Black | Color of the fill area.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>An invalid value is handled as the default value.|
38| fillOpacity | number \| string \| [Resource](ts-types.md#resource)| 1 | Opacity of the fill area.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>An invalid value is handled as the default value.|
39| stroke | [ResourceColor](ts-types.md) | - | Stroke color. If this attribute is not set, the component does not have any stroke.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>If the value is invalid, no stroke will be drawn.|
40| strokeDashArray | Array&lt;Length&gt; | [] | Stroke dashes.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>An invalid value is handled as the default value.|
41| strokeDashOffset | number \| string | 0 | Offset of the start point for drawing the stroke.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>An invalid value is handled as the default value.|
42| strokeLineCap | [LineCapStyle](ts-appendix-enums.md#linecapstyle) | LineCapStyle.Butt | Cap style of the stroke.<br>Since API version 9, this API is supported in ArkTS widgets.|
43| strokeLineJoin | [LineJoinStyle](ts-appendix-enums.md#linejoinstyle) | LineJoinStyle.Miter | Join style of the stroke.<br>Since API version 9, this API is supported in ArkTS widgets.|
44| strokeMiterLimit | number \| string | 4 | Limit on the ratio of the miter length to the value of **strokeWidth** used to draw a miter join. The miter length indicates the distance from the outer tip to the inner corner of the miter.<br>**NOTE**<br>This attribute works only when **strokeLineJoin** is set to **LineJoinStyle.Miter**.<br>The value must be greater than or equal to 1.0. If the value is in the [0, 1) range, the value **1.0** will be used. In other cases, the default value will be used.<br>Since API version 9, this API is supported in ArkTS widgets.|
45| strokeOpacity | number \| string \| [Resource](ts-types.md#resource)| 1 | Stroke opacity.<br>**NOTE**<br>The value range is [0.0, 1.0]. A value less than 0.0 evaluates to the value **0.0**. A value greater than 1.0 evaluates to the value **1.0**. Any other value evaluates to the value **1.0**.<br>Since API version 9, this API is supported in ArkTS widgets.|
46| strokeWidth | Length | 1 | Stroke width.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>The value cannot be a percentage.<br>An invalid value is handled as the default value.|
47| antiAlias | boolean | true | Whether anti-aliasing is enabled.<br>Since API version 9, this API is supported in ArkTS widgets.|
48
49## Point
50
51Describes the coordinates of a point.
52
53Since API version 9, this API is supported in ArkTS widgets.
54
55| Name     | Type            | Description                                                        |
56| --------- | -------------------- | ------------------------------------------------------------ |
57| Point | [number, number] | Coordinates of a point. The first parameter is the x-coordinate, and the second parameter is the y-coordinate (relative coordinate).|
58
59
60## Example
61
62```ts
63// xxx.ets
64@Entry
65@Component
66struct PolygonExample {
67  build() {
68    Column({ space: 10 }) {
69      // Draw a triangle in a 100 x 100 rectangle. The start point is (0, 0), the end point is (100, 0), and the passing point is (50, 100).
70      Polygon({ width: 100, height: 100 })
71        .points([[0, 0], [50, 100], [100, 0]])
72        .fill(Color.Green)
73      // Draw a quadrilateral in a 100 x 100 rectangle. The start point is (0, 0), the end point is (100, 0), and the passing points are (0, 100) and (100, 100).
74      Polygon().width(100).height(100)
75        .points([[0, 0], [0, 100], [100, 100], [100, 0]])
76        .fillOpacity(0)
77        .strokeWidth(5)
78        .stroke(Color.Blue)
79      // Draw a pentagon in a 100 x 100 rectangle. The start point is (50, 0), the end point is (100, 50), and the passing points are (0, 50), (20, 100), and (80, 100).
80      Polygon().width(100).height(100)
81        .points([[50, 0], [0, 50], [20, 100], [80, 100], [100, 50]])
82        .fill(Color.Red)
83        .fillOpacity(0.6)
84    }.width('100%').margin({ top: 10 })
85  }
86}
87```
88
89![en-us_image_0000001174582856](figures/en-us_image_0000001174582856.png)
90