js-components-canvas-canvasrenderingcontext2d.md 55.4 KB
Newer Older
E
ester.zhou 已提交
1 2
# CanvasRenderingContext2D

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

**CanvasRenderingContext2D** allows you to draw rectangles, text, images, and other objects on a canvas.


10 11
**Example**
  ```html
E
ester.zhou 已提交
12 13 14 15 16 17 18 19
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas1" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
    <input type="button" style="width: 180px; height: 60px;" value="handleClick" onclick="handleClick" />
    <input type="button" style="width: 180px; height: 60px;" value="antialias" onclick="antialias" />
  </div>
  ```

20
  ```js
E
ester.zhou 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
  // xxx.js
  export default {
    handleClick() {
      const el = this.$refs.canvas1;
      const ctx = el.getContext('2d');
      ctx.beginPath();
      ctx.arc(100, 75, 50, 0, 6.28);
      ctx.stroke();
    },
    antialias() {
      const el = this.$refs.canvas1;
      const ctx = el.getContext('2d', { antialias: true });
      ctx.beginPath();   
      ctx.arc(100, 75, 50, 0, 6.28);
      ctx.stroke(); 
Z
zengyawen 已提交
36 37
      }
    }
E
ester.zhou 已提交
38
  ```
Z
zengyawen 已提交
39

E
ester.zhou 已提交
40 41 42 43 44 45 46 47 48 49 50
- Anti-aliasing disabled

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

- Anti-aliasing enabled

  ![screenshot-8](figures/screenshot-8.png)


## Attributes

E
ester.zhou 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| Name                                      | Type                                      | Description                                      |
| ---------------------------------------- | ---------------------------------------- | ---------------------------------------- |
| [fillStyle](#fillstyle)                  | &lt;color&gt; \| [CanvasGradient](../arkui-js/js-components-canvas-canvasgradient.md) \| CanvasPattern | Style to fill an area.<br>- When the type is **\<color>**, this parameter indicates the color of the filling area.<br>- When the type is **CanvasGradient**, this parameter indicates a gradient object, which is created using the **createLinearGradient()** method.<br>- When the type is **CanvasPattern**, this parameter indicates a canvas pattern, which is created using the **createPattern()** method.|
| [lineWidth](#linewidth)                  | number                                   | Line width.                              |
| [strokeStyle](#strokestyle)              | &lt;color&gt; \| [CanvasGradient](../arkui-js/js-components-canvas-canvasgradient.md) \| CanvasPattern | Stroke style.<br>- When the type is **\<color>**, this parameter indicates the stroke color.<br>- When the type is **CanvasGradient**, this parameter indicates a gradient object, which is created using the **createLinearGradient()** method.<br>- When the type is **CanvasPattern**, this parameter indicates a canvas pattern, which is created using the **createPattern()** method.|
| [lineCap](#linecap)                      | string                                   | Style of the specified line endpoint. The options are as follows:<br>- **butt**: The endpoints of the line are squared off.<br>- **round**: The endpoints of the line are rounded.<br>- **square**: The endpoints of the line are squared off, and each endpoint has added a rectangle whose length is the same as the line thickness and whose width is half of the line thickness.<br>Default value: **butt**|
| [lineJoin](#linejoin)                    | string                                   | Style of the intersection point between line segments. The options are as follows:<br>- **round**: The intersection is a sector, whose radius at the rounded corner is equal to the line width.<br>- **bevel**: The intersection is a triangle. The rectangular corner of each line is independent.<br>- **miter**: The intersection has a miter corner by extending the outside edges of the lines until they meet. You can view the effect of this attribute in **miterLimit**.<br>Default value: **miter**|
| [miterLimit](#miterlimit)                | number                                   | Maximum miter length. The miter length is the distance between the inner corner and the outer corner where two lines meet.<br>Default value: **10**  |
| [font](#font)                            | string                                   | Font style.<br>Syntax: ctx.font="font-style font-weight font-size font-family"<sup>5+</sup><br>- (Optional) **font-style**: font style. Available values are **normal** and **italic**.<br>- (Optional) **font-weight**: font weight. Available values are as follows: **normal**, **bold**, **bolder**, **lighter**, **100**, **200**, **300**, **400**, **500**, **600**, **700**, **800**, **900**.<br>- (Optional) **font-size**: font size and row height. The unit can only be pixels.<br>- (Optional) **font-family**: font family. Available values are **sans-serif**, **serif**, and **monospace**.<br>Default value: **"normal normal 14px sans-serif"**|
| [textAlign](#textalign)                  | string                                   | Text alignment mode. Available values are as follows:<br>- **left**: The text is left-aligned.<br>- **right**: The text is right-aligned.<br>- **center**: The text is center-aligned.<br>- **start**: The text is aligned with the start bound.<br>- **end**: The text is aligned with the end bound.<br>In the **ltr** layout mode, the value **start** equals **left**. In the **rtl** layout mode, the value **start** equals **right**.<br>Default value: **left**|
| [textBaseline](#textbaseline)            | string                                   | Horizontal alignment mode of text. Available values are as follows:<br>- **alphabetic**: The text baseline is the normal alphabetic baseline.<br>- **top**: The text baseline is on the top of the text bounding box.<br>- **hanging**: The text baseline is a hanging baseline over the text.<br>- **middle**: The text baseline is in the middle of the text bounding box.<br>- **ideographic**: The text baseline is the ideographic baseline. If a character exceeds the alphabetic baseline, the ideographic baseline is located at the bottom of the excessive character.<br>- **bottom**: The text baseline is at the bottom of the text bounding box. Its difference from the ideographic baseline is that the ideographic baseline does not consider letters in the next line.<br>Default value: **alphabetic**|
| [globalAlpha](#globalalpha)              | number                                   | Opacity.<br>**0.0**: completely transparent.<br>**1.0**: completely opaque.               |
| [lineDashOffset](#linedashoffset)        | number                                   | Offset of the dashed line. The precision is float.<br>Default value: **0.0**         |
| [globalCompositeOperation](#globalcompositeoperation) | string                                   | Composition operation type. Available values are as follows: **source-over**, **source-atop**, **source-in**, **source-out**, **destination-over**, **destination-atop**, **destination-in**, **destination-out**, **lighter**, copy, and **xor**. For details, see [Operation types](#globalcompositeoperation).<br>Default value: **ource-over**|
| [shadowBlur](#shadowblur)                | number                                   | Blur level during shadow drawing. A larger value indicates a more blurred effect. The precision is float.<br>Default value: **0.0**|
| [shadowColor](#shadowcolor)              | &lt;color&gt;                            | Shadow color.                           |
| [shadowOffsetX](#shadowoffsetx)          | number                                   | X-axis shadow offset relative to the original object.                     |
| [shadowOffsetY](#shadowoffsety)          | number                                   | Y-axis shadow offset relative to the original object.                     |
| [imageSmoothingEnabled](#imagesmoothingenabled6)<sup>6+</sup> | boolean                                  | Whether to adjust the image smoothness during image drawing. The value **true** means to enable this feature, and **false** means the opposite.<br>Default value: **true**|
E
ester.zhou 已提交
70 71 72 73


### fillStyle

74
  ```html
Z
zengyawen 已提交
75 76 77 78
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
</div>
E
ester.zhou 已提交
79
  ```
Z
zengyawen 已提交
80

81 82
```js
// xxx.js
Z
zengyawen 已提交
83 84 85 86 87 88 89 90
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.fillStyle = '#0000ff';
    ctx.fillRect(20, 20, 150, 100);
  }
}
Z
zengyawen 已提交
91 92
```

E
ester.zhou 已提交
93 94
![en-us_image_0000001166962736](figures/en-us_image_0000001166962736.png)

Z
zengyawen 已提交
95

E
ester.zhou 已提交
96
### lineWidth
Z
zengyawen 已提交
97

98
```html
Z
zengyawen 已提交
99 100 101 102 103 104
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
</div>
```

105 106
```js
// xxx.js
Z
zengyawen 已提交
107 108 109 110 111 112 113 114
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.lineWidth = 5;
    ctx.strokeRect(25, 25, 85, 105);
  }
}
E
ester.zhou 已提交
115

Z
zengyawen 已提交
116 117
```

E
ester.zhou 已提交
118 119
![en-us_image_0000001166484430](figures/en-us_image_0000001166484430.png)

Z
zengyawen 已提交
120

E
ester.zhou 已提交
121
### strokeStyle
Z
zengyawen 已提交
122

123
```html
Z
zengyawen 已提交
124 125 126 127 128 129
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
</div>
```

130 131
```js
// xxx.js
Z
zengyawen 已提交
132 133 134 135 136 137 138 139 140
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.lineWidth = 10;
    ctx.strokeStyle = '#0000ff';
    ctx.strokeRect(25, 25, 155, 105);
  }
}
Z
zengyawen 已提交
141 142 143
```


E
ester.zhou 已提交
144 145 146
![en-us_image_0000001212124299](figures/en-us_image_0000001212124299.png)

### lineCap
Z
zengyawen 已提交
147

148
```html
Z
zengyawen 已提交
149 150 151 152 153 154
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
</div>
```

155 156
```js
// xxx.js
Z
zengyawen 已提交
157 158 159 160 161 162 163 164 165 166 167 168
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.lineWidth = 8;
    ctx.beginPath();
    ctx.lineCap = 'round';
    ctx.moveTo(30, 50);
    ctx.lineTo(220, 50);
    ctx.stroke();
  }
}
Z
zengyawen 已提交
169 170
```

E
ester.zhou 已提交
171
![en-us_image_0000001214837127](figures/en-us_image_0000001214837127.png)
Z
zengyawen 已提交
172

E
ester.zhou 已提交
173
### lineJoin
Z
zengyawen 已提交
174

175
```html
Z
zengyawen 已提交
176 177 178 179 180 181
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
</div>
```

182 183
```js 
// xxx.js
Z
zengyawen 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.beginPath();
    ctx.lineWidth = 8;
    ctx.lineJoin = 'miter';
    ctx.moveTo(30, 30);
    ctx.lineTo(120, 60);
    ctx.lineTo(30, 110);
    ctx.stroke();
  }
}
Z
zengyawen 已提交
197 198
```

E
ester.zhou 已提交
199
![en-us_image_0000001214717247](figures/en-us_image_0000001214717247.png)
Z
zengyawen 已提交
200

E
ester.zhou 已提交
201
### miterLimit
Z
zengyawen 已提交
202

203
```html
Z
zengyawen 已提交
204 205 206 207 208 209
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 500px; height: 500px; "></canvas>
</div>
```

210 211
```js
// xxx.js
Z
zengyawen 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.lineWidth =14;
    ctx.lineJoin = 'miter';
    ctx.miterLimit = 3;
    ctx.moveTo(30, 30);
    ctx.lineTo(120, 60);
    ctx.lineTo(30, 70);
    ctx.stroke();
  }
}
Z
zengyawen 已提交
225 226
```

E
ester.zhou 已提交
227 228
![en-us_image_0000001167001464](figures/en-us_image_0000001167001464.png)

Z
zengyawen 已提交
229

E
ester.zhou 已提交
230
### font
Z
zengyawen 已提交
231

232
```html
Z
zengyawen 已提交
233 234 235 236 237 238
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
</div>
```

239 240
```js
// xxx.js
Z
zengyawen 已提交
241 242 243 244 245 246 247 248
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.font = '30px sans-serif';
    ctx.fillText("Hello World", 20, 60);
  }
}
Z
zengyawen 已提交
249 250
```

E
ester.zhou 已提交
251
![en-us_image_0000001167046832](figures/en-us_image_0000001167046832.png)
Z
zengyawen 已提交
252

E
ester.zhou 已提交
253 254

### textAlign
Z
zengyawen 已提交
255

256
```html
Z
zengyawen 已提交
257 258 259 260 261
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
</div>
```
Z
zengyawen 已提交
262

263 264
```js
// xxx.js
Z
zengyawen 已提交
265 266 267 268 269 270 271 272
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.strokeStyle = '#0000ff';
    ctx.moveTo(140, 10);
    ctx.lineTo(140, 160);
    ctx.stroke();
E
ester.zhou 已提交
273 274 275 276 277 278 279
    ctx.font = '18px sans-serif'; 
    // Show the different textAlign values
    ctx.textAlign = 'start'; 
    ctx.fillText('textAlign=start', 140, 60);
    ctx.textAlign = 'end';
    ctx.fillText('textAlign=end', 140, 80);
    ctx.textAlign = 'left'; 
Z
zengyawen 已提交
280
    ctx.fillText('textAlign=left', 140, 100);
E
ester.zhou 已提交
281 282 283
    ctx.textAlign = 'center'; 
    ctx.fillText('textAlign=center',140, 120);
    ctx.textAlign = 'right';
Z
zengyawen 已提交
284 285 286
    ctx.fillText('textAlign=right',140, 140);
  }
}
E
ester.zhou 已提交
287

Z
zengyawen 已提交
288 289 290
```


E
ester.zhou 已提交
291 292 293
![en-us_image_0000001167472798](figures/en-us_image_0000001167472798.png)

### textBaseline
Z
zengyawen 已提交
294

295
```html
Z
zengyawen 已提交
296 297 298 299 300
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 500px; height: 500px; "></canvas>
</div>
```
Z
zengyawen 已提交
301

302 303
```js
// xxx.js
Z
zengyawen 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.strokeStyle = '#0000ff';
    ctx.moveTo(0, 120);
    ctx.lineTo(400, 120);
    ctx.stroke();
    ctx.font = '20px sans-serif';
    ctx.textBaseline = 'top'; 
    ctx.fillText('Top', 10, 120); 
    ctx.textBaseline = 'bottom'; 
    ctx.fillText('Bottom', 55, 120); 
    ctx.textBaseline = 'middle'; 
    ctx.fillText('Middle', 125, 120); 
    ctx.textBaseline = 'alphabetic'; 
    ctx.fillText('Alphabetic', 195, 120); 
    ctx.textBaseline = 'hanging'; 
    ctx.fillText('Hanging', 295, 120);
  }
}
Z
zengyawen 已提交
325 326
```

E
ester.zhou 已提交
327
![en-us_image_0000001169315920](figures/en-us_image_0000001169315920.png)
Z
zengyawen 已提交
328

E
ester.zhou 已提交
329
### globalAlpha
Z
zengyawen 已提交
330

331
```html
Z
zengyawen 已提交
332 333 334 335 336 337
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
</div>
```

338 339
```js
// xxx.js
Z
zengyawen 已提交
340 341 342 343 344 345 346 347 348
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.fillStyle = 'rgb(255,0,0)';
    ctx.fillRect(0, 0, 50, 50);
    ctx.globalAlpha = 0.4;
    ctx.fillStyle = 'rgb(0,0,255)'; 
    ctx.fillRect(50, 50, 50, 50);
E
ester.zhou 已提交
349

Z
zengyawen 已提交
350 351
  }
}
Z
zengyawen 已提交
352 353
```

E
ester.zhou 已提交
354
![en-us_image_0000001167953648](figures/en-us_image_0000001167953648.png)
Z
zengyawen 已提交
355

E
ester.zhou 已提交
356
### lineDashOffset
Z
zengyawen 已提交
357

358
```html
Z
zengyawen 已提交
359 360 361 362 363 364
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
</div>
```

365 366
```js
// xxx.js
Z
zengyawen 已提交
367 368 369 370 371 372 373 374 375 376
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.arc(100, 75, 50, 0, 6.28);
    ctx.setLineDash([10,20]);
    ctx.lineDashOffset = 10.0;
    ctx.stroke();
  }
}
Z
zengyawen 已提交
377 378
```

E
ester.zhou 已提交
379 380 381 382
![en-us_image_0000001167950468](figures/en-us_image_0000001167950468.png)

### globalCompositeOperation

E
ester.zhou 已提交
383 384
Enumerates the operation types.

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
| Value               | Description                      |
| ---------------- | ------------------------ |
| source-over      | Displays the new drawing above the existing drawing. This attribute is used by default.  |
| source-atop      | Displays the new drawing on the top of the existing drawing.       |
| source-in        | Displays the new drawing inside the existing drawing.        |
| source-out       | Displays part of the new drawing that is outside of the existing drawing.       |
| destination-over | Displays the existing drawing above the new drawing.       |
| destination-atop | Displays the existing drawing on the top of the new drawing.       |
| destination-in   | Displays the existing drawing inside the new drawing.        |
| destination-out  | Displays the existing drawing outside the new drawing.        |
| lighter          | Displays both the new and existing drawing.         |
| copy             | Displays the new drawing and neglects the existing drawing.       |
| xor              | Combines the new drawing and existing drawing using the XOR operation.|

**Example**

```html
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
</div>
E
ester.zhou 已提交
406 407
```

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
  ```js
// xxx.js
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.fillStyle = 'rgb(255,0,0)';
    ctx.fillRect(20, 20, 50, 50);
    ctx.globalCompositeOperation = 'source-over';
    ctx.fillStyle = 'rgb(0,0,255)';
    ctx.fillRect(50, 50, 50, 50);
    // Start drawing second example
    ctx.fillStyle = 'rgb(255,0,0)';
    ctx.fillRect(120, 20, 50, 50);
    ctx.globalCompositeOperation = 'destination-over';
    ctx.fillStyle = 'rgb(0,0,255)';
    ctx.fillRect(150, 50, 50, 50);
E
ester.zhou 已提交
425
  }
426
}
E
ester.zhou 已提交
427
  ```
Z
zengyawen 已提交
428

E
ester.zhou 已提交
429
  ![en-us_image_0000001213192781](figures/en-us_image_0000001213192781.png)
Z
zengyawen 已提交
430

E
ester.zhou 已提交
431
  In the above example, the blue rectangle represents the new drawing, and the red rectangle represents the existing drawing.
Z
zengyawen 已提交
432

E
ester.zhou 已提交
433
### shadowBlur
Z
zengyawen 已提交
434

435
  ```html
Z
zengyawen 已提交
436 437 438 439
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
</div>
E
ester.zhou 已提交
440
  ```
Z
zengyawen 已提交
441

442 443
```js
// xxx.js
Z
zengyawen 已提交
444 445 446 447 448 449 450 451 452 453
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.shadowBlur = 30;
    ctx.shadowColor = 'rgb(0,0,0)';
    ctx.fillStyle = 'rgb(255,0,0)';
    ctx.fillRect(20, 20, 100, 80);
  }
}
Z
zengyawen 已提交
454 455
```

E
ester.zhou 已提交
456
![en-us_image_0000001168111514](figures/en-us_image_0000001168111514.png)
Z
zengyawen 已提交
457

E
ester.zhou 已提交
458
### shadowColor
Z
zengyawen 已提交
459

460
```html
Z
zengyawen 已提交
461 462 463 464 465 466
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px;"></canvas>
</div>
```

467 468
```js
// xxx.js
Z
zengyawen 已提交
469 470 471 472 473 474 475 476 477 478
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.shadowBlur = 30;
    ctx.shadowColor = 'rgb(0,0,255)';
    ctx.fillStyle = 'rgb(255,0,0)';
    ctx.fillRect(30, 30, 100, 100);
  }
}
Z
zengyawen 已提交
479 480
```

E
ester.zhou 已提交
481
![en-us_image_0000001168111610](figures/en-us_image_0000001168111610.png)
Z
zengyawen 已提交
482

E
ester.zhou 已提交
483
### shadowOffsetX
Z
zengyawen 已提交
484

485
```html
Z
zengyawen 已提交
486 487 488 489 490 491
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px;"></canvas>
</div>
```

492 493
```js
// xxx.js
Z
zengyawen 已提交
494 495 496 497 498 499 500 501 502 503 504
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.shadowBlur = 10;
    ctx.shadowOffsetX = 20;
    ctx.shadowColor = 'rgb(0,0,0)';
    ctx.fillStyle = 'rgb(255,0,0)';
    ctx.fillRect(20, 20, 100, 80);
  }
}
Z
zengyawen 已提交
505 506 507
```


E
ester.zhou 已提交
508 509 510
![en-us_image_0000001167631876](figures/en-us_image_0000001167631876.png)

### shadowOffsetY
Z
zengyawen 已提交
511

512
```html
Z
zengyawen 已提交
513 514 515 516 517 518
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
</div>
```

519 520
```js
// xxx.js
Z
zengyawen 已提交
521 522 523 524 525 526 527 528 529 530 531
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    ctx.shadowBlur = 10;
    ctx.shadowOffsetY = 20;
    ctx.shadowColor = 'rgb(0,0,0)';
    ctx.fillStyle = 'rgb(255,0,0)';
    ctx.fillRect(30, 30, 100, 100);
 }
}
Z
zengyawen 已提交
532 533
```

E
ester.zhou 已提交
534
![en-us_image_0000001213193285](figures/en-us_image_0000001213193285.png)
Z
zengyawen 已提交
535

E
ester.zhou 已提交
536
### imageSmoothingEnabled<sup>6+</sup>
Z
zengyawen 已提交
537

538
```html
Z
zengyawen 已提交
539 540 541 542 543 544
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
</div>
```

545 546
```js
// xxx.js
Z
zengyawen 已提交
547 548 549 550 551 552 553 554 555 556 557 558
export default {
  onShow() {
    const el =this.$refs.canvas;
    const ctx = el.getContext('2d');
    var img = new Image();
    img.src = 'common/image/example.jpg';
    img.onload = function() {
    ctx.imageSmoothingEnabled = false;
    ctx.drawImage(img, 0, 0, 400, 200);
    };
  }
}
Z
zengyawen 已提交
559 560
```

E
ester.zhou 已提交
561 562 563 564 565
![smoothoff](figures/smoothoff.png)



## Methods
Z
zengyawen 已提交
566 567


E
ester.zhou 已提交
568
### fillRect
Z
zengyawen 已提交
569

E
ester.zhou 已提交
570
fillRect(x: number, y: number, width:number, height: number): void
Z
zengyawen 已提交
571 572 573

Fills a rectangle on the canvas.

574
**Parameters**
E
ester.zhou 已提交
575

576 577 578 579 580 581
| Name    | 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.     |
E
ester.zhou 已提交
582

583 584 585
**Example**

```html
E
ester.zhou 已提交
586 587
  <!-- xxx.hml -->
  <div>
E
ester.zhou 已提交
588
    <canvas ref="canvas" style="width: 500px; height: 500px; "></canvas>
E
ester.zhou 已提交
589 590 591
  </div>
```

592
  ```js
E
ester.zhou 已提交
593 594 595 596 597 598
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.fillRect(20, 20, 200, 150);
Z
zengyawen 已提交
599
    }
E
ester.zhou 已提交
600 601
  }
  ```
Z
zengyawen 已提交
602

E
ester.zhou 已提交
603
  ![en-us_image_0000001214811029](figures/en-us_image_0000001214811029.png)
Z
zengyawen 已提交
604

E
ester.zhou 已提交
605
### clearRect
Z
zengyawen 已提交
606

E
ester.zhou 已提交
607
clearRect(x: number, y: number, width:number, height: number): void
Z
zengyawen 已提交
608 609 610

Clears the content in a rectangle on the canvas.

611
**Parameters**
E
ester.zhou 已提交
612

613 614 615 616 617 618
| Name    | 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.     |
E
ester.zhou 已提交
619

620 621
**Example**
  ```html
E
ester.zhou 已提交
622 623
  <!-- xxx.hml -->
  <div>
E
ester.zhou 已提交
624
    <canvas ref="canvas" style="width: 500px; height: 500px;"></canvas>
E
ester.zhou 已提交
625 626 627
  </div>
  ```

628
  ```js
E
ester.zhou 已提交
629 630 631 632 633 634 635 636
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.fillStyle = 'rgb(0,0,255)';
      ctx.fillRect(0, 0, 400, 200);
      ctx.clearRect(20, 20, 150, 100);
Z
zengyawen 已提交
637
    }
E
ester.zhou 已提交
638 639
  }
  ```
Z
zengyawen 已提交
640

E
ester.zhou 已提交
641
  ![en-us_image_0000001214619417](figures/en-us_image_0000001214619417.png)
Z
zengyawen 已提交
642 643


E
ester.zhou 已提交
644
### strokeRect
Z
zengyawen 已提交
645

E
ester.zhou 已提交
646
strokeRect(x: number, y: number, width:number, height: number): void
Z
zengyawen 已提交
647 648 649

Draws a rectangle stroke on the canvas.

650
**Parameters**
E
ester.zhou 已提交
651

652 653 654 655 656 657
| Name    | Type    | Description          |
| ------ | ------ | ------------ |
| x      | number | X-coordinate of the upper left corner of the rectangle stroke.|
| y      | number | Y-coordinate of the upper left corner of the rectangle stroke.|
| width  | number | Width of the rectangle.    |
| height | number | Height of the rectangle.    |
E
ester.zhou 已提交
658

659 660
**Example**
  ```html
E
ester.zhou 已提交
661 662 663 664 665 666
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
  </div>
  ```

667
  ```js
E
ester.zhou 已提交
668 669 670 671 672 673
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.strokeRect(30, 30, 200, 150);
Z
zengyawen 已提交
674
    }
E
ester.zhou 已提交
675 676
  }
  ```
Z
zengyawen 已提交
677

E
ester.zhou 已提交
678
  ![en-us_image_0000001214822091](figures/en-us_image_0000001214822091.png)
Z
zengyawen 已提交
679 680


E
ester.zhou 已提交
681
### fillText
Z
zengyawen 已提交
682

E
ester.zhou 已提交
683
fillText(text: string, x: number, y: number): void
Z
zengyawen 已提交
684 685 686

Draws filled text on the canvas.

687
**Parameters**
E
ester.zhou 已提交
688

689 690 691 692 693
| Name  | Type    | Description             |
| ---- | ------ | --------------- |
| text | string | Text to draw.     |
| x    | number | X-coordinate of the lower left corner of the text.|
| y    | number | Y-coordinate of the lower left corner of the text.|
E
ester.zhou 已提交
694

695 696
**Example**
  ```html
E
ester.zhou 已提交
697 698 699 700 701 702
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
  </div>
  ```

703
  ```js
E
ester.zhou 已提交
704 705 706 707 708 709 710
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.font = '35px sans-serif';
      ctx.fillText("Hello World!", 10, 60);
Z
zengyawen 已提交
711
    }
E
ester.zhou 已提交
712 713
  }
  ```
Z
zengyawen 已提交
714

E
ester.zhou 已提交
715
  ![en-us_image_0000001214469787](figures/en-us_image_0000001214469787.png)
Z
zengyawen 已提交
716

E
ester.zhou 已提交
717
### strokeText
Z
zengyawen 已提交
718

E
ester.zhou 已提交
719
strokeText(text: string, x: number, y: number): void
Z
zengyawen 已提交
720 721 722

Draws a text stroke on the canvas.

723
**Parameters**
E
ester.zhou 已提交
724

725 726 727 728 729
| Name  | Type    | Description             |
| ---- | ------ | --------------- |
| text | string | Text to draw.     |
| x    | number | X-coordinate of the lower left corner of the text.|
| y    | number | Y-coordinate of the lower left corner of the text.|
E
ester.zhou 已提交
730

731 732
**Example**
  ```html
E
ester.zhou 已提交
733 734 735 736 737 738
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
  </div>
  ```

739
  ```js
E
ester.zhou 已提交
740 741 742 743 744 745 746
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.font = '25px sans-serif';
      ctx.strokeText("Hello World!", 10, 60);
Z
zengyawen 已提交
747
    }
E
ester.zhou 已提交
748 749 750 751 752 753 754 755 756 757 758
  }
  ```

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

### measureText

measureText(text: string): TextMetrics

Returns a **TextMetrics** object used to obtain the width of specified text.

759
**Parameters**
E
ester.zhou 已提交
760

761 762 763
| Name  | Type    | Description        |
| ---- | ------ | ---------- |
| text | string | Text to be measured.|
E
ester.zhou 已提交
764

765
**Return value**
E
ester.zhou 已提交
766

767 768 769
| Type         | Description                                    |
| ----------- | -------------------------------------- |
| TextMetrics | Object that contains the text width. You can obtain the width by **TextMetrics.width**.|
E
ester.zhou 已提交
770

771 772
**Example**
  ```html
E
ester.zhou 已提交
773 774 775 776 777 778
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px;"></canvas>
  </div>
  ```

779
  ```js
E
ester.zhou 已提交
780 781 782 783 784 785 786 787 788
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.font = '20px sans-serif';
      var txt = 'Hello World';
      ctx.fillText("width:" + ctx.measureText(txt).width, 20, 60);
      ctx.fillText(txt, 20, 110);
Z
zengyawen 已提交
789
    }
E
ester.zhou 已提交
790 791
  }
  ```
Z
zengyawen 已提交
792

E
ester.zhou 已提交
793
  ![en-us_image_0000001169142476](figures/en-us_image_0000001169142476.png)
Z
zengyawen 已提交
794 795


E
ester.zhou 已提交
796 797
### stroke
stroke(): void
Z
zengyawen 已提交
798 799 800

Draws a stroke.

801 802
**Example**
  ```html
E
ester.zhou 已提交
803 804 805 806 807 808
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
  </div>
  ```

809
  ```js
E
ester.zhou 已提交
810 811 812 813 814 815 816 817 818 819
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.moveTo(25, 25);
      ctx.lineTo(25, 250);
      ctx.lineWidth = '6';
      ctx.strokeStyle = 'rgb(0,0,255)';
      ctx.stroke();
Z
zengyawen 已提交
820
    }
E
ester.zhou 已提交
821 822
  }
  ```
Z
zengyawen 已提交
823

E
ester.zhou 已提交
824
  ![en-us_image_0000001236697937](figures/en-us_image_0000001236697937.png)
Z
zengyawen 已提交
825 826


E
ester.zhou 已提交
827 828
### beginPath
beginPath(): void
Z
zengyawen 已提交
829 830 831

Creates a drawing path.

832 833
**Example**
  ```html
E
ester.zhou 已提交
834 835 836 837 838 839
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 500px; height: 500px; "></canvas>
  </div>
  ```

840
  ```js
E
ester.zhou 已提交
841 842 843 844 845 846 847 848 849 850 851
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.beginPath();              
      ctx.lineWidth = '6';
      ctx.strokeStyle = '#0000ff';
      ctx.moveTo(15, 80); 
      ctx.lineTo(280, 80);
      ctx.stroke();
Z
zengyawen 已提交
852
    }
E
ester.zhou 已提交
853 854
  }
  ```
Z
zengyawen 已提交
855

E
ester.zhou 已提交
856
  ![en-us_image_0000001214629745](figures/en-us_image_0000001214629745.png)
Z
zengyawen 已提交
857 858


E
ester.zhou 已提交
859 860
### moveTo
moveTo(x: number, y: number): void
Z
zengyawen 已提交
861 862 863

Moves a drawing path to a target position on the canvas.

864
**Parameters**
E
ester.zhou 已提交
865

866 867 868 869
| Name  | Type    | Description       |
| ---- | ------ | --------- |
| x    | number | X-coordinate of the target position.|
| y    | number | Y-coordinate of the target position.|
E
ester.zhou 已提交
870

871 872
**Example**
  ```html
E
ester.zhou 已提交
873 874 875 876 877 878
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
  </div>
  ```

879
  ```js
E
ester.zhou 已提交
880 881 882 883 884 885 886 887 888
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.beginPath();
      ctx.moveTo(10, 10);
      ctx.lineTo(280, 160);
      ctx.stroke();
Z
zengyawen 已提交
889
    }
E
ester.zhou 已提交
890 891
  }
  ```
Z
zengyawen 已提交
892

E
ester.zhou 已提交
893
  ![en-us_image_0000001169309948](figures/en-us_image_0000001169309948.png)
Z
zengyawen 已提交
894 895


E
ester.zhou 已提交
896 897
### lineTo
lineTo(x: number, y: number): void
Z
zengyawen 已提交
898 899 900

Connects the current point to a target position using a straight line.

901
**Parameters**
E
ester.zhou 已提交
902

903 904 905 906
| Name  | Type    | Description       |
| ---- | ------ | --------- |
| x    | number | X-coordinate of the target position.|
| y    | number | Y-coordinate of the target position.|
E
ester.zhou 已提交
907

908 909
**Example**
  ```html
E
ester.zhou 已提交
910 911 912 913 914 915
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
  </div>
  ```

916
  ```js
E
ester.zhou 已提交
917 918 919 920 921 922 923 924 925
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.beginPath();
      ctx.moveTo(10, 10);
      ctx.lineTo(280, 160);
      ctx.stroke();
Z
zengyawen 已提交
926
    }
E
ester.zhou 已提交
927 928
  }
  ```
Z
zengyawen 已提交
929

E
ester.zhou 已提交
930
  ![en-us_image_0000001169469914](figures/en-us_image_0000001169469914.png)
Z
zengyawen 已提交
931 932


E
ester.zhou 已提交
933 934
### closePath
closePath(): void
Z
zengyawen 已提交
935 936 937

Draws a closed path.

938 939
**Example**
  ```html
E
ester.zhou 已提交
940 941 942 943 944 945
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px;"></canvas>
  </div>
  ```

946
  ```js
E
ester.zhou 已提交
947 948 949 950 951 952 953 954 955 956 957
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.beginPath();
      ctx.moveTo(30, 30);
      ctx.lineTo(110, 30);
      ctx.lineTo(70, 90);
      ctx.closePath();
      ctx.stroke();
Z
zengyawen 已提交
958
    }
E
ester.zhou 已提交
959 960
  }
  ```
Z
zengyawen 已提交
961

E
ester.zhou 已提交
962
  ![en-us_image_0000001169151508](figures/en-us_image_0000001169151508.png)
Z
zengyawen 已提交
963

E
ester.zhou 已提交
964
### createPattern
Z
zengyawen 已提交
965

E
ester.zhou 已提交
966
createPattern(image: Image, repetition: string): Object
Z
zengyawen 已提交
967 968 969

Creates a pattern for image filling based on a specified source image and repetition mode.

970
**Parameters**
E
ester.zhou 已提交
971

972 973 974 975
| Name        | Type    | Description                                      |
| ---------- | ------ | ---------------------------------------- |
| image      | Image  | Source image. For details, see [Image](../arkui-js/js-components-canvas-image.md).|
| repetition | string | Repetition mode. The value can be **"repeat"**, **"repeat-x"**, **"repeat-y"**, or **"no-repeat"**.|
E
ester.zhou 已提交
976

977
**Return value**
E
ester.zhou 已提交
978

979 980 981
| Type    | Description               |
| ------ | ----------------- |
| Object | Pattern of image filling.|
E
ester.zhou 已提交
982

983 984
**Example**
  ```html
E
ester.zhou 已提交
985 986
  <!-- xxx.hml -->
  <div>
E
ester.zhou 已提交
987
    <canvas ref="canvas" style="width: 1000px; height: 1000px;"></canvas>
E
ester.zhou 已提交
988 989 990
  </div>
  ```

991
  ```js
E
ester.zhou 已提交
992 993 994 995 996 997 998 999 1000
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      var img = new Image();
      img.src = 'common/images/example.jpg';
      var pat = ctx.createPattern(img, 'repeat');
      ctx.fillStyle = pat;
E
ester.zhou 已提交
1001
      ctx.fillRect(0, 0, 500, 500);
Z
zengyawen 已提交
1002
    }
E
ester.zhou 已提交
1003 1004
  }
  ```
Z
zengyawen 已提交
1005

E
ester.zhou 已提交
1006
  ![en-us_image_0000001169301188](figures/en-us_image_0000001169301188.png)
Z
zengyawen 已提交
1007

E
ester.zhou 已提交
1008
### bezierCurveTo
Z
zengyawen 已提交
1009

E
ester.zhou 已提交
1010
bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void
Z
zengyawen 已提交
1011 1012 1013

Draws a cubic bezier curve on the canvas.

1014
**Parameters**
E
ester.zhou 已提交
1015

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
| Name  | 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**
  ```html
E
ester.zhou 已提交
1027 1028 1029 1030 1031 1032
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
  </div>
  ```

1033
  ```js
E
ester.zhou 已提交
1034 1035 1036 1037 1038 1039 1040 1041 1042
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.beginPath();
      ctx.moveTo(10, 10);
      ctx.bezierCurveTo(20, 100, 200, 100, 200, 20);
      ctx.stroke();
Z
zengyawen 已提交
1043
    }
E
ester.zhou 已提交
1044 1045
  }
  ```
Z
zengyawen 已提交
1046

E
ester.zhou 已提交
1047
  ![en-us_image_0000001214621177](figures/en-us_image_0000001214621177.png)
Z
zengyawen 已提交
1048

E
ester.zhou 已提交
1049
### quadraticCurveTo
Z
zengyawen 已提交
1050

E
ester.zhou 已提交
1051
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void
Z
zengyawen 已提交
1052 1053 1054

Draws a quadratic curve on the canvas.

1055
**Parameters**
E
ester.zhou 已提交
1056

1057 1058 1059 1060 1061 1062
| Name  | 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.|
E
ester.zhou 已提交
1063

1064 1065
**Example**
  ```html
E
ester.zhou 已提交
1066 1067 1068 1069 1070 1071
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
  </div>
  ```

1072
  ```js
E
ester.zhou 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.beginPath();
      ctx.moveTo(20, 20);
      ctx.quadraticCurveTo(100, 100, 200, 20);
      ctx.stroke();
Z
zengyawen 已提交
1082
    }
E
ester.zhou 已提交
1083 1084
  }
  ```
Z
zengyawen 已提交
1085

E
ester.zhou 已提交
1086
  ![en-us_image_0000001169461910](figures/en-us_image_0000001169461910.png)
Z
zengyawen 已提交
1087 1088


E
ester.zhou 已提交
1089 1090
### arc
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise: boolean): void
Z
zengyawen 已提交
1091 1092 1093

Draws an arc on the canvas.

1094
**Parameters**
E
ester.zhou 已提交
1095

1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
| Name           | 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**
  ```html
E
ester.zhou 已提交
1107 1108 1109 1110 1111 1112
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px;"></canvas>
  </div>
  ```

1113
  ```js
E
ester.zhou 已提交
1114 1115 1116 1117 1118 1119 1120 1121
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.beginPath();
      ctx.arc(100, 75, 50, 0, 6.28);
      ctx.stroke();
Z
zengyawen 已提交
1122
    }
E
ester.zhou 已提交
1123 1124
  }
  ```
Z
zengyawen 已提交
1125

E
ester.zhou 已提交
1126
  ![en-us_image_0000001169470288](figures/en-us_image_0000001169470288.png)
Z
zengyawen 已提交
1127

E
ester.zhou 已提交
1128
### arcTo
Z
zengyawen 已提交
1129

E
ester.zhou 已提交
1130
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void
Z
zengyawen 已提交
1131 1132 1133

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

1134
**Parameters**
E
ester.zhou 已提交
1135

1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
| Name    | 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**
  ```html
E
ester.zhou 已提交
1146 1147 1148 1149 1150 1151
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px;"></canvas>
  </div>
  ```

1152
  ```js
E
ester.zhou 已提交
1153 1154 1155 1156 1157 1158
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.moveTo(100, 20);
1159
      ctx.arcTo(150, 20, 150, 70, 50); // Create an arc
E
ester.zhou 已提交
1160
      ctx.stroke();
Z
zengyawen 已提交
1161
    }
E
ester.zhou 已提交
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
  }
  ```

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

### ellipse<sup>6+</sup>

ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise: number): void

Draws an ellipse in the specified rectangular region on the canvas.

1173
**Parameters**
E
ester.zhou 已提交
1174

1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
| Name           | 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 unit is radian.                   |
| anticlockwise | number | Whether to draw the ellipse counterclockwise. The value **0** means clockwise, and **1** means counterclockwise. This parameter is optional. The default value is **0**.|

**Example**
  ```html
E
ester.zhou 已提交
1188 1189 1190 1191 1192 1193
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
  </div>
  ```

1194
  ```js
E
ester.zhou 已提交
1195 1196 1197 1198 1199 1200 1201 1202
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.beginPath();
      ctx.ellipse(200, 200, 50, 100, Math.PI * 0.25, Math.PI * 0.5, Math.PI, 1);
      ctx.stroke();
Z
zengyawen 已提交
1203
    }
E
ester.zhou 已提交
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
  }
  ```

  ![ellipse](figures/ellipse.png)


### rect
rect(x: number, y: number, width: number, height: number): void

Creates a rectangle on the canvas.

1215
**Parameters**
E
ester.zhou 已提交
1216

1217 1218 1219 1220 1221 1222
| Name    | 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.     |
E
ester.zhou 已提交
1223

1224 1225
**Example**
  ```html
E
ester.zhou 已提交
1226 1227 1228 1229 1230 1231
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
  </div>
  ```

1232
  ```js
E
ester.zhou 已提交
1233 1234 1235 1236 1237
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
1238
      ctx.rect(20, 20, 100, 100); // Create a 100*100 rectangle at (20, 20)
E
ester.zhou 已提交
1239
      ctx.stroke(); // Draw it
Z
zengyawen 已提交
1240
    }
E
ester.zhou 已提交
1241 1242
  }
  ```
Z
zengyawen 已提交
1243

E
ester.zhou 已提交
1244
  ![en-us_image_0000001214630783](figures/en-us_image_0000001214630783.png)
Z
zengyawen 已提交
1245

E
ester.zhou 已提交
1246
### fill
Z
zengyawen 已提交
1247

E
ester.zhou 已提交
1248
fill(): void
Z
zengyawen 已提交
1249

E
ester.zhou 已提交
1250
Fills the area inside a closed path on the canvas.
Z
zengyawen 已提交
1251

1252 1253
**Example**
  ```html
E
ester.zhou 已提交
1254 1255 1256 1257 1258
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px;"></canvas>
  </div>
  ```
Z
zengyawen 已提交
1259

1260
  ```js
E
ester.zhou 已提交
1261 1262 1263 1264 1265
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
1266 1267
      ctx.rect(20, 20, 100, 100); // Create a 100*100 rectangle at (20, 20)
      ctx.fill(); // Draw it in default setting
Z
zengyawen 已提交
1268
    }
E
ester.zhou 已提交
1269 1270
  }
  ```
Z
zengyawen 已提交
1271

E
ester.zhou 已提交
1272
  ![en-us_image_0000001214703717](figures/en-us_image_0000001214703717.png)
Z
zengyawen 已提交
1273

E
ester.zhou 已提交
1274
### clip
Z
zengyawen 已提交
1275

E
ester.zhou 已提交
1276
clip(): void
Z
zengyawen 已提交
1277 1278 1279

Sets the current path to a clipping path.

1280 1281
**Example**
  ```html
E
ester.zhou 已提交
1282 1283 1284 1285 1286 1287
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px;"></canvas>
  </div>
  ```

1288
  ```js
E
ester.zhou 已提交
1289 1290 1291 1292 1293 1294 1295 1296
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.rect(0, 0, 200, 200);
      ctx.stroke();
      ctx.clip();
1297
      // Draw red rectangle after clip
E
ester.zhou 已提交
1298 1299
      ctx.fillStyle = "rgb(255,0,0)";
      ctx.fillRect(0, 0, 150, 150);
Z
zengyawen 已提交
1300
    }
E
ester.zhou 已提交
1301 1302
  }
  ```
Z
zengyawen 已提交
1303

E
ester.zhou 已提交
1304
  ![en-us_image_0000001169303414](figures/en-us_image_0000001169303414.png)
Z
zengyawen 已提交
1305

E
ester.zhou 已提交
1306
### rotate
Z
zengyawen 已提交
1307

E
ester.zhou 已提交
1308
rotate(rotate: number): void
Z
zengyawen 已提交
1309 1310 1311

Rotates a canvas clockwise around its coordinate axes.

1312
**Parameters**
E
ester.zhou 已提交
1313

1314 1315 1316
| Name    | Type    | Description                                      |
| ------ | ------ | ---------------------------------------- |
| rotate | number | Clockwise rotation angle. You can use **Math.PI / 180** to convert the angle to a radian.|
E
ester.zhou 已提交
1317

1318 1319
**Example**
  ```html
E
ester.zhou 已提交
1320 1321 1322 1323 1324 1325
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px;"></canvas>
  </div>
  ```

1326
  ```js
E
ester.zhou 已提交
1327 1328 1329 1330 1331 1332 1333
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.rotate(45 * Math.PI / 180); // Rotate the rectangle 45 degrees
      ctx.fillRect(70, 20, 50, 50);
Z
zengyawen 已提交
1334
    }
E
ester.zhou 已提交
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
  }
  ```

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

### scale

scale(x: number, y: number): void

Scales the canvas based on scale factors.

1346
**Parameters**
E
ester.zhou 已提交
1347

1348 1349 1350 1351
| Name  | Type    | Description         |
| ---- | ------ | ----------- |
| x    | number | Horizontal scale factor.|
| y    | number | Vertical scale factor.|
E
ester.zhou 已提交
1352

1353 1354
**Example**
  ```html
E
ester.zhou 已提交
1355 1356 1357 1358 1359 1360
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px;"></canvas>
  </div>
  ```

1361
  ```js
E
ester.zhou 已提交
1362 1363 1364 1365 1366 1367
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.strokeRect(10, 10, 25, 25);
1368
      ctx.scale(2, 2);// Scale to 200%
E
ester.zhou 已提交
1369
      ctx.strokeRect(10, 10, 25, 25);
Z
zengyawen 已提交
1370
    }
E
ester.zhou 已提交
1371 1372
  }
  ```
Z
zengyawen 已提交
1373

E
ester.zhou 已提交
1374
  ![en-us_image_0000001214463281](figures/en-us_image_0000001214463281.png)
Z
zengyawen 已提交
1375

E
ester.zhou 已提交
1376
### transform
Z
zengyawen 已提交
1377

E
ester.zhou 已提交
1378
transform(scaleX: number, skewX: number, skewY: number, scale: number, translateX: number, translateY: number): void
Z
zengyawen 已提交
1379 1380 1381

Defines a transformation matrix. To transform a graph, you only need to set parameters of the matrix. The coordinates of the graph are multiplied by the matrix values to obtain new coordinates of the transformed graph. You can use the matrix to implement multiple transform effects.

1382 1383
>  **NOTE**<br>
>  The following formulas calculate coordinates of the transformed graph. **x** and **y** represent coordinates before transformation, and **x'** and **y'** represent coordinates after transformation.
E
ester.zhou 已提交
1384
>
1385
>  - x' = scaleX \* x + skewY \* y + translateX
E
ester.zhou 已提交
1386
>
1387 1388 1389
>  - y' = skewX \* x + scaleY \* y + translateY

**Parameters**
E
ester.zhou 已提交
1390

1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
| Name        | Type    | Description      |
| ---------- | ------ | -------- |
| scaleX     | number | X-axis scale.|
| skewX      | number | X-axis skew.|
| skewY      | number | Y-axis skew.|
| scaleY     | number | Y-axis scale.|
| translateX | number | X-axis translation.|
| translateY | number | Y-axis translation.|

**Example**
  ```html
E
ester.zhou 已提交
1402 1403 1404 1405 1406 1407
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px;"></canvas>
  </div>
  ```

1408
  ```js
E
ester.zhou 已提交
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.fillStyle = 'rgb(0,0,0)';
      ctx.fillRect(0, 0, 100, 100)
      ctx.transform(1, 0.5, -0.5, 1, 10, 10);
      ctx.fillStyle = 'rgb(255,0,0)';
      ctx.fillRect(0, 0, 100, 100);
      ctx.transform(1, 0.5, -0.5, 1, 10, 10);
      ctx.fillStyle = 'rgb(0,0,255)';
      ctx.fillRect(0, 0, 100, 100);
Z
zengyawen 已提交
1422
    }
E
ester.zhou 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431
  }
  ```

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

### setTransform

setTransform(scaleX: number, skewX: number, skewY: number, scale: number, translateX: number, translateY: number): void

E
ester.zhou 已提交
1432
Resets the existing transformation matrix and creates a new transformation matrix by using the same parameters as the **transform()** API.
E
ester.zhou 已提交
1433

1434
**Parameters**
E
ester.zhou 已提交
1435

1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
| Name        | Type    | Description      |
| ---------- | ------ | -------- |
| scaleX     | number | X-axis scale.|
| skewX      | number | X-axis skew.|
| skewY      | number | Y-axis skew.|
| scaleY     | number | Y-axis scale.|
| translateX | number | X-axis translation.|
| translateY | number | Y-axis translation.|

**Example**
  ```html
E
ester.zhou 已提交
1447 1448 1449 1450 1451 1452
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px;"></canvas>
  </div>
  ```

1453
  ```js
E
ester.zhou 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.fillStyle = 'rgb(255,0,0)';
      ctx.fillRect(0, 0, 100, 100)
      ctx.setTransform(1,0.5, -0.5, 1, 10, 10);
      ctx.fillStyle = 'rgb(0,0,255)';
      ctx.fillRect(0, 0, 100, 100);
Z
zengyawen 已提交
1464
    }
E
ester.zhou 已提交
1465 1466
  }
  ```
Z
zengyawen 已提交
1467

E
ester.zhou 已提交
1468
  ![en-us_image_0000001168984880](figures/en-us_image_0000001168984880.png)
Z
zengyawen 已提交
1469

E
ester.zhou 已提交
1470
### translate
Z
zengyawen 已提交
1471

E
ester.zhou 已提交
1472
translate(x: number, y: number): void
Z
zengyawen 已提交
1473 1474 1475

Moves the origin of the coordinate system.

1476
**Parameters**
E
ester.zhou 已提交
1477

1478 1479 1480 1481
| Name  | Type    | Description      |
| ---- | ------ | -------- |
| x    | number | X-axis translation.|
| y    | number | Y-axis translation.|
E
ester.zhou 已提交
1482

1483 1484
**Example**
  ```html
E
ester.zhou 已提交
1485 1486 1487 1488 1489 1490
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px;"></canvas>
  </div>
  ```

1491
  ```js
E
ester.zhou 已提交
1492 1493 1494 1495 1496 1497 1498 1499
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.fillRect(10, 10, 50, 50);
      ctx.translate(70, 70);
      ctx.fillRect(10, 10, 50, 50);
Z
zengyawen 已提交
1500
    }
E
ester.zhou 已提交
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
  }
  ```

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

### createPath2D<sup>6+</sup>

createPath2D(path: Path2D, cmds: string): Path2D

Creates a **Path2D** object.

1512
**Parameters**
E
ester.zhou 已提交
1513

1514 1515 1516 1517
| Name  | Type    | Description            |
| ---- | ------ | -------------- |
| path | Path2D | **Path2D** object.     |
| cmds | string | Path description of the SVG image.|
E
ester.zhou 已提交
1518

1519
**Return value**
E
ester.zhou 已提交
1520

E
ester.zhou 已提交
1521
  [Path2D object](../arkui-js/js-components-canvas-path2d.md)
1522 1523 1524

**Example**
  ```html
E
ester.zhou 已提交
1525 1526 1527 1528 1529 1530
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
  </div>
  ```

1531
  ```js
E
ester.zhou 已提交
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      var path1 = ctx.createPath2D();
      path1.moveTo(100, 100);
      path1.lineTo(200, 100);
      path1.lineTo(100, 200);
      path1.closePath();
      ctx.stroke(path1);
      var path2 = ctx.createPath2D("M150 150 L50 250 L250 250 Z");
      ctx.stroke(path2);
      var path3 = ctx.createPath2D(path2);
      ctx.stroke(path3);
Z
zengyawen 已提交
1547
    }
E
ester.zhou 已提交
1548 1549 1550 1551 1552 1553 1554
  }
  ```

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

### drawImage

1555
drawImage(image: Image | PixelMap, sx: number, sy: number, sWidth: number, sHeight: number, dx: number, dy: number, dWidth: number, dHeight: number):void
E
ester.zhou 已提交
1556 1557 1558

Draws an image on the canvas.

1559
**Parameters**
E
ester.zhou 已提交
1560

1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
| Name     | Type                            | Description                                      |
| ------- | ------------------------------ | ---------------------------------------- |
| image   | Image \| PixelMap<sup>9+</sup> | Image resource. For details, see [Image](../arkui-js/js-components-canvas-image.md) or [PixelMap](../apis/js-apis-image.md#pixelmap7).|
| sx      | number                         | X-coordinate of the upper left corner of the rectangle used to crop the source image.                    |
| sy      | number                         | Y-coordinate of the upper left corner of the rectangle used to crop the source image.                    |
| sWidth  | number                         | Target width to crop the source image.                          |
| sHeight | number                         | Target height to crop the source image.                          |
| dx      | number                         | X-coordinate of the upper left corner of the drawing area on the canvas.                          |
| dy      | number                         | Y-coordinate of the upper left corner of the drawing area on the canvas.                    |
| dWidth  | number                         | Width of the drawing area.                                |
| dHeight | number                         | Height of the drawing area.                                |

**Example**
  ```html
E
ester.zhou 已提交
1575 1576
  <!-- xxx.hml -->
  <div>
E
ester.zhou 已提交
1577
    <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
E
ester.zhou 已提交
1578 1579 1580
  </div>
  ```

1581
  ```js
E
ester.zhou 已提交
1582 1583 1584
  //xxx.js
  export default {
    onShow() {
E
ester.zhou 已提交
1585
      var test = this.$refs.canvas;
E
ester.zhou 已提交
1586 1587 1588
      var ctx = test.getContext('2d');
      var img = new Image();
      img.src = 'common/image/test.jpg';
E
ester.zhou 已提交
1589
      ctx.drawImage(img, 0, 0, 200, 200, 10, 10, 200, 200);
Z
zengyawen 已提交
1590
    }
E
ester.zhou 已提交
1591 1592
  }
  ```
Z
zengyawen 已提交
1593

E
ester.zhou 已提交
1594
  ![en-us_image_0000001214704759](figures/en-us_image_0000001214704759.png)
Z
zengyawen 已提交
1595

E
ester.zhou 已提交
1596
### restore
Z
zengyawen 已提交
1597

E
ester.zhou 已提交
1598
restore(): void
Z
zengyawen 已提交
1599 1600 1601

Restores the saved drawing context.

1602 1603
**Example**
  ```html
E
ester.zhou 已提交
1604 1605 1606 1607 1608 1609
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
  </div>
  ```

1610
  ```js
E
ester.zhou 已提交
1611 1612 1613 1614 1615 1616
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.restore();
Z
zengyawen 已提交
1617
    }
E
ester.zhou 已提交
1618 1619
  }
  ```
Z
zengyawen 已提交
1620

E
ester.zhou 已提交
1621
### save
Z
zengyawen 已提交
1622

E
ester.zhou 已提交
1623
save(): void
Z
zengyawen 已提交
1624 1625 1626

Saves the current drawing context.

1627 1628
**Example**
  ```html
E
ester.zhou 已提交
1629 1630 1631 1632 1633 1634
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
  </div>
  ```

1635
  ```js
E
ester.zhou 已提交
1636 1637 1638 1639 1640 1641
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.save();
Z
zengyawen 已提交
1642
    }
E
ester.zhou 已提交
1643 1644 1645 1646 1647 1648 1649 1650 1651
  }
  ```

### createLinearGradient<sup>6+</sup>

createLinearGradient(x0: number, y0: number, x1: number, y1: number): Object

Creates a linear gradient and returns a **CanvasGradient** object. For details, see [CanvasGradient](../arkui-js/js-components-canvas-canvasgradient.md).

1652
**Parameters**
E
ester.zhou 已提交
1653

1654 1655 1656 1657 1658 1659 1660 1661
| Name  | Type    | Description      |
| ---- | ------ | -------- |
| x0   | number | X-coordinate of the start point.|
| y0   | number | Y-coordinate of the start point.|
| x1   | number | X-coordinate of the end point.|
| y1   | number | Y-coordinate of the end point.|

**Return value**
E
ester.zhou 已提交
1662

1663 1664 1665 1666 1667 1668
| Type    | Description                    |
| ------ | ---------------------- |
| Object | Created **CanvasGradient** object.|

**Example**
  ```html
E
ester.zhou 已提交
1669 1670 1671 1672 1673 1674 1675
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
    <input type="button" style="width: 180px; height: 60px;" value="fillStyle" onclick="handleClick" />
  </div>
  ```

1676
  ```js
E
ester.zhou 已提交
1677 1678 1679 1680 1681 1682 1683 1684
  // xxx.js
  export default {
    handleClick() {
      const el = this.$refs.canvas;
      const ctx = el.getContext('2d');
      // Linear gradient: start(50,0) end(300,100)
      var gradient = ctx.createLinearGradient(50,0, 300,100);
      // Add three color stops
E
ester.zhou 已提交
1685 1686 1687
      gradient.addColorStop(0.0, '#ff0000');
      gradient.addColorStop(0.5, '#ffffff');
      gradient.addColorStop(1.0, '#00ff00');
E
ester.zhou 已提交
1688 1689 1690
      // Set the fill style and draw a rectangle
      ctx.fillStyle = gradient;
      ctx.fillRect(0, 0, 500, 500);
Z
zengyawen 已提交
1691
    }
E
ester.zhou 已提交
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
  }
  ```

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

### createRadialGradient<sup>6+</sup>

createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number): Object

Creates a radial gradient and returns a **CanvasGradient** object.

1703
**Parameters**
E
ester.zhou 已提交
1704

1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
| Name  | Type    | Description               |
| ---- | ------ | ----------------- |
| x0   | number | X-coordinate of the center of the start circle.        |
| y0   | number | Y-coordinate of the center of the start circle.        |
| r0   | number | Radius of the start circle, which must be a non-negative finite number.|
| x1   | number | X-coordinate of the center of the end circle.        |
| y1   | number | Y-coordinate of the center of the end circle.        |
| r1   | number | Radius of the end circle, which must be a non-negative finite number.|

**Return value**
E
ester.zhou 已提交
1715

1716 1717 1718 1719 1720 1721
| Type    | Description                    |
| ------ | ---------------------- |
| Object | Created **CanvasGradient** object.|

**Example**
  ```html
E
ester.zhou 已提交
1722 1723 1724 1725 1726 1727 1728
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
    <input type="button" style="width: 180px; height: 60px;" value="fillStyle" onclick="handleClick" />
  </div>
  ```

1729
  ```js
E
ester.zhou 已提交
1730 1731 1732 1733 1734 1735 1736 1737
  // xxx.js
  export default {
    handleClick() {
      const el = this.$refs.canvas;
      const ctx = el.getContext('2d');
      // Radial gradient: inner circle(200,200,r:50) outer circle(200,200,r:200)
      var gradient = ctx.createRadialGradient(200,200,50, 200,200,200);
      // Add three color stops
E
ester.zhou 已提交
1738 1739 1740
      gradient.addColorStop(0.0, '#ff0000');
      gradient.addColorStop(0.5, '#ffffff');
      gradient.addColorStop(1.0, '#00ff00');
E
ester.zhou 已提交
1741 1742 1743
      // Set the fill style and draw a rectangle
      ctx.fillStyle = gradient;
      ctx.fillRect(0, 0, 500, 500);
Z
zengyawen 已提交
1744
    }
E
ester.zhou 已提交
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
  }
  ```

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

### createImageData

createImageData(width: number, height: number, imageData: Object): Object

Creates an **ImageData** object. For details, see [ImageData](../arkui-js/js-components-canvas-imagedata.md).

1756
**Parameters**
E
ester.zhou 已提交
1757

1758 1759 1760 1761 1762
| Name       | Type    | Description               |
| --------- | ------ | ----------------- |
| width     | number | Width of the **ImageData** object.    |
| height    | number | Height of the **ImageData** object.    |
| imagedata | Object | **ImageData** object with the same width and height copied from the original **ImageData** object.|
E
ester.zhou 已提交
1763

1764
**Return value**
E
ester.zhou 已提交
1765

1766 1767 1768
| Type    | Description               |
| ------ | ----------------- |
| Object | Created **ImageData** object.|
E
ester.zhou 已提交
1769

1770 1771
**Example**
  ```html
E
ester.zhou 已提交
1772 1773 1774 1775 1776 1777
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
  </div>
  ```

1778
  ```js
E
ester.zhou 已提交
1779 1780 1781 1782 1783 1784 1785
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      var imageData = ctx.createImageData(50, 100);  // Create ImageData with 50px width and 100px height
      var newImageData = ctx.createImageData(imageData);  // Create ImageData using the input imageData
Z
zengyawen 已提交
1786
    }
E
ester.zhou 已提交
1787 1788 1789 1790 1791 1792 1793
  }
  ```

### getImageData

getImageData(sx: number, sy: number, sw: number, sh: number): Object

1794
Obtains the **ImageData** object created with the pixels within the specified area on the canvas.
E
ester.zhou 已提交
1795

1796
**Parameters**
E
ester.zhou 已提交
1797

1798 1799 1800 1801 1802 1803
| Name  | Type    | Description             |
| ---- | ------ | --------------- |
| sx   | number | X-coordinate of the upper left corner of the output area.|
| sy   | number | Y-coordinate of the upper left corner of the output area.|
| sw   | number | Width of the output area.    |
| sh   | number | Height of the output area.    |
E
ester.zhou 已提交
1804

1805
**Return value**
E
ester.zhou 已提交
1806

1807 1808 1809
| Type    | Description                     |
| ------ | ----------------------- |
| Object | **ImageData** object that contains pixels in the specified area on the canvas.|
E
ester.zhou 已提交
1810

1811 1812
**Example**
  ```html
E
ester.zhou 已提交
1813 1814
  <!-- xxx.hml -->
  <div>
E
ester.zhou 已提交
1815
    <canvas id="getImageData" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
E
ester.zhou 已提交
1816 1817 1818
  </div>
  ```

1819
  ```js
E
ester.zhou 已提交
1820 1821 1822 1823 1824 1825
  //xxx.js
  export default {
    onShow() {
      const test = this.$element('getImageData')
      const ctx = test.getContext('2d');
      var imageData = ctx.getImageData(0, 0, 280, 300);
Z
zengyawen 已提交
1826
    }
E
ester.zhou 已提交
1827 1828 1829 1830 1831 1832 1833 1834 1835
  }
  ```

### putImageData

putImageData(imageData: Object, dx: number, dy: number, dirtyX: number, dirtyY: number, dirtyWidth: number, dirtyHeight: number): void

Puts the **ImageData** onto a rectangular area on the canvas.

1836
**Parameters**
E
ester.zhou 已提交
1837

1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849
| Name         | Type    | Description                           |
| ----------- | ------ | ----------------------------- |
| imagedata   | Object | **ImageData** object with pixels to put onto the canvas.           |
| dx          | number | X-axis offset of the rectangular area on the canvas.               |
| dy          | number | Y-axis offset of the rectangular area on the canvas.               |
| dirtyX      | number | X-axis offset of the upper left corner of the rectangular area relative to that of the source image.|
| dirtyY      | number | Y-axis offset of the upper left corner of the rectangular area relative to that of the source image.|
| dirtyWidth  | number | Width of the rectangular area to crop the source image.              |
| dirtyHeight | number | Height of the rectangular area to crop the source image.              |

**Example**
  ```html
E
ester.zhou 已提交
1850 1851
  <!-- xxx.hml -->
  <div>
E
ester.zhou 已提交
1852
    <canvas id="getImageData" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
E
ester.zhou 已提交
1853 1854 1855
  </div>
  ```

1856
  ```js
E
ester.zhou 已提交
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
  //xxx.js
  export default {
    onShow() {
      const test = this.$element('getImageData')
      const ctx = test.getContext('2d');
      var imgData = ctx.createImageData(100, 100);
      for (var i = 0; i < imgData.data.length; i += 4) {
        imgData.data[i + 0] = 255;
        imgData.data[i + 1] = 0;
        imgData.data[i + 2] = 0;
        imgData.data[i + 3] = 255;
Z
zengyawen 已提交
1868
    }
E
ester.zhou 已提交
1869 1870 1871 1872
      ctx.putImageData(imgData, 10, 10);
    }
  }
  ```
Z
zengyawen 已提交
1873

E
ester.zhou 已提交
1874
  ![en-us_image_0000001214463283](figures/en-us_image_0000001214463283.png)
Z
zengyawen 已提交
1875

1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
### getPixelMap<sup>9+</sup>

getPixelMap(sx: number, sy: number, sw: number, sh: number): PixelMap

Obtains the **PixelMap** object created with the pixels within the specified area on the canvas.

**Parameters**

| Name  | Type    | Description          |
| ---- | ------ | ------------ |
| sx   | number | X-coordinate of the upper left corner of the specified area.|
| sy   | number | Y-coordinate of the upper left corner of the specified area.|
| sw   | number | Width of the specified area.    |
| sh   | number | Height of the specified area.    |

**Return value**

| Type                                      | Description                    |
| ---------------------------------------- | ---------------------- |
| [PixelMap](../apis/js-apis-image.md#pixelmap7) | **PixelMap** object that contains pixels in the specified area on the canvas.|

**Example**

  ```html
  <!-- xxx.hml -->
  <div>
    <canvas id="canvasId" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
  </div>
  ```

  ```js
  //xxx.js
  export default {
    onShow() {
      const test = this.$element('canvasId')
      const ctx = test.getContext('2d');
      var pixelMap = ctx.getPixelMap(0, 0, 280, 300);
    }
  }
  ```

E
ester.zhou 已提交
1917
### setLineDash
Z
zengyawen 已提交
1918

E
ester.zhou 已提交
1919
setLineDash(segments: Array): void
Z
zengyawen 已提交
1920 1921 1922

Sets the dash line style.

1923
**Parameters**
E
ester.zhou 已提交
1924

1925 1926 1927
| Name      | Type   | Description                  |
| -------- | ----- | -------------------- |
| segments | Array | An array describing the interval of alternate line segments and length of spacing.|
E
ester.zhou 已提交
1928

1929 1930
**Example**
  ```html
E
ester.zhou 已提交
1931 1932 1933 1934 1935 1936
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
  </div>
  ```

1937
  ```js
E
ester.zhou 已提交
1938 1939 1940 1941 1942 1943 1944 1945
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      ctx.arc(100, 75, 50, 0, 6.28);
      ctx.setLineDash([10,20]);
      ctx.stroke();
Z
zengyawen 已提交
1946
    }
E
ester.zhou 已提交
1947 1948
  }
  ```
Z
zengyawen 已提交
1949

E
ester.zhou 已提交
1950
  ![en-us_image_0000001214623229](figures/en-us_image_0000001214623229.png)
Z
zengyawen 已提交
1951

E
ester.zhou 已提交
1952
### getLineDash
Z
zengyawen 已提交
1953

E
ester.zhou 已提交
1954
getLineDash(): Array
Z
zengyawen 已提交
1955 1956 1957

Obtains the dash line style.

1958
**Return value**
E
ester.zhou 已提交
1959

1960 1961 1962
| Type   | Description                      |
| ----- | ------------------------ |
| Array | An array describing the interval of alternate line segments and length of spacing.|
E
ester.zhou 已提交
1963

1964 1965
**Example**
  ```html
E
ester.zhou 已提交
1966 1967 1968 1969 1970 1971
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 200px; height: 150px; "></canvas>
  </div>
  ```

1972
  ```js
E
ester.zhou 已提交
1973 1974 1975 1976 1977 1978
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      var info = ctx.getLineDash();
Z
zengyawen 已提交
1979
    }
E
ester.zhou 已提交
1980 1981 1982 1983 1984 1985 1986 1987 1988
  }
  ```

### transferFromImageBitmap<sup>7+</sup>

transferFromImageBitmap(bitmap: ImageBitmap): void

Displays the specified **ImageBitmap** object.

1989
**Parameters**
E
ester.zhou 已提交
1990

1991 1992 1993
| Name    | Type         | Description                |
| ------ | ----------- | ------------------ |
| bitmap | ImageBitmap | **ImageBitmap** object to display.|
E
ester.zhou 已提交
1994

1995 1996
**Example**
  ```html
E
ester.zhou 已提交
1997 1998 1999 2000 2001 2002
  <!-- xxx.hml -->
  <div>
    <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
  </div>
  ```

2003
  ```js
E
ester.zhou 已提交
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
  //xxx.js
  export default {
    onShow() {
      const el =this.$refs.canvas;
      const ctx = el.getContext('2d');
      var canvas = this.$refs.canvas.getContext('2d');
      var offscreen = new OffscreenCanvas(500,500);
      var offscreenCanvasCtx = offscreen.getContext("2d");
      offscreenCanvasCtx.fillRect(0, 0, 200, 200); 

      var bitmap = offscreen.transferToImageBitmap();
      canvas.transferFromImageBitmap(bitmap);
Z
zengyawen 已提交
2016
    }
E
ester.zhou 已提交
2017 2018
  }
  ```
Z
zengyawen 已提交
2019

E
ester.zhou 已提交
2020
  ![en-us_image_0000001168984882](figures/en-us_image_0000001168984882.png)