CanvasRenderingContext2D: getImageData() method

Baseline Widely available

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

The CanvasRenderingContext2D method getImageData() of the Canvas 2D API returns an ImageData object representing the underlying pixel data for a specified portion of the canvas.

This method is not affected by the canvas's transformation matrix. If the specified rectangle extends outside the bounds of the canvas, the pixels outside the canvas are transparent black in the returned ImageData object.

Note: Image data can be painted onto a canvas using the putImageData() method.

You can find more information about getImageData() and general manipulation of canvas contents in Pixel manipulation with canvas.

Syntax

js
getImageData(sx, sy, sw, sh)
getImageData(sx, sy, sw, sh, settings)

Parameters

sx

The x-axis coordinate of the top-left corner of the rectangle from which the ImageData will be extracted.

sy

The y-axis coordinate of the top-left corner of the rectangle from which the ImageData will be extracted.

sw

The width of the rectangle from which the ImageData will be extracted. Positive values are to the right, and negative to the left.

sh

The height of the rectangle from which the ImageData will be extracted. Positive values are down, and negative are up.

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.
  • "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).

Return value

An ImageData object containing the image data for the rectangle of the canvas specified. The coordinates of the rectangle's top-left corner are (sx, sy), while the coordinates of the bottom corner are (sx + sw - 1, sy + sh - 1).

Exceptions

IndexSizeError DOMException

Thrown if either sw or sh are zero.

SecurityError DOMException

The canvas contains or may contain pixels which were loaded from an origin other than the one from which the document itself was loaded. To avoid a SecurityError DOMException being thrown in this situation, configure CORS to allow the source image to be used in this way. See Allowing cross-origin use of images and canvas.

Examples

Getting image data from a canvas

This example draws an image, and then uses getImageData() to grab a portion of the canvas.

We use getImageData() to extract a slice of the image, starting at (10, 20), with a width of 80 and a height of 230. We then draw this slice three times, positioning the slices progressively below and to the right of the last slice.

HTML

html
<canvas id="canvas" width="700" height="400"></canvas>

JavaScript

js
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");

const image = new Image();
image.src = "plumeria.jpg";
image.addEventListener("load", () => {
  ctx.drawImage(image, 0, 0, 233, 320);

  const imageData = ctx.getImageData(10, 20, 80, 230);
  ctx.putImageData(imageData, 260, 0);
  ctx.putImageData(imageData, 380, 50);
  ctx.putImageData(imageData, 500, 100);
});

Result

Color space conversion

The optional colorSpace setting allows you to get image data in the desired format.

js
const context = canvas.getContext("2d", { colorSpace: "display-p3" });
context.fillStyle = "color(display-p3 0.5 0 0)";
context.fillRect(0, 0, 10, 10);

// Get ImageData converted to sRGB
const imageData = context.getImageData(0, 0, 1, 1, { colorSpace: "srgb" });
console.log(imageData.colorSpace); // "srgb"

Getting data in different pixel formats

The optional pixelFormat setting allows you to get image data in the desired pixel format.

js
const context = canvas.getContext("2d");

const imageData = context.getImageData(0, 0, 1, 1);
console.log(imageData.pixelFormat); // "rgba-unorm8"

const imageData = context.getImageData(0, 0, 1, 1, {
  pixelFormat: "rgba-float16",
});
console.log(imageData.pixelFormat); // "rgba-float16"

Specifications

Specification
HTML
# dom-context-2d-getimagedata-dev

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
getImageData
Chrome – Full support
Chrome 2 (Release date: 2009-05-21)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 2 (Release date: 2006-10-24)
footnote
footnote Since Firefox 5, getImageData now correctly accepts rectangles that extend beyond the bounds of the canvas; pixels outside the canvas are returned as transparent black and now also returns at least one pixel's worth of image data if a rectangle smaller than one pixel is specified.
Opera – Full support
Opera 9.5 (Release date: 2008-06-12)
footnote Full support
Safari – Full support
Safari 4 (Release date: 2009-06-08)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote
footnote Since Firefox for Android 5, getImageData now correctly accepts rectangles that extend beyond the bounds of the canvas; pixels outside the canvas are returned as transparent black and now also returns at least one pixel's worth of image data if a rectangle smaller than one pixel is specified.
Opera Android – Full support
Opera Android 10.1 (Release date: 2010-11-09)
footnote Full support
Safari on iOS – Full support
Safari on iOS 3.2 (Release date: 2010-04-03)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 3.2 (Release date: 2010-04-03)
footnote Full support

Legend

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

Full support
Full support
See implementation notes.

See also