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

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

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


E
esterzhou 已提交
10

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

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

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

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

E
esterzhou 已提交
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.                      |
| setting | [RenderingContextSettings](ts-canvasrenderingcontext2d.md#renderingcontextsettings) | Yes  | See RenderingContextSettings.|
Z
zengyawen 已提交
24 25 26 27


## Attributes

E
ester.zhou 已提交
28
| Name                                                 | Type                                                        | Description                                                        |
E
ester.zhou 已提交
29
| ----------------------------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
E
ester.zhou 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| [fillStyle](#fillstyle)                               | string \|number<sup>10+</sup> \|[CanvasGradient](ts-components-canvas-canvasgradient.md) \| [CanvasPattern](#canvaspattern) | Style to fill an area.<br>- When the type is **string**, this attribute indicates the color of the filling area.<br>- When the type is **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](#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.|
| [font](#font)                                         | string                                                       | Font style.<br>Syntax: ctx.font='font-size font-family'<br>- (Optional) **font-size**: font size and row height. The unit can only be pixels.<br>(Optional) **font-family**: font family.<br>Syntax: ctx.font='font-style font-weight font-size font-family'<br>- (Optional) **font-style**: font style. Available values are **normal** and **italic**.<br>- (Optional) **font-weight**: font weight. Available values are as follows: **normal**, **bold**, **bolder**, **lighter**, **100**, **200**, **300**, **400**, **500**, **600**, **700**, **800**, **900**.<br>- (Optional) **font-size**: font size and row height. The unit can only be pixels.<br>- (Optional) **font-family**: font family. Available values are **sans-serif**, **serif**, and **monospace**.<br>Default value: **'normal normal 14px sans-serif'**<br>Since API version 9, this API is supported in ArkTS widgets.|
| [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.|
E
esterzhou 已提交
47

48
> **NOTE**
E
ester.zhou 已提交
49
> 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 已提交
50 51 52 53


### fillStyle

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

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.fillStyle = '#0000ff'
E
ester.zhou 已提交
71
          this.offContext.fillRect(20, 20, 150, 100)
E
ester.zhou 已提交
72 73
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
Z
zengyawen 已提交
74 75 76 77 78 79 80 81
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
82
![en-us_image_0000001193872516](figures/en-us_image_0000001193872516.png)
Z
zengyawen 已提交
83 84 85


### lineWidth
Z
zengyawen 已提交
86

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

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

E
ester.zhou 已提交
115
![en-us_image_0000001238832403](figures/en-us_image_0000001238832403.png)
Z
zengyawen 已提交
116 117 118


### strokeStyle
Z
zengyawen 已提交
119

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

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

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


### lineCap

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

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

E
ester.zhou 已提交
186
![en-us_image_0000001194192454](figures/en-us_image_0000001194192454.PNG)
Z
zengyawen 已提交
187 188 189


### lineJoin
Z
zengyawen 已提交
190

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

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

E
ester.zhou 已提交
224
![en-us_image_0000001194352450](figures/en-us_image_0000001194352450.png)
Z
zengyawen 已提交
225 226 227


### miterLimit
Z
zengyawen 已提交
228

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

E
ester.zhou 已提交
262
![en-us_image_0000001238952397](figures/en-us_image_0000001238952397.png)
Z
zengyawen 已提交
263 264 265


### font
Z
zengyawen 已提交
266

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

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


### textAlign
Z
zengyawen 已提交
299

300 301
```ts
// xxx.ets
Z
zengyawen 已提交
302 303
@Entry
@Component
E
ester.zhou 已提交
304 305
struct CanvasExample {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
        this.offContext.strokeStyle = '#0000ff'
        this.offContext.moveTo(140, 10)
        this.offContext.lineTo(140, 160)
        this.offContext.stroke()

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

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

E
ester.zhou 已提交
343
![en-us_image_0000001239032423](figures/en-us_image_0000001239032423.png)
Z
zengyawen 已提交
344 345 346


### textBaseline
Z
zengyawen 已提交
347

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

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

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

E
ester.zhou 已提交
391
![en-us_image_0000001193872518](figures/en-us_image_0000001193872518.png)
Z
zengyawen 已提交
392 393 394


### globalAlpha
Z
zengyawen 已提交
395

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

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


### lineDashOffset
Z
zengyawen 已提交
431

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

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


### globalCompositeOperation

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

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

E
ester.zhou 已提交
517
![en-us_image_0000001194192456](figures/en-us_image_0000001194192456.png)
Z
zengyawen 已提交
518 519 520


### shadowBlur
Z
zengyawen 已提交
521

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

E
ester.zhou 已提交
552
![en-us_image_0000001194352452](figures/en-us_image_0000001194352452.png)
Z
zengyawen 已提交
553 554 555


### shadowColor
Z
zengyawen 已提交
556

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

E
ester.zhou 已提交
587
![en-us_image_0000001238952399](figures/en-us_image_0000001238952399.png)
Z
zengyawen 已提交
588 589 590


### shadowOffsetX
Z
zengyawen 已提交
591

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

E
ester.zhou 已提交
623
![en-us_image_0000001194032478](figures/en-us_image_0000001194032478.png)
Z
zengyawen 已提交
624 625 626


### shadowOffsetY
Z
zengyawen 已提交
627

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

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

E
ester.zhou 已提交
659
![en-us_image_0000001239032425](figures/en-us_image_0000001239032425.png)
Z
zengyawen 已提交
660 661 662


### imageSmoothingEnabled
Z
zengyawen 已提交
663

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

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

Z
zengyawen 已提交
695

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


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

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

Fills a rectangle on the canvas.

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

E
ester.zhou 已提交
707
 **Parameters**
Z
zengyawen 已提交
708

E
ester.zhou 已提交
709
| Name | Type    | Mandatory  | Default Value | Description           |
E
ester.zhou 已提交
710 711 712 713 714 715 716
| ------ | ------ | ---- | ---- | ------------- |
| 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 已提交
717

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

E
ester.zhou 已提交
745
  ![en-us_image_0000001194192436](figures/en-us_image_0000001194192436.png)
Z
zengyawen 已提交
746 747 748 749 750 751 752 753


### strokeRect

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

Draws an outlined rectangle on the canvas.

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

E
ester.zhou 已提交
756
 **Parameters**
E
ester.zhou 已提交
757

E
ester.zhou 已提交
758
| Name | Type    | Mandatory  | Default Value | Description          |
E
ester.zhou 已提交
759 760 761 762 763
| ------ | ------ | ---- | ---- | ------------ |
| 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 已提交
764

E
ester.zhou 已提交
765
 **Example**
H
HelloCrease 已提交
766

767 768
  ```ts
  // xxx.ets
Z
zengyawen 已提交
769 770 771 772 773 774
  @Entry
  @Component
  struct StrokeRect {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
775

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

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


Z
zengyawen 已提交
797
### clearRect
Z
zengyawen 已提交
798

Z
zengyawen 已提交
799
clearRect(x: number, y: number, w: number, h: number): void
Z
zengyawen 已提交
800 801 802

Clears the content in a rectangle on the canvas.

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

E
ester.zhou 已提交
805
 **Parameters**
E
ester.zhou 已提交
806

E
ester.zhou 已提交
807
| Name | Type    | Mandatory  | Default Value | Description           |
E
ester.zhou 已提交
808 809 810 811 812
| ------ | ------ | ---- | ---- | ------------- |
| 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 已提交
813

E
ester.zhou 已提交
814
 **Example**
H
HelloCrease 已提交
815

816
  ```ts
817
  // xxx.ets
Z
zengyawen 已提交
818 819 820 821 822 823
  @Entry
  @Component
  struct ClearRect {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
824

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

E
ester.zhou 已提交
845
  ![en-us_image_0000001238952377](figures/en-us_image_0000001238952377.png)
Z
zengyawen 已提交
846 847


Z
zengyawen 已提交
848
### fillText
Z
zengyawen 已提交
849

E
ester.zhou 已提交
850
fillText(text: string, x: number, y: number, maxWidth?: number): void
Z
zengyawen 已提交
851 852 853

Draws filled text on the canvas.

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

E
ester.zhou 已提交
856
**Parameters**
Z
zengyawen 已提交
857

E
ester.zhou 已提交
858
| Name  | Type    | Mandatory  | Default Value | Description             |
E
esterzhou 已提交
859 860 861 862 863
| -------- | ------ | ---- | ---- | --------------- |
| 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 已提交
864

E
ester.zhou 已提交
865
 **Example**
H
HelloCrease 已提交
866

867
  ```ts
868
  // xxx.ets
Z
zengyawen 已提交
869 870 871 872 873 874
  @Entry
  @Component
  struct FillText {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
875

Z
zengyawen 已提交
876 877 878 879 880 881 882 883 884 885 886 887
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.font = '30px sans-serif'
            this.offContext.fillText("Hello World!", 20, 100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
888
      }
Z
zengyawen 已提交
889 890
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
891
    }
Z
zengyawen 已提交
892 893
  }
  ```
Z
zengyawen 已提交
894

E
ester.zhou 已提交
895
  ![en-us_image_0000001194032458](figures/en-us_image_0000001194032458.png)
Z
zengyawen 已提交
896 897


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

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

Draws a text stroke on the canvas.

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

E
ester.zhou 已提交
906 907
**Parameters**

E
ester.zhou 已提交
908
| Name  | Type    | Mandatory  | Default Value | Description             |
E
esterzhou 已提交
909 910 911 912 913
| -------- | ------ | ---- | ---- | --------------- |
| 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 已提交
914

E
ester.zhou 已提交
915
 **Example**
H
HelloCrease 已提交
916

917 918
  ```ts
  // xxx.ets
Z
zengyawen 已提交
919 920 921 922 923 924
  @Entry
  @Component
  struct StrokeText {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
925

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

E
ester.zhou 已提交
945
  ![en-us_image_0000001238952401](figures/en-us_image_0000001238952401.png)
Z
zengyawen 已提交
946 947 948 949 950 951 952 953


### measureText

measureText(text: string): TextMetrics

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

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

E
ester.zhou 已提交
956
 **Parameters**
E
ester.zhou 已提交
957

E
ester.zhou 已提交
958
| Name | Type    | Mandatory  | Default Value | Description        |
E
ester.zhou 已提交
959 960 961
| ---- | ------ | ---- | ---- | ---------- |
| text | string | Yes   | ""   | Text to be measured.|

E
ester.zhou 已提交
962
 **Return value**
Z
zengyawen 已提交
963

E
ester.zhou 已提交
964 965 966
| Type       | Description                                                        |
| ----------- | ------------------------------------------------------------ |
| TextMetrics | **TextMetrics** object.<br>Since API version 9, this API is supported in ArkTS widgets.|
Z
zengyawen 已提交
967

E
esterzhou 已提交
968
**TextMetrics** attributes
Z
zengyawen 已提交
969

E
esterzhou 已提交
970 971 972 973
| Name                      | Type    | Description                                      |
| ------------------------ | ------ | ---------------------------------------- |
| width                    | number | Width of the text.                                 |
| height                   | number | Height of the text.                                 |
E
ester.zhou 已提交
974 975 976 977 978 979 980 981 982 983 984 985
| 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 已提交
986
 **Example**
H
HelloCrease 已提交
987

988
  ```ts
989
  // xxx.ets
Z
zengyawen 已提交
990 991 992 993 994 995
  @Entry
  @Component
  struct MeasureText {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
996

Z
zengyawen 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.font = '50px sans-serif'
            this.offContext.fillText("Hello World!", 20, 100)
            this.offContext.fillText("width:" + this.context.measureText("Hello World!").width, 20, 200)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
1010
      }
Z
zengyawen 已提交
1011 1012
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1013
    }
Z
zengyawen 已提交
1014 1015
  }
  ```
Z
zengyawen 已提交
1016

E
ester.zhou 已提交
1017
  ![en-us_image_0000001194032480](figures/en-us_image_0000001194032480.png)
Z
zengyawen 已提交
1018 1019


Z
zengyawen 已提交
1020
### stroke
Z
zengyawen 已提交
1021

Z
zengyawen 已提交
1022
stroke(path?: Path2D): void
Z
zengyawen 已提交
1023 1024 1025

Strokes a path.

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

E
ester.zhou 已提交
1028
 **Parameters**
E
ester.zhou 已提交
1029

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

E
ester.zhou 已提交
1034
 **Example**
E
ester.zhou 已提交
1035

1036 1037
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1038 1039 1040 1041 1042 1043
  @Entry
  @Component
  struct Stroke {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1044

Z
zengyawen 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.moveTo(25, 25)
            this.offContext.lineTo(25, 105)
E
esterzhou 已提交
1054 1055
            this.offContext.lineTo(75, 105)
            this.offContext.lineTo(75, 25)
Z
zengyawen 已提交
1056 1057 1058 1059 1060
            this.offContext.strokeStyle = 'rgb(0,0,255)'
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1061
      }
Z
zengyawen 已提交
1062 1063
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1064
    }
Z
zengyawen 已提交
1065 1066
  }
  ```
Z
zengyawen 已提交
1067

E
ester.zhou 已提交
1068
  ![en-us_image_0000001238832389](figures/en-us_image_0000001238832389.png)
Z
zengyawen 已提交
1069 1070


Z
zengyawen 已提交
1071
### beginPath
Z
zengyawen 已提交
1072

Z
zengyawen 已提交
1073
beginPath(): void
Z
zengyawen 已提交
1074 1075 1076

Creates a drawing path.

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

E
ester.zhou 已提交
1079
 **Example**
H
HelloCrease 已提交
1080

1081
  ```ts
1082
  // xxx.ets
Z
zengyawen 已提交
1083 1084 1085 1086 1087 1088
  @Entry
  @Component
  struct BeginPath {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1089

Z
zengyawen 已提交
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.lineWidth = 6
            this.offContext.strokeStyle = '#0000ff'
            this.offContext.moveTo(15, 80)
            this.offContext.lineTo(280, 160)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1106
      }
Z
zengyawen 已提交
1107 1108
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1109
    }
Z
zengyawen 已提交
1110 1111
  }
  ```
Z
zengyawen 已提交
1112

E
ester.zhou 已提交
1113
  ![en-us_image_0000001193872522](figures/en-us_image_0000001193872522.png)
Z
zengyawen 已提交
1114 1115


Z
zengyawen 已提交
1116
### moveTo
Z
zengyawen 已提交
1117

Z
zengyawen 已提交
1118
moveTo(x: number, y: number): void
Z
zengyawen 已提交
1119 1120 1121

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

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

E
ester.zhou 已提交
1124
 **Parameters**
Z
zengyawen 已提交
1125

E
ester.zhou 已提交
1126
| Name | Type    | Mandatory  | Default Value | Description       |
E
ester.zhou 已提交
1127 1128 1129 1130
| ---- | ------ | ---- | ---- | --------- |
| x    | number | Yes   | 0    | X-coordinate of the target position.|
| y    | number | Yes   | 0    | Y-coordinate of the target position.|

E
ester.zhou 已提交
1131
 **Example**
H
HelloCrease 已提交
1132

1133
  ```ts
1134
  // xxx.ets
Z
zengyawen 已提交
1135 1136 1137 1138 1139 1140
  @Entry
  @Component
  struct MoveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1141

Z
zengyawen 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.moveTo(10, 10)
            this.offContext.lineTo(280, 160)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1156
      }
Z
zengyawen 已提交
1157 1158
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1159
    }
Z
zengyawen 已提交
1160 1161
  }
  ```
Z
zengyawen 已提交
1162

E
ester.zhou 已提交
1163
  ![en-us_image_0000001238832409](figures/en-us_image_0000001238832409.png)
Z
zengyawen 已提交
1164 1165


Z
zengyawen 已提交
1166
### lineTo
Z
zengyawen 已提交
1167

Z
zengyawen 已提交
1168
lineTo(x: number, y: number): void
Z
zengyawen 已提交
1169 1170 1171

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

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

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

E
ester.zhou 已提交
1176
| Name | Type    | Mandatory  | Default Value | Description       |
E
ester.zhou 已提交
1177 1178 1179
| ---- | ------ | ---- | ---- | --------- |
| x    | number | Yes   | 0    | X-coordinate of the target position.|
| y    | number | Yes   | 0    | Y-coordinate of the target position.|
Z
zengyawen 已提交
1180

E
ester.zhou 已提交
1181
 **Example**
H
HelloCrease 已提交
1182

1183
  ```ts
1184
  // xxx.ets
Z
zengyawen 已提交
1185 1186 1187 1188 1189 1190
  @Entry
  @Component
  struct LineTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1191

Z
zengyawen 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.moveTo(10, 10)
            this.offContext.lineTo(280, 160)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1206
      }
Z
zengyawen 已提交
1207 1208
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1209
    }
Z
zengyawen 已提交
1210 1211
  }
  ```
Z
zengyawen 已提交
1212

E
ester.zhou 已提交
1213
  ![en-us_image_0000001238712443](figures/en-us_image_0000001238712443.png)
Z
zengyawen 已提交
1214 1215


Z
zengyawen 已提交
1216
### closePath
Z
zengyawen 已提交
1217

Z
zengyawen 已提交
1218
closePath(): void
Z
zengyawen 已提交
1219 1220 1221

Draws a closed path.

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

E
ester.zhou 已提交
1224
 **Example**
H
HelloCrease 已提交
1225

1226
  ```ts
1227
  // xxx.ets
Z
zengyawen 已提交
1228 1229 1230 1231 1232 1233
  @Entry
  @Component
  struct ClosePath {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1234

Z
zengyawen 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
              this.offContext.beginPath()
              this.offContext.moveTo(30, 30)
              this.offContext.lineTo(110, 30)
              this.offContext.lineTo(70, 90)
              this.offContext.closePath()
              this.offContext.stroke()
              var image = this.offContext.transferToImageBitmap()
              this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1251
      }
Z
zengyawen 已提交
1252 1253
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1254
    }
Z
zengyawen 已提交
1255 1256
  }
  ```
Z
zengyawen 已提交
1257

E
ester.zhou 已提交
1258
  ![en-us_image_0000001194192460](figures/en-us_image_0000001194192460.png)
Z
zengyawen 已提交
1259 1260


Z
zengyawen 已提交
1261
### createPattern
Z
zengyawen 已提交
1262

E
ester.zhou 已提交
1263
createPattern(image: ImageBitmap, repetition: string | null): CanvasPattern | null
Z
zengyawen 已提交
1264 1265 1266

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

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

E
ester.zhou 已提交
1269
**Parameters**
Z
zengyawen 已提交
1270

E
ester.zhou 已提交
1271
| Name    | Type                                      | Mandatory  | Default Value | Description                                      |
E
ester.zhou 已提交
1272 1273
| ---------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- |
| image      | [ImageBitmap](ts-components-canvas-imagebitmap.md) | Yes   | null | Source image. For details, see **ImageBitmap**.                 |
E
ester.zhou 已提交
1274
| repetition | string                                   | Yes   | ""  | Repetition mode. The value can be **'repeat'**, **'repeat-x'**, **'repeat-y'**, **'no-repeat'**, **'clamp'**, or **'mirror'**.|
E
ester.zhou 已提交
1275 1276 1277

**Return value**

E
esterzhou 已提交
1278 1279 1280
| Type                             | Description                     |
| ------------------------------- | ----------------------- |
| [CanvasPattern](#canvaspattern) | Created pattern for image filling based on a specified source image and repetition mode.|
E
ester.zhou 已提交
1281

E
ester.zhou 已提交
1282
 **Example**
H
HelloCrease 已提交
1283

1284
  ```ts
1285
  // xxx.ets
Z
zengyawen 已提交
1286 1287 1288 1289 1290 1291 1292
  @Entry
  @Component
  struct CreatePattern {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private img:ImageBitmap = new ImageBitmap("common/images/icon.jpg")
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1293

Z
zengyawen 已提交
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var pattern = this.offContext.createPattern(this.img, 'repeat')
            this.offContext.fillStyle = pattern
            this.offContext.fillRect(0, 0, 200, 200)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1307
      }
Z
zengyawen 已提交
1308 1309
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1310
    }
Z
zengyawen 已提交
1311 1312
  }
  ```
Z
zengyawen 已提交
1313

E
ester.zhou 已提交
1314
  ![en-us_image_0000001194352456](figures/en-us_image_0000001194352456.png)
Z
zengyawen 已提交
1315 1316


Z
zengyawen 已提交
1317
### bezierCurveTo
Z
zengyawen 已提交
1318

Z
zengyawen 已提交
1319
bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void
Z
zengyawen 已提交
1320 1321 1322

Draws a cubic bezier curve on the canvas.

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

E
ester.zhou 已提交
1325
 **Parameters**
E
ester.zhou 已提交
1326

E
ester.zhou 已提交
1327
| Name | Type    | Mandatory  | Default Value | Description            |
E
ester.zhou 已提交
1328 1329 1330 1331 1332 1333 1334
| ---- | ------ | ---- | ---- | -------------- |
| 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 已提交
1335

E
ester.zhou 已提交
1336
 **Example**
H
HelloCrease 已提交
1337

1338
  ```ts
1339
  // xxx.ets
Z
zengyawen 已提交
1340 1341 1342 1343 1344 1345
  @Entry
  @Component
  struct BezierCurveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1346

Z
zengyawen 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.moveTo(10, 10)
            this.offContext.bezierCurveTo(20, 100, 200, 100, 200, 20)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1361
      }
Z
zengyawen 已提交
1362 1363
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1364
    }
Z
zengyawen 已提交
1365 1366
  }
  ```
Z
zengyawen 已提交
1367

E
ester.zhou 已提交
1368
  ![en-us_image_0000001238952403](figures/en-us_image_0000001238952403.png)
Z
zengyawen 已提交
1369 1370


Z
zengyawen 已提交
1371
### quadraticCurveTo
Z
zengyawen 已提交
1372

Z
zengyawen 已提交
1373
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void
Z
zengyawen 已提交
1374 1375 1376

Draws a quadratic curve on the canvas.

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

E
ester.zhou 已提交
1379
 **Parameters**
Z
zengyawen 已提交
1380

E
ester.zhou 已提交
1381
| Name | Type    | Mandatory  | Default Value | Description         |
E
ester.zhou 已提交
1382 1383 1384 1385 1386 1387
| ---- | ------ | ---- | ---- | ----------- |
| 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 已提交
1388
 **Example**
H
HelloCrease 已提交
1389

1390
  ```ts
1391
  // xxx.ets
Z
zengyawen 已提交
1392 1393 1394 1395 1396 1397
  @Entry
  @Component
  struct QuadraticCurveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1398

Z
zengyawen 已提交
1399 1400 1401 1402 1403 1404 1405
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1406 1407 1408 1409
            this.offContext.beginPath()
            this.offContext.moveTo(20, 20)
            this.offContext.quadraticCurveTo(100, 100, 200, 20)
            this.offContext.stroke()
Z
zengyawen 已提交
1410 1411 1412
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
1413
      }
Z
zengyawen 已提交
1414 1415
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1416
    }
Z
zengyawen 已提交
1417 1418
  }
  ```
Z
zengyawen 已提交
1419

E
ester.zhou 已提交
1420
  ![en-us_image_0000001194032482](figures/en-us_image_0000001194032482.png)
Z
zengyawen 已提交
1421 1422


Z
zengyawen 已提交
1423
### arc
Z
zengyawen 已提交
1424

E
ester.zhou 已提交
1425
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void
Z
zengyawen 已提交
1426 1427 1428

Draws an arc on the canvas.

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

E
ester.zhou 已提交
1431
 **Parameters**
E
ester.zhou 已提交
1432

E
ester.zhou 已提交
1433
| Name          | Type     | Mandatory  | Default Value  | Description        |
E
esterzhou 已提交
1434 1435 1436 1437 1438 1439
| ---------------- | ------- | ---- | ----- | ---------- |
| 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 已提交
1440
| counterclockwise | boolean | No   | false | Whether to draw the arc counterclockwise.|
Z
zengyawen 已提交
1441

E
ester.zhou 已提交
1442
 **Example**
H
HelloCrease 已提交
1443

1444
  ```ts
1445
  // xxx.ets
Z
zengyawen 已提交
1446 1447 1448 1449 1450 1451
  @Entry
  @Component
  struct Arc {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1452

Z
zengyawen 已提交
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.arc(100, 75, 50, 0, 6.28)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1466
      }
Z
zengyawen 已提交
1467 1468
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1469
    }
Z
zengyawen 已提交
1470 1471
  }
  ```
Z
zengyawen 已提交
1472

E
ester.zhou 已提交
1473
  ![en-us_image_0000001239032429](figures/en-us_image_0000001239032429.png)
Z
zengyawen 已提交
1474 1475


Z
zengyawen 已提交
1476
### arcTo
Z
zengyawen 已提交
1477

Z
zengyawen 已提交
1478
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void
Z
zengyawen 已提交
1479 1480 1481

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

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

E
ester.zhou 已提交
1484
 **Parameters**
E
ester.zhou 已提交
1485

E
ester.zhou 已提交
1486
| Name | Type    | Mandatory  | Default Value | Description             |
E
ester.zhou 已提交
1487 1488 1489 1490 1491 1492
| ------ | ------ | ---- | ---- | --------------- |
| 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 已提交
1493

E
ester.zhou 已提交
1494
 **Example**
H
HelloCrease 已提交
1495

1496
  ```ts
1497
  // xxx.ets
Z
zengyawen 已提交
1498 1499 1500 1501 1502 1503
  @Entry
  @Component
  struct ArcTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1504

Z
zengyawen 已提交
1505 1506 1507 1508 1509 1510 1511
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1512 1513 1514
            this.offContext.moveTo(100, 20)
            this.offContext.arcTo(150, 20, 150, 70, 50)
            this.offContext.stroke()
Z
zengyawen 已提交
1515 1516 1517
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1518
      }
Z
zengyawen 已提交
1519 1520
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1521
    }
Z
zengyawen 已提交
1522 1523
  }
  ```
Z
zengyawen 已提交
1524

E
ester.zhou 已提交
1525
  ![en-us_image_0000001193872524](figures/en-us_image_0000001193872524.png)
Z
zengyawen 已提交
1526 1527


Z
zengyawen 已提交
1528
### ellipse
Z
zengyawen 已提交
1529

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

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

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

E
ester.zhou 已提交
1536
 **Parameters**
Z
zengyawen 已提交
1537

E
ester.zhou 已提交
1538
| Name          | Type     | Mandatory  | Default Value  | Description               |
E
esterzhou 已提交
1539 1540 1541 1542 1543 1544 1545 1546
| ---------------- | ------- | ---- | ----- | ----------------- |
| x                | number  | Yes   | 0     | X-coordinate of the ellipse center.       |
| y                | number  | Yes   | 0     | Y-coordinate of the ellipse center.       |
| radiusX          | number  | Yes   | 0     | Ellipse radius on the x-axis.       |
| radiusY          | number  | Yes   | 0     | Ellipse radius on the y-axis.       |
| rotation         | number  | Yes   | 0     | Rotation angle of the ellipse. The unit is radian.   |
| startAngle       | number  | Yes   | 0     | Angle of the start point for drawing the ellipse. The unit is radian.|
| endAngle         | number  | Yes   | 0     | Angle of the end point for drawing the ellipse. The unit is radian.|
E
ester.zhou 已提交
1547
| counterclockwise | boolean | No   | false | Whether to draw the ellipse counterclockwise.<br>**true**: Draw the ellipse counterclockwise.<br>**false**: Draw the ellipse clockwise.    |
Z
zengyawen 已提交
1548

E
ester.zhou 已提交
1549
 **Example**
H
HelloCrease 已提交
1550

1551
  ```ts
1552
  // xxx.ets
Z
zengyawen 已提交
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
E
esterzhou 已提交
1567
            this.offContext.ellipse(200, 200, 50, 100, Math.PI * 0.25, Math.PI * 0.5, Math.PI * 2)
Z
zengyawen 已提交
1568 1569 1570 1571
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1572
      }
Z
zengyawen 已提交
1573 1574
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1575
    }
Z
zengyawen 已提交
1576 1577
  }
  ```
Z
zengyawen 已提交
1578

E
ester.zhou 已提交
1579
  ![en-us_image_0000001194192440](figures/en-us_image_0000001194192440.png)
Z
zengyawen 已提交
1580 1581


Z
zengyawen 已提交
1582
### rect
Z
zengyawen 已提交
1583

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

E
ester.zhou 已提交
1586
Creates a rectangle on the canvas.
Z
zengyawen 已提交
1587

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

E
ester.zhou 已提交
1590
 **Parameters**
Z
zengyawen 已提交
1591

E
ester.zhou 已提交
1592
| Name | Type    | Mandatory  | Default Value | Description           |
E
esterzhou 已提交
1593 1594 1595 1596 1597
| ---- | ------ | ---- | ---- | ------------- |
| 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 已提交
1598

E
ester.zhou 已提交
1599
 **Example**
H
HelloCrease 已提交
1600

1601
  ```ts
1602
  // xxx.ets
Z
zengyawen 已提交
1603 1604 1605 1606 1607 1608
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1609

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

E
ester.zhou 已提交
1629
  ![en-us_image_0000001238712445](figures/en-us_image_0000001238712445.png)
Z
zengyawen 已提交
1630 1631


Z
zengyawen 已提交
1632
### fill
Z
zengyawen 已提交
1633

E
esterzhou 已提交
1634
fill(fillRule?: CanvasFillRule): void
Z
zengyawen 已提交
1635

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

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

E
esterzhou 已提交
1640 1641
**Parameters**

E
ester.zhou 已提交
1642
| Name  | Type            | Mandatory  | Default Value      | Description                                      |
E
esterzhou 已提交
1643 1644
| -------- | -------------- | ---- | --------- | ---------------------------------------- |
| 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 已提交
1645

1646
  ```ts
1647
  // xxx.ets
Z
zengyawen 已提交
1648 1649 1650 1651 1652 1653
  @Entry
  @Component
  struct Fill {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1654

Z
zengyawen 已提交
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.rect(20, 20, 100, 100) // Create a 100*100 rectangle at (20, 20)
            this.offContext.fill()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1667
      }
Z
zengyawen 已提交
1668 1669
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1670
    }
Z
zengyawen 已提交
1671 1672
  }
  ```
Z
zengyawen 已提交
1673

E
ester.zhou 已提交
1674
  ![en-us_image_0000001194192462](figures/en-us_image_0000001194192462.png)
Z
zengyawen 已提交
1675 1676


E
esterzhou 已提交
1677 1678 1679 1680
fill(path: Path2D, fillRule?: CanvasFillRule): void

Fills the area inside a closed path on the canvas.

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

E
esterzhou 已提交
1683 1684
**Parameters**

E
ester.zhou 已提交
1685
| Name  | Type            | Mandatory  | Default Value      | Description                                      |
E
esterzhou 已提交
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
| -------- | -------------- | ---- | --------- | ---------------------------------------- |
| path     | Path2D         | Yes   |           | A **Path2D** path to fill.                             |
| fillRule | CanvasFillRule | No   | "nonzero" | Rule by which to determine whether a point is inside or outside the area to fill.<br>The options are **"nonzero"** and **"evenodd"**.|


**Example**  

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

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
1709 1710 1711 1712 1713 1714 1715 1716
          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 已提交
1717
          // Fill path
E
ester.zhou 已提交
1718
          this.offContext.fillStyle = '#00ff00'
E
ester.zhou 已提交
1719
          this.offContext.fill(region, "evenodd")
E
esterzhou 已提交
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

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



Z
zengyawen 已提交
1734
### clip
Z
zengyawen 已提交
1735

E
esterzhou 已提交
1736
clip(fillRule?: CanvasFillRule): void
Z
zengyawen 已提交
1737 1738 1739

Sets the current path to a clipping path.

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

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

E
ester.zhou 已提交
1744
| Name  | Type            | Mandatory  | Default Value      | Description                                      |
E
esterzhou 已提交
1745 1746 1747
| -------- | -------------- | ---- | --------- | ---------------------------------------- |
| 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 已提交
1748
 **Example**
H
HelloCrease 已提交
1749

1750
  ```ts
1751
  // xxx.ets
Z
zengyawen 已提交
1752 1753 1754 1755 1756 1757
  @Entry
  @Component
  struct Clip {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1758

Z
zengyawen 已提交
1759 1760 1761 1762 1763 1764 1765
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
esterzhou 已提交
1766
            this.offContext.rect(0, 0, 100, 200)
Z
zengyawen 已提交
1767 1768 1769
            this.offContext.stroke()
            this.offContext.clip()
            this.offContext.fillStyle = "rgb(255,0,0)"
E
esterzhou 已提交
1770
            this.offContext.fillRect(0, 0, 200, 200)
Z
zengyawen 已提交
1771 1772 1773
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1774
      }
Z
zengyawen 已提交
1775 1776
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1777
    }
Z
zengyawen 已提交
1778 1779
  }
  ```
Z
zengyawen 已提交
1780

E
ester.zhou 已提交
1781
  ![en-us_image_0000001194032462](figures/en-us_image_0000001194032462.png)
Z
zengyawen 已提交
1782 1783


E
esterzhou 已提交
1784 1785 1786 1787
clip(path:Path2D, fillRule?: CanvasFillRule): void

Sets a closed path to a clipping path.

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

E
esterzhou 已提交
1790 1791
**Parameters**

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

E
ester.zhou 已提交
1797
 **Example**
E
esterzhou 已提交
1798 1799 1800

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1801 1802 1803 1804 1805 1806
  @Entry
  @Component
  struct Clip {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1807

E
ester.zhou 已提交
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            let region = new Path2D()
            region.moveTo(30, 90)
            region.lineTo(110, 20)
            region.lineTo(240, 130)
            region.lineTo(60, 130)
            region.lineTo(190, 20)
            region.lineTo(270, 90)
            region.closePath()
            this.offContext.clip(region,"evenodd")
            this.offContext.fillStyle = "rgb(0,255,0)"
            this.offContext.fillRect(0, 0, 600, 600)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
E
esterzhou 已提交
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
    }
  }
  ```

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



### filter

filter(filter: string): void

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

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

E
esterzhou 已提交
1848 1849
**Parameters**

E
ester.zhou 已提交
1850
| Name | Type    | Mandatory  | Default Value | Description          |
E
esterzhou 已提交
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
| ------ | ------ | ---- | ---- | ------------ |
| filter | string | Yes   | -    | Functions that accept various filter effects.|


### getTransform

getTransform(): Matrix2D

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

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

E
esterzhou 已提交
1863 1864 1865 1866 1867 1868 1869

### resetTransform

resetTransform(): void

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

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

E
esterzhou 已提交
1872 1873 1874 1875 1876 1877 1878

### direction

direction(direction: CanvasDirection): void

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

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

E
esterzhou 已提交
1881

Z
zengyawen 已提交
1882
### rotate
Z
zengyawen 已提交
1883

E
ester.zhou 已提交
1884
rotate(angle: number): void
Z
zengyawen 已提交
1885 1886 1887

Rotates a canvas clockwise around its coordinate axes.

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

E
ester.zhou 已提交
1890
 **Parameters**
E
ester.zhou 已提交
1891

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

E
ester.zhou 已提交
1896
 **Example**
H
HelloCrease 已提交
1897

1898
  ```ts
1899
  // xxx.ets
Z
zengyawen 已提交
1900 1901 1902 1903 1904 1905
  @Entry
  @Component
  struct Rotate {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
1906

Z
zengyawen 已提交
1907 1908 1909 1910 1911 1912 1913
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1914
            this.offContext.rotate(45 * Math.PI / 180)
Z
zengyawen 已提交
1915 1916 1917 1918
            this.offContext.fillRect(70, 20, 50, 50)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1919
      }
Z
zengyawen 已提交
1920 1921
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1922
    }
Z
zengyawen 已提交
1923 1924 1925
  }
  ```

E
ester.zhou 已提交
1926
  ![en-us_image_0000001238952405](figures/en-us_image_0000001238952405.png)
Z
zengyawen 已提交
1927 1928 1929 1930 1931 1932


### scale

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

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

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

E
ester.zhou 已提交
1937
 **Parameters**
Z
zengyawen 已提交
1938

E
ester.zhou 已提交
1939
| Name | Type    | Mandatory  | Default Value | Description         |
E
ester.zhou 已提交
1940 1941 1942 1943
| ---- | ------ | ---- | ---- | ----------- |
| x    | number | Yes   | 0    | Horizontal scale factor.|
| y    | number | Yes   | 0    | Vertical scale factor.|

E
ester.zhou 已提交
1944
 **Example**
H
HelloCrease 已提交
1945

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

Z
zengyawen 已提交
1955 1956 1957 1958 1959 1960 1961
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
esterzhou 已提交
1962 1963
            this.offContext.lineWidth = 3
            this.offContext.strokeRect(30, 30, 50, 50)
Z
zengyawen 已提交
1964
            this.offContext.scale(2, 2) // Scale to 200%
E
esterzhou 已提交
1965
            this.offContext.strokeRect(30, 30, 50, 50)
Z
zengyawen 已提交
1966 1967 1968
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1969
      }
Z
zengyawen 已提交
1970 1971
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1972
    }
Z
zengyawen 已提交
1973 1974
  }
  ```
Z
zengyawen 已提交
1975

E
ester.zhou 已提交
1976
  ![en-us_image_0000001193872498](figures/en-us_image_0000001193872498.png)
Z
zengyawen 已提交
1977 1978


Z
zengyawen 已提交
1979
### transform
Z
zengyawen 已提交
1980

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

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 已提交
1985 1986
Since API version 9, this API is supported in ArkTS widgets.

1987
> **NOTE**
E
ester.zhou 已提交
1988
>
Z
zengyawen 已提交
1989
> 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 已提交
1990
>
Z
zengyawen 已提交
1991
> - x' = scaleX \* x + skewY \* y + translateX
H
HelloCrease 已提交
1992
>
Z
zengyawen 已提交
1993 1994
> - y' = skewX \* x + scaleY \* y + translateY

E
ester.zhou 已提交
1995
**Parameters**
Z
zengyawen 已提交
1996

E
ester.zhou 已提交
1997
| Name | Type    | Mandatory  | Default Value | Description                  |
E
esterzhou 已提交
1998 1999 2000 2001 2002 2003 2004
| ---- | ------ | ---- | ---- | -------------------- |
| 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 已提交
2005

E
ester.zhou 已提交
2006
 **Example**
H
HelloCrease 已提交
2007

2008
  ```ts
2009
  // xxx.ets
Z
zengyawen 已提交
2010 2011 2012 2013 2014 2015
  @Entry
  @Component
  struct Transform {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
2016

Z
zengyawen 已提交
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.fillStyle = 'rgb(0,0,0)'
            this.offContext.fillRect(0, 0, 100, 100)
            this.offContext.transform(1, 0.5, -0.5, 1, 10, 10)
            this.offContext.fillStyle = 'rgb(255,0,0)'
            this.offContext.fillRect(0, 0, 100, 100)
            this.offContext.transform(1, 0.5, -0.5, 1, 10, 10)
            this.offContext.fillStyle = 'rgb(0,0,255)'
            this.offContext.fillRect(0, 0, 100, 100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2035
      }
Z
zengyawen 已提交
2036 2037
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2038
    }
Z
zengyawen 已提交
2039 2040 2041
  }
  ```

E
ester.zhou 已提交
2042
  ![en-us_image_0000001239032431](figures/en-us_image_0000001239032431.png)
Z
zengyawen 已提交
2043 2044 2045 2046


### setTransform

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

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

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

E
ester.zhou 已提交
2053 2054
**Parameters**

E
ester.zhou 已提交
2055
| Name | Type    | Mandatory  | Default Value | Description                  |
E
esterzhou 已提交
2056 2057 2058 2059 2060 2061 2062
| ---- | ------ | ---- | ---- | -------------------- |
| 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 已提交
2063

E
ester.zhou 已提交
2064
 **Example**
H
HelloCrease 已提交
2065

2066
  ```ts
2067
  // xxx.ets
Z
zengyawen 已提交
2068 2069 2070 2071 2072 2073
  @Entry
  @Component
  struct SetTransform {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
2074

Z
zengyawen 已提交
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.fillStyle = 'rgb(255,0,0)'
            this.offContext.fillRect(0, 0, 100, 100)
            this.offContext.setTransform(1,0.5, -0.5, 1, 10, 10)
            this.offContext.fillStyle = 'rgb(0,0,255)'
            this.offContext.fillRect(0, 0, 100, 100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2090
      }
Z
zengyawen 已提交
2091 2092
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2093
    }
Z
zengyawen 已提交
2094 2095
  }
  ```
Z
zengyawen 已提交
2096

E
esterzhou 已提交
2097
  ![en-us_image_0000001193872526](figures/en-us_image_0000001193872526.png)
Z
zengyawen 已提交
2098 2099


Z
zengyawen 已提交
2100
### translate
Z
zengyawen 已提交
2101

Z
zengyawen 已提交
2102
translate(x: number, y: number): void
Z
zengyawen 已提交
2103 2104 2105

Moves the origin of the coordinate system.

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

E
ester.zhou 已提交
2108
 **Parameters**
E
ester.zhou 已提交
2109

E
ester.zhou 已提交
2110
| Name | Type    | Mandatory  | Default Value | Description      |
E
ester.zhou 已提交
2111 2112 2113
| ---- | ------ | ---- | ---- | -------- |
| x    | number | Yes   | 0    | X-axis translation.|
| y    | number | Yes   | 0    | Y-axis translation.|
Z
zengyawen 已提交
2114

E
ester.zhou 已提交
2115
 **Example**
H
HelloCrease 已提交
2116

2117
  ```ts
2118
  // xxx.ets
Z
zengyawen 已提交
2119 2120 2121 2122 2123 2124
  @Entry
  @Component
  struct Translate {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
2125

Z
zengyawen 已提交
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.fillRect(10, 10, 50, 50)
            this.offContext.translate(70, 70)
            this.offContext.fillRect(10, 10, 50, 50)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
    }
  }
  ```

E
esterzhou 已提交
2146
  ![en-us_image_0000001238832413](figures/en-us_image_0000001238832413.png)
Z
zengyawen 已提交
2147 2148 2149 2150


### drawImage

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

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

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

E
ester.zhou 已提交
2157
Draws an image on the canvas.
Z
zengyawen 已提交
2158

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

E
ester.zhou 已提交
2161
 **Parameters**
Z
zengyawen 已提交
2162

E
ester.zhou 已提交
2163
| Name | Type                                      | Mandatory  | Default Value | Description                           |
E
esterzhou 已提交
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
| ----- | ---------------------------------------- | ---- | ---- | ----------------------------- |
| 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 已提交
2174

E
ester.zhou 已提交
2175 2176

 **Example**
H
HelloCrease 已提交
2177

2178
  ```ts
2179
  // xxx.ets
Z
zengyawen 已提交
2180 2181
  @Entry
  @Component
E
esterzhou 已提交
2182
  struct DrawImage {
Z
zengyawen 已提交
2183 2184 2185 2186 2187 2188 2189
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private img:ImageBitmap = new ImageBitmap("common/images/icon.jpg")
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
Z
zengyawen 已提交
2190 2191
        .width('100%')
        .height('100%')
Z
zengyawen 已提交
2192 2193 2194 2195 2196 2197
        .backgroundColor('#ffff00')
        .onReady(() => {
            this.offContext.drawImage( this.img,0,0,400,200)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
2198
      }
Z
zengyawen 已提交
2199 2200
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2201
    }
Z
zengyawen 已提交
2202 2203
  }
  ```
Z
zengyawen 已提交
2204

E
ester.zhou 已提交
2205
  ![en-us_image_0000001238712447](figures/en-us_image_0000001238712447.png)
Z
zengyawen 已提交
2206 2207


Z
zengyawen 已提交
2208
### createImageData
Z
zengyawen 已提交
2209

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

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

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

E
ester.zhou 已提交
2216
 **Parameters**
Z
zengyawen 已提交
2217

E
ester.zhou 已提交
2218
| Name  | Type    | Mandatory  | Default Value  | Description           |
E
esterzhou 已提交
2219 2220 2221
| ---- | ------ | ---- | ---- | ------------- |
| sw   | number | Yes   | 0    | Width of the **ImageData** object.|
| sh   | number | Yes   | 0    | Height of the **ImageData** object.|
Z
zengyawen 已提交
2222 2223


E
ester.zhou 已提交
2224 2225
createImageData(imageData: ImageData): ImageData

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

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

E
ester.zhou 已提交
2230
 **Parameters**
Z
zengyawen 已提交
2231

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

E
ester.zhou 已提交
2236 2237
 **Return value**

E
esterzhou 已提交
2238 2239 2240
| Type                                      | Description           |
| ---------------------------------------- | ------------- |
| [ImageData](ts-components-canvas-imagedata.md) | New **ImageData** object.|
Z
zengyawen 已提交
2241

2242 2243 2244 2245 2246
### 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 已提交
2247

E
ester.zhou 已提交
2248
 **Parameters**
E
ester.zhou 已提交
2249

E
ester.zhou 已提交
2250
| Name | Type    | Mandatory  | Default Value | Description             |
E
esterzhou 已提交
2251 2252 2253 2254 2255
| ---- | ------ | ---- | ---- | --------------- |
| 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 已提交
2256 2257 2258

**Return value**

E
esterzhou 已提交
2259 2260 2261
| Type                                      | Description          |
| ---------------------------------------- | ------------ |
| [PixelMap](../apis/js-apis-image.md#pixelmap7) | **PixelMap** object.|
E
ester.zhou 已提交
2262

E
ester.zhou 已提交
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283
### setPixelMap

setPixelMap(value?: PixelMap): void

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

 **Parameters**

| Name | Type    | Mandatory  | Default Value | Description             |
| ---- | ------ | ---- | ---- | --------------- |
| sx   | number | Yes   | 0    | X-coordinate of the upper left corner of the output area.|
| sy   | number | Yes   | 0    | Y-coordinate of the upper left corner of the output area.|
| sw   | number | Yes   | 0    | Width of the output area.    |
| sh   | number | Yes   | 0    | Height of the output area.    |

**Return value**

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

Z
zengyawen 已提交
2284 2285 2286

### getImageData

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

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

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

E
ester.zhou 已提交
2293
 **Parameters**
E
ester.zhou 已提交
2294

E
ester.zhou 已提交
2295
| Name | Type    | Mandatory  | Default Value | Description             |
E
ester.zhou 已提交
2296 2297 2298 2299 2300 2301 2302 2303
| ---- | ------ | ---- | ---- | --------------- |
| 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 已提交
2304 2305 2306 2307
| Type                                      | Description           |
| ---------------------------------------- | ------------- |
| [ImageData](ts-components-canvas-imagedata.md) | New **ImageData** object.|

E
ester.zhou 已提交
2308

E
esterzhou 已提交
2309 2310 2311 2312
**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2313 2314 2315 2316 2317 2318 2319
  @Entry
  @Component
  struct GetImageData {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    private img:ImageBitmap = new ImageBitmap("/common/images/1234.png")
E
esterzhou 已提交
2320

E
ester.zhou 已提交
2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.drawImage(this.img,0,0,130,130)
            var imagedata = this.offContext.getImageData(50,50,130,130)
            this.offContext.putImageData(imagedata,150,150)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
E
esterzhou 已提交
2337 2338 2339 2340 2341
    }
  }
  ```

  ![en-us_image_000000127777780](figures/en-us_image_000000127777780.png)
Z
zengyawen 已提交
2342 2343 2344 2345


### putImageData

E
ester.zhou 已提交
2346 2347 2348
putImageData(imageData: Object, dx: number, dy: number): void

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

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

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

E
ester.zhou 已提交
2354
 **Parameters**
E
ester.zhou 已提交
2355

E
ester.zhou 已提交
2356
| Name     | Type    | Mandatory  | Default Value         | Description                           |
E
ester.zhou 已提交
2357 2358 2359 2360 2361 2362 2363 2364
| ----------- | ------ | ---- | ------------ | ----------------------------- |
| imagedata   | Object | Yes   | null         | **ImageData** object with pixels to put onto the canvas.           |
| dx          | number | Yes   | 0            | X-axis offset of the rectangular area on the canvas.               |
| dy          | number | Yes   | 0            | Y-axis offset of the rectangular area on the canvas.               |
| dirtyX      | number | No   | 0            | X-axis offset of the upper left corner of the rectangular area relative to that of the source image.|
| dirtyY      | number | No   | 0            | Y-axis offset of the upper left corner of the rectangular area relative to that of the source image.|
| dirtyWidth  | number | No   | Width of the **ImageData** object| Width of the rectangular area to crop the source image.              |
| dirtyHeight | number | No   | Height of the **ImageData** object| Height of the rectangular area to crop the source image.              |
Z
zengyawen 已提交
2365

E
ester.zhou 已提交
2366
 **Example**
H
HelloCrease 已提交
2367

2368
  ```ts
2369
  // xxx.ets
Z
zengyawen 已提交
2370 2371 2372 2373 2374 2375 2376 2377 2378
  @Entry
  @Component
  struct PutImageData {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
Z
zengyawen 已提交
2379 2380 2381
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
Z
zengyawen 已提交
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
          .onReady(() =>{
            var imageData = this.offContext.createImageData(100, 100)
            for (var i = 0; i < imageData.data.length; i += 4) {
              imageData.data[i + 0] = 255
              imageData.data[i + 1] = 0
              imageData.data[i + 2] = 255
              imageData.data[i + 3] = 255
            }
            this.offContext.putImageData(imageData, 10, 10)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
Z
zengyawen 已提交
2393 2394
          })
      }
Z
zengyawen 已提交
2395 2396
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2397
    }
Z
zengyawen 已提交
2398 2399
  }
  ```
Z
zengyawen 已提交
2400

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

E
ester.zhou 已提交
2403 2404 2405 2406 2407 2408
### setLineDash

setLineDash(segments: number[]): void

Sets the dash line style.

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

E
ester.zhou 已提交
2411 2412
**Parameters**

E
ester.zhou 已提交
2413
| Name  | Type      | Description                 |
E
esterzhou 已提交
2414
| -------- | -------- | ------------------- |
E
ester.zhou 已提交
2415 2416 2417 2418 2419
| segments | number[] | An array of numbers that specify distances to alternately draw a line and a gap.|

**Example**

  ```ts
E
ester.zhou 已提交
2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
  @Entry
  @Component
  struct SetLineDash {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.arc(100, 75, 50, 0, 6.28)
            this.offContext.setLineDash([10,20])
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
      }
      .width('100%')
      .height('100%')
E
ester.zhou 已提交
2443 2444 2445
    }
  }
  ```
E
esterzhou 已提交
2446
  ![en-us_image_000000127777772](figures/en-us_image_000000127777772.png)
E
ester.zhou 已提交
2447 2448 2449 2450 2451 2452 2453 2454


### getLineDash

getLineDash(): number[]

Obtains the dash line style.

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

E
ester.zhou 已提交
2457 2458
**Return value**

E
esterzhou 已提交
2459 2460
| Type      | Description                      |
| -------- | ------------------------ |
E
ester.zhou 已提交
2461 2462 2463 2464 2465 2466
| number[] | An array describing the interval of alternate line segments and length of spacing.|

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498
  @Entry
  @Component
  struct OffscreenCanvasGetLineDash {
    @State message: string = 'Hello World'
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Row() {
        Column() {
          Text(this.message)
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
            .onClick(()=>{
              console.error('before getlinedash clicked')
              let res = this.offContext.getLineDash()
              console.error(JSON.stringify(res))
            })
          Canvas(this.context)
            .width('100%')
            .height('100%')
            .backgroundColor('#ffff00')
            .onReady(() => {
              this.offContext.arc(100, 75, 50, 0, 6.28)
              this.offContext.setLineDash([10,20])
              this.offContext.stroke()
              let res = this.offContext.getLineDash()
              var image = this.offContext.transferToImageBitmap()
              this.context.transferFromImageBitmap(image)
            })
        }
        .width('100%')
E
ester.zhou 已提交
2499
      }
E
ester.zhou 已提交
2500
      .height('100%')
E
ester.zhou 已提交
2501 2502 2503
    }
  }
  ```
E
esterzhou 已提交
2504
![en-us_image_000000127777778](figures/en-us_image_000000127777778.png) 
E
ester.zhou 已提交
2505 2506 2507 2508 2509 2510 2511 2512 2513



### toDataURL

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

Generates a URL containing image display information.

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

E
ester.zhou 已提交
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
**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 已提交
2532 2533 2534 2535 2536 2537 2538
  // xxx.ets  
  @Entry
  @Component
  struct ToDataURL {
      private settings: RenderingContextSettings = new RenderingContextSettings(true)
      private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
      private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
ester.zhou 已提交
2539

E
ester.zhou 已提交
2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var dataURL = this.offContext.toDataURL()
          })
      }
      .width('100%')
      .height('100%')
E
ester.zhou 已提交
2552 2553 2554 2555 2556
    }
  }
  ```


E
esterzhou 已提交
2557 2558 2559 2560 2561 2562
### imageSmoothingQuality

imageSmoothingQuality(quality: imageSmoothingQuality)

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

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

E
ester.zhou 已提交
2565
 **Parameters**
E
esterzhou 已提交
2566

E
ester.zhou 已提交
2567
| Name | Type                   | Description                                      |
E
esterzhou 已提交
2568 2569 2570 2571
| ------- | --------------------- | ---------------------------------------- |
| quality | imageSmoothingQuality | Quality of image smoothing. The value can be **'low'**, **'medium'**,or **'high'**.|


E
ester.zhou 已提交
2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
### transferToImageBitmap

transferToImageBitmap(): ImageBitmap

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

**Return value**

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

E
ester.zhou 已提交
2584 2585

 **Example**
E
ester.zhou 已提交
2586 2587 2588 2589 2590

  ```ts
  // xxx.ets
  @Entry
  @Component
E
esterzhou 已提交
2591
  struct PutImageData {
E
ester.zhou 已提交
2592 2593 2594
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
E
esterzhou 已提交
2595

E
ester.zhou 已提交
2596 2597 2598 2599 2600 2601 2602
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
esterzhou 已提交
2603 2604 2605 2606 2607 2608 2609 2610
            var imageData = this.offContext.createImageData(100, 100)
            for (var i = 0; i < imageData.data.length; i += 4) {
              imageData.data[i + 0] = 255
              imageData.data[i + 1] = 0
              imageData.data[i + 2] = 255
              imageData.data[i + 3] = 255
            }
            this.offContext.putImageData(imageData, 10, 10)
E
ester.zhou 已提交
2611 2612 2613 2614 2615 2616 2617 2618 2619
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
    }
  }
  ```
E
esterzhou 已提交
2620
![en-us_image_0000001238952387](figures/en-us_image_0000001238952387.png) 
Z
zengyawen 已提交
2621

Z
zengyawen 已提交
2622
### restore
Z
zengyawen 已提交
2623

Z
zengyawen 已提交
2624
restore(): void
Z
zengyawen 已提交
2625 2626 2627

Restores the saved drawing context.

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

E
ester.zhou 已提交
2630
 **Example**
H
HelloCrease 已提交
2631

2632
  ```ts
2633
  // xxx.ets
E
ester.zhou 已提交
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.save() // save the default state
            this.offContext.fillStyle = "#00ff00"
            this.offContext.fillRect(20, 20, 100, 100)
            this.offContext.restore() // restore to the default state
            this.offContext.fillRect(150, 75, 100, 100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2659
    }
Z
zengyawen 已提交
2660 2661
  }
  ```
E
esterzhou 已提交
2662
![en-us_image_000000127777781](figures/en-us_image_000000127777781.png) 
Z
zengyawen 已提交
2663 2664


Z
zengyawen 已提交
2665
### save
Z
zengyawen 已提交
2666

Z
zengyawen 已提交
2667
save(): void
Z
zengyawen 已提交
2668 2669 2670

Saves the current drawing context.

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

E
ester.zhou 已提交
2673
 **Example**
H
HelloCrease 已提交
2674

2675
  ```ts
2676
  // xxx.ets
E
ester.zhou 已提交
2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.save() // save the default state
            this.offContext.fillStyle = "#00ff00"
            this.offContext.fillRect(20, 20, 100, 100)
            this.offContext.restore() // restore to the default state
            this.offContext.fillRect(150, 75, 100, 100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2702
    }
Z
zengyawen 已提交
2703 2704
  }
  ```
E
esterzhou 已提交
2705
![en-us_image_000000127777781](figures/en-us_image_000000127777781.png) 
Z
zengyawen 已提交
2706 2707


Z
zengyawen 已提交
2708
### createLinearGradient
Z
zengyawen 已提交
2709

Z
zengyawen 已提交
2710
createLinearGradient(x0: number, y0: number, x1: number, y1: number): void
Z
zengyawen 已提交
2711 2712 2713

Creates a linear gradient.

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

E
ester.zhou 已提交
2716
 **Parameters**
E
ester.zhou 已提交
2717

E
ester.zhou 已提交
2718
| Name | Type    | Mandatory  | Default Value | Description      |
E
ester.zhou 已提交
2719 2720 2721 2722 2723
| ---- | ------ | ---- | ---- | -------- |
| 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 已提交
2724

E
ester.zhou 已提交
2725
 **Example**
H
HelloCrease 已提交
2726

2727
  ```ts
2728
  // xxx.ets
Z
zengyawen 已提交
2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743
  @Entry
  @Component
  struct CreateLinearGradient {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var grad = this.offContext.createLinearGradient(50,0, 300,100)
E
ester.zhou 已提交
2744 2745 2746
            grad.addColorStop(0.0, '#ff0000')
            grad.addColorStop(0.5, '#ffffff')
            grad.addColorStop(1.0, '#00ff00')
Z
zengyawen 已提交
2747
            this.offContext.fillStyle = grad
E
ester.zhou 已提交
2748
            this.offContext.fillRect(0, 0, 400, 400)
Z
zengyawen 已提交
2749 2750 2751
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2752
      }
Z
zengyawen 已提交
2753 2754
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2755
    }
Z
zengyawen 已提交
2756 2757 2758
  }
  ```

E
ester.zhou 已提交
2759
  ![en-us_image_0000001194352460](figures/en-us_image_0000001194352460.png)
Z
zengyawen 已提交
2760 2761


Z
zengyawen 已提交
2762
### createRadialGradient
Z
zengyawen 已提交
2763

Z
zengyawen 已提交
2764
createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number): void
Z
zengyawen 已提交
2765 2766 2767

Creates a linear gradient.

E
ester.zhou 已提交
2768 2769 2770
Since API version 9, this API is supported in ArkTS widgets.

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

E
ester.zhou 已提交
2772
| Name | Type    | Mandatory  | Default Value | Description               |
E
ester.zhou 已提交
2773 2774 2775 2776 2777 2778 2779
| ---- | ------ | ---- | ---- | ----------------- |
| 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 已提交
2780

E
ester.zhou 已提交
2781
  **Example** 
H
HelloCrease 已提交
2782

2783
  ```ts
2784
  // xxx.ets
Z
zengyawen 已提交
2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799
  @Entry
  @Component
  struct CreateRadialGradient {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var grad = this.offContext.createRadialGradient(200,200,50, 200,200,200)
E
ester.zhou 已提交
2800 2801 2802
            grad.addColorStop(0.0, '#ff0000')
            grad.addColorStop(0.5, '#ffffff')
            grad.addColorStop(1.0, '#00ff00')
Z
zengyawen 已提交
2803
            this.offContext.fillStyle = grad
E
ester.zhou 已提交
2804
            this.offContext.fillRect(0, 0, 440, 440)
Z
zengyawen 已提交
2805 2806 2807
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2808
      }
Z
zengyawen 已提交
2809 2810
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2811
    }
Z
zengyawen 已提交
2812 2813
  }
  ```
Z
zengyawen 已提交
2814

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

E
ester.zhou 已提交
2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
### createConicGradient<sup>10+</sup>

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

Creates a conic gradient.

**Parameters**

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

**Example**

```ts
// xxx.ets
@Entry
@Component
struct OffscreenCanvasConicGradientPage {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)

  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffffff')
        .onReady(() =>{
          var grad = this.offContext.createConicGradient(0, 50, 80)
          grad.addColorStop(0.0, '#ff0000')
          grad.addColorStop(0.5, '#ffffff')
          grad.addColorStop(1.0, '#00ff00')
          this.offContext.fillStyle = grad
          this.offContext.fillRect(0, 30, 100, 100)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

  ![en-us_image_0000001239032419](figures/en-us_image_0000001239032420.png)
Z
zengyawen 已提交
2867

Z
zengyawen 已提交
2868
## CanvasPattern
Z
zengyawen 已提交
2869

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

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