I'm creating a PNG file from a canvas, but before I want to display the canvas as an img element, using a blob as the src of the img. What I tried so far:
canvas = document.getElementById("canvas");
//Get data from canvas
img_b64 = canvas.toDataURL('image/png');
//Create PNG
png = img_b64.split(',')[1];
//Create new img
img = document.createElement("img");
img.src = img_b64;
//Append img to document
//Save png
This works, but instead of img_b64
I want a blob to be the src of the img, like this:
//Create blob from new PNG
blob = new Blob([window.atob(png)], { type: 'image/png', encoding: 'utf-8' });
//Create new img with blob as src
img = document.createElement("img");
img.src = URL.createObjectURL(blob);
//Append img to document
//Save png
The above code only shows me an empty img. Is there a way to do this or do I have to create the png file first and afterwards create a blob for it?
blob = new Blob([window.atob(png)]...
, how ispng
in that statement defined? – Catie