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

Z
zengyawen 已提交
3

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

Z
zengyawen 已提交
8

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


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

None

Z
zengyawen 已提交
16 17

## Child Components
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31
Not supported

## APIs

Path(value?: { width?: number | string, height?: number | string, commands?: string })

- Parameters

  | me       | Type             | Mandatory | Default Value | Description                                        |
  | -------- | ---------------- | --------- | ------------- | -------------------------------------------------- |
  | width    | number \| string | No        | 0             | Width of the rectangle where the path is located.  |
  | height   | number \| string | No        | 0             | Height of the rectangle where the path is located. |
  | commands | string           | No        | ''            | Command string for drawing the path.               |
Z
zengyawen 已提交
32

Z
zengyawen 已提交
33 34 35

## Attributes

E
ester.zhou 已提交
36
| Name | Type | Default Value | Mandatory | Description |
Z
zengyawen 已提交
37
| -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
38 39 40
| width | number \| string | 0 | No | Width of the rectangle where the path is located. |
| height | number \| string | 0 | No | Height of the rectangle where the path is located. |
| commands | string | '' | No | Command string for drawing the path. |
Z
zengyawen 已提交
41

Z
zengyawen 已提交
42 43 44 45

The supported commands are as follows:


Z
zengyawen 已提交
46 47 48 49 50 51 52
- M = moveto

- L = lineto

- H = horizontal lineto

- V = vertical lineto
Z
zengyawen 已提交
53

Z
zengyawen 已提交
54
- C = curveto
Z
zengyawen 已提交
55

Z
zengyawen 已提交
56 57
- S = smooth curveto

E
ester.zhou 已提交
58
- Q = quadratic Belzier curve
Z
zengyawen 已提交
59

E
ester.zhou 已提交
60
- T = smooth quadratic Belzier curveto
Z
zengyawen 已提交
61 62 63 64 65 66 67 68 69 70 71

- 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

E
ester.zhou 已提交
72

Z
zengyawen 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
```
@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 })
  }
}
```

Z
zengyawen 已提交
104
![en-us_image_0000001212058492](figures/en-us_image_0000001212058492.png)