Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new ImageData object independently?

I want to create a new ImageData object in code. If I have a Uint8ClampedArray out of which I want to make an image object, what is the best way to do it?

I guess I could make a new canvas element, extract its ImageData and overwrite its data attribute, but that seems like a wrong approach.

It would be great if I could use the ImageData constructor directly, but I can't figure out how to.

like image 769
Likhit Avatar asked Nov 29 '12 13:11

Likhit


People also ask

Which method in canvas is used to create a new blank Image data object?

createImageData() method of the Canvas 2D API creates a new, blank ImageData object with the specified dimensions.

Which method returns an object that contains image data of a specific ImageData object?

The getImageData() method returns an ImageData object that copies the pixel data for the specified rectangle on a canvas.

What is ImageData?

The ImageData interface represents the underlying pixel data of an area of a <canvas> element. It is created using the ImageData() constructor or creator methods on the CanvasRenderingContext2D object associated with a canvas: createImageData() and getImageData() .


2 Answers

This is interesting problem... You can't just create ImageData object:

var test = new ImageData(); // TypeError: Illegal constructor

I have also tried:

var imageData= context.createImageData(width, height);
imageData.data = mydata; // TypeError: Cannot assign to read only property 'data' of #<ImageData> 

but as described in MDN data property is readonly.

So I think the only way is to create object and set data property with iteration:

var canvas = document.createElement('canvas');
var imageData = canvas.getContext('2d').createImageData(width, height);
for(var i = 0; i < myData.length; i++){
    imageData.data[i] = myData[i];
}

Update: I have discovered the set method in data property of ImageData, so solution is very simple:

var canvas = document.createElement('canvas');
var imageData = canvas.getContext('2d').createImageData(width, height);
imageData.data.set(myData);
like image 124
hamczu Avatar answered Oct 20 '22 01:10

hamczu


Newest versions of Chrome and Firefox already support ImageData constructor. You can find its definition at MDN.

For other browsers you can create this constructor yourself:

function ImageData() {
    var i = 0;
    if(arguments[0] instanceof Uint8ClampedArray) {
        var data = arguments[i++];
    }
    var width = arguments[i++];
    var height = arguments[i];      

    var canvas = document.createElement('canvas');
    canvas.width = width;
    canvas.height = height;
    var ctx = canvas.getContext('2d');
    var imageData = ctx.createImageData(width, height);
    if(data) imageData.data.set(data);
    return imageData;
}

You can use it like this:

var imgData1 = new ImageData(data, width, height);
var imgData2 = new ImageData(width, height);
like image 35
Luka Avatar answered Oct 20 '22 00:10

Luka