• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Path2D对象
2<!--Kit: ArkUI-->
3<!--Subsystem: ArkUI-->
4<!--Owner: @sd-wu-->
5<!--Designer: @sunbees-->
6<!--Tester: @liuli0427-->
7<!--Adviser: @HelloCrease-->
8
9>  **说明:**
10>  从API version 6开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
11
12路径对象,支持通过对象的接口进行路径的描述,并通过Canvas的stroke接口进行绘制。
13
14
15## addPath
16
17addPath(path: Object): void
18
19将另一个路径添加到当前的路径对象中。
20
21**参数:**
22
23| 参数   | 类型     | 描述             |
24| ---- | ------ | -------------- |
25| path | Object | 需要添加到当前路径的路径对象。 |
26
27**示例:**
28
29  ```html
30<!-- xxx.hml -->
31<div>
32    <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
33</div>
34  ```
35
36  ```js
37// xxx.js
38export default {
39    onShow() {
40        const el = this.$refs.canvas;
41        const ctx = el.getContext('2d');
42        var path1 = ctx.createPath2D("M250 150 L150 350 L350 350 Z");
43        var path2 = ctx.createPath2D();
44        path2.addPath(path1);
45        ctx.stroke(path2);
46    }
47}
48  ```
49
50  ![zh-cn_image_0000001173164873](figures/zh-cn_image_0000001173164873.png)
51
52## setTransform
53
54setTransform(scaleX: number, skewX: number, skewY: number, scaleY: number, translateX: number, translateY: number): void
55
56设置路径变换矩阵。
57
58**参数:**
59
60| 参数         | 类型     | 描述      |
61| ---------- | ------ | ------- |
62| scaleX     | number | x轴的缩放比例。 |
63| skewX      | number | x轴的倾斜角度。 |
64| skewY      | number | y轴的倾斜角度。 |
65| scaleY     | number | y轴的缩放比例。 |
66| translateX | number | x轴的平移距离。 |
67| translateY | number | y轴的平移距离。 |
68
69**示例:**
70
71  ```html
72<!-- xxx.hml -->
73<div>
74    <canvas ref="canvas" style="width: 300px; height: 250px; background-color: #ffff00;"></canvas>
75</div>
76  ```
77
78  ```js
79// xxx.js
80export default {
81    onShow() {
82        const el = this.$refs.canvas;
83        const ctx = el.getContext('2d');
84        var path = ctx.createPath2D("M250 150 L150 350 L350 350 Z");
85        path.setTransform(0.8, 0, 0, 0.4, 0, 0);
86        ctx.stroke(path);
87    }
88}
89  ```
90
91  ![zh-cn_image_0000001127125208](figures/zh-cn_image_0000001127125208.png)
92
93
94## closePath
95
96closePath(): void
97
98将路径的当前点移回到路径的起点,当前点到起点间画一条直线。如果形状已经闭合或只有一个点,则此功能不执行任何操作。
99
100**示例:**
101
102  ```html
103<!-- xxx.hml -->
104<div>
105    <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
106</div>
107  ```
108
109  ```js
110// xxx.js
111export default {
112    onShow() {
113        const el = this.$refs.canvas;
114        const ctx = el.getContext('2d');
115        var path = ctx.createPath2D();
116        path.moveTo(200, 100);
117        path.lineTo(300, 100);
118        path.lineTo(200, 200);
119        path.closePath();
120        ctx.stroke(path);
121    }
122}
123  ```
124
125  ![zh-cn_image_0000001127125202](figures/zh-cn_image_0000001127125202.png)
126
127
128## moveTo
129
130moveTo(x: number, y: number): void
131
132将路径的当前坐标点移动到目标点,移动过程中不绘制线条。
133
134**参数:**
135
136| 参数   | 类型     | 描述      |
137| ---- | ------ | ------- |
138| x    | number | 目标点X轴坐标。 |
139| y    | number | 目标点Y轴坐标。 |
140
141**示例:**
142
143  ```html
144<!-- xxx.hml -->
145<div>
146    <canvas ref="canvas" style="width: 300px; height: 250px; background-color: #ffff00;"></canvas>
147</div>
148  ```
149
150  ```js
151// xxx.js
152export default {
153    onShow() {
154        const el = this.$refs.canvas;
155        const ctx = el.getContext('2d');
156        var path = ctx.createPath2D();
157        path.moveTo(50, 100);
158        path.lineTo(250, 100);
159        path.lineTo(150, 200);
160        path.closePath();
161        ctx.stroke(path);
162    }
163}
164  ```
165
166  ![zh-cn_image_0000001173164869](figures/zh-cn_image_0000001173164869.png)
167
168
169## lineTo
170
171lineTo(x: number, y: number): void
172
173从当前点绘制一条直线到目标点。
174
175**参数:**
176
177| 参数   | 类型     | 描述      |
178| ---- | ------ | ------- |
179| x    | number | 目标点X轴坐标。 |
180| y    | number | 目标点Y轴坐标。|
181
182**示例:**
183
184  ```html
185<!-- xxx.hml -->
186<div>
187    <canvas ref="canvas" style="width: 400px; height: 450px; background-color: #ffff00;"></canvas>
188</div>
189  ```
190
191  ```js
192// xxx.js
193export default {
194    onShow() {
195        const el = this.$refs.canvas;
196        const ctx = el.getContext('2d');
197        var path = ctx.createPath2D();
198        path.moveTo(100, 100);
199        path.lineTo(100, 200);
200        path.lineTo(200, 200);
201        path.lineTo(200, 100);
202        path.closePath();
203        ctx.stroke(path);
204    }
205}
206  ```
207
208  ![zh-cn_image_0000001127285024](figures/zh-cn_image_0000001127285024.png)
209
210
211## bezierCurveTo
212
213bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void
214
215创建三次贝赛尔曲线的路径。
216
217**参数:**
218
219| 参数   | 类型     | 描述             |
220| ---- | ------ | -------------- |
221| cp1x | number | 第一个贝塞尔参数的x坐标值。 |
222| cp1y | number | 第一个贝塞尔参数的y坐标值。 |
223| cp2x | number | 第二个贝塞尔参数的x坐标值。 |
224| cp2y | number | 第二个贝塞尔参数的y坐标值。 |
225| x    | number | 路径结束时的x坐标值。    |
226| y    | number | 路径结束时的y坐标值。    |
227
228**示例:**
229
230  ```html
231<!-- xxx.hml -->
232<div>
233    <canvas ref="canvas" style="width: 300px; height: 250px; background-color: #ffff00;"></canvas>
234</div>
235  ```
236
237  ```js
238// xxx.js
239export default {
240    onShow() {
241        const el = this.$refs.canvas;
242        const ctx = el.getContext('2d');
243        var path = ctx.createPath2D();
244        path.moveTo(10, 10);
245        path.bezierCurveTo(20, 100, 200, 100, 200, 20);
246        ctx.stroke(path);
247    }
248}
249  ```
250
251  ![zh-cn_image_0000001173324783](figures/zh-cn_image_0000001173324783.png)
252
253
254## quadraticCurveTo
255
256quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void
257
258创建二次贝赛尔曲线的路径。
259
260**参数:**
261
262| 参数   | 类型     | 描述          |
263| ---- | ------ | ----------- |
264| cpx  | number | 贝塞尔参数的x坐标值。 |
265| cpy  | number | 贝塞尔参数的y坐标值。 |
266| x    | number | 路径结束时的x坐标值。 |
267| y    | number | 路径结束时的y坐标值。 |
268
269**示例:**
270
271  ```html
272<!-- xxx.hml -->
273<div>
274    <canvas ref="canvas" style="width: 300px; height: 250px; background-color: #ffff00;"></canvas>
275</div>
276  ```
277
278  ```js
279// xxx.js
280export default {
281    onShow() {
282        const el = this.$refs.canvas;
283        const ctx = el.getContext('2d');
284        var path = ctx.createPath2D();
285        path.moveTo(10, 10);
286        path.quadraticCurveTo(100, 100, 200, 20);
287        ctx.stroke(path);
288    }
289}
290  ```
291
292  ![zh-cn_image_0000001173164871](figures/zh-cn_image_0000001173164871.png)
293
294
295## arc
296
297arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise: number): void
298
299绘制弧线路径。
300
301**参数:**
302
303| 参数            | 类型      | 描述         |
304| ------------- | ------- | ---------- |
305| x             | number  | 弧线圆心的x坐标值。 |
306| y             | number  | 弧线圆心的y坐标值。 |
307| radius        | number  | 弧线的圆半径。    |
308| startAngle    | number  | 弧线的起始弧度。   |
309| endAngle      | number  | 弧线的终止弧度。   |
310| anticlockwise | boolean | 是否逆时针绘制圆弧。 |
311
312**示例:**
313
314  ```html
315<!-- xxx.hml -->
316<div>
317    <canvas ref="canvas" style="width: 300px; height: 250px; background-color: #ffff00;"></canvas>
318</div>
319  ```
320
321  ```js
322// xxx.js
323export default {
324    onShow() {
325        const el = this.$refs.canvas;
326        const ctx = el.getContext('2d');
327        var path = ctx.createPath2D();
328        path.arc(100, 75, 50, 0, 6.28);
329        ctx.stroke(path);
330    }
331}
332  ```
333
334  ![zh-cn_image_0000001173164867](figures/zh-cn_image_0000001173164867.png)
335
336
337## arcTo
338
339arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void
340
341依据圆弧经过的点和圆弧半径创建圆弧路径。
342
343**参数:**
344
345| 参数     | 类型     | 描述              |
346| ------ | ------ | --------------- |
347| x1     | number | 圆弧经过的第一个点的x坐标值。 |
348| y1     | number | 圆弧经过的第一个点的y坐标值。 |
349| x2     | number | 圆弧经过的第二个点的x坐标值。 |
350| y2     | number | 圆弧经过的第二个点的y坐标值。 |
351| radius | number | 圆弧的圆半径值。        |
352
353**示例:**
354
355  ```html
356<!-- xxx.hml -->
357<div>
358    <canvas ref="canvas" style="width: 300px; height: 250px; background-color: #ffff00;"></canvas>
359</div>
360  ```
361
362  ```js
363// xxx.js
364export default {
365    onShow() {
366        const el = this.$refs.canvas;
367        const ctx = el.getContext('2d');
368        var path = ctx.createPath2D();
369        path.arcTo(150, 20, 150, 70, 50);
370        ctx.stroke(path);
371    }
372}
373  ```
374
375  ![zh-cn_image_0000001127125204](figures/zh-cn_image_0000001127125204.png)
376
377
378## ellipse
379
380ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise: number): void
381
382在规定的矩形区域绘制一个椭圆。
383
384**参数:**
385
386| 参数            | 类型     | 描述                                   |
387| ------------- | ------ | ------------------------------------ |
388| x             | number | 椭圆圆心的x轴坐标。                           |
389| y             | number | 椭圆圆心的y轴坐标。                           |
390| radiusX       | number | 椭圆x轴的半径长度。                           |
391| radiusY       | number | 椭圆y轴的半径长度。                           |
392| rotation      | number | 椭圆的旋转角度,单位为弧度。                       |
393| startAngle    | number | 椭圆绘制的起始点角度,以弧度表示。                    |
394| endAngle      | number | 椭圆绘制的结束点角度,以弧度表示。                    |
395| anticlockwise | number | 是否以逆时针方向绘制椭圆,0为顺时针,1为逆时针。(可选参数,默认为0) |
396
397**示例:**
398
399  ```html
400<!-- xxx.hml -->
401<div>
402    <canvas ref="canvas" style="width: 500px; height: 450px; background-color: #ffff00;"></canvas>
403</div>
404  ```
405
406  ```js
407// xxx.js
408export default {
409    onShow() {
410        const el = this.$refs.canvas;
411        const ctx = el.getContext('2d');
412        var path = ctx.createPath2D();
413        path.ellipse(200, 200, 50, 100, Math.PI * 0.25, Math.PI * 0.5, Math.PI, 1);
414        ctx.stroke(path);
415    }
416}
417  ```
418
419  ![zh-cn_image_0000001173324787](figures/zh-cn_image_0000001173324787.png)
420
421
422## rect
423
424rect(x: number, y: number, width: number, height: number): void
425
426创建矩形路径。
427
428**参数:**
429
430| 参数     | 类型     | 描述            |
431| ------ | ------ | ------------- |
432| x      | number | 指定矩形的左上角x坐标值。 |
433| y      | number | 指定矩形的左上角y坐标值。 |
434| width  | number | 指定矩形的宽度。      |
435| height | number | 指定矩形的高度。      |
436
437**示例:**
438
439  ```html
440<!-- xxx.hml -->
441<div>
442    <canvas ref="canvas" style="width: 500px; height: 450px; background-color: #ffff00;"></canvas>
443</div>
444  ```
445
446  ```js
447// xxx.js
448export default {
449    onShow() {
450        const el = this.$refs.canvas;
451        const ctx = el.getContext('2d');
452        var path = ctx.createPath2D();
453        path.rect(20, 20, 100, 100);
454        ctx.stroke(path);
455    }
456}
457  ```
458
459  ![zh-cn_image_0000001127125212](figures/zh-cn_image_0000001127125212.png)
460