---
name: "canvasGetImageData"
mode: "api"
versionRequirements:
  - { name: "Base Library", version: "2.21.8" }
title: "ty.canvasGetImageData - 获取 canvas 区域隐含的像素数据"
---

## canvasGetImageData

> [VERSION] Base Library >= 2.21.8

### Description

Get the underlying pixel data in a canvas region

### Parameters

`Params`

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `params` | `CanvasGetImageDataOption` | Yes | Pixel region configuration |

### Return Value

None


### Referenced Types

##### `interface` CanvasGetImageDataOption

| Property | Type | Description |
| --- | --- | --- |
| `canvasId` | `string` | canvas component id |
| `x` | `number` | X coordinate of the top-left corner of the rectangle from which to extract image data |
| `y` | `number` | Y coordinate of the top-left corner of the rectangle from which to extract image data |
| `width` | `number` | Width of the rectangle from which to extract image data |
| `height` | `number` | Height of the rectangle from which to extract image data |
| `success` | `(result: CanvasGetImageDataResult) => void` | Callback on successful API call |
| `fail` | `(result: CanvasGetImageDataResult) => void` | Callback on failure |
| `complete` | `(result: CanvasGetImageDataCompleteResult) => void` | Callback on complete (success or failure) |

##### `interface` CanvasGetImageDataResult

| Property | Type | Description |
| --- | --- | --- |
| `width` | `number` | Pixel data width |
| `height` | `number` | Pixel data height |
| `data` | `number[]` | Image pixel data, a 1D array where every four items represent a pixel's RGBA |

##### `type` CanvasGetImageDataCompleteResult

| Property | Type | Description |
| --- | --- | --- |
| `type` | `string` | Result type |
| `result` | `CanvasGetImageDataResult` | Result data |


### Examples

#### Get canvas pixel data

```ts
ty.canvasGetImageData({
  canvasId: 'myCanvas',
  x: 0,
  y: 0,
  width: 100,
  height: 100,
  success(res) {
    console.log('Pixel data', res.data);
  },
});
```
