ts-components-canvas-imagedata.md 1.7 KB
Newer Older
Z
zengyawen 已提交
1
# ImageData
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
An **ImageData** object stores pixel data rendered on a canvas.
Z
zengyawen 已提交
4

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

Z
zengyawen 已提交
10 11 12

## Attributes

E
ester.zhou 已提交
13
| Name| Type| Description|
Z
zengyawen 已提交
14
| -------- | -------- | -------- |
E
ester.zhou 已提交
15 16 17
| width | number | Actual width of the rectangle on the canvas, in pixels.<br>Since API version 9, this API is supported in ArkTS widgets.|
| height | number | Actual height of the rectangle on the canvas, in pixels.<br>Since API version 9, this API is supported in ArkTS widgets.|
| data | Uint8ClampedArray | A one-dimensional array of color values. The values range from 0 to 255.<br>Since API version 9, this API is supported in ArkTS widgets.|
E
esterzhou 已提交
18

E
ester.zhou 已提交
19 20 21 22
>  **NOTE**
> 
> You can use [px2vp](ts-pixel-units.md) to convert units.

E
esterzhou 已提交
23 24 25 26 27 28 29
**Example**

  ```ts
  // xxx.ets
@Entry
@Component
struct Translate {
E
ester.zhou 已提交
30 31
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
E
esterzhou 已提交
32 33 34 35 36 37 38 39 40
  private img:ImageBitmap = new ImageBitmap("/common/images/1234.png")

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
E
ester.zhou 已提交
41 42 43
          this.context.drawImage(this.img,0,0,130,130)
          var imagedata = this.context.getImageData(50,50,130,130)
          this.context.putImageData(imagedata,150,150)
E
esterzhou 已提交
44 45 46 47 48 49 50 51 52
        })
    }
    .width('100%')
    .height('100%')
  }
}
  ```

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