I am trying to save my map as a .png image but I am encountering some difficulties. The code I have been using is supposed to allow me to do so, but it seems like there might be something missing because I cannot save the image under a custom name.
document.getElementById('export-png').addEventListener('click', function () {
map.once('rendercomplete', function () {
var mapCanvas = document.createElement('canvas');
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
var imgName = prompt("Please provide the name", "survey_map");
navigator.msSaveBlob(mapCanvas.msToBlob(), imgName + '.png');
} else {
var link = document.getElementById('image-download');
link.href = mapCanvas.toDataURL();
link.click();
}
});
map.renderSync();
});
I tried using the prompt property to set a custom name for the image, but unfortunately, it did not work in this case.
https://i.sstatic.net/ynjSW.png
Is there a way to successfully save the .png image with a custom name?