ts-canvasrenderingcontext2d.md 104.7 KB
Newer Older
Z
zengyawen 已提交
1 2
# CanvasRenderingContext2D

E
ester.zhou 已提交
3
Use **RenderingContext** to draw rectangles, text, images, and other objects on a canvas.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6
> **NOTE**
>
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

E
esterzhou 已提交
9 10


Z
zengyawen 已提交
11 12
## APIs

E
ester.zhou 已提交
13
CanvasRenderingContext2D(settings?: RenderingContextSettings)
Z
zengyawen 已提交
14

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

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

E
ester.zhou 已提交
19 20 21
| Name     | Type                                    | Mandatory  | Description                                    |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| settings | [RenderingContextSettings](#renderingcontextsettings) | No   | See [RenderingContextSettings](#renderingcontextsettings).|
Z
zengyawen 已提交
22 23 24 25


### RenderingContextSettings

26
RenderingContextSettings(antialias?: boolean)
Z
zengyawen 已提交
27 28 29

Configures the settings of a **CanvasRenderingContext2D** object, including whether to enable antialiasing.

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

E
ester.zhou 已提交
32 33 34 35
**Parameters**

| Name      | Type   | Mandatory  | Description                         |
| --------- | ------- | ---- | ----------------------------- |
E
ester.zhou 已提交
36
| antialias | boolean | No   | Whether to enable antialiasing.<br>Default value: **false**|
Z
zengyawen 已提交
37 38 39 40


## Attributes

E
ester.zhou 已提交
41 42
| Name                                      | Type                                      | Description                                      |
| ---------------------------------------- | ---------------------------------------- | ---------------------------------------- |
E
ester.zhou 已提交
43
| [fillStyle](#fillstyle)                  | string \| number<sup>10+</sup> \| [CanvasGradient](ts-components-canvas-canvasgradient.md) \| [CanvasPattern](ts-components-canvas-canvaspattern.md#canvaspattern) | Style to fill an area.<br>- When the type is string, this attribute indicates the color of the fill area.<br>Default value: **'black'**<br>- When the type is number, this attribute indicates the color of the fill area.<br>Default value: **'#000000'**<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.|
E
ester.zhou 已提交
44
| [lineWidth](#linewidth)                  | number                                   | Line width.<br>Default value: **1(px)**                              |
E
ester.zhou 已提交
45
| [strokeStyle](#strokestyle)              | string \| number<sup>10+</sup> \| [CanvasGradient](ts-components-canvas-canvasgradient.md) \| [CanvasPattern](ts-components-canvas-canvaspattern.md#canvaspattern) | Stroke style.<br>- When the type is string, this attribute indicates the stroke color.<br>Default value: **'black'**<br>- When the type is number, this attribute indicates the stroke color.<br>Default value: **'#000000'**<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.|
E
ester.zhou 已提交
46 47 48
| [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 by adding a box with an equal width and half the height of the line's 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 shape used to join line segments is a sector, whose radius at the rounded corner is equal to the line width.<br>- **'bevel'**: The shape used to join line segments is a triangle. The rectangular corner of each line is independent.<br>- **'miter'**: The shape used to join line segments has a mitered corner by extending the outside edges of the lines until they meet. You can view the effect of this attribute in **miterLimit**.<br>Default value: **'miter'**<br>Since API version 9, this API is supported in ArkTS widgets.|
| [miterLimit](#miterlimit)                | number                                   | Maximum miter length. The miter length is the distance between the inner corner and the outer corner where two lines meet.<br>Default value: **10**<br>Since API version 9, this API is supported in ArkTS widgets.|
E
ester.zhou 已提交
49
| [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 px.<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 must be specified and can only be px or vp.<br>- (Optional) **font-family**: font family. Available values are **'sans-serif'**, **'serif'**, and **'monospace'**.<br>Default value: **'normal normal 14px sans-serif'**<br>Since API version 9, this API is supported in ArkTS widgets.|
E
ester.zhou 已提交
50 51 52 53
| [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>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.<br>Since API version 9, this API is supported in ArkTS widgets.|
| [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.|
E
ester.zhou 已提交
54
| [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.|
E
ester.zhou 已提交
55 56 57 58
| [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.|
E
ester.zhou 已提交
59
| [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
ester.zhou 已提交
60 61
| [height](#height)                        | number                                   | Component height.<br>Unit: vp<br>Since API version 9, this API is supported in ArkTS widgets.|
| [width](#width)                          | number                                   | Component width.<br>Unit: vp<br>Since API version 9, this API is supported in ArkTS widgets.|
E
ester.zhou 已提交
62 63
| [imageSmoothingQuality](#imagesmoothingquality)       | [ImageSmoothingQuality](#imagesmoothingquality-1)     | Quality of image smoothing. This attribute works only when **imageSmoothingEnabled** is set to **true**.<br>Default value: **ImageSmoothingQuality.low**<br>Since API version 9, this API is supported in ArkTS widgets.|
| [direction](#direction)       | [CanvasDirection](#canvasdirection)    | Text direction used for drawing text.<br>Default value: **CanvasDirection.inherit**<br>Since API version 9, this API is supported in ArkTS widgets.|
E
ester.zhou 已提交
64
| [filter](#filter)                        | string                                   | Filter effect. Available values are as follows:<br>- **'none'**: no filter effect.<br>- **'blur'**: applies the Gaussian blur for the image.<br>- **'brightness'**: applies a linear multiplication to the image to make it look brighter or darker.<br>- **'contrast'**: adjusts the image contrast.<br>- **'grayscale'**: converts the image to a grayscale image.<br>- **'hue-rotate'**: applies hue rotation to the image.<br>- **'invert'**: inverts the input image.<br>- **'opacity'**: sets the opacity of the image.<br>- **'saturate'**: sets the saturation of the image.<br>- **'sepia'**: converts the image to dark brown.<br>Default value: **'none'**<br>Since API version 9, this API is supported in ArkTS widgets.|
E
ester.zhou 已提交
65 66 67 68

> **NOTE**
>
> The string-type value of **fillStyle**, **shadowColor**, and **strokeStyle** can be in 'rgb(255, 255, 255)', 'rgba(255, 255, 255, 1.0)', or '\#FFFFFF' format.
Z
zengyawen 已提交
69 70 71 72


### fillStyle

73 74
```ts
// xxx.ets
Z
zengyawen 已提交
75 76 77
@Entry
@Component
struct FillStyleExample {
E
ester.zhou 已提交
78 79
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
80 81 82 83 84 85 86 87 88

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.context.fillStyle = '#0000ff'
E
ester.zhou 已提交
89
          this.context.fillRect(20, 20, 150, 100)
Z
zengyawen 已提交
90 91 92 93 94 95 96 97
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
98 99 100 101
![en-us_image_0000001257058395](figures/en-us_image_0000001257058395.png)


### lineWidth
Z
zengyawen 已提交
102

103 104
```ts
// xxx.ets
Z
zengyawen 已提交
105 106 107
@Entry
@Component
struct LineWidthExample {
E
ester.zhou 已提交
108 109
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

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

Z
zengyawen 已提交
128 129 130 131
![en-us_image_0000001212378408](figures/en-us_image_0000001212378408.png)


### strokeStyle
Z
zengyawen 已提交
132

133 134
```ts
// xxx.ets
Z
zengyawen 已提交
135 136 137
@Entry
@Component
struct StrokeStyleExample {
E
ester.zhou 已提交
138 139
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

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


Z
zengyawen 已提交
160 161 162 163 164
![en-us_image_0000001212218446](figures/en-us_image_0000001212218446.png)


### lineCap

165 166
```ts
// xxx.ets
Z
zengyawen 已提交
167 168 169
@Entry
@Component
struct LineCapExample {
E
ester.zhou 已提交
170 171
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

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

Z
zengyawen 已提交
194 195 196 197
![en-us_image_0000001212378406](figures/en-us_image_0000001212378406.png)


### lineJoin
Z
zengyawen 已提交
198

199 200
```ts
// xxx.ets
Z
zengyawen 已提交
201 202 203
@Entry
@Component
struct LineJoinExample {
E
ester.zhou 已提交
204 205
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

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

Z
zengyawen 已提交
229 230 231 232
![en-us_image_0000001212218438](figures/en-us_image_0000001212218438.png)


### miterLimit
Z
zengyawen 已提交
233

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

Z
zengyawen 已提交
264 265 266 267
![en-us_image_0000001212378398](figures/en-us_image_0000001212378398.png)


### font
Z
zengyawen 已提交
268

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

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


### textAlign
Z
zengyawen 已提交
298

299 300
```ts
// xxx.ets
Z
zengyawen 已提交
301 302
@Entry
@Component
E
ester.zhou 已提交
303
struct CanvasExample {
E
ester.zhou 已提交
304 305
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
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
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
        this.context.strokeStyle = '#0000ff'
        this.context.moveTo(140, 10)
        this.context.lineTo(140, 160)
        this.context.stroke()
        this.context.font = '18px sans-serif'
        this.context.textAlign = 'start'
        this.context.fillText('textAlign=start', 140, 60)
        this.context.textAlign = 'end'
        this.context.fillText('textAlign=end', 140, 80)
        this.context.textAlign = 'left'
        this.context.fillText('textAlign=left', 140, 100)
        this.context.textAlign = 'center'
        this.context.fillText('textAlign=center',140, 120)
        this.context.textAlign = 'right'
        this.context.fillText('textAlign=right',140, 140)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
337 338 339 340
![en-us_image_0000001256978351](figures/en-us_image_0000001256978351.png)


### textBaseline
Z
zengyawen 已提交
341

342 343
```ts
// xxx.ets
Z
zengyawen 已提交
344 345 346
@Entry
@Component
struct TextBaseline {
E
ester.zhou 已提交
347 348
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.context.strokeStyle = '#0000ff'
          this.context.moveTo(0, 120)
          this.context.lineTo(400, 120)
          this.context.stroke()
          this.context.font = '20px sans-serif'
          this.context.textBaseline = 'top'
          this.context.fillText('Top', 10, 120)
          this.context.textBaseline = 'bottom'
          this.context.fillText('Bottom', 55, 120)
          this.context.textBaseline = 'middle'
          this.context.fillText('Middle', 125, 120)
          this.context.textBaseline = 'alphabetic'
          this.context.fillText('Alphabetic', 195, 120)
          this.context.textBaseline = 'hanging'
          this.context.fillText('Hanging', 295, 120)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
380 381 382 383
![en-us_image_0000001212058462](figures/en-us_image_0000001212058462.png)


### globalAlpha
Z
zengyawen 已提交
384

385 386
```ts
// xxx.ets
Z
zengyawen 已提交
387 388 389
@Entry
@Component
struct GlobalAlpha {
E
ester.zhou 已提交
390 391
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
392 393 394 395 396 397 398 399
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
400 401 402 403 404
          this.context.fillStyle = 'rgb(0,0,255)'
          this.context.fillRect(0, 0, 50, 50)
          this.context.globalAlpha = 0.4
          this.context.fillStyle = 'rgb(0,0,255)'
          this.context.fillRect(50, 50, 50, 50)
Z
zengyawen 已提交
405 406 407 408 409 410 411 412
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
413
![en-us_image_0000001194192434](figures/en-us_image_0000001194192434.png)
Z
zengyawen 已提交
414 415 416


### lineDashOffset
Z
zengyawen 已提交
417

418 419
```ts
// xxx.ets
Z
zengyawen 已提交
420 421 422
@Entry
@Component
struct LineDashOffset {
E
ester.zhou 已提交
423 424
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
425 426 427 428 429 430 431 432 433 434
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.context.arc(100, 75, 50, 0, 6.28)
          this.context.setLineDash([10,20])
E
ester.zhou 已提交
435 436
          this.context.lineDashOffset = 10.0
          this.context.stroke()
Z
zengyawen 已提交
437 438 439 440 441 442 443 444
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

E
ester.zhou 已提交
445
![en-us_image_0000001194352434](figures/en-us_image_0000001194352434.png)
Z
zengyawen 已提交
446 447 448 449


### globalCompositeOperation

E
ester.zhou 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462
| 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 已提交
463

464 465
```ts
// xxx.ets
Z
zengyawen 已提交
466 467 468
@Entry
@Component
struct GlobalCompositeOperation {
E
ester.zhou 已提交
469 470
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.context.fillStyle = 'rgb(255,0,0)'
          this.context.fillRect(20, 20, 50, 50)
          this.context.globalCompositeOperation = 'source-over'
          this.context.fillStyle = 'rgb(0,0,255)'
          this.context.fillRect(50, 50, 50, 50)
          this.context.fillStyle = 'rgb(255,0,0)'
          this.context.fillRect(120, 20, 50, 50)
          this.context.globalCompositeOperation = 'destination-over'
          this.context.fillStyle = 'rgb(0,0,255)'
          this.context.fillRect(150, 50, 50, 50)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
497 498 499 500
![en-us_image_0000001211898480](figures/en-us_image_0000001211898480.png)


### shadowBlur
Z
zengyawen 已提交
501

502 503
```ts
// xxx.ets
Z
zengyawen 已提交
504 505 506
@Entry
@Component
struct ShadowBlur {
E
ester.zhou 已提交
507 508
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.context.shadowBlur = 30
          this.context.shadowColor = 'rgb(0,0,0)'
          this.context.fillStyle = 'rgb(255,0,0)'
          this.context.fillRect(20, 20, 100, 80)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
529 530 531 532
![en-us_image_0000001256978343](figures/en-us_image_0000001256978343.png)


### shadowColor
Z
zengyawen 已提交
533

534 535
```ts
// xxx.ets
Z
zengyawen 已提交
536 537 538
@Entry
@Component
struct ShadowColor {
E
ester.zhou 已提交
539 540
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.context.shadowBlur = 30
          this.context.shadowColor = 'rgb(0,0,255)'
          this.context.fillStyle = 'rgb(255,0,0)'
          this.context.fillRect(30, 30, 100, 100)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
561 562 563 564
![en-us_image_0000001257138353](figures/en-us_image_0000001257138353.png)


### shadowOffsetX
Z
zengyawen 已提交
565

566 567
```ts
// xxx.ets
Z
zengyawen 已提交
568 569 570
@Entry
@Component
struct ShadowOffsetX {
E
ester.zhou 已提交
571 572
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.context.shadowBlur = 10
          this.context.shadowOffsetX = 20
          this.context.shadowColor = 'rgb(0,0,0)'
          this.context.fillStyle = 'rgb(255,0,0)'
          this.context.fillRect(20, 20, 100, 80)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
594 595 596 597
![en-us_image_0000001212218436](figures/en-us_image_0000001212218436.png)


### shadowOffsetY
Z
zengyawen 已提交
598

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

Z
zengyawen 已提交
626 627 628 629
![en-us_image_0000001212378410](figures/en-us_image_0000001212378410.png)


### imageSmoothingEnabled
Z
zengyawen 已提交
630

631 632
```ts
// xxx.ets
Z
zengyawen 已提交
633 634 635
@Entry
@Component
struct ImageSmoothingEnabled {
E
ester.zhou 已提交
636 637 638
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private img:ImageBitmap = new ImageBitmap("common/images/icon.jpg")
Z
zengyawen 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.context.imageSmoothingEnabled = false
          this.context.drawImage( this.img,0,0,400,200)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
657
![en-us_image_0000001211898472](figures/en-us_image_0000001211898472.png)
Z
zengyawen 已提交
658 659


E
ester.zhou 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
### height

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

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width(300)
        .height(300)
        .backgroundColor('#ffff00')
        .onReady(() => {
          let h = this.context.height
          let w = this.context.width
          this.context.fillRect(0, 0, 300, h/2)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

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


### width

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

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width(300)
        .height(300)
        .backgroundColor('#ffff00')
        .onReady(() => {
          let h = this.context.height
          let w = this.context.width
          this.context.fillRect(0, 0, w/2, 300)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

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


E
ester.zhou 已提交
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
### imageSmoothingQuality

```ts
  // xxx.ets
  @Entry
  @Component
  struct ImageSmoothingQualityDemo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true);
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
    private img:ImageBitmap = new ImageBitmap("common/images/example.jpg");

    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            let ctx = this.context
            ctx.imageSmoothingEnabled = true
            ctx.imageSmoothingQuality = 'high'
            ctx.drawImage(this.img, 0, 0, 400, 200)
          })
      }
      .width('100%')
      .height('100%')
    }
  }
```

![ImageSmoothingQualityDemo](figures/ImageSmoothingQualityDemo.jpeg)


### direction

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

    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            let ctx = this.context
            ctx.font = '48px serif';
            ctx.textAlign = 'start'
            ctx.fillText("Hi ltr!", 200, 50);

            ctx.direction = "rtl";
            ctx.fillText("Hi rtl!", 200, 100);
          })
      }
      .width('100%')
      .height('100%')
    }
  }
```

![directionDemo](figures/directionDemo.jpeg)


### filter

```ts
  // xxx.ets
  @Entry
  @Component
  struct FilterDemo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true);
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
    private img:ImageBitmap = new ImageBitmap("common/images/example.jpg");

    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            let ctx = this.context
            let img = this.img

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

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

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

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

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

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

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

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

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

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

            let result = ctx.toDataURL()
            console.info(result)
          })
      }
      .width('100%')
      .height('100%')
    }
  }
```

![filterDemo](figures/filterDemo.jpeg)

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

Z
zengyawen 已提交
854 855 856 857

### fillRect

fillRect(x: number, y: number, w: number, h: number): void
Z
zengyawen 已提交
858 859 860

Fills a rectangle on the canvas.

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

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

| Name    | Type    | Mandatory  | Default Value | Description           |
| ------ | ------ | ---- | ---- | ------------- |
E
ester.zhou 已提交
867 868
| 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.|
E
ester.zhou 已提交
869 870
| width  | number | Yes   | 0    | Width of the rectangle.     |
| height | number | Yes   | 0    | Height of the rectangle.     |
Z
zengyawen 已提交
871

E
ester.zhou 已提交
872
**Example**
H
HelloCrease 已提交
873

874 875
  ```ts
  // xxx.ets
Z
zengyawen 已提交
876 877 878
  @Entry
  @Component
  struct FillRect {
E
ester.zhou 已提交
879 880
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
881 882 883 884 885 886 887 888
    
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
889
            this.context.fillRect(30,30,100,100)
Z
zengyawen 已提交
890
         })
Z
zengyawen 已提交
891
        }
Z
zengyawen 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
      .width('100%')
      .height('100%')
    }
  }
  ```

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


### strokeRect

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

Draws an outlined rectangle on the canvas.

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

E
ester.zhou 已提交
909 910 911 912
**Parameters**

| Name  | Type    | Mandatory  | Default Value | Description          |
| ---- | ------ | ---- | ---- | ------------ |
E
ester.zhou 已提交
913 914
| 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.|
E
ester.zhou 已提交
915 916
| w    | number | Yes   | 0    | Width of the rectangle.    |
| h    | number | Yes   | 0    | Height of the rectangle.    |
Z
zengyawen 已提交
917

E
ester.zhou 已提交
918
**Example**
H
HelloCrease 已提交
919

920 921
  ```ts
  // xxx.ets
Z
zengyawen 已提交
922 923 924
  @Entry
  @Component
  struct StrokeRect {
E
ester.zhou 已提交
925 926
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
esterzhou 已提交
927

Z
zengyawen 已提交
928 929 930 931 932 933 934 935 936
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.strokeRect(30, 30, 200, 150)
        })
Z
zengyawen 已提交
937
      }
Z
zengyawen 已提交
938 939
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
940
    }
Z
zengyawen 已提交
941 942
  }
  ```
Z
zengyawen 已提交
943

Z
zengyawen 已提交
944
  ![en-us_image_0000001257138359](figures/en-us_image_0000001257138359.png)
Z
zengyawen 已提交
945 946


Z
zengyawen 已提交
947
### clearRect
Z
zengyawen 已提交
948

Z
zengyawen 已提交
949
clearRect(x: number, y: number, w: number, h: number): void
Z
zengyawen 已提交
950 951 952

Clears the content in a rectangle on the canvas.

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

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

| Name  | Type    | Mandatory  | Default Value | Description           |
| ---- | ------ | ---- | ---- | ------------- |
E
ester.zhou 已提交
959 960
| 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.|
E
ester.zhou 已提交
961 962
| w    | number | Yes   | 0    | Width of the rectangle.     |
| h    | number | Yes   | 0    | Height of the rectangle.     |
Z
zengyawen 已提交
963

E
ester.zhou 已提交
964
**Example**
H
HelloCrease 已提交
965

966 967
  ```ts
  // xxx.ets
Z
zengyawen 已提交
968 969 970
  @Entry
  @Component
  struct ClearRect {
E
ester.zhou 已提交
971 972
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
esterzhou 已提交
973

Z
zengyawen 已提交
974 975 976 977 978
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
E
ester.zhou 已提交
979
          .backgroundColor('#ffff00')
Z
zengyawen 已提交
980 981
          .onReady(() =>{
            this.context.fillStyle = 'rgb(0,0,255)'
E
esterzhou 已提交
982 983
            this.context.fillRect(20,20,200,200)
            this.context.clearRect(30,30,150,100)
Z
zengyawen 已提交
984
        })
Z
zengyawen 已提交
985
      }
Z
zengyawen 已提交
986 987
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
988
    }
Z
zengyawen 已提交
989 990
  }
  ```
Z
zengyawen 已提交
991

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


Z
zengyawen 已提交
995
### fillText
Z
zengyawen 已提交
996

E
ester.zhou 已提交
997
fillText(text: string, x: number, y: number, maxWidth?: number): void
Z
zengyawen 已提交
998 999 1000

Draws filled text on the canvas.

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

E
ester.zhou 已提交
1003 1004
**Parameters**

E
ester.zhou 已提交
1005 1006 1007
| Name      | Type    | Mandatory  | Default Value | Description             |
| -------- | ------ | ---- | ---- | --------------- |
| text     | string | Yes   | ''   | Text to draw.     |
E
ester.zhou 已提交
1008 1009
| 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.|
E
ester.zhou 已提交
1010
| maxWidth | number | No   | -    | Maximum width allowed for the text.   |
Z
zengyawen 已提交
1011

E
ester.zhou 已提交
1012
**Example**
H
HelloCrease 已提交
1013

1014 1015
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1016 1017 1018
  @Entry
  @Component
  struct FillText {
E
ester.zhou 已提交
1019 1020
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
esterzhou 已提交
1021

Z
zengyawen 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.font = '30px sans-serif'
            this.context.fillText("Hello World!", 20, 100)
        })
Z
zengyawen 已提交
1032
      }
Z
zengyawen 已提交
1033 1034
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1035
    }
Z
zengyawen 已提交
1036 1037
  }
  ```
Z
zengyawen 已提交
1038

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


Z
zengyawen 已提交
1042
### strokeText
Z
zengyawen 已提交
1043

E
ester.zhou 已提交
1044
strokeText(text: string, x: number, y: number, maxWidth?:number): void
Z
zengyawen 已提交
1045 1046 1047

Draws a text stroke on the canvas.

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

E
ester.zhou 已提交
1050 1051
**Parameters**

E
ester.zhou 已提交
1052 1053 1054
| Name      | Type    | Mandatory  | Default Value | Description             |
| -------- | ------ | ---- | ---- | --------------- |
| text     | string | Yes   | ''   | Text to draw.     |
E
ester.zhou 已提交
1055 1056
| 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.|
E
ester.zhou 已提交
1057
| maxWidth | number | No   | -    | Maximum width of the text to be drawn. |
Z
zengyawen 已提交
1058

E
ester.zhou 已提交
1059
**Example**
H
HelloCrease 已提交
1060

1061 1062
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1063 1064 1065
  @Entry
  @Component
  struct StrokeText {
E
ester.zhou 已提交
1066 1067
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
esterzhou 已提交
1068

Z
zengyawen 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.font = '55px sans-serif'
            this.context.strokeText("Hello World!", 20, 60)
        })
Z
zengyawen 已提交
1079
      }
Z
zengyawen 已提交
1080 1081
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1082
    }
Z
zengyawen 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
  }
  ```

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


### measureText

measureText(text: string): TextMetrics

E
ester.zhou 已提交
1093 1094
Measures the specified text to obtain its width. This API returns a **TextMetrics** object.

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

E
ester.zhou 已提交
1097
**Parameters**
Z
zengyawen 已提交
1098

E
ester.zhou 已提交
1099 1100 1101
| Name  | Type    | Mandatory  | Default Value | Description        |
| ---- | ------ | ---- | ---- | ---------- |
| text | string | Yes   | ''   | Text to be measured.|
Z
zengyawen 已提交
1102

E
ester.zhou 已提交
1103
**Return value**
Z
zengyawen 已提交
1104

E
ester.zhou 已提交
1105 1106
| Type         | Description                                      |
| ----------- | ---------------------------------------- |
E
ester.zhou 已提交
1107
| TextMetrics | **TextMetrics** object.<br>Since API version 9, this API is supported in ArkTS widgets.|
Z
zengyawen 已提交
1108

E
ester.zhou 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
**TextMetrics**

| Attribute                      | Type    | Description                                      |
| ------------------------ | ------ | ---------------------------------------- |
| width                    | number | Width of the text.                                 |
| height                   | number | Height of the text.                                 |
| actualBoundingBoxAscent  | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the top of the bounding rectangle used to render the text. The current value is **0**.|
| actualBoundingBoxDescent | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the bottom of the bounding rectangle used to render the text. The current value is **0**.|
| actualBoundingBoxLeft    | number | Distance parallel to the baseline from the alignment point determined by the **CanvasRenderingContext2D.textAlign** attribute to the left side of the bounding rectangle of the text. The current value is **0**.|
| actualBoundingBoxRight   | number | Distance parallel to the baseline from the alignment point determined by the **CanvasRenderingContext2D.textAlign** attribute to the right side of the bounding rectangle of the text. The current value is **0**.|
| alphabeticBaseline       | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the alphabetic baseline of the line box. The current value is **0**.|
| emHeightAscent           | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the top of the em square in the line box. The current value is **0**.|
| emHeightDescent          | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the bottom of the em square in the line box. The current value is **0**.|
| fontBoundingBoxAscent    | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the top of the highest bounding rectangle of all the fonts used to render the text. The current value is **0**.|
| fontBoundingBoxDescent   | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the bottom of the bounding rectangle of all the fonts used to render the text. The current value is **0**.|
| hangingBaseline          | number | Distance from the horizontal line specified by the **CanvasRenderingContext2D.textBaseline** attribute to the hanging baseline of the line box. The current value is **0**.|
| ideographicBaseline      | number | Distance from the horizontal line indicated by the **CanvasRenderingContext2D.textBaseline** attribute to the ideographic baseline of the line box. The current value is **0**.|

  


**Example**
H
HelloCrease 已提交
1131

1132 1133
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1134 1135 1136
  @Entry
  @Component
  struct MeasureText {
E
ester.zhou 已提交
1137 1138
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
esterzhou 已提交
1139

Z
zengyawen 已提交
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.font = '50px sans-serif'
            this.context.fillText("Hello World!", 20, 100)
            this.context.fillText("width:" + this.context.measureText("Hello World!").width, 20, 200)
        })
Z
zengyawen 已提交
1151
      }
Z
zengyawen 已提交
1152 1153
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1154
    }
Z
zengyawen 已提交
1155 1156
  }
  ```
Z
zengyawen 已提交
1157

Z
zengyawen 已提交
1158
  ![en-us_image_0000001257138343](figures/en-us_image_0000001257138343.png)
Z
zengyawen 已提交
1159 1160


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

Z
zengyawen 已提交
1163
stroke(path?: Path2D): void
Z
zengyawen 已提交
1164 1165 1166

Strokes a path.

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

E
ester.zhou 已提交
1169 1170 1171 1172 1173
**Parameters**

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

E
ester.zhou 已提交
1175
**Example**
H
HelloCrease 已提交
1176

1177 1178
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1179 1180 1181
  @Entry
  @Component
  struct Stroke {
E
ester.zhou 已提交
1182 1183
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
1184

Z
zengyawen 已提交
1185 1186 1187 1188 1189 1190 1191 1192 1193
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.moveTo(25, 25)
            this.context.lineTo(25, 105)
E
esterzhou 已提交
1194 1195
            this.context.lineTo(75, 105)
            this.context.lineTo(75, 25)
Z
zengyawen 已提交
1196 1197 1198
            this.context.strokeStyle = 'rgb(0,0,255)'
            this.context.stroke()
          })
Z
zengyawen 已提交
1199
      }
Z
zengyawen 已提交
1200 1201
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1202
    }
Z
zengyawen 已提交
1203 1204
  }
  ```
Z
zengyawen 已提交
1205

Z
zengyawen 已提交
1206
  ![en-us_image_0000001257058411](figures/en-us_image_0000001257058411.png)
Z
zengyawen 已提交
1207 1208


Z
zengyawen 已提交
1209
### beginPath
Z
zengyawen 已提交
1210

Z
zengyawen 已提交
1211
beginPath(): void
Z
zengyawen 已提交
1212 1213 1214

Creates a drawing path.

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

E
ester.zhou 已提交
1217
**Example**
H
HelloCrease 已提交
1218

1219 1220
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1221 1222 1223
  @Entry
  @Component
  struct BeginPath {
E
ester.zhou 已提交
1224 1225
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
1226

Z
zengyawen 已提交
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.beginPath()
            this.context.lineWidth = 6
            this.context.strokeStyle = '#0000ff'
            this.context.moveTo(15, 80)
            this.context.lineTo(280, 160)
            this.context.stroke()
          })
Z
zengyawen 已提交
1241
      }
Z
zengyawen 已提交
1242 1243
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1244
    }
Z
zengyawen 已提交
1245 1246
  }
  ```
Z
zengyawen 已提交
1247

Z
zengyawen 已提交
1248
  ![en-us_image_0000001212058478](figures/en-us_image_0000001212058478.png)
Z
zengyawen 已提交
1249 1250


Z
zengyawen 已提交
1251
### moveTo
Z
zengyawen 已提交
1252

Z
zengyawen 已提交
1253
moveTo(x: number, y: number): void
Z
zengyawen 已提交
1254 1255 1256

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

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

E
ester.zhou 已提交
1259 1260 1261 1262
**Parameters**

| Name  | Type    | Mandatory  | Default Value | Description       |
| ---- | ------ | ---- | ---- | --------- |
E
ester.zhou 已提交
1263 1264
| x    | number | Yes   | 0    | X coordinate of the target position.|
| y    | number | Yes   | 0    | Y coordinate of the target position.|
Z
zengyawen 已提交
1265

E
ester.zhou 已提交
1266
**Example**
H
HelloCrease 已提交
1267

1268 1269
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1270 1271 1272
  @Entry
  @Component
  struct MoveTo {
E
ester.zhou 已提交
1273 1274
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
1275

Z
zengyawen 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.beginPath()
            this.context.moveTo(10, 10)
            this.context.lineTo(280, 160)
            this.context.stroke()
          })
Z
zengyawen 已提交
1288
      }
Z
zengyawen 已提交
1289 1290
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1291
    }
Z
zengyawen 已提交
1292 1293
  }
  ```
Z
zengyawen 已提交
1294

Z
zengyawen 已提交
1295
  ![en-us_image_0000001256858391](figures/en-us_image_0000001256858391.png)
Z
zengyawen 已提交
1296 1297


Z
zengyawen 已提交
1298
### lineTo
Z
zengyawen 已提交
1299

Z
zengyawen 已提交
1300
lineTo(x: number, y: number): void
Z
zengyawen 已提交
1301 1302 1303

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

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

E
ester.zhou 已提交
1306 1307 1308 1309
**Parameters**

| Name  | Type    | Mandatory  | Default Value | Description       |
| ---- | ------ | ---- | ---- | --------- |
E
ester.zhou 已提交
1310 1311
| x    | number | Yes   | 0    | X coordinate of the target position.|
| y    | number | Yes   | 0    | Y coordinate of the target position.|
Z
zengyawen 已提交
1312

E
ester.zhou 已提交
1313
**Example**
H
HelloCrease 已提交
1314

1315 1316
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1317 1318 1319
  @Entry
  @Component
  struct LineTo {
E
ester.zhou 已提交
1320 1321
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
1322

Z
zengyawen 已提交
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.beginPath()
            this.context.moveTo(10, 10)
            this.context.lineTo(280, 160)
            this.context.stroke()
          })
Z
zengyawen 已提交
1335
      }
Z
zengyawen 已提交
1336 1337
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1338
    }
Z
zengyawen 已提交
1339 1340
  }
  ```
Z
zengyawen 已提交
1341

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


Z
zengyawen 已提交
1345
### closePath
Z
zengyawen 已提交
1346

Z
zengyawen 已提交
1347
closePath(): void
Z
zengyawen 已提交
1348 1349 1350

Draws a closed path.

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

E
ester.zhou 已提交
1353
**Example**
H
HelloCrease 已提交
1354

1355 1356
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1357 1358 1359
  @Entry
  @Component
  struct ClosePath {
E
ester.zhou 已提交
1360 1361
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
1362

Z
zengyawen 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
              this.context.beginPath()
              this.context.moveTo(30, 30)
              this.context.lineTo(110, 30)
              this.context.lineTo(70, 90)
              this.context.closePath()
              this.context.stroke()
          })
Z
zengyawen 已提交
1377
      }
Z
zengyawen 已提交
1378 1379
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1380
    }
Z
zengyawen 已提交
1381 1382
  }
  ```
Z
zengyawen 已提交
1383

Z
zengyawen 已提交
1384
  ![en-us_image_0000001256978347](figures/en-us_image_0000001256978347.png)
Z
zengyawen 已提交
1385 1386


Z
zengyawen 已提交
1387
### createPattern
Z
zengyawen 已提交
1388

E
ester.zhou 已提交
1389
createPattern(image: ImageBitmap, repetition: string | null): CanvasPattern | null
Z
zengyawen 已提交
1390 1391 1392

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

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

E
ester.zhou 已提交
1395 1396
**Parameters**

E
ester.zhou 已提交
1397 1398 1399 1400
| Name        | Type                                      | Mandatory  | Description                                      |
| ---------- | ---------------------------------------- | ---- | ---------------------------------------- |
| image      | [ImageBitmap](ts-components-canvas-imagebitmap.md) | Yes   | Source image. For details, see **ImageBitmap**.                 |
| repetition | string                                   | Yes   | Repetition mode. The value can be **'repeat'**, **'repeat-x'**, **'repeat-y'**, **'no-repeat'**, **'clamp'**, or **'mirror'**.<br>Default value: **''**|
E
ester.zhou 已提交
1401 1402

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

E
ester.zhou 已提交
1404 1405 1406
| Type                                      | Description                     |
| ---------------------------------------- | ----------------------- |
| [CanvasPattern](ts-components-canvas-canvaspattern.md#canvaspattern) | Created pattern for image filling based on a specified source image and repetition mode.|
E
ester.zhou 已提交
1407 1408

**Example**
H
HelloCrease 已提交
1409

1410 1411
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1412 1413 1414
  @Entry
  @Component
  struct CreatePattern {
E
ester.zhou 已提交
1415 1416 1417
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private img:ImageBitmap = new ImageBitmap("common/images/icon.jpg")
H
HelloCrease 已提交
1418

Z
zengyawen 已提交
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var pattern = this.context.createPattern(this.img, 'repeat')
            this.context.fillStyle = pattern
            this.context.fillRect(0, 0, 200, 200)
          })
Z
zengyawen 已提交
1430
      }
Z
zengyawen 已提交
1431 1432
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1433
    }
Z
zengyawen 已提交
1434 1435
  }
  ```
Z
zengyawen 已提交
1436

Z
zengyawen 已提交
1437
  ![en-us_image_0000001211898490](figures/en-us_image_0000001211898490.png)
Z
zengyawen 已提交
1438 1439


Z
zengyawen 已提交
1440
### bezierCurveTo
Z
zengyawen 已提交
1441

Z
zengyawen 已提交
1442
bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void
Z
zengyawen 已提交
1443 1444 1445

Draws a cubic bezier curve on the canvas.

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

E
ester.zhou 已提交
1448 1449 1450 1451
**Parameters**

| Name  | Type    | Mandatory  | Default Value | Description            |
| ---- | ------ | ---- | ---- | -------------- |
E
ester.zhou 已提交
1452 1453 1454 1455 1456 1457
| 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 已提交
1458

E
ester.zhou 已提交
1459
**Example**
H
HelloCrease 已提交
1460

1461 1462
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1463 1464 1465
  @Entry
  @Component
  struct BezierCurveTo {
E
ester.zhou 已提交
1466 1467
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
1468

Z
zengyawen 已提交
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.beginPath()
            this.context.moveTo(10, 10)
            this.context.bezierCurveTo(20, 100, 200, 100, 200, 20)
            this.context.stroke()
          })
Z
zengyawen 已提交
1481
      }
Z
zengyawen 已提交
1482 1483
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1484
    }
Z
zengyawen 已提交
1485 1486
  }
  ```
Z
zengyawen 已提交
1487

Z
zengyawen 已提交
1488
  ![en-us_image_0000001257138349](figures/en-us_image_0000001257138349.png)
Z
zengyawen 已提交
1489 1490


Z
zengyawen 已提交
1491
### quadraticCurveTo
Z
zengyawen 已提交
1492

Z
zengyawen 已提交
1493
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void
Z
zengyawen 已提交
1494 1495 1496

Draws a quadratic curve on the canvas.

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

E
ester.zhou 已提交
1499
**Parameters**
Z
zengyawen 已提交
1500

E
ester.zhou 已提交
1501 1502
| Name  | Type    | Mandatory  | Default Value | Description         |
| ---- | ------ | ---- | ---- | ----------- |
E
ester.zhou 已提交
1503 1504 1505 1506
| 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 已提交
1507 1508

**Example**
H
HelloCrease 已提交
1509

1510 1511
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1512 1513 1514
  @Entry
  @Component
  struct QuadraticCurveTo {
E
ester.zhou 已提交
1515 1516
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
1517

Z
zengyawen 已提交
1518 1519 1520 1521 1522 1523 1524
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1525 1526 1527 1528
            this.context.beginPath()
            this.context.moveTo(20, 20)
            this.context.quadraticCurveTo(100, 100, 200, 20)
            this.context.stroke()
Z
zengyawen 已提交
1529
        })
Z
zengyawen 已提交
1530
      }
Z
zengyawen 已提交
1531 1532
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1533
    }
Z
zengyawen 已提交
1534 1535
  }
  ```
Z
zengyawen 已提交
1536

Z
zengyawen 已提交
1537
  ![en-us_image_0000001257058397](figures/en-us_image_0000001257058397.png)
Z
zengyawen 已提交
1538 1539


Z
zengyawen 已提交
1540
### arc
Z
zengyawen 已提交
1541

E
ester.zhou 已提交
1542
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void
Z
zengyawen 已提交
1543 1544 1545

Draws an arc on the canvas.

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

E
ester.zhou 已提交
1548 1549 1550 1551
**Parameters**

| Name              | Type     | Mandatory  | Default Value  | Description        |
| ---------------- | ------- | ---- | ----- | ---------- |
E
ester.zhou 已提交
1552 1553
| 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.|
E
ester.zhou 已提交
1554 1555 1556 1557
| 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.  |
| counterclockwise | boolean | No   | false | Whether to draw the arc counterclockwise.|
Z
zengyawen 已提交
1558

E
ester.zhou 已提交
1559
**Example**
H
HelloCrease 已提交
1560

E
ester.zhou 已提交
1561 1562
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1563 1564 1565
  @Entry
  @Component
  struct Arc {
E
ester.zhou 已提交
1566 1567
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
1568

Z
zengyawen 已提交
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.beginPath()
            this.context.arc(100, 75, 50, 0, 6.28)
            this.context.stroke()
          })
Z
zengyawen 已提交
1580
      }
Z
zengyawen 已提交
1581 1582
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1583
    }
Z
zengyawen 已提交
1584 1585
  }
  ```
Z
zengyawen 已提交
1586

Z
zengyawen 已提交
1587
  ![en-us_image_0000001212378404](figures/en-us_image_0000001212378404.png)
Z
zengyawen 已提交
1588 1589


Z
zengyawen 已提交
1590
### arcTo
Z
zengyawen 已提交
1591

Z
zengyawen 已提交
1592
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void
Z
zengyawen 已提交
1593 1594 1595

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

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

E
ester.zhou 已提交
1598
**Parameters**
Z
zengyawen 已提交
1599

E
ester.zhou 已提交
1600 1601
| Name    | Type    | Mandatory  | Default Value | Description             |
| ------ | ------ | ---- | ---- | --------------- |
E
ester.zhou 已提交
1602 1603 1604 1605
| 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.|
E
ester.zhou 已提交
1606 1607 1608
| radius | number | Yes   | 0    | Radius of the arc.       |

**Example**
H
HelloCrease 已提交
1609

1610 1611
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1612 1613 1614
  @Entry
  @Component
  struct ArcTo {
E
ester.zhou 已提交
1615 1616
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
1617

Z
zengyawen 已提交
1618 1619 1620 1621 1622 1623 1624
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1625 1626 1627
            this.context.moveTo(100, 20)
            this.context.arcTo(150, 20, 150, 70, 50)
            this.context.stroke()
Z
zengyawen 已提交
1628
          })
Z
zengyawen 已提交
1629
      }
Z
zengyawen 已提交
1630 1631
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1632
    }
Z
zengyawen 已提交
1633 1634
  }
  ```
Z
zengyawen 已提交
1635

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


Z
zengyawen 已提交
1639
### ellipse
Z
zengyawen 已提交
1640

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

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

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

E
ester.zhou 已提交
1647
**Parameters**
Z
zengyawen 已提交
1648

E
ester.zhou 已提交
1649 1650
| Name              | Type     | Mandatory  | Default Value  | Description                                      |
| ---------------- | ------- | ---- | ----- | ---------------------------------------- |
E
ester.zhou 已提交
1651 1652
| x                | number  | Yes   | 0     | X coordinate of the ellipse center.                              |
| y                | number  | Yes   | 0     | Y coordinate of the ellipse center.                              |
E
ester.zhou 已提交
1653 1654 1655 1656 1657 1658
| radiusX          | number  | Yes   | 0     | Ellipse radius on the x-axis.                              |
| radiusY          | number  | Yes   | 0     | Ellipse radius on the y-axis.                              |
| rotation         | number  | Yes   | 0     | Rotation angle of the ellipse. The unit is radian.                          |
| startAngle       | number  | Yes   | 0     | Angle of the start point for drawing the ellipse. The unit is radian.                       |
| endAngle         | number  | Yes   | 0     | Angle of the end point for drawing the ellipse. The unit is radian.                       |
| counterclockwise | boolean | No   | false | Whether to draw the ellipse in the counterclockwise direction.<br>**true**: Draw the arc counterclockwise.<br>**false**: Draw the arc clockwise.|
Z
zengyawen 已提交
1659

E
ester.zhou 已提交
1660
**Example**
H
HelloCrease 已提交
1661

1662 1663
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1664 1665 1666
  @Entry
  @Component
  struct CanvasExample {
E
ester.zhou 已提交
1667 1668
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
1669

Z
zengyawen 已提交
1670 1671 1672 1673 1674 1675 1676 1677
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.beginPath()
E
esterzhou 已提交
1678
            this.context.ellipse(200, 200, 50, 100, Math.PI * 0.25, Math.PI * 0.5, Math.PI * 2)
Z
zengyawen 已提交
1679 1680
            this.context.stroke()
          })
Z
zengyawen 已提交
1681
      }
Z
zengyawen 已提交
1682 1683
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1684
    }
Z
zengyawen 已提交
1685 1686
  }
  ```
Z
zengyawen 已提交
1687

Z
zengyawen 已提交
1688
  ![en-us_image_0000001256978339](figures/en-us_image_0000001256978339.png)
Z
zengyawen 已提交
1689 1690


Z
zengyawen 已提交
1691
### rect
Z
zengyawen 已提交
1692

E
ester.zhou 已提交
1693 1694 1695
rect(x: number, y: number, w: number, h: number): void

Creates a rectangle on the canvas.
Z
zengyawen 已提交
1696

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

E
ester.zhou 已提交
1699
**Parameters**
Z
zengyawen 已提交
1700

E
ester.zhou 已提交
1701 1702
| Name  | Type    | Mandatory  | Default Value | Description           |
| ---- | ------ | ---- | ---- | ------------- |
E
ester.zhou 已提交
1703 1704
| 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.|
E
ester.zhou 已提交
1705 1706
| w    | number | Yes   | 0    | Width of the rectangle.     |
| h    | number | Yes   | 0    | Height of the rectangle.     |
Z
zengyawen 已提交
1707

E
ester.zhou 已提交
1708
**Example**
H
HelloCrease 已提交
1709

1710 1711
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1712 1713 1714
  @Entry
  @Component
  struct CanvasExample {
E
ester.zhou 已提交
1715 1716
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
1717

Z
zengyawen 已提交
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.rect(20, 20, 100, 100) // Create a 100*100 rectangle at (20, 20)
            this.context.stroke()
          })
Z
zengyawen 已提交
1728
      }
Z
zengyawen 已提交
1729 1730
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1731
    }
Z
zengyawen 已提交
1732 1733
  }
  ```
Z
zengyawen 已提交
1734

Z
zengyawen 已提交
1735
  ![en-us_image_0000001256978341](figures/en-us_image_0000001256978341.png)
Z
zengyawen 已提交
1736 1737


Z
zengyawen 已提交
1738
### fill
Z
zengyawen 已提交
1739

E
ester.zhou 已提交
1740 1741 1742 1743
fill(fillRule?: CanvasFillRule): void

Fills the area inside a closed path on the canvas.

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

E
ester.zhou 已提交
1746
**Parameters**
Z
zengyawen 已提交
1747

E
ester.zhou 已提交
1748 1749
| Name      | Type            | Mandatory  | Default Value      | Description                                      |
| -------- | -------------- | ---- | --------- | ---------------------------------------- |
E
esterzhou 已提交
1750
| 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"**.|
Z
zengyawen 已提交
1751

E
ester.zhou 已提交
1752 1753

**Example**  
H
HelloCrease 已提交
1754

1755 1756
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1757 1758 1759
  @Entry
  @Component
  struct Fill {
E
ester.zhou 已提交
1760 1761
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
1762

Z
zengyawen 已提交
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.rect(20, 20, 100, 100) // Create a 100*100 rectangle at (20, 20)
            this.context.fill()
          })
Z
zengyawen 已提交
1773
      }
Z
zengyawen 已提交
1774 1775
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1776
    }
Z
zengyawen 已提交
1777 1778
  }
  ```
Z
zengyawen 已提交
1779

Z
zengyawen 已提交
1780
  ![en-us_image_0000001256858389](figures/en-us_image_0000001256858389.png)
Z
zengyawen 已提交
1781 1782


E
ester.zhou 已提交
1783 1784 1785 1786
fill(path: Path2D, fillRule?: CanvasFillRule): void

Fills the area inside a closed path on the canvas.

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

E
ester.zhou 已提交
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
**Parameters**

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


**Example**  

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

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

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


Z
zengyawen 已提交
1836
### clip
Z
zengyawen 已提交
1837

E
ester.zhou 已提交
1838 1839 1840
clip(fillRule?: CanvasFillRule): void

Sets the current path to a clipping area.
Z
zengyawen 已提交
1841

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

E
ester.zhou 已提交
1844
**Parameters**
Z
zengyawen 已提交
1845

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

**Example**
H
HelloCrease 已提交
1851

1852 1853
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1854 1855 1856
  @Entry
  @Component
  struct Clip {
E
ester.zhou 已提交
1857 1858
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
1859

Z
zengyawen 已提交
1860 1861 1862 1863 1864 1865 1866
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
esterzhou 已提交
1867
            this.context.rect(0, 0, 100, 200)
Z
zengyawen 已提交
1868 1869 1870
            this.context.stroke()
            this.context.clip()
            this.context.fillStyle = "rgb(255,0,0)"
E
esterzhou 已提交
1871
            this.context.fillRect(0, 0, 200, 200)
Z
zengyawen 已提交
1872
          })
Z
zengyawen 已提交
1873
      }
Z
zengyawen 已提交
1874 1875
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1876
    }
Z
zengyawen 已提交
1877 1878
  }
  ```
Z
zengyawen 已提交
1879

Z
zengyawen 已提交
1880
  ![en-us_image_0000001211898488](figures/en-us_image_0000001211898488.png)
Z
zengyawen 已提交
1881 1882


E
ester.zhou 已提交
1883 1884
clip(path: Path2D, fillRule?: CanvasFillRule): void

E
esterzhou 已提交
1885
Sets the current path to a clipping path.
E
ester.zhou 已提交
1886

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

E
ester.zhou 已提交
1889 1890 1891 1892 1893 1894 1895 1896
**Parameters**

| Name      | Type            | Mandatory  | Default Value      | Description                                      |
| -------- | -------------- | ---- | --------- | ---------------------------------------- |
| path     | Path2D         | Yes   | -         | A **Path2D** path to use as a clipping area.                             |
| 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
esterzhou 已提交
1897 1898 1899 1900
**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
  @Entry
  @Component
  struct Clip {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            let region = new Path2D()
            region.moveTo(30, 90)
            region.lineTo(110, 20)
            region.lineTo(240, 130)
            region.lineTo(60, 130)
            region.lineTo(190, 20)
            region.lineTo(270, 90)
            region.closePath()
            this.context.clip(region,"evenodd")
            this.context.fillStyle = "rgb(0,255,0)"
            this.context.fillRect(0, 0, this.context.width, this.context.height)
          })
      }
      .width('100%')
      .height('100%')
E
esterzhou 已提交
1928 1929 1930 1931 1932 1933
    }
  }
  ```

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

E
ester.zhou 已提交
1934 1935 1936 1937 1938

### resetTransform

resetTransform(): void

E
esterzhou 已提交
1939
Resets the current transform to the identity matrix. This API is a void API.
E
ester.zhou 已提交
1940

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

E
ester.zhou 已提交
1943

Z
zengyawen 已提交
1944
### rotate
Z
zengyawen 已提交
1945

E
ester.zhou 已提交
1946
rotate(angle: number): void
Z
zengyawen 已提交
1947 1948 1949

Rotates a canvas clockwise around its coordinate axes.

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

E
ester.zhou 已提交
1952
**Parameters**
Z
zengyawen 已提交
1953

E
ester.zhou 已提交
1954 1955 1956 1957 1958
| Name   | Type    | Mandatory  | Default Value | Description                                      |
| ----- | ------ | ---- | ---- | ---------------------------------------- |
| angle | number | Yes   | 0    | Clockwise rotation angle. You can use **Math.PI / 180** to convert the angle to a radian.|

**Example**
H
HelloCrease 已提交
1959

1960 1961
  ```ts
  // xxx.ets
Z
zengyawen 已提交
1962 1963 1964
  @Entry
  @Component
  struct Rotate {
E
ester.zhou 已提交
1965 1966
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
1967

Z
zengyawen 已提交
1968 1969 1970 1971 1972 1973 1974
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
1975
            this.context.rotate(45 * Math.PI / 180)
Z
zengyawen 已提交
1976 1977
            this.context.fillRect(70, 20, 50, 50)
          })
Z
zengyawen 已提交
1978
      }
Z
zengyawen 已提交
1979 1980
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1981
    }
Z
zengyawen 已提交
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
  }
  ```

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


### scale

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

E
ester.zhou 已提交
1992
Scales the canvas based on the given scale factors.
Z
zengyawen 已提交
1993

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

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

E
ester.zhou 已提交
1998 1999 2000 2001 2002 2003
| Name  | Type    | Mandatory  | Default Value | Description         |
| ---- | ------ | ---- | ---- | ----------- |
| x    | number | Yes   | 0    | Horizontal scale factor.|
| y    | number | Yes   | 0    | Vertical scale factor.|

**Example**
H
HelloCrease 已提交
2004

2005 2006
  ```ts
  // xxx.ets
Z
zengyawen 已提交
2007 2008 2009
  @Entry
  @Component
  struct Scale {
E
ester.zhou 已提交
2010 2011
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
2012

Z
zengyawen 已提交
2013 2014 2015 2016 2017 2018 2019
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
esterzhou 已提交
2020 2021
            this.context.lineWidth = 3
            this.context.strokeRect(30, 30, 50, 50)
Z
zengyawen 已提交
2022
            this.context.scale(2, 2) // Scale to 200%
E
esterzhou 已提交
2023
            this.context.strokeRect(30, 30, 50, 50)
Z
zengyawen 已提交
2024
          })
Z
zengyawen 已提交
2025
      }
Z
zengyawen 已提交
2026 2027
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2028
    }
Z
zengyawen 已提交
2029 2030
  }
  ```
Z
zengyawen 已提交
2031

Z
zengyawen 已提交
2032
  ![en-us_image_0000001257138347](figures/en-us_image_0000001257138347.png)
Z
zengyawen 已提交
2033 2034


Z
zengyawen 已提交
2035
### transform
Z
zengyawen 已提交
2036

E
ester.zhou 已提交
2037
transform(a: number, b: number, c: number, d: number, e: number, f: number): void
Z
zengyawen 已提交
2038 2039 2040

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

E
ester.zhou 已提交
2043
> **NOTE**
E
ester.zhou 已提交
2044
>
Z
zengyawen 已提交
2045
> 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 已提交
2046
>
Z
zengyawen 已提交
2047
> - x' = scaleX \* x + skewY \* y + translateX
H
HelloCrease 已提交
2048
>
Z
zengyawen 已提交
2049 2050
> - y' = skewX \* x + scaleY \* y + translateY

E
ester.zhou 已提交
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060
**Parameters**

| Name  | Type    | Mandatory  | Default Value | Description                  |
| ---- | ------ | ---- | ---- | -------------------- |
| a    | number | Yes   | 0    | X-axis scale.    |
| b    | number | Yes   | 0    | X-axis skew.     |
| c    | number | Yes   | 0    | Y-axis skew.     |
| d    | number | Yes   | 0    | Y-axis scale.    |
| e    | number | Yes   | 0    | X-axis translation.|
| f    | number | Yes   | 0    | Y-axis translation.|
Z
zengyawen 已提交
2061

E
ester.zhou 已提交
2062
**Example**
H
HelloCrease 已提交
2063

2064 2065
  ```ts
  // xxx.ets
Z
zengyawen 已提交
2066 2067 2068
  @Entry
  @Component
  struct Transform {
E
ester.zhou 已提交
2069 2070
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
2071

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

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


### setTransform

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

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

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

E
ester.zhou 已提交
2106
**Parameters**
Z
zengyawen 已提交
2107

E
ester.zhou 已提交
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
| Name  | Type    | Mandatory  | Default Value | Description                  |
| ---- | ------ | ---- | ---- | -------------------- |
| a    | number | Yes   | 0    | X-axis scale.    |
| b    | number | Yes   | 0    | X-axis skew.     |
| c    | number | Yes   | 0    | Y-axis skew.     |
| d    | number | Yes   | 0    | Y-axis scale.    |
| e    | number | Yes   | 0    | X-axis translation.|
| f    | number | Yes   | 0    | Y-axis translation.|

**Example**
H
HelloCrease 已提交
2118

2119 2120
  ```ts
  // xxx.ets
Z
zengyawen 已提交
2121 2122 2123
  @Entry
  @Component
  struct SetTransform {
E
ester.zhou 已提交
2124 2125
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
2126

Z
zengyawen 已提交
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.fillStyle = 'rgb(255,0,0)'
            this.context.fillRect(0, 0, 100, 100)
            this.context.setTransform(1,0.5, -0.5, 1, 10, 10)
            this.context.fillStyle = 'rgb(0,0,255)'
            this.context.fillRect(0, 0, 100, 100)
          })
Z
zengyawen 已提交
2140
      }
Z
zengyawen 已提交
2141 2142
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2143
    }
Z
zengyawen 已提交
2144 2145
  }
  ```
Z
zengyawen 已提交
2146

Z
zengyawen 已提交
2147
  ![en-us_image_0000001256858395](figures/en-us_image_0000001256858395.png)
Z
zengyawen 已提交
2148

E
ester.zhou 已提交
2149
### setTransform
Z
zengyawen 已提交
2150

E
ester.zhou 已提交
2151 2152
setTransform(transform?: Matrix2D): void

E
ester.zhou 已提交
2153
Resets the current transformation to the identity matrix, and then creates a new transformation matrix based on the specified **Matrix2D** object.
E
ester.zhou 已提交
2154

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

E
ester.zhou 已提交
2157 2158 2159 2160 2161
**Parameters**

| Name       | Type                                      | Mandatory  | Default Value | Description   |
| --------- | ---------------------------------------- | ---- | ---- | ----- |
| transform | [Matrix2D](ts-components-canvas-matrix2d.md#Matrix2D) | No   | null | Transformation matrix.|
E
ester.zhou 已提交
2162

E
ester.zhou 已提交
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
### getTransform

getTransform(): Matrix2D

Obtains the current transformation matrix being applied to the context.

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

**Return value**

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

**Example**

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

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

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

Z
zengyawen 已提交
2223
### translate
Z
zengyawen 已提交
2224

Z
zengyawen 已提交
2225
translate(x: number, y: number): void
Z
zengyawen 已提交
2226 2227 2228

Moves the origin of the coordinate system.

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

E
ester.zhou 已提交
2231 2232 2233 2234 2235 2236
**Parameters**

| Name  | Type    | Mandatory  | Default Value | Description      |
| ---- | ------ | ---- | ---- | -------- |
| x    | number | Yes   | 0    | X-axis translation.|
| y    | number | Yes   | 0    | Y-axis translation.|
Z
zengyawen 已提交
2237

E
ester.zhou 已提交
2238
**Example**
H
HelloCrease 已提交
2239

2240 2241
  ```ts
  // xxx.ets
Z
zengyawen 已提交
2242 2243 2244
  @Entry
  @Component
  struct Translate {
E
ester.zhou 已提交
2245 2246
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
2247

Z
zengyawen 已提交
2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.fillRect(10, 10, 50, 50)
            this.context.translate(70, 70)
            this.context.fillRect(10, 10, 50, 50)
          })
Z
zengyawen 已提交
2259
      }
Z
zengyawen 已提交
2260 2261
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2262
    }
Z
zengyawen 已提交
2263 2264
  }
  ```
Z
zengyawen 已提交
2265

Z
zengyawen 已提交
2266
  ![en-us_image_0000001257138357](figures/en-us_image_0000001257138357.png)
Z
zengyawen 已提交
2267 2268


Z
zengyawen 已提交
2269
### drawImage
Z
zengyawen 已提交
2270

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

E
ester.zhou 已提交
2273 2274 2275
drawImage(image: ImageBitmap | PixelMap, dx: number, dy: number, dw: number, dh: number): void

drawImage(image: ImageBitmap | PixelMap, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number):void
Z
zengyawen 已提交
2276

E
ester.zhou 已提交
2277
Draws an image on the canvas.
Z
zengyawen 已提交
2278

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

E
ester.zhou 已提交
2281
**Parameters**
Z
zengyawen 已提交
2282

E
ester.zhou 已提交
2283 2284 2285
| Name   | Type                                      | Mandatory  | Default Value | Description                                      |
| ----- | ---------------------------------------- | ---- | ---- | ---------------------------------------- |
| image | [ImageBitmap](ts-components-canvas-imagebitmap.md) or [PixelMap](../apis/js-apis-image.md#pixelmap7)| Yes   | null | Image resource. For details, see **ImageBitmap** or PixelMap.           |
E
ester.zhou 已提交
2286 2287
| 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.                    |
E
ester.zhou 已提交
2288 2289
| sw    | number                                   | No   | 0    | Target width to crop the source image.                          |
| sh    | number                                   | No   | 0    | Target height to crop the source image.                          |
E
ester.zhou 已提交
2290 2291
| 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.                    |
E
ester.zhou 已提交
2292 2293
| dw    | number                                   | No   | 0    | Width of the drawing area. If the width of the drawing area is different from that of the cropped image, the latter will be stretched or compressed to the former.|
| dh    | number                                   | No   | 0    | Height of the drawing area. If the height of the drawing area is different from that of the cropped image, the latter will be stretched or compressed to the former.|
Z
zengyawen 已提交
2294 2295


E
ester.zhou 已提交
2296
**Example**
H
HelloCrease 已提交
2297

2298 2299
  ```ts
  // xxx.ets
Z
zengyawen 已提交
2300 2301 2302
  @Entry
  @Component
  struct ImageExample {
E
ester.zhou 已提交
2303 2304 2305
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private img:ImageBitmap = new ImageBitmap("common/images/example.jpg")
E
ester.zhou 已提交
2306

Z
zengyawen 已提交
2307 2308 2309 2310 2311 2312 2313
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2314
            this.context.drawImage( this.img,0,0,500,500,0,0,400,200)
Z
zengyawen 已提交
2315
        })
Z
zengyawen 已提交
2316
      }
Z
zengyawen 已提交
2317 2318
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2319
    }
Z
zengyawen 已提交
2320 2321 2322 2323 2324 2325 2326 2327
  }
  ```

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


### createImageData

E
ester.zhou 已提交
2328
createImageData(sw: number, sh: number): ImageData
Z
zengyawen 已提交
2329

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

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

E
ester.zhou 已提交
2334 2335 2336 2337 2338 2339
**Parameters**

| Name  | Type    | Mandatory  | Default Value  | Description           |
| ---- | ------ | ---- | ---- | ------------- |
| sw   | number | Yes   | 0    | Width of the **ImageData** object.|
| sh   | number | Yes   | 0    | Height of the **ImageData** object.|
Z
zengyawen 已提交
2340 2341


E
ester.zhou 已提交
2342
createImageData(imageData: ImageData): ImageData
Z
zengyawen 已提交
2343

E
esterzhou 已提交
2344
Creates an **[ImageData](ts-components-canvas-imagedata.md)** object.
Z
zengyawen 已提交
2345

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

E
ester.zhou 已提交
2348 2349 2350 2351
**Parameters**

| Name       | Type                                      | Mandatory  | Default Value  | Description               |
| --------- | ---------------------------------------- | ---- | ---- | ----------------- |
E
esterzhou 已提交
2352
| imagedata | [ImageData](ts-components-canvas-imagedata.md) | Yes   | null | **ImageData** object with the same width and height copied from the original **ImageData** object.|
E
ester.zhou 已提交
2353 2354 2355 2356 2357

  **Return value**

| Type                                      | Description            |
| ---------------------------------------- | -------------- |
E
esterzhou 已提交
2358
| [ImageData](ts-components-canvas-imagedata.md) | New **ImageData** object.|
E
ester.zhou 已提交
2359

Z
zengyawen 已提交
2360

2361 2362 2363 2364 2365
### 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 已提交
2366 2367 2368 2369 2370

**Parameters**

| Name  | Type    | Mandatory  | Default Value | Description             |
| ---- | ------ | ---- | ---- | --------------- |
E
ester.zhou 已提交
2371 2372
| 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.|
E
ester.zhou 已提交
2373 2374 2375 2376 2377 2378 2379 2380
| 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 已提交
2381 2382 2383

### getImageData

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

E
esterzhou 已提交
2386
Obtains the **[ImageData](ts-components-canvas-imagedata.md)** object created with the pixels within the specified area on the canvas.
E
ester.zhou 已提交
2387

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

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

| Name  | Type    | Mandatory  | Default Value | Description             |
| ---- | ------ | ---- | ---- | --------------- |
E
ester.zhou 已提交
2394 2395
| 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.|
E
ester.zhou 已提交
2396 2397 2398 2399 2400 2401 2402
| sw   | number | Yes   | 0    | Width of the output area.    |
| sh   | number | Yes   | 0    | Height of the output area.    |

  **Return value**

| Type                                      | Description            |
| ---------------------------------------- | -------------- |
E
esterzhou 已提交
2403 2404 2405 2406 2407 2408 2409
| [ImageData](ts-components-canvas-imagedata.md) | New **ImageData** object.|


**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2410 2411 2412 2413 2414 2415
  @Entry
  @Component
  struct GetImageData {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private img:ImageBitmap = new ImageBitmap("/common/images/1234.png")
E
esterzhou 已提交
2416

E
ester.zhou 已提交
2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.drawImage(this.img,0,0,130,130)
            var imagedata = this.context.getImageData(50,50,130,130)
            this.context.putImageData(imagedata,150,150)
          })
      }
      .width('100%')
      .height('100%')
E
esterzhou 已提交
2431 2432 2433 2434 2435
    }
  }
  ```

  ![en-us_image_000000127777780](figures/en-us_image_000000127777780.png)
Z
zengyawen 已提交
2436 2437 2438 2439


### putImageData

E
ester.zhou 已提交
2440
putImageData(imageData: ImageData, dx: number | string, dy: number | string): void
E
ester.zhou 已提交
2441

E
ester.zhou 已提交
2442
putImageData(imageData: ImageData, dx: number | string, dy: number | string, dirtyX: number | string, dirtyY: number | string, dirtyWidth: number | string, dirtyHeight: number | string): void
E
ester.zhou 已提交
2443

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

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

E
ester.zhou 已提交
2448
**Parameters**
Z
zengyawen 已提交
2449

E
ester.zhou 已提交
2450 2451
| Name         | Type                                      | Mandatory  | Default Value         | Description                           |
| ----------- | ---------------------------------------- | ---- | ------------ | ----------------------------- |
E
esterzhou 已提交
2452
| imagedata   | [ImageData](ts-components-canvas-imagedata.md) | Yes   | null         | **ImageData** object with pixels to put onto the canvas.           |
E
ester.zhou 已提交
2453 2454 2455 2456 2457 2458
| dx          | number \| string<sup>10+</sup> | Yes   | 0            | X-axis offset of the rectangular area on the canvas.               |
| dy          | number \| string<sup>10+</sup> | Yes   | 0            | Y-axis offset of the rectangular area on the canvas.               |
| dirtyX      | number \| string<sup>10+</sup> | No   | 0            | X-axis offset of the upper left corner of the rectangular area relative to that of the source image.|
| dirtyY      | number \| string<sup>10+</sup> | No   | 0            | Y-axis offset of the upper left corner of the rectangular area relative to that of the source image.|
| dirtyWidth  | number \| string<sup>10+</sup> | No   | Width of the **ImageData** object| Width of the rectangular area to crop the source image.              |
| dirtyHeight | number \| string<sup>10+</sup> | No   | Height of the **ImageData** object| Height of the rectangular area to crop the source image.              |
Z
zengyawen 已提交
2459

E
ester.zhou 已提交
2460
**Example**
H
HelloCrease 已提交
2461

2462 2463
  ```ts
  // xxx.ets
Z
zengyawen 已提交
2464 2465 2466
  @Entry
  @Component
  struct PutImageData {
E
ester.zhou 已提交
2467 2468
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
2469

Z
zengyawen 已提交
2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var imageData = this.context.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.context.putImageData(imageData, 10, 10)
          })
Z
zengyawen 已提交
2486
      }
Z
zengyawen 已提交
2487 2488
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2489
    }
Z
zengyawen 已提交
2490 2491
  }
  ```
Z
zengyawen 已提交
2492

Z
zengyawen 已提交
2493
  ![en-us_image_0000001212378396](figures/en-us_image_0000001212378396.png)
Z
zengyawen 已提交
2494 2495


E
ester.zhou 已提交
2496 2497 2498 2499 2500 2501
### setLineDash

setLineDash(segments: number[]): void

Sets the dash line style.

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

E
ester.zhou 已提交
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
**Parameters**

| Name      | Type      | Description                 |
| -------- | -------- | ------------------- |
| segments | number[] | An array of numbers that specify distances to alternately draw a line and a gap.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct SetLineDash {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.context.arc(100, 75, 50, 0, 6.28)
            this.context.setLineDash([10,20])
E
esterzhou 已提交
2529
            this.context.stroke()
E
ester.zhou 已提交
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546
          })
      }
      .width('100%')
      .height('100%')
    }
  }
  ```

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


### getLineDash

getLineDash(): number[]

Obtains the dash line style.

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

E
ester.zhou 已提交
2549 2550 2551 2552 2553 2554
**Return value**

| Type      | Description                      |
| -------- | ------------------------ |
| number[] | An array of numbers that specify distances to alternately draw a line and a gap.|

E
esterzhou 已提交
2555

E
ester.zhou 已提交
2556 2557 2558 2559
**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2560 2561 2562 2563 2564 2565
  @Entry
  @Component
  struct CanvasGetLineDash {
    @State message: string = 'Hello World'
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
esterzhou 已提交
2566

E
ester.zhou 已提交
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589
    build() {
      Row() {
        Column() {
          Text(this.message)
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
            .onClick(()=>{
              console.error('before getlinedash clicked')
              let res = this.context.getLineDash()
              console.error(JSON.stringify(res))
            })
          Canvas(this.context)
            .width('100%')
            .height('100%')
            .backgroundColor('#ffff00')
            .onReady(() => {
              this.context.arc(100, 75, 50, 0, 6.28)
              this.context.setLineDash([10,20])
              this.context.stroke()
              let res = this.context.getLineDash()
            })
        }
        .width('100%')
E
ester.zhou 已提交
2590
      }
E
ester.zhou 已提交
2591
      .height('100%')
E
ester.zhou 已提交
2592 2593 2594
    }
  }
  ```
E
esterzhou 已提交
2595 2596
![en-us_image_000000127777778](figures/en-us_image_000000127777778.png) 

E
ester.zhou 已提交
2597 2598 2599 2600 2601 2602 2603

### transferFromImageBitmap

transferFromImageBitmap(bitmap: ImageBitmap): void

Displays the specified **ImageBitmap** object.

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

E
ester.zhou 已提交
2606 2607 2608 2609
**Parameters**

| Name    | Type                                      | Description                |
| ------ | ---------------------------------------- | ------------------ |
E
esterzhou 已提交
2610
| bitmap | [ImageBitmap](ts-components-canvas-imagebitmap.md) | **ImageBitmap** object to display.|
E
ester.zhou 已提交
2611 2612 2613 2614 2615 2616 2617

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
E
esterzhou 已提交
2618
  struct TransferFromImageBitmap {
E
ester.zhou 已提交
2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648
    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 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)
          })
      }
      .width('100%')
      .height('100%')
    }
  }
  ```
  ![en-us_image_000000127777773](figures/en-us_image_000000127777773.png)  

E
esterzhou 已提交
2649

E
ester.zhou 已提交
2650 2651 2652 2653 2654 2655
### toDataURL

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

Generates a URL containing image display information.

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

E
ester.zhou 已提交
2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
**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
  // xxx.ets
E
ester.zhou 已提交
2675 2676 2677 2678 2679
  @Entry
  @Component
  struct ToDataURL {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
ester.zhou 已提交
2680

E
ester.zhou 已提交
2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var dataURL = this.context.toDataURL()
          })
      }
      .width('100%')
      .height('100%')
E
ester.zhou 已提交
2693 2694 2695 2696 2697
    }
  }
  ```


Z
zengyawen 已提交
2698
### restore
Z
zengyawen 已提交
2699

Z
zengyawen 已提交
2700
restore(): void
Z
zengyawen 已提交
2701 2702 2703

Restores the saved drawing context.

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

E
ester.zhou 已提交
2706
**Example**
H
HelloCrease 已提交
2707

2708 2709
  ```ts
  // xxx.ets
Z
zengyawen 已提交
2710 2711 2712
  @Entry
  @Component
  struct CanvasExample {
E
ester.zhou 已提交
2713 2714
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
2715

Z
zengyawen 已提交
2716 2717 2718 2719 2720 2721 2722
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2723
            this.context.save() // save the default state
E
ester.zhou 已提交
2724
            this.context.fillStyle = "#00ff00"
E
ester.zhou 已提交
2725 2726 2727
            this.context.fillRect(20, 20, 100, 100)
            this.context.restore() // restore to the default state
            this.context.fillRect(150, 75, 100, 100)
Z
zengyawen 已提交
2728
          })
Z
zengyawen 已提交
2729
      }
Z
zengyawen 已提交
2730 2731
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2732
    }
Z
zengyawen 已提交
2733 2734
  }
  ```
E
esterzhou 已提交
2735
  ![en-us_image_000000127777781](figures/en-us_image_000000127777781.png)
Z
zengyawen 已提交
2736 2737


Z
zengyawen 已提交
2738
### save
Z
zengyawen 已提交
2739

Z
zengyawen 已提交
2740
save(): void
Z
zengyawen 已提交
2741

E
ester.zhou 已提交
2742
Saves all states of the canvas in the stack. This API is usually called when the drawing state needs to be saved.
Z
zengyawen 已提交
2743

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

E
ester.zhou 已提交
2746
**Example**
H
HelloCrease 已提交
2747

2748 2749
  ```ts
  // xxx.ets
Z
zengyawen 已提交
2750 2751 2752
  @Entry
  @Component
  struct CanvasExample {
E
ester.zhou 已提交
2753 2754
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
H
HelloCrease 已提交
2755

Z
zengyawen 已提交
2756 2757 2758 2759 2760 2761 2762
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
2763
            this.context.save() // save the default state
E
ester.zhou 已提交
2764
            this.context.fillStyle = "#00ff00"
E
ester.zhou 已提交
2765 2766 2767
            this.context.fillRect(20, 20, 100, 100)
            this.context.restore() // restore to the default state
            this.context.fillRect(150, 75, 100, 100)
E
esterzhou 已提交
2768
          })
Z
zengyawen 已提交
2769
      }
Z
zengyawen 已提交
2770 2771
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2772
    }
Z
zengyawen 已提交
2773 2774
  }
  ```
E
esterzhou 已提交
2775
  ![en-us_image_000000127777781](figures/en-us_image_000000127777781.png)
Z
zengyawen 已提交
2776 2777


Z
zengyawen 已提交
2778
### createLinearGradient
Z
zengyawen 已提交
2779

Z
zengyawen 已提交
2780
createLinearGradient(x0: number, y0: number, x1: number, y1: number): void
Z
zengyawen 已提交
2781 2782 2783

Creates a linear gradient.

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

E
ester.zhou 已提交
2786 2787 2788 2789
**Parameters**

| Name  | Type    | Mandatory  | Default Value | Description      |
| ---- | ------ | ---- | ---- | -------- |
E
ester.zhou 已提交
2790 2791 2792 2793
| 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 已提交
2794

E
ester.zhou 已提交
2795
**Example**
H
HelloCrease 已提交
2796

2797 2798
  ```ts
  // xxx.ets
Z
zengyawen 已提交
2799 2800 2801
  @Entry
  @Component
  struct CreateLinearGradient {
E
ester.zhou 已提交
2802 2803
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
2804
    
Z
zengyawen 已提交
2805 2806 2807 2808 2809 2810 2811 2812
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var grad = this.context.createLinearGradient(50,0, 300,100)
E
ester.zhou 已提交
2813 2814 2815
            grad.addColorStop(0.0, '#ff0000')
            grad.addColorStop(0.5, '#ffffff')
            grad.addColorStop(1.0, '#00ff00')
Z
zengyawen 已提交
2816
            this.context.fillStyle = grad
E
ester.zhou 已提交
2817
            this.context.fillRect(0, 0, 400, 400)
Z
zengyawen 已提交
2818
          })
Z
zengyawen 已提交
2819
      }
Z
zengyawen 已提交
2820 2821
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2822
    }
Z
zengyawen 已提交
2823 2824
  }
  ```
Z
zengyawen 已提交
2825

Z
zengyawen 已提交
2826
  ![en-us_image_0000001212378412](figures/en-us_image_0000001212378412.png)
Z
zengyawen 已提交
2827 2828


Z
zengyawen 已提交
2829
### createRadialGradient
Z
zengyawen 已提交
2830

Z
zengyawen 已提交
2831
createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number): void
Z
zengyawen 已提交
2832 2833 2834

Creates a linear gradient.

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

E
ester.zhou 已提交
2837 2838 2839 2840
**Parameters**

| Name  | Type    | Mandatory  | Default Value | Description               |
| ---- | ------ | ---- | ---- | ----------------- |
E
ester.zhou 已提交
2841 2842
| 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.        |
E
ester.zhou 已提交
2843
| r0   | number | Yes   | 0    | Radius of the start circle, which must be a non-negative finite number.|
E
ester.zhou 已提交
2844 2845
| 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.        |
E
ester.zhou 已提交
2846
| r1   | number | Yes   | 0    | Radius of the end circle, which must be a non-negative finite number.|
Z
zengyawen 已提交
2847

E
ester.zhou 已提交
2848
**Example**
H
HelloCrease 已提交
2849

2850 2851
  ```ts
  // xxx.ets
Z
zengyawen 已提交
2852 2853 2854
  @Entry
  @Component
  struct CreateRadialGradient {
E
ester.zhou 已提交
2855 2856
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Z
zengyawen 已提交
2857 2858 2859 2860 2861 2862 2863 2864 2865
    
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var grad = this.context.createRadialGradient(200,200,50, 200,200,200)
E
ester.zhou 已提交
2866 2867 2868
            grad.addColorStop(0.0, '#ff0000')
            grad.addColorStop(0.5, '#ffffff')
            grad.addColorStop(1.0, '#00ff00')
Z
zengyawen 已提交
2869
            this.context.fillStyle = grad
E
ester.zhou 已提交
2870
            this.context.fillRect(0, 0, 440, 440)
Z
zengyawen 已提交
2871
          })
Z
zengyawen 已提交
2872
      }
Z
zengyawen 已提交
2873 2874
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2875
    }
Z
zengyawen 已提交
2876 2877
  }
  ```
Z
zengyawen 已提交
2878

Z
zengyawen 已提交
2879
  ![en-us_image_0000001257058405](figures/en-us_image_0000001257058405.png)
Z
zengyawen 已提交
2880

E
ester.zhou 已提交
2881 2882 2883 2884 2885 2886 2887 2888
### createConicGradient<sup>10+</sup>

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

Creates a conic gradient.

**Parameters**

E
ester.zhou 已提交
2889 2890 2891
| 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.|
E
ester.zhou 已提交
2892 2893
| x          | number | Yes   | 0    | X coordinate of the center of the conic gradient, in vp.                  |
| y          | number | Yes   | 0    | Y coordinate of the center of the conic gradient, in vp.                  |
E
ester.zhou 已提交
2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926

**Example**

```ts
// xxx.ets
@Entry
@Component
struct CanvasExample {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(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.context.createConicGradient(0, 50, 80)
          grad.addColorStop(0.0, '#ff0000')
          grad.addColorStop(0.5, '#ffffff')
          grad.addColorStop(1.0, '#00ff00')
          this.context.fillStyle = grad
          this.context.fillRect(0, 30, 100, 100)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```

  ![en-us_image_0000001239032419](figures/en-us_image_0000001239032420.png)
E
ester.zhou 已提交
2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001


## CanvasDirection

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

| Name       | Description            |
| -------- | -------------- |
| inherit | The text direction is inherited from the **\<Canvas>** component.|
| ltr     | The text direction is from left to right.|
| rtl     | The text direction is from right to left.|

## CanvasFillRule

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

| Name       | Description            |
| -------- | -------------- |
| evenodd  | The inside part of a shape is determined based on whether the counting result is an odd number or not.|
| nonzero   | The inside part of a shape is determined based on whether the counting result is zero or not.|

## CanvasLineCap

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

| Name       | Description            |
| -------- | -------------- |
| butt   | The ends of the line are squared off, and the line does not extend beyond its two endpoints.              |
| round  | The line is extended at the endpoints by a half circle whose diameter is equal to the line width.           |
| square | The line is extended at the endpoints by a rectangle whose width is equal to half the line width and height equal to the line width.|

## CanvasLineJoin

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

| Name       | Description            |
| -------- | -------------- |
| bevel  | The intersection is a triangle. The rectangular corner of each line is independent.|
| 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**.|
| round   | The intersection is a sector, whose radius at the rounded corner is equal to the line width.|

## CanvasTextAlign

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

| Name       | Description            |
| -------- | -------------- |
| center  | The text is center-aligned.|
| start   | The text is aligned with the start bound.|
| end   | The text is aligned with the end bound.|
| left  | The text is left-aligned.|
| right   | The text is right-aligned.|

## CanvasTextBaseline

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

| Name       | Description            |
| -------- | -------------- |
| alphabetic  | The text baseline is the normal alphabetic baseline.|
| 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.|
| hanging  | The text baseline is a hanging baseline over the text.|
| ideographic   | The text baseline is the ideographic baseline. If a character exceeds the alphabetic baseline, the ideographic baseline is located at the bottom of the excessive character.|
| middle   | The text baseline is in the middle of the text bounding box.|
| top   | The text baseline is on the top of the text bounding box.|

## ImageSmoothingQuality

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

| Name       | Description            |
| -------- | -------------- |
| low      | Low quality.|
| medium   | Medium quality.|
| high     | High quality.|