0

I would like to set an option for exporting my OpenLayers map as the .png file.

I used this example:

https://openlayers.org/en/master/examples/export-map.html

from where I retrieved all relevant coding and links.

My situation looks like this:

HTML

   <button type="button" class="saveBtn"><a id="export-png" class="btn btn-default"><i 
   class="fa fa-download"></i> Download PNG</a>
   <a id="image-download" download="map.png"></a></button>

JS

 document.getElementById('export-png').addEventListener('click', function () {
  map.once('rendercomplete', function () {
   var mapCanvas = document.createElement('canvas');
   //var img = new Image();
   //img.crossOrigin = "anonymous";
    var size = map.getSize();
    mapCanvas.width = size[0];
    mapCanvas.height = size[1];
    var mapContext = mapCanvas.getContext('2d');
    Array.prototype.forEach.call(
    document.querySelectorAll('.ol-layer canvas'),
    function (canvas) {
    if (canvas.width > 0) {
      var opacity = canvas.parentNode.style.opacity;
      mapContext.globalAlpha = opacity === '' ? 1 : Number(opacity);
      var transform = canvas.style.transform;
      // Get the transform parameters from the style's transform matrix
      var matrix = transform
        .match(/^matrix\(([^\(]*)\)$/)[1]
        .split(',')
        .map(Number);
      // Apply the transform to the export map context
      CanvasRenderingContext2D.prototype.setTransform.apply(
        mapContext,
        matrix
        );
        mapContext.drawImage(canvas, 0, 0);
      }
    }
   );
   if (navigator.msSaveBlob) {
    // link download attribuute does not work on MS browsers
    navigator.msSaveBlob(mapCanvas.msToBlob(), 'map.png');
  } else {
  var link = document.getElementById('image-download');
  link.href = mapCanvas.toDataURL();
  link.click();
   }
  });
  map.renderSync();
 });

The console says nothing. There is no reaction after clicking the button. enter image description here What is missing here?

See my full situation here:

https://mlearnweb.online/

Geographos
  • 827
  • 2
  • 23
  • 57
  • you can not export tainted canvases https://stackoverflow.com/questions/22710627/tainted-canvases-may-not-be-exported – Sebastian Nov 18 '21 at 15:19
  • Yes, I have this error in my console, but offline only. When I upload it to the server the error is gone. How they can export the map then? (from the original link attached?). Is there any other solution for getting the map exported as .pdf? – Geographos Nov 18 '21 at 15:23
  • are all layers hosted on your server? then you maybe have to adjust the crossorigin. maybe you can turn off the layers one by one to identify which one should not be exported – Sebastian Nov 18 '21 at 15:34
  • Yes the layers are on my server, only map tiles are external. How to define the crossorigin for them? – Geographos Nov 18 '21 at 15:35
  • var baseLayer = new ol.layer.Tile({ name: 'basic', source: new ol.source.XYZ({ url: options.baseMap.basic, crossOrigin: "Anonymous" }) }); – Sebastian Nov 18 '21 at 15:36
  • does it apply only to base layers|? – Geographos Nov 18 '21 at 16:08
  • sorry for my bad example, you can use this in all non-vector layers like here https://stackoverflow.com/questions/66671183/how-to-export-map-image-in-openlayer-6-without-cors-problems-tainted-canvas-iss – Sebastian Nov 19 '21 at 07:15

0 Answers0