Javascript canvas.toDataURL()下载大小限制
我正在尝试使用canvas.toDataURL()下载整个画布图像。图像是在画布上渲染的贴图(开放层3) 在Firefox中,我可以使用以下链接下载地图:Javascript canvas.toDataURL()下载大小限制,javascript,html,canvas,Javascript,Html,Canvas,我正在尝试使用canvas.toDataURL()下载整个画布图像。图像是在画布上渲染的贴图(开放层3) 在Firefox中,我可以使用以下链接下载地图: var exportPNGElement = document.getElementById('export_image_button'); if ('download' in exportPNGElement) { map.once('postcompose', function(event) { var canvas =
var exportPNGElement = document.getElementById('export_image_button');
if ('download' in exportPNGElement) {
map.once('postcompose', function(event) {
var canvas = event.context.canvas;
exportPNGElement.href = canvas.toDataURL('image/png');
});
map.renderSync();
} else {
alert("Sorry, something went wrong during our attempt to create the image");
}
然而,在Chrome和Opera中,我在链接上遇到了大小限制。我必须将窗口缩小,以便下载工作
浏览器之间存在大小限制差异,Chrome的限制尤其有限。这里有一篇类似的帖子(现在已经2岁多了)提出了一个广泛的服务器端解决方案:
是否有一个客户端可以解决这个问题 签出
toBlob
不过,浏览器支持不如toDataURL
那么强大。但Chrome和Firefox都有,所以它解决了你最大的问题。上面的mdn链接还有一个基于toDataURL
的polyfill,因此您可以获得最好的支持
如果您不知道,还可以使用jpeg压缩显著减小大小
exportPNGElement.href = canvas.toDataURL('image/jpeg', 0.7);
这正是我想要的。toBlob的浏览器非常有限,仅供参考。取决于IE/Safari的重要性。
exportPNGElement.href = canvas.toDataURL('image/jpeg', 0.7);