js-components-canvas-path2d.md 12.2 KB
Newer Older
E
ester.zhou 已提交
1
# Path2D
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3 4 5
>  **NOTE**
>
>  This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

E
ester.zhou 已提交
7
**Path2D** allows you to describe a path through an existing path. This path can be drawn through the **stroke** API of **Canvas**.
Z
zengyawen 已提交
8

E
ester.zhou 已提交
9 10 11
## addPath

addPath(path: Object): void
Z
zengyawen 已提交
12 13 14

Adds a path to this path.

E
ester.zhou 已提交
15 16 17 18 19 20 21
**Parameters**

| Name | Type   | Description                    |
| ---- | ------ | ------------------------------ |
| path | Object | Path to be added to this path. |

**Example**
E
ester.zhou 已提交
22

E
ester.zhou 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  ```html
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
</div>
  ```

  ```js
// xxx.js
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    var path1 = ctx.createPath2D("M250 150 L150 350 L350 350 Z");
    var path2 = ctx.createPath2D();
    path2.addPath(path1);
    ctx.stroke(path2);
  }
}
  ```

  ![en-us_image_0000001173164873](figures/en-us_image_0000001173164873.png)

## setTransform

setTransform(scaleX: number, skewX: number, skewY: number, scaleY: number, translateX: number, translateY: number): void
Z
zengyawen 已提交
49

Z
zengyawen 已提交
50
Sets the path transformation matrix.
Z
zengyawen 已提交
51

E
ester.zhou 已提交
52 53 54 55 56 57 58 59 60 61 62 63
**Parameters**

| Name       | Type   | Description                         |
| ---------- | ------ | ----------------------------------- |
| scaleX     | number | Scale ratio of the x-axis.          |
| skewX      | number | Skew angle of the x-axis.           |
| skewY      | number | Skew angle of the y-axis.           |
| scaleY     | number | Scale ratio of the y-axis.          |
| translateX | number | Translation distance of the x-axis. |
| translateY | number | Translation distance of the y-axis. |

**Example**
E
ester.zhou 已提交
64

E
ester.zhou 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  ```html
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
</div>
  ```

  ```js
// xxx.js
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    var path = ctx.createPath2D("M250 150 L150 350 L350 350 Z");
    path.setTransform(0.8, 0, 0, 0.4, 0, 0);
    ctx.stroke(path);
  }
}
  ```

  ![en-us_image_0000001127125208](figures/en-us_image_0000001127125208.png)


## closePath

closePath(): void
Z
zengyawen 已提交
91 92 93

Moves 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 is closed or has only one point, this method does not perform any action.

E
ester.zhou 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
**Example**
  ```html
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
</div>
  ```

  ```js
// xxx.js
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    var path = ctx.createPath2D();
    path.moveTo(200, 100);
    path.lineTo(300, 100);
    path.lineTo(200, 200);
    path.closePath();
    ctx.stroke(path);
  }
}
  ```

![](figures/en-us_image_0000001127125202.png)


## moveTo

moveTo(x: number, y: number): void
Z
zengyawen 已提交
124

E
ester.zhou 已提交
125
Moves the current coordinate point of the path to the target point, without drawing a line during the movement.
Z
zengyawen 已提交
126

E
ester.zhou 已提交
127
**Parameters**
Z
zengyawen 已提交
128

E
ester.zhou 已提交
129 130 131 132
| Parameter | Type   | Description                       |
| --------- | ------ | --------------------------------- |
| x         | number | X-coordinate of the target point. |
| y         | number | Y-coordinate of the target point. |
Z
zengyawen 已提交
133

E
ester.zhou 已提交
134
**Example**
E
ester.zhou 已提交
135

E
ester.zhou 已提交
136 137 138 139 140 141
  ```html
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 300px; height: 250px; background-color: #ffff00;"></canvas>
</div>
  ```
Z
zengyawen 已提交
142

E
ester.zhou 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  ```js
// xxx.js
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    var path = ctx.createPath2D();
    path.moveTo(50, 100);
    path.lineTo(250, 100);
    path.lineTo(150, 200);
    path.closePath();
    ctx.stroke(path);
  }
}
  ```
Z
zengyawen 已提交
158

E
ester.zhou 已提交
159
  ![en-us_image_0000001173164869](figures/en-us_image_0000001173164869.png)
Z
zengyawen 已提交
160 161


E
ester.zhou 已提交
162 163 164
## lineTo

lineTo(x: number, y: number): void
Z
zengyawen 已提交
165 166 167

Draws a straight line from the current point to the target point.

E
ester.zhou 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
**Parameters**

| Parameter | Type   | Description                       |
| --------- | ------ | --------------------------------- |
| x         | number | X-coordinate of the target point. |
| y         | number | Y-coordinate of the target point. |

**Example**

  ```html
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 400px; height: 450px; background-color: #ffff00;"></canvas>
</div>
  ```

  ```js
// xxx.js
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    var path = ctx.createPath2D();
    path.moveTo(100, 100);
    path.lineTo(100, 200);
    path.lineTo(200, 200);
    path.lineTo(200, 100);
    path.closePath();
    ctx.stroke(path);
  }
}
  ```

  ![en-us_image_0000001127285024](figures/en-us_image_0000001127285024.png)


## bezierCurveTo

bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void
Z
zengyawen 已提交
207 208 209

Draws a cubic bezier curve on the canvas.

E
ester.zhou 已提交
210 211 212 213 214 215 216 217 218 219 220 221
**Parameters**

| Parameter | Type   | Description                                               |
| --------- | ------ | --------------------------------------------------------- |
| cp1x      | number | X-coordinate of the first parameter of the bezier curve.  |
| cp1y      | number | Y-coordinate of the first parameter of the bezier curve.  |
| cp2x      | number | X-coordinate of the second parameter of the bezier curve. |
| cp2y      | number | Y-coordinate of the second parameter of the bezier curve  |
| x         | number | X-coordinate of the end point on the bezier curve.        |
| y         | number | Y-coordinate of the end point on the bezier curve.        |

**Example**
E
ester.zhou 已提交
222

E
ester.zhou 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
  ```html
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
</div>
  ```

  ```js
// xxx.js
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    var path = ctx.createPath2D();
    path.moveTo(10, 10);
    path.bezierCurveTo(20, 100, 200, 100, 200, 20);
    ctx.stroke(path);
  }
}
  ```

  ![en-us_image_0000001173324783](figures/en-us_image_0000001173324783.png)


## quadraticCurveTo

quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void
Z
zengyawen 已提交
250 251 252

Draws a quadratic curve on the canvas.

E
ester.zhou 已提交
253 254 255 256 257 258 259 260 261 262
**Parameters**

| Parameter | Type   | Description                                        |
| --------- | ------ | -------------------------------------------------- |
| cpx       | number | X-coordinate of the bezier curve parameter.        |
| cpy       | number | Y-coordinate of the bezier curve parameter.        |
| x         | number | X-coordinate of the end point on the bezier curve. |
| y         | number | Y-coordinate of the end point on the bezier curve. |

**Example**
E
ester.zhou 已提交
263

E
ester.zhou 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  ```html
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
</div>
  ```

  ```js
// xxx.js
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    var path = ctx.createPath2D();
    path.moveTo(10, 10);
    path.quadraticCurveTo(100, 100, 200, 20);
    ctx.stroke(path);
  }
}
  ```

  ![en-us_image_0000001173164871](figures/en-us_image_0000001173164871.png)


## arc

arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise: number): void
Z
zengyawen 已提交
291 292 293

Draws an arc on the canvas.

E
ester.zhou 已提交
294 295 296 297 298 299 300 301 302 303 304 305
**Parameters**

| Parameter     | Type    | Description                                  |
| ------------- | ------- | -------------------------------------------- |
| x             | number  | X-coordinate of the center point of the arc. |
| y             | number  | Y-coordinate of the center point of the arc. |
| radius        | number  | Radius of the arc.                           |
| startAngle    | number  | Start radian of the arc.                     |
| endAngle      | number  | End radian of the arc.                       |
| anticlockwise | boolean | Whether to draw the arc counterclockwise.    |

**Example**
E
ester.zhou 已提交
306

E
ester.zhou 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
  ```html
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
</div>
  ```

  ```js
// xxx.js
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    var path = ctx.createPath2D();
    path.arc(100, 75, 50, 0, 6.28);
    ctx.stroke(path);
  }
}
  ```

  ![en-us_image_0000001173164867](figures/en-us_image_0000001173164867.png)


## arcTo

arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void
Z
zengyawen 已提交
333 334 335

Draws an arc based on the radius and points on the arc.

E
ester.zhou 已提交
336 337 338 339 340 341 342 343 344 345 346
**Parameters**

| Parameter | Type   | Description                                  |
| --------- | ------ | -------------------------------------------- |
| x1        | number | X-coordinate of the first point on the arc.  |
| y1        | number | Y-coordinate of the first point on the arc.  |
| x2        | number | X-coordinate of the second point on the arc. |
| y2        | number | Y-coordinate of the second point on the arc. |
| radius    | number | Radius of the arc.                           |

**Example**
E
ester.zhou 已提交
347

E
ester.zhou 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  ```html
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
</div>
  ```

  ```js
// xxx.js
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    var path = ctx.createPath2D();
    path.arcTo(150, 20, 150, 70, 50);
    ctx.stroke(path);
  }
}
  ```

  ![en-us_image_0000001127125204](figures/en-us_image_0000001127125204.png)


## ellipse

ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise: number): void
Z
zengyawen 已提交
374 375 376

Draws an ellipse in the specified rectangular region.

E
ester.zhou 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390
**Parameters**

| Parameter     | Type   | Description                                                  |
| ------------- | ------ | ------------------------------------------------------------ |
| x             | number | X-coordinate of the ellipse center.                          |
| y             | number | Y-coordinate of the ellipse center.                          |
| radiusX       | number | Ellipse radius on the x-axis.                                |
| radiusY       | number | Ellipse radius on the y-axis.                                |
| rotation      | number | Rotation angle of the ellipse. The unit is radian.           |
| startAngle    | number | Angle of the start point for drawing the ellipse. The unit is radian. |
| endAngle      | number | Angle of the end point for drawing the ellipse. The angle is represented by radians. |
| anticlockwise | number | Whether to draw the ellipse in the anticlockwise direction. The value **0** indicates clockwise and the value **1** indicates anticlockwise. This parameter is optional. The default value is **0**. |

**Example**
E
ester.zhou 已提交
391

E
ester.zhou 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
  ```html
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 500px; height: 450px; background-color: #ffff00;"></canvas>
</div>
  ```

  ```js
// xxx.js
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx =el.getContext('2d');
    var path = ctx.createPath2D();
    path.ellipse(200, 200, 50, 100, Math.PI * 0.25, Math.PI * 0.5, Math.PI, 1);
    ctx.stroke(path);
  }
}
  ```

  ![en-us_image_0000001173324787](figures/en-us_image_0000001173324787.png)


## rect

rect(x: number, y: number, width: number, height: number): void
Z
zengyawen 已提交
418 419 420

Creates a rectangle.

E
ester.zhou 已提交
421 422 423 424 425 426 427 428 429 430
**Parameters**

| Parameter | Type   | Description                                             |
| --------- | ------ | ------------------------------------------------------- |
| x         | number | X-coordinate of the upper left corner of the rectangle. |
| y         | number | Y-coordinate of the upper left corner of the rectangle. |
| width     | number | Width of the rectangle.                                 |
| height    | number | Height of the rectangle.                                |

**Example**
E
ester.zhou 已提交
431

E
ester.zhou 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
  ```html
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 500px; height: 450px; background-color: #ffff00;"></canvas>
</div>
  ```

  ```js
// xxx.js
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    var path = ctx.createPath2D();
    path.rect(20, 20, 100, 100);
    ctx.stroke(path);
  }
}
  ```

  ![en-us_image_0000001127125212](figures/en-us_image_0000001127125212.png)
新手
引导
客服 返回
顶部