3

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 = '';
 }
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Feb 26, 2018 at 10:00
3
  • This is a CORS problem of your images consisting your map. What sort of layers exist within the image you are trying to export? Commented Feb 26, 2018 at 13:09
  • My layers consists of administrative boundaries and I enabled CORS extension in Chrome! Commented 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. Commented Feb 27, 2018 at 11:39

2 Answers 2

2

try adding crossOrigin: 'Anonymous' to the layer sources !

answered Mar 25, 2019 at 10:50
0
0

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
2
  • I tried this one also but same error.. Commented 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) Commented Feb 26, 2018 at 13:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.