--- title: CanvasRenderingContext2D.getImageData() slug: Web/API/CanvasRenderingContext2D/getImageData tags: - API - Canvas - Method translation_of: Web/API/CanvasRenderingContext2D/getImageData ---
CanvasRenderingContext2D.getImageData() - метод Canvas 2D API, возвращает объект {{domxref("ImageData")}}, представляющий базовые пиксельные данные для области холста, обозначенного прямоугольником, который начинается в точке (sx, sy) и имеет ширину sw и высоту sh.
ImageData ctx.getImageData(sx, sy, sw, sh);
sxsyswshОбъект {{domxref("ImageData")}}, содержащий данные изображения для данного прямоугольника холста.
IndexSizeErrorSecurityErrorSecurityError 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 a rectangle, and then uses getImageData() to grab a portion of the canvas.
<canvas id="canvas"></canvas>
The object retrieved by getImageData() has a width of 200 and a height of 100, for a total of 20,000 pixels. Of those pixels, most are either transparent or taken from off the canvas; only 5,000 of them are opaque black (the color of the drawn rectangle).
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.rect(10, 10, 100, 100);
ctx.fill();
let imageData = ctx.getImageData(60, 60, 200, 100);
ctx.putImageData(imageData, 150, 10);
{{EmbedLiveSample("Getting_image_data_from_a_canvas", 700, 180)}}
{{Compat}}