ts-drawing-components-path.md 2.6 KB
Newer Older
Z
zengyawen 已提交
1
# Path
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3

Mr-YX's avatar
Mr-YX 已提交
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

Z
zengyawen 已提交
7

8
The **<Path\>** component is used to draw a path.
Z
zengyawen 已提交
9 10 11


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

None

Z
zengyawen 已提交
15 16

## Child Components
Z
zengyawen 已提交
17 18 19

None

Z
zengyawen 已提交
20 21 22

## Attributes

Mr-YX's avatar
Mr-YX 已提交
23
| Name | Type | Default Value | Mandatory | Description | 
Z
zengyawen 已提交
24
| -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
25 26
| width | Length | 0 | No | Width of the rectangle where the path is located. | 
| height | Length | 0 | No | Height of the rectangle where the path is located. | 
27
| commands | string | '' | Yes | Commands for drawing the path. | 
Z
zengyawen 已提交
28

Z
zengyawen 已提交
29 30 31 32

The supported commands are as follows:


Z
zengyawen 已提交
33 34 35 36 37 38 39
- M = moveto

- L = lineto

- H = horizontal lineto

- V = vertical lineto
Z
zengyawen 已提交
40

Z
zengyawen 已提交
41
- C = curveto
Z
zengyawen 已提交
42

Z
zengyawen 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
- S = smooth curveto

- Q = quadratic Bezier curve

- T = smooth quadratic Bezier curveto

- A = elliptical Arc

- Z = closepath


For example, the command **M0 20 L50 50 L50 100 Z** defines a path that starts from (0, 20), reaches (50, 50) and then (50, 100), and ends at (0, 20).


## Example

  
Mr-YX's avatar
Mr-YX 已提交
60
```ts
Z
zengyawen 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
@Entry
@Component
struct PathExample {
  build() {
    Column({ space: 5 }) {
      Text('Straight line').fontSize(9).fontColor(0xCCCCCC).width('90%')
      Path().width(300).height(10).commands('M0 0 L900 0').stroke(Color.Black).strokeWidth(3)

      Text('Straight line graph').fontSize(9).fontColor(0xCCCCCC).width('90%')
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        // Run MoveTo(150, 0), LineTo(300, 300), LineTo(0, 300), and ClosePath() in sequence.
        Path().width(100).height(100).commands('M150 0 L300 300 L0 300 Z')
        // Run MoveTo(0, 0), HorizontalLineto(300), VerticalLineto(300), HorizontalLineto(0), and ClosePath() in sequence.
        Path().width(100).height(100).commands('M0 0 H300 V300 H0 Z')
        // Run MoveTo(150, 0), LineTo(0, 150), LineTo(60, 300), LineTo(240, 300), LineTo(300, 150), and ClosePath() in sequence.
        Path().width(100).height(100).commands('M150 0 L0 150 L60 300 L240 300 L300 150 Z')
      }.width('100%')

      Text('Curve graphics').fontSize(9).fontColor(0xCCCCCC).width('90%')
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        / / Run MoveTo(0, 300), draw a curve between (150, 0) and (300, 300), and then run ClosePath().
        Path().width(100).height(100).commands("M0 300 S150 0 300 300 Z")
        / / Run MoveTo(0, 150), draw a curve among (0, 150), (150, 0), and (300, 150), and then run ClosePath().
        Path().width(100).height(100).commands('M0 150 C0 150 150 0 300 150 L150 300 Z')
      }
    }.width('100%').margin({ top: 5 })
  }
}
```

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