且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将画布保存为png图像?

更新时间:2022-02-18 22:03:19

尝试:

var c=document.getElementById("alpha");
var d=c.toDataURL("image/png");
var w=window.open('about:blank','image from canvas');
w.document.write("<img src='"+d+"' alt='from canvas'/>");

这会在新页面上显示画布上的图片,但如果您有在新标签中设置它在地址栏中显示 about:blank

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.

- 虽然 window.open(< img src ='+ c.toDataURL('image / png')+'/>) FF或Chrome,下面的工作虽然渲染有点不同于在画布上显示,我认为透明度是问题:

- though window.open("<img src='"+ c.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(c.toDataURL('image/png'));