HTML canvas createImageData() method
Example
Create a 100*100 pixels ImageData object where every pixel is red, then place it on the canvas:
JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var imgData=ctx.createImageData(100,100);
for (var i=0;i { imgData.data[i+0]=255; imgData.data[i+1]=0; imgData.data[i+2]=0; imgData.data[i+3]=255; } ctx.putImageData(imgData,10,10); Try it » Internet Explorer 9, Firefox, Opera, Chrome and Safari support the createImageData() method. **Note:** Internet Explorer 8 and earlier versions do not support The createImageData() method creates a new blank ImageData object. The default pixel value of the new object is transparent black. For each pixel in the ImageData object, there are four pieces of information, namely the RGBA values: R - Red (0-255)
Therefore, transparent black means (0,0,0,0). color/alpha information is stored as an array, and since the array contains four pieces of information for each pixel, the size of the array is four times that of the ImageData object: widthheight4. (A simpler way to get the array size is to use ImageDataObject.data.length) The array containing color/alpha information is stored in the ImageData object's data attribute. **Tip:** After you finish manipulating the color/alpha information in the array, you can use putImageData() method to copy the image data back onto the canvas. Example: The syntax to turn the first pixel in the ImageData object red: imgData=ctx.createImageData(100,100); imgData.data[0]=255; imgData.data[1]=0; imgData.data[2]=0; imgData.data[3]=255; The syntax to turn the second pixel in the ImageData object green: imgData=ctx.createImageData(100,100); imgData.data[4]=0; imgData.data[5]=255; imgData.data[6]=0; imgData.data[7]=255; There are two versions of the createImageData() method:
Browser support





Definition and Usage
G - Green (0-255)
B - Blue (0-255)
A - alpha channel (0-255; 0 is transparent, 255 is fully visible)
JavaScript syntax
JavaScript syntax:
var imgData=context.createImageData(width,height);
JavaScript syntax:
var imgData=context.createImageData(imageData);
Parameter value
Parameters
Description
width
The width of the ImageData object, in pixels.
height
The height of the ImageData object, in pixels.
imageData
Another ImageData object.
HTML canvas Reference Manual