ts-offscreencanvasrenderingcontext2d.md 75.7 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4
# OffscreenCanvasRenderingConxt2D对象

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 从 API Version 8 开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
5 6 7 8


使用OffscreenCanvasRenderingContext2D在Canvas上进行离屏绘制,绘制对象可以是矩形、文本、图片等。离屏绘制是指将需要绘制的内容先绘制在缓存区,然后将其转换成图片,一次性绘制绘制到canvas上,加快了绘制速度。

Z
zengyawen 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

## 接口

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

- 参数
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 |
  | -------- | -------- | -------- | -------- | -------- |
  | width | number | 是 | - | 离屏画布的宽度 |
  | height | number | 是 | - | 离屏画布的高度 |
  | setting | [RenderingContextSettings](ts-canvasrenderingcontext2d#renderingcontextsettings) | 是 | - | 见RenderingContextSettings接口描述。 |


## 属性

| 名称 | 类型 | 默认值 | 描述 |
| -------- | -------- | -------- | -------- |
| [fillStyle](#fillstyle) | &lt;color&gt;&nbsp;\|&nbsp;[CanvasGradient](ts-components-canvas-canvasgradient.md)&nbsp;\|&nbsp;[CanvasPattern](#canvaspattern) | - | 指定绘制的填充色。<br/>-&nbsp;类型为&lt;color&gt;时,表示设置填充区域的颜色。<br/>-&nbsp;类型为CanvasGradient时,表示渐变对象,使用&nbsp;[createLinearGradient](#createlineargradient)方法创建。<br/>-&nbsp;类型为CanvasPattern时,使用[createPattern](#createpattern)方法创建。 |
| [lineWidth](#linewidth) | number | - | 设置绘制线条的宽度。 |
| [strokeStyle](#strokestyle) | &lt;color&gt;&nbsp;\|&nbsp;[CanvasGradient](ts-components-canvas-canvasgradient.md)&nbsp;\|&nbsp;[CanvasPattern](#canvaspattern) | - | 设置描边的颜色。<br/>-&nbsp;类型为&lt;color&gt;时,表示设置描边使用的颜色。<br/>-&nbsp;类型为CanvasGradient时,表示渐变对象,使用[createLinearGradient](#createlineargradient)方法创建。<br/>-&nbsp;类型为CanvasPattern时,使用[createPattern](#createpattern)方法创建。 |
| [lineCap](#linecap) | string | 'butt' | 指定线端点的样式,可选值为:<br/>-&nbsp;'butt':线端点以方形结束。<br/>-&nbsp;'round':线端点以圆形结束。<br/>-&nbsp;'square':线端点以方形结束,该样式下会增加一个长度和线段厚度相同,宽度是线段厚度一半的矩形。 |
| [lineJoin](#linejoin) | string | 'miter' | 指定线段间相交的交点样式,可选值为:<br/>-&nbsp;'round':在线段相连处绘制一个扇形,扇形的圆角半径是线段的宽度。<br/>-&nbsp;'bevel':在线段相连处使用三角形为底填充,&nbsp;每个部分矩形拐角独立。<br/>-&nbsp;'miter':在相连部分的外边缘处进行延伸,使其相交于一点,形成一个菱形区域,该属性可以通过设置miterLimit属性展现效果。 |
| [miterLimit](#miterlimit) | number | 10 | 设置斜接面限制值,该值指定了线条相交处内角和外角的距离。 |
| [font](#font) | string | 'normal&nbsp;normal&nbsp;14px&nbsp;sans-serif' | 设置文本绘制中的字体样式。<br/>语法:ctx.font='font-size&nbsp;font-family'<br/>-&nbsp;font-size(可选),指定字号和行高,单位只支持px。<br/>-&nbsp;font-family(可选),指定字体系列。<br/>语法:ctx.font='font-style&nbsp;font-weight&nbsp;font-size&nbsp;font-family'<br/>-&nbsp;font-style(可选),用于指定字体样式,支持如下几种样式:'normal',&nbsp;'italic'。<br/>-&nbsp;font-weight(可选),用于指定字体的粗细,支持如下几种类型:'normal',&nbsp;'bold',&nbsp;'bolder',&nbsp;'lighter',&nbsp;100,&nbsp;200,&nbsp;300,&nbsp;400,&nbsp;500,&nbsp;600,&nbsp;700,&nbsp;800,&nbsp;900。<br/>-&nbsp;font-size(可选),指定字号和行高,单位只支持px。<br/>-&nbsp;font-family(可选),指定字体系列,支持如下几种类型:'sans-serif',&nbsp;'serif',&nbsp;'monospace'。 |
| [textAlign](#textalign) | string | 'left' | 设置文本绘制中的文本对齐方式,可选值为:<br/>-&nbsp;'left':文本左对齐。<br/>-&nbsp;'right':文本右对齐。<br/>-&nbsp;'center':文本居中对齐。<br/>-&nbsp;'start':文本对齐界线开始的地方。<br/>-&nbsp;'end':文本对齐界线结束的地方。<br/>>&nbsp;![icon-note.gif](public_sys-resources/icon-note.gif)&nbsp;**说明:**<br/>>&nbsp;ltr布局模式下start和left一致,rtl布局模式下start和right一致·。 |
| [textBaseline](#textbaseline) | string | 'alphabetic' | 设置文本绘制中的水平对齐方式,可选值为:<br/>-&nbsp;'alphabetic':文本基线是标准的字母基线。<br/>-&nbsp;'top':文本基线在文本块的顶部。<br/>-&nbsp;'hanging':文本基线是悬挂基线。<br/>-&nbsp;'middle':文本基线在文本块的中间。<br/>-&nbsp;'ideographic':文字基线是表意字基线;如果字符本身超出了alphabetic基线,那么ideograhpic基线位置在字符本身的底部。<br/>-&nbsp;'bottom':文本基线在文本块的底部。&nbsp;与ideographic基线的区别在于ideographic&nbsp;基线不需要考虑下行字母。 |
| [globalAlpha](#globalalpha) | number | - | 设置透明度,0.0为完全透明,1.0为完全不透明。 |
| [lineDashOffset](#linedashoffset) | number | 0.0 | 设置画布的虚线偏移量,精度为float。 |
| [globalCompositeOperation](#globalcompositeoperation) | string | 'source-over' | 设置合成操作的方式。类型字段可选值有'source-over','source-atop','source-in','source-out','destination-over','destination-atop','destination-in','destination-out','lighter','copy','xor'。 |
| [shadowBlur](#shadowblur) | number | 0.0 | 设置绘制阴影时的模糊级别,值越大越模糊,精度为float。 |
| [shadowColor](#shadowcolor) | &lt;color&gt; | - | 设置绘制阴影时的阴影颜色。 |
| [shadowOffsetX](#shadowoffsetx) | number | - | 设置绘制阴影时和原有对象的水平偏移值。 |
| [shadowOffsetY](#shadowoffsety) | number | - | 设置绘制阴影时和原有对象的垂直偏移值。 |
| [imageSmoothingEnabled](#imagesmoothingenabled)       | boolean | true | 用于设置绘制图片时是否进行图像平滑度调整,true为启用,false为不启用。 |
| imageSmoothingQuality | string | 'low' | 用于设置图像平滑度,支持如下三种类型:'low',&nbsp;'medium',&nbsp;'high'。 |

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> &lt;color&gt;类型格式为 'rgb(255, 255, 255)','rgba(255, 255, 255, 1.0)','\#FFFFFF'。


### fillStyle
Z
zengyawen 已提交
50 51 52 53 54

```
@Entry
@Component
struct FillStyleExample {
Y
yaoyuchi 已提交
55
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)

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

Z
zengyawen 已提交
78
![zh-cn_image_0000001193872516](figures/zh-cn_image_0000001193872516.png)
Z
zengyawen 已提交
79

Z
zengyawen 已提交
80 81

### lineWidth
Z
zengyawen 已提交
82 83 84 85 86

```
@Entry
@Component
struct LineWidthExample {
Y
yaoyuchi 已提交
87
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)

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

Z
zengyawen 已提交
110 111
![zh-cn_image_0000001238832403](figures/zh-cn_image_0000001238832403.png)

Z
zengyawen 已提交
112

Z
zengyawen 已提交
113
### strokeStyle
Z
zengyawen 已提交
114 115 116 117 118

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

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


Z
zengyawen 已提交
144 145 146 147
![zh-cn_image_0000001238712437](figures/zh-cn_image_0000001238712437.png)


### lineCap
Z
zengyawen 已提交
148 149 150 151 152

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

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

Z
zengyawen 已提交
180 181
![zh-cn_image_0000001194192454](figures/zh-cn_image_0000001194192454.png)

Z
zengyawen 已提交
182

Z
zengyawen 已提交
183
### lineJoin
Z
zengyawen 已提交
184 185 186 187 188

```
@Entry
@Component
struct LineJoinExample {
Y
yaoyuchi 已提交
189
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)

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

Z
zengyawen 已提交
217 218
![zh-cn_image_0000001194352450](figures/zh-cn_image_0000001194352450.png)

Z
zengyawen 已提交
219

Z
zengyawen 已提交
220
### miterLimit
Z
zengyawen 已提交
221 222 223 224 225

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

Z
zengyawen 已提交
254
![zh-cn_image_0000001238952397](figures/zh-cn_image_0000001238952397.png)
Z
zengyawen 已提交
255

Z
zengyawen 已提交
256 257

### font
Z
zengyawen 已提交
258 259 260 261 262

```
@Entry
@Component
struct Font {
Y
yaoyuchi 已提交
263
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.font = '30px sans-serif'
          this.offContext.fillText("Hello World", 20, 60)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
286 287
![zh-cn_image_0000001194032476](figures/zh-cn_image_0000001194032476.png)

Z
zengyawen 已提交
288

Z
zengyawen 已提交
289
### textAlign
Z
zengyawen 已提交
290 291 292 293

```
@Entry
@Component
Y
yaoyuchi 已提交
294 295
struct CanvasExample {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
        this.offContext.strokeStyle = '#0000ff'
        this.offContext.moveTo(140, 10)
        this.offContext.lineTo(140, 160)
        this.offContext.stroke()

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

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

Z
zengyawen 已提交
333 334
![zh-cn_image_0000001239032423](figures/zh-cn_image_0000001239032423.png)

Z
zengyawen 已提交
335

Z
zengyawen 已提交
336
### textBaseline
Z
zengyawen 已提交
337 338 339 340 341

```
@Entry
@Component
struct TextBaseline {
Y
yaoyuchi 已提交
342
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
343 344 345 346 347 348 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
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.strokeStyle = '#0000ff'
          this.offContext.moveTo(0, 120)
          this.offContext.lineTo(400, 120)
          this.offContext.stroke()

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

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

Z
zengyawen 已提交
380
![zh-cn_image_0000001193872518](figures/zh-cn_image_0000001193872518.png)
Z
zengyawen 已提交
381

Z
zengyawen 已提交
382 383

### globalAlpha
Z
zengyawen 已提交
384 385 386 387 388

```
@Entry
@Component
struct GlobalAlpha {
Y
yaoyuchi 已提交
389
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.fillStyle = 'rgb(255,0,0)'
          this.offContext.fillRect(0, 0, 50, 50)
          this.offContext.globalAlpha = 0.4
          this.offContext.fillStyle = 'rgb(0,0,255)'
          this.offContext.fillRect(50, 50, 50, 50)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
415 416
![zh-cn_image_0000001238832405](figures/zh-cn_image_0000001238832405.png)

Z
zengyawen 已提交
417

Z
zengyawen 已提交
418
### lineDashOffset
Z
zengyawen 已提交
419 420 421 422 423

```
@Entry
@Component
struct LineDashOffset {
Y
yaoyuchi 已提交
424
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.arc(100, 75, 50, 0, 6.28)
          this.offContext.setLineDash([10,20])
          this.offContext.stroke();
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
![zh-cn_image_0000001238712439](figures/zh-cn_image_0000001238712439.png)


### globalCompositeOperation

| 名称 | 描述 | 
| -------- | -------- |
| source-over | 在现有绘制内容上显示新绘制内容,属于默认值。 | 
| source-atop | 在现有绘制内容顶部显示新绘制内容。 | 
| source-in | 在现有绘制内容中显示新绘制内容。 | 
| source-out | 在现有绘制内容之外显示新绘制内容。 | 
| destination-over | 在新绘制内容上方显示现有绘制内容。 | 
| destination-atop | 在新绘制内容顶部显示现有绘制内容。 | 
| destination-in | 在新绘制内容中显示现有绘制内容。 | 
| destination-out | 在新绘制内容外显示现有绘制内容。 | 
| lighter | 显示新绘制内容和现有绘制内容。 | 
| copy | 显示新绘制内容而忽略现有绘制内容。 | 
| xor | 使用异或操作对新绘制内容与现有绘制内容进行融合。 | 
Z
zengyawen 已提交
466 467 468 469 470

```
@Entry
@Component
struct GlobalCompositeOperation {
Y
yaoyuchi 已提交
471
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
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 497 498 499 500 501
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.fillStyle = 'rgb(255,0,0)'
          this.offContext.fillRect(20, 20, 50, 50)
          this.offContext.globalCompositeOperation = 'source-over'
          this.offContext.fillStyle = 'rgb(0,0,255)'
          this.offContext.fillRect(50, 50, 50, 50)
          this.offContext.fillStyle = 'rgb(255,0,0)'
          this.offContext.fillRect(120, 20, 50, 50)
          this.offContext.globalCompositeOperation = 'destination-over'
          this.offContext.fillStyle = 'rgb(0,0,255)'
          this.offContext.fillRect(150, 50, 50, 50)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
502 503
![zh-cn_image_0000001194192456](figures/zh-cn_image_0000001194192456.png)

Z
zengyawen 已提交
504

Z
zengyawen 已提交
505
### shadowBlur
Z
zengyawen 已提交
506 507 508 509 510

```
@Entry
@Component
struct ShadowBlur {
Y
yaoyuchi 已提交
511
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.shadowBlur = 30
          this.offContext.shadowColor = 'rgb(0,0,0)'
          this.offContext.fillStyle = 'rgb(255,0,0)'
          this.offContext.fillRect(20, 20, 100, 80)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
536
![zh-cn_image_0000001194352452](figures/zh-cn_image_0000001194352452.png)
Z
zengyawen 已提交
537

Z
zengyawen 已提交
538 539

### shadowColor
Z
zengyawen 已提交
540 541 542 543 544

```
@Entry
@Component
struct ShadowColor {
Y
yaoyuchi 已提交
545
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.shadowBlur = 30
          this.offContext.shadowColor = 'rgb(0,0,255)'
          this.offContext.fillStyle = 'rgb(255,0,0)'
          this.offContext.fillRect(30, 30, 100, 100)
          var image = this.offContext.transferToImageBitmap
()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
571 572
![zh-cn_image_0000001238952399](figures/zh-cn_image_0000001238952399.png)

Z
zengyawen 已提交
573

Z
zengyawen 已提交
574
### shadowOffsetX
Z
zengyawen 已提交
575 576 577 578 579

```
@Entry
@Component
struct ShadowOffsetX {
Y
yaoyuchi 已提交
580
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.shadowBlur = 10
          this.offContext.shadowOffsetX = 20
          this.offContext.shadowColor = 'rgb(0,0,0)'
          this.offContext.fillStyle = 'rgb(255,0,0)'
          this.offContext.fillRect(20, 20, 100, 80)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
606 607
![zh-cn_image_0000001194032478](figures/zh-cn_image_0000001194032478.png)

Z
zengyawen 已提交
608

Z
zengyawen 已提交
609
### shadowOffsetY
Z
zengyawen 已提交
610 611 612 613 614

```
@Entry
@Component
struct ShadowOffsetY {
Y
yaoyuchi 已提交
615
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)

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

Z
zengyawen 已提交
641
![zh-cn_image_0000001239032425](figures/zh-cn_image_0000001239032425.png)
Z
zengyawen 已提交
642

Z
zengyawen 已提交
643 644

### imageSmoothingEnabled
Z
zengyawen 已提交
645 646 647 648 649

```
@Entry
@Component
struct ImageSmoothingEnabled {
Y
yaoyuchi 已提交
650
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private img:ImageBitmap = new ImageBitmap("common/images/icon.jpg")
  private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
  
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.offContext.imageSmoothingEnabled = false
          this.offContext.drawImage( this.img,0,0,400,200)
          var image = this.offContext.transferToImageBitmap()
          this.context.transferFromImageBitmap(image)
      })
    }
    .width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
674 675 676 677
![zh-cn_image_0000001193872520](figures/zh-cn_image_0000001193872520.png)


## 方法
Z
zengyawen 已提交
678 679


Z
zengyawen 已提交
680
### fillRect
Z
zengyawen 已提交
681

Z
zengyawen 已提交
682
fillRect(x: number, y: number, w: number, h: number): void
Z
zengyawen 已提交
683 684 685

填充一个矩形。

Z
zengyawen 已提交
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
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 说明 | 
  | -------- | -------- | -------- | -------- | -------- |
  | x | number | 是 | 0 | 指定矩形左上角点的x坐标。 | 
  | y | number | 是 | 0 | 指定矩形左上角点的y坐标。 | 
  | width | number | 是 | 0 | 指定矩形的宽度。 | 
  | height | number | 是 | 0 | 指定矩形的高度。 | 

- 示例
  ```
  @Entry
  @Component
  struct FillRect {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.fillRect(0,30,100,100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
         })
        }
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
717
    }
Z
zengyawen 已提交
718 719
  }
  ```
Z
zengyawen 已提交
720

Z
zengyawen 已提交
721
  ![zh-cn_image_0000001238832407](figures/zh-cn_image_0000001238832407.png)
Z
zengyawen 已提交
722 723


Z
zengyawen 已提交
724
### strokeRect
Z
zengyawen 已提交
725

Z
zengyawen 已提交
726
strokeRect(x: number, y: number, w: number, h: number): void
Z
zengyawen 已提交
727 728 729

绘制具有边框的矩形,矩形内部不填充。

Z
zengyawen 已提交
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
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 说明 | 
  | -------- | -------- | -------- | -------- | -------- |
  | x | number | 是 | 0 | 指定矩形的左上角x坐标。 | 
  | y | number | 是 | 0 | 指定矩形的左上角y坐标。 | 
  | width | number | 是 | 0 | 指定矩形的宽度。 | 
  | height | number | 是 | 0 | 指定矩形的高度。 | 

- 示例
  ```
  @Entry
  @Component
  struct StrokeRect {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.strokeRect(30, 30, 200, 150)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
757
      }
Z
zengyawen 已提交
758 759
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
760
    }
Z
zengyawen 已提交
761 762
  }
  ```
Z
zengyawen 已提交
763

Z
zengyawen 已提交
764
  ![zh-cn_image_0000001238712441](figures/zh-cn_image_0000001238712441.png)
Z
zengyawen 已提交
765 766


Z
zengyawen 已提交
767
### clearRect
Z
zengyawen 已提交
768

Z
zengyawen 已提交
769
clearRect(x: number, y: number, w: number, h: number): void
Z
zengyawen 已提交
770 771 772

删除指定区域内的绘制内容。

Z
zengyawen 已提交
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
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | x | number | 是 | 0 | 指定矩形上的左上角x坐标。 | 
  | y | number | 是 | 0 | 指定矩形上的左上角y坐标。 | 
  | width | number | 是 | 0 | 指定矩形的宽度。 | 
  | height | number | 是 | 0 | 指定矩形的高度。 | 

- 示例
  ```
  @Entry
  @Component
  struct ClearRect {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.fillStyle = 'rgb(0,0,255)'
            this.offContext.fillRect(0,0,500,500)
            this.offContext.clearRect(20,20,150,100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
802
      }
Z
zengyawen 已提交
803 804
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
805
    }
Z
zengyawen 已提交
806 807
  }
  ```
Z
zengyawen 已提交
808

Z
zengyawen 已提交
809
  ![zh-cn_image_0000001194192458](figures/zh-cn_image_0000001194192458.png)
Z
zengyawen 已提交
810 811


Z
zengyawen 已提交
812
### fillText
Z
zengyawen 已提交
813

Z
zengyawen 已提交
814
fillText(text: string, x: number, y: number): void
Z
zengyawen 已提交
815 816 817

绘制填充类文本。

Z
zengyawen 已提交
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
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 说明 | 
  | -------- | -------- | -------- | -------- | -------- |
  | text | string | 是 | “” | 需要绘制的文本内容。 | 
  | x | number | 是 | 0 | 需要绘制的文本的左下角x坐标。 | 
  | y | number | 是 | 0 | 需要绘制的文本的左下角y坐标。 | 

- 示例
  ```
  @Entry
  @Component
  struct FillText {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.font = '30px sans-serif'
            this.offContext.fillText("Hello World!", 20, 100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
845
      }
Z
zengyawen 已提交
846 847
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
848
    }
Z
zengyawen 已提交
849 850
  }
  ```
Z
zengyawen 已提交
851

Z
zengyawen 已提交
852
  ![zh-cn_image_0000001194352454](figures/zh-cn_image_0000001194352454.png)
Z
zengyawen 已提交
853 854


Z
zengyawen 已提交
855
### strokeText
Z
zengyawen 已提交
856

Z
zengyawen 已提交
857
strokeText(text: string, x: number, y: number): void
Z
zengyawen 已提交
858 859 860

绘制描边类文本。

Z
zengyawen 已提交
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | text | string | 是 | “” | 需要绘制的文本内容。 | 
  | x | number | 是 | 0 | 需要绘制的文本的左下角x坐标。 | 
  | y | number | 是 | 0 | 需要绘制的文本的左下角y坐标。 | 

- 示例
  ```
  @Entry
  @Component
  struct StrokeText {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.font = '55px sans-serif'
            this.offContext.strokeText("Hello World!", 20, 60)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
888
      }
Z
zengyawen 已提交
889 890
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
891
    }
Z
zengyawen 已提交
892 893
  }
  ```
Z
zengyawen 已提交
894

Z
zengyawen 已提交
895
  ![zh-cn_image_0000001238952401](figures/zh-cn_image_0000001238952401.png)
Z
zengyawen 已提交
896 897


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

Z
zengyawen 已提交
900
measureText(text: string): TextMetrics
Z
zengyawen 已提交
901 902 903

该方法返回一个文本测算的对象,通过该对象可以获取指定文本的宽度值。

Z
zengyawen 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 说明 | 
  | -------- | -------- | -------- | -------- | -------- |
  | text | string | 是 | "" | 需要进行测量的文本。 | 

- 返回值
  | 类型 | 说明 |
  | -------- | -------- |
  | TextMetrics | 文本的尺寸信息 |

- TextMetrics类型描述
  | 属性 | 类型 | 描述 | 
  | -------- | -------- | -------- |
  | width | number | 字符串的宽度。 | 

- 示例
  ```
  @Entry
  @Component
  struct MeasureText {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.font = '50px sans-serif'
            this.offContext.fillText("Hello World!", 20, 100)
            this.offContext.fillText("width:" + this.context.measureText("Hello World!").width, 20, 200)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
940
      }
Z
zengyawen 已提交
941 942
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
943
    }
Z
zengyawen 已提交
944 945
  }
  ```
Z
zengyawen 已提交
946

Z
zengyawen 已提交
947
  ![zh-cn_image_0000001194032480](figures/zh-cn_image_0000001194032480.png)
Z
zengyawen 已提交
948 949


Z
zengyawen 已提交
950
### stroke
Z
zengyawen 已提交
951

Z
zengyawen 已提交
952
stroke(path?: Path2D): void
Z
zengyawen 已提交
953 954 955

进行边框绘制操作。

Z
zengyawen 已提交
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 |
  | -------- | -------- | -------- | -------- | -------- |
  | path | [Path2D](ts-components-canvas-path2d.md) | 否 | null | 需要绘制的Path2D。 |

- 示例
  ```
  @Entry
  @Component
  struct Stroke {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.moveTo(25, 25)
            this.offContext.lineTo(25, 105)
            this.offContext.strokeStyle = 'rgb(0,0,255)'
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
983
      }
Z
zengyawen 已提交
984 985
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
986
    }
Z
zengyawen 已提交
987 988
  }
  ```
Z
zengyawen 已提交
989

Z
zengyawen 已提交
990
  ![zh-cn_image_0000001239032427](figures/zh-cn_image_0000001239032427.png)
Z
zengyawen 已提交
991 992


Z
zengyawen 已提交
993
### beginPath
Z
zengyawen 已提交
994

Z
zengyawen 已提交
995
beginPath(): void
Z
zengyawen 已提交
996 997 998

创建一个新的绘制路径。

Z
zengyawen 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
- 示例
  ```
  @Entry
  @Component
  struct BeginPath {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.lineWidth = 6
            this.offContext.strokeStyle = '#0000ff'
            this.offContext.moveTo(15, 80)
            this.offContext.lineTo(280, 160)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1023
      }
Z
zengyawen 已提交
1024 1025
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1026
    }
Z
zengyawen 已提交
1027 1028
  }
  ```
Z
zengyawen 已提交
1029

Z
zengyawen 已提交
1030
  ![zh-cn_image_0000001193872522](figures/zh-cn_image_0000001193872522.png)
Z
zengyawen 已提交
1031 1032


Z
zengyawen 已提交
1033
### moveTo
Z
zengyawen 已提交
1034

Z
zengyawen 已提交
1035
moveTo(x: number, y: number): void
Z
zengyawen 已提交
1036 1037 1038

路径从当前点移动到指定点。

Z
zengyawen 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 说明 | 
  | -------- | -------- | -------- | -------- | -------- |
  | x | number | 是 | 0 | 指定位置的x坐标。 | 
  | y | number | 是 | 0 | 指定位置的y坐标。 | 

- 示例
  ```
  @Entry
  @Component
  struct MoveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.moveTo(10, 10)
            this.offContext.lineTo(280, 160)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1067
      }
Z
zengyawen 已提交
1068 1069
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1070
    }
Z
zengyawen 已提交
1071 1072
  }
  ```
Z
zengyawen 已提交
1073

Z
zengyawen 已提交
1074
  ![zh-cn_image_0000001238832409](figures/zh-cn_image_0000001238832409.png)
Z
zengyawen 已提交
1075 1076


Z
zengyawen 已提交
1077
### lineTo
Z
zengyawen 已提交
1078

Z
zengyawen 已提交
1079
lineTo(x: number, y: number): void
Z
zengyawen 已提交
1080 1081 1082

从当前点到指定点进行路径连接。

Z
zengyawen 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | x | number | 是 | 0 | 指定位置的x坐标。 | 
  | y | number | 是 | 0 | 指定位置的y坐标。 | 

- 示例
  ```
  @Entry
  @Component
  struct LineTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.moveTo(10, 10)
            this.offContext.lineTo(280, 160)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1111
      }
Z
zengyawen 已提交
1112 1113
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1114
    }
Z
zengyawen 已提交
1115 1116
  }
  ```
Z
zengyawen 已提交
1117

Z
zengyawen 已提交
1118
  ![zh-cn_image_0000001238712443](figures/zh-cn_image_0000001238712443.png)
Z
zengyawen 已提交
1119 1120


Z
zengyawen 已提交
1121
### closePath
Z
zengyawen 已提交
1122

Z
zengyawen 已提交
1123
closePath(): void
Z
zengyawen 已提交
1124 1125 1126

结束当前路径形成一个封闭路径。

Z
zengyawen 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
- 示例
  ```
  @Entry
  @Component
  struct ClosePath {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
              this.offContext.beginPath()
              this.offContext.moveTo(30, 30)
              this.offContext.lineTo(110, 30)
              this.offContext.lineTo(70, 90)
              this.offContext.closePath()
              this.offContext.stroke()
              var image = this.offContext.transferToImageBitmap()
              this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1151
      }
Z
zengyawen 已提交
1152 1153
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1154
    }
Z
zengyawen 已提交
1155 1156
  }
  ```
Z
zengyawen 已提交
1157

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


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

Z
zengyawen 已提交
1163
createPattern(image: ImageBitmap, repetition: string): CanvasPattern
Z
zengyawen 已提交
1164 1165 1166

通过指定图像和重复方式创建图片填充的模板。

Z
zengyawen 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 |
  | -------- | -------- | -------- | -------- | -------- |
  | image | [ImageBitmap](ts-components-canvas-imagebitmap.md) | 是 | null | 图源对象,具体参考ImageBitmap对象。 |
  | repetition | string | 是 | “” | 设置图像重复的方式,取值为:'repeat'、'repeat-x'、&nbsp;'repeat-y'、'no-repeat'。 |

- 示例
  ```
  @Entry
  @Component
  struct CreatePattern {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private img:ImageBitmap = new ImageBitmap("common/images/icon.jpg")
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var pattern = this.offContext.createPattern(this.img, 'repeat')
            this.offContext.fillStyle = pattern
            this.offContext.fillRect(0, 0, 200, 200)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1195
      }
Z
zengyawen 已提交
1196 1197
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1198
    }
Z
zengyawen 已提交
1199 1200
  }
  ```
Z
zengyawen 已提交
1201

Z
zengyawen 已提交
1202
  ![zh-cn_image_0000001194352456](figures/zh-cn_image_0000001194352456.png)
Z
zengyawen 已提交
1203 1204


Z
zengyawen 已提交
1205
### bezierCurveTo
Z
zengyawen 已提交
1206

Z
zengyawen 已提交
1207
bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void
Z
zengyawen 已提交
1208 1209 1210

创建三次贝赛尔曲线的路径。

Z
zengyawen 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | cp1x | number | 是 | 0 | 第一个贝塞尔参数的x坐标值。 | 
  | cp1y | number | 是 | 0 | 第一个贝塞尔参数的y坐标值。 | 
  | cp2x | number | 是 | 0 | 第二个贝塞尔参数的x坐标值。 | 
  | cp2y | number | 是 | 0 | 第二个贝塞尔参数的y坐标值。 | 
  | x | number | 是 | 0 | 路径结束时的x坐标值。 | 
  | y | number | 是 | 0 | 路径结束时的y坐标值。 | 

- 示例
  ```
  @Entry
  @Component
  struct BezierCurveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.moveTo(10, 10)
            this.offContext.bezierCurveTo(20, 100, 200, 100, 200, 20)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1243
      }
Z
zengyawen 已提交
1244 1245
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1246
    }
Z
zengyawen 已提交
1247 1248
  }
  ```
Z
zengyawen 已提交
1249

Z
zengyawen 已提交
1250
  ![zh-cn_image_0000001238952403](figures/zh-cn_image_0000001238952403.png)
Z
zengyawen 已提交
1251 1252


Z
zengyawen 已提交
1253
### quadraticCurveTo
Z
zengyawen 已提交
1254

Z
zengyawen 已提交
1255
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void
Z
zengyawen 已提交
1256 1257 1258

创建二次贝赛尔曲线的路径。

Z
zengyawen 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | cpx | number | 是 | 0 | 贝塞尔参数的x坐标值。 | 
  | cpy | number | 是 | 0 | 贝塞尔参数的y坐标值。 | 
  | x | number | 是 | 0 | 路径结束时的x坐标值。 | 
  | y | number | 是 | 0 | 路径结束时的y坐标值。 | 

- 示例
  ```
  @Entry
  @Component
  struct QuadraticCurveTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath();
            this.offContext.moveTo(20, 20);
            this.offContext.quadraticCurveTo(100, 100, 200, 20);
            this.offContext.stroke();
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
1289
      }
Z
zengyawen 已提交
1290 1291
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1292
    }
Z
zengyawen 已提交
1293 1294
  }
  ```
Z
zengyawen 已提交
1295

Z
zengyawen 已提交
1296
  ![zh-cn_image_0000001194032482](figures/zh-cn_image_0000001194032482.png)
Z
zengyawen 已提交
1297 1298


Z
zengyawen 已提交
1299
### arc
Z
zengyawen 已提交
1300

Z
zengyawen 已提交
1301
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise?: boolean): void
Z
zengyawen 已提交
1302 1303 1304

绘制弧线路径。

Z
zengyawen 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | x | number | 是 | 0 | 弧线圆心的x坐标值。 | 
  | y | number | 是 | 0 | 弧线圆心的y坐标值。 | 
  | radius | number | 是 | 0 | 弧线的圆半径。 | 
  | startAngle | number | 是 | 0 | 弧线的起始弧度。 | 
  | endAngle | number | 是 | 0 | 弧线的终止弧度。 | 
  | anticlockwise | boolean | 否 | false | 是否逆时针绘制圆弧。 | 

- 示例
  ```
  @Entry
  @Component
  struct Arc {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
            this.offContext.arc(100, 75, 50, 0, 6.28)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1336
      }
Z
zengyawen 已提交
1337 1338
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1339
    }
Z
zengyawen 已提交
1340 1341
  }
  ```
Z
zengyawen 已提交
1342

Z
zengyawen 已提交
1343
  ![zh-cn_image_0000001239032429](figures/zh-cn_image_0000001239032429.png)
Z
zengyawen 已提交
1344 1345


Z
zengyawen 已提交
1346
### arcTo
Z
zengyawen 已提交
1347

Z
zengyawen 已提交
1348
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void
Z
zengyawen 已提交
1349 1350 1351

依据圆弧经过的点和圆弧半径创建圆弧路径。

Z
zengyawen 已提交
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | x1 | number | 是 | 0 | 圆弧经过的第一个点的x坐标值。 | 
  | y1 | number | 是 | 0 | 圆弧经过的第一个点的y坐标值。 | 
  | x2 | number | 是 | 0 | 圆弧经过的第二个点的x坐标值。 | 
  | y2 | number | 是 | 0 | 圆弧经过的第二个点的y坐标值。 | 
  | radius | number | 是 | 0 | 圆弧的圆半径值。 | 

- 示例
  ```
  @Entry
  @Component
  struct ArcTo {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.moveTo(100, 20);
            this.offContext.arcTo(150, 20, 150, 70, 50);
            this.offContext.stroke();
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1382
      }
Z
zengyawen 已提交
1383 1384
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1385
    }
Z
zengyawen 已提交
1386 1387
  }
  ```
Z
zengyawen 已提交
1388

Z
zengyawen 已提交
1389
  ![zh-cn_image_0000001193872524](figures/zh-cn_image_0000001193872524.png)
Z
zengyawen 已提交
1390 1391


Z
zengyawen 已提交
1392
### ellipse
Z
zengyawen 已提交
1393

Z
zengyawen 已提交
1394
ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise?: boolean): void
Z
zengyawen 已提交
1395 1396 1397

在规定的矩形区域绘制一个椭圆。

Z
zengyawen 已提交
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 说明 | 
  | -------- | -------- | -------- | -------- | -------- |
  | x | number | 是 | 0 | 椭圆圆心的x轴坐标。 | 
  | y | number | 是 | 0 | 椭圆圆心的y轴坐标。 | 
  | radiusX | number | 是 | 0 | 椭圆x轴的半径长度。 | 
  | radiusY | number | 是 | 0 | 椭圆y轴的半径长度。 | 
  | rotation | number | 是 | 0 | 椭圆的旋转角度,单位为弧度。 | 
  | startAngle | number | 是 | 0 | 椭圆绘制的起始点角度,以弧度表示。 | 
  | endAngle | number | 是 | 0 | 椭圆绘制的结束点角度,以弧度表示。 | 
  | anticlockwise | boolean | 否 | false | 是否以逆时针方向绘制椭圆。 | 

- 示例

  ```
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.beginPath()
Z
zengyawen 已提交
1427
            this.offContext.ellipse(200, 200, 50, 100, Math.PI * 0.25, Math.PI * 0.5, Math.PI)
Z
zengyawen 已提交
1428 1429 1430 1431
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1432
      }
Z
zengyawen 已提交
1433 1434
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1435
    }
Z
zengyawen 已提交
1436 1437
  }
  ```
Z
zengyawen 已提交
1438

Z
zengyawen 已提交
1439
  ![zh-cn_image_0000001238832411](figures/zh-cn_image_0000001238832411.png)
Z
zengyawen 已提交
1440 1441


Z
zengyawen 已提交
1442
### rect
Z
zengyawen 已提交
1443

Z
zengyawen 已提交
1444
rect(x: number, y: number, width: number, height: number): void
Z
zengyawen 已提交
1445 1446 1447

创建矩形路径。

Z
zengyawen 已提交
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | x | number | 是 | 0 | 指定矩形的左上角x坐标值。 | 
  | y | number | 是 | 0 | 指定矩形的左上角y坐标值。 | 
  | width | number | 是 | 0 | 指定矩形的宽度。 | 
  | height | number | 是 | 0 | 指定矩形的高度。 | 

- 示例
  ```
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.rect(20, 20, 100, 100) // Create a 100*100 rectangle at (20, 20)
            this.offContext.stroke()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1476
      }
Z
zengyawen 已提交
1477 1478
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1479
    }
Z
zengyawen 已提交
1480 1481
  }
  ```
Z
zengyawen 已提交
1482

Z
zengyawen 已提交
1483
  ![zh-cn_image_0000001238712445](figures/zh-cn_image_0000001238712445.png)
Z
zengyawen 已提交
1484 1485


Z
zengyawen 已提交
1486
### fill
Z
zengyawen 已提交
1487

Z
zengyawen 已提交
1488
fill(): void
Z
zengyawen 已提交
1489 1490 1491

对封闭路径进行填充。

Z
zengyawen 已提交
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
- 示例
  ```
  @Entry
  @Component
  struct Fill {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.rect(20, 20, 100, 100) // Create a 100*100 rectangle at (20, 20)
            this.offContext.fill()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1512
      }
Z
zengyawen 已提交
1513 1514
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1515
    }
Z
zengyawen 已提交
1516 1517
  }
  ```
Z
zengyawen 已提交
1518

Z
zengyawen 已提交
1519
  ![zh-cn_image_0000001194192462](figures/zh-cn_image_0000001194192462.png)
Z
zengyawen 已提交
1520 1521


Z
zengyawen 已提交
1522
### clip
Z
zengyawen 已提交
1523

Z
zengyawen 已提交
1524
clip(): void
Z
zengyawen 已提交
1525 1526 1527

设置当前路径为剪切路径。

Z
zengyawen 已提交
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
- 示例
  ```
  @Entry
  @Component
  struct Clip {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.rect(0, 0, 200, 200)
            this.offContext.stroke()
            this.offContext.clip()
            this.offContext.fillStyle = "rgb(255,0,0)"
            this.offContext.fillRect(0, 0, 150, 150)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1551
      }
Z
zengyawen 已提交
1552 1553
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1554
    }
Z
zengyawen 已提交
1555 1556
  }
  ```
Z
zengyawen 已提交
1557

Z
zengyawen 已提交
1558
  ![zh-cn_image_0000001194352458](figures/zh-cn_image_0000001194352458.png)
Z
zengyawen 已提交
1559 1560


Z
zengyawen 已提交
1561
### rotate
Z
zengyawen 已提交
1562

Z
zengyawen 已提交
1563
rotate(rotate: number): void
Z
zengyawen 已提交
1564 1565 1566

针对当前坐标轴进行顺时针旋转。

Z
zengyawen 已提交
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | rotate | number | 是 | 0 | 设置顺时针旋转的弧度值,可以通过Math.PI&nbsp;/&nbsp;180将角度转换为弧度值。 | 

- 示例
  ```
  @Entry
  @Component
  struct Rotate {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.rotate(45 * Math.PI / 180) // Rotate the rectangle 45 degrees
            this.offContext.fillRect(70, 20, 50, 50)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1592
      }
Z
zengyawen 已提交
1593 1594
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1595
    }
Z
zengyawen 已提交
1596 1597
  }
  ```
Z
zengyawen 已提交
1598

Z
zengyawen 已提交
1599
  ![zh-cn_image_0000001238952405](figures/zh-cn_image_0000001238952405.png)
Z
zengyawen 已提交
1600 1601


Z
zengyawen 已提交
1602
### scale
Z
zengyawen 已提交
1603

Z
zengyawen 已提交
1604
scale(x: number, y: number): void
Z
zengyawen 已提交
1605 1606 1607

设置canvas画布的缩放变换属性,后续的绘制操作将按照缩放比例进行缩放。

Z
zengyawen 已提交
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | x | number | 是 | 0 | 设置水平方向的缩放值。 | 
  | y | number | 是 | 0 | 设置垂直方向的缩放值。 | 

- 示例
  ```
  @Entry
  @Component
  struct Scale {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.strokeRect(10, 10, 25, 25)
            this.offContext.scale(2, 2) // Scale to 200%
            this.offContext.strokeRect(10, 10, 25, 25)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1635
      }
Z
zengyawen 已提交
1636 1637
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1638
    }
Z
zengyawen 已提交
1639 1640
  }
  ```
Z
zengyawen 已提交
1641

Z
zengyawen 已提交
1642
  ![zh-cn_image_0000001194032484](figures/zh-cn_image_0000001194032484.png)
Z
zengyawen 已提交
1643 1644


Z
zengyawen 已提交
1645
### transform
Z
zengyawen 已提交
1646

Z
zengyawen 已提交
1647
transform(scaleX: number, skewX: number, skewY: number, scaleY: number, translateX: number, translateY: number): void
Z
zengyawen 已提交
1648 1649 1650

transform方法对应一个变换矩阵,想对一个图形进行变化的时候,只要设置此变换矩阵相应的参数,对图形的各个定点的坐标分别乘以这个矩阵,就能得到新的定点的坐标。矩阵变换效果可叠加。

Z
zengyawen 已提交
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 变换后的坐标计算方式(x和y为变换前坐标,x'和y'为变换后坐标):
> 
> - x' = scaleX \* x + skewY \* y + translateX
> 
> - y' = skewX \* x + scaleY \* y + translateY

- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | scaleX | number | 是 | 0 | 指定水平缩放值。 | 
  | skewX | number | 是 | 0 | 指定水平倾斜值。 | 
  | skewY | number | 是 | 0 | 指定垂直倾斜值。 | 
  | scaleY | number | 是 | 0 | 指定垂直缩放值。 | 
  | translateX | number | 是 | 0 | 指定水平移动值。 | 
  | translateY | number | 是 | 0 | 指定垂直移动值。 | 

- 示例
  ```
  @Entry
  @Component
  struct Transform {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.fillStyle = 'rgb(0,0,0)'
            this.offContext.fillRect(0, 0, 100, 100)
            this.offContext.transform(1, 0.5, -0.5, 1, 10, 10)
            this.offContext.fillStyle = 'rgb(255,0,0)'
            this.offContext.fillRect(0, 0, 100, 100)
            this.offContext.transform(1, 0.5, -0.5, 1, 10, 10)
            this.offContext.fillStyle = 'rgb(0,0,255)'
            this.offContext.fillRect(0, 0, 100, 100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1694
      }
Z
zengyawen 已提交
1695 1696
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1697
    }
Z
zengyawen 已提交
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
  }
  ```

  ![zh-cn_image_0000001239032431](figures/zh-cn_image_0000001239032431.png)


### setTransform

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

setTransfrom方法使用的参数和transform()方法相同,但setTransform()方法会重置现有的变换矩阵并创建新的变换矩阵。

- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | scaleX | number | 是 | 0 | 指定水平缩放值。 | 
  | skewX | number | 是 | 0 | 指定水平倾斜值。 | 
  | skewY | number | 是 | 0 | 指定垂直倾斜值。 | 
  | scaleY | number | 是 | 0 | 指定垂直缩放值。 | 
  | translateX | number | 是 | 0 | 指定水平移动值。 | 
  | translateY | number | 是 | 0 | 指定垂直移动值。 | 

- 示例
  ```
  @Entry
  @Component
  struct SetTransform {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.fillStyle = 'rgb(255,0,0)'
            this.offContext.fillRect(0, 0, 100, 100)
            this.offContext.setTransform(1,0.5, -0.5, 1, 10, 10)
            this.offContext.fillStyle = 'rgb(0,0,255)'
            this.offContext.fillRect(0, 0, 100, 100)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1743
      }
Z
zengyawen 已提交
1744 1745
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1746
    }
Z
zengyawen 已提交
1747 1748
  }
  ```
Z
zengyawen 已提交
1749

Z
zengyawen 已提交
1750
  ![zh-cn_image_0000001193872526](figures/zh-cn_image_0000001193872526.png)
Z
zengyawen 已提交
1751 1752


Z
zengyawen 已提交
1753
### translate
Z
zengyawen 已提交
1754

Z
zengyawen 已提交
1755
translate(x: number, y: number): void
Z
zengyawen 已提交
1756 1757 1758

移动当前坐标系的原点。

Z
zengyawen 已提交
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | x | number | 是 | 0 | 设置水平平移量。 | 
  | y | number | 是 | 0 | 设置竖直平移量。 | 

- 示例
  ```
  @Entry
  @Component
  struct Translate {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.fillRect(10, 10, 50, 50)
            this.offContext.translate(70, 70)
            this.offContext.fillRect(10, 10, 50, 50)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
      }
      .width('100%')
      .height('100%')
    }
  }
  ```

  ![zh-cn_image_0000001238832413](figures/zh-cn_image_0000001238832413.png)


### drawImage

drawImage(image: ImageBitmap, dx: number, dy: number): void

drawImage(image: ImageBitmap, dx: number, dy: number, dWidth: number, dHeight: number): void

drawImage(image: ImageBitmap, sx: number, sy: number, sWidth: number, sHeight: number, dx: number, dy: number, dWidth: number, dHeight: number):void

进行图像绘制。

- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 |
  | -------- | -------- | -------- | -------- | -------- |
  | image | [ImageBitmap](ts-components-canvas-imagebitmap.md) | 是 | null | 图片资源,请参考ImageBitmap。 |
  | sx | number | 否 | 0 | 裁切源图像时距离源图像左上角的x坐标值。 |
  | sy | number | 否 | 0 | 裁切源图像时距离源图像左上角的y坐标值。 |
  | sWidth | number | 否 | 0 | 裁切源图像时需要裁切的宽度。 |
  | sHeight | number | 否 | 0 | 裁切源图像时需要裁切的高度。 |
  | dx | number | 是 | 0 | 绘制区域左上角在x轴的位置。 |
  | dy | number | 是 | 0 | 绘制区域左上角在y&nbsp;轴的位置。 |
  | dWidth | number | 否 | 0 | 绘制区域的宽度。 |
  | dHeight | number | 否 | 0 | 绘制区域的高度。 |


- 示例
  ```
  @Entry
  @Component
  struct Index {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private img:ImageBitmap = new ImageBitmap("common/images/icon.jpg")
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
Z
zengyawen 已提交
1832 1833
        .width('100%')
        .height('100%')
Z
zengyawen 已提交
1834 1835 1836 1837 1838 1839
        .backgroundColor('#ffff00')
        .onReady(() => {
            this.offContext.drawImage( this.img,0,0,400,200)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
1840
      }
Z
zengyawen 已提交
1841 1842
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1843
    }
Z
zengyawen 已提交
1844 1845
  }
  ```
Z
zengyawen 已提交
1846

Z
zengyawen 已提交
1847
  ![zh-cn_image_0000001238712447](figures/zh-cn_image_0000001238712447.png)
Z
zengyawen 已提交
1848 1849


Z
zengyawen 已提交
1850
### createImageData
Z
zengyawen 已提交
1851

Z
zengyawen 已提交
1852
createImageData(width: number, height: number): Object
Z
zengyawen 已提交
1853

Z
zengyawen 已提交
1854
根据宽高创建ImageData对象,请参考[ImageData](ts-components-canvas-imagebitmap.md)
Z
zengyawen 已提交
1855

Z
zengyawen 已提交
1856 1857 1858 1859 1860
- 参数
  | 参数 | 类型 | 必填 | 默认 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | width | number | 是 | 0 | ImageData的宽度。 | 
  | height | number | 是 | 0 | ImageData的高度。 | 
Z
zengyawen 已提交
1861 1862


Z
zengyawen 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
### createImageData

createImageData(imageData: ImageData): Object

根据已创建的ImageData对象创建新的ImageData对象,请参考[ImageData](ts-components-canvas-imagebitmap.md)

- 参数
  | 参数 | 类型 | 必填 | 默认 | 描述 |
  | -------- | -------- | -------- | -------- | -------- |
  | imagedata | [ImageData](ts-components-canvas-imagebitmap.md) | 是 | null | 被复制的ImageData对象。 |


### getImageData

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

以当前canvas指定区域内的像素创建[ImageData](ts-components-canvas-imagebitmap.md)对象。

- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | sx | number | 是 | 0 | 需要输出的区域的左上角x坐标。 | 
  | sy | number | 是 | 0 | 需要输出的区域的左上角y坐标。 | 
  | sw | number | 是 | 0 | 需要输出的区域的宽度。 | 
  | sh | number | 是 | 0 | 需要输出的区域的高度。 | 


### putImageData

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

使用[ImageData](ts-components-canvas-imagebitmap.md)数据填充新的矩形区域。

- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | imagedata | Object | 是 | null | 包含像素值的ImageData对象。 | 
  | dx | number | 是 | 0 | 填充区域在x轴方向的偏移量。 | 
  | dy | number | 是 | 0 | 填充区域在y轴方向的偏移量。 | 
  | dirtyX | number | 否 | 0 | 源图像数据矩形裁切范围左上角距离源图像左上角的x轴偏移量。 | 
  | dirtyY | number | 否 | 0 | 源图像数据矩形裁切范围左上角距离源图像左上角的y轴偏移量。 | 
  | dirtyWidth | number | 否 | imagedata的宽度 | 源图像数据矩形裁切范围的宽度。 | 
  | dirtyHeight | number | 否 | imagedata的高度 | 源图像数据矩形裁切范围的高度。 | 

- 示例
  ```
  @Entry
  @Component
  struct PutImageData {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
Z
zengyawen 已提交
1918 1919 1920
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
Z
zengyawen 已提交
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
          .onReady(() =>{
            var imageData = this.offContext.createImageData(100, 100)
            for (var i = 0; i < imageData.data.length; i += 4) {
              imageData.data[i + 0] = 255
              imageData.data[i + 1] = 0
              imageData.data[i + 2] = 255
              imageData.data[i + 3] = 255
            }
            this.offContext.putImageData(imageData, 10, 10)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
Z
zengyawen 已提交
1932 1933
          })
      }
Z
zengyawen 已提交
1934 1935
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1936
    }
Z
zengyawen 已提交
1937 1938
  }
  ```
Z
zengyawen 已提交
1939

Z
zengyawen 已提交
1940
  ![zh-cn_image_0000001194192464](figures/zh-cn_image_0000001194192464.png)
Z
zengyawen 已提交
1941 1942


Z
zengyawen 已提交
1943
### restore
Z
zengyawen 已提交
1944

Z
zengyawen 已提交
1945
restore(): void
Z
zengyawen 已提交
1946 1947 1948

对保存的绘图上下文进行恢复。

Z
zengyawen 已提交
1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
- 示例
  ```
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.restore()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
1968
      }
Z
zengyawen 已提交
1969 1970
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
1971
    }
Z
zengyawen 已提交
1972 1973
  }
  ```
Z
zengyawen 已提交
1974 1975


Z
zengyawen 已提交
1976
### save
Z
zengyawen 已提交
1977

Z
zengyawen 已提交
1978
save(): void
Z
zengyawen 已提交
1979 1980 1981

对当前的绘图上下文进行保存。

Z
zengyawen 已提交
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
- 示例
  ```
  @Entry
  @Component
  struct CanvasExample {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            this.offContext.save()
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
        })
Z
zengyawen 已提交
2001
      }
Z
zengyawen 已提交
2002 2003
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2004
    }
Z
zengyawen 已提交
2005 2006
  }
  ```
Z
zengyawen 已提交
2007 2008


Z
zengyawen 已提交
2009
### createLinearGradient
Z
zengyawen 已提交
2010

Z
zengyawen 已提交
2011
createLinearGradient(x0: number, y0: number, x1: number, y1: number): void
Z
zengyawen 已提交
2012 2013 2014

创建一个线性渐变色。

Z
zengyawen 已提交
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | x0 | number | 是 | 0 | 起点的x轴坐标。 | 
  | y0 | number | 是 | 0 | 起点的y轴坐标。 | 
  | x1 | number | 是 | 0 | 终点的x轴坐标。 | 
  | y1 | number | 是 | 0 | 终点的y轴坐标。 | 

- 示例
  ```
  @Entry
  @Component
  struct CreateLinearGradient {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var grad = this.offContext.createLinearGradient(50,0, 300,100)
            this.grad.addColorStop(0.0, 'red')
            this.grad.addColorStop(0.5, 'white')
            this.grad.addColorStop(1.0, 'green')
            this.offContext.fillStyle = grad
            this.offContext.fillRect(0, 0, 500, 500)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2048
      }
Z
zengyawen 已提交
2049 2050
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2051
    }
Z
zengyawen 已提交
2052 2053
  }
  ```
Z
zengyawen 已提交
2054

Z
zengyawen 已提交
2055
  ![zh-cn_image_0000001194352460](figures/zh-cn_image_0000001194352460.png)
Z
zengyawen 已提交
2056 2057


Z
zengyawen 已提交
2058
### createRadialGradient
Z
zengyawen 已提交
2059

Z
zengyawen 已提交
2060
createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number): void
Z
zengyawen 已提交
2061 2062 2063

创建一个径向渐变色。

Z
zengyawen 已提交
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
- 参数
  | 参数 | 类型 | 必填 | 默认值 | 描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | x0 | number | 是 | 0 | 起始圆的x轴坐标。 | 
  | y0 | number | 是 | 0 | 起始圆的y轴坐标。 | 
  | r0 | number | 是 | 0 | 起始圆的半径。必须是非负且有限的。 | 
  | x1 | number | 是 | 0 | 终点圆的x轴坐标。 | 
  | y1 | number | 是 | 0 | 终点圆的y轴坐标。 | 
  | r1 | number | 是 | 0 | 终点圆的半径。必须为非负且有限的。 | 

- 示例
  ```
  @Entry
  @Component
  struct CreateRadialGradient {
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    
    private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
            var grad = this.offContext.createRadialGradient(200,200,50, 200,200,200)
            this.grad.addColorStop(0.0, 'red')
            this.grad.addColorStop(0.5, 'white')
            this.grad.addColorStop(1.0, 'green')
            this.offContext.fillStyle = grad
            this.offContext.fillRect(0, 0, 500, 500)
            var image = this.offContext.transferToImageBitmap()
            this.context.transferFromImageBitmap(image)
          })
Z
zengyawen 已提交
2099
      }
Z
zengyawen 已提交
2100 2101
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
2102
    }
Z
zengyawen 已提交
2103 2104
  }
  ```
Z
zengyawen 已提交
2105

Z
zengyawen 已提交
2106
  ![zh-cn_image_0000001238952407](figures/zh-cn_image_0000001238952407.png)
Z
zengyawen 已提交
2107 2108


Z
zengyawen 已提交
2109
## CanvasPattern
Z
zengyawen 已提交
2110

Z
zengyawen 已提交
2111
一个Object对象, 通过[createPattern](#createpattern)方法创建。