简体   繁体   English

使用Cross doimain用javascript将画布另存为png

[英]Save canvas as png locally with javascript with Cross doimain

I want to save an image of the canvas tag, but when i try create an image from the canvas it tells me "SecurityError: The operation is insecure", which i believe is problems with the canvas coming from another domain than what im on. 我想保存canvas标记的图像,但是当我尝试从画布创建图像时,它告诉我“ SecurityError:操作不安全”,我认为这是画布来自其他域的问题,而不是即时消息。 This canvas is a map which is generated with openlayers3. 此画布是使用openlayers3生成的地图。

var canvas = document.getElementsByClassName('ol-unselectable')[0];
var dataURL = canvas.toDataURL('image/png');
var window = window.open();
window.document.write('<img src='+dataURL+'/>');

I've also tried 我也尝试过

canvas.toBlob(function(blob) {
saveAs(blob, 'map.png')
}

Is there an easy work around so the canvas is not tainted? 是否有容易解决的问题,以使画布不被污染?

You tried write in new browser window from another window this is impossible without https protocol or localhost. 您尝试从另一个窗口在新的浏览器窗口中写入,如果没有https协议或本地主机,则无法实现。 But you can create new img in currently window. 但是您可以在当前窗口中创建新的img。

 var canvas = document.getElementsByClassName('ol-unselectable')[0]; var dataURL = canvas.toDataURL('image/png'); var img = new Image(); img.width = 1000; img.height = 1000; img.src = dataURL; img.onload = function () { document.body.append(img); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM