files/en-us/web/api/canvasrenderingcontext2d/getimagedata/index.md
{{APIRef("Canvas API")}}
The {{domxref("CanvasRenderingContext2D")}} method
getImageData() of the Canvas 2D API returns an
{{domxref("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 {{domxref("CanvasRenderingContext2D.putImageData()", "putImageData()")}} method.
You can find more information about getImageData() and general
manipulation of canvas contents in Pixel manipulation with canvas.
getImageData(sx, sy, sw, sh)
getImageData(sx, sy, sw, sh, settings)
sx
ImageData will be extracted.sy
ImageData will be extracted.sw
ImageData will be extracted.
Positive values are to the right, and negative to the left.sh
ImageData will be extracted.
Positive values are down, and negative are up.settings {{optional_inline}}
colorSpace
"srgb" for the sRGB color space or "display-p3" for the display-p3 color space.pixelFormat
"rgba-unorm8", for RGBA with 8 bit per component unsigned normalized format, using a {{jsxref("Uint8ClampedArray")}}."rgba-float16", for RGBA with 16 bits per component, using a {{jsxref("Float16Array")}}. Floating-point pixel values allow representing colors in arbitrarily wide gamuts and high dynamic range (HDR).An {{domxref("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).
IndexSizeError {{domxref("DOMException")}}
sw or sh are zero.SecurityError {{domxref("DOMException")}}
SecurityError {{domxref("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.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.
<canvas id="canvas" width="700" height="400"></canvas>
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);
});
{{EmbedLiveSample("Getting_image_data_from_a_canvas", "", 420)}}
The optional colorSpace setting allows you to get image data in the desired format.
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"
The optional pixelFormat setting allows you to get image data in the desired pixel format.
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}}
{{Compat}}