1# Path2D 2 3**Path2D** allows you to describe a path through an existing path. This path can be drawn through the **stroke** or **fill** API of **Canvas**. 4 5> **NOTE** 6> 7> The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version. 8 9## APIs 10 11Path2D(unit?: LengthMetricsUnit) 12 13Constructs an empty **Path2D** object. 14 15**Widget capability**: This API can be used in ArkTS widgets since API version 9. 16 17**Atomic service API**: This API can be used in atomic services since API version 11. 18 19**System capability**: SystemCapability.ArkUI.ArkUI.Full 20 21**Parameters** 22 23| Name | Type | Mandatory| Description | 24| ----- | -------- | ---- | ---------- | 25| unit<sup>12+</sup> | [LengthMetricsUnit](../js-apis-arkui-graphics.md#lengthmetricsunit12) | No| Unit mode of the **Path2D** object. The value cannot be dynamically changed once set. The configuration method is the same as that of [CanvasRenderingContext2D](ts-canvasrenderingcontext2d.md#lengthmetricsunit12).<br>Default value: **DEFAULT**| 26 27Path2D(description: string, unit?: LengthMetricsUnit) 28 29Constructs a **Path2D** object using a path that complies with the SVG path specifications. 30 31**Widget capability**: This API can be used in ArkTS widgets since API version 9. 32 33**Atomic service API**: This API can be used in atomic services since API version 11. 34 35**System capability**: SystemCapability.ArkUI.ArkUI.Full 36 37**Parameters** 38 39| Name | Type | Mandatory| Description | 40| ----- | -------- | ---- | ---------- | 41| description | string | Yes| Path that complies with the [SVG path syntax](ts-drawing-components-path.md#svg-path-syntax).| 42| unit<sup>12+</sup> | [LengthMetricsUnit](../js-apis-arkui-graphics.md#lengthmetricsunit12) | No| Unit mode of the **Path2D** object. The value cannot be dynamically changed once set. The configuration method is the same as that of [CanvasRenderingContext2D](ts-canvasrenderingcontext2d.md#lengthmetricsunit12).<br>Default value: **DEFAULT**| 43 44## addPath 45 46addPath(path: Path2D, transform?: Matrix2D): void 47 48Adds a path to this path. 49 50**Widget capability**: This API can be used in ArkTS widgets since API version 9. 51 52**Atomic service API**: This API can be used in atomic services since API version 11. 53 54**System capability**: SystemCapability.ArkUI.ArkUI.Full 55 56**Parameters** 57 58| Name | Type | Mandatory| Description | 59| ----- | -------- | ---- | ---------- | 60| path | [Path2D](ts-components-canvas-path2d.md) | Yes| Path to be added to this path. Unit: px.| 61| transform | [Matrix2D](ts-components-canvas-matrix2d.md) | No| Transformation matrix of the new path.<br>Default value: **null**.| 62 63 64**Example** 65 66 ```ts 67 // xxx.ets 68 @Entry 69 @Component 70 struct AddPath { 71 private settings: RenderingContextSettings = new RenderingContextSettings(true) 72 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 73 private path2Da: Path2D = new Path2D("M250 150 L150 350 L350 350 Z") 74 private path2Db: Path2D = new Path2D() 75 76 build() { 77 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 78 Canvas(this.context) 79 .width('100%') 80 .height('100%') 81 .backgroundColor('#ffff00') 82 .onReady(() => { 83 this.path2Db.addPath(this.path2Da) 84 this.context.stroke(this.path2Db) 85 }) 86 } 87 .width('100%') 88 .height('100%') 89 } 90 } 91 ``` 92 93  94 95 96## closePath 97 98closePath(): void 99 100Moves 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. 101 102**Widget capability**: This API can be used in ArkTS widgets since API version 9. 103 104**Atomic service API**: This API can be used in atomic services since API version 11. 105 106**System capability**: SystemCapability.ArkUI.ArkUI.Full 107 108**Example** 109 110 ```ts 111 // xxx.ets 112 @Entry 113 @Component 114 struct ClosePath { 115 private settings: RenderingContextSettings = new RenderingContextSettings(true) 116 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 117 private path2Db: Path2D = new Path2D() 118 119 build() { 120 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 121 Canvas(this.context) 122 .width('100%') 123 .height('100%') 124 .backgroundColor('#ffff00') 125 .onReady(() => { 126 this.path2Db.moveTo(200, 100) 127 this.path2Db.lineTo(300, 100) 128 this.path2Db.lineTo(200, 200) 129 this.path2Db.closePath() 130 this.context.stroke(this.path2Db) 131 }) 132 } 133 .width('100%') 134 .height('100%') 135 } 136 } 137 ``` 138 139  140 141 142## moveTo 143 144moveTo(x: number, y: number): void 145 146Moves the current coordinate point of the path to the target point, without drawing a line during the movement. 147 148**Widget capability**: This API can be used in ArkTS widgets since API version 9. 149 150**Atomic service API**: This API can be used in atomic services since API version 11. 151 152**System capability**: SystemCapability.ArkUI.ArkUI.Full 153 154**Parameters** 155 156| Name | Type | Mandatory| Description | 157| ---- | ------ | ---- | -------- | 158| x | number | Yes| X coordinate of the target point.<br>Default unit: vp.| 159| y | number | Yes| Y coordinate of the target point.<br>Default unit: vp.| 160 161**Example** 162 163 ```ts 164 // xxx.ets 165 @Entry 166 @Component 167 struct MoveTo { 168 private settings: RenderingContextSettings = new RenderingContextSettings(true) 169 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 170 private path2Db: Path2D = new Path2D() 171 172 build() { 173 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 174 Canvas(this.context) 175 .width('100%') 176 .height('100%') 177 .backgroundColor('#ffff00') 178 .onReady(() => { 179 this.path2Db.moveTo(50, 100) 180 this.path2Db.lineTo(250, 100) 181 this.path2Db.lineTo(150, 200) 182 this.path2Db.closePath() 183 this.context.stroke(this.path2Db) 184 }) 185 } 186 .width('100%') 187 .height('100%') 188 } 189 } 190 ``` 191 192  193 194 195## lineTo 196 197lineTo(x: number, y: number): void 198 199Draws a straight line from the current point to the target point. 200 201**Widget capability**: This API can be used in ArkTS widgets since API version 9. 202 203**Atomic service API**: This API can be used in atomic services since API version 11. 204 205**System capability**: SystemCapability.ArkUI.ArkUI.Full 206 207**Parameters** 208 209| Name | Type | Mandatory| Description | 210| ----- | -------- | ---- | ---------- | 211| x | number | Yes| X coordinate of the target point.<br>Default unit: vp.| 212| y | number | Yes| Y coordinate of the target point.<br>Default unit: vp.| 213 214**Example** 215 216 ```ts 217 // xxx.ets 218 @Entry 219 @Component 220 struct LineTo { 221 private settings: RenderingContextSettings = new RenderingContextSettings(true) 222 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 223 private path2Db: Path2D = new Path2D() 224 225 build() { 226 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 227 Canvas(this.context) 228 .width('100%') 229 .height('100%') 230 .backgroundColor('#ffff00') 231 .onReady(() => { 232 this.path2Db.moveTo(100, 100) 233 this.path2Db.lineTo(100, 200) 234 this.path2Db.lineTo(200, 200) 235 this.path2Db.lineTo(200, 100) 236 this.path2Db.closePath() 237 this.context.stroke(this.path2Db) 238 }) 239 } 240 .width('100%') 241 .height('100%') 242 } 243 } 244 ``` 245 246  247 248 249## bezierCurveTo 250 251bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void 252 253Draws a cubic Bezier curve on the canvas. 254 255**Widget capability**: This API can be used in ArkTS widgets since API version 9. 256 257**Atomic service API**: This API can be used in atomic services since API version 11. 258 259**System capability**: SystemCapability.ArkUI.ArkUI.Full 260 261**Parameters** 262 263| Name | Type | Mandatory| Description | 264| ----- | -------- | ---- | ---------- | 265| cp1x | number | Yes| X coordinate of the first parameter of the bezier curve.<br>Default unit: vp.| 266| cp1y | number | Yes| Y coordinate of the first parameter of the bezier curve.<br>Default unit: vp.| 267| cp2x | number | Yes| X coordinate of the second parameter of the bezier curve.<br>Default unit: vp.| 268| cp2y | number | Yes| Y coordinate of the second parameter of the bezier curve.<br>Default unit: vp.| 269| x | number | Yes| X coordinate of the end point on the bezier curve.<br>Default unit: vp. | 270| y | number | Yes| Y coordinate of the end point on the bezier curve.<br>Default unit: vp. | 271 272**Example** 273 274 ```ts 275 // xxx.ets 276 @Entry 277 @Component 278 struct BezierCurveTo { 279 private settings: RenderingContextSettings = new RenderingContextSettings(true) 280 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 281 private path2Db: Path2D = new Path2D() 282 283 build() { 284 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 285 Canvas(this.context) 286 .width('100%') 287 .height('100%') 288 .backgroundColor('#ffff00') 289 .onReady(() => { 290 this.path2Db.moveTo(10, 10) 291 this.path2Db.bezierCurveTo(20, 100, 200, 100, 200, 20) 292 this.context.stroke(this.path2Db) 293 }) 294 } 295 .width('100%') 296 .height('100%') 297 } 298 } 299 ``` 300 301  302 303 304## quadraticCurveTo 305 306quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void 307 308Draws a quadratic curve on the canvas. 309 310**Widget capability**: This API can be used in ArkTS widgets since API version 9. 311 312**Atomic service API**: This API can be used in atomic services since API version 11. 313 314**System capability**: SystemCapability.ArkUI.ArkUI.Full 315 316**Parameters** 317 318| Name | Type | Mandatory| Description | 319| ----- | -------- | ---- | ---------- | 320| cpx | number | Yes| X coordinate of the bezier curve parameter.<br>Default unit: vp.| 321| cpy | number | Yes| Y coordinate of the bezier curve parameter.<br>Default unit: vp.| 322| x | number | Yes| X coordinate of the end point on the bezier curve.<br>Default unit: vp.| 323| y | number | Yes| Y coordinate of the end point on the bezier curve.<br>Default unit: vp.| 324 325**Example** 326 327 ```ts 328 // xxx.ets 329 @Entry 330 @Component 331 struct QuadraticCurveTo { 332 private settings: RenderingContextSettings = new RenderingContextSettings(true) 333 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 334 private path2Db: Path2D = new Path2D() 335 336 build() { 337 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 338 Canvas(this.context) 339 .width('100%') 340 .height('100%') 341 .backgroundColor('#ffff00') 342 .onReady(() => { 343 this.path2Db.moveTo(10, 10) 344 this.path2Db.quadraticCurveTo(100, 100, 200, 20) 345 this.context.stroke(this.path2Db) 346 }) 347 } 348 .width('100%') 349 .height('100%') 350 } 351 } 352 ``` 353 354  355 356 357## arc 358 359arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void 360 361Draws an arc on the canvas. 362 363**Widget capability**: This API can be used in ArkTS widgets since API version 9. 364 365**Atomic service API**: This API can be used in atomic services since API version 11. 366 367**System capability**: SystemCapability.ArkUI.ArkUI.Full 368 369**Parameters** 370 371| Name | Type | Mandatory| Description | 372| ----- | -------- | ---- | ---------- | 373| x | number | Yes| X coordinate of the center point of the arc.<br>Default unit: vp.| 374| y | number | Yes| Y coordinate of the center point of the arc.<br>Default unit: vp.| 375| radius | number | Yes| Radius of the arc.<br>Default unit: vp. | 376| startAngle | number | Yes| Start radian of the arc.<br>Unit: radian. | 377| endAngle | number | Yes| End radian of the arc.<br>Unit: radian. | 378| counterclockwise | boolean | No| Whether to draw the arc counterclockwise.<br>**true**: Draw the ellipse counterclockwise.<br>**false**: Draw the ellipse clockwise.<br>Default value: **false**.| 379 380**Example** 381 382 ```ts 383 // xxx.ets 384 @Entry 385 @Component 386 struct Arc { 387 private settings: RenderingContextSettings = new RenderingContextSettings(true) 388 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 389 private path2Db: Path2D = new Path2D() 390 391 build() { 392 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 393 Canvas(this.context) 394 .width('100%') 395 .height('100%') 396 .backgroundColor('#ffff00') 397 .onReady(() => { 398 this.path2Db.arc(100, 75, 50, 0, 6.28) 399 this.context.stroke(this.path2Db) 400 }) 401 } 402 .width('100%') 403 .height('100%') 404 } 405 } 406 ``` 407 408  409 410 411## arcTo 412 413arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void 414 415Draws an arc based on the radius and points on the arc. 416 417**Widget capability**: This API can be used in ArkTS widgets since API version 9. 418 419**Atomic service API**: This API can be used in atomic services since API version 11. 420 421**System capability**: SystemCapability.ArkUI.ArkUI.Full 422 423**Parameters** 424 425| Name | Type | Mandatory| Description | 426| ----- | -------- | ---- | ---------- | 427| x1 | number | Yes| X coordinate of the first point on the arc.<br>Default unit: vp.| 428| y1 | number | Yes| Y coordinate of the first point on the arc.<br>Default unit: vp.| 429| x2 | number | Yes| X coordinate of the second point on the arc.<br>Default unit: vp.| 430| y2 | number | Yes| Y coordinate of the second point on the arc.<br>Default unit: vp.| 431| radius | number | Yes| Radius of the arc.<br>Default unit: vp.| 432 433**Example** 434 435 ```ts 436 // xxx.ets 437 @Entry 438 @Component 439 struct ArcTo { 440 private settings: RenderingContextSettings = new RenderingContextSettings(true) 441 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 442 private path2Db: Path2D = new Path2D() 443 444 build() { 445 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 446 Canvas(this.context) 447 .width('100%') 448 .height('100%') 449 .backgroundColor('#ffff00') 450 .onReady(() => { 451 this.path2Db.arcTo(150, 20, 150, 70, 50) 452 this.context.stroke(this.path2Db) 453 }) 454 } 455 .width('100%') 456 .height('100%') 457 } 458 } 459 ``` 460 461  462 463 464## ellipse 465 466ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void 467 468Draws an ellipse in the specified rectangular region on the canvas. 469 470**Widget capability**: This API can be used in ArkTS widgets since API version 9. 471 472**Atomic service API**: This API can be used in atomic services since API version 11. 473 474**System capability**: SystemCapability.ArkUI.ArkUI.Full 475 476**Parameters** 477 478| Name | Type | Mandatory| Description | 479| ----- | -------- | ---- | ---------- | 480| x | number | Yes | X coordinate of the ellipse center.<br>Default unit: vp.| 481| y | number | Yes | Y coordinate of the ellipse center.<br>Default unit: vp.| 482| radiusX | number | Yes | Radius of the ellipse on the x-axis.<br>Default unit: vp.| 483| radiusY | number | Yes | Radius of the ellipse on the y-axis.<br>Default unit: vp.| 484| rotation | number | Yes | Rotation angle of the ellipse.<br>Unit: radian. | 485| startAngle | number | Yes | Angle of the start point for drawing the ellipse.<br>Unit: radian. | 486| endAngle | number | Yes | Angle of the end point for drawing the ellipse.<br>Unit: radian. | 487| counterclockwise | boolean | No | Whether to draw the ellipse counterclockwise.<br>**true**: Draw the ellipse counterclockwise.<br>**false**: Draw the ellipse clockwise.<br>Default value: **false**.| 488 489**Example** 490 491 ```ts 492 // xxx.ets 493 @Entry 494 @Component 495 struct CanvasExample { 496 private settings: RenderingContextSettings = new RenderingContextSettings(true) 497 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 498 private path2Db: Path2D = new Path2D() 499 500 build() { 501 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 502 Canvas(this.context) 503 .width('100%') 504 .height('100%') 505 .backgroundColor('#ffff00') 506 .onReady(() => { 507 this.path2Db.ellipse(200, 200, 50, 100, 0, Math.PI * 1, Math.PI * 2) 508 this.context.stroke(this.path2Db) 509 }) 510 } 511 .width('100%') 512 .height('100%') 513 } 514 } 515 ``` 516 517  518 519 520## rect 521 522rect(x: number, y: number, w: number, h: number): void 523 524Creates a rectangle on the canvas. 525 526**Widget capability**: This API can be used in ArkTS widgets since API version 9. 527 528**Atomic service API**: This API can be used in atomic services since API version 11. 529 530**System capability**: SystemCapability.ArkUI.ArkUI.Full 531 532**Parameters** 533 534| Name | Type | Mandatory| Description | 535| ----- | -------- | ---- | ---------- | 536| x | number | Yes| X coordinate of the upper left corner of the rectangle.<br>Default unit: vp.| 537| y | number | Yes| Y coordinate of the upper left corner of the rectangle.<br>Default unit: vp.| 538| w | number | Yes| Width of the rectangle.<br>Default unit: vp.| 539| h | number | Yes| Height of the rectangle.<br>Default unit: vp.| 540 541**Example** 542 543 ```ts 544 // xxx.ets 545 @Entry 546 @Component 547 struct CanvasExample { 548 private settings: RenderingContextSettings = new RenderingContextSettings(true) 549 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 550 private path2Db: Path2D = new Path2D() 551 552 build() { 553 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 554 Canvas(this.context) 555 .width('100%') 556 .height('100%') 557 .backgroundColor('#ffff00') 558 .onReady(() => { 559 this.path2Db.rect(20, 20, 100, 100); 560 this.context.stroke(this.path2Db) 561 }) 562 } 563 .width('100%') 564 .height('100%') 565 } 566 } 567 ``` 568 569  570