• 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
10
11## Attributes
12
13| Name| Type| Description|
14| -------- | -------- | -------- |
15| width | number | Actual width of the rectangle on the canvas, in pixels.<br>Since API version 9, this API is supported in ArkTS widgets.|
16| height | number | Actual height of the rectangle on the canvas, in pixels.<br>Since API version 9, this API is supported in ArkTS widgets.|
17| 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.|
18
19>  **NOTE**
20>
21> You can use [px2vp](ts-pixel-units.md) to convert units.
22
23**Example**
24
25  ```ts
26  // xxx.ets
27  @Entry
28  @Component
29  struct Translate {
30    private settings: RenderingContextSettings = new RenderingContextSettings(true)
31    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
32    private img:ImageBitmap = new ImageBitmap("common/images/1234.png")
33
34    build() {
35      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
36        Canvas(this.context)
37          .width('100%')
38          .height('100%')
39          .backgroundColor('#ffff00')
40          .onReady(() =>{
41            this.context.drawImage(this.img,0,0,130,130)
42            var imagedata = this.context.getImageData(50,50,130,130)
43            this.context.putImageData(imagedata,150,150)
44          })
45      }
46      .width('100%')
47      .height('100%')
48    }
49  }
50  ```
51
52  ![en-us_image_000000127777780](figures/en-us_image_000000127777780.png)
53