ts-offscreencanvasrenderingcontext2d.md 99.3 KB
Newer Older
E
ester.zhou 已提交
1
# OffscreenCanvasRenderingContext2D
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
Use **OffscreenCanvasRenderingContext2D** to draw rectangles, images, and text offscreen onto a canvas. Drawing offscreen onto a canvas is a process where content to draw onto the canvas is first drawn in the buffer, and then converted into a picture, and finally the picture is drawn on the canvas. This process increases the drawing efficiency.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5
>  **NOTE**
E
esterzhou 已提交
6
> 
E
ester.zhou 已提交
7
>  The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9


E
esterzhou 已提交
10

Z
zengyawen 已提交
11 12 13 14
## APIs

OffscreenCanvasRenderingContext2D(width: number, height: number, setting: RenderingContextSettings)

E
ester.zhou 已提交
15 16
**Parameters**

E
esterzhou 已提交
17 18 19 20 21
| Name | Type                                                    | Mandatory| Description                            |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------ |
| width   | number                                                       | Yes  | Width of the offscreen canvas.                      |
| height  | number                                                       | Yes  | Height of the offscreen canvas.                      |
| setting | [RenderingContextSettings](ts-canvasrenderingcontext2d.md#renderingcontextsettings) | Yes  | See RenderingContextSettings.|
Z
zengyawen 已提交
22 23 24 25


## Attributes

E
esterzhou 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| Name                                      | Type                                      | Description                                      |
| ---------------------------------------- | ---------------------------------------- | ---------------------------------------- |
| [fillStyle](#fillstyle)                  | string \| [CanvasGradient](ts-components-canvas-canvasgradient.md) \| [CanvasPattern](#canvaspattern) | Style to fill an area.<br>- When the type is **string**, this attribute indicates the color of the filling area.<br>- When the type is **CanvasGradient**, this attribute indicates a gradient object, which is created using the **[createLinearGradient](#createlineargradient)** API.<br>- When the type is **CanvasPattern**, this attribute indicates a pattern, which is created using the **[createPattern](#createpattern)** API.|
| [lineWidth](#linewidth)                  | number                                   | Line width.                              |
| [strokeStyle](#strokestyle)              | string \| [CanvasGradient](ts-components-canvas-canvasgradient.md) \| [CanvasPattern](#canvaspattern) | Stroke style.<br>- When the type is **\<color>**, this parameter indicates the stroke color.<br>- When the type is **CanvasGradient**, this attribute indicates a gradient object, which is created using the **[createLinearGradient](#createlineargradient)** API.<br>- When the type is **CanvasPattern**, this attribute indicates a pattern, which is created using the **[createPattern](#createpattern)** API.|
| [lineCap](#linecap)                      | CanvasLineCap                            | Style of the line endpoints. 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)                    | CanvasLineJoin                           | Style of the shape used to join 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-size font-family'<br>- (Optional) **font-size**: font size and row height. The unit can only be pixels.<br>(Optional) **font-family**: font family.<br>Syntax: ctx.font='font-style font-weight font-size font-family'<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)                  | CanvasTextAlign                          | 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>> **NOTE**<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)            | CanvasTextBaseline                       | 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 excess 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'**.<br>- Default value: **'source-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)              | string                                   | 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](#imagesmoothingenabled) | 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
esterzhou 已提交
45

46
> **NOTE**
E
ester.zhou 已提交
47 48
>
> For **fillStyle**, **shadowColor**, and **strokeStyle**, the value format of the string type is 'rgb(255, 255, 255)', 'rgba(255, 255, 255, 1.0)', '\#FFFFFF'.
Z
zengyawen 已提交
49 50 51 52


### fillStyle

53 54
```ts
// xxx.ets
Z
zengyawen 已提交
55 56 57
@Entry
@Component
struct FillStyleExample {
E
ester.zhou 已提交
58
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.fillStyle = '#0000ff'
          this.offContext.fillRect(20, 160, 150, 100)
          var image = this.offContext.transferToImageBitmap();
          this.context.transferFromImageBitmap(image);
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
81 82 83 84
![en-us_image_0000001211898510](figures/en-us_image_0000001211898510.png)


### lineWidth
Z
zengyawen 已提交
85

86 87
```ts
// xxx.ets
Z
zengyawen 已提交
88 89 90
@Entry
@Component
struct LineWidthExample {
E
ester.zhou 已提交
91
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.lineWidth = 5
          this.offContext.strokeRect(25, 25, 85, 105)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
114 115 116 117
![en-us_image_0000001257058439](figures/en-us_image_0000001257058439.png)


### strokeStyle
Z
zengyawen 已提交
118

119 120
```ts
// xxx.ets
Z
zengyawen 已提交
121 122 123
@Entry
@Component
struct StrokeStyleExample {
E
ester.zhou 已提交
124
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.lineWidth = 10
          this.offContext.strokeStyle = '#0000ff'
          this.offContext.strokeRect(25, 25, 155, 105)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
148 149 150 151 152
![en-us_image_0000001257058429](figures/en-us_image_0000001257058429.png)


### lineCap

153 154
```ts
// xxx.ets
Z
zengyawen 已提交
155 156 157
@Entry
@Component
struct LineCapExample {
E
ester.zhou 已提交
158
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.lineWidth = 8
          this.offContext.beginPath()
          this.offContext.lineCap = 'round'
          this.offContext.moveTo(30, 50)
          this.offContext.lineTo(220, 50)
          this.offContext.stroke()
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
185 186 187 188
![en-us_image_0000001256858427](figures/en-us_image_0000001256858427.png)


### lineJoin
Z
zengyawen 已提交
189

190 191
```ts
// xxx.ets
Z
zengyawen 已提交
192 193 194
@Entry
@Component
struct LineJoinExample {
E
ester.zhou 已提交
195
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.beginPath()
          this.offContext.lineWidth = 8
          this.offContext.lineJoin = 'miter'
          this.offContext.moveTo(30, 30)
          this.offContext.lineTo(120, 60)
          this.offContext.lineTo(30, 110)
          this.offContext.stroke()
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
223 224 225 226
![en-us_image_0000001256858429](figures/en-us_image_0000001256858429.png)


### miterLimit
Z
zengyawen 已提交
227

228 229
```ts
// xxx.ets
Z
zengyawen 已提交
230 231 232
@Entry
@Component
struct MiterLimit {
E
ester.zhou 已提交
233
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.lineWidth = 8
          this.offContext.lineJoin = 'miter'
          this.offContext.miterLimit = 3
          this.offContext.moveTo(30, 30)
          this.offContext.lineTo(60, 35)
          this.offContext.lineTo(30, 37)
          this.offContext.stroke()
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
261 262 263 264
![en-us_image_0000001212218472](figures/en-us_image_0000001212218472.png)


### font
Z
zengyawen 已提交
265

266 267
```ts
// xxx.ets
Z
zengyawen 已提交
268 269
@Entry
@Component
270
struct Fonts {
E
ester.zhou 已提交
271
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.font = '30px sans-serif'
          this.offContext.fillText("Hello World", 20, 60)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
294 295 296 297
![en-us_image_0000001211898508](figures/en-us_image_0000001211898508.png)


### textAlign
Z
zengyawen 已提交
298

299 300
```ts
// xxx.ets
Z
zengyawen 已提交
301 302
@Entry
@Component
E
ester.zhou 已提交
303 304
struct CanvasExample {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
305 306 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 333 334 335 336 337 338 339 340 341
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
        this.offContext.strokeStyle = '#0000ff'
        this.offContext.moveTo(140, 10)
        this.offContext.lineTo(140, 160)
        this.offContext.stroke()

        this.offContext.font = '18px sans-serif'

        this.offContext.textAlign = 'start'
        this.offContext.fillText('textAlign=start', 140, 60)
        this.offContext.textAlign = 'end'
        this.offContext.fillText('textAlign=end', 140, 80)
        this.offContext.textAlign = 'left'
        this.offContext.fillText('textAlign=left', 140, 100)
        this.offContext.textAlign = 'center'
        this.offContext.fillText('textAlign=center',140, 120)
        this.offContext.textAlign = 'right'
        this.offContext.fillText('textAlign=right',140, 140)
        var image = this.offContext.transferToImageBitmap()
        this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
342 343 344 345
![en-us_image_0000001257138377](figures/en-us_image_0000001257138377.png)


### textBaseline
Z
zengyawen 已提交
346

347 348
```ts
// xxx.ets
Z
zengyawen 已提交
349 350 351
@Entry
@Component
struct TextBaseline {
E
ester.zhou 已提交
352
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.strokeStyle = '#0000ff'
          this.offContext.moveTo(0, 120)
          this.offContext.lineTo(400, 120)
          this.offContext.stroke()

          this.offContext.font = '20px sans-serif'

          this.offContext.textBaseline = 'top'
          this.offContext.fillText('Top', 10, 120)
          this.offContext.textBaseline = 'bottom'
          this.offContext.fillText('Bottom', 55, 120)
          this.offContext.textBaseline = 'middle'
          this.offContext.fillText('Middle', 125, 120)
          this.offContext.textBaseline = 'alphabetic'
          this.offContext.fillText('Alphabetic', 195, 120)
          this.offContext.textBaseline = 'hanging'
          this.offContext.fillText('Hanging', 295, 120)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
390 391 392 393
![en-us_image_0000001256978375](figures/en-us_image_0000001256978375.png)


### globalAlpha
Z
zengyawen 已提交
394

395 396
```ts
// xxx.ets
Z
zengyawen 已提交
397 398 399
@Entry
@Component
struct GlobalAlpha {
E
ester.zhou 已提交
400
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.fillStyle = 'rgb(255,0,0)'
          this.offContext.fillRect(0, 0, 50, 50)
          this.offContext.globalAlpha = 0.4
          this.offContext.fillStyle = 'rgb(0,0,255)'
          this.offContext.fillRect(50, 50, 50, 50)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
426 427 428 429
![en-us_image_0000001211898506](figures/en-us_image_0000001211898506.png)


### lineDashOffset
Z
zengyawen 已提交
430

431 432
```ts
// xxx.ets
Z
zengyawen 已提交
433 434 435
@Entry
@Component
struct LineDashOffset {
E
ester.zhou 已提交
436
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
437 438 439 440 441 442 443 444 445 446 447 448
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.arc(100, 75, 50, 0, 6.28)
          this.offContext.setLineDash([10,20])
E
ester.zhou 已提交
449
          this.offContext.lineDashOffset = 10.0;
Z
zengyawen 已提交
450 451 452 453 454 455 456 457 458 459
          this.offContext.stroke();
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}

E
ester.zhou 已提交
460
```
Z
zengyawen 已提交
461 462 463 464 465
![en-us_image_0000001212058506](figures/en-us_image_0000001212058506.png)


### globalCompositeOperation

E
ester.zhou 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478
| Name              | 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.|
Z
zengyawen 已提交
479

480 481
```ts
// xxx.ets
Z
zengyawen 已提交
482 483 484
@Entry
@Component
struct GlobalCompositeOperation {
E
ester.zhou 已提交
485
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.fillStyle = 'rgb(255,0,0)'
          this.offContext.fillRect(20, 20, 50, 50)
          this.offContext.globalCompositeOperation = 'source-over'
          this.offContext.fillStyle = 'rgb(0,0,255)'
          this.offContext.fillRect(50, 50, 50, 50)
          this.offContext.fillStyle = 'rgb(255,0,0)'
          this.offContext.fillRect(120, 20, 50, 50)
          this.offContext.globalCompositeOperation = 'destination-over'
          this.offContext.fillStyle = 'rgb(0,0,255)'
          this.offContext.fillRect(150, 50, 50, 50)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
516 517 518 519
![en-us_image_0000001212218474](figures/en-us_image_0000001212218474.png)


### shadowBlur
Z
zengyawen 已提交
520

521 522
```ts
// xxx.ets
Z
zengyawen 已提交
523 524 525
@Entry
@Component
struct ShadowBlur {
E
ester.zhou 已提交
526
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.shadowBlur = 30
          this.offContext.shadowColor = 'rgb(0,0,0)'
          this.offContext.fillStyle = 'rgb(255,0,0)'
          this.offContext.fillRect(20, 20, 100, 80)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
551 552 553 554
![en-us_image_0000001211898514](figures/en-us_image_0000001211898514.png)


### shadowColor
Z
zengyawen 已提交
555

556 557
```ts
// xxx.ets
Z
zengyawen 已提交
558 559 560
@Entry
@Component
struct ShadowColor {
E
ester.zhou 已提交
561
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.shadowBlur = 30
          this.offContext.shadowColor = 'rgb(0,0,255)'
          this.offContext.fillStyle = 'rgb(255,0,0)'
          this.offContext.fillRect(30, 30, 100, 100)
E
esterzhou 已提交
576
          var image = this.offContext.transferToImageBitmap()
Z
zengyawen 已提交
577 578 579 580 581 582 583 584 585
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
586 587 588 589
![en-us_image_0000001212058502](figures/en-us_image_0000001212058502.png)


### shadowOffsetX
Z
zengyawen 已提交
590

591 592
```ts
// xxx.ets
Z
zengyawen 已提交
593 594 595
@Entry
@Component
struct ShadowOffsetX {
E
ester.zhou 已提交
596
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.shadowBlur = 10
          this.offContext.shadowOffsetX = 20
          this.offContext.shadowColor = 'rgb(0,0,0)'
          this.offContext.fillStyle = 'rgb(255,0,0)'
          this.offContext.fillRect(20, 20, 100, 80)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
622 623 624 625
![en-us_image_0000001257138379](figures/en-us_image_0000001257138379.png)


### shadowOffsetY
Z
zengyawen 已提交
626

627 628
```ts
// xxx.ets
Z
zengyawen 已提交
629 630 631
@Entry
@Component
struct ShadowOffsetY {
E
ester.zhou 已提交
632
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.shadowBlur = 10
          this.offContext.shadowOffsetY = 20
          this.offContext.shadowColor = 'rgb(0,0,0)'
          this.offContext.fillStyle = 'rgb(255,0,0)'
          this.offContext.fillRect(30, 30, 100, 100)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
658 659 660 661
![en-us_image_0000001257058427](figures/en-us_image_0000001257058427.png)


### imageSmoothingEnabled
Z
zengyawen 已提交
662

663 664
```ts
// xxx.ets
Z
zengyawen 已提交
665 666 667
@Entry
@Component
struct ImageSmoothingEnabled {
E
ester.zhou 已提交
668
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private img:ImageBitmap = new ImageBitmap("common/images/icon.jpg")
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.imageSmoothingEnabled = false
          this.offContext.drawImage( this.img,0,0,400,200)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
692 693
![en-us_image_0000001257138385](figures/en-us_image_0000001257138385.png)

Z
zengyawen 已提交
694

Z
zengyawen 已提交
695
## Methods
Z
zengyawen 已提交
696 697


Z
zengyawen 已提交
698 699 700
### fillRect

fillRect(x: number, y: number, w: number, h: number): void
Z
zengyawen 已提交
701 702 703

Fills a rectangle on the canvas.

E
ester.zhou 已提交
704
 **Parameters**
Z
zengyawen 已提交
705

E
ester.zhou 已提交
706 707 708 709 710 711 712 713
| Name    | Type    | Mandatory  | Default Value | Description           |
| ------ | ------ | ---- | ---- | ------------- |
| x      | number | Yes   | 0    | X-coordinate of the upper left corner of the rectangle.|
| y      | number | Yes   | 0    | Y-coordinate of the upper left corner of the rectangle.|
| width  | number | Yes   | 0    | Width of the rectangle.     |
| height | number | Yes   | 0    | Height of the rectangle.     |

 **Example**
H
HelloCrease 已提交
714

715
  ```ts
716
  // xxx.ets
Z
zengyawen 已提交
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
  @Entry
  @Component
  struct FillRect {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.fillRect(0,30,100,100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
         })
Z
zengyawen 已提交
735
        }
Z
zengyawen 已提交
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
      .width('100%')
      .height('100%')
    }
  }
  ```

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


### strokeRect

strokeRect(x: number, y: number, w: number, h: number): void

Draws an outlined rectangle on the canvas.

E
ester.zhou 已提交
751 752 753 754 755 756 757 758
 **Parameters**

| Name    | Type    | Mandatory  | Default Value | Description          |
| ------ | ------ | ---- | ---- | ------------ |
| x      | number | Yes   | 0    | X-coordinate of the upper left corner of the rectangle.|
| y      | number | Yes   | 0    | Y-coordinate of the upper left corner of the rectangle.|
| width  | number | Yes   | 0    | Width of the rectangle.    |
| height | number | Yes   | 0    | Height of the rectangle.    |
Z
zengyawen 已提交
759

E
ester.zhou 已提交
760
 **Example**
H
HelloCrease 已提交
761

762 763
  ```ts
  // xxx.ets
Z
zengyawen 已提交
764 765 766 767 768 769
  @Entry
  @Component
  struct StrokeRect {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
770

Z
zengyawen 已提交
771 772 773 774 775 776 777 778 779 780 781
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.strokeRect(30, 30, 200, 150)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
782
      }
Z
zengyawen 已提交
783 784
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
785
    }
Z
zengyawen 已提交
786 787
  }
  ```
Z
zengyawen 已提交
788

Z
zengyawen 已提交
789
  ![en-us_image_0000001212378436](figures/en-us_image_0000001212378436.png)
Z
zengyawen 已提交
790 791


Z
zengyawen 已提交
792
### clearRect
Z
zengyawen 已提交
793

Z
zengyawen 已提交
794
clearRect(x: number, y: number, w: number, h: number): void
Z
zengyawen 已提交
795 796 797

Clears the content in a rectangle on the canvas.

E
ester.zhou 已提交
798 799 800 801 802 803 804 805
 **Parameters**

| Name    | Type    | Mandatory  | Default Value | Description           |
| ------ | ------ | ---- | ---- | ------------- |
| x      | number | Yes   | 0    | X-coordinate of the upper left corner of the rectangle.|
| y      | number | Yes   | 0    | Y-coordinate of the upper left corner of the rectangle.|
| width  | number | Yes   | 0    | Width of the rectangle.     |
| height | number | Yes   | 0    | Height of the rectangle.     |
Z
zengyawen 已提交
806

E
ester.zhou 已提交
807
 **Example**
H
HelloCrease 已提交
808

809
  ```ts
810
  // xxx.ets
Z
zengyawen 已提交
811 812 813 814 815 816
  @Entry
  @Component
  struct ClearRect {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
817

Z
zengyawen 已提交
818 819 820 821 822 823 824 825
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.fillStyle = 'rgb(0,0,255)'
E
esterzhou 已提交
826 827
            this.offContext.fillRect(20,20,200,200)
            this.offContext.clearRect(30,30,150,100)
Z
zengyawen 已提交
828 829 830
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
831
      }
Z
zengyawen 已提交
832 833
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
834
    }
Z
zengyawen 已提交
835 836
  }
  ```
Z
zengyawen 已提交
837

Z
zengyawen 已提交
838
  ![en-us_image_0000001212058500](figures/en-us_image_0000001212058500.png)
Z
zengyawen 已提交
839 840


Z
zengyawen 已提交
841
### fillText
Z
zengyawen 已提交
842

E
ester.zhou 已提交
843
fillText(text: string, x: number, y: number, maxWidth?: number): void
Z
zengyawen 已提交
844 845 846

Draws filled text on the canvas.

E
ester.zhou 已提交
847
**Parameters**
Z
zengyawen 已提交
848

E
esterzhou 已提交
849 850 851 852 853 854
| Name      | Type    | Mandatory  | Default Value | Description             |
| -------- | ------ | ---- | ---- | --------------- |
| text     | string | Yes   | ""   | Text to draw.     |
| x        | number | Yes   | 0    | X-coordinate of the lower left corner of the text.|
| y        | number | Yes   | 0    | Y-coordinate of the lower left corner of the text.|
| maxWidth | number | No   | -    | Maximum width allowed for the text.   |
E
ester.zhou 已提交
855 856

 **Example**
H
HelloCrease 已提交
857

858
  ```ts
859
  // xxx.ets
Z
zengyawen 已提交
860 861 862 863 864 865
  @Entry
  @Component
  struct FillText {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
866

Z
zengyawen 已提交
867 868 869 870 871 872 873 874 875 876 877 878
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.font = '30px sans-serif'
            this.offContext.fillText("Hello World!", 20, 100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
879
      }
Z
zengyawen 已提交
880 881
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
882
    }
Z
zengyawen 已提交
883 884
  }
  ```
Z
zengyawen 已提交
885

Z
zengyawen 已提交
886
  ![en-us_image_0000001257058437](figures/en-us_image_0000001257058437.png)
Z
zengyawen 已提交
887 888


Z
zengyawen 已提交
889
### strokeText
Z
zengyawen 已提交
890

Z
zengyawen 已提交
891
strokeText(text: string, x: number, y: number): void
Z
zengyawen 已提交
892 893 894

Draws a text stroke on the canvas.

E
ester.zhou 已提交
895 896
**Parameters**

E
esterzhou 已提交
897 898 899 900 901 902
| Name      | Type    | Mandatory  | Default Value | Description             |
| -------- | ------ | ---- | ---- | --------------- |
| text     | string | Yes   | ""   | Text to draw.     |
| x        | number | Yes   | 0    | X-coordinate of the lower left corner of the text.|
| y        | number | Yes   | 0    | Y-coordinate of the lower left corner of the text.|
| maxWidth | number | No   | -    | Maximum width of the text to be drawn. |
Z
zengyawen 已提交
903

E
ester.zhou 已提交
904
 **Example**
H
HelloCrease 已提交
905

906 907
  ```ts
  // xxx.ets
Z
zengyawen 已提交
908 909 910 911 912 913
  @Entry
  @Component
  struct StrokeText {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
914

Z
zengyawen 已提交
915 916 917 918 919 920 921 922 923 924 925 926
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.font = '55px sans-serif'
            this.offContext.strokeText("Hello World!", 20, 60)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
927
      }
Z
zengyawen 已提交
928 929
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
930
    }
Z
zengyawen 已提交
931 932 933 934 935 936 937 938 939 940 941 942
  }
  ```

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


### measureText

measureText(text: string): TextMetrics

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

E
ester.zhou 已提交
943 944 945 946 947 948 949
 **Parameters**

| Name  | Type    | Mandatory  | Default Value | Description        |
| ---- | ------ | ---- | ---- | ---------- |
| text | string | Yes   | ""   | Text to be measured.|

 **Return value**
Z
zengyawen 已提交
950

E
ester.zhou 已提交
951 952 953
| Type         | Description     |
| ----------- | ------- |
| TextMetrics | **TextMetrics** object.|
Z
zengyawen 已提交
954

E
esterzhou 已提交
955
**TextMetrics** attributes
Z
zengyawen 已提交
956

E
esterzhou 已提交
957 958 959 960
| Name                      | Type    | Description                                      |
| ------------------------ | ------ | ---------------------------------------- |
| width                    | number | Width of the text.                                 |
| height                   | number | Height of the text.                                 |
E
ester.zhou 已提交
961 962 963 964 965 966 967 968 969 970 971 972 973
| actualBoundingBoxAscent  | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the top of the bounding rectangle used to render the text. The current value is **0**.|
| actualBoundingBoxDescent | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the bottom of the bounding rectangle used to render the text. The current value is **0**.|
| actualBoundingBoxLeft    | number | Distance parallel to the baseline from the alignment point determined by the **CanvasRenderingContext2D.textAlign** attribute to the left side of the bounding rectangle of the text. The current value is **0**.|
| actualBoundingBoxRight   | number | Distance parallel to the baseline from the alignment point determined by the **CanvasRenderingContext2D.textAlign** attribute to the right side of the bounding rectangle of the text. The current value is **0**.|
| alphabeticBaseline       | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the alphabetic baseline of the line box. The current value is **0**.|
| emHeightAscent           | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the top of the em square in the line box. The current value is **0**.|
| emHeightDescent          | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the bottom of the em square in the line box. The current value is **0**.|
| fontBoundingBoxAscent    | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the top of the highest bounding rectangle of all the fonts used to render the text. The current value is **0**.|
| fontBoundingBoxDescent   | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the bottom of the bounding rectangle of all the fonts used to render the text. The current value is **0**.|
| hangingBaseline          | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the hanging baseline of the line box. The current value is **0**.|
| ideographicBaseline      | number | Distance from the horizontal line indicated by the **CanvasRenderingContext2D.textBaseline** attribute to the ideographic baseline of the line box. The current value is **0**.|

 **Example**
H
HelloCrease 已提交
974

975
  ```ts
976
  // xxx.ets
Z
zengyawen 已提交
977 978 979 980 981 982
  @Entry
  @Component
  struct MeasureText {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
983

Z
zengyawen 已提交
984 985 986 987 988 989 990 991 992 993 994 995 996
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.font = '50px sans-serif'
            this.offContext.fillText("Hello World!", 20, 100)
            this.offContext.fillText("width:" + this.context.measureText("Hello World!").width, 20, 200)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
997
      }
Z
zengyawen 已提交
998 999
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1000
    }
Z
zengyawen 已提交
1001 1002
  }
  ```
Z
zengyawen 已提交
1003

Z
zengyawen 已提交
1004
  ![en-us_image_0000001256858431](figures/en-us_image_0000001256858431.png)
Z
zengyawen 已提交
1005 1006


Z
zengyawen 已提交
1007
### stroke
Z
zengyawen 已提交
1008

Z
zengyawen 已提交
1009
stroke(path?: Path2D): void
Z
zengyawen 已提交
1010 1011 1012

Strokes a path.

E
ester.zhou 已提交
1013 1014 1015 1016 1017
 **Parameters**

| Name  | Type                                      | Mandatory  | Default Value | Description          |
| ---- | ---------------------------------------- | ---- | ---- | ------------ |
| path | [Path2D](ts-components-canvas-path2d.md) | No   | null | A **Path2D** path to draw.|
Z
zengyawen 已提交
1018

E
ester.zhou 已提交
1019
 **Example**
E
esterzhou 已提交
1020
 
1021 1022
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1023 1024 1025 1026 1027 1028
  @Entry
  @Component
  struct Stroke {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1029

Z
zengyawen 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.moveTo(25, 25)
            this.offContext.lineTo(25, 105)
E
esterzhou 已提交
1039 1040
            this.offContext.lineTo(75, 105)
            this.offContext.lineTo(75, 25)
Z
zengyawen 已提交
1041 1042 1043 1044 1045
            this.offContext.strokeStyle = 'rgb(0,0,255)'
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1046
      }
Z
zengyawen 已提交
1047 1048
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1049
    }
Z
zengyawen 已提交
1050 1051
  }
  ```
Z
zengyawen 已提交
1052

Z
zengyawen 已提交
1053
  ![en-us_image_0000001257138373](figures/en-us_image_0000001257138373.png)
Z
zengyawen 已提交
1054 1055


Z
zengyawen 已提交
1056
### beginPath
Z
zengyawen 已提交
1057

Z
zengyawen 已提交
1058
beginPath(): void
Z
zengyawen 已提交
1059 1060 1061

Creates a drawing path.

E
ester.zhou 已提交
1062
 **Example**
H
HelloCrease 已提交
1063

1064
  ```ts
1065
  // xxx.ets
Z
zengyawen 已提交
1066 1067 1068 1069 1070 1071
  @Entry
  @Component
  struct BeginPath {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1072

Z
zengyawen 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.lineWidth = 6
            this.offContext.strokeStyle = '#0000ff'
            this.offContext.moveTo(15, 80)
            this.offContext.lineTo(280, 160)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1089
      }
Z
zengyawen 已提交
1090 1091
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1092
    }
Z
zengyawen 已提交
1093 1094
  }
  ```
Z
zengyawen 已提交
1095

Z
zengyawen 已提交
1096
  ![en-us_image_0000001212378440](figures/en-us_image_0000001212378440.png)
Z
zengyawen 已提交
1097 1098


Z
zengyawen 已提交
1099
### moveTo
Z
zengyawen 已提交
1100

Z
zengyawen 已提交
1101
moveTo(x: number, y: number): void
Z
zengyawen 已提交
1102 1103 1104

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

E
ester.zhou 已提交
1105
 **Parameters**
Z
zengyawen 已提交
1106

E
ester.zhou 已提交
1107 1108 1109 1110 1111 1112
| Name  | Type    | Mandatory  | Default Value | Description       |
| ---- | ------ | ---- | ---- | --------- |
| x    | number | Yes   | 0    | X-coordinate of the target position.|
| y    | number | Yes   | 0    | Y-coordinate of the target position.|

 **Example**
H
HelloCrease 已提交
1113

1114
  ```ts
1115
  // xxx.ets
Z
zengyawen 已提交
1116 1117 1118 1119 1120 1121
  @Entry
  @Component
  struct MoveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1122

Z
zengyawen 已提交
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.moveTo(10, 10)
            this.offContext.lineTo(280, 160)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1137
      }
Z
zengyawen 已提交
1138 1139
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1140
    }
Z
zengyawen 已提交
1141 1142
  }
  ```
Z
zengyawen 已提交
1143

Z
zengyawen 已提交
1144
  ![en-us_image_0000001212058498](figures/en-us_image_0000001212058498.png)
Z
zengyawen 已提交
1145 1146


Z
zengyawen 已提交
1147
### lineTo
Z
zengyawen 已提交
1148

Z
zengyawen 已提交
1149
lineTo(x: number, y: number): void
Z
zengyawen 已提交
1150 1151 1152

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

E
ester.zhou 已提交
1153 1154 1155 1156 1157 1158
 **Parameters**

| Name  | Type    | Mandatory  | Default Value | Description       |
| ---- | ------ | ---- | ---- | --------- |
| x    | number | Yes   | 0    | X-coordinate of the target position.|
| y    | number | Yes   | 0    | Y-coordinate of the target position.|
Z
zengyawen 已提交
1159

E
ester.zhou 已提交
1160
 **Example**
H
HelloCrease 已提交
1161

1162
  ```ts
1163
  // xxx.ets
Z
zengyawen 已提交
1164 1165 1166 1167 1168 1169
  @Entry
  @Component
  struct LineTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1170

Z
zengyawen 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.moveTo(10, 10)
            this.offContext.lineTo(280, 160)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1185
      }
Z
zengyawen 已提交
1186 1187
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1188
    }
Z
zengyawen 已提交
1189 1190
  }
  ```
Z
zengyawen 已提交
1191

Z
zengyawen 已提交
1192
  ![en-us_image_0000001257058435](figures/en-us_image_0000001257058435.png)
Z
zengyawen 已提交
1193 1194


Z
zengyawen 已提交
1195
### closePath
Z
zengyawen 已提交
1196

Z
zengyawen 已提交
1197
closePath(): void
Z
zengyawen 已提交
1198 1199 1200

Draws a closed path.

E
ester.zhou 已提交
1201
 **Example**
H
HelloCrease 已提交
1202

1203
  ```ts
1204
  // xxx.ets
Z
zengyawen 已提交
1205 1206 1207 1208 1209 1210
  @Entry
  @Component
  struct ClosePath {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1211

Z
zengyawen 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
              this.offContext.beginPath()
              this.offContext.moveTo(30, 30)
              this.offContext.lineTo(110, 30)
              this.offContext.lineTo(70, 90)
              this.offContext.closePath()
              this.offContext.stroke()
              var image = this.offContext.transferToImageBitmap()
              this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1228
      }
Z
zengyawen 已提交
1229 1230
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1231
    }
Z
zengyawen 已提交
1232 1233
  }
  ```
Z
zengyawen 已提交
1234

Z
zengyawen 已提交
1235
  ![en-us_image_0000001257058431](figures/en-us_image_0000001257058431.png)
Z
zengyawen 已提交
1236 1237


Z
zengyawen 已提交
1238
### createPattern
Z
zengyawen 已提交
1239

E
ester.zhou 已提交
1240
createPattern(image: ImageBitmap, repetition: string | null): CanvasPattern | null
Z
zengyawen 已提交
1241 1242 1243

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

E
ester.zhou 已提交
1244
**Parameters**
Z
zengyawen 已提交
1245

E
ester.zhou 已提交
1246 1247 1248 1249 1250 1251 1252
| Name        | Type                                      | Mandatory  | Default Value | Description                                      |
| ---------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- |
| image      | [ImageBitmap](ts-components-canvas-imagebitmap.md) | Yes   | null | Source image. For details, see **ImageBitmap**.                 |
| repetition | string                                   | Yes   | ""  | Repetition mode. The value can be **"repeat"**, **"repeat-x"**, **"repeat-y"**, or **"no-repeat"**.|

**Return value**

E
esterzhou 已提交
1253 1254 1255
| Type                             | Description                     |
| ------------------------------- | ----------------------- |
| [CanvasPattern](#canvaspattern) | Created pattern for image filling based on a specified source image and repetition mode.|
E
ester.zhou 已提交
1256 1257

 **Example**
H
HelloCrease 已提交
1258

1259
  ```ts
1260
  // xxx.ets
Z
zengyawen 已提交
1261 1262 1263 1264 1265 1266 1267
  @Entry
  @Component
  struct CreatePattern {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private img:ImageBitmap = new ImageBitmap("common/images/icon.jpg")
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1268

Z
zengyawen 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var pattern = this.offContext.createPattern(this.img, 'repeat')
            this.offContext.fillStyle = pattern
            this.offContext.fillRect(0, 0, 200, 200)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1282
      }
Z
zengyawen 已提交
1283 1284
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1285
    }
Z
zengyawen 已提交
1286 1287
  }
  ```
Z
zengyawen 已提交
1288

Z
zengyawen 已提交
1289
  ![en-us_image_0000001257138387](figures/en-us_image_0000001257138387.png)
Z
zengyawen 已提交
1290 1291


Z
zengyawen 已提交
1292
### bezierCurveTo
Z
zengyawen 已提交
1293

Z
zengyawen 已提交
1294
bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void
Z
zengyawen 已提交
1295 1296 1297

Draws a cubic bezier curve on the canvas.

E
ester.zhou 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
 **Parameters**

| Name  | Type    | Mandatory  | Default Value | Description            |
| ---- | ------ | ---- | ---- | -------------- |
| cp1x | number | Yes   | 0    | X-coordinate of the first parameter of the bezier curve.|
| cp1y | number | Yes   | 0    | Y-coordinate of the first parameter of the bezier curve.|
| cp2x | number | Yes   | 0    | X-coordinate of the second parameter of the bezier curve.|
| cp2y | number | Yes   | 0    | Y-coordinate of the second parameter of the bezier curve.|
| x    | number | Yes   | 0    | X-coordinate of the end point on the bezier curve.   |
| y    | number | Yes   | 0    | Y-coordinate of the end point on the bezier curve.   |
Z
zengyawen 已提交
1308

E
ester.zhou 已提交
1309
 **Example**
H
HelloCrease 已提交
1310

1311
  ```ts
1312
  // xxx.ets
Z
zengyawen 已提交
1313 1314 1315 1316 1317 1318
  @Entry
  @Component
  struct BezierCurveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1319

Z
zengyawen 已提交
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.moveTo(10, 10)
            this.offContext.bezierCurveTo(20, 100, 200, 100, 200, 20)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1334
      }
Z
zengyawen 已提交
1335 1336
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1337
    }
Z
zengyawen 已提交
1338 1339
  }
  ```
Z
zengyawen 已提交
1340

Z
zengyawen 已提交
1341
  ![en-us_image_0000001212378442](figures/en-us_image_0000001212378442.png)
Z
zengyawen 已提交
1342 1343


Z
zengyawen 已提交
1344
### quadraticCurveTo
Z
zengyawen 已提交
1345

Z
zengyawen 已提交
1346
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void
Z
zengyawen 已提交
1347 1348 1349

Draws a quadratic curve on the canvas.

E
ester.zhou 已提交
1350
 **Parameters**
Z
zengyawen 已提交
1351

E
ester.zhou 已提交
1352 1353 1354 1355 1356 1357 1358 1359
| Name  | Type    | Mandatory  | Default Value | Description         |
| ---- | ------ | ---- | ---- | ----------- |
| cpx  | number | Yes   | 0    | X-coordinate of the bezier curve parameter.|
| cpy  | number | Yes   | 0    | Y-coordinate of the bezier curve parameter.|
| x    | number | Yes   | 0    | X-coordinate of the end point on the bezier curve.|
| y    | number | Yes   | 0    | Y-coordinate of the end point on the bezier curve.|

 **Example**
H
HelloCrease 已提交
1360

1361
  ```ts
1362
  // xxx.ets
Z
zengyawen 已提交
1363 1364 1365 1366 1367 1368
  @Entry
  @Component
  struct QuadraticCurveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1369

Z
zengyawen 已提交
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath();
            this.offContext.moveTo(20, 20);
            this.offContext.quadraticCurveTo(100, 100, 200, 20);
            this.offContext.stroke();
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
1384
      }
Z
zengyawen 已提交
1385 1386
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1387
    }
Z
zengyawen 已提交
1388 1389
  }
  ```
Z
zengyawen 已提交
1390

Z
zengyawen 已提交
1391
  ![en-us_image_0000001256978383](figures/en-us_image_0000001256978383.png)
Z
zengyawen 已提交
1392 1393


Z
zengyawen 已提交
1394
### arc
Z
zengyawen 已提交
1395

E
ester.zhou 已提交
1396
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void
Z
zengyawen 已提交
1397 1398 1399

Draws an arc on the canvas.

E
ester.zhou 已提交
1400 1401
 **Parameters**

E
esterzhou 已提交
1402 1403 1404 1405 1406 1407 1408
| Name              | Type     | Mandatory  | Default Value  | Description        |
| ---------------- | ------- | ---- | ----- | ---------- |
| x                | number  | Yes   | 0     | X-coordinate of the center point of the arc.|
| y                | number  | Yes   | 0     | Y-coordinate of the center point of the arc.|
| radius           | number  | Yes   | 0     | Radius of the arc.   |
| startAngle       | number  | Yes   | 0     | Start radian of the arc.  |
| endAngle         | number  | Yes   | 0     | End radian of the arc.  |
E
ester.zhou 已提交
1409
| counterclockwise | boolean | No   | false | Whether to draw the arc counterclockwise.|
Z
zengyawen 已提交
1410

E
ester.zhou 已提交
1411
 **Example**
H
HelloCrease 已提交
1412

1413
  ```ts
1414
  // xxx.ets
Z
zengyawen 已提交
1415 1416 1417 1418 1419 1420
  @Entry
  @Component
  struct Arc {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1421

Z
zengyawen 已提交
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.arc(100, 75, 50, 0, 6.28)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1435
      }
Z
zengyawen 已提交
1436 1437
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1438
    }
Z
zengyawen 已提交
1439 1440
  }
  ```
Z
zengyawen 已提交
1441

Z
zengyawen 已提交
1442
  ![en-us_image_0000001212378430](figures/en-us_image_0000001212378430.png)
Z
zengyawen 已提交
1443 1444


Z
zengyawen 已提交
1445
### arcTo
Z
zengyawen 已提交
1446

Z
zengyawen 已提交
1447
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void
Z
zengyawen 已提交
1448 1449 1450

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

E
ester.zhou 已提交
1451 1452 1453 1454 1455 1456 1457 1458 1459
 **Parameters**

| Name    | Type    | Mandatory  | Default Value | Description             |
| ------ | ------ | ---- | ---- | --------------- |
| x1     | number | Yes   | 0    | X-coordinate of the first point on the arc.|
| y1     | number | Yes   | 0    | Y-coordinate of the first point on the arc.|
| x2     | number | Yes   | 0    | X-coordinate of the second point on the arc.|
| y2     | number | Yes   | 0    | Y-coordinate of the second point on the arc.|
| radius | number | Yes   | 0    | Radius of the arc.       |
Z
zengyawen 已提交
1460

E
ester.zhou 已提交
1461
 **Example**
H
HelloCrease 已提交
1462

1463
  ```ts
1464
  // xxx.ets
Z
zengyawen 已提交
1465 1466 1467 1468 1469 1470
  @Entry
  @Component
  struct ArcTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1471

Z
zengyawen 已提交
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.moveTo(100, 20);
            this.offContext.arcTo(150, 20, 150, 70, 50);
            this.offContext.stroke();
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1485
      }
Z
zengyawen 已提交
1486 1487
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1488
    }
Z
zengyawen 已提交
1489 1490
  }
  ```
Z
zengyawen 已提交
1491

Z
zengyawen 已提交
1492
  ![en-us_image_0000001257138383](figures/en-us_image_0000001257138383.png)
Z
zengyawen 已提交
1493 1494


Z
zengyawen 已提交
1495
### ellipse
Z
zengyawen 已提交
1496

E
ester.zhou 已提交
1497
ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void
Z
zengyawen 已提交
1498

E
ester.zhou 已提交
1499
Draws an ellipse in the specified rectangular region on the canvas.
Z
zengyawen 已提交
1500

E
ester.zhou 已提交
1501
 **Parameters**
Z
zengyawen 已提交
1502

E
esterzhou 已提交
1503 1504 1505 1506 1507 1508 1509 1510 1511
| Name              | Type     | Mandatory  | Default Value  | Description               |
| ---------------- | ------- | ---- | ----- | ----------------- |
| x                | number  | Yes   | 0     | X-coordinate of the ellipse center.       |
| y                | number  | Yes   | 0     | Y-coordinate of the ellipse center.       |
| radiusX          | number  | Yes   | 0     | Ellipse radius on the x-axis.       |
| radiusY          | number  | Yes   | 0     | Ellipse radius on the y-axis.       |
| rotation         | number  | Yes   | 0     | Rotation angle of the ellipse. The unit is radian.   |
| startAngle       | number  | Yes   | 0     | Angle of the start point for drawing the ellipse. The unit is radian.|
| endAngle         | number  | Yes   | 0     | Angle of the end point for drawing the ellipse. The unit is radian.|
E
ester.zhou 已提交
1512
| counterclockwise | boolean | No   | false | Whether to draw the ellipse in the counterclockwise direction.    |
Z
zengyawen 已提交
1513

E
ester.zhou 已提交
1514
 **Example**
H
HelloCrease 已提交
1515

1516
  ```ts
1517
  // xxx.ets
Z
zengyawen 已提交
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
E
esterzhou 已提交
1532
            this.offContext.ellipse(200, 200, 50, 100, Math.PI * 0.25, Math.PI * 0.5, Math.PI * 2)
Z
zengyawen 已提交
1533 1534 1535 1536
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1537
      }
Z
zengyawen 已提交
1538 1539
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1540
    }
Z
zengyawen 已提交
1541 1542
  }
  ```
Z
zengyawen 已提交
1543

Z
zengyawen 已提交
1544
  ![en-us_image_0000001256858423](figures/en-us_image_0000001256858423.png)
Z
zengyawen 已提交
1545 1546


Z
zengyawen 已提交
1547
### rect
Z
zengyawen 已提交
1548

E
ester.zhou 已提交
1549
rect(x: number, y: number, w: number, h: number): void
Z
zengyawen 已提交
1550

E
ester.zhou 已提交
1551
Creates a rectangle on the canvas.
Z
zengyawen 已提交
1552

E
ester.zhou 已提交
1553
 **Parameters**
Z
zengyawen 已提交
1554

E
esterzhou 已提交
1555 1556 1557 1558 1559 1560
| Name  | Type    | Mandatory  | Default Value | Description           |
| ---- | ------ | ---- | ---- | ------------- |
| x    | number | Yes   | 0    | X-coordinate of the upper left corner of the rectangle.|
| y    | number | Yes   | 0    | Y-coordinate of the upper left corner of the rectangle.|
| w    | number | Yes   | 0    | Width of the rectangle.     |
| h    | number | Yes   | 0    | Height of the rectangle.     |
E
ester.zhou 已提交
1561 1562

 **Example**
H
HelloCrease 已提交
1563

1564
  ```ts
1565
  // xxx.ets
Z
zengyawen 已提交
1566 1567 1568 1569 1570 1571
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1572

Z
zengyawen 已提交
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.rect(20, 20, 100, 100) // Create a 100*100 rectangle at (20, 20)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1585
      }
Z
zengyawen 已提交
1586 1587
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1588
    }
Z
zengyawen 已提交
1589 1590
  }
  ```
Z
zengyawen 已提交
1591

Z
zengyawen 已提交
1592
  ![en-us_image_0000001257138381](figures/en-us_image_0000001257138381.png)
Z
zengyawen 已提交
1593 1594


Z
zengyawen 已提交
1595
### fill
Z
zengyawen 已提交
1596

E
esterzhou 已提交
1597
fill(fillRule?: CanvasFillRule): void
Z
zengyawen 已提交
1598

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

E
esterzhou 已提交
1601 1602 1603 1604 1605
**Parameters**

| Name      | Type            | Mandatory  | Default Value      | Description                                      |
| -------- | -------------- | ---- | --------- | ---------------------------------------- |
| fillRule | CanvasFillRule | No   | "nonzero" | Rule by which to determine whether a point is inside or outside the area to fill.<br>The options are **"nonzero"** and **"evenodd"**.|
H
HelloCrease 已提交
1606

1607
  ```ts
1608
  // xxx.ets
Z
zengyawen 已提交
1609 1610 1611 1612 1613 1614
  @Entry
  @Component
  struct Fill {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1615

Z
zengyawen 已提交
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.rect(20, 20, 100, 100) // Create a 100*100 rectangle at (20, 20)
            this.offContext.fill()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1628
      }
Z
zengyawen 已提交
1629 1630
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1631
    }
Z
zengyawen 已提交
1632 1633
  }
  ```
Z
zengyawen 已提交
1634

Z
zengyawen 已提交
1635
  ![en-us_image_0000001256858421](figures/en-us_image_0000001256858421.png)
Z
zengyawen 已提交
1636 1637


E
esterzhou 已提交
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
fill(path: Path2D, fillRule?: CanvasFillRule): void

Fills the area inside a closed path on the canvas.

**Parameters**

| Name      | Type            | Mandatory  | Default Value      | Description                                      |
| -------- | -------------- | ---- | --------- | ---------------------------------------- |
| path     | Path2D         | Yes   |           | A **Path2D** path to fill.                             |
| fillRule | CanvasFillRule | No   | "nonzero" | Rule by which to determine whether a point is inside or outside the area to fill.<br>The options are **"nonzero"** and **"evenodd"**.|


**Example**  

```ts
// xxx.ets
@Entry
@Component
struct Fill {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          let region = new Path2D();
          region.moveTo(30, 90);
          region.lineTo(110, 20);
          region.lineTo(240, 130);
          region.lineTo(60, 130);
          region.lineTo(190, 20);
          region.lineTo(270, 90);
          region.closePath();
          // Fill path
          this.offContext.fillStyle = 'green';
          this.offContext.fill(region, "evenodd");
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

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



Z
zengyawen 已提交
1693
### clip
Z
zengyawen 已提交
1694

E
esterzhou 已提交
1695
clip(fillRule?: CanvasFillRule): void
Z
zengyawen 已提交
1696 1697 1698

Sets the current path to a clipping path.

E
esterzhou 已提交
1699 1700 1701 1702 1703 1704
**Parameters**

| Name      | Type            | Mandatory  | Default Value      | Description                                      |
| -------- | -------------- | ---- | --------- | ---------------------------------------- |
| fillRule | CanvasFillRule | No   | "nonzero" | Rule by which to determine whether a point is inside or outside the area to clip.<br>The options are **"nonzero"** and **"evenodd"**.|

E
ester.zhou 已提交
1705
 **Example**
H
HelloCrease 已提交
1706

1707
  ```ts
1708
  // xxx.ets
Z
zengyawen 已提交
1709 1710 1711 1712 1713 1714
  @Entry
  @Component
  struct Clip {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1715

Z
zengyawen 已提交
1716 1717 1718 1719 1720 1721 1722
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
esterzhou 已提交
1723
            this.offContext.rect(0, 0, 100, 200)
Z
zengyawen 已提交
1724 1725 1726
            this.offContext.stroke()
            this.offContext.clip()
            this.offContext.fillStyle = "rgb(255,0,0)"
E
esterzhou 已提交
1727
            this.offContext.fillRect(0, 0, 200, 200)
Z
zengyawen 已提交
1728 1729 1730
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1731
      }
Z
zengyawen 已提交
1732 1733
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1734
    }
Z
zengyawen 已提交
1735 1736
  }
  ```
Z
zengyawen 已提交
1737

Z
zengyawen 已提交
1738
  ![en-us_image_0000001257058441](figures/en-us_image_0000001257058441.png)
Z
zengyawen 已提交
1739 1740


E
esterzhou 已提交
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
clip(path:Path2D, fillRule?: CanvasFillRule): void

Sets a closed path to a clipping path.

**Parameters**

| Name      | Type            | Mandatory  | Default Value      | Description                                      |
| -------- | -------------- | ---- | --------- | ---------------------------------------- |
| path | Path2D | Yes   |  | A **Path2D** path to clip.|
| fillRule | CanvasFillRule | No   | "nonzero" | Rule by which to determine whether a point is inside or outside the area to clip.<br>The options are **"nonzero"** and **"evenodd"**.|

 **Example**

  ```ts
  // xxx.ets
@Entry
@Component
struct Clip {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          let region = new Path2D();
          region.rect(80,10,20,130);
          region.rect(40,50,100,50);
          this.offContext.clip(region,"evenodd")
          this.offContext.fillStyle = "rgb(255,0,0)"
          this.offContext.fillRect(0, 0, 600, 600)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
        })
    }
    .width('100%')
    .height('100%')
  }
}
  ```

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



### filter

filter(filter: string): void

Sets a filter for the image on the canvas. This API is a void API.

**Parameters**

| Name    | Type    | Mandatory  | Default Value | Description          |
| ------ | ------ | ---- | ---- | ------------ |
| filter | string | Yes   | -    | Functions that accept various filter effects.|


### getTransform

getTransform(): Matrix2D

Obtains the current transformation matrix being applied to the context. This API is a void API.


### resetTransform

resetTransform(): void

Resets the current transform to the identity matrix. This API is a void API.


### direction

direction(direction: CanvasDirection): void

Sets the text direction for drawing text. This API is a void API.


Z
zengyawen 已提交
1824
### rotate
Z
zengyawen 已提交
1825

E
ester.zhou 已提交
1826
rotate(angle: number): void
Z
zengyawen 已提交
1827 1828 1829

Rotates a canvas clockwise around its coordinate axes.

E
ester.zhou 已提交
1830 1831
 **Parameters**

E
esterzhou 已提交
1832 1833
| Name   | Type    | Mandatory  | Default Value | Description                                      |
| ----- | ------ | ---- | ---- | ---------------------------------------- |
E
ester.zhou 已提交
1834
| angle | number | Yes   | 0    | Clockwise rotation angle. You can use **Math.PI / 180** to convert the angle to a radian.|
Z
zengyawen 已提交
1835

E
ester.zhou 已提交
1836
 **Example**
H
HelloCrease 已提交
1837

1838
  ```ts
1839
  // xxx.ets
Z
zengyawen 已提交
1840 1841 1842 1843 1844 1845
  @Entry
  @Component
  struct Rotate {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1846

Z
zengyawen 已提交
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.rotate(45 * Math.PI / 180) // Rotate the rectangle 45 degrees
            this.offContext.fillRect(70, 20, 50, 50)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1859
      }
Z
zengyawen 已提交
1860 1861
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1862
    }
Z
zengyawen 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
  }
  ```

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


### scale

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

E
ester.zhou 已提交
1873
Scales the canvas based on scale factors.
Z
zengyawen 已提交
1874

E
ester.zhou 已提交
1875
 **Parameters**
Z
zengyawen 已提交
1876

E
ester.zhou 已提交
1877 1878 1879 1880 1881 1882
| Name  | Type    | Mandatory  | Default Value | Description         |
| ---- | ------ | ---- | ---- | ----------- |
| x    | number | Yes   | 0    | Horizontal scale factor.|
| y    | number | Yes   | 0    | Vertical scale factor.|

 **Example**
H
HelloCrease 已提交
1883

1884
  ```ts
1885
  // xxx.ets
Z
zengyawen 已提交
1886 1887 1888 1889 1890 1891
  @Entry
  @Component
  struct Scale {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1892

Z
zengyawen 已提交
1893 1894 1895 1896 1897 1898 1899
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
esterzhou 已提交
1900 1901
            this.offContext.lineWidth = 3
            this.offContext.strokeRect(30, 30, 50, 50)
Z
zengyawen 已提交
1902
            this.offContext.scale(2, 2) // Scale to 200%
E
esterzhou 已提交
1903
            this.offContext.strokeRect(30, 30, 50, 50)
Z
zengyawen 已提交
1904 1905 1906
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1907
      }
Z
zengyawen 已提交
1908 1909
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1910
    }
Z
zengyawen 已提交
1911 1912
  }
  ```
Z
zengyawen 已提交
1913

Z
zengyawen 已提交
1914
  ![en-us_image_0000001211898516](figures/en-us_image_0000001211898516.png)
Z
zengyawen 已提交
1915 1916


Z
zengyawen 已提交
1917
### transform
Z
zengyawen 已提交
1918

E
ester.zhou 已提交
1919
transform(a: number, b: number, c: number, d: number, e: number, f: number): void
Z
zengyawen 已提交
1920 1921 1922

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.

1923
> **NOTE**
E
ester.zhou 已提交
1924
>
Z
zengyawen 已提交
1925
> The following formulas calculate coordinates of the transformed graph. **x** and **y** represent coordinates before transformation, and **x'** and **y'** represent coordinates after transformation.
H
HelloCrease 已提交
1926
>
Z
zengyawen 已提交
1927
> - x' = scaleX \* x + skewY \* y + translateX
H
HelloCrease 已提交
1928
>
Z
zengyawen 已提交
1929 1930
> - y' = skewX \* x + scaleY \* y + translateY

E
ester.zhou 已提交
1931
**Parameters**
Z
zengyawen 已提交
1932

E
esterzhou 已提交
1933 1934 1935 1936 1937 1938 1939 1940
| Name  | Type    | Mandatory  | Default Value | Description                  |
| ---- | ------ | ---- | ---- | -------------------- |
| a    | number | Yes   | 0    | X-axis scale.    |
| b    | number | Yes   | 0    | X-axis skew.     |
| c    | number | Yes   | 0    | Y-axis skew.     |
| d    | number | Yes   | 0    | Y-axis scale.    |
| e    | number | Yes   | 0    | X-axis translation.|
| f    | number | Yes   | 0    | Y-axis translation.|
E
ester.zhou 已提交
1941 1942

 **Example**
H
HelloCrease 已提交
1943

1944
  ```ts
1945
  // xxx.ets
Z
zengyawen 已提交
1946 1947 1948 1949 1950 1951
  @Entry
  @Component
  struct Transform {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1952

Z
zengyawen 已提交
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.fillStyle = 'rgb(0,0,0)'
            this.offContext.fillRect(0, 0, 100, 100)
            this.offContext.transform(1, 0.5, -0.5, 1, 10, 10)
            this.offContext.fillStyle = 'rgb(255,0,0)'
            this.offContext.fillRect(0, 0, 100, 100)
            this.offContext.transform(1, 0.5, -0.5, 1, 10, 10)
            this.offContext.fillStyle = 'rgb(0,0,255)'
            this.offContext.fillRect(0, 0, 100, 100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1971
      }
Z
zengyawen 已提交
1972 1973
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1974
    }
Z
zengyawen 已提交
1975 1976 1977 1978 1979 1980 1981 1982
  }
  ```

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


### setTransform

E
ester.zhou 已提交
1983
setTransform(a: number, b: number, c: number, d: number, e: number, f: number): void
Z
zengyawen 已提交
1984

E
esterzhou 已提交
1985
Resets the existing transformation matrix and creates a new transformation matrix by using the same parameters as the **transform()** API.
Z
zengyawen 已提交
1986

E
ester.zhou 已提交
1987 1988
**Parameters**

E
esterzhou 已提交
1989 1990 1991 1992 1993 1994 1995 1996
| Name  | Type    | Mandatory  | Default Value | Description                  |
| ---- | ------ | ---- | ---- | -------------------- |
| a    | number | Yes   | 0    | X-axis scale.    |
| b    | number | Yes   | 0    | X-axis skew.     |
| c    | number | Yes   | 0    | Y-axis skew.     |
| d    | number | Yes   | 0    | Y-axis scale.    |
| e    | number | Yes   | 0    | X-axis translation.|
| f    | number | Yes   | 0    | Y-axis translation.|
Z
zengyawen 已提交
1997

E
ester.zhou 已提交
1998
 **Example**
H
HelloCrease 已提交
1999

2000
  ```ts
2001
  // xxx.ets
Z
zengyawen 已提交
2002 2003 2004 2005 2006 2007
  @Entry
  @Component
  struct SetTransform {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
2008

Z
zengyawen 已提交
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.fillStyle = 'rgb(255,0,0)'
            this.offContext.fillRect(0, 0, 100, 100)
            this.offContext.setTransform(1,0.5, -0.5, 1, 10, 10)
            this.offContext.fillStyle = 'rgb(0,0,255)'
            this.offContext.fillRect(0, 0, 100, 100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2024
      }
Z
zengyawen 已提交
2025 2026
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2027
    }
Z
zengyawen 已提交
2028 2029
  }
  ```
Z
zengyawen 已提交
2030

E
esterzhou 已提交
2031
  ![en-us_image_0000001193872526](figures/en-us_image_0000001193872526.png)
Z
zengyawen 已提交
2032 2033


G
Gloria 已提交
2034 2035
### translate

Z
zengyawen 已提交
2036
### translate
Z
zengyawen 已提交
2037

Z
zengyawen 已提交
2038
translate(x: number, y: number): void
Z
zengyawen 已提交
2039 2040 2041

Moves the origin of the coordinate system.

E
ester.zhou 已提交
2042 2043
 **Parameters**

E
esterzhou 已提交
2044
| Name  | Type    | Mandatory  | Default Value | Description      |
E
ester.zhou 已提交
2045 2046 2047
| ---- | ------ | ---- | ---- | -------- |
| x    | number | Yes   | 0    | X-axis translation.|
| y    | number | Yes   | 0    | Y-axis translation.|
Z
zengyawen 已提交
2048

E
ester.zhou 已提交
2049
 **Example**
H
HelloCrease 已提交
2050

2051
  ```ts
2052
  // xxx.ets
Z
zengyawen 已提交
2053 2054 2055 2056 2057 2058
  @Entry
  @Component
  struct Translate {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
2059

Z
zengyawen 已提交
2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.fillRect(10, 10, 50, 50)
            this.offContext.translate(70, 70)
            this.offContext.fillRect(10, 10, 50, 50)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
    }
  }
  ```

E
esterzhou 已提交
2080
  ![en-us_image_0000001238832413](figures/en-us_image_0000001238832413.png)
Z
zengyawen 已提交
2081 2082 2083 2084


### drawImage

2085
drawImage(image: ImageBitmap | PixelMap, dx: number, dy: number): void
Z
zengyawen 已提交
2086

E
ester.zhou 已提交
2087
drawImage(image: ImageBitmap | PixelMap, dx: number, dy: number, dw: number, dh: number): void
Z
zengyawen 已提交
2088

E
ester.zhou 已提交
2089
drawImage(image: ImageBitmap | PixelMap, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number):void
Z
zengyawen 已提交
2090

E
ester.zhou 已提交
2091
Draws an image on the canvas.
Z
zengyawen 已提交
2092

E
ester.zhou 已提交
2093
 **Parameters**
Z
zengyawen 已提交
2094

E
esterzhou 已提交
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
| Name   | Type                                      | Mandatory  | Default Value | Description                           |
| ----- | ---------------------------------------- | ---- | ---- | ----------------------------- |
| image | [ImageBitmap](ts-components-canvas-imagebitmap.md) or [PixelMap](../apis/js-apis-image.md#pixelmap7)| Yes   | null | Image resource. For details, see **ImageBitmap** or **PixelMap**.|
| sx    | number                                   | No   | 0    | X-coordinate of the upper left corner of the rectangle used to crop the source image.         |
| sy    | number                                   | No   | 0    | Y-coordinate of the upper left corner of the rectangle used to crop the source image.         |
| sw    | number                                   | No   | 0    | Target width to crop the source image.               |
| sh    | number                                   | No   | 0    | Target height to crop the source image.               |
| dx    | number                                   | Yes   | 0    | X-coordinate of the upper left corner of the drawing area on the canvas.               |
| dy    | number                                   | Yes   | 0    | Y-coordinate of the upper left corner of the drawing area on the canvas.         |
| dw    | number                                   | No   | 0    | Width of the drawing area.                     |
| dh    | number                                   | No   | 0    | Height of the drawing area.                     |
Z
zengyawen 已提交
2106

E
ester.zhou 已提交
2107 2108

 **Example**
H
HelloCrease 已提交
2109

2110
  ```ts
2111
  // xxx.ets
Z
zengyawen 已提交
2112 2113
  @Entry
  @Component
E
esterzhou 已提交
2114
  struct DrawImage {
Z
zengyawen 已提交
2115 2116 2117 2118 2119 2120 2121
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private img:ImageBitmap = new ImageBitmap("common/images/icon.jpg")
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
Z
zengyawen 已提交
2122 2123
        .width('100%')
        .height('100%')
Z
zengyawen 已提交
2124 2125 2126 2127 2128 2129
        .backgroundColor('#ffff00')
        .onReady(() => {
            this.offContext.drawImage( this.img,0,0,400,200)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
2130
      }
Z
zengyawen 已提交
2131 2132
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2133
    }
Z
zengyawen 已提交
2134 2135
  }
  ```
Z
zengyawen 已提交
2136

Z
zengyawen 已提交
2137
  ![en-us_image_0000001256978377](figures/en-us_image_0000001256978377.png)
Z
zengyawen 已提交
2138 2139


Z
zengyawen 已提交
2140
### createImageData
Z
zengyawen 已提交
2141

E
ester.zhou 已提交
2142 2143
createImageData(sw: number, sh: number): ImageData

E
esterzhou 已提交
2144
Creates an **[ImageData](ts-components-canvas-imagedata.md)** object with the specified dimensions.
Z
zengyawen 已提交
2145

E
ester.zhou 已提交
2146
 **Parameters**
Z
zengyawen 已提交
2147

E
esterzhou 已提交
2148 2149 2150 2151
| Name  | Type    | Mandatory  | Default Value  | Description           |
| ---- | ------ | ---- | ---- | ------------- |
| sw   | number | Yes   | 0    | Width of the **ImageData** object.|
| sh   | number | Yes   | 0    | Height of the **ImageData** object.|
Z
zengyawen 已提交
2152 2153


E
ester.zhou 已提交
2154 2155
createImageData(imageData: ImageData): ImageData

E
esterzhou 已提交
2156
Creates an **[ImageData](ts-components-canvas-imagedata.md)** object by copying an existing **ImageData** object.
E
ester.zhou 已提交
2157 2158

 **Parameters**
Z
zengyawen 已提交
2159

E
ester.zhou 已提交
2160 2161
| Name       | Type                                      | Mandatory  | Default Value  | Description              |
| --------- | ---------------------------------------- | ---- | ---- | ---------------- |
E
esterzhou 已提交
2162
| imagedata | [ImageData](ts-components-canvas-imagedata.md) | Yes   | null | **ImageData** object to copy.|
Z
zengyawen 已提交
2163

E
ester.zhou 已提交
2164 2165
 **Return value**

E
esterzhou 已提交
2166 2167 2168
| Type                                      | Description           |
| ---------------------------------------- | ------------- |
| [ImageData](ts-components-canvas-imagedata.md) | New **ImageData** object.|
Z
zengyawen 已提交
2169

2170 2171 2172 2173 2174
### getPixelMap

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

Obtains the **[PixelMap](../apis/js-apis-image.md#pixelmap7)** object created with the pixels within the specified area on the canvas.
E
ester.zhou 已提交
2175 2176 2177

 **Parameters**

E
esterzhou 已提交
2178 2179 2180 2181 2182 2183
| Name  | Type    | Mandatory  | Default Value | Description             |
| ---- | ------ | ---- | ---- | --------------- |
| sx   | number | Yes   | 0    | X-coordinate of the upper left corner of the output area.|
| sy   | number | Yes   | 0    | Y-coordinate of the upper left corner of the output area.|
| sw   | number | Yes   | 0    | Width of the output area.    |
| sh   | number | Yes   | 0    | Height of the output area.    |
E
ester.zhou 已提交
2184 2185 2186

**Return value**

E
esterzhou 已提交
2187 2188 2189
| Type                                      | Description          |
| ---------------------------------------- | ------------ |
| [PixelMap](../apis/js-apis-image.md#pixelmap7) | **PixelMap** object.|
E
ester.zhou 已提交
2190

Z
zengyawen 已提交
2191 2192 2193

### getImageData

E
ester.zhou 已提交
2194
getImageData(sx: number, sy: number, sw: number, sh: number): ImageData
Z
zengyawen 已提交
2195

E
esterzhou 已提交
2196
Obtains the **[ImageData](ts-components-canvas-imagedata.md)** object created with the pixels within the specified area on the canvas.
Z
zengyawen 已提交
2197

E
ester.zhou 已提交
2198 2199
 **Parameters**

E
esterzhou 已提交
2200
| Name  | Type    | Mandatory  | Default Value | Description             |
E
ester.zhou 已提交
2201 2202 2203 2204 2205 2206 2207 2208
| ---- | ------ | ---- | ---- | --------------- |
| sx   | number | Yes   | 0    | X-coordinate of the upper left corner of the output area.|
| sy   | number | Yes   | 0    | Y-coordinate of the upper left corner of the output area.|
| sw   | number | Yes   | 0    | Width of the output area.    |
| sh   | number | Yes   | 0    | Height of the output area.    |

   **Return value**

E
esterzhou 已提交
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246
| Type                                      | Description           |
| ---------------------------------------- | ------------- |
| [ImageData](ts-components-canvas-imagedata.md) | New **ImageData** object.|


**Example**

  ```ts
  // xxx.ets
@Entry
@Component
struct GetImageData {
  private settings: RenderingContextSettings = new RenderingContextSettings(true);
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  private img:ImageBitmap = new ImageBitmap("/common/images/1234.png")

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.drawImage(this.img,0,0,130,130);
          var imagedata = this.offContext.getImageData(50,50,130,130);
          this.offContext.putImageData(imagedata,150,150);
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
        })
    }
    .width('100%')
    .height('100%')
  }
}
  ```

  ![en-us_image_000000127777780](figures/en-us_image_000000127777780.png)
Z
zengyawen 已提交
2247 2248 2249 2250


### putImageData

E
ester.zhou 已提交
2251 2252 2253
putImageData(imageData: Object, dx: number, dy: number): void

putImageData(imageData: Object, dx: number, dy: number, dirtyX: number, dirtyY: number, dirtyWidth?: number, dirtyHeight: number): void
Z
zengyawen 已提交
2254

E
esterzhou 已提交
2255
Puts an **[ImageData](ts-components-canvas-imagedata.md)** object onto a rectangular area on the canvas.
Z
zengyawen 已提交
2256

E
ester.zhou 已提交
2257 2258
 **Parameters**

E
esterzhou 已提交
2259
| Name         | Type    | Mandatory  | Default Value         | Description                           |
E
ester.zhou 已提交
2260 2261 2262 2263 2264 2265 2266 2267
| ----------- | ------ | ---- | ------------ | ----------------------------- |
| imagedata   | Object | Yes   | null         | **ImageData** object with pixels to put onto the canvas.           |
| dx          | number | Yes   | 0            | X-axis offset of the rectangular area on the canvas.               |
| dy          | number | Yes   | 0            | Y-axis offset of the rectangular area on the canvas.               |
| dirtyX      | number | No   | 0            | X-axis offset of the upper left corner of the rectangular area relative to that of the source image.|
| dirtyY      | number | No   | 0            | Y-axis offset of the upper left corner of the rectangular area relative to that of the source image.|
| dirtyWidth  | number | No   | Width of the **ImageData** object| Width of the rectangular area to crop the source image.              |
| dirtyHeight | number | No   | Height of the **ImageData** object| Height of the rectangular area to crop the source image.              |
Z
zengyawen 已提交
2268

E
ester.zhou 已提交
2269
 **Example**
H
HelloCrease 已提交
2270

2271
  ```ts
2272
  // xxx.ets
Z
zengyawen 已提交
2273 2274 2275 2276 2277 2278 2279 2280 2281
  @Entry
  @Component
  struct PutImageData {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
Z
zengyawen 已提交
2282 2283 2284
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
Z
zengyawen 已提交
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
          .onReady(() =>{
            var imageData = this.offContext.createImageData(100, 100)
            for (var i = 0; i < imageData.data.length; i += 4) {
              imageData.data[i + 0] = 255
              imageData.data[i + 1] = 0
              imageData.data[i + 2] = 255
              imageData.data[i + 3] = 255
            }
            this.offContext.putImageData(imageData, 10, 10)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
Z
zengyawen 已提交
2296 2297
          })
      }
Z
zengyawen 已提交
2298 2299
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2300
    }
Z
zengyawen 已提交
2301 2302
  }
  ```
Z
zengyawen 已提交
2303

Z
zengyawen 已提交
2304
  ![en-us_image_0000001212058496](figures/en-us_image_0000001212058496.png)
Z
zengyawen 已提交
2305

E
ester.zhou 已提交
2306 2307 2308 2309 2310 2311 2312 2313
### setLineDash

setLineDash(segments: number[]): void

Sets the dash line style.

**Parameters**

E
esterzhou 已提交
2314 2315
| Name      | Type      | Description                 |
| -------- | -------- | ------------------- |
E
ester.zhou 已提交
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346
| segments | number[] | An array of numbers that specify distances to alternately draw a line and a gap.|

**Example**

  ```ts
@Entry
@Component
struct SetLineDash {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.arc(100, 75, 50, 0, 6.28)
          this.offContext.setLineDash([10,20])
          this.offContext.stroke();
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
  ```
E
esterzhou 已提交
2347
  ![en-us_image_000000127777772](figures/en-us_image_000000127777772.png)
E
ester.zhou 已提交
2348 2349 2350 2351 2352 2353 2354 2355 2356 2357


### getLineDash

getLineDash(): number[]

Obtains the dash line style.

**Return value**

E
esterzhou 已提交
2358 2359
| Type      | Description                      |
| -------- | ------------------------ |
E
ester.zhou 已提交
2360 2361 2362 2363 2364 2365
| number[] | An array describing the interval of alternate line segments and length of spacing.|

**Example**

  ```ts
  // xxx.ets
E
esterzhou 已提交
2366 2367 2368 2369
@Entry
@Component
struct OffscreenCanvasGetLineDash {
  @State message: string = 'Hello World'
E
ester.zhou 已提交
2370 2371 2372
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            console.error('before getlinedash clicked')
            let res = this.offContext.getLineDash()
            console.error(JSON.stringify(res))
          })
E
ester.zhou 已提交
2384 2385 2386 2387
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
E
esterzhou 已提交
2388
          .onReady(() => {
E
ester.zhou 已提交
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
            this.offContext.arc(100, 75, 50, 0, 6.28)
            this.offContext.setLineDash([10,20])
            this.offContext.stroke();
            let res = this.offContext.getLineDash()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
    }
E
esterzhou 已提交
2399
    .height('100%')
E
ester.zhou 已提交
2400
  }
E
esterzhou 已提交
2401
}
E
ester.zhou 已提交
2402
  ```
E
esterzhou 已提交
2403
![en-us_image_000000127777778](figures/en-us_image_000000127777778.png) 
E
ester.zhou 已提交
2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428



### toDataURL

toDataURL(type?: string, quality?: number): string

Generates a URL containing image display information.

**Parameters**

| Name    | Type  | Mandatory  | Description                                      |
| ------- | ------ | ---- | ---------------------------------------- |
| type    | string | No   | Image format. The default value is **image/png**.           |
| quality | number | No   | Image quality, which ranges from 0 to 1, when the image format is **image/jpeg** or **image/webp**. If the set value is beyond the value range, the default value **0.92** is used.|

**Return value**

| Type    | Description       |
| ------ | --------- |
| string | Image URL.|

**Example**

  ```ts
E
esterzhou 已提交
2429
// xxx.ets
E
ester.zhou 已提交
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453
@Entry
@Component
struct ToDataURL {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          var dataURL = this.offContext.toDataURL();
        })
    }
    .width('100%')
    .height('100%')
  }
}
  ```


E
esterzhou 已提交
2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466
### imageSmoothingQuality

imageSmoothingQuality(quality: imageSmoothingQuality)

Sets the quality of image smoothing. This API is a void API.

 **Parameters**

| Name     | Type                   | Description                                      |
| ------- | --------------------- | ---------------------------------------- |
| quality | imageSmoothingQuality | Quality of image smoothing. The value can be **'low'**, **'medium'**,or **'high'**.|


E
ester.zhou 已提交
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476
### transferToImageBitmap

transferToImageBitmap(): ImageBitmap

Creates an **ImageBitmap** object on the most recently rendered image of the **OffscreenCanvas**.

**Return value**

| Type                                      | Description             |
| ---------------------------------------- | --------------- |
E
esterzhou 已提交
2477
| [ImageBitmap](ts-components-canvas-imagebitmap.md) | Pixel data rendered on the **OffscreenCanvas**.|
E
ester.zhou 已提交
2478 2479 2480 2481 2482 2483 2484 2485


 **Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
E
esterzhou 已提交
2486
  struct PutImageData {
E
ester.zhou 已提交
2487 2488 2489
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
2490

E
ester.zhou 已提交
2491 2492 2493 2494 2495 2496 2497
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
esterzhou 已提交
2498 2499 2500 2501 2502 2503 2504 2505
            var imageData = this.offContext.createImageData(100, 100)
            for (var i = 0; i < imageData.data.length; i += 4) {
              imageData.data[i + 0] = 255
              imageData.data[i + 1] = 0
              imageData.data[i + 2] = 255
              imageData.data[i + 3] = 255
            }
            this.offContext.putImageData(imageData, 10, 10)
E
ester.zhou 已提交
2506 2507 2508 2509 2510 2511 2512 2513 2514
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
    }
  }
  ```
E
esterzhou 已提交
2515
![en-us_image_0000001238952387](figures/en-us_image_0000001238952387.png) 
Z
zengyawen 已提交
2516

Z
zengyawen 已提交
2517
### restore
Z
zengyawen 已提交
2518

Z
zengyawen 已提交
2519
restore(): void
Z
zengyawen 已提交
2520 2521 2522

Restores the saved drawing context.

E
ester.zhou 已提交
2523
 **Example**
H
HelloCrease 已提交
2524

2525
  ```ts
2526
  // xxx.ets
E
esterzhou 已提交
2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548
@Entry
@Component
struct CanvasExample {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.save(); // save the default state
          this.offContext.fillStyle = "green";
          this.offContext.fillRect(20, 20, 100, 100);
          this.offContext.restore(); // restore to the default state
          this.offContext.fillRect(150, 75, 100, 100);
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
2549
    }
E
esterzhou 已提交
2550 2551
    .width('100%')
    .height('100%')
Z
zengyawen 已提交
2552
  }
E
esterzhou 已提交
2553
}
Z
zengyawen 已提交
2554
  ```
E
esterzhou 已提交
2555
![en-us_image_000000127777781](figures/en-us_image_000000127777781.png) 
Z
zengyawen 已提交
2556 2557


Z
zengyawen 已提交
2558
### save
Z
zengyawen 已提交
2559

Z
zengyawen 已提交
2560
save(): void
Z
zengyawen 已提交
2561 2562 2563

Saves the current drawing context.

E
ester.zhou 已提交
2564
 **Example**
H
HelloCrease 已提交
2565

2566
  ```ts
2567
  // xxx.ets
E
esterzhou 已提交
2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588
@Entry
@Component
struct CanvasExample {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.save(); // save the default state
          this.offContext.fillStyle = "green";
          this.offContext.fillRect(20, 20, 100, 100);
          this.offContext.restore(); // restore to the default state
          this.offContext.fillRect(150, 75, 100, 100);
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
Z
zengyawen 已提交
2589
        })
Z
zengyawen 已提交
2590
    }
E
esterzhou 已提交
2591 2592
    .width('100%')
    .height('100%')
Z
zengyawen 已提交
2593
  }
E
esterzhou 已提交
2594
}
Z
zengyawen 已提交
2595
  ```
E
esterzhou 已提交
2596
![en-us_image_000000127777781](figures/en-us_image_000000127777781.png) 
Z
zengyawen 已提交
2597 2598


Z
zengyawen 已提交
2599
### createLinearGradient
Z
zengyawen 已提交
2600

Z
zengyawen 已提交
2601
createLinearGradient(x0: number, y0: number, x1: number, y1: number): void
Z
zengyawen 已提交
2602 2603 2604

Creates a linear gradient.

E
ester.zhou 已提交
2605 2606 2607 2608 2609 2610 2611 2612
 **Parameters**

| Name  | Type    | Mandatory  | Default Value | Description      |
| ---- | ------ | ---- | ---- | -------- |
| x0   | number | Yes   | 0    | X-coordinate of the start point.|
| y0   | number | Yes   | 0    | Y-coordinate of the start point.|
| x1   | number | Yes   | 0    | X-coordinate of the end point.|
| y1   | number | Yes   | 0    | Y-coordinate of the end point.|
Z
zengyawen 已提交
2613

E
ester.zhou 已提交
2614
 **Example**
H
HelloCrease 已提交
2615

2616
  ```ts
2617
  // xxx.ets
Z
zengyawen 已提交
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632
  @Entry
  @Component
  struct CreateLinearGradient {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var grad = this.offContext.createLinearGradient(50,0, 300,100)
H
HelloCrease 已提交
2633 2634 2635
            grad.addColorStop(0.0, 'red')
            grad.addColorStop(0.5, 'white')
            grad.addColorStop(1.0, 'green')
Z
zengyawen 已提交
2636 2637 2638 2639 2640
            this.offContext.fillStyle = grad
            this.offContext.fillRect(0, 0, 500, 500)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2641
      }
Z
zengyawen 已提交
2642 2643
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2644
    }
Z
zengyawen 已提交
2645 2646 2647 2648
  }
  ```

  ![en-us_image_0000001212378434](figures/en-us_image_0000001212378434.png)
Z
zengyawen 已提交
2649 2650


Z
zengyawen 已提交
2651
### createRadialGradient
Z
zengyawen 已提交
2652

Z
zengyawen 已提交
2653
createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number): void
Z
zengyawen 已提交
2654 2655 2656

Creates a linear gradient.

E
ester.zhou 已提交
2657 2658 2659 2660 2661 2662 2663 2664 2665 2666
  **Parameters**

| Name  | Type    | Mandatory  | Default Value | Description               |
| ---- | ------ | ---- | ---- | ----------------- |
| x0   | number | Yes   | 0    | X-coordinate of the center of the start circle.        |
| y0   | number | Yes   | 0    | Y-coordinate of the center of the start circle.        |
| r0   | number | Yes   | 0    | Radius of the start circle, which must be a non-negative finite number.|
| x1   | number | Yes   | 0    | X-coordinate of the center of the end circle.        |
| y1   | number | Yes   | 0    | Y-coordinate of the center of the end circle.        |
| r1   | number | Yes   | 0    | Radius of the end circle, which must be a non-negative finite number.|
Z
zengyawen 已提交
2667

E
ester.zhou 已提交
2668
  **Example** 
H
HelloCrease 已提交
2669

2670
  ```ts
2671
  // xxx.ets
Z
zengyawen 已提交
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
  @Entry
  @Component
  struct CreateRadialGradient {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var grad = this.offContext.createRadialGradient(200,200,50, 200,200,200)
H
HelloCrease 已提交
2687 2688 2689
            grad.addColorStop(0.0, 'red')
            grad.addColorStop(0.5, 'white')
            grad.addColorStop(1.0, 'green')
Z
zengyawen 已提交
2690 2691 2692 2693 2694
            this.offContext.fillStyle = grad
            this.offContext.fillRect(0, 0, 500, 500)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2695
      }
Z
zengyawen 已提交
2696 2697
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2698
    }
Z
zengyawen 已提交
2699 2700
  }
  ```
Z
zengyawen 已提交
2701

Z
zengyawen 已提交
2702
  ![en-us_image_0000001212218480](figures/en-us_image_0000001212218480.png)
Z
zengyawen 已提交
2703 2704


Z
zengyawen 已提交
2705
## CanvasPattern
Z
zengyawen 已提交
2706

E
ester.zhou 已提交
2707
Defines an object created using the **[createPattern](#createpattern)** API.