ts-drawing-components-shape.md 6.7 KB
Newer Older
Z
zengyawen 已提交
1
# Shape
Z
zengyawen 已提交
2

E
esterzhou 已提交
3 4
>  **NOTE**
> 
Z
zengyawen 已提交
5
> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6 7


E
esterzhou 已提交
8
The **\<Shape>** component is the parent component of the drawing components. The attributes described in this topic are universal attributes supported by all the drawing components.
Z
zengyawen 已提交
9

Z
zengyawen 已提交
10

E
esterzhou 已提交
11
1. Drawing components use **\<Shape>** as their parent to implement the effect similar to SVG.
Z
zengyawen 已提交
12 13


E
esterzhou 已提交
14
2. The **\<Shape>** component is used independently to draw a specific shape.
Z
zengyawen 已提交
15 16 17


## Required Permissions
Z
zengyawen 已提交
18 19 20

None

Z
zengyawen 已提交
21

Z
zengyawen 已提交
22
## Child Components
Z
zengyawen 已提交
23

E
esterzhou 已提交
24
This component can contain child components.
Z
zengyawen 已提交
25 26


Z
zengyawen 已提交
27
## APIs
Z
zengyawen 已提交
28

E
esterzhou 已提交
29
Shape(value?: PixelMap)
Z
zengyawen 已提交
30

Z
zengyawen 已提交
31
- Parameters
E
esterzhou 已提交
32
  | Name| Type| Mandatory| Default Value| Description| 
Z
zengyawen 已提交
33
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
34
  | value | PixelMap | No| - | Shape to draw. You can draw a shape in the specified **PixelMap** object. If no object is specified, the shape is drawn in the current drawing target.| 
Z
zengyawen 已提交
35 36


Z
zengyawen 已提交
37
## Attributes
Z
zengyawen 已提交
38

E
esterzhou 已提交
39
In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
Z
zengyawen 已提交
40

E
esterzhou 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| Name| Type| Default Value| Mandatory| Description|
| -------- | -------- | -------- | -------- | -------- |
| viewPort | {<br>x?: number \| string,<br>y?: number \| string,<br>width?: number \| string,<br>height?: number \| string<br>} | { x:0, y:0, width:0, height:0 } | No| View port of the shape.|
| fill | [ResourceColor](../../ui/ts-types.md) | Color.Black | No| Color of the fill area.|
| fillOpacity | number \| string \| [Resource](../../ui/ts-types.md#resource-type)| 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 dash.|
| 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-type)| 1 | No| Stroke opacity.|
| strokeWidth | number \| string | 1 | No| Stroke width.|
| antiAlias | boolean | true | No| Whether anti-aliasing is enabled.|
| mesh<sup>8+</sup> | Array&lt;number&gt;,number,number | [],0,0 | No| Mesh effect. The first parameter is an array of lengths (column + 1) * (row + 1) * 2, which records the position of each vertex of the distorted bitmap. The second parameter is the number of columns in the mesh matrix. The third parameter is the number of rows in the mesh matrix.|
Z
zengyawen 已提交
56

Z
zengyawen 已提交
57
## Example
Z
zengyawen 已提交
58

Mr-YX's avatar
Mr-YX 已提交
59
```ts
E
esterzhou 已提交
60
// xxx.ets
Z
zengyawen 已提交
61 62 63 64 65
@Entry
@Component
struct ShapeExample {
  build() {
    Column({ space: 5 }) {
Z
zengyawen 已提交
66
      Text('basic').fontSize(30).fontColor(0xCCCCCC).width(320)
Z
zengyawen 已提交
67 68
      // Draw a 300 x 50 rectangle with strokes at (-2, -2). The fill color is 0x317Af7, the stroke color is black, the stroke width is 4, the stroke dash is 20, the offset is 10 to the left, the cap style is rounded, the join style is rounded, and anti-aliasing is enabled (default).
      // Draw a 300 x 50 ellipse with strokes at (-2, 58). The fill color is 0x317Af7, the stroke color is black, the stroke width is 4, the stroke dash is 20, the offset is 10 to the left, the cap style is rounded, the join style is rounded, and anti-aliasing is enabled (default).
E
esterzhou 已提交
69
      // Draw a 300 x 10 path at (-2, 118). The fill color is 0x317Af7, the stroke color is black, the stroke width is 4, the stroke dash is 20, the offset is 10 to the left, the cap style is rounded, the join style is rounded, and anti-aliasing is enabled (default).
Z
zengyawen 已提交
70 71 72 73 74
      Shape() {
        Rect().width(300).height(50)
        Ellipse().width(300).height(50).offset({ x: 0, y: 60 })
        Path().width(300).height(10).commands('M0 0 L900 0').offset({ x: 0, y: 120 })
      }
Z
zengyawen 已提交
75
      .viewPort({ x: -2, y: -2, width: 304, height: 130 })
Z
zengyawen 已提交
76 77 78 79 80 81 82 83
      .fill(0x317Af7).stroke(Color.Black).strokeWidth(4)
      .strokeDashArray([20]).strokeDashOffset(10).strokeLineCap(LineCapStyle.Round)
      .strokeLineJoin(LineJoinStyle.Round).antiAlias(true)
      // Draw a 300 x 50 rectangle with strokes at (-1, -1). The fill color is 0x317Af7, the stroke color is black, and the stroke width is 2.
      Shape() {
        Rect().width(300).height(50)
      }.viewPort({ x: -1, y: -1, width: 302, height: 52 }).fill(0x317Af7).stroke(Color.Black).strokeWidth(2)

Z
zengyawen 已提交
84
      Text('border').fontSize(30).fontColor(0xCCCCCC).width(320).margin({top:30})
Z
zengyawen 已提交
85 86 87
      // Draw a 300 x 10 path at (0, -5). The color is 0xEE8443, the stroke width is 10, and the stroke gap is 20.
      Shape() {
        Path().width(300).height(10).commands('M0 0 L900 0')
Z
zengyawen 已提交
88
      }.viewPort({ x: 0, y: -5, width: 300, height: 20 }).stroke(0xEE8443).strokeWidth(10).strokeDashArray([20])
E
esterzhou 已提交
89
      // Draw a 300 x 10 path at (0, -5). The fill color is 0xEE8443, the stroke width is 10, the stroke dash is 20, and the offset is 10 to the left.
Z
zengyawen 已提交
90 91 92
      Shape() {
        Path().width(300).height(10).commands('M0 0 L900 0')
      }
Z
zengyawen 已提交
93
      .viewPort({ x: 0, y: -5, width: 300, height: 20 })
Z
zengyawen 已提交
94 95 96 97
      .stroke(0xEE8443).strokeWidth(10).strokeDashArray([20]).strokeDashOffset(10)
      // Draw a 300 x 10 path at (0, -5). The fill color is 0xEE8443, the stroke width is 10, and the stroke opacity is 0.5.
      Shape() {
        Path().width(300).height(10).commands('M0 0 L900 0')
Z
zengyawen 已提交
98
      }.viewPort({ x: 0, y: -5, width: 300, height: 20 }).stroke(0xEE8443).strokeWidth(10).strokeOpacity(0.5)
Z
zengyawen 已提交
99 100 101 102
      // Draw a 300 x 10 path at (0, -5). The fill color is 0xEE8443, the stroke width is 10, the stroke dash is 20, the offset is 10 to the left, and the cap style is rounded.
      Shape() {
        Path().width(300).height(10).commands('M0 0 L900 0')
      }
Z
zengyawen 已提交
103
      .viewPort({ x: 0, y: -5, width: 300, height: 20 })
Z
zengyawen 已提交
104 105 106
      .stroke(0xEE8443).strokeWidth(10).strokeDashArray([20]).strokeLineCap(LineCapStyle.Round)
      // Draw a 300 x 50 rectangle with strokes at (-5, -5). The fill color is 0x317Af7, the stroke width is 10, the stroke color is 0xEE8443, and the join style is rounded.
      Shape() {
Z
zengyawen 已提交
107
        Rect().width(300).height(100)
Z
zengyawen 已提交
108
      }
Z
zengyawen 已提交
109
      .viewPort({ x: -5, y: -5, width: 310, height: 120 })
Z
zengyawen 已提交
110 111 112 113 114 115 116 117 118 119 120 121
      .fill(0x317Af7).stroke(0xEE8443).strokeWidth(10).strokeLineJoin(LineJoinStyle.Round)
      Shape() {
        Path().width(300).height(60).commands('M0 0 L400 0 L400 200 Z')
      }
      .viewPort({ x: -80, y: -5, width: 310, height: 100 })
      .fill(0x317Af7).stroke(0xEE8443).strokeWidth(10)
      .strokeLineJoin(LineJoinStyle.Miter).strokeMiterLimit(5)
    }.width('100%').margin({ top: 15 })
  }
}
```

E
esterzhou 已提交
122
![en-us_image_0000001257058393](figures/en-us_image_0000001257058393.png)