ts-drawing-components-polygon.md 3.4 KB
Newer Older
Z
zengyawen 已提交
1
# Polygon
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3

E
ester.zhou 已提交
4
The **\<Polygon>** component is used to draw a polygon.
Z
zengyawen 已提交
5

E
ester.zhou 已提交
6 7 8
>  **NOTE**
>
>  This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
9 10 11


## Required Permissions
Z
zengyawen 已提交
12 13 14

None

Z
zengyawen 已提交
15 16

## Child Components
Z
zengyawen 已提交
17

E
ester.zhou 已提交
18
Not supported
Z
zengyawen 已提交
19 20


Z
zengyawen 已提交
21 22
## APIs

E
ester.zhou 已提交
23
Polygon(options?: {width?: string | number, height?: string | number})
Z
zengyawen 已提交
24 25

- Parameters
E
ester.zhou 已提交
26
  | Name| Type| Mandatory| Default Value| Description|
Z
zengyawen 已提交
27
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
28 29
  | width | string \| number | No| 0 | Width of the polygon. |
  | height | string \| number | No| 0 | Height of the polygon. |
Z
zengyawen 已提交
30 31 32 33


## Attributes

E
ester.zhou 已提交
34 35 36
In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.

| Name| Type| Default Value| Mandatory| Description|
Z
zengyawen 已提交
37
| -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| points | Array&lt;Point&gt; | [] | No| Vertex coordinates of the polygon.|
| fill | [ResourceColor](../../ui/ts-types.md) | Color.Black | No| Color of the fill area.|
| fillOpacity | number \| string \| [Resource](../../ui/ts-types.md#resource)| 1 | No| Opacity of the fill area.|
| stroke | [ResourceColor](../../ui/ts-types.md) | Color.Black | No| Stroke color.|
| strokeDashArray | Array&lt;Length&gt; | [] | No| Stroke dashes.|
| strokeDashOffset | number \| string | 0 | No| Offset of the start point for drawing the stroke.|
| strokeLineCap | [LineCapStyle](ts-appendix-enums.md#linecapstyle) | LineCapStyle.Butt | No| Cap style of the stroke.|
| strokeLineJoin | [LineJoinStyle](ts-appendix-enums.md#linejoinstyle) | LineJoinStyle.Miter | No| Join style of the stroke.|
| strokeMiterLimit | number \| string | 4 | No| Limit value when the sharp angle is drawn as a miter.|
| strokeOpacity | number \| string \| [Resource](../../ui/ts-types.md#resource)| 1 | No| Stroke opacity.|
| strokeWidth | Length | 1 | No| Stroke width.|
| antiAlias | boolean | true | No| Whether anti-aliasing is enabled.|

## Point

Describes the coordinates of a point.

| Name     | Type            | Description                                                        |
| --------- | -------------------- | ------------------------------------------------------------ |
| Point | [number, number] | Coordinates of a point. The first parameter is the x-coordinate, and the second parameter is the y-coordinate (relative coordinate).|
Z
zengyawen 已提交
58 59 60 61


## Example

Mr-YX's avatar
Mr-YX 已提交
62
```ts
E
ester.zhou 已提交
63
// xxx.ets
Z
zengyawen 已提交
64 65 66 67 68 69 70 71
@Entry
@Component
struct PolygonExample {
  build() {
    Column({ space: 5 }) {
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        // 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).
        Polygon({ width: 100, height: 100 }).points([[0, 0], [50, 100], [100, 0]])
E
ester.zhou 已提交
72
        // 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).
Z
zengyawen 已提交
73 74 75 76 77 78 79 80 81
        Polygon().width(100).height(100).points([[0, 0], [0, 100], [100, 100], [100, 0]])
        // 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).
        Polygon().width(100).height(100).points([[50, 0], [0, 50], [20, 100], [80, 100], [100, 50]])
      }.width('100%')
    }.margin({ top: 5 })
  }
}
```

Mr-YX's avatar
Mr-YX 已提交
82
![en-us_image_0000001212218458](figures/en-us_image_0000001212218458.gif)