• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class (Path)
2
3<!--Kit: ArkGraphics 2D-->
4<!--Subsystem: Graphics-->
5<!--Owner: @hangmengxin-->
6<!--Designer: @wangyanglan-->
7<!--Tester: @nobuggers-->
8<!--Adviser: @ge-yafang-->
9
10> **说明:**
11>
12> - 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
13>
14> - 本模块使用屏幕物理像素单位px。
15>
16> - 本模块为单线程模型策略,需要调用方自行管理线程安全和上下文状态的切换。
17
18由直线、圆弧、二阶贝塞尔、三阶贝塞尔组成的复合几何路径。
19
20## 导入模块
21
22```ts
23import { drawing } from '@kit.ArkGraphics2D';
24```
25
26## constructor<sup>12+</sup>
27
28constructor()
29
30构造一个路径。
31
32**系统能力:** SystemCapability.Graphics.Drawing
33
34**示例:**
35
36```ts
37import { drawing } from '@kit.ArkGraphics2D';
38
39let path: drawing.Path = new drawing.Path();
40```
41
42## constructor<sup>12+</sup>
43
44constructor(path: Path)
45
46构造一个已有路径的副本。
47
48**系统能力:** SystemCapability.Graphics.Drawing
49
50**参数:**
51
52| 参数名   | 类型                                         | 必填 | 说明                            |
53| -------- | -------------------------------------------- | ---- | ------------------------------- |
54| path | [Path](arkts-apis-graphics-drawing-Path.md) | 是   | 待复制的路径对象。                 |
55
56**示例:**
57
58```ts
59import { drawing } from '@kit.ArkGraphics2D';
60
61let path: drawing.Path = new drawing.Path();
62path.moveTo(0, 0);
63path.lineTo(0, 700);
64path.lineTo(700, 0);
65path.close();
66let path1: drawing.Path =  new drawing.Path(path);
67```
68
69## set<sup>20+</sup>
70
71set(src: Path): void
72
73使用另一个路径对当前路径进行更新。
74
75**系统能力:** SystemCapability.Graphics.Drawing
76
77**参数:**
78
79| 参数名   | 类型                                         | 必填 | 说明                            |
80| -------- | -------------------------------------------- | ---- | ------------------------------- |
81| src | [Path](arkts-apis-graphics-drawing-Path.md) | 是   | 用于更新的路径。                 |
82
83**示例:**
84
85```ts
86import { drawing } from '@kit.ArkGraphics2D';
87let path: drawing.Path = new drawing.Path();
88path.moveTo(0, 0);
89path.lineTo(0, 700);
90path.lineTo(700, 0);
91path.close();
92let path1: drawing.Path = new drawing.Path();
93path1.set(path);
94```
95
96## moveTo
97
98moveTo(x: number, y: number) : void
99
100设置自定义路径的起始点位置。
101
102**系统能力:** SystemCapability.Graphics.Drawing
103
104**参数:**
105
106| 参数名 | 类型   | 必填 | 说明                    |
107| ------ | ------ | ---- | ----------------------- |
108| x      | number | 是   | 起始点的x轴坐标,该参数为浮点数。 |
109| y      | number | 是   | 起始点的y轴坐标,该参数为浮点数。 |
110
111**错误码:**
112
113以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
114
115| 错误码ID | 错误信息 |
116| ------- | --------------------------------------------|
117| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
118
119**示例:**
120
121```ts
122import { drawing } from '@kit.ArkGraphics2D';
123
124let path = new drawing.Path();
125path.moveTo(10,10);
126```
127
128## lineTo
129
130lineTo(x: number, y: number) : void
131
132添加一条从路径的最后点位置(若路径没有内容则默认为 (0, 0))到目标点位置的线段。
133
134**系统能力:** SystemCapability.Graphics.Drawing
135
136**参数:**
137
138| 参数名 | 类型   | 必填 | 说明                    |
139| ------ | ------ | ---- | ----------------------- |
140| x      | number | 是   | 目标点的x轴坐标,该参数为浮点数。 |
141| y      | number | 是   | 目标点的y轴坐标,该参数为浮点数。 |
142
143**错误码:**
144
145以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
146
147| 错误码ID | 错误信息 |
148| ------- | --------------------------------------------|
149| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
150
151**示例:**
152
153```ts
154import { drawing } from '@kit.ArkGraphics2D';
155
156let path = new drawing.Path();
157path.moveTo(10,10);
158path.lineTo(10, 15);
159```
160
161## arcTo
162
163arcTo(x1: number, y1: number, x2: number, y2: number, startDeg: number, sweepDeg: number): void
164
165给路径添加一段弧线,绘制弧线的方式为角度弧,该方式首先会指定一个矩形边框,取其内切椭圆,然后会指定一个起始角度和扫描度数,从起始角度扫描截取的椭圆周长一部分即为绘制的弧线。另外会默认添加一条从路径的最后点位置到弧线起始点位置的线段。
166
167**系统能力:** SystemCapability.Graphics.Drawing
168
169**参数:**
170
171| 参数名   | 类型   | 必填 | 说明                       |
172| -------- | ------ | ---- | -------------------------- |
173| x1       | number | 是   | 矩形左上角的x坐标,该参数为浮点数。 |
174| y1       | number | 是   | 矩形左上角的y坐标,该参数为浮点数。 |
175| x2       | number | 是   | 矩形右下角的x坐标,该参数为浮点数。 |
176| y2       | number | 是   | 矩形右下角的y坐标,该参数为浮点数。 |
177| startDeg | number | 是   | 起始的角度。角度的起始方向(0°)为x轴正方向。 |
178| sweepDeg | number | 是   | 扫描的度数,为正数时顺时针扫描,为负数时逆时针扫描。实际扫描的度数为该入参对360取模的结果。 |
179
180**错误码:**
181
182以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
183
184| 错误码ID | 错误信息 |
185| ------- | --------------------------------------------|
186| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
187
188**示例:**
189
190```ts
191import { drawing } from '@kit.ArkGraphics2D';
192
193let path = new drawing.Path();
194path.moveTo(10,10);
195path.arcTo(10, 15, 10, 10, 10, 10);
196```
197
198## quadTo
199
200quadTo(ctrlX: number, ctrlY: number, endX: number, endY: number): void
201
202添加从路径最后点位置(若路径没有内容则为 (0, 0))到目标点位置的二阶贝塞尔曲线。
203
204**系统能力:** SystemCapability.Graphics.Drawing
205
206**参数:**
207
208| 参数名 | 类型   | 必填 | 说明                  |
209| ------ | ------ | ---- | --------------------- |
210| ctrlX  | number | 是   | 控制点的x坐标,该参数为浮点数。 |
211| ctrlY  | number | 是   | 控制点的y坐标,该参数为浮点数。 |
212| endX   | number | 是   | 目标点的x坐标,该参数为浮点数。 |
213| endY   | number | 是   | 目标点的y坐标,该参数为浮点数。 |
214
215**错误码:**
216
217以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
218
219| 错误码ID | 错误信息 |
220| ------- | --------------------------------------------|
221| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
222
223**示例:**
224
225```ts
226import { drawing } from '@kit.ArkGraphics2D';
227
228let path = new drawing.Path();
229path.moveTo(10,10);
230path.quadTo(10, 15, 10, 10);
231```
232
233## conicTo<sup>12+</sup>
234
235conicTo(ctrlX: number, ctrlY: number, endX: number, endY: number, weight: number): void
236
237在当前路径上添加一条路径最后点位置(若路径没有内容则默认为 (0, 0))到目标点位置的圆锥曲线,其控制点为 (ctrlX, ctrlY),结束点为 (endX, endY)。
238
239**系统能力:** SystemCapability.Graphics.Drawing
240
241**参数:**
242
243| 参数名 | 类型   | 必填 | 说明                  |
244| ------ | ------ | ---- | --------------------- |
245| ctrlX  | number | 是   | 控制点的x坐标,该参数为浮点数。 |
246| ctrlY  | number | 是   | 控制点的y坐标,该参数为浮点数。 |
247| endX   | number | 是   | 目标点的x坐标,该参数为浮点数。 |
248| endY   | number | 是   | 目标点的y坐标,该参数为浮点数。 |
249| weight | number | 是   | 表示曲线权重,决定了曲线的形状。值越大,曲线越接近控制点。小于等于0时,效果与[lineTo](#lineto)相同;值为1时,效果与[quadTo](#quadto)相同。该参数为浮点数。 |
250
251**错误码:**
252
253以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
254
255| 错误码ID | 错误信息 |
256| ------- | --------------------------------------------|
257| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
258
259**示例:**
260
261```ts
262import { drawing } from '@kit.ArkGraphics2D';
263
264const path = new drawing.Path();
265path.conicTo(200, 400, 100, 200, 0);
266```
267
268## cubicTo
269
270cubicTo(ctrlX1: number, ctrlY1: number, ctrlX2: number, ctrlY2: number, endX: number, endY: number): void
271
272添加一条从路径最后点位置(若路径没有内容则默认为 (0, 0))到目标点位置的三阶贝塞尔圆滑曲线。
273
274**系统能力:** SystemCapability.Graphics.Drawing
275
276**参数:**
277
278| 参数名 | 类型   | 必填 | 说明                        |
279| ------ | ------ | ---- | --------------------------- |
280| ctrlX1 | number | 是   | 第一个控制点的x坐标,该参数为浮点数。 |
281| ctrlY1 | number | 是   | 第一个控制点的y坐标,该参数为浮点数。 |
282| ctrlX2 | number | 是   | 第二个控制点的x坐标,该参数为浮点数。 |
283| ctrlY2 | number | 是   | 第二个控制点的y坐标,该参数为浮点数。 |
284| endX   | number | 是   | 目标点的x坐标,该参数为浮点数。 |
285| endY   | number | 是   | 目标点的y坐标,该参数为浮点数。 |
286
287**错误码:**
288
289以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
290
291| 错误码ID | 错误信息 |
292| ------- | --------------------------------------------|
293| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
294
295**示例:**
296
297```ts
298import { drawing } from '@kit.ArkGraphics2D';
299
300let path = new drawing.Path();
301path.moveTo(10,10);
302path.cubicTo(100, 100, 80, 150, 300, 150);
303```
304
305## rMoveTo<sup>12+</sup>
306
307rMoveTo(dx: number, dy: number): void
308
309设置一个相对于当前路径终点(若路径没有内容则默认为 (0, 0))的路径起始点位置。
310
311**系统能力:** SystemCapability.Graphics.Drawing
312
313**参数:**
314
315| 参数名 | 类型   | 必填 | 说明                    |
316| ------ | ------ | ---- | ----------------------- |
317| dx     | number | 是   | 路径新起始点相对于当前路径终点的x轴偏移量,正数往x轴正方向偏移,负数往x轴负方向偏移,该参数为浮点数。 |
318| dy     | number | 是   | 路径新起始点相对于当前路径终点的y轴偏移量,正数往y轴正方向偏移,负数往y轴负方向偏移,该参数为浮点数。 |
319
320**错误码:**
321
322以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
323
324| 错误码ID | 错误信息 |
325| ------- | --------------------------------------------|
326| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
327
328**示例:**
329
330```ts
331import { drawing } from '@kit.ArkGraphics2D';
332
333const path = new drawing.Path();
334path.rMoveTo(10, 10);
335```
336
337## rLineTo<sup>12+</sup>
338
339rLineTo(dx: number, dy: number): void
340
341使用相对位置在当前路径上添加一条当前路径终点(若路径没有内容则默认为 (0, 0))到目标点位置的线段。
342
343**系统能力:** SystemCapability.Graphics.Drawing
344
345**参数:**
346
347| 参数名 | 类型   | 必填 | 说明                    |
348| ------ | ------ | ---- | ----------------------- |
349| dx     | number | 是   | 目标点相对于当前路径终点的x轴偏移量,正数往x轴正方向偏移,负数往x轴负方向偏移,该参数为浮点数。 |
350| dy     | number | 是   | 目标点相对于当前路径终点的y轴偏移量,正数往y轴正方向偏移,负数往y轴负方向偏移,该参数为浮点数。 |
351
352**错误码:**
353
354以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
355
356| 错误码ID | 错误信息 |
357| ------- | --------------------------------------------|
358| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
359
360**示例:**
361
362```ts
363import { drawing } from '@kit.ArkGraphics2D';
364
365const path = new drawing.Path();
366path.rLineTo(400, 200);
367```
368
369## rQuadTo<sup>12+</sup>
370
371rQuadTo(dx1: number, dy1: number, dx2: number, dy2: number): void
372
373使用相对位置在当前路径上添加一条当前路径终点(若路径没有内容则默认为 (0, 0))到目标点位置的二阶贝塞尔曲线。
374
375**系统能力:** SystemCapability.Graphics.Drawing
376
377**参数:**
378
379| 参数名 | 类型   | 必填 | 说明                  |
380| ------ | ------ | ---- | --------------------- |
381| dx1  | number | 是   | 控制点相对于路径终点的x轴偏移量,正数往x轴正方向偏移,负数往x轴负方向偏移,该参数为浮点数。 |
382| dy1  | number | 是   | 控制点相对于路径终点的y轴偏移量,正数往y轴正方向偏移,负数往y轴负方向偏移,该参数为浮点数。 |
383| dx2   | number | 是   | 目标点相对于路径终点的x轴偏移量,正数往x轴正方向偏移,负数往x轴负方向偏移,该参数为浮点数。 |
384| dy2   | number | 是   | 目标点相对于路径终点的y轴偏移量,正数往y轴正方向偏移,负数往y轴负方向偏移,该参数为浮点数。 |
385
386**错误码:**
387
388以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
389
390| 错误码ID | 错误信息 |
391| ------- | --------------------------------------------|
392| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
393
394**示例:**
395
396```ts
397import { drawing } from '@kit.ArkGraphics2D';
398
399const path = new drawing.Path();
400path.rQuadTo(100, 0, 0, 200);
401```
402
403## rConicTo<sup>12+</sup>
404
405rConicTo(ctrlX: number, ctrlY: number, endX: number, endY: number, weight: number): void
406
407使用相对位置在当前路径上添加一条路径终点(若路径没有内容则默认为 (0, 0))到目标点位置的圆锥曲线。
408
409**系统能力:** SystemCapability.Graphics.Drawing
410
411**参数:**
412
413| 参数名 | 类型   | 必填 | 说明                  |
414| ------ | ------ | ---- | --------------------- |
415| ctrlX  | number | 是   | 控制点相对于路径终点的x轴偏移量,正数往x轴正方向偏移,负数往x轴负方向偏移,该参数为浮点数。 |
416| ctrlY  | number | 是   | 控制点相对于路径终点的y轴偏移量,正数往y轴正方向偏移,负数往y轴负方向偏移,该参数为浮点数。 |
417| endX   | number | 是   | 目标点相对于路径终点的x轴偏移量,正数往x轴正方向偏移,负数往x轴负方向偏移,该参数为浮点数。 |
418| endY   | number | 是   | 目标点相对于路径终点的y轴偏移量,正数往y轴正方向偏移,负数往y轴负方向偏移,该参数为浮点数。 |
419| weight | number | 是   | 表示曲线的权重,决定了曲线的形状,越大越接近控制点。若小于等于0则等同于使用[rLineTo](#rlineto12)添加一条到结束点的线段,若为1则等同于[rQuadTo](#rquadto12),该参数为浮点数。 |
420
421**错误码:**
422
423以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
424
425| 错误码ID | 错误信息 |
426| ------- | --------------------------------------------|
427| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
428
429**示例:**
430
431```ts
432import { drawing } from '@kit.ArkGraphics2D';
433
434const path = new drawing.Path();
435path.rConicTo(200, 400, 100, 200, 0);
436```
437
438## rCubicTo<sup>12+</sup>
439
440rCubicTo(ctrlX1: number, ctrlY1: number, ctrlX2: number, ctrlY2: number, endX: number, endY: number): void
441
442使用相对位置在当前路径上添加一条当前路径终点(若路径没有内容则默认为 (0, 0))到目标点位置的三阶贝塞尔曲线。
443
444**系统能力:** SystemCapability.Graphics.Drawing
445
446**参数:**
447
448| 参数名 | 类型   | 必填 | 说明                        |
449| ------ | ------ | ---- | --------------------------- |
450| ctrlX1 | number | 是   | 第一个控制点相对于路径终点的x轴偏移量,正数往x轴正方向偏移,负数往x轴负方向偏移,该参数为浮点数。 |
451| ctrlY1 | number | 是   | 第一个控制点相对于路径终点的y轴偏移量,正数往y轴正方向偏移,负数往y轴负方向偏移,该参数为浮点数。 |
452| ctrlX2 | number | 是   | 第二个控制点相对于路径终点的x轴偏移量,正数往x轴正方向偏移,负数往x轴负方向偏移,该参数为浮点数。 |
453| ctrlY2 | number | 是   | 第二个控制点相对于路径终点的y轴偏移量,正数往y轴正方向偏移,负数往y轴负方向偏移,该参数为浮点数。 |
454| endX   | number | 是   | 目标点相对于路径终点的x轴偏移量,正数往x轴正方向偏移,负数往x轴负方向偏移,该参数为浮点数。 |
455| endY   | number | 是   | 目标点相对于路径终点的y轴偏移量,正数往y轴正方向偏移,负数往y轴负方向偏移,该参数为浮点数。 |
456
457**错误码:**
458
459以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
460
461| 错误码ID | 错误信息 |
462| ------- | --------------------------------------------|
463| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
464
465**示例:**
466
467```ts
468import { drawing } from '@kit.ArkGraphics2D';
469
470const path = new drawing.Path();
471path.rCubicTo(200, 0, 0, 200, -20, 0);
472```
473
474## addArc<sup>12+</sup>
475
476addArc(rect: common2D.Rect, startAngle: number, sweepAngle: number): void
477
478向路径添加一段圆弧。
479当startAngle和sweepAngle同时满足以下两种情况时,添加整个椭圆而不是圆弧:
4801.startAngle对90取余接近于0;
4812.sweepAngle不在(-360, 360)区间内。
482其余情况sweepAngle会对360取余后添加圆弧。
483
484**系统能力:** SystemCapability.Graphics.Drawing
485
486**参数:**
487
488| 参数名         | 类型                                       | 必填   | 说明                  |
489| ----------- | ---------------------------------------- | ---- | ------------------- |
490| rect        | [common2D.Rect](js-apis-graphics-common2D.md#rect) | 是    | 包含弧的椭圆的矩形边界。      |
491| startAngle   | number | 是   | 弧的起始角度,单位为度,0度为x轴正方向,该参数为浮点数。 |
492| sweepAngle   | number | 是   | 扫描角度,单位为度。正数表示顺时针方向,负数表示逆时针方向,该参数为浮点数。 |
493
494**错误码:**
495
496以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
497
498| 错误码ID | 错误信息 |
499| ------- | --------------------------------------------|
500| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
501
502**示例:**
503
504```ts
505import { common2D, drawing } from '@kit.ArkGraphics2D';
506
507let path = new drawing.Path();
508const rect: common2D.Rect = {left:100, top:100, right:500, bottom:500};
509path.addArc(rect, 90, 180);
510```
511
512## addCircle<sup>12+</sup>
513
514addCircle(x: number, y: number, radius: number, pathDirection?: PathDirection): void
515
516按指定方向,向路径添加圆形,圆的起点位于(x + radius, y)。
517
518**系统能力:** SystemCapability.Graphics.Drawing
519
520**参数:**
521
522| 参数名         | 类型                                       | 必填   | 说明                  |
523| ----------- | ---------------------------------------- | ---- | ------------------- |
524| x   | number | 是   | 表示圆心的x轴坐标,该参数为浮点数。 |
525| y   | number | 是   | 表示圆心的y轴坐标,该参数为浮点数。 |
526| radius   | number | 是   | 表示圆形的半径,该参数为浮点数,小于等于0时不会有任何效果。 |
527| pathDirection   | [PathDirection](arkts-apis-graphics-drawing-e.md#pathdirection12)  | 否   | 表示路径方向,默认为顺时针方向。 |
528
529**错误码:**
530
531以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
532
533| 错误码ID | 错误信息 |
534| ------- | --------------------------------------------|
535| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
536
537**示例:**
538
539```ts
540
541import { drawing } from '@kit.ArkGraphics2D';
542
543let path = new drawing.Path();
544path.addCircle(100, 200, 50, drawing.PathDirection.CLOCKWISE);
545```
546
547## addOval<sup>12+</sup>
548
549addOval(rect: common2D.Rect, start: number, pathDirection?: PathDirection): void
550
551按指定方向,将矩形的内切椭圆添加到路径中。
552
553**系统能力:** SystemCapability.Graphics.Drawing
554
555**参数:**
556
557| 参数名         | 类型                                       | 必填   | 说明                  |
558| ----------- | ---------------------------------------- | ---- | ------------------- |
559| rect        | [common2D.Rect](js-apis-graphics-common2D.md#rect) | 是    | 椭圆的矩形边界。      |
560| start   | number | 是   | 表示椭圆初始点的索引,0,1,2,3分别对应椭圆的上端点,右端点,下端点,左端点,该参数为不小于0的整数,大于等于4时会对4取余。 |
561| pathDirection   | [PathDirection](arkts-apis-graphics-drawing-e.md#pathdirection12)  | 否   | 表示路径方向,默认为顺时针方向。 |
562
563**错误码:**
564
565以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
566
567| 错误码ID | 错误信息 |
568| ------- | --------------------------------------------|
569| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.|
570
571**示例:**
572
573```ts
574import { common2D, drawing } from '@kit.ArkGraphics2D';
575
576let path = new drawing.Path();
577const rect: common2D.Rect = {left:100, top:100, right:500, bottom:500};
578path.addOval(rect, 5, drawing.PathDirection.CLOCKWISE);
579```
580
581## addRect<sup>12+</sup>
582
583addRect(rect: common2D.Rect, pathDirection?: PathDirection): void
584
585按指定方向,将矩形添加到路径中,添加的路径的起始点为矩形左上角。
586
587**系统能力:** SystemCapability.Graphics.Drawing
588
589**参数:**
590
591| 参数名         | 类型                                       | 必填   | 说明                  |
592| ----------- | ---------------------------------------- | ---- | ------------------- |
593| rect        | [common2D.Rect](js-apis-graphics-common2D.md#rect) | 是    | 向路径中添加的矩形轮廓。      |
594| pathDirection   | [PathDirection](arkts-apis-graphics-drawing-e.md#pathdirection12)  | 否   | 表示路径方向,默认为顺时针方向。 |
595
596**错误码:**
597
598以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
599
600| 错误码ID | 错误信息 |
601| ------- | --------------------------------------------|
602| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.|
603
604**示例:**
605
606```ts
607import { common2D, drawing } from '@kit.ArkGraphics2D';
608
609let path = new drawing.Path();
610const rect: common2D.Rect = {left:100, top:100, right:500, bottom:500};
611path.addRect(rect, drawing.PathDirection.CLOCKWISE);
612```
613
614## addRoundRect<sup>12+</sup>
615
616addRoundRect(roundRect: RoundRect, pathDirection?: PathDirection): void
617
618按指定方向,向路径添加圆角矩形轮廓。路径添加方向为顺时针时,起始点位于圆角矩形左下方圆角与左边界的交点;路径添加方向为逆时针时,起始点位于圆角矩形左上方圆角与左边界的交点。
619
620**系统能力:** SystemCapability.Graphics.Drawing
621
622**参数:**
623
624| 参数名         | 类型                                       | 必填   | 说明                  |
625| ----------- | ---------------------------------------- | ---- | ------------------- |
626| roundRect        | [RoundRect](arkts-apis-graphics-drawing-RoundRect.md) | 是    | 圆角矩形对象。      |
627| pathDirection   | [PathDirection](arkts-apis-graphics-drawing-e.md#pathdirection12)  | 否   | 表示路径方向,默认为顺时针方向。 |
628
629**错误码:**
630
631以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
632
633| 错误码ID | 错误信息 |
634| ------- | --------------------------------------------|
635| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.|
636
637**示例:**
638
639```ts
640import { common2D, drawing } from '@kit.ArkGraphics2D';
641
642let path = new drawing.Path();
643const rect: common2D.Rect = {left:100, top:100, right:500, bottom:500};
644let roundRect = new drawing.RoundRect(rect, 50, 50);
645path.addRoundRect(roundRect, drawing.PathDirection.CLOCKWISE);
646```
647
648## addPath<sup>12+</sup>
649
650addPath(path: Path, matrix?: Matrix | null): void
651
652对源路径进行矩阵变换后,将其添加到当前路径中。
653
654**系统能力:** SystemCapability.Graphics.Drawing
655
656**参数:**
657
658| 参数名         | 类型                                       | 必填   | 说明                  |
659| ----------- | ---------------------------------------- | ---- | ------------------- |
660| path        | [Path](arkts-apis-graphics-drawing-Path.md) | 是    | 表示源路径对象。      |
661| matrix   | [Matrix](arkts-apis-graphics-drawing-Matrix.md)\|null  | 否   | 表示矩阵对象,默认为单位矩阵。 |
662
663**错误码:**
664
665以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
666
667| 错误码ID | 错误信息 |
668| ------- | --------------------------------------------|
669| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
670
671**示例:**
672
673```ts
674import { common2D, drawing } from '@kit.ArkGraphics2D';
675
676let path = new drawing.Path();
677let matrix = new drawing.Matrix();
678const rect: common2D.Rect = {left:100, top:100, right:500, bottom:500};
679let roundRect = new drawing.RoundRect(rect, 50, 50);
680path.addRoundRect(roundRect, drawing.PathDirection.CLOCKWISE);
681let dstPath = new drawing.Path();
682dstPath.addPath(path, matrix);
683```
684
685## transform<sup>12+</sup>
686
687transform(matrix: Matrix): void
688
689对路径进行矩阵变换。
690
691**系统能力:** SystemCapability.Graphics.Drawing
692
693**参数:**
694
695| 参数名         | 类型                                       | 必填   | 说明                  |
696| ----------- | ---------------------------------------- | ---- | ------------------- |
697| matrix   | [Matrix](arkts-apis-graphics-drawing-Matrix.md)  | 是   | 表示矩阵对象。 |
698
699**错误码:**
700
701以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
702
703| 错误码ID | 错误信息 |
704| ------- | --------------------------------------------|
705| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
706
707**示例:**
708
709```ts
710import { common2D, drawing } from '@kit.ArkGraphics2D';
711
712let path = new drawing.Path();
713let matrix = new drawing.Matrix();
714matrix.setScale(1.5, 1.5, 10, 10);
715const rect: common2D.Rect = {left:100, top:100, right:500, bottom:500};
716let roundRect = new drawing.RoundRect(rect, 50, 50);
717path.addRoundRect(roundRect, drawing.PathDirection.CLOCKWISE);
718path.transform(matrix);
719```
720
721## contains<sup>12+</sup>
722
723contains(x: number, y: number): boolean
724
725判断指定坐标点是否被路径包含,判定是否被路径包含的规则参考[PathFillType](arkts-apis-graphics-drawing-e.md#pathfilltype12)。
726
727**系统能力:** SystemCapability.Graphics.Drawing
728
729**参数:**
730
731| 参数名 | 类型   | 必填 | 说明                    |
732| ------ | ------ | ---- | ----------------------- |
733| x      | number | 是   | x轴上坐标点,该参数必须为浮点数。 |
734| y      | number | 是   | y轴上坐标点,该参数必须为浮点数。 |
735
736**返回值:**
737
738| 类型    | 说明           |
739| ------- | -------------- |
740| boolean | 返回指定坐标点是否在路径内。true表示点在路径内,false表示点不在路径内。 |
741
742**错误码:**
743
744以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
745
746| 错误码ID | 错误信息 |
747| ------- | --------------------------------------------|
748| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
749
750**示例:**
751
752```ts
753import { common2D, drawing } from '@kit.ArkGraphics2D';
754
755const path = new drawing.Path();
756let rect : common2D.Rect = {left: 50, top: 50, right: 250, bottom: 250};
757path.addRect(rect, drawing.PathDirection.CLOCKWISE);
758console.info("test contains: " + path.contains(0, 0));
759console.info("test contains: " + path.contains(60, 60));
760```
761
762## setLastPoint<sup>20+</sup>
763
764setLastPoint(x: number, y: number): void
765
766修改路径的最后一个点。
767
768**系统能力:** SystemCapability.Graphics.Drawing
769
770**参数:**
771
772| 参数名 | 类型   | 必填 | 说明                    |
773| ------ | ------ | ---- | ----------------------- |
774| x      | number | 是   | 指定点的x轴坐标,该参数为浮点数。0表示坐标原点,负数表示位于坐标原点左侧,正数表示位于坐标原点右侧。 |
775| y      | number | 是   | 指定点的y轴坐标,该参数为浮点数。0表示坐标原点,负数表示位于坐标原点上侧,正数表示位于坐标原点下侧。 |
776
777**示例:**
778
779```ts
780import { drawing } from '@kit.ArkGraphics2D';
781const path = new drawing.Path();
782path.moveTo(0, 0);
783path.lineTo(0, 700);
784let isEmpty = path.isEmpty();
785console.info('isEmpty:', isEmpty);
786path.reset();
787isEmpty = path.isEmpty();
788console.info('isEmpty:', isEmpty);
789path.setLastPoint(50, 50);
790isEmpty = path.isEmpty();
791console.info('isEmpty:', isEmpty);
792```
793
794## setFillType<sup>12+</sup>
795
796setFillType(pathFillType: PathFillType): void
797
798设置路径的填充类型,决定路径内部区域的定义方式。例如,使用Winding填充类型时,路径内部区域由路径环绕的次数决定,而使用EvenOdd填充类型时,路径内部区域由路径环绕的次数是否为奇数决定。
799
800**系统能力:** SystemCapability.Graphics.Drawing
801
802**参数:**
803
804| 参数名         | 类型                                       | 必填   | 说明                  |
805| ----------- | ---------------------------------------- | ---- | ------------------- |
806| pathFillType   | [PathFillType](arkts-apis-graphics-drawing-e.md#pathfilltype12)  | 是   | 表示路径填充规则。 |
807
808**错误码:**
809
810以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
811
812| 错误码ID | 错误信息 |
813| ------- | --------------------------------------------|
814| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.|
815
816**示例:**
817
818```ts
819import { drawing } from '@kit.ArkGraphics2D';
820
821const path = new drawing.Path();
822path.setFillType(drawing.PathFillType.WINDING);
823```
824
825## getFillType<sup>20+</sup>
826
827getFillType(): PathFillType
828
829获取路径的填充类型。
830
831**系统能力:** SystemCapability.Graphics.Drawing
832
833**返回值:**
834
835| 类型                                               | 说明                   |
836| -------------------------------------------------- | ---------------------- |
837| [PathFillType](arkts-apis-graphics-drawing-e.md#pathfilltype12) | 路径填充类型。 |
838
839**示例:**
840
841```ts
842import { drawing } from '@kit.ArkGraphics2D';
843const path = new drawing.Path();
844path.setFillType(drawing.PathFillType.WINDING);
845let type = path.getFillType();
846console.info("type :" + type);
847```
848
849## getBounds<sup>12+</sup>
850
851getBounds(): common2D.Rect
852
853获取包含路径的最小矩形边界。
854
855**系统能力:** SystemCapability.Graphics.Drawing
856
857**返回值:**
858
859| 类型                                               | 说明                   |
860| -------------------------------------------------- | ---------------------- |
861| [common2D.Rect](js-apis-graphics-common2D.md#rect) | 包含路径的最小矩形区域。 |
862
863**示例:**
864
865```ts
866import { common2D, drawing } from '@kit.ArkGraphics2D';
867
868const path = new drawing.Path();
869path.lineTo(50, 40)
870let rect : common2D.Rect = {left: 0, top: 0, right: 0, bottom: 0};
871rect = path.getBounds();
872console.info("test rect.left: " + rect.left);
873console.info("test rect.top: " + rect.top);
874console.info("test rect.right: " + rect.right);
875console.info("test rect.bottom: " + rect.bottom);
876```
877
878## addPolygon<sup>12+</sup>
879
880addPolygon(points: Array\<common2D.Point>, close: boolean): void
881
882通过坐标点列表添加多条连续的线段。
883
884**系统能力:** SystemCapability.Graphics.Drawing
885
886**参数:**
887
888| 参数名 | 类型   | 必填 | 说明                    |
889| ------ | ------ | ---- | ----------------------- |
890| points | Array\<[common2D.Point](js-apis-graphics-common2D.md#point12)>   | 是   | 坐标点数组。 |
891| close  | boolean                                                        | 是   | 表示是否将路径闭合,即是否添加路径起始点到终点的连线。true表示将路径闭合,false表示不将路径闭合。 |
892
893**错误码:**
894
895以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
896
897| 错误码ID | 错误信息 |
898| ------- | --------------------------------------------|
899| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
900
901**示例:**
902
903```ts
904import { common2D, drawing } from '@kit.ArkGraphics2D';
905
906let pointsArray = new Array<common2D.Point>();
907const point1: common2D.Point = { x: 200, y: 200 };
908const point2: common2D.Point = { x: 400, y: 200 };
909const point3: common2D.Point = { x: 100, y: 400 };
910const point4: common2D.Point = { x: 300, y: 400 };
911pointsArray.push(point1);
912pointsArray.push(point2);
913pointsArray.push(point3);
914pointsArray.push(point4);
915const path = new drawing.Path();
916path.addPolygon(pointsArray, false);
917```
918
919## offset<sup>12+</sup>
920
921offset(dx: number, dy: number): Path
922
923将路径沿着x轴和y轴方向偏移一定距离并保存在返回的路径对象中。
924
925**系统能力:** SystemCapability.Graphics.Drawing
926
927**参数:**
928
929| 参数名 | 类型   | 必填 | 说明                    |
930| ------ | ------ | ---- | ----------------------- |
931| dx     | number        | 是   | x轴方向偏移量,正数往x轴正方向偏移,负数往x轴负方向偏移,该参数为浮点数。 |
932| dy     | number        | 是   | y轴方向偏移量,正数往y轴正方向偏移,负数往y轴负方向偏移,该参数为浮点数。 |
933
934**返回值:**
935
936| 类型   | 说明                |
937| ------ | ------------------ |
938| [Path](arkts-apis-graphics-drawing-Path.md) | 返回当前路径偏移(dx,dy)后生成的新路径对象。 |
939
940**错误码:**
941
942以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
943
944| 错误码ID | 错误信息 |
945| ------- | --------------------------------------------|
946| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
947
948**示例:**
949
950```ts
951import { drawing } from '@kit.ArkGraphics2D';
952
953const path = new drawing.Path();
954path.moveTo(200, 200);
955path.lineTo(300, 300);
956const dst = path.offset(200, 200);
957```
958
959## op<sup>12+</sup>
960
961op(path: Path, pathOp: PathOp): boolean
962
963将当前路径置为和path按照指定的路径操作类型合并后的结果。
964
965**系统能力:** SystemCapability.Graphics.Drawing
966
967**参数:**
968
969| 参数名 | 类型   | 必填 | 说明                    |
970| ------ | ------ | ---- | ----------------------- |
971| path    | [Path](arkts-apis-graphics-drawing-Path.md) | 是   | 路径对象,用于与当前路径合并。 |
972| pathOp  | [PathOp](arkts-apis-graphics-drawing-e.md#pathop12)   | 是    | 路径操作类型枚举。    |
973
974**返回值:**
975
976| 类型   | 说明                |
977| ------ | ------------------ |
978| boolean | 返回路径合并是否成功的结果。true表示合并成功,false表示合并失败。 |
979
980**错误码:**
981
982以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
983
984| 错误码ID | 错误信息 |
985| ------- | --------------------------------------------|
986| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed. |
987
988**示例:**
989
990```ts
991import { drawing } from '@kit.ArkGraphics2D';
992
993const path = new drawing.Path();
994const path2 = new drawing.Path();
995path.addCircle(100, 200, 100, drawing.PathDirection.CLOCKWISE);
996console.info("get pathOp: ", path2.op(path, drawing.PathOp.DIFFERENCE));
997```
998
999## close
1000
1001close(): void
1002
1003闭合路径,会添加一条从路径起点位置到最后点位置的线段。
1004
1005**系统能力:** SystemCapability.Graphics.Drawing
1006
1007**示例:**
1008
1009```ts
1010import { drawing } from '@kit.ArkGraphics2D';
1011
1012let path = new drawing.Path();
1013path.moveTo(10,10);
1014path.cubicTo(10, 10, 10, 10, 15, 15);
1015path.close();
1016```
1017
1018## reset
1019
1020reset(): void
1021
1022重置自定义路径数据。
1023
1024**系统能力:** SystemCapability.Graphics.Drawing
1025
1026**示例:**
1027
1028```ts
1029import { drawing } from '@kit.ArkGraphics2D';
1030
1031let path = new drawing.Path();
1032path.moveTo(10,10);
1033path.cubicTo(10, 10, 10, 10, 15, 15);
1034path.reset();
1035```
1036
1037## rewind<sup>20+</sup>
1038
1039rewind(): void
1040
1041将路径内添加的各类点/线清空,但是保留内存空间。
1042
1043**系统能力:** SystemCapability.Graphics.Drawing
1044
1045**示例:**
1046
1047```ts
1048import { drawing } from '@kit.ArkGraphics2D';
1049let path = new drawing.Path();
1050path.moveTo(10,10);
1051path.lineTo(20,20);
1052path.rewind();
1053let empty = path.isEmpty();
1054console.info('empty : ', empty);
1055```
1056
1057## isEmpty<sup>20+</sup>
1058
1059isEmpty(): boolean
1060
1061判断路径是否为空。
1062
1063**系统能力:** SystemCapability.Graphics.Drawing
1064
1065**返回值:**
1066
1067| 类型  | 说明 |
1068| ------ | ---- |
1069| boolean | 路径是否为空。true表示当前路径为空,false表示路径不为空。|
1070
1071**示例:**
1072
1073```ts
1074import { drawing } from '@kit.ArkGraphics2D';
1075let path = new drawing.Path();
1076path.moveTo(10,10);
1077path.lineTo(20,20);
1078let isEmpty = path.isEmpty();
1079console.info('isEmpty:', isEmpty);
1080```
1081
1082## isRect<sup>20+</sup>
1083
1084isRect(rect: common2D.Rect | null): boolean
1085
1086判断路径是否构成矩形。
1087
1088**系统能力:** SystemCapability.Graphics.Drawing
1089
1090**参数:**
1091
1092| 参数名 | 类型   | 必填 | 说明                    |
1093| ------ | ------ | ---- | ----------------------- |
1094| rect   | [common2D.Rect](js-apis-graphics-common2D.md#rect)\| null | 是   | 矩形对象,作为出参使用,路径构成矩形时,会被改写为路径表示的矩形,否则不会改变。可以为null,表示无需获取路径表示的矩形。 |
1095
1096**返回值:**
1097
1098| 类型  | 说明 |
1099| ------ | ---- |
1100| boolean | 返回路径是否构成矩形。true表示路径构成矩形,false表示路径不构成矩形。|
1101
1102**示例:**
1103
1104```ts
1105import { common2D, drawing } from '@kit.ArkGraphics2D';
1106
1107let path = new drawing.Path();
1108path.moveTo(10,10);
1109path.lineTo(20,10);
1110let isRect = path.isRect(null);
1111console.info("isRect: ", isRect);
1112let rect: common2D.Rect = { left : 100, top : 100, right : 400, bottom : 500 };
1113path.lineTo(20, 20);
1114path.lineTo(10, 20);
1115path.lineTo(10, 10);
1116isRect = path.isRect(rect);
1117console.info('isRect: ', isRect);
1118```
1119
1120## getLength<sup>12+</sup>
1121
1122getLength(forceClosed: boolean): number
1123
1124获取路径长度。
1125
1126**系统能力:** SystemCapability.Graphics.Drawing
1127
1128**参数:**
1129
1130| 参数名| 类型  | 必填| 说明     |
1131| ----- | ------ | ---- | --------- |
1132| forceClosed  | boolean | 是  | 表示是否按照闭合路径测量,true表示测量时路径会被强制视为已闭合,false表示会根据路径的实际闭合状态测量。|
1133
1134**返回值:**
1135
1136| 类型  | 说明 |
1137| ------ | ---- |
1138| number | 路径长度。|
1139
1140**示例:**
1141
1142```ts
1143import { drawing } from '@kit.ArkGraphics2D';
1144
1145let path = new drawing.Path();
1146path.arcTo(20, 20, 180, 180, 180, 90);
1147let len = path.getLength(false);
1148console.info("path length = " + len);
1149```
1150
1151## getPositionAndTangent<sup>12+</sup>
1152
1153getPositionAndTangent(forceClosed: boolean, distance: number, position: common2D.Point, tangent: common2D.Point): boolean
1154
1155获取路径起始点指定距离处的坐标点和切线值。
1156
1157**系统能力:** SystemCapability.Graphics.Drawing
1158
1159**参数:**
1160
1161| 参数名   | 类型                                         | 必填 | 说明                            |
1162| -------- | -------------------------------------------- | ---- | ------------------------------- |
1163| forceClosed | boolean | 是   | 表示是否按照闭合路径测量,true表示测量时路径会被强制视为已闭合,false表示会根据路径的实际闭合状态测量。                 |
1164| distance | number | 是   | 表示与路径起始点的距离,小于0时会被视作0,大于路径长度时会被视作路径长度。该参数为浮点数。               |
1165| position | [common2D.Point](js-apis-graphics-common2D.md#point12) | 是   | 存储获取到的距离路径起始点distance处的点的坐标。                  |
1166| tangent | [common2D.Point](js-apis-graphics-common2D.md#point12) | 是   | 存储获取到的距离路径起始点distance处的点的切线值,tangent.x表示该点切线的余弦值,tangent.y表示该点切线的正弦值。                 |
1167
1168**返回值:**
1169
1170| 类型                  | 说明           |
1171| --------------------- | -------------- |
1172| boolean |表示是否成功获取距离路径起始点distance处的点的坐标和正切值的结果。true表示获取成功,false表示获取失败,position和tangent不会被改变。 |
1173
1174**错误码:**
1175
1176以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
1177
1178| 错误码ID | 错误信息 |
1179| ------- | --------------------------------------------|
1180| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
1181
1182**示例:**
1183
1184```ts
1185import { common2D, drawing } from '@kit.ArkGraphics2D';
1186
1187let path: drawing.Path = new drawing.Path();
1188path.moveTo(0, 0);
1189path.lineTo(0, 700);
1190path.lineTo(700, 0);
1191let position: common2D.Point = { x: 0.0, y: 0.0 };
1192let tangent: common2D.Point = { x: 0.0, y: 0.0 };
1193if (path.getPositionAndTangent(false, 0.1, position, tangent)) {
1194  console.info("getPositionAndTangent-----position:  "+ position.x);
1195  console.info("getPositionAndTangent-----position:  "+ position.y);
1196  console.info("getPositionAndTangent-----tangent:  "+ tangent.x);
1197  console.info("getPositionAndTangent-----tangent:  "+ tangent.y);
1198}
1199```
1200
1201## getSegment<sup>18+</sup>
1202
1203getSegment(forceClosed: boolean, start: number, stop: number, startWithMoveTo: boolean, dst: Path): boolean
1204
1205截取路径的片段并追加到目标路径上。
1206
1207**系统能力:** SystemCapability.Graphics.Drawing
1208
1209**参数:**
1210
1211| 参数名   | 类型                                         | 必填 | 说明                            |
1212| -------- | -------------------------------------------- | ---- | ------------------------------- |
1213| forceClosed | boolean | 是   | 表示是否按照闭合路径测量,true表示测量时路径会被强制视为已闭合,false表示会根据路径的实际闭合状态测量。                 |
1214| start | number | 是   | 表示与路径起始点的距离,距离路径起始点start距离的位置即为截取路径片段的起始点,小于0时会被视作0,大于等于stop时会截取失败。该参数为浮点数。               |
1215| stop | number | 是   | 表示与路径起始点的距离,距离路径起始点stop距离的位置即为截取路径片段的终点,小于等于start时会截取失败,大于路径长度时会被视作路径长度。该参数为浮点数。                  |
1216| startWithMoveTo | boolean | 是   | 表示是否在目标路径执行[moveTo](#moveto)移动到截取路径片段的起始点位置。true表示执行,false表示不执行。                |
1217| dst | [Path](arkts-apis-graphics-drawing-Path.md) | 是   | 目标路径,截取成功时会将得到的路径片段追加到目标路径上,截取失败时不做改变。               |
1218
1219**返回值:**
1220
1221| 类型                  | 说明           |
1222| --------------------- | -------------- |
1223| boolean |表示是否成功截取路径片段。true表示截取成功,false表示截取失败。 |
1224
1225**示例:**
1226
1227```ts
1228import { drawing } from '@kit.ArkGraphics2D';
1229
1230let path: drawing.Path = new drawing.Path();
1231path.moveTo(0, 0);
1232path.lineTo(0, 700);
1233path.lineTo(700, 0);
1234let dstPath: drawing.Path = new drawing.Path();
1235console.info("getSegment-----result:  "+ path.getSegment(true, 10.0, 20.0, true, dstPath));
1236```
1237
1238## isClosed<sup>12+</sup>
1239
1240isClosed(): boolean
1241
1242获取路径是否闭合。
1243
1244**系统能力:** SystemCapability.Graphics.Drawing
1245
1246**返回值:**
1247
1248| 类型                  | 说明           |
1249| --------------------- | -------------- |
1250| boolean | 表示当前路径是否闭合,true表示闭合,false表示不闭合。 |
1251
1252**示例:**
1253
1254```ts
1255import { drawing } from '@kit.ArkGraphics2D';
1256
1257let path: drawing.Path = new drawing.Path();
1258path.moveTo(0, 0);
1259path.lineTo(0, 700);
1260if (path.isClosed()) {
1261  console.info("path is closed.");
1262} else {
1263  console.info("path is not closed.");
1264}
1265```
1266
1267## getMatrix<sup>12+</sup>
1268
1269getMatrix(forceClosed: boolean, distance: number, matrix: Matrix, flags: PathMeasureMatrixFlags): boolean
1270
1271在路径上的某个位置,获取一个变换矩阵,用于表示该点的坐标和朝向。
1272
1273**系统能力:** SystemCapability.Graphics.Drawing
1274
1275**参数:**
1276
1277| 参数名   | 类型                                         | 必填 | 说明                            |
1278| -------- | -------------------------------------------- | ---- | ------------------------------- |
1279| forceClosed | boolean | 是   | 表示是否按照闭合路径测量,true表示测量时路径会被强制视为已闭合,false表示会根据路径的实际闭合状态测量。                  |
1280| distance | number | 是   | 表示与路径起始点的距离,小于0时会被视作0,大于路径长度时会被视作路径长度。该参数为浮点数。                  |
1281| matrix | [Matrix](arkts-apis-graphics-drawing-Matrix.md) | 是   | 矩阵对象,用于存储得到的矩阵。                  |
1282| flags | [PathMeasureMatrixFlags](arkts-apis-graphics-drawing-e.md#pathmeasurematrixflags12) | 是   | 矩阵信息维度枚举。                  |
1283
1284**返回值:**
1285
1286| 类型                  | 说明           |
1287| --------------------- | -------------- |
1288| boolean | 返回是否成功获取变换矩阵的结果。true表示成功,false表示失败。 |
1289
1290**错误码:**
1291
1292以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
1293
1294| 错误码ID | 错误信息 |
1295| ------- | --------------------------------------------|
1296| 401 | Parameter error. Possible causes: Mandatory parameters are left unspecified. |
1297
1298**示例:**
1299
1300```ts
1301import { drawing } from '@kit.ArkGraphics2D';
1302
1303let path: drawing.Path = new drawing.Path();
1304let matrix = new drawing.Matrix();
1305if(path.getMatrix(false, 10, matrix, drawing.PathMeasureMatrixFlags.GET_TANGENT_MATRIX)) {
1306  console.info("path.getMatrix return true");
1307} else {
1308  console.info("path.getMatrix return false");
1309}
1310```
1311
1312## buildFromSvgString<sup>12+</sup>
1313
1314buildFromSvgString(str: string): boolean
1315
1316解析SVG字符串表示的路径。
1317
1318**系统能力:** SystemCapability.Graphics.Drawing
1319
1320**参数:**
1321
1322| 参数名   | 类型                                         | 必填 | 说明                            |
1323| -------- | -------------------------------------------- | ---- | ------------------------------- |
1324| str | string | 是   | SVG格式的字符串,用于描述绘制路径。                 |
1325
1326**返回值:**
1327
1328| 类型                  | 说明           |
1329| --------------------- | -------------- |
1330| boolean | 返回是否成功解析SVG字符串的结果。true表示解析成功,false表示解析失败。 |
1331
1332**错误码:**
1333
1334以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
1335
1336| 错误码ID | 错误信息 |
1337| ------- | --------------------------------------------|
1338| 401 | Parameter error. Possible causes: Mandatory parameters are left unspecified. |
1339
1340**示例:**
1341
1342```ts
1343import { drawing } from '@kit.ArkGraphics2D';
1344
1345let path: drawing.Path = new drawing.Path();
1346let svgStr: string =  "M150 100 L75 300 L225 300 Z";
1347if(path.buildFromSvgString(svgStr)) {
1348  console.info("buildFromSvgString return true");
1349} else {
1350  console.info("buildFromSvgString return false");
1351}
1352```
1353
1354## getPathIterator<sup>18+</sup>
1355
1356getPathIterator(): PathIterator
1357
1358返回该路径的操作迭代器。
1359
1360**系统能力:** SystemCapability.Graphics.Drawing
1361
1362**返回值:**
1363
1364| 类型                  | 说明           |
1365| --------------------- | -------------- |
1366| [PathIterator](arkts-apis-graphics-drawing-PathIterator.md) | 该路径的迭代器对象。 |
1367
1368**示例:**
1369
1370```ts
1371import { drawing } from '@kit.ArkGraphics2D';
1372
1373let path: drawing.Path = new drawing.Path();
1374let iter = path.getPathIterator();
1375```
1376
1377## approximate<sup>20+</sup>
1378
1379approximate(acceptableError: number): Array\<number>
1380
1381将当前路径转化为由连续直线段构成的近似路径。
1382
1383> **说明:**
1384>
1385> - 当acceptableError为0时,曲线路径被极度细分,会严重影响性能和内存消耗,不建议设置误差值为0。
1386> - 当acceptableError特别大时,路径会极度简化,保留少量关键点,可能会丢失原有形状。
1387> - 对于椭圆等曲线,当acceptableError过大时,拟合结果通常只包含椭圆的分段贝塞尔曲线的起止点,椭圆形会被极度简化为多边形。
1388
1389**系统能力:** SystemCapability.Graphics.Drawing
1390
1391**参数:**
1392
1393| 参数名   | 类型                                         | 必填 | 说明                            |
1394| -------- | -------------------------------------------- | ---- | ------------------------------- |
1395| acceptableError | number | 是 | 表示路径上每条线段的可接受误差。该参数为浮点数,不应小于0,当参数小于0时报错。 |
1396
1397**返回值:**
1398
1399| 类型                  | 说明           |
1400| --------------------- | -------------- |
1401| Array\<number> | 返回包含近似路径的点的数组,至少包含两个点。每个点由三个值组成:<br>1. 该点所在的位置距离路径起点的长度比例值,范围为[0.0, 1.0]。<br>2. 点的x坐标。<br>3. 点的y坐标。 |
1402
1403**错误码:**
1404
1405以下错误码的详细介绍请参见[图形绘制与显示错误码](../apis-arkgraphics2d/errorcode-drawing.md)。
1406
1407| 错误码ID | 错误信息 |
1408| ------- | --------------------------------------------|
1409| 25900001 | Parameter error.Possible causes: Incorrect parameter range. |
1410
1411**示例:**
1412
1413```ts
1414import { drawing } from '@kit.ArkGraphics2D';
1415
1416let path: drawing.Path = new drawing.Path();
1417path.moveTo(100, 100);
1418path.lineTo(500, 500);
1419let points: number[] = path.approximate(0.5);
1420for (let i = 0; i < points.length; i += 3) {
1421  console.info("PathApproximate Fraction =" + points[i] + ", X =" + points[i + 1] + ", Y =" + points[i + 2] + "\n");
1422}
1423```
1424
1425## interpolate<sup>20+</sup>
1426
1427interpolate(other: Path, weight: number, interpolatedPath: Path): boolean
1428
1429根据给定的权重,在当前路径和另一条路径之间进行插值,并将结果存储在目标路径对象中。两条路径点数相同即可插值成功,目标路径按照当前路径的结构进行创建。
1430
1431**系统能力:** SystemCapability.Graphics.Drawing
1432
1433**参数:**
1434
1435| 参数名   | 类型                                         | 必填 | 说明                            |
1436| -------- | -------------------------------------------- | ---- | ------------------------------- |
1437| other | [Path](arkts-apis-graphics-drawing-Path.md) | 是 | 表示另一条路径对象。 |
1438| weight | number | 是 | 表示插值权重,必须在[0.0, 1.0]范围内。该参数为浮点数。 |
1439| interpolatedPath | [Path](arkts-apis-graphics-drawing-Path.md) | 是 | 表示用于存储插值结果的目标路径对象。 |
1440
1441**返回值:**
1442
1443| 类型                  | 说明           |
1444| --------------------- | -------------- |
1445| boolean | 返回插值操作是否成功的结果。true表示插值成功,false表示插值失败。 |
1446
1447**错误码:**
1448
1449以下错误码的详细介绍请参见[图形绘制与显示错误码](../apis-arkgraphics2d/errorcode-drawing.md)。
1450
1451| 错误码ID | 错误信息 |
1452| ------- | --------------------------------------------|
1453| 25900001 | Parameter error.Possible causes: Incorrect parameter range. |
1454
1455**示例:**
1456
1457```ts
1458import { drawing } from '@kit.ArkGraphics2D';
1459
1460let path: drawing.Path = new drawing.Path();
1461path.moveTo(50, 50);
1462path.lineTo(100, 100);
1463path.lineTo(200, 200);
1464let other: drawing.Path = new drawing.Path();
1465other.moveTo(80, 80);
1466other.lineTo(300, 300);
1467let interpolatedPath: drawing.Path = new drawing.Path();
1468if (path.interpolate(other, 0.0, interpolatedPath)) {
1469  console.info('interpolate return true');
1470} else {
1471  console.info('interpolate return false');
1472}
1473```
1474
1475## isInterpolate<sup>20+</sup>
1476
1477isInterpolate(other: Path): boolean
1478
1479判断当前路径与另一条路径在结构和操作顺序上是否完全一致,以确定两条路径是否兼容插值。若路径中包含圆锥曲线(Conic)操作,则对应操作的权重值也必须一致,才能视为兼容插值。
1480
1481**系统能力:** SystemCapability.Graphics.Drawing
1482
1483**参数:**
1484
1485| 参数名   | 类型                                         | 必填 | 说明                            |
1486| -------- | -------------------------------------------- | ---- | ------------------------------- |
1487| other | [Path](arkts-apis-graphics-drawing-Path.md) | 是 | 表示另一条路径对象。 |
1488
1489**返回值:**
1490
1491| 类型                  | 说明           |
1492| --------------------- | -------------- |
1493| boolean | 返回当前路径与另一条路径是否兼容插值的结果。true表示兼容插值,false表示不兼容插值。 |
1494
1495**示例:**
1496
1497```ts
1498import { drawing } from '@kit.ArkGraphics2D';
1499
1500let path: drawing.Path = new drawing.Path();
1501path.moveTo(0, 0);
1502path.lineTo(100, 100);
1503let other: drawing.Path = new drawing.Path();
1504other.moveTo(0, 1);
1505other.lineTo(200, 200);
1506if (path.isInterpolate(other)) {
1507  console.info('isInterpolate return true');
1508} else {
1509  console.info('isInterpolate return false');
1510}
1511```