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

Z
zengyawen 已提交
3

4
> **NOTE**<br>
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


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

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


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

Z
zengyawen 已提交
24
The **&lt;Shape&gt;** component can contain child components.
Z
zengyawen 已提交
25 26


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

Z
zengyawen 已提交
29
Shape(value:{target?: PixelMap})
Z
zengyawen 已提交
30

Z
zengyawen 已提交
31
- Parameters
32
  | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
33
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
34
  | target | PixelMap | No | null | 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

39
| Name | Type | Default Value | Mandatory | Description | 
Z
zengyawen 已提交
40
| -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
41 42 43 44 45 46 47 48 49 50 51
| viewPort | {<br/>x: Length,<br/>y: Length,<br/>width: Length,<br/>height: Length<br/>} | - | Yes | View port of the shape. | 
| fill | Color | Black | No | Fill color. | 
| stroke | Color | - | No | Stroke color. | 
| strokeDashArray | Array&lt;Length&gt; | [] | No | Stroke dash. | 
| strokeDashOffset | Length | 0 | No | Offset of the start point for drawing the stroke. | 
| strokeLineCap | LineCapStyle | LineCapStyle.Butt | No | Cap style of the stroke. | 
| strokeLineJoin | LineJoinStyle | LineJoinStyle.Miter | No | Join style of the stroke. | 
| strokeMiterLimit | number | 4 | No | Limit value when the sharp angle is drawn as a miter. | 
| strokeOpacity | number | 1 | No | Stroke opacity. | 
| strokeWidth | Length | 1 | No | Stroke width. | 
| antiAlias | boolean | true | No | Whether anti-aliasing is enabled. | 
Z
zengyawen 已提交
52

Z
zengyawen 已提交
53
- LineJoinStyle enums
54
  | Name | Description | 
Z
zengyawen 已提交
55
  | -------- | -------- |
E
esterzhou 已提交
56 57 58
  | Bevel | Bevel is used to connect paths. | 
  | Miter | Miter is used to connect paths. | 
  | Round | Round is used to connect paths. | 
Z
zengyawen 已提交
59 60


Z
zengyawen 已提交
61
## Example
Z
zengyawen 已提交
62

Z
zengyawen 已提交
63
  
Mr-YX's avatar
Mr-YX 已提交
64
```ts
Z
zengyawen 已提交
65 66 67 68 69
@Entry
@Component
struct ShapeExample {
  build() {
    Column({ space: 5 }) {
Z
zengyawen 已提交
70
      Text('basic').fontSize(30).fontColor(0xCCCCCC).width(320)
Z
zengyawen 已提交
71 72 73 74 75 76 77 78
      // 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).
      // 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, and the join style is rounded, and anti-aliasing is enabled (default).
      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 已提交
79
      .viewPort({ x: -2, y: -2, width: 304, height: 130 })
Z
zengyawen 已提交
80 81 82 83 84 85 86 87
      .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 已提交
88
      Text('border').fontSize(30).fontColor(0xCCCCCC).width(320).margin({top:30})
Z
zengyawen 已提交
89 90 91
      // 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 已提交
92
      }.viewPort({ x: 0, y: -5, width: 300, height: 20 }).stroke(0xEE8443).strokeWidth(10).strokeDashArray([20])
Z
zengyawen 已提交
93 94 95 96
      // Draw a 300 x 10 path at (0, -5). The fill color is 0xEE8443, the stroke width is 10, the stroke gap is 20, and the offset is 10 to the left.
      Shape() {
        Path().width(300).height(10).commands('M0 0 L900 0')
      }
Z
zengyawen 已提交
97
      .viewPort({ x: 0, y: -5, width: 300, height: 20 })
Z
zengyawen 已提交
98 99 100 101
      .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 已提交
102
      }.viewPort({ x: 0, y: -5, width: 300, height: 20 }).stroke(0xEE8443).strokeWidth(10).strokeOpacity(0.5)
Z
zengyawen 已提交
103 104 105 106
      // 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 已提交
107
      .viewPort({ x: 0, y: -5, width: 300, height: 20 })
Z
zengyawen 已提交
108 109 110
      .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 已提交
111
        Rect().width(300).height(100)
Z
zengyawen 已提交
112
      }
Z
zengyawen 已提交
113
      .viewPort({ x: -5, y: -5, width: 310, height: 120 })
Z
zengyawen 已提交
114 115 116 117 118 119 120 121 122 123 124 125
      .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 })
  }
}
```

Mr-YX's avatar
Mr-YX 已提交
126
![en-us_image_0000001257058393](figures/en-us_image_0000001257058393.png)