ts-components-canvas-path2d.md 14.5 KB
Newer Older
Z
zengyawen 已提交
1
# Path2D
Z
zengyawen 已提交
2 3


E
esterzhou 已提交
4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
Z
zengyawen 已提交
5
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6 7


Z
zengyawen 已提交
8 9 10 11 12 13
**Path2D** allows you to describe a path through an existing path. This path can be drawn through the **stroke** API of **Canvas**.


## addPath

addPath(path: Object): void
Z
zengyawen 已提交
14 15 16

Adds a path to this path.

Z
zengyawen 已提交
17
- Parameters
E
esterzhou 已提交
18
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
19
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
20
  | path | Object | Yes | null | Path to be added to this path. | 
Z
zengyawen 已提交
21 22

- Example
Z
zengyawen 已提交
23
    
Z
zengyawen 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  ```
  @Entry
  @Component
  struct AddPath {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  
    private path2Da: Path2D = new Path2D("M250 150 L150 350 L350 350 Z")
    private path2Db: Path2D = new Path2D()
  
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.path2Db.addPath(this.path2Da)
            this.context.stroke(this.path2Db)
          })
Z
zengyawen 已提交
44
      }
Z
zengyawen 已提交
45 46
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
47
    }
Z
zengyawen 已提交
48 49
  }
  ```
Z
zengyawen 已提交
50

Z
zengyawen 已提交
51
  ![en-us_image_0000001211898520](figures/en-us_image_0000001211898520.png)
Z
zengyawen 已提交
52 53


Z
zengyawen 已提交
54
## closePath
Z
zengyawen 已提交
55

Z
zengyawen 已提交
56
closePath(): void
Z
zengyawen 已提交
57

Z
zengyawen 已提交
58
Moves the current point of the path back to the start point of the path, and draws a straight line between the current point and the start point. If the shape has already been closed or has only one point, this method does nothing.
Z
zengyawen 已提交
59

Z
zengyawen 已提交
60
- Example
Z
zengyawen 已提交
61
    
Z
zengyawen 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  ```
  @Entry
  @Component
  struct ClosePath {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private path2Db: Path2D = new Path2D()
  
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.path2Db.moveTo(200, 100)
            this.path2Db.lineTo(300, 100)
            this.path2Db.lineTo(200, 200)
            this.path2Db.closePath()
            this.context.stroke(this.path2Db)
          })
Z
zengyawen 已提交
83
      }
Z
zengyawen 已提交
84 85
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
86
    }
Z
zengyawen 已提交
87 88
  }
  ```
Z
zengyawen 已提交
89

Z
zengyawen 已提交
90
  ![en-us_image_0000001212218482](figures/en-us_image_0000001212218482.png)
Z
zengyawen 已提交
91 92


Z
zengyawen 已提交
93
## moveTo
Z
zengyawen 已提交
94

Z
zengyawen 已提交
95
moveTo(x: number, y: number): void
Z
zengyawen 已提交
96 97 98

Moves the current coordinate point of the path to the target point, without drawing a line during the movement.

Z
zengyawen 已提交
99
- Parameters
E
esterzhou 已提交
100
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
101
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
102 103
  | x | number | Yes | 0 | X-coordinate of the target point. | 
  | y | number | Yes | 0 | Y-coordinate of the target point. | 
Z
zengyawen 已提交
104 105

- Example
Z
zengyawen 已提交
106
    
Z
zengyawen 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  ```
  @Entry
  @Component
  struct MoveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private path2Db: Path2D = new Path2D()
  
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.path2Db.moveTo(50, 100)
            this.path2Db.lineTo(250, 100)
            this.path2Db.lineTo(150, 200)
            this.path2Db.closePath()
            this.context.stroke(this.path2Db)
          })
Z
zengyawen 已提交
128
      }
Z
zengyawen 已提交
129 130
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
131
    }
Z
zengyawen 已提交
132 133
  }
  ```
Z
zengyawen 已提交
134

Z
zengyawen 已提交
135
  ![en-us_image_0000001257138389](figures/en-us_image_0000001257138389.png)
Z
zengyawen 已提交
136 137


Z
zengyawen 已提交
138
## lineTo
Z
zengyawen 已提交
139

Z
zengyawen 已提交
140
lineTo(x: number, y: number): void
Z
zengyawen 已提交
141 142 143

Draws a straight line from the current point to the target point.

Z
zengyawen 已提交
144
- Parameters
E
esterzhou 已提交
145
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
146
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
147 148
  | x | number | Yes | 0 | X-coordinate of the target point. | 
  | y | number | Yes | 0 | Y-coordinate of the target point. | 
Z
zengyawen 已提交
149 150

- Example
Z
zengyawen 已提交
151
    
Z
zengyawen 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  ```
  @Entry
  @Component
  struct LineTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private path2Db: Path2D = new Path2D()
  
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.path2Db.moveTo(100, 100)
            this.path2Db.lineTo(100, 200)
            this.path2Db.lineTo(200, 200)
            this.path2Db.lineTo(200, 100)
            this.path2Db.closePath()
            this.context.stroke(this.path2Db)
          })
Z
zengyawen 已提交
174
      }
Z
zengyawen 已提交
175 176
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
177
    }
Z
zengyawen 已提交
178 179
  }
  ```
Z
zengyawen 已提交
180

Z
zengyawen 已提交
181
  ![en-us_image_0000001256858435](figures/en-us_image_0000001256858435.png)
Z
zengyawen 已提交
182 183


Z
zengyawen 已提交
184
## bezierCurveTo
Z
zengyawen 已提交
185

Z
zengyawen 已提交
186
bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void
Z
zengyawen 已提交
187 188 189

Draws a cubic bezier curve on the canvas.

Z
zengyawen 已提交
190
- Parameters
E
esterzhou 已提交
191
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
192
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
193 194 195 196 197 198
  | cp1x | number | Yes | 0 | X-coordinate of the first parameter of the bezier curve. | 
  | cp1y | number | Yes | 0 | Y-coordinate of the first parameter of the bezier curve. | 
  | cp2x | number | Yes | 0 | X-coordinate of the second parameter of the bezier curve. | 
  | cp2y | number | Yes | 0 | Y-coordinate of the second parameter of the bezier curve. | 
  | x | number | Yes | 0 | X-coordinate of the end point on the bezier curve. | 
  | y | number | Yes | 0 | Y-coordinate of the end point on the bezier curve. | 
Z
zengyawen 已提交
199 200

- Example
Z
zengyawen 已提交
201
    
Z
zengyawen 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  ```
  @Entry
  @Component
  struct BezierCurveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private path2Db: Path2D = new Path2D()
  
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.path2Db.moveTo(10, 10)
            this.path2Db.bezierCurveTo(20, 100, 200, 100, 200, 20);this.context.stroke(this.path2Db)
          })
Z
zengyawen 已提交
220
      }
Z
zengyawen 已提交
221 222
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
223
    }
Z
zengyawen 已提交
224 225
  }
  ```
Z
zengyawen 已提交
226

Z
zengyawen 已提交
227
  ![en-us_image_0000001257058445](figures/en-us_image_0000001257058445.png)
Z
zengyawen 已提交
228 229


Z
zengyawen 已提交
230
## quadraticCurveTo
Z
zengyawen 已提交
231

Z
zengyawen 已提交
232
quadraticCurveTo(cpx: number, cpy: number, x: number ,y: number): void
Z
zengyawen 已提交
233 234 235

Draws a quadratic curve on the canvas.

Z
zengyawen 已提交
236
- Parameters
E
esterzhou 已提交
237
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
238
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
239 240 241 242
  | cpx | number | Yes | 0 | X-coordinate of the bezier curve parameter. | 
  | cpy | number | Yes | 0 | Y-coordinate of the bezier curve parameter. | 
  | x | number | Yes | 0 | X-coordinate of the end point on the bezier curve. | 
  | y | number | Yes | 0 | Y-coordinate of the end point on the bezier curve. | 
Z
zengyawen 已提交
243 244

- Example
Z
zengyawen 已提交
245
    
Z
zengyawen 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
  ```
  @Entry
  @Component
  struct QuadraticCurveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private path2Db: Path2D = new Path2D()
  
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.path2Db.moveTo(10, 10)
            this.path2Db.quadraticCurveTo(100, 100, 200, 20)
            this.context.stroke(this.path2Db)
        })
Z
zengyawen 已提交
265
      }
Z
zengyawen 已提交
266 267
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
268
    }
Z
zengyawen 已提交
269 270
  }
  ```
Z
zengyawen 已提交
271

Z
zengyawen 已提交
272
  ![en-us_image_0000001212058512](figures/en-us_image_0000001212058512.png)
Z
zengyawen 已提交
273 274


Z
zengyawen 已提交
275
## arc
Z
zengyawen 已提交
276

Z
zengyawen 已提交
277
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise?: number): void
Z
zengyawen 已提交
278 279 280

Draws an arc on the canvas.

Z
zengyawen 已提交
281
- Parameters
E
esterzhou 已提交
282
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
283
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
284 285 286 287 288 289
  | x | number | Yes | 0 | X-coordinate of the center point of the arc. | 
  | y | number | Yes | 0 | Y-coordinate of the center point of the arc. | 
  | radius | number | Yes | 0 | Radius of the arc. | 
  | startAngle | number | Yes | 0 | Start radian of the arc. | 
  | endAngle | number | Yes | 0 | End radian of the arc. | 
  | anticlockwise | boolean | No | false | Whether to draw the arc counterclockwise. | 
Z
zengyawen 已提交
290 291

- Example
Z
zengyawen 已提交
292
    
Z
zengyawen 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  ```
  @Entry
  @Component
  struct Arc {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private path2Db: Path2D = new Path2D()
  
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.path2Db.arc(100, 75, 50, 0, 6.28);this.context.stroke(this.path2Db)
          })
Z
zengyawen 已提交
310
      }
Z
zengyawen 已提交
311 312
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
313
    }
Z
zengyawen 已提交
314 315
  }
  ```
Z
zengyawen 已提交
316

Z
zengyawen 已提交
317
  ![en-us_image_0000001212378446](figures/en-us_image_0000001212378446.png)
Z
zengyawen 已提交
318 319


Z
zengyawen 已提交
320
## arcTo
Z
zengyawen 已提交
321

Z
zengyawen 已提交
322
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void
Z
zengyawen 已提交
323 324 325

Draws an arc based on the radius and points on the arc.

Z
zengyawen 已提交
326
- Parameters
E
esterzhou 已提交
327
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
328
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
329 330 331 332 333
  | x1 | number | Yes | 0 | X-coordinate of the first point on the arc. | 
  | y1 | number | Yes | 0 | Y-coordinate of the first point on the arc. | 
  | x2 | number | Yes | 0 | X-coordinate of the second point on the arc. | 
  | y2 | number | Yes | 0 | Y-coordinate of the second point on the arc. | 
  | radius | number | Yes | 0 | Radius of the arc. | 
Z
zengyawen 已提交
334 335

- Example
Z
zengyawen 已提交
336
    
Z
zengyawen 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
  ```
  @Entry
  @Component
  struct ArcTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private path2Db: Path2D = new Path2D()
  
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.path2Db.arcTo(150, 20, 150, 70, 50)
            this.context.stroke(this.path2Db)
          })
Z
zengyawen 已提交
355
      }
Z
zengyawen 已提交
356 357
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
358
    }
Z
zengyawen 已提交
359 360
  }
  ```
Z
zengyawen 已提交
361

Z
zengyawen 已提交
362
  ![en-us_image_0000001212058510](figures/en-us_image_0000001212058510.png)
Z
zengyawen 已提交
363 364


Z
zengyawen 已提交
365
## ellipse
Z
zengyawen 已提交
366

Z
zengyawen 已提交
367
ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise?: number): void
Z
zengyawen 已提交
368 369 370

Draws an ellipse in the specified rectangular region.

Z
zengyawen 已提交
371
- Parameters
E
esterzhou 已提交
372
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
373
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
374 375 376 377 378 379 380 381
  | x | number | Yes | 0 | X-coordinate of the ellipse center. | 
  | y | number | Yes | 0 | Y-coordinate of the ellipse center. | 
  | radiusX | number | Yes | 0 | Ellipse radius on the x-axis. | 
  | radiusY | number | Yes | 0 | Ellipse radius on the y-axis. | 
  | rotation | number | Yes | 0 | Rotation angle of the ellipse, in radians. | 
  | startAngle | number | Yes | 0 | Angle of the start point for drawing the ellipse, in radians. | 
  | endAngle | number | Yes | 0 | Angle of the end point for drawing the ellipse, in radians. | 
  | anticlockwise | number | No | 0 | Whether to draw the ellipse in the counterclockwise direction. The value **0** means to draw the ellipse in the clockwise direction, and **1** means to draw the ellipse in the counterclockwise direction. This parameter is optional. The default value is **0**. | 
Z
zengyawen 已提交
382 383

- Example
Z
zengyawen 已提交
384
    
Z
zengyawen 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  ```
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private path2Db: Path2D = new Path2D()
  
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.path2Db.ellipse(200, 200, 50, 100, Math.PI * 0.25, Math.PI * 0.5, Math.PI)
            this.context.stroke(this.path2Db)
          })
Z
zengyawen 已提交
403
      }
Z
zengyawen 已提交
404 405
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
406
    }
Z
zengyawen 已提交
407 408
  }
  ```
Z
zengyawen 已提交
409

Z
zengyawen 已提交
410
  ![en-us_image_0000001257138391](figures/en-us_image_0000001257138391.png)
Z
zengyawen 已提交
411 412


Z
zengyawen 已提交
413
## rect
Z
zengyawen 已提交
414

Z
zengyawen 已提交
415
rect(x: number, y: number, width: number, height: number): void
Z
zengyawen 已提交
416 417 418

Creates a rectangle.

Z
zengyawen 已提交
419
- Parameters
E
esterzhou 已提交
420
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
421
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
422 423 424 425
  | x | number | Yes | 0 | X-coordinate of the upper left corner of the rectangle. | 
  | y | number | Yes | 0 | Y-coordinate of the upper left corner of the rectangle. | 
  | width | number | Yes | 0 | Width of the rectangle. | 
  | height | number | Yes | 0 | Height of the rectangle. | 
Z
zengyawen 已提交
426 427

- Example
Z
zengyawen 已提交
428
    
Z
zengyawen 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
  ```
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private path2Db: Path2D = new Path2D()
  
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.path2Db.rect(20, 20, 100, 100);this.context.stroke(this.path2Db)
          })
Z
zengyawen 已提交
446
      }
Z
zengyawen 已提交
447 448
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
449
    }
Z
zengyawen 已提交
450 451
  }
  ```
Z
zengyawen 已提交
452

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