The CanvasRenderingContext2D.getImageData() method of the Canvas 2D API returns an ImageData object representing the underlying pixel data for the area of the canvas denoted by the rectangle which starts at (sx, sy) and has an sw width and sh height. This method is not affected by the canvas transformation matrix.

Pixels outside of the canvas area are present as transparent black values in the returned ImageData.


imageData = ctx.getImageData(sx, sy, sw, sh);


The x coordinate of the upper left corner of the rectangle from which the ImageData will be extracted.
The y coordinate of the upper left corner of the rectangle from which the ImageData will be extracted.
The width of the rectangle from which the ImageData will be extracted.
The height of the rectangle from which the ImageData will be extracted.

Return value

An ImageData object containing the image data for the given rectangle of the canvas.

Errors thrown

Thrown if either of the width or height arguments are zero.


Using the getImageData method

This is just a simple code snippet which uses the getImageData method. For more information, see Pixel manipulation with canvas and the ImageData object.


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


var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.rect(10, 10, 100, 100);

console.log(ctx.getImageData(50, 50, 100, 100));
// ImageData { width: 100, height: 100, data: Uint8ClampedArray[40000] }


Specification Status Comment
HTML Living Standard
The definition of 'CanvasRenderingContext2D.getImageData' in that specification.
Living Standard  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support Yes12



Yes Yes Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes Yes



Yes Yes Yes

1. 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.

See also

Document Tags and Contributors

Contributors to this page: TimothyGu, fscholz, zbjornson, brttd, nmve, erikadoyle, michaellaszlo
Last updated by: TimothyGu,