• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ImageData
2
3An **ImageData** object stores pixel data rendered on a canvas.
4
5>  **NOTE**
6>
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.
8
9## APIs
10
11constructor(width: number, height: number, data?: Uint8ClampedArray);
12
13Since API version 9, this API is supported in ArkTS widgets.
14
15## Attributes
16
17| Name    | Type               | Description                                      |
18| ------ | ----------------- | ---------------------------------------- |
19| width  | number            | Actual width of the rectangle on the canvas, in pixels. Read-only.<br>Since API version 9, this API is supported in ArkTS widgets.|
20| height | number            | Actual height of the rectangle on the canvas, in pixels. Read-only.<br>Since API version 9, this API is supported in ArkTS widgets.|
21| data   | Uint8ClampedArray | A one-dimensional array of color values. Read-only. The values range from 0 to 255.<br>Since API version 9, this API is supported in ArkTS widgets.|
22
23>  **NOTE**
24>
25>  You can use the [px2vp](ts-pixel-units.md#pixel-unit-conversion) API to convert the unit.
26
27**Example**
28
29  ```ts
30  // xxx.ets
31  @Entry
32  @Component
33  struct Translate {
34    private settings: RenderingContextSettings = new RenderingContextSettings(true)
35    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
36    private img:ImageBitmap = new ImageBitmap("common/images/1234.png")
37
38    build() {
39      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
40        Canvas(this.context)
41          .width('100%')
42          .height('100%')
43          .backgroundColor('#ffff00')
44          .onReady(() =>{
45            this.context.drawImage(this.img,0,0,130,130)
46            let imagedata = this.context.getImageData(50,50,130,130)
47            this.context.putImageData(imagedata,150,150)
48          })
49      }
50      .width('100%')
51      .height('100%')
52    }
53  }
54  ```
55
56  ![en-us_image_000000127777780](figures/en-us_image_000000127777780.png)
57