Im using this js file to export map in Openlayers 3 in Google Chrome but i'm getting en error saying that Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
I also set an cross Origin attribute as anonymous. The function of export button is as follow
var exportPNGElement = document.getElementById('export-png');
exportPNGElement.crossOrigin = "anonymous";
if ('download' in exportPNGElement) {
exportPNGElement.addEventListener('click', function(e) {
map.once('postcompose', function(event) {
var canvas = event.context.canvas;
exportPNGElement.href = canvas.toDataURL('image/png');
});
map.renderSync();
}, false);
} else {
var info = document.getElementById('no-download');
/**
* display error message
*/
info.style.display = '';
}
asked Feb 26, 2018 at 10:00
-
This is a CORS problem of your images consisting your map. What sort of layers exist within the image you are trying to export?pavlos– pavlos2018年02月26日 13:09:15 +00:00Commented Feb 26, 2018 at 13:09
-
My layers consists of administrative boundaries and I enabled CORS extension in Chrome!Satya Chandra– Satya Chandra2018年02月26日 13:14:09 +00:00Commented Feb 26, 2018 at 13:14
-
You have to enable CORS on the server side. Enable it on client doesnt help. I am asking the type of layer , is it geoserver for example? If yes you have to enable CORS on geoserver side.pavlos– pavlos2018年02月27日 11:39:25 +00:00Commented Feb 27, 2018 at 11:39
2 Answers 2
try adding crossOrigin: 'Anonymous' to the layer sources !
Try to change it something like that:
function exportPNGElement() {
map.once('postcompose', function(event) {
var canvas = event.context.canvas;
if (navigator.msSaveBlob) {
navigator.msSaveBlob(canvas.msToBlob(), 'map.png');
} else {
canvas.toBlob(function(blob) {
saveAs(blob, 'map.png');
});
}
});
map.renderSync();
};
answered Feb 26, 2018 at 13:04
-
I tried this one also but same error..Satya Chandra– Satya Chandra2018年02月26日 13:06:44 +00:00Commented Feb 26, 2018 at 13:06
-
@Hyma I am working with this function and it works in all browsers, try to check your code. (why your canvas is tainted)Moh– Moh2018年02月26日 13:08:37 +00:00Commented Feb 26, 2018 at 13:08
lang-js