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

E
esterzhou 已提交
3
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 已提交
4

Z
zengyawen 已提交
5

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

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

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

Z
zengyawen 已提交
14

Z
zengyawen 已提交
15
## Child Components
Z
zengyawen 已提交
16

E
esterzhou 已提交
17
This component can contain child components.
Z
zengyawen 已提交
18 19


Z
zengyawen 已提交
20
## APIs
Z
zengyawen 已提交
21

E
esterzhou 已提交
22
Shape(value?: PixelMap)
Z
zengyawen 已提交
23

Z
zengyawen 已提交
24
- Parameters
E
esterzhou 已提交
25
  | Name| Type| Mandatory| Default Value| Description| 
Z
zengyawen 已提交
26
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
27
  | 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 已提交
28 29


Z
zengyawen 已提交
30
## Attributes
Z
zengyawen 已提交
31

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

E
esterzhou 已提交
34 35 36
| 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.|
E
ester.zhou 已提交
37 38 39
| fill | [ResourceColor](ts-types.md#resourcecolor) | Color.Black | No| Color of the fill area.|
| fillOpacity | number \| string \| [Resource](ts-types.md#resource)| 1 | No| Opacity of the fill area.|
| stroke | [ResourceColor](ts-types.md#resourcecolor) | Color.Black | No| Stroke color.|
E
esterzhou 已提交
40 41 42 43 44
| 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.|
E
ester.zhou 已提交
45
| strokeOpacity | number \| string \| [Resource](ts-types.md#resource)| 1 | No| Stroke opacity.|
E
esterzhou 已提交
46 47 48
| 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 已提交
49

Z
zengyawen 已提交
50
## Example
Z
zengyawen 已提交
51

Mr-YX's avatar
Mr-YX 已提交
52
```ts
E
esterzhou 已提交
53
// xxx.ets
Z
zengyawen 已提交
54 55 56 57 58
@Entry
@Component
struct ShapeExample {
  build() {
    Column({ space: 5 }) {
Z
zengyawen 已提交
59
      Text('basic').fontSize(30).fontColor(0xCCCCCC).width(320)
Z
zengyawen 已提交
60 61
      // 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 已提交
62
      // 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 已提交
63 64 65 66 67
      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 已提交
68
      .viewPort({ x: -2, y: -2, width: 304, height: 130 })
Z
zengyawen 已提交
69 70 71 72 73 74 75 76
      .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 已提交
77
      Text('border').fontSize(30).fontColor(0xCCCCCC).width(320).margin({top:30})
Z
zengyawen 已提交
78 79 80
      // 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 已提交
81
      }.viewPort({ x: 0, y: -5, width: 300, height: 20 }).stroke(0xEE8443).strokeWidth(10).strokeDashArray([20])
E
esterzhou 已提交
82
      // 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 已提交
83 84 85
      Shape() {
        Path().width(300).height(10).commands('M0 0 L900 0')
      }
Z
zengyawen 已提交
86
      .viewPort({ x: 0, y: -5, width: 300, height: 20 })
Z
zengyawen 已提交
87 88 89 90
      .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 已提交
91
      }.viewPort({ x: 0, y: -5, width: 300, height: 20 }).stroke(0xEE8443).strokeWidth(10).strokeOpacity(0.5)
Z
zengyawen 已提交
92 93 94 95
      // 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 已提交
96
      .viewPort({ x: 0, y: -5, width: 300, height: 20 })
Z
zengyawen 已提交
97 98 99
      .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 已提交
100
        Rect().width(300).height(100)
Z
zengyawen 已提交
101
      }
Z
zengyawen 已提交
102
      .viewPort({ x: -5, y: -5, width: 310, height: 120 })
Z
zengyawen 已提交
103 104 105 106 107 108 109 110 111 112 113 114
      .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 已提交
115
![en-us_image_0000001257058393](figures/en-us_image_0000001257058393.png)