Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML canvas putImageData() method

HTML canvas putImageData() method

Canvas Object Reference Canvas object

Example

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);

}

Try it »


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 9, Firefox, Opera, Chrome and Safari support the putImageData() method.

**Note:** Internet Explorer 8 and earlier versions do not support .


Definition and Usage

The putImageData() method puts the image data (from the specified ImageData object) back onto the canvas.

**Tip:** See the getImageData() method, which can copy the pixel data of the specified rectangle on the canvas.

**Tip:** See the createImageData() method, which can create a new blank ImageData object.


JavaScript syntax

JavaScript syntax: context.putImageData(*imgData,x,y,*dirtyX,dirtyY,dirtyWidth,dirtyHeight);

Parameter value

Parameters Description
imgData Specifies the ImageData object to 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, where the image is placed on the canvas.
dirtyY Optional. The vertical value (y), in pixels, where the image is placed 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.

Canvas Object Reference Canvas object