I have two canvases and I want to transfer the contents of canvas1, serialize it to ArrayBuffer, and then load it into canvas2. In the future, I will send the contents of canvas1 to the server, process it and return it to canvas2, but right now I just want to serialize and deserialize it.
I found this way to get canvas information in bytes:
var img1 = context.getImageData(0, 0, 400, 320); var binary = new Uint8Array(img1.data.length); for (var i = 0; i < img1.data.length; i++) { binary[i] = img1.data[i]; }
And also found this way of setting information to an Image object:
var blob = new Blob( [binary], { type: "image/png" } ); var urlCreator = window.URL || window.webkitURL; var imageUrl = urlCreator.createObjectURL( blob ); var img = new Image(); img.src = imageUrl;
But, unfortunately, this does not work.
What would be the right way to do this?
Thanks.
javascript html5 serialization html5-canvas arraybuffer
vtortola
source share