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


4
> **NOTE**<br>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 已提交
5 6 7 8 9 10 11 12 13 14


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.


## APIs

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

- Parameters
H
HelloCrease 已提交
15 16 17 18 19
    | Name    | Type                                     | Mandatory | Default Value | 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 已提交
20 21 22 23


## Attributes

H
HelloCrease 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| Name                                     | Type                                     | Default Value                   | Description                              |
| ---------------------------------------- | ---------------------------------------- | ------------------------------- | ---------------------------------------- |
| [fillStyle](#fillstyle)                  | &lt;color&gt; \| [CanvasGradient](ts-components-canvas-canvasgradient.md) \| [CanvasPattern](#canvaspattern) | -                               | Style used to fill an area.<br/>- When the type is **&lt;color&gt;**, this attribute indicates the fill color.<br/>- When the type is **CanvasGradient**, this attribute indicates a gradient object, which is created using the [createLinearGradient](#createlineargradient) method.<br/>- When the type is **CanvasPattern**, this attribute indicates a pattern, which is created using the [createPattern](#createpattern) method. |
| [lineWidth](#linewidth)                  | number                                   | -                               | Line width.                              |
| [strokeStyle](#strokestyle)              | &lt;color&gt; \| [CanvasGradient](ts-components-canvas-canvasgradient.md) \| [CanvasPattern](#canvaspattern) | -                               | Stroke style.<br/>- When the type is **&lt;color&gt;**, this attribute indicates the stroke color.<br/>- When the type is **CanvasGradient**, this attribute indicates a gradient object, which is created using the [createLinearGradient](#createlineargradient) method.<br/>- When the type is **CanvasPattern**, this attribute indicates a pattern, which is created using the [createPattern](#createpattern) method. |
| [lineCap](#linecap)                      | string                                   | 'butt'                          | 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 by adding a box with an equal width and half the height of the line's thickness. |
| [lineJoin](#linejoin)                    | string                                   | 'miter'                         | Style of the shape used to join line segments. The options are as follows:<br/>- **'round'**: The shape used to join line segments is a rounded corner with the radius equal to the line width.<br/>- **'bevel'**: The shape used to join line segments is a beveled corner. The rectangular corner of each line is independent.<br/>- **'miter'**: The shape used to join line segments is a mitered corner. The corner is formed by extending the outside edges of the lines until they meet. You can adjust the effect of this attribute using **miterLimit**. |
| [miterLimit](#miterlimit)                | number                                   | 10                              | Maximum miter length. The miter length is the distance between the inner corner and the outer corner where two lines meet. |
| [font](#font)                            | string                                   | 'normal normal 14px sans-serif' | Font style.<br/>Syntax: ctx.font='font-size font-family'<br/>- (Optional) **font-size**: font size and row height. The unit can only be px.<br/>- (Optional) **font-family**: font family.<br/>Syntax: ctx.font='font-style font-weight font-size font-family'<br/>- (Optional) **font-style**: specifies the 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 px.<br/>- (Optional) **font-family**: font family. Available values are **'sans-serif'**, **'serif'**, and **'monospace'**. |
| [textAlign](#textalign)                  | string                                   | 'left'                          | 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/>> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>> In the **ltr** layout mode, the value **start** equals **left**. In the **rtl** layout mode, the value **start** equals **right**. |
| [textBaseline](#textbaseline)            | string                                   | 'alphabetic'                    | 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. |
| [globalAlpha](#globalalpha)              | number                                   | -                               | Opacity. **0.0**: completely transparent; **1.0**: completely opaque. |
| [lineDashOffset](#linedashoffset)        | number                                   | 0.0                             | Offset of the dashed line. The precision is float. |
| [globalCompositeOperation](#globalcompositeoperation) | string                                   | 'source-over'                   | 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'**. |
| [shadowBlur](#shadowblur)                | number                                   | 0.0                             | Blur level during shadow drawing. A larger value indicates a more blurred effect. The precision is float. |
| [shadowColor](#shadowcolor)              | &lt;color&gt;                            | -                               | Shadow color.                            |
| [shadowOffsetX](#shadowoffsetx)          | number                                   | -                               | X-axis shadow offset relative to the original object. |
| [shadowOffsetY](#shadowoffsety)          | number                                   | -                               | Y-axis shadow offset relative to the original object. |
| [imageSmoothingEnabled](#imagesmoothingenabled) | boolean                                  | true                            | Whether to adjust the image smoothness during image drawing. The value **true** means to enable this feature, and **false** means the opposite. |
| imageSmoothingQuality                    | string                                   | 'low'                           | Image smoothness. The value can be **'low'**, **'medium'**, or **'high'**. |
E
esterzhou 已提交
44

45
> **NOTE**
Z
zengyawen 已提交
46 47 48 49 50
> The value of the **&lt;color&gt;** type can be in 'rgb(255, 255, 255)', 'rgba(255, 255, 255, 1.0)', or '\#FFFFFF' format.


### fillStyle

Z
zengyawen 已提交
51

52 53
```ts
// xxx.ets
Z
zengyawen 已提交
54 55 56
@Entry
@Component
struct FillStyleExample {
E
ester.zhou 已提交
57
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  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 已提交
80 81 82 83
![en-us_image_0000001211898510](figures/en-us_image_0000001211898510.png)


### lineWidth
Z
zengyawen 已提交
84 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 121
```ts
// xxx.ets
Z
zengyawen 已提交
122 123 124
@Entry
@Component
struct StrokeStyleExample {
E
ester.zhou 已提交
125
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  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 已提交
150 151 152 153 154
![en-us_image_0000001257058429](figures/en-us_image_0000001257058429.png)


### lineCap

Z
zengyawen 已提交
155

156 157
```ts
// xxx.ets
Z
zengyawen 已提交
158 159 160
@Entry
@Component
struct LineCapExample {
E
ester.zhou 已提交
161
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  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 已提交
188 189 190 191
![en-us_image_0000001256858427](figures/en-us_image_0000001256858427.png)


### lineJoin
Z
zengyawen 已提交
192 193


194 195
```ts
// xxx.ets
Z
zengyawen 已提交
196 197 198
@Entry
@Component
struct LineJoinExample {
E
ester.zhou 已提交
199
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  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 已提交
227 228 229 230
![en-us_image_0000001256858429](figures/en-us_image_0000001256858429.png)


### miterLimit
Z
zengyawen 已提交
231 232


233 234
```ts
// xxx.ets
Z
zengyawen 已提交
235 236 237
@Entry
@Component
struct MiterLimit {
E
ester.zhou 已提交
238
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  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 已提交
266 267 268 269
![en-us_image_0000001212218472](figures/en-us_image_0000001212218472.png)


### font
Z
zengyawen 已提交
270 271


272 273
```ts
// xxx.ets
Z
zengyawen 已提交
274 275
@Entry
@Component
276
struct Fonts {
E
ester.zhou 已提交
277
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
  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 已提交
300 301 302 303
![en-us_image_0000001211898508](figures/en-us_image_0000001211898508.png)


### textAlign
Z
zengyawen 已提交
304 305


306 307
```ts
// xxx.ets
Z
zengyawen 已提交
308 309
@Entry
@Component
E
ester.zhou 已提交
310 311
struct CanvasExample {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
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 342 343 344 345 346 347 348
  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 已提交
349 350 351 352
![en-us_image_0000001257138377](figures/en-us_image_0000001257138377.png)


### textBaseline
Z
zengyawen 已提交
353 354


355 356
```ts
// xxx.ets
Z
zengyawen 已提交
357 358 359
@Entry
@Component
struct TextBaseline {
E
ester.zhou 已提交
360
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
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 390 391 392 393 394 395 396 397
  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 已提交
398 399 400 401
![en-us_image_0000001256978375](figures/en-us_image_0000001256978375.png)


### globalAlpha
Z
zengyawen 已提交
402 403


404 405
```ts
// xxx.ets
Z
zengyawen 已提交
406 407 408
@Entry
@Component
struct GlobalAlpha {
E
ester.zhou 已提交
409
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
  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 已提交
435 436 437 438
![en-us_image_0000001211898506](figures/en-us_image_0000001211898506.png)


### lineDashOffset
Z
zengyawen 已提交
439 440


441 442
```ts
// xxx.ets
Z
zengyawen 已提交
443 444 445
@Entry
@Component
struct LineDashOffset {
E
ester.zhou 已提交
446
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
  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%')
  }
}
```

Z
zengyawen 已提交
470 471 472 473 474
![en-us_image_0000001212058506](figures/en-us_image_0000001212058506.png)


### globalCompositeOperation

H
HelloCrease 已提交
475 476 477 478 479 480
| 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 the part of the new drawing that is outside of the existing drawing. |
E
esterzhou 已提交
481 482
| destination-over | Displays the existing drawing above the new drawing. |
| destination-atop | Displays the existing drawing on the top of the new drawing. |
H
HelloCrease 已提交
483 484 485 486 487
| destination-in   | Displays the existing drawing inside the new drawing. |
| destination-out  | Displays the part of the existing drawing that is outside of the new drawing. |
| lighter          | Displays both the new drawing and the 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 已提交
488

Z
zengyawen 已提交
489

490 491
```ts
// xxx.ets
Z
zengyawen 已提交
492 493 494
@Entry
@Component
struct GlobalCompositeOperation {
E
ester.zhou 已提交
495
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
  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 已提交
526 527 528 529
![en-us_image_0000001212218474](figures/en-us_image_0000001212218474.png)


### shadowBlur
Z
zengyawen 已提交
530 531


532 533
```ts
// xxx.ets
Z
zengyawen 已提交
534 535 536
@Entry
@Component
struct ShadowBlur {
E
ester.zhou 已提交
537
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
  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 已提交
562 563 564 565
![en-us_image_0000001211898514](figures/en-us_image_0000001211898514.png)


### shadowColor
Z
zengyawen 已提交
566 567


568 569
```ts
// xxx.ets
Z
zengyawen 已提交
570 571 572
@Entry
@Component
struct ShadowColor {
E
ester.zhou 已提交
573
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
  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)
          var image = this.offContext.transferToImageBitmap
()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
599 600 601 602
![en-us_image_0000001212058502](figures/en-us_image_0000001212058502.png)


### shadowOffsetX
Z
zengyawen 已提交
603 604


605 606
```ts
// xxx.ets
Z
zengyawen 已提交
607 608 609
@Entry
@Component
struct ShadowOffsetX {
E
ester.zhou 已提交
610
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
  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 已提交
636 637 638 639
![en-us_image_0000001257138379](figures/en-us_image_0000001257138379.png)


### shadowOffsetY
Z
zengyawen 已提交
640 641


642 643
```ts
// xxx.ets
Z
zengyawen 已提交
644 645 646
@Entry
@Component
struct ShadowOffsetY {
E
ester.zhou 已提交
647
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
  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 已提交
673 674 675 676
![en-us_image_0000001257058427](figures/en-us_image_0000001257058427.png)


### imageSmoothingEnabled
Z
zengyawen 已提交
677 678


679 680
```ts
// xxx.ets
Z
zengyawen 已提交
681 682 683
@Entry
@Component
struct ImageSmoothingEnabled {
E
ester.zhou 已提交
684
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
  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 已提交
708 709
![en-us_image_0000001257138385](figures/en-us_image_0000001257138385.png)

Z
zengyawen 已提交
710

Z
zengyawen 已提交
711
## Methods
Z
zengyawen 已提交
712 713


Z
zengyawen 已提交
714 715 716
### fillRect

fillRect(x: number, y: number, w: number, h: number): void
Z
zengyawen 已提交
717 718 719

Fills a rectangle on the canvas.

Z
zengyawen 已提交
720
- Parameters
H
HelloCrease 已提交
721 722 723 724 725 726
    | 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 已提交
727 728

- Example
H
HelloCrease 已提交
729

730
  ```ts
731
  // xxx.ets
Z
zengyawen 已提交
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
  @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 已提交
750
        }
Z
zengyawen 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
      .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.

- Parameters
H
HelloCrease 已提交
767 768 769 770 771 772
    | 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 已提交
773 774

- Example
H
HelloCrease 已提交
775

776 777
  ```ts
  // xxx.ets
Z
zengyawen 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
  @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)
    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 已提交
795
      }
Z
zengyawen 已提交
796 797
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
798
    }
Z
zengyawen 已提交
799 800
  }
  ```
Z
zengyawen 已提交
801

Z
zengyawen 已提交
802
  ![en-us_image_0000001212378436](figures/en-us_image_0000001212378436.png)
Z
zengyawen 已提交
803 804


Z
zengyawen 已提交
805
### clearRect
Z
zengyawen 已提交
806

Z
zengyawen 已提交
807
clearRect(x: number, y: number, w: number, h: number): void
Z
zengyawen 已提交
808 809 810

Clears the content in a rectangle on the canvas.

Z
zengyawen 已提交
811
- Parameters
H
HelloCrease 已提交
812 813 814 815 816 817
    | 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 已提交
818 819

- Example
H
HelloCrease 已提交
820

821
  ```ts
822
  // xxx.ets
Z
zengyawen 已提交
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
  @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)
    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)'
            this.offContext.fillRect(0,0,500,500)
            this.offContext.clearRect(20,20,150,100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
842
      }
Z
zengyawen 已提交
843 844
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
845
    }
Z
zengyawen 已提交
846 847
  }
  ```
Z
zengyawen 已提交
848

Z
zengyawen 已提交
849
  ![en-us_image_0000001212058500](figures/en-us_image_0000001212058500.png)
Z
zengyawen 已提交
850 851


Z
zengyawen 已提交
852
### fillText
Z
zengyawen 已提交
853

Z
zengyawen 已提交
854
fillText(text: string, x: number, y: number): void
Z
zengyawen 已提交
855 856 857

Draws filled text on the canvas.

Z
zengyawen 已提交
858
- Parameters
H
HelloCrease 已提交
859 860 861 862 863
    | 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. |
Z
zengyawen 已提交
864 865

- Example
H
HelloCrease 已提交
866

867
  ```ts
868
  // xxx.ets
Z
zengyawen 已提交
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
  @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)
    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 已提交
887
      }
Z
zengyawen 已提交
888 889
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
890
    }
Z
zengyawen 已提交
891 892
  }
  ```
Z
zengyawen 已提交
893

Z
zengyawen 已提交
894
  ![en-us_image_0000001257058437](figures/en-us_image_0000001257058437.png)
Z
zengyawen 已提交
895 896


Z
zengyawen 已提交
897
### strokeText
Z
zengyawen 已提交
898

Z
zengyawen 已提交
899
strokeText(text: string, x: number, y: number): void
Z
zengyawen 已提交
900 901 902

Draws a text stroke on the canvas.

Z
zengyawen 已提交
903
- Parameters
H
HelloCrease 已提交
904 905 906 907 908
    | 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. |
Z
zengyawen 已提交
909 910

- Example
H
HelloCrease 已提交
911

912 913
  ```ts
  // xxx.ets
Z
zengyawen 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
  @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)
    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 已提交
932
      }
Z
zengyawen 已提交
933 934
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
935
    }
Z
zengyawen 已提交
936 937 938 939 940 941 942 943 944 945 946 947 948
  }
  ```

  ![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.

- Parameters
H
HelloCrease 已提交
949 950 951
    | Name | Type   | Mandatory | Default Value | Description          |
    | ---- | ------ | --------- | ------------- | -------------------- |
    | text | string | Yes       | ""            | Text to be measured. |
Z
zengyawen 已提交
952 953

- Return value
H
HelloCrease 已提交
954 955 956
    | Type        | Description             |
    | ----------- | ----------------------- |
    | TextMetrics | **TextMetrics** object. |
Z
zengyawen 已提交
957 958

- **TextMetrics** attributes
H
HelloCrease 已提交
959 960 961
    | Name  | Type   | Description        |
    | ----- | ------ | ------------------ |
    | width | number | Width of the text. |
Z
zengyawen 已提交
962 963

- Example
H
HelloCrease 已提交
964

965
  ```ts
966
  // xxx.ets
Z
zengyawen 已提交
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
  @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)
    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 已提交
986
      }
Z
zengyawen 已提交
987 988
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
989
    }
Z
zengyawen 已提交
990 991
  }
  ```
Z
zengyawen 已提交
992

Z
zengyawen 已提交
993
  ![en-us_image_0000001256858431](figures/en-us_image_0000001256858431.png)
Z
zengyawen 已提交
994 995


Z
zengyawen 已提交
996
### stroke
Z
zengyawen 已提交
997

Z
zengyawen 已提交
998
stroke(path?: Path2D): void
Z
zengyawen 已提交
999 1000 1001

Strokes a path.

Z
zengyawen 已提交
1002
- Parameters
H
HelloCrease 已提交
1003 1004 1005
    | Name | Type                                     | Mandatory | Default Value | Description                |
    | ---- | ---------------------------------------- | --------- | ------------- | -------------------------- |
    | path | [Path2D](ts-components-canvas-path2d.md) | No        | null          | A **Path2D** path to draw. |
Z
zengyawen 已提交
1006 1007

- Example
H
HelloCrease 已提交
1008

1009 1010
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
  @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)
    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)
            this.offContext.strokeStyle = 'rgb(0,0,255)'
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1031
      }
Z
zengyawen 已提交
1032 1033
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1034
    }
Z
zengyawen 已提交
1035 1036
  }
  ```
Z
zengyawen 已提交
1037

Z
zengyawen 已提交
1038
  ![en-us_image_0000001257138373](figures/en-us_image_0000001257138373.png)
Z
zengyawen 已提交
1039 1040


Z
zengyawen 已提交
1041
### beginPath
Z
zengyawen 已提交
1042

Z
zengyawen 已提交
1043
beginPath(): void
Z
zengyawen 已提交
1044 1045 1046

Creates a drawing path.

Z
zengyawen 已提交
1047
- Example
H
HelloCrease 已提交
1048

1049
  ```ts
1050
  // xxx.ets
Z
zengyawen 已提交
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
  @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)
    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 已提交
1073
      }
Z
zengyawen 已提交
1074 1075
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1076
    }
Z
zengyawen 已提交
1077 1078
  }
  ```
Z
zengyawen 已提交
1079

Z
zengyawen 已提交
1080
  ![en-us_image_0000001212378440](figures/en-us_image_0000001212378440.png)
Z
zengyawen 已提交
1081 1082


Z
zengyawen 已提交
1083
### moveTo
Z
zengyawen 已提交
1084

Z
zengyawen 已提交
1085
moveTo(x: number, y: number): void
Z
zengyawen 已提交
1086 1087 1088

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

Z
zengyawen 已提交
1089
- Parameters
H
HelloCrease 已提交
1090 1091 1092 1093
    | 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 已提交
1094 1095

- Example
H
HelloCrease 已提交
1096

1097
  ```ts
1098
  // xxx.ets
Z
zengyawen 已提交
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
  @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)
    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 已提交
1119
      }
Z
zengyawen 已提交
1120 1121
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1122
    }
Z
zengyawen 已提交
1123 1124
  }
  ```
Z
zengyawen 已提交
1125

Z
zengyawen 已提交
1126
  ![en-us_image_0000001212058498](figures/en-us_image_0000001212058498.png)
Z
zengyawen 已提交
1127 1128


Z
zengyawen 已提交
1129
### lineTo
Z
zengyawen 已提交
1130

Z
zengyawen 已提交
1131
lineTo(x: number, y: number): void
Z
zengyawen 已提交
1132 1133 1134

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

Z
zengyawen 已提交
1135
- Parameters
H
HelloCrease 已提交
1136 1137 1138 1139
    | 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 已提交
1140 1141

- Example
H
HelloCrease 已提交
1142

1143
  ```ts
1144
  // xxx.ets
Z
zengyawen 已提交
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
  @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)
    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 已提交
1165
      }
Z
zengyawen 已提交
1166 1167
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1168
    }
Z
zengyawen 已提交
1169 1170
  }
  ```
Z
zengyawen 已提交
1171

Z
zengyawen 已提交
1172
  ![en-us_image_0000001257058435](figures/en-us_image_0000001257058435.png)
Z
zengyawen 已提交
1173 1174


Z
zengyawen 已提交
1175
### closePath
Z
zengyawen 已提交
1176

Z
zengyawen 已提交
1177
closePath(): void
Z
zengyawen 已提交
1178 1179 1180

Draws a closed path.

Z
zengyawen 已提交
1181
- Example
H
HelloCrease 已提交
1182

1183
  ```ts
1184
  // xxx.ets
Z
zengyawen 已提交
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
  @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)
    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 已提交
1207
      }
Z
zengyawen 已提交
1208 1209
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1210
    }
Z
zengyawen 已提交
1211 1212
  }
  ```
Z
zengyawen 已提交
1213

Z
zengyawen 已提交
1214
  ![en-us_image_0000001257058431](figures/en-us_image_0000001257058431.png)
Z
zengyawen 已提交
1215 1216


Z
zengyawen 已提交
1217
### createPattern
Z
zengyawen 已提交
1218

Z
zengyawen 已提交
1219
createPattern(image: ImageBitmap, repetition: string): CanvasPattern
Z
zengyawen 已提交
1220 1221 1222

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

Z
zengyawen 已提交
1223
- Parameters
H
HelloCrease 已提交
1224 1225 1226 1227
    | 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'**. |
Z
zengyawen 已提交
1228 1229

- Example
H
HelloCrease 已提交
1230

1231
  ```ts
1232
  // xxx.ets
Z
zengyawen 已提交
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
  @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)
    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 已提交
1253
      }
Z
zengyawen 已提交
1254 1255
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1256
    }
Z
zengyawen 已提交
1257 1258
  }
  ```
Z
zengyawen 已提交
1259

Z
zengyawen 已提交
1260
  ![en-us_image_0000001257138387](figures/en-us_image_0000001257138387.png)
Z
zengyawen 已提交
1261 1262


Z
zengyawen 已提交
1263
### bezierCurveTo
Z
zengyawen 已提交
1264

Z
zengyawen 已提交
1265
bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void
Z
zengyawen 已提交
1266 1267 1268

Draws a cubic bezier curve on the canvas.

Z
zengyawen 已提交
1269
- Name
H
HelloCrease 已提交
1270 1271 1272 1273 1274 1275 1276 1277
    | 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 已提交
1278 1279

- Example
H
HelloCrease 已提交
1280

1281
  ```ts
1282
  // xxx.ets
Z
zengyawen 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
  @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)
    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 已提交
1303
      }
Z
zengyawen 已提交
1304 1305
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1306
    }
Z
zengyawen 已提交
1307 1308
  }
  ```
Z
zengyawen 已提交
1309

Z
zengyawen 已提交
1310
  ![en-us_image_0000001212378442](figures/en-us_image_0000001212378442.png)
Z
zengyawen 已提交
1311 1312


Z
zengyawen 已提交
1313
### quadraticCurveTo
Z
zengyawen 已提交
1314

Z
zengyawen 已提交
1315
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void
Z
zengyawen 已提交
1316 1317 1318

Draws a quadratic curve on the canvas.

Z
zengyawen 已提交
1319
- Parameters
H
HelloCrease 已提交
1320 1321 1322 1323 1324 1325
    | 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. |
Z
zengyawen 已提交
1326 1327

- Example
H
HelloCrease 已提交
1328

1329
  ```ts
1330
  // xxx.ets
Z
zengyawen 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
  @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)
    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 已提交
1351
      }
Z
zengyawen 已提交
1352 1353
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1354
    }
Z
zengyawen 已提交
1355 1356
  }
  ```
Z
zengyawen 已提交
1357

Z
zengyawen 已提交
1358
  ![en-us_image_0000001256978383](figures/en-us_image_0000001256978383.png)
Z
zengyawen 已提交
1359 1360


Z
zengyawen 已提交
1361
### arc
Z
zengyawen 已提交
1362

Z
zengyawen 已提交
1363
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise?: boolean): void
Z
zengyawen 已提交
1364 1365 1366

Draws an arc on the canvas.

Z
zengyawen 已提交
1367
- Parameters
H
HelloCrease 已提交
1368 1369 1370 1371 1372 1373 1374 1375
    | 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.                   |
    | anticlockwise | boolean | No        | false         | Whether to draw the arc counterclockwise. |
Z
zengyawen 已提交
1376 1377

- Example
H
HelloCrease 已提交
1378

1379
  ```ts
1380
  // xxx.ets
Z
zengyawen 已提交
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
  @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)
    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 已提交
1400
      }
Z
zengyawen 已提交
1401 1402
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1403
    }
Z
zengyawen 已提交
1404 1405
  }
  ```
Z
zengyawen 已提交
1406

Z
zengyawen 已提交
1407
  ![en-us_image_0000001212378430](figures/en-us_image_0000001212378430.png)
Z
zengyawen 已提交
1408 1409


Z
zengyawen 已提交
1410
### arcTo
Z
zengyawen 已提交
1411

Z
zengyawen 已提交
1412
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void
Z
zengyawen 已提交
1413 1414 1415

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

Z
zengyawen 已提交
1416
- Parameters
H
HelloCrease 已提交
1417 1418 1419 1420 1421 1422 1423
    | 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 已提交
1424 1425

- Example
H
HelloCrease 已提交
1426

1427
  ```ts
1428
  // xxx.ets
Z
zengyawen 已提交
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
  @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)
    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 已提交
1448
      }
Z
zengyawen 已提交
1449 1450
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1451
    }
Z
zengyawen 已提交
1452 1453
  }
  ```
Z
zengyawen 已提交
1454

Z
zengyawen 已提交
1455
  ![en-us_image_0000001257138383](figures/en-us_image_0000001257138383.png)
Z
zengyawen 已提交
1456 1457


Z
zengyawen 已提交
1458
### ellipse
Z
zengyawen 已提交
1459

Z
zengyawen 已提交
1460
ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise?: boolean): void
Z
zengyawen 已提交
1461 1462 1463

Draws an ellipse in the specified rectangular region.

Z
zengyawen 已提交
1464
- Parameters
H
HelloCrease 已提交
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
    | 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, in radians. |
    | startAngle    | number  | Yes       | 0             | Angle of the start point for drawing the ellipse, in radians. |
    | endAngle      | number  | Yes       | 0             | Angle of the end point for drawing the ellipse, in radians. |
    | anticlockwise | boolean | No        | false         | Whether to draw the ellipse in the counterclockwise direction. |
Z
zengyawen 已提交
1475 1476 1477

- Example

H
HelloCrease 已提交
1478

1479
  ```ts
1480
  // xxx.ets
Z
zengyawen 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
  @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()
            this.offContext.ellipse(200, 200, 50, 100, Math.PI * 0.25, Math.PI * 0.5, Math.PI)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1500
      }
Z
zengyawen 已提交
1501 1502
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1503
    }
Z
zengyawen 已提交
1504 1505
  }
  ```
Z
zengyawen 已提交
1506

Z
zengyawen 已提交
1507
  ![en-us_image_0000001256858423](figures/en-us_image_0000001256858423.png)
Z
zengyawen 已提交
1508 1509


Z
zengyawen 已提交
1510
### rect
Z
zengyawen 已提交
1511

Z
zengyawen 已提交
1512
rect(x: number, y: number, width: number, height: number): void
Z
zengyawen 已提交
1513 1514 1515

Creates a rectangle.

Z
zengyawen 已提交
1516
- Parameters
H
HelloCrease 已提交
1517 1518 1519 1520 1521 1522
    | 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 已提交
1523 1524

- Example
H
HelloCrease 已提交
1525

1526
  ```ts
1527
  // xxx.ets
Z
zengyawen 已提交
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
  @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.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 已提交
1546
      }
Z
zengyawen 已提交
1547 1548
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1549
    }
Z
zengyawen 已提交
1550 1551
  }
  ```
Z
zengyawen 已提交
1552

Z
zengyawen 已提交
1553
  ![en-us_image_0000001257138381](figures/en-us_image_0000001257138381.png)
Z
zengyawen 已提交
1554 1555


Z
zengyawen 已提交
1556
### fill
Z
zengyawen 已提交
1557

Z
zengyawen 已提交
1558
fill(): void
Z
zengyawen 已提交
1559 1560 1561

Fills the area inside a closed path.

Z
zengyawen 已提交
1562
- Example
H
HelloCrease 已提交
1563

1564
  ```ts
1565
  // xxx.ets
Z
zengyawen 已提交
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
  @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(() =>{
            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 已提交
1584
      }
Z
zengyawen 已提交
1585 1586
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1587
    }
Z
zengyawen 已提交
1588 1589
  }
  ```
Z
zengyawen 已提交
1590

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


Z
zengyawen 已提交
1594
### clip
Z
zengyawen 已提交
1595

Z
zengyawen 已提交
1596
clip(): void
Z
zengyawen 已提交
1597 1598 1599

Sets the current path to a clipping path.

Z
zengyawen 已提交
1600
- Example
H
HelloCrease 已提交
1601

1602
  ```ts
1603
  // xxx.ets
Z
zengyawen 已提交
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
  @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(() =>{
            this.offContext.rect(0, 0, 200, 200)
            this.offContext.stroke()
            this.offContext.clip()
            this.offContext.fillStyle = "rgb(255,0,0)"
            this.offContext.fillRect(0, 0, 150, 150)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1625
      }
Z
zengyawen 已提交
1626 1627
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1628
    }
Z
zengyawen 已提交
1629 1630
  }
  ```
Z
zengyawen 已提交
1631

Z
zengyawen 已提交
1632
  ![en-us_image_0000001257058441](figures/en-us_image_0000001257058441.png)
Z
zengyawen 已提交
1633 1634


Z
zengyawen 已提交
1635
### rotate
Z
zengyawen 已提交
1636

Z
zengyawen 已提交
1637
rotate(rotate: number): void
Z
zengyawen 已提交
1638 1639 1640

Rotates a canvas clockwise around its coordinate axes.

Z
zengyawen 已提交
1641
- Parameters
H
HelloCrease 已提交
1642 1643 1644
    | Name   | Type   | Mandatory | Default Value | Description                              |
    | ------ | ------ | --------- | ------------- | ---------------------------------------- |
    | rotate | number | Yes       | 0             | Clockwise rotation angle. You can use **Math.PI / 180** to convert the angle to a radian. |
Z
zengyawen 已提交
1645 1646

- Example
H
HelloCrease 已提交
1647

1648
  ```ts
1649
  // xxx.ets
Z
zengyawen 已提交
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
  @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)
    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 已提交
1668
      }
Z
zengyawen 已提交
1669 1670
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1671
    }
Z
zengyawen 已提交
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
  }
  ```

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


### scale

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

Scales a canvas based on scale factors.

- Parameters
H
HelloCrease 已提交
1685 1686 1687 1688
    | Name | Type   | Mandatory | Default Value | Description              |
    | ---- | ------ | --------- | ------------- | ------------------------ |
    | x    | number | Yes       | 0             | Horizontal scale factor. |
    | y    | number | Yes       | 0             | Vertical scale factor.   |
Z
zengyawen 已提交
1689 1690

- Example
H
HelloCrease 已提交
1691

1692
  ```ts
1693
  // xxx.ets
Z
zengyawen 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
  @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)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.strokeRect(10, 10, 25, 25)
            this.offContext.scale(2, 2) // Scale to 200%
            this.offContext.strokeRect(10, 10, 25, 25)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1713
      }
Z
zengyawen 已提交
1714 1715
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1716
    }
Z
zengyawen 已提交
1717 1718
  }
  ```
Z
zengyawen 已提交
1719

Z
zengyawen 已提交
1720
  ![en-us_image_0000001211898516](figures/en-us_image_0000001211898516.png)
Z
zengyawen 已提交
1721 1722


Z
zengyawen 已提交
1723
### transform
Z
zengyawen 已提交
1724

Z
zengyawen 已提交
1725
transform(scaleX: number, skewX: number, skewY: number, scaleY: number, translateX: number, translateY: number): void
Z
zengyawen 已提交
1726 1727 1728

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.

1729
> **NOTE**
Z
zengyawen 已提交
1730
> 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 已提交
1731
>
Z
zengyawen 已提交
1732
> - x' = scaleX \* x + skewY \* y + translateX
H
HelloCrease 已提交
1733
>
Z
zengyawen 已提交
1734 1735 1736
> - y' = skewX \* x + scaleY \* y + translateY

- Parameters
H
HelloCrease 已提交
1737 1738 1739 1740 1741 1742 1743 1744
    | Name       | Type   | Mandatory | Default Value | Description         |
    | ---------- | ------ | --------- | ------------- | ------------------- |
    | scaleX     | number | Yes       | 0             | X-axis scale.       |
    | skewX      | number | Yes       | 0             | X-axis skew.        |
    | skewY      | number | Yes       | 0             | Y-axis skew.        |
    | scaleY     | number | Yes       | 0             | Y-axis scale.       |
    | translateX | number | Yes       | 0             | X-axis translation. |
    | translateY | number | Yes       | 0             | Y-axis translation. |
Z
zengyawen 已提交
1745 1746

- Example
H
HelloCrease 已提交
1747

1748
  ```ts
1749
  // xxx.ets
Z
zengyawen 已提交
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
  @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)
    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 已提交
1774
      }
Z
zengyawen 已提交
1775 1776
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1777
    }
Z
zengyawen 已提交
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
  }
  ```

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


### setTransform

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

Resets the existing transformation matrix and creates a new transformation matrix by using the same parameters as the **transform()** function.

- Parameters
H
HelloCrease 已提交
1791 1792 1793 1794 1795 1796 1797 1798
    | Name       | Type   | Mandatory | Default Value | Description         |
    | ---------- | ------ | --------- | ------------- | ------------------- |
    | scaleX     | number | Yes       | 0             | X-axis scale.       |
    | skewX      | number | Yes       | 0             | X-axis skew.        |
    | skewY      | number | Yes       | 0             | Y-axis skew.        |
    | scaleY     | number | Yes       | 0             | Y-axis scale.       |
    | translateX | number | Yes       | 0             | X-axis translation. |
    | translateY | number | Yes       | 0             | Y-axis translation. |
Z
zengyawen 已提交
1799 1800

- Example
H
HelloCrease 已提交
1801

1802
  ```ts
1803
  // xxx.ets
Z
zengyawen 已提交
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
  @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)
    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 已提交
1825
      }
Z
zengyawen 已提交
1826 1827
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1828
    }
Z
zengyawen 已提交
1829 1830
  }
  ```
Z
zengyawen 已提交
1831

Z
zengyawen 已提交
1832
  ![en-us_image_0000001211898518](figures/en-us_image_0000001211898518.png)
Z
zengyawen 已提交
1833 1834


Z
zengyawen 已提交
1835
### translate
Z
zengyawen 已提交
1836

Z
zengyawen 已提交
1837
translate(x: number, y: number): void
Z
zengyawen 已提交
1838 1839 1840

Moves the origin of the coordinate system.

Z
zengyawen 已提交
1841
- Parameters
H
HelloCrease 已提交
1842 1843 1844 1845
    | Name | Type   | Mandatory | Default Value | Description         |
    | ---- | ------ | --------- | ------------- | ------------------- |
    | x    | number | Yes       | 0             | X-axis translation. |
    | y    | number | Yes       | 0             | Y-axis translation. |
Z
zengyawen 已提交
1846 1847

- Example
H
HelloCrease 已提交
1848

1849
  ```ts
1850
  // xxx.ets
Z
zengyawen 已提交
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
  @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)
    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%')
    }
  }
  ```

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


### drawImage

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

1884
drawImage(image: ImageBitmap | PixelMap, dx: number, dy: number, dWidth: number, dHeight: number): void
Z
zengyawen 已提交
1885

1886
drawImage(image: ImageBitmap | PixelMap, sx: number, sy: number, sWidth: number, sHeight: number, dx: number, dy: number, dWidth: number, dHeight: number):void
Z
zengyawen 已提交
1887 1888 1889 1890

Draws an image.

- Parameters
H
HelloCrease 已提交
1891 1892
    | Name    | Type                                     | Mandatory | Default Value | Description                              |
    | ------- | ---------------------------------------- | --------- | ------------- | ---------------------------------------- |
1893
    | image   | [ImageBitmap](ts-components-canvas-imagebitmap.md) or [PixelMap](../apis/js-apis-image.md#pixelmap7) | Yes       | null          | Image resource. For details, see ImageBitmap. |
H
HelloCrease 已提交
1894 1895 1896 1897 1898 1899 1900 1901
    | 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. |
    | sWidth  | number                                   | No        | 0             | Target width to crop the source image.   |
    | sHeight | 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. |
    | dWidth  | number                                   | No        | 0             | Width of the drawing area.               |
    | dHeight | number                                   | No        | 0             | Height of the drawing area.              |
Z
zengyawen 已提交
1902 1903 1904


- Example
H
HelloCrease 已提交
1905

1906
  ```ts
1907
  // xxx.ets
Z
zengyawen 已提交
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
  @Entry
  @Component
  struct Index {
    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 已提交
1918 1919
        .width('100%')
        .height('100%')
Z
zengyawen 已提交
1920 1921 1922 1923 1924 1925
        .backgroundColor('#ffff00')
        .onReady(() => {
            this.offContext.drawImage( this.img,0,0,400,200)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
1926
      }
Z
zengyawen 已提交
1927 1928
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1929
    }
Z
zengyawen 已提交
1930 1931
  }
  ```
Z
zengyawen 已提交
1932

Z
zengyawen 已提交
1933
  ![en-us_image_0000001256978377](figures/en-us_image_0000001256978377.png)
Z
zengyawen 已提交
1934 1935


Z
zengyawen 已提交
1936
### createImageData
Z
zengyawen 已提交
1937

Z
zengyawen 已提交
1938
createImageData(width: number, height: number): Object
Z
zengyawen 已提交
1939

E
esterzhou 已提交
1940
Creates an **ImageData** object based on the specified width and height. For details, see [ImageData](ts-components-canvas-imagebitmap.md).
Z
zengyawen 已提交
1941

Z
zengyawen 已提交
1942
- Parameters
H
HelloCrease 已提交
1943 1944 1945 1946
    | Name   | Type   | Mandatory | Default Value | Description                         |
    | ------ | ------ | --------- | ------------- | ----------------------------------- |
    | width  | number | Yes       | 0             | Width of the **ImageData** object.  |
    | height | number | Yes       | 0             | Height of the **ImageData** object. |
Z
zengyawen 已提交
1947 1948


Z
zengyawen 已提交
1949 1950 1951 1952
### createImageData

createImageData(imageData: ImageData): Object

E
esterzhou 已提交
1953
Creates an **ImageData** object by copying an existing **ImageData** object. For details, see [ImageData](ts-components-canvas-imagebitmap.md).
Z
zengyawen 已提交
1954 1955

- Parameters
H
HelloCrease 已提交
1956 1957 1958
    | Name      | Type                                     | Mandatory | Default Value | Description                   |
    | --------- | ---------------------------------------- | --------- | ------------- | ----------------------------- |
    | imagedata | [ImageData](ts-components-canvas-imagebitmap.md) | Yes       | null          | **ImageData** object to copy. |
Z
zengyawen 已提交
1959

1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
### 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.
- Parameters
  | 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. |
Z
zengyawen 已提交
1972 1973 1974 1975 1976

### getImageData

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

1977
Obtains the **[ImageData](ts-components-canvas-imagebitmap.md)** object created with the pixels within the specified area on the canvas.
Z
zengyawen 已提交
1978 1979

- Parameters
H
HelloCrease 已提交
1980 1981 1982 1983 1984 1985
    | 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.               |
Z
zengyawen 已提交
1986 1987 1988 1989 1990 1991


### putImageData

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

E
esterzhou 已提交
1992
Puts the [ImageData](ts-components-canvas-imagebitmap.md) onto a rectangular area on the canvas.
Z
zengyawen 已提交
1993 1994

- Parameters
H
HelloCrease 已提交
1995 1996 1997 1998 1999 2000 2001 2002 2003
    | Name        | Type   | Mandatory | Default Value                      | Description                              |
    | ----------- | ------ | --------- | ---------------------------------- | ---------------------------------------- |
    | 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 已提交
2004 2005

- Example
H
HelloCrease 已提交
2006

2007
  ```ts
2008
  // xxx.ets
Z
zengyawen 已提交
2009 2010 2011 2012 2013 2014 2015 2016 2017
  @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 已提交
2018 2019 2020
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
Z
zengyawen 已提交
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
          .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 已提交
2032 2033
          })
      }
Z
zengyawen 已提交
2034 2035
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2036
    }
Z
zengyawen 已提交
2037 2038
  }
  ```
Z
zengyawen 已提交
2039

Z
zengyawen 已提交
2040
  ![en-us_image_0000001212058496](figures/en-us_image_0000001212058496.png)
Z
zengyawen 已提交
2041 2042


Z
zengyawen 已提交
2043
### restore
Z
zengyawen 已提交
2044

Z
zengyawen 已提交
2045
restore(): void
Z
zengyawen 已提交
2046 2047 2048

Restores the saved drawing context.

Z
zengyawen 已提交
2049
- Example
H
HelloCrease 已提交
2050

2051
  ```ts
2052
  // xxx.ets
Z
zengyawen 已提交
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069
  @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.restore()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2070
      }
Z
zengyawen 已提交
2071 2072
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2073
    }
Z
zengyawen 已提交
2074 2075
  }
  ```
Z
zengyawen 已提交
2076 2077


Z
zengyawen 已提交
2078
### save
Z
zengyawen 已提交
2079

Z
zengyawen 已提交
2080
save(): void
Z
zengyawen 已提交
2081 2082 2083

Saves the current drawing context.

Z
zengyawen 已提交
2084
- Example
H
HelloCrease 已提交
2085

2086
  ```ts
2087
  // xxx.ets
Z
zengyawen 已提交
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
  @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()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
2105
      }
Z
zengyawen 已提交
2106 2107
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2108
    }
Z
zengyawen 已提交
2109 2110
  }
  ```
Z
zengyawen 已提交
2111 2112


Z
zengyawen 已提交
2113
### createLinearGradient
Z
zengyawen 已提交
2114

Z
zengyawen 已提交
2115
createLinearGradient(x0: number, y0: number, x1: number, y1: number): void
Z
zengyawen 已提交
2116 2117 2118

Creates a linear gradient.

Z
zengyawen 已提交
2119
- Parameters
H
HelloCrease 已提交
2120 2121 2122 2123 2124 2125
    | 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 已提交
2126 2127

- Example
H
HelloCrease 已提交
2128

2129
  ```ts
2130
  // xxx.ets
Z
zengyawen 已提交
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145
  @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 已提交
2146 2147 2148
            grad.addColorStop(0.0, 'red')
            grad.addColorStop(0.5, 'white')
            grad.addColorStop(1.0, 'green')
Z
zengyawen 已提交
2149 2150 2151 2152 2153
            this.offContext.fillStyle = grad
            this.offContext.fillRect(0, 0, 500, 500)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2154
      }
Z
zengyawen 已提交
2155 2156
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2157
    }
Z
zengyawen 已提交
2158 2159 2160 2161
  }
  ```

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


Z
zengyawen 已提交
2164
### createRadialGradient
Z
zengyawen 已提交
2165

Z
zengyawen 已提交
2166
createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number): void
Z
zengyawen 已提交
2167 2168 2169

Creates a linear gradient.

Z
zengyawen 已提交
2170
- Parameters
H
HelloCrease 已提交
2171 2172 2173 2174 2175 2176 2177 2178
    | 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 已提交
2179 2180

- Example
H
HelloCrease 已提交
2181

2182
  ```ts
2183
  // xxx.ets
Z
zengyawen 已提交
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198
  @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 已提交
2199 2200 2201
            grad.addColorStop(0.0, 'red')
            grad.addColorStop(0.5, 'white')
            grad.addColorStop(1.0, 'green')
Z
zengyawen 已提交
2202 2203 2204 2205 2206
            this.offContext.fillStyle = grad
            this.offContext.fillRect(0, 0, 500, 500)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2207
      }
Z
zengyawen 已提交
2208 2209
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2210
    }
Z
zengyawen 已提交
2211 2212
  }
  ```
Z
zengyawen 已提交
2213

Z
zengyawen 已提交
2214
  ![en-us_image_0000001212218480](figures/en-us_image_0000001212218480.png)
Z
zengyawen 已提交
2215 2216


Z
zengyawen 已提交
2217
## CanvasPattern
Z
zengyawen 已提交
2218

Z
zengyawen 已提交
2219
Defines an object created by using the [createPattern](#createpattern) method.