HTML canvas putImageData()Method
Examples
The following code copies the pixel data of a specified rectangle on the canvas via getImageData(), then puts the image data back onto the canvas via putImageData():
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="red";
ctx.fillRect(10,10,50,50);
function copy()
{
var imgData=ctx.getImageData(10,10,50,50);
ctx.putImageData(imgData,10,70);
}
var ctx=c.getContext("2d");
ctx.fillStyle="red";
ctx.fillRect(10,10,50,50);
function copy()
{
var imgData=ctx.getImageData(10,10,50,50);
ctx.putImageData(imgData,10,70);
}
Try it Yourself »
Browser Support
![]()
![]()
![]()
![]()
![]()
Internet Explorer 9, Firefox, Opera, Chrome, and Safari support the putImageData() method.
Note:Internet Explorer 8 and earlier versions do not support the <canvas> element.
Definition and Usage
The putImageData() method puts image data (from the specified ImageData object) back onto the canvas.
Tip:Please refer togetImageData()the method, which can copy the pixel data of a specified rectangle on the canvas.
Tip:Please refer tocreateImageData()the method, which can create a new blank ImageData object.
JavaScript Syntax
| JavaScript syntax: | context.putImageData(imgData,x,y,dirtyX,dirtyY,dirtyWidth,dirtyHeight); |
|---|
Parameter Values
| Parameter | Description |
|---|---|
| imgData | Specifies the ImageData object to be put back onto the canvas. |
| x | The x-coordinate of the upper-left corner of the ImageData object, in pixels. |
| y | The y-coordinate of the upper-left corner of the ImageData object, in pixels. |
| dirtyX | Optional. The horizontal value (x), in pixels, of the position to place the image on the canvas. |
| dirtyY | Optional. The vertical value (y), in pixels, of the position to place the image on the canvas. |
| dirtyWidth | Optional. The width used to draw the image on the canvas. |
| dirtyHeight | Optional. The height used to draw the image on the canvas. |
HTML canvas Reference Manual Other Extensions