1. You can provide the width and height of the imagedata directly like this:
var Imagedata = ctx.createImageData(width, height);
Here, createImageData() will return an ImageData object with pixel information preset
to black transparent (0,0,0,0). The ImageData object will be 4 * width * height
long array. When imagedata object is put on the canvas using the putImageData()
method, it will have the width and height specified in the createImageData() method.
2. You can also pass another ImageData object as a parameter to the createImageData()
method. In this case, the return value imagedata will have the
same dimensions of
the ImageData that is passed as parameter. However, all pixel information will be
preset to black transparent (0,0,0,0).
<html>
<head>
<script type="application/javascript">
function init() {
var canvas = document.getElementById("canvas");
if (canvas.getContext)
{
var ctx = canvas.getContext("2d");
// Create an ImageData object by specifying the width and height.
var pixelSet1 = ctx.createImageData(50,50);
// Create another ImageData by passing pixelSet1 as a parameter.
var pixelSet2 = ctx.createImageData(pixelSet1);