简体   繁体   中英

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. This canvas is a map which is generated with 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. But you can create new img in currently window.

 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); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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