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