how to save canvas as png image?

try this:

var canvas = document.getElementById("alpha");
var dataURL = canvas.toDataURL("image/png");
var newTab = window.open('about:blank','image from canvas');
newTab.document.write("<img src="https://stackoverflow.com/questions/11112321/" + dataURL + "" alt="from canvas"/>");

This shows image from canvas on new page, but if you have open popup in new tab setting it shows about:blank in address bar.

EDIT:- though window.open("<img src="https://stackoverflow.com/questions/11112321/"+ canvas.toDataURL("image/png') +"'/>") does not work in FF or Chrome, following works though rendering is somewhat different from what is shown on canvas, I think transparency is the issue:

window.open(canvas.toDataURL('image/png'));

Leave a Comment