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

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

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


E
esterzhou 已提交
10

Z
zengyawen 已提交
11 12
## APIs

E
ester.zhou 已提交
13
OffscreenCanvasRenderingContext2D(width: number, height: number, settings?: RenderingContextSettings)
Z
zengyawen 已提交
14

E
ester.zhou 已提交
15 16
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
17 18
**Parameters**

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


## Attributes

E
ester.zhou 已提交
28 29 30 31 32 33 34 35
| Name                                      | Type                                      | Description                                      |
| ---------------------------------------- | ---------------------------------------- | ---------------------------------------- |
| [fillStyle](#fillstyle)                  | string \|number<sup>10+</sup> \|[CanvasGradient](ts-components-canvas-canvasgradient.md) \| [CanvasPattern](ts-components-canvas-canvaspattern.md#canvaspattern) | Style to fill an area.<br>- When the type is **string**, this attribute indicates the color of the filling area.<br>- When the type is **number**, this attribute indicates the color of the filling area.<br>- When the type is **CanvasGradient**, this attribute indicates a gradient object, which is created using the **[createLinearGradient](#createlineargradient)** API.<br>- When the type is **CanvasPattern**, this attribute indicates a pattern, which is created using the **[createPattern](#createpattern)** API.<br>Since API version 9, this API is supported in ArkTS widgets.|
| [lineWidth](#linewidth)                  | number                                   | Line width.<br>Since API version 9, this API is supported in ArkTS widgets.|
| [strokeStyle](#strokestyle)              | string \|number<sup>10+</sup> \|[CanvasGradient](ts-components-canvas-canvasgradient.md) \| [CanvasPattern](ts-components-canvas-canvaspattern.md#canvaspattern) | Stroke style.<br>- When the type is **string**, this attribute indicates the stroke color.<br>- When the type is **number**, 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)** API.<br>- When the type is **CanvasPattern**, this attribute indicates a pattern, which is created using the **[createPattern](#createpattern)** API.<br>Since API version 9, this API is supported in ArkTS widgets.|
| [lineCap](#linecap)                      | CanvasLineCap                            | Style of the line endpoints. The options are as follows:<br>- **butt**: The endpoints of the line are squared off.<br>- **round**: The endpoints of the line are rounded.<br>- **square**: The endpoints of the line are squared off, and each endpoint has added a rectangle whose length is the same as the line thickness and whose width is half of the line thickness.<br>- Default value: **'butt'**<br>Since API version 9, this API is supported in ArkTS widgets.|
| [lineJoin](#linejoin)                    | CanvasLineJoin                           | Style of the shape used to join line segments. The options are as follows:<br>- **round**: The intersection is a sector, whose radius at the rounded corner is equal to the line width.<br>- **bevel**: The intersection is a triangle. The rectangular corner of each line is independent.<br>- **miter**: The intersection has a miter corner by extending the outside edges of the lines until they meet. You can view the effect of this attribute in **miterLimit**.<br>- Default value: **'miter'**<br>Since API version 9, this API is supported in ArkTS widgets.|
| [miterLimit](#miterlimit)                | number                                   | Maximum miter length. The miter length is the distance between the inner corner and the outer corner where two lines meet.<br>- Default value: **10**<br>Since API version 9, this API is supported in ArkTS widgets.|
E
ester.zhou 已提交
36
| [font](#font)                            | string                                   | Font style.<br>Syntax: ctx.font='font-size font-family'<br>- (Optional) **font-size**: font size and line height. The unit can only be pixels.<br>(Optional) **font-family**: font family.<br>Syntax: ctx.font='font-style font-weight font-size font-family'<br>- (Optional) **font-style**: font style. Available values are **normal** and **italic**.<br>- (Optional) **font-weight**: font weight. Available values are as follows: **normal**, **bold**, **bolder**, **lighter**, **100**, **200**, **300**, **400**, **500**, **600**, **700**, **800**, **900**.<br>- (Optional) **font-size**: font size and line height. The unit can be px or vp and must be specified.<br>- (Optional) **font-family**: font family. Available values are **sans-serif**, **serif**, and **monospace**.<br>Default value: **'normal normal 14px sans-serif'**<br>Since API version 9, this API is supported in ArkTS widgets.|
E
ester.zhou 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49
| [textAlign](#textalign)                  | CanvasTextAlign                          | Text alignment mode. Available values are as follows:<br>- **left**: The text is left-aligned.<br>- **right**: The text is right-aligned.<br>- **center**: The text is center-aligned.<br>- **start**: The text is aligned with the start bound.<br>- **end**: The text is aligned with the end bound.<br>**NOTE**<br><br>In the **ltr** layout mode, the value **'start'** equals **'left'**. In the **rtl** layout mode, the value **'start'** equals **'right'**.<br>- Default value: **'left'**<br>Since API version 9, this API is supported in ArkTS widgets.|
| [textBaseline](#textbaseline)            | CanvasTextBaseline                       | Horizontal alignment mode of text. Available values are as follows:<br>- **alphabetic**: The text baseline is the normal alphabetic baseline.<br>- **top**: The text baseline is on the top of the text bounding box.<br>- **hanging**: The text baseline is a hanging baseline over the text.<br>- **middle**: The text baseline is in the middle of the text bounding box.<br>**'ideographic'**: The text baseline is the ideographic baseline. If a character exceeds the alphabetic baseline, the ideographic baseline is located at the bottom of the excess character.<br>- **bottom**: The text baseline is at the bottom of the text bounding box. Its difference from the ideographic baseline is that the ideographic baseline does not consider letters in the next line.<br>- Default value: **'alphabetic'**<br>Since API version 9, this API is supported in ArkTS widgets.|
| [globalAlpha](#globalalpha)              | number                                   | Opacity.<br>**0.0**: completely transparent.<br>**1.0**: completely opaque.               |
| [lineDashOffset](#linedashoffset)        | number                                   | Offset of the dashed line. The precision is float.<br>- Default value: **0.0**<br>Since API version 9, this API is supported in ArkTS widgets.|
| [globalCompositeOperation](#globalcompositeoperation) | string                                   | Composition operation type. Available values are as follows: **'source-over'**, **'source-atop'**, **'source-in'**, **'source-out'**, **'destination-over'**, **'destination-atop'**, **'destination-in'**, **'destination-out'**, **'lighter'**, **'copy'**, and **'xor'**.<br>- Default value: **'source-over'**<br>Since API version 9, this API is supported in ArkTS widgets.|
| [shadowBlur](#shadowblur)                | number                                   | Blur level during shadow drawing. A larger value indicates a more blurred effect. The precision is float.<br>- Default value: **0.0**<br>Since API version 9, this API is supported in ArkTS widgets.|
| [shadowColor](#shadowcolor)              | string                                   | Shadow color.<br>Since API version 9, this API is supported in ArkTS widgets.|
| [shadowOffsetX](#shadowoffsetx)          | number                                   | X-axis shadow offset relative to the original object.<br>Since API version 9, this API is supported in ArkTS widgets.|
| [shadowOffsetY](#shadowoffsety)          | number                                   | Y-axis shadow offset relative to the original object.<br>Since API version 9, this API is supported in ArkTS widgets.|
| [imageSmoothingEnabled](#imagesmoothingenabled) | boolean                                  | Whether to adjust the image smoothness during image drawing. The value **true** means to enable this feature, and **false** means the opposite.<br>- Default value: **true**<br>Since API version 9, this API is supported in ArkTS widgets.|
| [imageSmoothingQuality](#imagesmoothingquality) | ImageSmoothingQuality                    | Quality of image smoothing. This attribute works only when **imageSmoothingEnabled** is set to **true**. Available values are as follows:<br>- **'low'**: low quality.<br>- **'medium'**: medium quality.<br>- **'high'**: high quality.<br>Default value: **'low'**<br>Since API version 9, this API is supported in ArkTS widgets.|
| [direction](#direction)                  | CanvasDirection                          | Text direction used for drawing text. Available values are as follows:<br>- **'inherit'**: The text direction is inherited from the **\<Canvas>** component.<br>- **'ltr'**: The text direction is from left to right.<br>- **'rtl'**: The text direction is from right to left.<br>Default value: **'inherit'**<br>Since API version 9, this API is supported in ArkTS widgets.|
| [filter](#filter)                        | string                                   | Filter effect. Available values are as follows:<br>- **'none'**: no filter effect.<br>- **'blur'**: applies the Gaussian blur for the image.<br>- **'brightness'**: applies a linear multiplication to the image to make it look brighter or darker.<br>- **'contrast'**: adjusts the image contrast.<br>- **'grayscale'**: converts the image to a grayscale image.<br>- **'hue-rotate'**: applies hue rotation to the image.<br>- **'invert'**: inverts the input image.<br>- **'opacity'**: sets the opacity of the image.<br>- **'saturate'**: sets the saturation of the image.<br>- **'sepia'**: converts the image to dark brown.<br>Default value: **'none'**<br>Since API version 9, this API is supported in ArkTS widgets.|
E
esterzhou 已提交
50

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


### fillStyle

57 58
```ts
// xxx.ets
Z
zengyawen 已提交
59 60 61
@Entry
@Component
struct FillStyleExample {
E
ester.zhou 已提交
62
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
63
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
64
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
65 66 67 68 69 70 71 72

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.fillStyle = '#0000ff'
          offContext.fillRect(20, 20, 150, 100)
          var image = this.offCanvas.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```



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

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.fillStyle = 0x0000FF
          offContext.fillRect(20, 20, 150, 100)
          var image = this.offCanvas.transferToImageBitmap()
E
ester.zhou 已提交
108
          this.context.transferFromImageBitmap(image)
Z
zengyawen 已提交
109 110 111 112 113 114 115 116
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
117
![en-us_image_0000001193872516](figures/en-us_image_0000001193872516.png)
Z
zengyawen 已提交
118 119 120


### lineWidth
Z
zengyawen 已提交
121

122 123
```ts
// xxx.ets
Z
zengyawen 已提交
124 125 126
@Entry
@Component
struct LineWidthExample {
E
ester.zhou 已提交
127
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
128
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
129
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
130 131 132 133 134 135 136 137

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
138 139 140 141
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.lineWidth = 5
          offContext.strokeRect(25, 25, 85, 105)
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
142 143 144 145 146 147 148 149 150
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
151
![en-us_image_0000001238832403](figures/en-us_image_0000001238832403.png)
Z
zengyawen 已提交
152 153 154


### strokeStyle
Z
zengyawen 已提交
155

156 157
```ts
// xxx.ets
Z
zengyawen 已提交
158 159 160
@Entry
@Component
struct StrokeStyleExample {
E
ester.zhou 已提交
161
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
162
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
163
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
164 165 166 167 168 169 170 171

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.lineWidth = 10
          offContext.strokeStyle = '#0000ff'
          offContext.strokeRect(25, 25, 155, 105)
          var image = this.offCanvas.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```



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

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.lineWidth = 10
          offContext.strokeStyle = 0x0000ff
          offContext.strokeRect(25, 25, 155, 105)
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
209 210 211 212 213 214 215 216 217
          this.context.transferFromImageBitmap(image)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
218
![en-us_image_0000001238712437](figures/en-us_image_0000001238712437.png)
Z
zengyawen 已提交
219 220 221 222


### lineCap

223 224
```ts
// xxx.ets
Z
zengyawen 已提交
225 226 227
@Entry
@Component
struct LineCapExample {
E
ester.zhou 已提交
228
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
229
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
230
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
231 232 233 234 235 236 237 238

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
239 240 241 242 243 244 245 246
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.lineWidth = 8
          offContext.beginPath()
          offContext.lineCap = 'round'
          offContext.moveTo(30, 50)
          offContext.lineTo(220, 50)
          offContext.stroke()
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
247 248 249 250 251 252 253 254 255
          this.context.transferFromImageBitmap(image)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
256
![en-us_image_0000001194192454](figures/en-us_image_0000001194192454.PNG)
Z
zengyawen 已提交
257 258 259


### lineJoin
Z
zengyawen 已提交
260

261 262
```ts
// xxx.ets
Z
zengyawen 已提交
263 264 265
@Entry
@Component
struct LineJoinExample {
E
ester.zhou 已提交
266
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
267
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
268
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
269 270 271 272 273 274 275 276

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
277 278 279 280 281 282 283 284 285
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.beginPath()
          offContext.lineWidth = 8
          offContext.lineJoin = 'miter'
          offContext.moveTo(30, 30)
          offContext.lineTo(120, 60)
          offContext.lineTo(30, 110)
          offContext.stroke()
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
286 287 288 289 290 291 292 293 294
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
295
![en-us_image_0000001194352450](figures/en-us_image_0000001194352450.png)
Z
zengyawen 已提交
296 297 298


### miterLimit
Z
zengyawen 已提交
299

300 301
```ts
// xxx.ets
Z
zengyawen 已提交
302 303 304
@Entry
@Component
struct MiterLimit {
E
ester.zhou 已提交
305
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
306
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
307
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
308 309 310 311 312 313 314 315
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
316 317 318 319 320 321 322 323 324
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.lineWidth = 8
          offContext.lineJoin = 'miter'
          offContext.miterLimit = 3
          offContext.moveTo(30, 30)
          offContext.lineTo(60, 35)
          offContext.lineTo(30, 37)
          offContext.stroke()
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
325 326 327 328 329 330 331 332 333
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
334
![en-us_image_0000001238952397](figures/en-us_image_0000001238952397.png)
Z
zengyawen 已提交
335 336 337


### font
Z
zengyawen 已提交
338

339 340
```ts
// xxx.ets
Z
zengyawen 已提交
341 342
@Entry
@Component
343
struct Fonts {
E
ester.zhou 已提交
344
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
345
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
346
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
347 348 349 350 351 352 353 354
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
355 356 357 358
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.font = '30px sans-serif'
          offContext.fillText("Hello World", 20, 60)
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
359 360 361 362 363 364 365 366 367
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
368
![en-us_image_0000001194032476](figures/en-us_image_0000001194032476.png)
Z
zengyawen 已提交
369 370 371


### textAlign
Z
zengyawen 已提交
372

373 374
```ts
// xxx.ets
Z
zengyawen 已提交
375 376
@Entry
@Component
E
ester.zhou 已提交
377 378
struct CanvasExample {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
379
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
380
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
381 382 383 384 385 386 387 388
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
        var offContext = this.offCanvas.getContext("2d", this.settings)
        offContext.strokeStyle = '#0000ff'
        offContext.moveTo(140, 10)
        offContext.lineTo(140, 160)
        offContext.stroke()

        offContext.font = '18px sans-serif'

        offContext.textAlign = 'start'
        offContext.fillText('textAlign=start', 140, 60)
        offContext.textAlign = 'end'
        offContext.fillText('textAlign=end', 140, 80)
        offContext.textAlign = 'left'
        offContext.fillText('textAlign=left', 140, 100)
        offContext.textAlign = 'center'
        offContext.fillText('textAlign=center',140, 120)
        offContext.textAlign = 'right'
        offContext.fillText('textAlign=right',140, 140)
        var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
408 409 410 411 412 413 414 415 416
        this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
417
![en-us_image_0000001239032423](figures/en-us_image_0000001239032423.png)
Z
zengyawen 已提交
418 419 420


### textBaseline
Z
zengyawen 已提交
421

422 423
```ts
// xxx.ets
Z
zengyawen 已提交
424 425 426
@Entry
@Component
struct TextBaseline {
E
ester.zhou 已提交
427
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
428
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
429
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
430 431 432 433 434 435 436 437
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.strokeStyle = '#0000ff'
          offContext.moveTo(0, 120)
          offContext.lineTo(400, 120)
          offContext.stroke()

          offContext.font = '20px sans-serif'

          offContext.textBaseline = 'top'
          offContext.fillText('Top', 10, 120)
          offContext.textBaseline = 'bottom'
          offContext.fillText('Bottom', 55, 120)
          offContext.textBaseline = 'middle'
          offContext.fillText('Middle', 125, 120)
          offContext.textBaseline = 'alphabetic'
          offContext.fillText('Alphabetic', 195, 120)
          offContext.textBaseline = 'hanging'
          offContext.fillText('Hanging', 295, 120)
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
457 458 459 460 461 462 463 464 465
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
466
![en-us_image_0000001193872518](figures/en-us_image_0000001193872518.png)
Z
zengyawen 已提交
467 468 469


### globalAlpha
Z
zengyawen 已提交
470

471 472
```ts
// xxx.ets
Z
zengyawen 已提交
473 474 475
@Entry
@Component
struct GlobalAlpha {
E
ester.zhou 已提交
476
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
477
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
478
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
479 480 481 482 483 484 485 486
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
487 488 489 490 491 492 493
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.fillStyle = 'rgb(0,0,255)'
          offContext.fillRect(0, 0, 50, 50)
          offContext.globalAlpha = 0.4
          offContext.fillStyle = 'rgb(0,0,255)'
          offContext.fillRect(50, 50, 50, 50)
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
494 495 496 497 498 499 500 501 502
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
503
![en-us_image_0000001238832405](figures/en-us_image_0000001238832405.png)
Z
zengyawen 已提交
504 505 506


### lineDashOffset
Z
zengyawen 已提交
507

508 509
```ts
// xxx.ets
Z
zengyawen 已提交
510 511 512
@Entry
@Component
struct LineDashOffset {
E
ester.zhou 已提交
513
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
514
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
515
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
516 517 518 519 520 521 522 523
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
524 525 526 527 528 529
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.arc(100, 75, 50, 0, 6.28)
          offContext.setLineDash([10,20])
          offContext.lineDashOffset = 10.0
          offContext.stroke()
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
530 531 532 533 534 535 536 537
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}

E
ester.zhou 已提交
538
```
E
ester.zhou 已提交
539
![en-us_image_0000001238712439](figures/en-us_image_0000001238712439.png)
Z
zengyawen 已提交
540 541 542 543


### globalCompositeOperation

E
ester.zhou 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556
| Name              | Description                      |
| ---------------- | ------------------------ |
| source-over      | Displays the new drawing above the existing drawing. This attribute is used by default.  |
| source-atop      | Displays the new drawing on the top of the existing drawing.       |
| source-in        | Displays the new drawing inside the existing drawing.        |
| source-out       | Displays part of the new drawing that is outside of the existing drawing.       |
| destination-over | Displays the existing drawing above the new drawing.       |
| destination-atop | Displays the existing drawing on the top of the new drawing.       |
| destination-in   | Displays the existing drawing inside the new drawing.        |
| destination-out  | Displays the existing drawing outside the new drawing.        |
| lighter          | Displays both the new and existing drawing.         |
| copy             | Displays the new drawing and neglects the existing drawing.       |
| xor              | Combines the new drawing and existing drawing using the XOR operation.|
Z
zengyawen 已提交
557

558 559
```ts
// xxx.ets
Z
zengyawen 已提交
560 561 562
@Entry
@Component
struct GlobalCompositeOperation {
E
ester.zhou 已提交
563
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
564
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
565
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
566 567 568 569 570 571 572 573
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
574 575 576 577 578 579 580 581 582 583 584 585
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.fillStyle = 'rgb(255,0,0)'
          offContext.fillRect(20, 20, 50, 50)
          offContext.globalCompositeOperation = 'source-over'
          offContext.fillStyle = 'rgb(0,0,255)'
          offContext.fillRect(50, 50, 50, 50)
          offContext.fillStyle = 'rgb(255,0,0)'
          offContext.fillRect(120, 20, 50, 50)
          offContext.globalCompositeOperation = 'destination-over'
          offContext.fillStyle = 'rgb(0,0,255)'
          offContext.fillRect(150, 50, 50, 50)
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
586 587 588 589 590 591 592 593 594
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
595
![en-us_image_0000001194192456](figures/en-us_image_0000001194192456.png)
Z
zengyawen 已提交
596 597 598


### shadowBlur
Z
zengyawen 已提交
599

600 601
```ts
// xxx.ets
Z
zengyawen 已提交
602 603 604
@Entry
@Component
struct ShadowBlur {
E
ester.zhou 已提交
605
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
606
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
607
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
608 609 610 611 612 613 614 615
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
616 617 618 619 620 621
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.shadowBlur = 30
          offContext.shadowColor = 'rgb(0,0,0)'
          offContext.fillStyle = 'rgb(255,0,0)'
          offContext.fillRect(20, 20, 100, 80)
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
622 623 624 625 626 627 628 629 630
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
631
![en-us_image_0000001194352452](figures/en-us_image_0000001194352452.png)
Z
zengyawen 已提交
632 633 634


### shadowColor
Z
zengyawen 已提交
635

636 637
```ts
// xxx.ets
Z
zengyawen 已提交
638 639 640
@Entry
@Component
struct ShadowColor {
E
ester.zhou 已提交
641
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
642
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
643
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
644 645 646 647 648 649 650 651
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
652 653 654 655 656 657
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.shadowBlur = 30
          offContext.shadowColor = 'rgb(0,0,255)'
          offContext.fillStyle = 'rgb(255,0,0)'
          offContext.fillRect(30, 30, 100, 100)
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
658 659 660 661 662 663 664 665 666
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
667
![en-us_image_0000001238952399](figures/en-us_image_0000001238952399.png)
Z
zengyawen 已提交
668 669 670


### shadowOffsetX
Z
zengyawen 已提交
671

672 673
```ts
// xxx.ets
Z
zengyawen 已提交
674 675 676
@Entry
@Component
struct ShadowOffsetX {
E
ester.zhou 已提交
677
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
678
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
679
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
680 681 682 683 684 685 686 687
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
688 689 690 691 692 693 694
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.shadowBlur = 10
          offContext.shadowOffsetX = 20
          offContext.shadowColor = 'rgb(0,0,0)'
          offContext.fillStyle = 'rgb(255,0,0)'
          offContext.fillRect(20, 20, 100, 80)
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
695 696 697 698 699 700 701 702 703
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
704
![en-us_image_0000001194032478](figures/en-us_image_0000001194032478.png)
Z
zengyawen 已提交
705 706 707


### shadowOffsetY
Z
zengyawen 已提交
708

709 710
```ts
// xxx.ets
Z
zengyawen 已提交
711 712 713
@Entry
@Component
struct ShadowOffsetY {
E
ester.zhou 已提交
714
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
715
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
716
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
717 718 719 720 721 722 723 724

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
725 726 727 728 729 730 731
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.shadowBlur = 10
          offContext.shadowOffsetY = 20
          offContext.shadowColor = 'rgb(0,0,0)'
          offContext.fillStyle = 'rgb(255,0,0)'
          offContext.fillRect(30, 30, 100, 100)
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
732 733 734 735 736 737 738 739 740
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
741
![en-us_image_0000001239032425](figures/en-us_image_0000001239032425.png)
Z
zengyawen 已提交
742 743 744


### imageSmoothingEnabled
Z
zengyawen 已提交
745

746 747
```ts
// xxx.ets
Z
zengyawen 已提交
748 749 750
@Entry
@Component
struct ImageSmoothingEnabled {
E
ester.zhou 已提交
751
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
752 753
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private img:ImageBitmap = new ImageBitmap("common/images/icon.jpg")
E
ester.zhou 已提交
754
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
755 756 757 758 759 760 761 762
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
763 764 765 766
          var offContext = this.offCanvas.getContext("2d", this.settings)
          offContext.imageSmoothingEnabled = false
          offContext.drawImage(this.img,0,0,400,200)
          var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
767 768 769 770 771 772 773 774 775
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
776
![en-us_image_0000001193872520](figures/en-us_image_0000001193872520.png)
Z
zengyawen 已提交
777

Z
zengyawen 已提交
778

Z
zengyawen 已提交
779
## Methods
Z
zengyawen 已提交
780 781


Z
zengyawen 已提交
782 783 784
### fillRect

fillRect(x: number, y: number, w: number, h: number): void
Z
zengyawen 已提交
785 786 787

Fills a rectangle on the canvas.

E
ester.zhou 已提交
788 789
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
790
 **Parameters**
Z
zengyawen 已提交
791

E
ester.zhou 已提交
792
| Name    | Type    | Mandatory  | Default Value | Description           |
E
ester.zhou 已提交
793 794 795 796 797 798 799
| ------ | ------ | ---- | ---- | ------------- |
| x      | number | Yes   | 0    | X-coordinate of the upper left corner of the rectangle.|
| y      | number | Yes   | 0    | Y-coordinate of the upper left corner of the rectangle.|
| width  | number | Yes   | 0    | Width of the rectangle.     |
| height | number | Yes   | 0    | Height of the rectangle.     |

 **Example**
H
HelloCrease 已提交
800

801
  ```ts
802
  // xxx.ets
Z
zengyawen 已提交
803 804 805 806 807
  @Entry
  @Component
  struct FillRect {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
808
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
809 810 811 812 813 814 815 816
    
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
817 818 819
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.fillRect(30,30,100,100)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
820 821
            this.context.transferFromImageBitmap(image)
         })
Z
zengyawen 已提交
822
        }
Z
zengyawen 已提交
823 824 825 826 827 828
      .width('100%')
      .height('100%')
    }
  }
  ```

E
ester.zhou 已提交
829
  ![en-us_image_0000001194192436](figures/en-us_image_0000001194192436.png)
Z
zengyawen 已提交
830 831 832 833 834 835 836 837


### strokeRect

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

Draws an outlined rectangle on the canvas.

E
ester.zhou 已提交
838 839
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
840
 **Parameters**
E
ester.zhou 已提交
841

E
ester.zhou 已提交
842
| Name    | Type    | Mandatory  | Default Value | Description          |
E
ester.zhou 已提交
843 844 845 846 847
| ------ | ------ | ---- | ---- | ------------ |
| 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 已提交
848

E
ester.zhou 已提交
849
 **Example**
H
HelloCrease 已提交
850

851 852
  ```ts
  // xxx.ets
Z
zengyawen 已提交
853 854 855 856 857
  @Entry
  @Component
  struct StrokeRect {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
858
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
859

Z
zengyawen 已提交
860 861 862 863 864 865 866
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
867 868 869
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.strokeRect(30, 30, 200, 150)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
870 871
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
872
      }
Z
zengyawen 已提交
873 874
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
875
    }
Z
zengyawen 已提交
876 877
  }
  ```
Z
zengyawen 已提交
878

E
ester.zhou 已提交
879
  ![en-us_image_0000001194352436](figures/en-us_image_0000001194352436.png)
Z
zengyawen 已提交
880 881


Z
zengyawen 已提交
882
### clearRect
Z
zengyawen 已提交
883

Z
zengyawen 已提交
884
clearRect(x: number, y: number, w: number, h: number): void
Z
zengyawen 已提交
885 886 887

Clears the content in a rectangle on the canvas.

E
ester.zhou 已提交
888 889
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
890
 **Parameters**
E
ester.zhou 已提交
891

E
ester.zhou 已提交
892
| Name    | Type    | Mandatory  | Default Value | Description           |
E
ester.zhou 已提交
893 894 895 896 897
| ------ | ------ | ---- | ---- | ------------- |
| 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 已提交
898

E
ester.zhou 已提交
899
 **Example**
H
HelloCrease 已提交
900

901
  ```ts
902
  // xxx.ets
Z
zengyawen 已提交
903 904 905 906 907
  @Entry
  @Component
  struct ClearRect {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
908
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
909

Z
zengyawen 已提交
910 911 912 913 914 915 916
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
917 918 919 920 921
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.fillStyle = 'rgb(0,0,255)'
            offContext.fillRect(20,20,200,200)
            offContext.clearRect(30,30,150,100)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
922 923
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
924
      }
Z
zengyawen 已提交
925 926
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
927
    }
Z
zengyawen 已提交
928 929
  }
  ```
Z
zengyawen 已提交
930

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


Z
zengyawen 已提交
934
### fillText
Z
zengyawen 已提交
935

E
ester.zhou 已提交
936
fillText(text: string, x: number, y: number, maxWidth?: number): void
Z
zengyawen 已提交
937 938 939

Draws filled text on the canvas.

E
ester.zhou 已提交
940 941
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
942
**Parameters**
Z
zengyawen 已提交
943

E
ester.zhou 已提交
944
| Name      | Type    | Mandatory  | Default Value | Description             |
E
esterzhou 已提交
945 946 947 948 949
| -------- | ------ | ---- | ---- | --------------- |
| text     | string | Yes   | ""   | Text to draw.     |
| x        | number | Yes   | 0    | X-coordinate of the lower left corner of the text.|
| y        | number | Yes   | 0    | Y-coordinate of the lower left corner of the text.|
| maxWidth | number | No   | -    | Maximum width allowed for the text.   |
E
ester.zhou 已提交
950

E
ester.zhou 已提交
951
 **Example**
H
HelloCrease 已提交
952

953
  ```ts
954
  // xxx.ets
Z
zengyawen 已提交
955 956 957 958 959
  @Entry
  @Component
  struct FillText {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
960
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
961

Z
zengyawen 已提交
962 963 964 965 966 967 968
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
969 970 971 972
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.font = '30px sans-serif'
            offContext.fillText("Hello World!", 20, 100)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
973 974
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
975
      }
Z
zengyawen 已提交
976 977
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
978
    }
Z
zengyawen 已提交
979 980
  }
  ```
Z
zengyawen 已提交
981

E
ester.zhou 已提交
982
  ![en-us_image_0000001194032458](figures/en-us_image_0000001194032458.png)
Z
zengyawen 已提交
983 984


Z
zengyawen 已提交
985
### strokeText
Z
zengyawen 已提交
986

Z
zengyawen 已提交
987
strokeText(text: string, x: number, y: number): void
Z
zengyawen 已提交
988 989 990

Draws a text stroke on the canvas.

E
ester.zhou 已提交
991 992
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
993 994
**Parameters**

E
ester.zhou 已提交
995
| Name      | Type    | Mandatory  | Default Value | Description             |
E
esterzhou 已提交
996 997 998 999 1000
| -------- | ------ | ---- | ---- | --------------- |
| text     | string | Yes   | ""   | Text to draw.     |
| x        | number | Yes   | 0    | X-coordinate of the lower left corner of the text.|
| y        | number | Yes   | 0    | Y-coordinate of the lower left corner of the text.|
| maxWidth | number | No   | -    | Maximum width of the text to be drawn. |
Z
zengyawen 已提交
1001

E
ester.zhou 已提交
1002
 **Example**
H
HelloCrease 已提交
1003

1004 1005
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1006 1007 1008 1009 1010
  @Entry
  @Component
  struct StrokeText {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1011
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1012

Z
zengyawen 已提交
1013 1014 1015 1016 1017 1018 1019
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1020 1021 1022 1023
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.font = '55px sans-serif'
            offContext.strokeText("Hello World!", 20, 60)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1024 1025
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
1026
      }
Z
zengyawen 已提交
1027 1028
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1029
    }
Z
zengyawen 已提交
1030 1031 1032
  }
  ```

E
ester.zhou 已提交
1033
  ![en-us_image_0000001238952401](figures/en-us_image_0000001238952401.png)
Z
zengyawen 已提交
1034 1035 1036 1037 1038 1039 1040 1041


### measureText

measureText(text: string): TextMetrics

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

E
ester.zhou 已提交
1042 1043
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
1044
 **Parameters**
E
ester.zhou 已提交
1045

E
ester.zhou 已提交
1046
| Name  | Type    | Mandatory  | Default Value | Description        |
E
ester.zhou 已提交
1047 1048 1049
| ---- | ------ | ---- | ---- | ---------- |
| text | string | Yes   | ""   | Text to be measured.|

E
ester.zhou 已提交
1050
 **Return value**
Z
zengyawen 已提交
1051

E
ester.zhou 已提交
1052 1053
| Type         | Description                                      |
| ----------- | ---------------------------------------- |
E
ester.zhou 已提交
1054
| TextMetrics | **TextMetrics** object.<br>Since API version 9, this API is supported in ArkTS widgets.|
Z
zengyawen 已提交
1055

E
esterzhou 已提交
1056
**TextMetrics** attributes
Z
zengyawen 已提交
1057

E
esterzhou 已提交
1058 1059 1060 1061
| Name                      | Type    | Description                                      |
| ------------------------ | ------ | ---------------------------------------- |
| width                    | number | Width of the text.                                 |
| height                   | number | Height of the text.                                 |
E
ester.zhou 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
| actualBoundingBoxAscent  | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the top of the bounding rectangle used to render the text. The current value is **0**.|
| actualBoundingBoxDescent | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the bottom of the bounding rectangle used to render the text. The current value is **0**.|
| actualBoundingBoxLeft    | number | Distance parallel to the baseline from the alignment point determined by the **CanvasRenderingContext2D.textAlign** attribute to the left side of the bounding rectangle of the text. The current value is **0**.|
| actualBoundingBoxRight   | number | Distance parallel to the baseline from the alignment point determined by the **CanvasRenderingContext2D.textAlign** attribute to the right side of the bounding rectangle of the text. The current value is **0**.|
| alphabeticBaseline       | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the alphabetic baseline of the line box. The current value is **0**.|
| emHeightAscent           | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the top of the em square in the line box. The current value is **0**.|
| emHeightDescent          | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the bottom of the em square in the line box. The current value is **0**.|
| fontBoundingBoxAscent    | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the top of the highest bounding rectangle of all the fonts used to render the text. The current value is **0**.|
| fontBoundingBoxDescent   | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the bottom of the bounding rectangle of all the fonts used to render the text. The current value is **0**.|
| hangingBaseline          | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the hanging baseline of the line box. The current value is **0**.|
| ideographicBaseline      | number | Distance from the horizontal line indicated by the **CanvasRenderingContext2D.textBaseline** attribute to the ideographic baseline of the line box. The current value is **0**.|

E
ester.zhou 已提交
1074
 **Example**
H
HelloCrease 已提交
1075

1076
  ```ts
1077
  // xxx.ets
Z
zengyawen 已提交
1078 1079 1080 1081 1082
  @Entry
  @Component
  struct MeasureText {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1083
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1084

Z
zengyawen 已提交
1085 1086 1087 1088 1089 1090 1091
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1092 1093 1094 1095 1096
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.font = '50px sans-serif'
            offContext.fillText("Hello World!", 20, 100)
            offContext.fillText("width:" + this.context.measureText("Hello World!").width, 20, 200)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1097 1098
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
1099
      }
Z
zengyawen 已提交
1100 1101
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1102
    }
Z
zengyawen 已提交
1103 1104
  }
  ```
Z
zengyawen 已提交
1105

E
ester.zhou 已提交
1106
  ![en-us_image_0000001194032480](figures/en-us_image_0000001194032480.png)
Z
zengyawen 已提交
1107 1108


Z
zengyawen 已提交
1109
### stroke
Z
zengyawen 已提交
1110

Z
zengyawen 已提交
1111
stroke(path?: Path2D): void
Z
zengyawen 已提交
1112 1113 1114

Strokes a path.

E
ester.zhou 已提交
1115 1116
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
1117
 **Parameters**
E
ester.zhou 已提交
1118

E
ester.zhou 已提交
1119
| Name  | Type                                      | Mandatory  | Default Value | Description          |
E
ester.zhou 已提交
1120 1121
| ---- | ---------------------------------------- | ---- | ---- | ------------ |
| path | [Path2D](ts-components-canvas-path2d.md) | No   | null | A **Path2D** path to draw.|
Z
zengyawen 已提交
1122

E
ester.zhou 已提交
1123
 **Example**
E
ester.zhou 已提交
1124

1125 1126
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1127 1128 1129 1130 1131
  @Entry
  @Component
  struct Stroke {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1132
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1133

Z
zengyawen 已提交
1134 1135 1136 1137 1138 1139 1140
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1141 1142 1143 1144 1145 1146 1147 1148
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.moveTo(25, 25)
            offContext.lineTo(25, 105)
            offContext.lineTo(75, 105)
            offContext.lineTo(75, 25)
            offContext.strokeStyle = 'rgb(0,0,255)'
            offContext.stroke()
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1149 1150
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1151
      }
Z
zengyawen 已提交
1152 1153
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1154
    }
Z
zengyawen 已提交
1155 1156
  }
  ```
Z
zengyawen 已提交
1157

E
ester.zhou 已提交
1158
  ![en-us_image_0000001238832389](figures/en-us_image_0000001238832389.png)
Z
zengyawen 已提交
1159 1160


Z
zengyawen 已提交
1161
### beginPath
Z
zengyawen 已提交
1162

Z
zengyawen 已提交
1163
beginPath(): void
Z
zengyawen 已提交
1164 1165 1166

Creates a drawing path.

E
ester.zhou 已提交
1167 1168
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
1169
 **Example**
H
HelloCrease 已提交
1170

1171
  ```ts
1172
  // xxx.ets
Z
zengyawen 已提交
1173 1174 1175 1176 1177
  @Entry
  @Component
  struct BeginPath {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1178
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1179

Z
zengyawen 已提交
1180 1181 1182 1183 1184 1185 1186
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1187 1188 1189 1190 1191 1192 1193 1194
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.beginPath()
            offContext.lineWidth = 6
            offContext.strokeStyle = '#0000ff'
            offContext.moveTo(15, 80)
            offContext.lineTo(280, 160)
            offContext.stroke()
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1195 1196
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1197
      }
Z
zengyawen 已提交
1198 1199
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1200
    }
Z
zengyawen 已提交
1201 1202
  }
  ```
Z
zengyawen 已提交
1203

E
ester.zhou 已提交
1204
  ![en-us_image_0000001193872522](figures/en-us_image_0000001193872522.png)
Z
zengyawen 已提交
1205 1206


Z
zengyawen 已提交
1207
### moveTo
Z
zengyawen 已提交
1208

Z
zengyawen 已提交
1209
moveTo(x: number, y: number): void
Z
zengyawen 已提交
1210 1211 1212

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

E
ester.zhou 已提交
1213 1214
Since API version 9, this API is supported in ArkTS widgets.

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

E
ester.zhou 已提交
1217
| Name  | Type    | Mandatory  | Default Value | Description       |
E
ester.zhou 已提交
1218 1219 1220 1221
| ---- | ------ | ---- | ---- | --------- |
| x    | number | Yes   | 0    | X-coordinate of the target position.|
| y    | number | Yes   | 0    | Y-coordinate of the target position.|

E
ester.zhou 已提交
1222
 **Example**
H
HelloCrease 已提交
1223

1224
  ```ts
1225
  // xxx.ets
Z
zengyawen 已提交
1226 1227 1228 1229 1230
  @Entry
  @Component
  struct MoveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1231
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1232

Z
zengyawen 已提交
1233 1234 1235 1236 1237 1238 1239
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1240 1241 1242 1243 1244 1245
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.beginPath()
            offContext.moveTo(10, 10)
            offContext.lineTo(280, 160)
            offContext.stroke()
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1246 1247
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1248
      }
Z
zengyawen 已提交
1249 1250
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1251
    }
Z
zengyawen 已提交
1252 1253
  }
  ```
Z
zengyawen 已提交
1254

E
ester.zhou 已提交
1255
  ![en-us_image_0000001238832409](figures/en-us_image_0000001238832409.png)
Z
zengyawen 已提交
1256 1257


Z
zengyawen 已提交
1258
### lineTo
Z
zengyawen 已提交
1259

Z
zengyawen 已提交
1260
lineTo(x: number, y: number): void
Z
zengyawen 已提交
1261 1262 1263

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

E
ester.zhou 已提交
1264 1265
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
1266
 **Parameters**
E
ester.zhou 已提交
1267

E
ester.zhou 已提交
1268
| Name  | Type    | Mandatory  | Default Value | Description       |
E
ester.zhou 已提交
1269 1270 1271
| ---- | ------ | ---- | ---- | --------- |
| x    | number | Yes   | 0    | X-coordinate of the target position.|
| y    | number | Yes   | 0    | Y-coordinate of the target position.|
Z
zengyawen 已提交
1272

E
ester.zhou 已提交
1273
 **Example**
H
HelloCrease 已提交
1274

1275
  ```ts
1276
  // xxx.ets
Z
zengyawen 已提交
1277 1278 1279 1280 1281
  @Entry
  @Component
  struct LineTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1282
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1283

Z
zengyawen 已提交
1284 1285 1286 1287 1288 1289 1290
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1291 1292 1293 1294 1295 1296
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.beginPath()
            offContext.moveTo(10, 10)
            offContext.lineTo(280, 160)
            offContext.stroke()
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1297 1298
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1299
      }
Z
zengyawen 已提交
1300 1301
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1302
    }
Z
zengyawen 已提交
1303 1304
  }
  ```
Z
zengyawen 已提交
1305

E
ester.zhou 已提交
1306
  ![en-us_image_0000001238712443](figures/en-us_image_0000001238712443.png)
Z
zengyawen 已提交
1307 1308


Z
zengyawen 已提交
1309
### closePath
Z
zengyawen 已提交
1310

Z
zengyawen 已提交
1311
closePath(): void
Z
zengyawen 已提交
1312 1313 1314

Draws a closed path.

E
ester.zhou 已提交
1315 1316
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
1317
 **Example**
H
HelloCrease 已提交
1318

1319
  ```ts
1320
  // xxx.ets
Z
zengyawen 已提交
1321 1322 1323 1324 1325
  @Entry
  @Component
  struct ClosePath {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1326
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1327

Z
zengyawen 已提交
1328 1329 1330 1331 1332 1333 1334
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1335 1336 1337 1338 1339 1340 1341 1342
              var offContext = this.offCanvas.getContext("2d", this.settings)
              offContext.beginPath()
              offContext.moveTo(30, 30)
              offContext.lineTo(110, 30)
              offContext.lineTo(70, 90)
              offContext.closePath()
              offContext.stroke()
              var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1343 1344
              this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1345
      }
Z
zengyawen 已提交
1346 1347
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1348
    }
Z
zengyawen 已提交
1349 1350
  }
  ```
Z
zengyawen 已提交
1351

E
ester.zhou 已提交
1352
  ![en-us_image_0000001194192460](figures/en-us_image_0000001194192460.png)
Z
zengyawen 已提交
1353 1354


Z
zengyawen 已提交
1355
### createPattern
Z
zengyawen 已提交
1356

E
ester.zhou 已提交
1357
createPattern(image: ImageBitmap, repetition: string | null): CanvasPattern | null
Z
zengyawen 已提交
1358 1359 1360

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

E
ester.zhou 已提交
1361 1362
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
1363
**Parameters**
Z
zengyawen 已提交
1364

E
ester.zhou 已提交
1365
| Name        | Type                                      | Mandatory  | Default Value | Description                                      |
E
ester.zhou 已提交
1366 1367
| ---------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- |
| image      | [ImageBitmap](ts-components-canvas-imagebitmap.md) | Yes   | null | Source image. For details, see **ImageBitmap**.                 |
E
ester.zhou 已提交
1368
| repetition | string                                   | Yes   | ""  | Repetition mode. The value can be **'repeat'**, **'repeat-x'**, **'repeat-y'**, **'no-repeat'**, **'clamp'**, or **'mirror'**.|
E
ester.zhou 已提交
1369 1370 1371

**Return value**

E
ester.zhou 已提交
1372 1373 1374
| Type                                      | Description                     |
| ---------------------------------------- | ----------------------- |
| [CanvasPattern](ts-components-canvas-canvaspattern.md#canvaspattern) | Created pattern for image filling based on a specified source image and repetition mode.|
E
ester.zhou 已提交
1375

E
ester.zhou 已提交
1376
 **Example**
H
HelloCrease 已提交
1377

1378
  ```ts
1379
  // xxx.ets
Z
zengyawen 已提交
1380 1381 1382 1383 1384 1385
  @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")
E
ester.zhou 已提交
1386
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1387

Z
zengyawen 已提交
1388 1389 1390 1391 1392 1393 1394
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1395 1396 1397 1398 1399
            var offContext = this.offCanvas.getContext("2d", this.settings)
            var pattern = offContext.createPattern(this.img, 'repeat')
            offContext.fillStyle = pattern
            offContext.fillRect(0, 0, 200, 200)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1400 1401
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1402
      }
Z
zengyawen 已提交
1403 1404
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1405
    }
Z
zengyawen 已提交
1406 1407
  }
  ```
Z
zengyawen 已提交
1408

E
ester.zhou 已提交
1409
  ![en-us_image_0000001194352456](figures/en-us_image_0000001194352456.png)
Z
zengyawen 已提交
1410 1411


Z
zengyawen 已提交
1412
### bezierCurveTo
Z
zengyawen 已提交
1413

Z
zengyawen 已提交
1414
bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void
Z
zengyawen 已提交
1415 1416 1417

Draws a cubic bezier curve on the canvas.

E
ester.zhou 已提交
1418 1419
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
1420
 **Parameters**
E
ester.zhou 已提交
1421

E
ester.zhou 已提交
1422
| Name  | Type    | Mandatory  | Default Value | Description            |
E
ester.zhou 已提交
1423 1424 1425 1426 1427 1428 1429
| ---- | ------ | ---- | ---- | -------------- |
| 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 已提交
1430

E
ester.zhou 已提交
1431
 **Example**
H
HelloCrease 已提交
1432

1433
  ```ts
1434
  // xxx.ets
Z
zengyawen 已提交
1435 1436 1437 1438 1439
  @Entry
  @Component
  struct BezierCurveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1440
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1441

Z
zengyawen 已提交
1442 1443 1444 1445 1446 1447 1448
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1449 1450 1451 1452 1453 1454
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.beginPath()
            offContext.moveTo(10, 10)
            offContext.bezierCurveTo(20, 100, 200, 100, 200, 20)
            offContext.stroke()
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1455 1456
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1457
      }
Z
zengyawen 已提交
1458 1459
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1460
    }
Z
zengyawen 已提交
1461 1462
  }
  ```
Z
zengyawen 已提交
1463

E
ester.zhou 已提交
1464
  ![en-us_image_0000001238952403](figures/en-us_image_0000001238952403.png)
Z
zengyawen 已提交
1465 1466


Z
zengyawen 已提交
1467
### quadraticCurveTo
Z
zengyawen 已提交
1468

Z
zengyawen 已提交
1469
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void
Z
zengyawen 已提交
1470 1471 1472

Draws a quadratic curve on the canvas.

E
ester.zhou 已提交
1473 1474
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
1475
 **Parameters**
Z
zengyawen 已提交
1476

E
ester.zhou 已提交
1477
| Name  | Type    | Mandatory  | Default Value | Description         |
E
ester.zhou 已提交
1478 1479 1480 1481 1482 1483
| ---- | ------ | ---- | ---- | ----------- |
| 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.|

E
ester.zhou 已提交
1484
 **Example**
H
HelloCrease 已提交
1485

1486
  ```ts
1487
  // xxx.ets
Z
zengyawen 已提交
1488 1489 1490 1491 1492
  @Entry
  @Component
  struct QuadraticCurveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1493
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1494

Z
zengyawen 已提交
1495 1496 1497 1498 1499 1500 1501
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1502 1503 1504 1505 1506 1507
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.beginPath()
            offContext.moveTo(20, 20)
            offContext.quadraticCurveTo(100, 100, 200, 20)
            offContext.stroke()
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1508 1509
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
1510
      }
Z
zengyawen 已提交
1511 1512
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1513
    }
Z
zengyawen 已提交
1514 1515
  }
  ```
Z
zengyawen 已提交
1516

E
ester.zhou 已提交
1517
  ![en-us_image_0000001194032482](figures/en-us_image_0000001194032482.png)
Z
zengyawen 已提交
1518 1519


Z
zengyawen 已提交
1520
### arc
Z
zengyawen 已提交
1521

E
ester.zhou 已提交
1522
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void
Z
zengyawen 已提交
1523 1524 1525

Draws an arc on the canvas.

E
ester.zhou 已提交
1526 1527
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
1528
 **Parameters**
E
ester.zhou 已提交
1529

E
ester.zhou 已提交
1530
| Name              | Type     | Mandatory  | Default Value  | Description        |
E
esterzhou 已提交
1531 1532 1533 1534 1535 1536
| ---------------- | ------- | ---- | ----- | ---------- |
| x                | number  | Yes   | 0     | X-coordinate of the center point of the arc.|
| y                | number  | Yes   | 0     | Y-coordinate of the center point of the arc.|
| radius           | number  | Yes   | 0     | Radius of the arc.   |
| startAngle       | number  | Yes   | 0     | Start radian of the arc.  |
| endAngle         | number  | Yes   | 0     | End radian of the arc.  |
E
ester.zhou 已提交
1537
| counterclockwise | boolean | No   | false | Whether to draw the arc counterclockwise.|
Z
zengyawen 已提交
1538

E
ester.zhou 已提交
1539
 **Example**
H
HelloCrease 已提交
1540

1541
  ```ts
1542
  // xxx.ets
Z
zengyawen 已提交
1543 1544 1545 1546 1547
  @Entry
  @Component
  struct Arc {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1548
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1549

Z
zengyawen 已提交
1550 1551 1552 1553 1554 1555 1556
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1557 1558 1559 1560 1561
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.beginPath()
            offContext.arc(100, 75, 50, 0, 6.28)
            offContext.stroke()
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1562 1563
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1564
      }
Z
zengyawen 已提交
1565 1566
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1567
    }
Z
zengyawen 已提交
1568 1569
  }
  ```
Z
zengyawen 已提交
1570

E
ester.zhou 已提交
1571
  ![en-us_image_0000001239032429](figures/en-us_image_0000001239032429.png)
Z
zengyawen 已提交
1572 1573


Z
zengyawen 已提交
1574
### arcTo
Z
zengyawen 已提交
1575

Z
zengyawen 已提交
1576
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void
Z
zengyawen 已提交
1577 1578 1579

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

E
ester.zhou 已提交
1580 1581
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
1582
 **Parameters**
E
ester.zhou 已提交
1583

E
ester.zhou 已提交
1584
| Name    | Type    | Mandatory  | Default Value | Description             |
E
ester.zhou 已提交
1585 1586 1587 1588 1589 1590
| ------ | ------ | ---- | ---- | --------------- |
| 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 已提交
1591

E
ester.zhou 已提交
1592
 **Example**
H
HelloCrease 已提交
1593

1594
  ```ts
1595
  // xxx.ets
Z
zengyawen 已提交
1596 1597 1598 1599 1600
  @Entry
  @Component
  struct ArcTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1601
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1602

Z
zengyawen 已提交
1603 1604 1605 1606 1607 1608 1609
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1610 1611 1612 1613 1614
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.moveTo(100, 20)
            offContext.arcTo(150, 20, 150, 70, 50)
            offContext.stroke()
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1615 1616
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1617
      }
Z
zengyawen 已提交
1618 1619
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1620
    }
Z
zengyawen 已提交
1621 1622
  }
  ```
Z
zengyawen 已提交
1623

E
ester.zhou 已提交
1624
  ![en-us_image_0000001193872524](figures/en-us_image_0000001193872524.png)
Z
zengyawen 已提交
1625 1626


Z
zengyawen 已提交
1627
### ellipse
Z
zengyawen 已提交
1628

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

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

E
ester.zhou 已提交
1633 1634
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
1635
 **Parameters**
Z
zengyawen 已提交
1636

E
ester.zhou 已提交
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
| Name              | Type     | Mandatory  | Default Value  | Description                                      |
| ---------------- | ------- | ---- | ----- | ---------------------------------------- |
| x                | number  | Yes   | 0     | X-coordinate of the ellipse center.                              |
| y                | number  | Yes   | 0     | Y-coordinate of the ellipse center.                              |
| radiusX          | number  | Yes   | 0     | Ellipse radius on the x-axis.                              |
| radiusY          | number  | Yes   | 0     | Ellipse radius on the y-axis.                              |
| rotation         | number  | Yes   | 0     | Rotation angle of the ellipse. The unit is radian.                          |
| startAngle       | number  | Yes   | 0     | Angle of the start point for drawing the ellipse. The unit is radian.                       |
| endAngle         | number  | Yes   | 0     | Angle of the end point for drawing the ellipse. The unit is radian.                       |
| counterclockwise | boolean | No   | false | Whether to draw the ellipse counterclockwise.<br>**true**: Draw the ellipse counterclockwise.<br>**false**: Draw the ellipse clockwise.|
Z
zengyawen 已提交
1647

E
ester.zhou 已提交
1648
 **Example**
H
HelloCrease 已提交
1649

1650
  ```ts
1651
  // xxx.ets
Z
zengyawen 已提交
1652 1653 1654 1655 1656
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1657
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
1658 1659 1660 1661 1662 1663 1664
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1665 1666 1667 1668 1669
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.beginPath()
            offContext.ellipse(200, 200, 50, 100, Math.PI * 0.25, Math.PI * 0.5, Math.PI * 2)
            offContext.stroke()
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1670 1671
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1672
      }
Z
zengyawen 已提交
1673 1674
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1675
    }
Z
zengyawen 已提交
1676 1677
  }
  ```
Z
zengyawen 已提交
1678

E
ester.zhou 已提交
1679
  ![en-us_image_0000001194192440](figures/en-us_image_0000001194192440.png)
Z
zengyawen 已提交
1680 1681


Z
zengyawen 已提交
1682
### rect
Z
zengyawen 已提交
1683

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

E
ester.zhou 已提交
1686
Creates a rectangle on the canvas.
Z
zengyawen 已提交
1687

E
ester.zhou 已提交
1688 1689
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
1690
 **Parameters**
Z
zengyawen 已提交
1691

E
ester.zhou 已提交
1692
| Name  | Type    | Mandatory  | Default Value | Description           |
E
esterzhou 已提交
1693 1694 1695 1696 1697
| ---- | ------ | ---- | ---- | ------------- |
| x    | number | Yes   | 0    | X-coordinate of the upper left corner of the rectangle.|
| y    | number | Yes   | 0    | Y-coordinate of the upper left corner of the rectangle.|
| w    | number | Yes   | 0    | Width of the rectangle.     |
| h    | number | Yes   | 0    | Height of the rectangle.     |
E
ester.zhou 已提交
1698

E
ester.zhou 已提交
1699
 **Example**
H
HelloCrease 已提交
1700

1701
  ```ts
1702
  // xxx.ets
Z
zengyawen 已提交
1703 1704 1705 1706 1707
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1708
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1709

Z
zengyawen 已提交
1710 1711 1712 1713 1714 1715 1716
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1717 1718 1719 1720
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.rect(20, 20, 100, 100) // Create a 100*100 rectangle at (20, 20)
            offContext.stroke()
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1721 1722
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1723
      }
Z
zengyawen 已提交
1724 1725
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1726
    }
Z
zengyawen 已提交
1727 1728
  }
  ```
Z
zengyawen 已提交
1729

E
ester.zhou 已提交
1730
  ![en-us_image_0000001238712445](figures/en-us_image_0000001238712445.png)
Z
zengyawen 已提交
1731 1732


Z
zengyawen 已提交
1733
### fill
Z
zengyawen 已提交
1734

E
esterzhou 已提交
1735
fill(fillRule?: CanvasFillRule): void
Z
zengyawen 已提交
1736

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

E
ester.zhou 已提交
1739 1740
Since API version 9, this API is supported in ArkTS widgets.

E
esterzhou 已提交
1741 1742
**Parameters**

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

1747
  ```ts
1748
  // xxx.ets
Z
zengyawen 已提交
1749 1750 1751 1752 1753
  @Entry
  @Component
  struct Fill {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1754
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1755

Z
zengyawen 已提交
1756 1757 1758 1759 1760 1761 1762
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1763 1764 1765 1766 1767
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.fillStyle = '#ffffff'
            offContext.rect(20, 20, 100, 100) // Create a 100*100 rectangle at (20, 20)
            offContext.fill()
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1768 1769
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1770
      }
Z
zengyawen 已提交
1771 1772
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1773
    }
Z
zengyawen 已提交
1774 1775
  }
  ```
Z
zengyawen 已提交
1776

E
ester.zhou 已提交
1777
  ![en-us_image_0000001194192462](figures/en-us_image_0000001194192462.png)
Z
zengyawen 已提交
1778 1779


E
esterzhou 已提交
1780 1781 1782 1783
fill(path: Path2D, fillRule?: CanvasFillRule): void

Fills the area inside a closed path on the canvas.

E
ester.zhou 已提交
1784 1785
Since API version 9, this API is supported in ArkTS widgets.

E
esterzhou 已提交
1786 1787
**Parameters**

E
ester.zhou 已提交
1788
| Name      | Type            | Mandatory  | Default Value      | Description                                      |
E
esterzhou 已提交
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
| -------- | -------------- | ---- | --------- | ---------------------------------------- |
| path     | Path2D         | Yes   |           | A **Path2D** path to fill.                             |
| fillRule | CanvasFillRule | No   | "nonzero" | Rule by which to determine whether a point is inside or outside the area to fill.<br>The options are **"nonzero"** and **"evenodd"**.|


**Example**  

```ts
// xxx.ets
@Entry
@Component
struct Fill {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1803
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1804 1805 1806 1807 1808 1809 1810 1811

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
1812
          var offContext = this.offCanvas.getContext("2d", this.settings)
E
ester.zhou 已提交
1813 1814 1815 1816 1817 1818 1819 1820
          let region = new Path2D()
          region.moveTo(30, 90)
          region.lineTo(110, 20)
          region.lineTo(240, 130)
          region.lineTo(60, 130)
          region.lineTo(190, 20)
          region.lineTo(270, 90)
          region.closePath()
E
esterzhou 已提交
1821
          // Fill path
E
ester.zhou 已提交
1822 1823 1824
          offContext.fillStyle = '#00ff00'
          offContext.fill(region, "evenodd")
          var image = this.offCanvas.transferToImageBitmap()
E
esterzhou 已提交
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
          this.context.transferFromImageBitmap(image)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

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



Z
zengyawen 已提交
1838
### clip
Z
zengyawen 已提交
1839

E
esterzhou 已提交
1840
clip(fillRule?: CanvasFillRule): void
Z
zengyawen 已提交
1841 1842 1843

Sets the current path to a clipping path.

E
ester.zhou 已提交
1844 1845
Since API version 9, this API is supported in ArkTS widgets.

E
esterzhou 已提交
1846 1847
**Parameters**

E
ester.zhou 已提交
1848
| Name      | Type            | Mandatory  | Default Value      | Description                                      |
E
esterzhou 已提交
1849 1850 1851
| -------- | -------------- | ---- | --------- | ---------------------------------------- |
| fillRule | CanvasFillRule | No   | "nonzero" | Rule by which to determine whether a point is inside or outside the area to clip.<br>The options are **"nonzero"** and **"evenodd"**.|

E
ester.zhou 已提交
1852
 **Example**
H
HelloCrease 已提交
1853

1854
  ```ts
1855
  // xxx.ets
Z
zengyawen 已提交
1856 1857 1858 1859 1860
  @Entry
  @Component
  struct Clip {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1861
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1862

Z
zengyawen 已提交
1863 1864 1865 1866 1867 1868 1869
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1870 1871 1872 1873 1874 1875 1876
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.rect(0, 0, 100, 200)
            offContext.stroke()
            offContext.clip()
            offContext.fillStyle = "rgb(255,0,0)"
            offContext.fillRect(0, 0, 200, 200)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
1877 1878
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1879
      }
Z
zengyawen 已提交
1880 1881
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1882
    }
Z
zengyawen 已提交
1883 1884
  }
  ```
Z
zengyawen 已提交
1885

E
ester.zhou 已提交
1886
  ![en-us_image_0000001194032462](figures/en-us_image_0000001194032462.png)
Z
zengyawen 已提交
1887 1888


E
esterzhou 已提交
1889 1890 1891 1892
clip(path:Path2D, fillRule?: CanvasFillRule): void

Sets a closed path to a clipping path.

E
ester.zhou 已提交
1893 1894
Since API version 9, this API is supported in ArkTS widgets.

E
esterzhou 已提交
1895 1896
**Parameters**

E
ester.zhou 已提交
1897
| Name      | Type            | Mandatory  | Default Value      | Description                                      |
E
esterzhou 已提交
1898
| -------- | -------------- | ---- | --------- | ---------------------------------------- |
E
ester.zhou 已提交
1899
| path     | Path2D         | Yes   |           | A **Path2D** path to clip.                             |
E
esterzhou 已提交
1900 1901
| fillRule | CanvasFillRule | No   | "nonzero" | Rule by which to determine whether a point is inside or outside the area to clip.<br>The options are **"nonzero"** and **"evenodd"**.|

E
ester.zhou 已提交
1902
 **Example**
E
esterzhou 已提交
1903 1904 1905

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1906 1907 1908 1909 1910
  @Entry
  @Component
  struct Clip {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1911
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
1912

E
ester.zhou 已提交
1913 1914 1915 1916 1917 1918 1919
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1920
            var offContext = this.offCanvas.getContext("2d", this.settings)
E
ester.zhou 已提交
1921 1922 1923 1924 1925 1926 1927 1928
            let region = new Path2D()
            region.moveTo(30, 90)
            region.lineTo(110, 20)
            region.lineTo(240, 130)
            region.lineTo(60, 130)
            region.lineTo(190, 20)
            region.lineTo(270, 90)
            region.closePath()
E
ester.zhou 已提交
1929 1930 1931 1932
            offContext.clip(region,"evenodd")
            offContext.fillStyle = "rgb(0,255,0)"
            offContext.fillRect(0, 0, 600, 600)
            var image = this.offCanvas.transferToImageBitmap()
E
ester.zhou 已提交
1933 1934 1935 1936 1937
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
E
esterzhou 已提交
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
    }
  }
  ```

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



### filter

filter(filter: string): void

E
ester.zhou 已提交
1950
Sets a filter for the image on the canvas.
E
esterzhou 已提交
1951

E
ester.zhou 已提交
1952 1953
Since API version 9, this API is supported in ArkTS widgets.

E
esterzhou 已提交
1954 1955
**Parameters**

E
ester.zhou 已提交
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
| Name    | Type    | Mandatory  | Default Value | Description                                      |
| ------ | ------ | ---- | ---- | ---------------------------------------- |
| filter | string | Yes   | -    | Functions that accept various filter effects. Available values are as follows:<br>- **'none'**: no filter effect.<br>- **'blur'**: applies the Gaussian blur for the image.<br>- **'brightness'**: applies a linear multiplication to the image to make it look brighter or darker.<br>- **'contrast'**: adjusts the image contrast.<br>- **'grayscale'**: converts the image to a grayscale image.<br>- **'hue-rotate'**: applies hue rotation to the image.<br>- **'invert'**: inverts the input image.<br>- **'opacity'**: sets the opacity of the image.<br>- **'saturate'**: sets the saturation of the image.<br>- **'sepia'**: converts the image to dark brown.<br>Default value: **'none'**|

**Example**
```ts
  // xxx.ets
  @Entry
  @Component
  struct FilterDemoOff {
    private settings: RenderingContextSettings = new RenderingContextSettings(true);
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
E
ester.zhou 已提交
1968
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
ester.zhou 已提交
1969 1970 1971 1972 1973 1974 1975 1976 1977
    private img:ImageBitmap = new ImageBitmap("common/images/example.jpg");

    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1978 1979
            var offContext = this.offCanvas.getContext("2d", this.settings)
            let offctx = offContext
E
ester.zhou 已提交
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
            let img = this.img

            offctx.drawImage(img, 0, 0, 100, 100);

            offctx.filter = 'grayscale(50%)';
            offctx.drawImage(img, 100, 0, 100, 100);

            offctx.filter = 'sepia(60%)';
            offctx.drawImage(img, 200, 0, 100, 100);

            offctx.filter = 'saturate(30%)';
            offctx.drawImage(img, 0, 100, 100, 100);

            offctx.filter = 'hue-rotate(90degree)';
            offctx.drawImage(img, 100, 100, 100, 100);

            offctx.filter = 'invert(100%)';
            offctx.drawImage(img, 200, 100, 100, 100);

            offctx.filter = 'opacity(25%)';
            offctx.drawImage(img, 0, 200, 100, 100);

            offctx.filter = 'brightness(0.4)';
            offctx.drawImage(img, 100, 200, 100, 100);

            offctx.filter = 'contrast(200%)';
            offctx.drawImage(img, 200, 200, 100, 100);

            offctx.filter = 'blur(5px)';
            offctx.drawImage(img, 0, 300, 100, 100);

E
ester.zhou 已提交
2011
            var image = this.offCanvas.transferToImageBitmap()
E
ester.zhou 已提交
2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
    }
  }
```

![filterDemo](figures/filterDemo.jpeg)
E
esterzhou 已提交
2022 2023 2024 2025 2026 2027 2028 2029


### resetTransform

resetTransform(): void

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

E
ester.zhou 已提交
2030 2031
Since API version 9, this API is supported in ArkTS widgets.

E
esterzhou 已提交
2032 2033 2034 2035 2036

### direction

direction(direction: CanvasDirection): void

E
ester.zhou 已提交
2037
Sets the text direction for drawing text.
E
esterzhou 已提交
2038

E
ester.zhou 已提交
2039 2040
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
2041 2042 2043 2044 2045 2046 2047 2048
**Example**
```ts
  // xxx.ets
  @Entry
  @Component
  struct DirectionDemoOff {
    private settings: RenderingContextSettings = new RenderingContextSettings(true);
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
E
ester.zhou 已提交
2049
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
ester.zhou 已提交
2050 2051 2052 2053 2054 2055 2056 2057

    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2058 2059
            var offContext = this.offCanvas.getContext("2d", this.settings)
            let offctx = offContext
E
ester.zhou 已提交
2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
            offctx.font = '48px serif';
            offctx.textAlign = 'start'
            offctx.fillText("Hi ltr!", 200, 50);

            offctx.direction = "rtl";
            offctx.fillText("Hi rtl!", 200, 100);

            var image = offctx.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
    }
  }
```

![directionDemo](figures/directionDemo.jpeg)


E
esterzhou 已提交
2080

Z
zengyawen 已提交
2081
### rotate
Z
zengyawen 已提交
2082

E
ester.zhou 已提交
2083
rotate(angle: number): void
Z
zengyawen 已提交
2084 2085 2086

Rotates a canvas clockwise around its coordinate axes.

E
ester.zhou 已提交
2087 2088
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
2089
 **Parameters**
E
ester.zhou 已提交
2090

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

E
ester.zhou 已提交
2095
 **Example**
H
HelloCrease 已提交
2096

2097
  ```ts
2098
  // xxx.ets
Z
zengyawen 已提交
2099 2100 2101 2102 2103
  @Entry
  @Component
  struct Rotate {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
2104
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
2105

Z
zengyawen 已提交
2106 2107 2108 2109 2110 2111 2112
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2113 2114 2115 2116
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.rotate(45 * Math.PI / 180)
            offContext.fillRect(70, 20, 50, 50)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
2117 2118
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2119
      }
Z
zengyawen 已提交
2120 2121
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2122
    }
Z
zengyawen 已提交
2123 2124 2125
  }
  ```

E
ester.zhou 已提交
2126
  ![en-us_image_0000001238952405](figures/en-us_image_0000001238952405.png)
Z
zengyawen 已提交
2127 2128 2129 2130 2131 2132


### scale

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

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

E
ester.zhou 已提交
2135 2136
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
2137
 **Parameters**
Z
zengyawen 已提交
2138

E
ester.zhou 已提交
2139
| Name  | Type    | Mandatory  | Default Value | Description         |
E
ester.zhou 已提交
2140 2141 2142 2143
| ---- | ------ | ---- | ---- | ----------- |
| x    | number | Yes   | 0    | Horizontal scale factor.|
| y    | number | Yes   | 0    | Vertical scale factor.|

E
ester.zhou 已提交
2144
 **Example**
H
HelloCrease 已提交
2145

2146
  ```ts
2147
  // xxx.ets
Z
zengyawen 已提交
2148 2149 2150 2151 2152
  @Entry
  @Component
  struct Scale {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
2153
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
2154

Z
zengyawen 已提交
2155 2156 2157 2158 2159 2160 2161
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2162 2163 2164 2165 2166 2167
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.lineWidth = 3
            offContext.strokeRect(30, 30, 50, 50)
            offContext.scale(2, 2) // Scale to 200%
            offContext.strokeRect(30, 30, 50, 50)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
2168 2169
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2170
      }
Z
zengyawen 已提交
2171 2172
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2173
    }
Z
zengyawen 已提交
2174 2175
  }
  ```
Z
zengyawen 已提交
2176

E
ester.zhou 已提交
2177
  ![en-us_image_0000001193872498](figures/en-us_image_0000001193872498.png)
Z
zengyawen 已提交
2178 2179


Z
zengyawen 已提交
2180
### transform
Z
zengyawen 已提交
2181

E
ester.zhou 已提交
2182
transform(a: number, b: number, c: number, d: number, e: number, f: number): void
Z
zengyawen 已提交
2183 2184 2185

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.

E
ester.zhou 已提交
2186 2187
Since API version 9, this API is supported in ArkTS widgets.

2188
> **NOTE**
Z
zengyawen 已提交
2189
> 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 已提交
2190
>
Z
zengyawen 已提交
2191
> - x' = scaleX \* x + skewY \* y + translateX
H
HelloCrease 已提交
2192
>
Z
zengyawen 已提交
2193 2194
> - y' = skewX \* x + scaleY \* y + translateY

E
ester.zhou 已提交
2195
**Parameters**
Z
zengyawen 已提交
2196

E
ester.zhou 已提交
2197
| Name  | Type    | Mandatory  | Default Value | Description                  |
E
esterzhou 已提交
2198 2199 2200 2201 2202 2203 2204
| ---- | ------ | ---- | ---- | -------------------- |
| a    | number | Yes   | 0    | X-axis scale.    |
| b    | number | Yes   | 0    | X-axis skew.     |
| c    | number | Yes   | 0    | Y-axis skew.     |
| d    | number | Yes   | 0    | Y-axis scale.    |
| e    | number | Yes   | 0    | X-axis translation.|
| f    | number | Yes   | 0    | Y-axis translation.|
E
ester.zhou 已提交
2205

E
ester.zhou 已提交
2206
 **Example**
H
HelloCrease 已提交
2207

2208
  ```ts
2209
  // xxx.ets
Z
zengyawen 已提交
2210 2211 2212 2213 2214
  @Entry
  @Component
  struct Transform {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
2215
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
2216

Z
zengyawen 已提交
2217 2218 2219 2220 2221 2222 2223
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.fillStyle = 'rgb(0,0,0)'
            offContext.fillRect(0, 0, 100, 100)
            offContext.transform(1, 0.5, -0.5, 1, 10, 10)
            offContext.fillStyle = 'rgb(255,0,0)'
            offContext.fillRect(0, 0, 100, 100)
            offContext.transform(1, 0.5, -0.5, 1, 10, 10)
            offContext.fillStyle = 'rgb(0,0,255)'
            offContext.fillRect(0, 0, 100, 100)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
2234 2235
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2236
      }
Z
zengyawen 已提交
2237 2238
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2239
    }
Z
zengyawen 已提交
2240 2241 2242
  }
  ```

E
ester.zhou 已提交
2243
  ![en-us_image_0000001239032431](figures/en-us_image_0000001239032431.png)
Z
zengyawen 已提交
2244 2245 2246 2247


### setTransform

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

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

E
ester.zhou 已提交
2252 2253
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
2254 2255
**Parameters**

E
ester.zhou 已提交
2256
| Name  | Type    | Mandatory  | Default Value | Description                  |
E
esterzhou 已提交
2257 2258 2259 2260 2261 2262 2263
| ---- | ------ | ---- | ---- | -------------------- |
| a    | number | Yes   | 0    | X-axis scale.    |
| b    | number | Yes   | 0    | X-axis skew.     |
| c    | number | Yes   | 0    | Y-axis skew.     |
| d    | number | Yes   | 0    | Y-axis scale.    |
| e    | number | Yes   | 0    | X-axis translation.|
| f    | number | Yes   | 0    | Y-axis translation.|
Z
zengyawen 已提交
2264

E
ester.zhou 已提交
2265
 **Example**
H
HelloCrease 已提交
2266

2267
  ```ts
2268
  // xxx.ets
Z
zengyawen 已提交
2269 2270 2271 2272 2273
  @Entry
  @Component
  struct SetTransform {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
2274
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
2275

Z
zengyawen 已提交
2276 2277 2278 2279 2280 2281 2282
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2283 2284 2285 2286 2287 2288 2289
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.fillStyle = 'rgb(255,0,0)'
            offContext.fillRect(0, 0, 100, 100)
            offContext.setTransform(1,0.5, -0.5, 1, 10, 10)
            offContext.fillStyle = 'rgb(0,0,255)'
            offContext.fillRect(0, 0, 100, 100)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
2290 2291
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2292
      }
Z
zengyawen 已提交
2293 2294
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2295
    }
Z
zengyawen 已提交
2296 2297
  }
  ```
Z
zengyawen 已提交
2298

E
esterzhou 已提交
2299
  ![en-us_image_0000001193872526](figures/en-us_image_0000001193872526.png)
Z
zengyawen 已提交
2300 2301


E
ester.zhou 已提交
2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
### setTransform

setTransform(transform?: Matrix2D): void

Resets the current transformation and creates a new transformation matrix based on the specified **Matrix2D** object.

Since API version 9, this API is supported in ArkTS widgets.

**Parameters**

| Name   | Type                                      | Mandatory  | Default Value | Description   |
| --------- | ---------------------------------------- | ---- | ---- | ----- |
| transform | [Matrix2D](ts-components-canvas-matrix2d.md#Matrix2D) | No   | null | Matrix object. |

### getTransform

getTransform(): Matrix2D

Obtains the current transformation matrix being applied to the context.

Since API version 9, this API is supported in ArkTS widgets.

**Return value**

| Type                                      | Description   |
| ---------------------------------------- | ----- |
| [Matrix2D](ts-components-canvas-matrix2d.md#Matrix2D) | Matrix object.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct TransFormDemo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true);
    private context1: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
    private offcontext1: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 100, this.settings);
    private context2: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
    private offcontext2: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 100, this.settings);

    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Text('context1');
        Canvas(this.context1)
          .width('230vp')
          .height('120vp')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offcontext1.fillRect(50, 50, 50, 50);
            this.offcontext1.setTransform(1.2, Math.PI/8, Math.PI/6, 0.5, 30, -25);
            this.offcontext1.fillRect(50, 50, 50, 50);
            var image = this.offcontext1.transferToImageBitmap();
            this.context1.transferFromImageBitmap(image);
          })
        Text('context2');
        Canvas(this.context2)
          .width('230vp')
          .height('120vp')
          .backgroundColor('#0ffff0')
          .onReady(() =>{
            this.offcontext2.fillRect(50, 50, 50, 50);
            let storedTransform = this.offcontext1.getTransform();
            console.log("Matrix [scaleX = " + storedTransform.scaleX + ", scaleY = " + storedTransform.scaleY +
            ", rotateX = " + storedTransform.rotateX + ", rotateY = " + storedTransform.rotateY +
            ", translateX = " + storedTransform.translateX + ", translateY = " + storedTransform.translateY + "]")
            this.offcontext2.setTransform(storedTransform);
            this.offcontext2.fillRect(50,50,50,50);
            var image = this.offcontext2.transferToImageBitmap();
            this.context2.transferFromImageBitmap(image);
          })
      }
      .width('100%')
      .height('100%')
    }
  }
  ```

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

Z
zengyawen 已提交
2382
### translate
Z
zengyawen 已提交
2383

Z
zengyawen 已提交
2384
translate(x: number, y: number): void
Z
zengyawen 已提交
2385 2386 2387

Moves the origin of the coordinate system.

E
ester.zhou 已提交
2388 2389
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
2390
 **Parameters**
E
ester.zhou 已提交
2391

E
ester.zhou 已提交
2392
| Name  | Type    | Mandatory  | Default Value | Description      |
E
ester.zhou 已提交
2393 2394 2395
| ---- | ------ | ---- | ---- | -------- |
| x    | number | Yes   | 0    | X-axis translation.|
| y    | number | Yes   | 0    | Y-axis translation.|
Z
zengyawen 已提交
2396

E
ester.zhou 已提交
2397
 **Example**
H
HelloCrease 已提交
2398

2399
  ```ts
2400
  // xxx.ets
Z
zengyawen 已提交
2401 2402 2403 2404 2405
  @Entry
  @Component
  struct Translate {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
2406
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
2407

Z
zengyawen 已提交
2408 2409 2410 2411 2412 2413 2414
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2415 2416 2417 2418 2419
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.fillRect(10, 10, 50, 50)
            offContext.translate(70, 70)
            offContext.fillRect(10, 10, 50, 50)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
2420 2421 2422 2423 2424 2425 2426 2427 2428
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
    }
  }
  ```

E
esterzhou 已提交
2429
  ![en-us_image_0000001238832413](figures/en-us_image_0000001238832413.png)
Z
zengyawen 已提交
2430 2431 2432 2433


### drawImage

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

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

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

E
ester.zhou 已提交
2440
Draws an image on the canvas.
Z
zengyawen 已提交
2441

E
ester.zhou 已提交
2442 2443
Since API version 9, this API is supported in ArkTS widgets, except that **PixelMap** objects are not supported.

E
ester.zhou 已提交
2444
 **Parameters**
Z
zengyawen 已提交
2445

E
ester.zhou 已提交
2446
| Name   | Type                                      | Mandatory  | Default Value | Description                           |
E
esterzhou 已提交
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
| ----- | ---------------------------------------- | ---- | ---- | ----------------------------- |
| image | [ImageBitmap](ts-components-canvas-imagebitmap.md) or [PixelMap](../apis/js-apis-image.md#pixelmap7)| Yes   | null | Image resource. For details, see **ImageBitmap** or **PixelMap**.|
| sx    | number                                   | No   | 0    | X-coordinate of the upper left corner of the rectangle used to crop the source image.         |
| sy    | number                                   | No   | 0    | Y-coordinate of the upper left corner of the rectangle used to crop the source image.         |
| sw    | number                                   | No   | 0    | Target width to crop the source image.               |
| sh    | number                                   | No   | 0    | Target height to crop the source image.               |
| dx    | number                                   | Yes   | 0    | X-coordinate of the upper left corner of the drawing area on the canvas.               |
| dy    | number                                   | Yes   | 0    | Y-coordinate of the upper left corner of the drawing area on the canvas.         |
| dw    | number                                   | No   | 0    | Width of the drawing area.                     |
| dh    | number                                   | No   | 0    | Height of the drawing area.                     |
Z
zengyawen 已提交
2457

E
ester.zhou 已提交
2458 2459

 **Example**
H
HelloCrease 已提交
2460

2461
  ```ts
2462
  // xxx.ets
Z
zengyawen 已提交
2463 2464
  @Entry
  @Component
E
esterzhou 已提交
2465
  struct DrawImage {
Z
zengyawen 已提交
2466 2467 2468
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private img:ImageBitmap = new ImageBitmap("common/images/icon.jpg")
E
ester.zhou 已提交
2469
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
2470 2471 2472
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
Z
zengyawen 已提交
2473 2474
        .width('100%')
        .height('100%')
Z
zengyawen 已提交
2475 2476
        .backgroundColor('#ffff00')
        .onReady(() => {
E
ester.zhou 已提交
2477 2478 2479
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.drawImage( this.img,0,0,400,200)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
2480 2481
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
2482
      }
Z
zengyawen 已提交
2483 2484
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2485
    }
Z
zengyawen 已提交
2486 2487
  }
  ```
Z
zengyawen 已提交
2488

E
ester.zhou 已提交
2489
  ![en-us_image_0000001238712447](figures/en-us_image_0000001238712447.png)
Z
zengyawen 已提交
2490 2491


Z
zengyawen 已提交
2492
### createImageData
Z
zengyawen 已提交
2493

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

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

E
ester.zhou 已提交
2498 2499
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
2500
 **Parameters**
Z
zengyawen 已提交
2501

E
ester.zhou 已提交
2502
| Name  | Type    | Mandatory  | Default Value  | Description           |
E
esterzhou 已提交
2503 2504 2505
| ---- | ------ | ---- | ---- | ------------- |
| sw   | number | Yes   | 0    | Width of the **ImageData** object.|
| sh   | number | Yes   | 0    | Height of the **ImageData** object.|
Z
zengyawen 已提交
2506 2507


E
ester.zhou 已提交
2508 2509
createImageData(imageData: ImageData): ImageData

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

E
ester.zhou 已提交
2512 2513
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
2514
 **Parameters**
Z
zengyawen 已提交
2515

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

E
ester.zhou 已提交
2520 2521
 **Return value**

E
esterzhou 已提交
2522 2523 2524
| Type                                      | Description           |
| ---------------------------------------- | ------------- |
| [ImageData](ts-components-canvas-imagedata.md) | New **ImageData** object.|
Z
zengyawen 已提交
2525

2526 2527 2528 2529 2530
### getPixelMap

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

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

E
ester.zhou 已提交
2532
 **Parameters**
E
ester.zhou 已提交
2533

E
ester.zhou 已提交
2534
| Name  | Type    | Mandatory  | Default Value | Description             |
E
esterzhou 已提交
2535 2536 2537 2538 2539
| ---- | ------ | ---- | ---- | --------------- |
| sx   | number | Yes   | 0    | X-coordinate of the upper left corner of the output area.|
| sy   | number | Yes   | 0    | Y-coordinate of the upper left corner of the output area.|
| sw   | number | Yes   | 0    | Width of the output area.    |
| sh   | number | Yes   | 0    | Height of the output area.    |
E
ester.zhou 已提交
2540 2541 2542

**Return value**

E
esterzhou 已提交
2543 2544 2545
| Type                                      | Description          |
| ---------------------------------------- | ------------ |
| [PixelMap](../apis/js-apis-image.md#pixelmap7) | **PixelMap** object.|
E
ester.zhou 已提交
2546

E
ester.zhou 已提交
2547 2548 2549 2550 2551 2552 2553 2554
### setPixelMap

setPixelMap(value?: PixelMap): void

Draws the input [PixelMap](../apis/js-apis-image.md#pixelmap7) object on the canvas.

 **Parameters**

E
ester.zhou 已提交
2555
| Name  | Type    | Mandatory  | Default Value | Description             |
E
ester.zhou 已提交
2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567
| ---- | ------ | ---- | ---- | --------------- |
| sx   | number | Yes   | 0    | X-coordinate of the upper left corner of the output area.|
| sy   | number | Yes   | 0    | Y-coordinate of the upper left corner of the output area.|
| sw   | number | Yes   | 0    | Width of the output area.    |
| sh   | number | Yes   | 0    | Height of the output area.    |

**Return value**

| Type                                      | Description          |
| ---------------------------------------- | ------------ |
| [PixelMap](../apis/js-apis-image.md#pixelmap7) | **PixelMap** object.|

Z
zengyawen 已提交
2568 2569 2570

### getImageData

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

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

E
ester.zhou 已提交
2575 2576
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
2577
 **Parameters**
E
ester.zhou 已提交
2578

E
ester.zhou 已提交
2579
| Name  | Type    | Mandatory  | Default Value | Description             |
E
ester.zhou 已提交
2580 2581 2582 2583 2584 2585 2586 2587
| ---- | ------ | ---- | ---- | --------------- |
| sx   | number | Yes   | 0    | X-coordinate of the upper left corner of the output area.|
| sy   | number | Yes   | 0    | Y-coordinate of the upper left corner of the output area.|
| sw   | number | Yes   | 0    | Width of the output area.    |
| sh   | number | Yes   | 0    | Height of the output area.    |

   **Return value**

E
esterzhou 已提交
2588 2589 2590 2591
| Type                                      | Description           |
| ---------------------------------------- | ------------- |
| [ImageData](ts-components-canvas-imagedata.md) | New **ImageData** object.|

E
ester.zhou 已提交
2592

E
esterzhou 已提交
2593 2594 2595 2596
**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2597 2598 2599 2600 2601
  @Entry
  @Component
  struct GetImageData {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
2602
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
ester.zhou 已提交
2603
    private img:ImageBitmap = new ImageBitmap("/common/images/1234.png")
E
esterzhou 已提交
2604

E
ester.zhou 已提交
2605 2606 2607 2608 2609 2610 2611
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2612 2613 2614 2615 2616
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.drawImage(this.img,0,0,130,130)
            var imagedata = offContext.getImageData(50,50,130,130)
            offContext.putImageData(imagedata,150,150)
            var image = this.offCanvas.transferToImageBitmap()
E
ester.zhou 已提交
2617 2618 2619 2620 2621
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
E
esterzhou 已提交
2622 2623 2624 2625 2626
    }
  }
  ```

  ![en-us_image_000000127777780](figures/en-us_image_000000127777780.png)
Z
zengyawen 已提交
2627 2628 2629 2630


### putImageData

E
ester.zhou 已提交
2631
putImageData(imageData: Object, dx: number | string, dy: number | string): void
E
ester.zhou 已提交
2632

E
ester.zhou 已提交
2633
putImageData(imageData: Object, dx: number | string, dy: number | string, dirtyX: number | string, dirtyY: number | string, dirtyWidth?: number | string, dirtyHeight: number | string): void
Z
zengyawen 已提交
2634

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

E
ester.zhou 已提交
2637 2638
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
2639
 **Parameters**
E
ester.zhou 已提交
2640

E
ester.zhou 已提交
2641 2642 2643 2644 2645 2646 2647 2648 2649
| Name         | Type                                      | Mandatory  | Default Value         | Description                           |
| ----------- | ---------------------------------------- | ---- | ------------ | ----------------------------- |
| imagedata   | Object                                   | Yes   | null         | **ImageData** object with pixels to put onto the canvas.           |
| dx          | number \| string<sup>10+</sup> | Yes   | 0            | X-axis offset of the rectangular area on the canvas.               |
| dy          | number \| string<sup>10+</sup> | Yes   | 0            | Y-axis offset of the rectangular area on the canvas.               |
| dirtyX      | number \| string<sup>10+</sup> | No   | 0            | X-axis offset of the upper left corner of the rectangular area relative to that of the source image.|
| dirtyY      | number \| string<sup>10+</sup> | No   | 0            | Y-axis offset of the upper left corner of the rectangular area relative to that of the source image.|
| dirtyWidth  | number \| string<sup>10+</sup> | No   | Width of the **ImageData** object| Width of the rectangular area to crop the source image.              |
| dirtyHeight | number \| string<sup>10+</sup> | No   | Height of the **ImageData** object| Height of the rectangular area to crop the source image.              |
Z
zengyawen 已提交
2650

E
ester.zhou 已提交
2651
 **Example**
H
HelloCrease 已提交
2652

2653
  ```ts
2654
  // xxx.ets
Z
zengyawen 已提交
2655 2656 2657 2658 2659
  @Entry
  @Component
  struct PutImageData {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
2660
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
2661 2662 2663
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
Z
zengyawen 已提交
2664 2665 2666
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
Z
zengyawen 已提交
2667
          .onReady(() =>{
E
ester.zhou 已提交
2668 2669
            var offContext = this.offCanvas.getContext("2d", this.settings)
            var imageData = offContext.createImageData(100, 100)
Z
zengyawen 已提交
2670 2671 2672 2673 2674 2675
            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
            }
E
ester.zhou 已提交
2676 2677
            offContext.putImageData(imageData, 10, 10)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
2678
            this.context.transferFromImageBitmap(image)
Z
zengyawen 已提交
2679 2680
          })
      }
Z
zengyawen 已提交
2681 2682
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2683
    }
Z
zengyawen 已提交
2684 2685
  }
  ```
Z
zengyawen 已提交
2686

E
ester.zhou 已提交
2687
  ![en-us_image_0000001194192464](figures/en-us_image_0000001194192464.png)
Z
zengyawen 已提交
2688

E
ester.zhou 已提交
2689 2690 2691 2692 2693 2694
### setLineDash

setLineDash(segments: number[]): void

Sets the dash line style.

E
ester.zhou 已提交
2695 2696
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
2697 2698
**Parameters**

E
ester.zhou 已提交
2699
| Name      | Type      | Description                 |
E
esterzhou 已提交
2700
| -------- | -------- | ------------------- |
E
ester.zhou 已提交
2701 2702 2703 2704 2705
| segments | number[] | An array of numbers that specify distances to alternately draw a line and a gap.|

**Example**

  ```ts
E
ester.zhou 已提交
2706 2707 2708 2709 2710
  @Entry
  @Component
  struct SetLineDash {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
2711
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
ester.zhou 已提交
2712 2713 2714 2715 2716 2717 2718 2719
    
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2720 2721 2722 2723 2724
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.arc(100, 75, 50, 0, 6.28)
            offContext.setLineDash([10,20])
            offContext.stroke()
            var image = this.offCanvas.transferToImageBitmap()
E
ester.zhou 已提交
2725 2726 2727 2728 2729
            this.context.transferFromImageBitmap(image)
        })
      }
      .width('100%')
      .height('100%')
E
ester.zhou 已提交
2730 2731 2732
    }
  }
  ```
E
esterzhou 已提交
2733
  ![en-us_image_000000127777772](figures/en-us_image_000000127777772.png)
E
ester.zhou 已提交
2734 2735 2736 2737 2738 2739 2740 2741


### getLineDash

getLineDash(): number[]

Obtains the dash line style.

E
ester.zhou 已提交
2742 2743
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
2744 2745
**Return value**

E
esterzhou 已提交
2746 2747
| Type      | Description                      |
| -------- | ------------------------ |
E
ester.zhou 已提交
2748 2749 2750 2751 2752 2753
| number[] | An array describing the interval of alternate line segments and length of spacing.|

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2754 2755 2756 2757 2758 2759
  @Entry
  @Component
  struct OffscreenCanvasGetLineDash {
    @State message: string = 'Hello World'
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
2760
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
ester.zhou 已提交
2761 2762 2763 2764 2765 2766 2767 2768
    build() {
      Row() {
        Column() {
          Text(this.message)
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
            .onClick(()=>{
              console.error('before getlinedash clicked')
E
ester.zhou 已提交
2769 2770
              var offContext = this.offCanvas.getContext("2d", this.settings)
              let res = offContext.getLineDash()
E
ester.zhou 已提交
2771 2772 2773 2774 2775 2776 2777
              console.error(JSON.stringify(res))
            })
          Canvas(this.context)
            .width('100%')
            .height('100%')
            .backgroundColor('#ffff00')
            .onReady(() => {
E
ester.zhou 已提交
2778 2779 2780 2781 2782 2783
              var offContext = this.offCanvas.getContext("2d", this.settings)
              offContext.arc(100, 75, 50, 0, 6.28)
              offContext.setLineDash([10,20])
              offContext.stroke()
              let res = offContext.getLineDash()
              var image = this.offCanvas.transferToImageBitmap()
E
ester.zhou 已提交
2784 2785 2786 2787
              this.context.transferFromImageBitmap(image)
            })
        }
        .width('100%')
E
ester.zhou 已提交
2788
      }
E
ester.zhou 已提交
2789
      .height('100%')
E
ester.zhou 已提交
2790 2791 2792
    }
  }
  ```
E
esterzhou 已提交
2793
![en-us_image_000000127777778](figures/en-us_image_000000127777778.png) 
E
ester.zhou 已提交
2794 2795 2796 2797 2798 2799 2800 2801 2802



### toDataURL

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

Generates a URL containing image display information.

E
ester.zhou 已提交
2803 2804
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820
**Parameters**

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

**Return value**

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

**Example**

  ```ts
E
ester.zhou 已提交
2821 2822 2823 2824 2825 2826
  // xxx.ets  
  @Entry
  @Component
  struct ToDataURL {
      private settings: RenderingContextSettings = new RenderingContextSettings(true)
      private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
2827
      private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
ester.zhou 已提交
2828

E
ester.zhou 已提交
2829 2830 2831 2832 2833 2834 2835
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2836 2837
            var offContext = this.offCanvas.getContext("2d", this.settings)
            var dataURL = offContext.toDataURL()
E
ester.zhou 已提交
2838 2839 2840 2841
          })
      }
      .width('100%')
      .height('100%')
E
ester.zhou 已提交
2842 2843 2844 2845 2846
    }
  }
  ```


E
esterzhou 已提交
2847 2848 2849 2850
### imageSmoothingQuality

imageSmoothingQuality(quality: imageSmoothingQuality)

E
ester.zhou 已提交
2851
Sets the quality of image smoothing.
E
esterzhou 已提交
2852

E
ester.zhou 已提交
2853 2854
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
2855
 **Parameters**
E
esterzhou 已提交
2856

E
ester.zhou 已提交
2857
| Name     | Type                   | Description                                      |
E
esterzhou 已提交
2858
| ------- | --------------------- | ---------------------------------------- |
E
ester.zhou 已提交
2859 2860 2861 2862 2863 2864 2865 2866 2867 2868
| quality | imageSmoothingQuality | Quality of image smoothing.<br>- **'low'**: low quality.<br>- **'medium'**: medium quality.<br>- **'high'**: high quality.|

**Example**
```ts
  // xxx.ets
  @Entry
  @Component
  struct ImageSmoothingQualityDemoOff {
    private settings: RenderingContextSettings = new RenderingContextSettings(true);
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
E
ester.zhou 已提交
2869
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
ester.zhou 已提交
2870
    private img:ImageBitmap = new ImageBitmap("common/images/example.jpg");
E
esterzhou 已提交
2871

E
ester.zhou 已提交
2872 2873 2874 2875 2876 2877 2878
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2879 2880
            var offContext = this.offCanvas.getContext("2d", this.settings)
            let offctx = offContext
E
ester.zhou 已提交
2881 2882 2883 2884
            offctx.imageSmoothingEnabled = true
            offctx.imageSmoothingQuality = 'high'
            offctx.drawImage(this.img, 0, 0, 400, 200)

E
ester.zhou 已提交
2885
            var image = this.offCanvas.transferToImageBitmap()
E
ester.zhou 已提交
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
    }
  }
```

![ImageSmoothingQualityDemo](figures/ImageSmoothingQualityDemo.jpeg)
E
esterzhou 已提交
2896

E
ester.zhou 已提交
2897 2898 2899 2900 2901 2902 2903 2904 2905 2906
### transferToImageBitmap

transferToImageBitmap(): ImageBitmap

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

**Return value**

| Type                                      | Description             |
| ---------------------------------------- | --------------- |
E
esterzhou 已提交
2907
| [ImageBitmap](ts-components-canvas-imagebitmap.md) | Pixel data rendered on the **OffscreenCanvas**.|
E
ester.zhou 已提交
2908

E
ester.zhou 已提交
2909 2910

 **Example**
E
ester.zhou 已提交
2911 2912 2913 2914 2915

  ```ts
  // xxx.ets
  @Entry
  @Component
E
esterzhou 已提交
2916
  struct PutImageData {
E
ester.zhou 已提交
2917 2918
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
2919
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
esterzhou 已提交
2920

E
ester.zhou 已提交
2921 2922 2923 2924 2925 2926 2927
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2928 2929
            var offContext = this.offCanvas.getContext("2d", this.settings)
            var imageData = offContext.createImageData(100, 100)
E
esterzhou 已提交
2930 2931 2932 2933 2934 2935
            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
            }
E
ester.zhou 已提交
2936 2937
            offContext.putImageData(imageData, 10, 10)
            var image = this.offCanvas.transferToImageBitmap()
E
ester.zhou 已提交
2938 2939 2940 2941 2942 2943 2944 2945
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
    }
  }
  ```
E
esterzhou 已提交
2946
![en-us_image_0000001238952387](figures/en-us_image_0000001238952387.png) 
Z
zengyawen 已提交
2947

Z
zengyawen 已提交
2948
### restore
Z
zengyawen 已提交
2949

Z
zengyawen 已提交
2950
restore(): void
Z
zengyawen 已提交
2951 2952 2953

Restores the saved drawing context.

E
ester.zhou 已提交
2954 2955
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
2956
 **Example**
H
HelloCrease 已提交
2957

2958
  ```ts
2959
  // xxx.ets
E
ester.zhou 已提交
2960 2961 2962 2963 2964
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
2965
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
ester.zhou 已提交
2966 2967 2968 2969 2970 2971 2972 2973
    
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2974 2975 2976 2977 2978 2979 2980
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.save() // save the default state
            offContext.fillStyle = "#00ff00"
            offContext.fillRect(20, 20, 100, 100)
            offContext.restore() // restore to the default state
            offContext.fillRect(150, 75, 100, 100)
            var image = this.offCanvas.transferToImageBitmap()
E
ester.zhou 已提交
2981 2982 2983 2984 2985
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2986
    }
Z
zengyawen 已提交
2987 2988
  }
  ```
E
esterzhou 已提交
2989
![en-us_image_000000127777781](figures/en-us_image_000000127777781.png) 
Z
zengyawen 已提交
2990 2991


Z
zengyawen 已提交
2992
### save
Z
zengyawen 已提交
2993

Z
zengyawen 已提交
2994
save(): void
Z
zengyawen 已提交
2995 2996 2997

Saves the current drawing context.

E
ester.zhou 已提交
2998 2999
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
3000
 **Example**
H
HelloCrease 已提交
3001

3002
  ```ts
3003
  // xxx.ets
E
ester.zhou 已提交
3004 3005 3006 3007 3008
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
3009
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
ester.zhou 已提交
3010 3011 3012 3013 3014 3015 3016 3017
    
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
3018 3019 3020 3021 3022 3023 3024
            var offContext = this.offCanvas.getContext("2d", this.settings)
            offContext.save() // save the default state
            offContext.fillStyle = "#00ff00"
            offContext.fillRect(20, 20, 100, 100)
            offContext.restore() // restore to the default state
            offContext.fillRect(150, 75, 100, 100)
            var image = this.offCanvas.transferToImageBitmap()
E
ester.zhou 已提交
3025 3026 3027 3028 3029
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
3030
    }
Z
zengyawen 已提交
3031 3032
  }
  ```
E
esterzhou 已提交
3033
![en-us_image_000000127777781](figures/en-us_image_000000127777781.png) 
Z
zengyawen 已提交
3034 3035


Z
zengyawen 已提交
3036
### createLinearGradient
Z
zengyawen 已提交
3037

Z
zengyawen 已提交
3038
createLinearGradient(x0: number, y0: number, x1: number, y1: number): void
Z
zengyawen 已提交
3039 3040 3041

Creates a linear gradient.

E
ester.zhou 已提交
3042 3043
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
3044
 **Parameters**
E
ester.zhou 已提交
3045

E
ester.zhou 已提交
3046
| Name  | Type    | Mandatory  | Default Value | Description      |
E
ester.zhou 已提交
3047 3048 3049 3050 3051
| ---- | ------ | ---- | ---- | -------- |
| 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 已提交
3052

E
ester.zhou 已提交
3053
 **Example**
H
HelloCrease 已提交
3054

3055
  ```ts
3056
  // xxx.ets
Z
zengyawen 已提交
3057 3058 3059 3060 3061
  @Entry
  @Component
  struct CreateLinearGradient {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
3062
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
3063 3064 3065 3066 3067 3068 3069 3070
    
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
3071 3072
            var offContext = this.offCanvas.getContext("2d", this.settings)
            var grad = offContext.createLinearGradient(50,0, 300,100)
E
ester.zhou 已提交
3073 3074 3075
            grad.addColorStop(0.0, '#ff0000')
            grad.addColorStop(0.5, '#ffffff')
            grad.addColorStop(1.0, '#00ff00')
E
ester.zhou 已提交
3076 3077 3078
            offContext.fillStyle = grad
            offContext.fillRect(0, 0, 400, 400)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
3079 3080
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
3081
      }
Z
zengyawen 已提交
3082 3083
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
3084
    }
Z
zengyawen 已提交
3085 3086 3087
  }
  ```

E
ester.zhou 已提交
3088
  ![en-us_image_0000001194352460](figures/en-us_image_0000001194352460.png)
Z
zengyawen 已提交
3089 3090


Z
zengyawen 已提交
3091
### createRadialGradient
Z
zengyawen 已提交
3092

Z
zengyawen 已提交
3093
createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number): void
Z
zengyawen 已提交
3094 3095 3096

Creates a linear gradient.

E
ester.zhou 已提交
3097 3098 3099
Since API version 9, this API is supported in ArkTS widgets.

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

E
ester.zhou 已提交
3101
| Name  | Type    | Mandatory  | Default Value | Description               |
E
ester.zhou 已提交
3102 3103 3104 3105 3106 3107 3108
| ---- | ------ | ---- | ---- | ----------------- |
| 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 已提交
3109

E
ester.zhou 已提交
3110
  **Example** 
H
HelloCrease 已提交
3111

3112
  ```ts
3113
  // xxx.ets
Z
zengyawen 已提交
3114 3115 3116 3117 3118
  @Entry
  @Component
  struct CreateRadialGradient {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
3119
    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
Z
zengyawen 已提交
3120 3121 3122 3123 3124 3125 3126 3127
    
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
3128 3129
            var offContext = this.offCanvas.getContext("2d", this.settings)
            var grad = offContext.createRadialGradient(200,200,50, 200,200,200)
E
ester.zhou 已提交
3130 3131 3132
            grad.addColorStop(0.0, '#ff0000')
            grad.addColorStop(0.5, '#ffffff')
            grad.addColorStop(1.0, '#00ff00')
E
ester.zhou 已提交
3133 3134 3135
            offContext.fillStyle = grad
            offContext.fillRect(0, 0, 440, 440)
            var image = this.offCanvas.transferToImageBitmap()
Z
zengyawen 已提交
3136 3137
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
3138
      }
Z
zengyawen 已提交
3139 3140
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
3141
    }
Z
zengyawen 已提交
3142 3143
  }
  ```
Z
zengyawen 已提交
3144

E
ester.zhou 已提交
3145
  ![en-us_image_0000001238952407](figures/en-us_image_0000001238952407.png)
Z
zengyawen 已提交
3146

E
ester.zhou 已提交
3147 3148 3149 3150 3151 3152 3153 3154
### createConicGradient<sup>10+</sup>

createConicGradient(startAngle: number, x: number, y: number): CanvasGradient

Creates a conic gradient.

**Parameters**

E
ester.zhou 已提交
3155 3156 3157 3158 3159
| Name        | Type    | Mandatory  | Default Value | Description                                 |
| ---------- | ------ | ---- | ---- | ----------------------------------- |
| startAngle | number | Yes   | 0    | Angle at which the gradient starts, in radians. The angle measurement starts horizontally from the right side of the center and moves clockwise.|
| x          | number | Yes   | 0    | X-coordinate of the center of the conic gradient, in vp.                  |
| y          | number | Yes   | 0    | Y-coordinate of the center of the conic gradient, in vp.                  |
E
ester.zhou 已提交
3160 3161 3162 3163 3164 3165 3166 3167 3168 3169

**Example**

```ts
// xxx.ets
@Entry
@Component
struct OffscreenCanvasConicGradientPage {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
3170
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
E
ester.zhou 已提交
3171 3172 3173 3174 3175 3176 3177 3178

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffffff')
        .onReady(() =>{
E
ester.zhou 已提交
3179 3180
          var offContext = this.offCanvas.getContext("2d", this.settings)
          var grad = offContext.createConicGradient(0, 50, 80)
E
ester.zhou 已提交
3181 3182 3183
          grad.addColorStop(0.0, '#ff0000')
          grad.addColorStop(0.5, '#ffffff')
          grad.addColorStop(1.0, '#00ff00')
E
ester.zhou 已提交
3184 3185 3186
          offContext.fillStyle = grad
          offContext.fillRect(0, 30, 100, 100)
          var image = this.offCanvas.transferToImageBitmap()
E
ester.zhou 已提交
3187 3188 3189 3190 3191 3192 3193 3194 3195 3196
          this.context.transferFromImageBitmap(image)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

  ![en-us_image_0000001239032419](figures/en-us_image_0000001239032420.png)