Canvas putImageData() Method
Example
Copy the pixel data for a rectangle with getImageData(). Then put the image data back onto the canvas with putImageData():
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(10, 10, 50, 50);
function copy()
{
const imgData = ctx.getImageData(10, 10, 50, 50);
ctx.putImageData(imgData, 10, 70);
}
Try it Yourself »
Description
The putImageData()
method puts the image data (from a specified ImageData object)
back onto the canvas.
See Also:
Syntax
context.putImageData(imgData, x, y, dirtyX, dirtyY, dirtyWidth, dirtyHeight) |
Parameter Values
Param | Description |
---|---|
imgData | The ImageData object to put on the context. |
x | The x-coordinate, in pixels, of the the image. |
y | The y-coordinate, in pixels, of the image. |
dirtyX | Optional. The x coordinate. Default: 0. |
dirtyY | Optional. The y coordinate. Default: 0. |
dirtyWidth | Optional. The width. Default: the width of the extracted image. |
dirtyHeight | Optional. The height. Default: the height of the extracted image. |
Return Values
NONE |
Browser Support
The <canvas>
element is an HTML5 standard (2014).
putImageData()
is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
❮ Canvas Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.