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);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…