---
name: "Canvas.createImageData"
mode: "api"
versionRequirements:
  - { name: "Base Library", version: "2.21.8" }
title: "Canvas.createImageData - 创建 ImageData 对象"
---

## Canvas.createImageData

> [VERSION] Base Library >= 2.21.8

> 💡 Implemented internally via the 2D context's createImageData. Returns undefined if the Canvas cannot obtain a 2D context.

### Description

Create an ImageData object for pixel-level operations

### Parameters

`Params`

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `width` | `number` | Yes | Width of the ImageData |
| `height` | `number` | Yes | Height of the ImageData |
| `settings` | `ImageDataSettings` | No | Optional. Specifies the image data's color space and other settings |

### Return Value

Type: `ImageData`

ImageData object; returns undefined if creation fails

### Examples

#### Create and manipulate ImageData

```js
const canvas = await this.instance.getCanvasById('myCanvas');
const imageData = canvas.createImageData(100, 100);
for (let i = 0; i < imageData.data.length; i += 4) {
  imageData.data[i] = 255;     // R
  imageData.data[i + 3] = 255; // A
}
const ctx = canvas.getContext('2d');
ctx.putImageData(imageData, 0, 0);
```
