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.
This is interesting problem… You can’t just create
ImageDataobject:I have also tried:
but as described in MDN
dataproperty is readonly.So I think the only way is to create object and set data property with iteration:
Update:
I have discovered the
setmethod indataproperty of ImageData, so solution is very simple: