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

E
ester.zhou 已提交
3 4 5
>  **NOTE**
>
>  The APIs of this module are 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
**Path2D** allows you to describe a path through an existing path. This path can be drawn through the **stroke** API of **Canvas**.


## addPath

E
ester.zhou 已提交
13
addPath(path: path2D, transform?:Matrix2D): void
Z
zengyawen 已提交
14 15 16

Adds a path to this path.

E
ester.zhou 已提交
17
**Parameters**
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19 20 21 22 23 24 25 26 27 28
| Name| Type| Mandatory| Default Value| Description|
| -------- | -------- | -------- | -------- | -------- |
| path | path2D | Yes| - | Path to be added to this path.|
| transform | Matrix2D | No| null | Transformation matrix of the new path.|


**Example**

  ```ts
  // xxx.ets
Z
zengyawen 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  @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 已提交
48
      }
Z
zengyawen 已提交
49 50
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
51
    }
Z
zengyawen 已提交
52 53
  }
  ```
Z
zengyawen 已提交
54

Z
zengyawen 已提交
55
  ![en-us_image_0000001211898520](figures/en-us_image_0000001211898520.png)
Z
zengyawen 已提交
56 57


Z
zengyawen 已提交
58
## closePath
Z
zengyawen 已提交
59

Z
zengyawen 已提交
60
closePath(): void
Z
zengyawen 已提交
61

Z
zengyawen 已提交
62
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 已提交
63

E
ester.zhou 已提交
64 65 66 67
**Example**

  ```ts
  // xxx.ets
Z
zengyawen 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  @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 已提交
88
      }
Z
zengyawen 已提交
89 90
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
91
    }
Z
zengyawen 已提交
92 93
  }
  ```
Z
zengyawen 已提交
94

Z
zengyawen 已提交
95
  ![en-us_image_0000001212218482](figures/en-us_image_0000001212218482.png)
Z
zengyawen 已提交
96 97


Z
zengyawen 已提交
98
## moveTo
Z
zengyawen 已提交
99

Z
zengyawen 已提交
100
moveTo(x: number, y: number): void
Z
zengyawen 已提交
101 102 103

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

E
ester.zhou 已提交
104
**Parameters**
Z
zengyawen 已提交
105

E
ester.zhou 已提交
106 107 108 109 110 111 112 113 114
| Name| Type| Mandatory| Default Value| Description|
| -------- | -------- | -------- | -------- | -------- |
| x | number | Yes| 0 | X-coordinate of the target point.|
| y | number | Yes| 0 | Y-coordinate of the target point.|

**Example**

  ```ts
  // xxx.ets
Z
zengyawen 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  @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 已提交
135
      }
Z
zengyawen 已提交
136 137
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
138
    }
Z
zengyawen 已提交
139 140
  }
  ```
Z
zengyawen 已提交
141

Z
zengyawen 已提交
142
  ![en-us_image_0000001257138389](figures/en-us_image_0000001257138389.png)
Z
zengyawen 已提交
143 144


Z
zengyawen 已提交
145
## lineTo
Z
zengyawen 已提交
146

Z
zengyawen 已提交
147
lineTo(x: number, y: number): void
Z
zengyawen 已提交
148 149 150

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

E
ester.zhou 已提交
151
**Parameters**
Z
zengyawen 已提交
152

E
ester.zhou 已提交
153 154 155 156 157 158 159 160 161
| Name| Type| Mandatory| Default Value| Description|
| -------- | -------- | -------- | -------- | -------- |
| x | number | Yes| 0 | X-coordinate of the target point.|
| y | number | Yes| 0 | Y-coordinate of the target point.|

**Example**

  ```ts
  // xxx.ets
Z
zengyawen 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  @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 已提交
183
      }
Z
zengyawen 已提交
184 185
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
186
    }
Z
zengyawen 已提交
187 188
  }
  ```
Z
zengyawen 已提交
189

Z
zengyawen 已提交
190
  ![en-us_image_0000001256858435](figures/en-us_image_0000001256858435.png)
Z
zengyawen 已提交
191 192


Z
zengyawen 已提交
193
## bezierCurveTo
Z
zengyawen 已提交
194

Z
zengyawen 已提交
195
bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void
Z
zengyawen 已提交
196 197 198

Draws a cubic bezier curve on the canvas.

E
ester.zhou 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
**Parameters**

| Name| Type| Mandatory| Default Value| Description|
| -------- | -------- | -------- | -------- | -------- |
| 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.|

**Example**

  ```ts
  // xxx.ets
Z
zengyawen 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  @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 已提交
231
      }
Z
zengyawen 已提交
232 233
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
234
    }
Z
zengyawen 已提交
235 236
  }
  ```
Z
zengyawen 已提交
237

Z
zengyawen 已提交
238
  ![en-us_image_0000001257058445](figures/en-us_image_0000001257058445.png)
Z
zengyawen 已提交
239 240


Z
zengyawen 已提交
241
## quadraticCurveTo
Z
zengyawen 已提交
242

Z
zengyawen 已提交
243
quadraticCurveTo(cpx: number, cpy: number, x: number ,y: number): void
Z
zengyawen 已提交
244 245 246

Draws a quadratic curve on the canvas.

E
ester.zhou 已提交
247
**Parameters**
Z
zengyawen 已提交
248

E
ester.zhou 已提交
249 250 251 252 253 254 255 256 257 258 259
| Name| Type| Mandatory| Default Value| Description|
| -------- | -------- | -------- | -------- | -------- |
| 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.|

**Example**

  ```ts
  // xxx.ets
Z
zengyawen 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  @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 已提交
278
      }
Z
zengyawen 已提交
279 280
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
281
    }
Z
zengyawen 已提交
282 283
  }
  ```
Z
zengyawen 已提交
284

Z
zengyawen 已提交
285
  ![en-us_image_0000001212058512](figures/en-us_image_0000001212058512.png)
Z
zengyawen 已提交
286 287


Z
zengyawen 已提交
288
## arc
Z
zengyawen 已提交
289

E
ester.zhou 已提交
290
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void
Z
zengyawen 已提交
291 292 293

Draws an arc on the canvas.

E
ester.zhou 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
**Parameters**

| Name| Type| Mandatory| Default Value| Description|
| -------- | -------- | -------- | -------- | -------- |
| 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.|
| counterclockwise | boolean | No| false | Whether to draw the arc counterclockwise.|

**Example**

  ```ts
  // xxx.ets
Z
zengyawen 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  @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 已提交
325
      }
Z
zengyawen 已提交
326 327
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
328
    }
Z
zengyawen 已提交
329 330
  }
  ```
Z
zengyawen 已提交
331

Z
zengyawen 已提交
332
  ![en-us_image_0000001212378446](figures/en-us_image_0000001212378446.png)
Z
zengyawen 已提交
333 334


Z
zengyawen 已提交
335
## arcTo
Z
zengyawen 已提交
336

Z
zengyawen 已提交
337
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void
Z
zengyawen 已提交
338 339 340

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

E
ester.zhou 已提交
341
**Parameters**
Z
zengyawen 已提交
342

E
ester.zhou 已提交
343 344 345 346 347 348 349 350 351 352 353 354
| Name| Type| Mandatory| Default Value| Description|
| -------- | -------- | -------- | -------- | -------- |
| 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.|

**Example**

  ```ts
  // xxx.ets
Z
zengyawen 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
  @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 已提交
372
      }
Z
zengyawen 已提交
373 374
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
375
    }
Z
zengyawen 已提交
376 377
  }
  ```
Z
zengyawen 已提交
378

Z
zengyawen 已提交
379
  ![en-us_image_0000001212058510](figures/en-us_image_0000001212058510.png)
Z
zengyawen 已提交
380 381


Z
zengyawen 已提交
382
## ellipse
Z
zengyawen 已提交
383

E
ester.zhou 已提交
384
ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, counterclockwise?: number): void
Z
zengyawen 已提交
385

E
ester.zhou 已提交
386
Draws an ellipse in the specified rectangular region on the canvas.
Z
zengyawen 已提交
387

E
ester.zhou 已提交
388
**Parameters**
Z
zengyawen 已提交
389

E
ester.zhou 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
| Name| Type| Mandatory| Default Value| Description|
| -------- | -------- | -------- | -------- | -------- |
| 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. The unit is radian.|
| startAngle | number | Yes| 0 | Angle of the start point for drawing the ellipse. The unit is radian.|
| endAngle | number | Yes| 0 | Angle of the end point for drawing the ellipse. The unit is radian.|
| counterclockwise | number | No| 0 | Whether to draw the ellipse counterclockwise. The value **0** means to draw the ellipse clockwise, and **1** means to draw the ellipse counterclockwise. This parameter is optional. The default value is **0**. |

**Example**

  ```ts
  // xxx.ets
Z
zengyawen 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
  @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 已提交
422
      }
Z
zengyawen 已提交
423 424
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
425
    }
Z
zengyawen 已提交
426 427
  }
  ```
Z
zengyawen 已提交
428

Z
zengyawen 已提交
429
  ![en-us_image_0000001257138391](figures/en-us_image_0000001257138391.png)
Z
zengyawen 已提交
430 431


Z
zengyawen 已提交
432
## rect
Z
zengyawen 已提交
433

E
ester.zhou 已提交
434
rect(x: number, y: number, w: number, h: number): void
Z
zengyawen 已提交
435

E
ester.zhou 已提交
436
Creates a rectangle on the canvas.
Z
zengyawen 已提交
437

E
ester.zhou 已提交
438
**Parameters**
Z
zengyawen 已提交
439

E
ester.zhou 已提交
440 441 442 443 444 445 446 447 448 449 450
| Name| Type| Mandatory| Default Value| Description|
| -------- | -------- | -------- | -------- | -------- |
| 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.|
| w | number | Yes| 0 | Width of the rectangle.|
| h | number | Yes| 0 | Height of the rectangle.|

**Example**

  ```ts
  // xxx.ets
Z
zengyawen 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
  @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 已提交
467
      }
Z
zengyawen 已提交
468 469
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
470
    }
Z
zengyawen 已提交
471 472
  }
  ```
Z
zengyawen 已提交
473

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