ImageData: ImageData() constructor

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since August 2016.

Note: This feature is available in Web Workers.

The ImageData() constructor returns a newly instantiated ImageData object built from the typed array given and having the specified width and height.

This constructor is the preferred way of creating such an object in a Worker.

Syntax

js
new ImageData(width, height)
new ImageData(width, height, settings)

new ImageData(dataArray, width)
new ImageData(dataArray, width, height)
new ImageData(dataArray, width, height, settings)

Parameters

width

An unsigned long representing the width of the image.

height

An unsigned long representing the height of the image. This value is optional if an array is given: the height will be inferred from the array's size and the given width.

settings Optional

An object with the following properties:

colorSpace

Specifies the color space of the image data. Can be set to "srgb" for the sRGB color space or "display-p3" for the display-p3 color space.

pixelFormat

Specifies the pixel format. Possible values:

  • "rgba-unorm8", for RGBA with 8 bit per component unsigned normalized format, using a Uint8ClampedArray. This is the default.
  • "rgba-float16", for RGBA with 16 bits per component, using a Float16Array. Floating-point pixel values allow representing colors in arbitrarily wide gamuts and high dynamic range (HDR).
dataArray

A Uint8ClampedArray or Float16Array containing the underlying pixel representation of the image. If no such array is given, an image with a transparent black rectangle of the specified width and height will be created. The type of the dataArray must match settings.pixelFormat.

Return value

A new ImageData object.

Exceptions

IndexSizeError DOMException

Thrown if dataArray is specified, but its length is not (bytesPerPixel * width * height), or a multiple of (bytesPerPixel * width) if height is not specified. bytesPerPixel is 4 when pixelFormat is "rgba-unorm8" and 8 otherwise.

InvalidStateError DOMException

Thrown if dataArray is of type Uint8ClampedArray and pixelFormat is not set to "rgba-unorm8", or if dataArray is of type Float16Array and pixelFormat is not set to "rgba-float16".

Examples

Creating a blank ImageData object

This example creates an ImageData object that is 200 pixels wide and 100 pixels tall, containing a total of 20,000 pixels.

js
let imageData = new ImageData(200, 100);
// ImageData { width: 200, height: 100, data: Uint8ClampedArray[80000] }

ImageData using the display-p3 color space

This example creates an ImageData object with the display-p3 color space.

js
let imageData = new ImageData(200, 100, { colorSpace: "display-p3" });

Floating-point pixel data for wide gamuts and high dynamic range (HDR)

Floating-point pixel values allow representing colors in arbitrarily wide gamuts and high dynamic range (HDR). You can set the pixelFormat setting to "rgba-float16" to use RGBA values with 16 bits per component. This requires the dataArray to be a Float16Array.

js
let floatArray = new Float16Array(4 * 200 * 200);
let imageData = new ImageData(floatArray, 200, 200, {
  pixelFormat: "rgba-float16",
});
console.log(imageData.pixelFormat); // "rgba-float16"

Initializing ImageData with an array

This example instantiates an ImageData object with pixel colors defined by an array.

HTML

html
<canvas id="canvas"></canvas>

JavaScript

The array (arr) has a length of 40000: it consists of 10,000 pixels, each of which is defined by 4 values. The ImageData constructor specifies a width of 200 for the new object, so its height defaults to 10,000 divided by 200, which is 50.

js
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const arr = new Uint8ClampedArray(40_000);

// Fill the array with the same RGBA values
for (let i = 0; i < arr.length; i += 4) {
  arr[i + 0] = 0; // R value
  arr[i + 1] = 190; // G value
  arr[i + 2] = 0; // B value
  arr[i + 3] = 255; // A value
}

// Initialize a new ImageData object
let imageData = new ImageData(arr, 200);

// Draw image data to the canvas
ctx.putImageData(imageData, 20, 20);

Result

Specifications

Specification
HTML
# dom-imagedata
HTML
# dom-imagedata-with-data

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
ImageData() constructor
Chrome – Full support
Chrome 36 (Release date: 2014-07-16)
footnote Full support
Edge – Full support
Edge 14 (Release date: 2016-08-02)
footnote Full support
Firefox – Full support
Firefox 29 (Release date: 2014-04-29)
footnote Full support
Opera – Full support
Opera 23 (Release date: 2014-07-22)
footnote Full support
Safari – Full support
Safari 8 (Release date: 2014-10-16)
footnote Full support
Chrome Android – Full support
Chrome Android 36 (Release date: 2014-07-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 29 (Release date: 2014-04-29)
footnote Full support
Opera Android – Full support
Opera Android 24 (Release date: 2014-09-10)
footnote Full support
Safari on iOS – Full support
Safari on iOS 8 (Release date: 2014-09-17)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 37 (Release date: 2014-09-03)
footnote Full support
WebView on iOS – Full support
WebView on iOS 8 (Release date: 2014-09-17)
footnote Full support
Deno – Full support
Deno 1.39 (Release date: 2023-12-14)
footnote Full support
settings_parameter
Chrome – Full support
Chrome 92 (Release date: 2021-07-20)
footnote Full support
Edge – Full support
Edge 92 (Release date: 2021-07-22)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 78 (Release date: 2021-08-03)
footnote Full support
Safari – Full support
Safari 15.2 (Release date: 2021-12-13)
footnote Full support
Chrome Android – Full support
Chrome Android 92 (Release date: 2021-07-20)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 65 (Release date: 2021-10-20)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15.2 (Release date: 2021-12-13)
footnote Full support
Samsung Internet – Full support
Samsung Internet 16 (Release date: 2021-11-25)
footnote Full support
WebView Android – Full support
WebView Android 92 (Release date: 2021-07-20)
footnote Full support
WebView on iOS – Full support
WebView on iOS 15.2 (Release date: 2021-12-13)
footnote Full support
Deno – No support
Deno
footnote No support
settings_parameter.colorSpace_option
Chrome – Full support
Chrome 92 (Release date: 2021-07-20)
footnote Full support
Edge – Full support
Edge 92 (Release date: 2021-07-22)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 78 (Release date: 2021-08-03)
footnote Full support
Safari – Full support
Safari 15.2 (Release date: 2021-12-13)
footnote Full support
Chrome Android – Full support
Chrome Android 92 (Release date: 2021-07-20)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 65 (Release date: 2021-10-20)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15.2 (Release date: 2021-12-13)
footnote Full support
Samsung Internet – Full support
Samsung Internet 16 (Release date: 2021-11-25)
footnote Full support
WebView Android – Full support
WebView Android 92 (Release date: 2021-07-20)
footnote Full support
WebView on iOS – Full support
WebView on iOS 15.2 (Release date: 2021-12-13)
footnote Full support
Deno – No support
Deno
footnote No support
pixelFormat option
Experimental
Chrome – Full support
Chrome 137 (Release date: 2025-05-27)
footnote Full support
Edge – Full support
Edge 137 (Release date: 2025-05-29)
footnote Full support
Firefox – No support
Firefox
footnote
footnote See bug 1958830
Opera – Full support
Opera 121 (Release date: 2025-08-27)
footnote Full support
Safari – No support
Safari
footnote
footnote See bug 291196
Chrome Android – Full support
Chrome Android 137 (Release date: 2025-05-27)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote
footnote See bug 1958830
Opera Android – Full support
Opera Android 90 (Release date: 2025-06-18)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote
footnote See bug 291196
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Full support
WebView Android 137 (Release date: 2025-05-27)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote
footnote See bug 291196
Deno – No support
Deno
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.

See also