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