简体   繁体   English

如何将 vuelayers map 导出为 png 或 jpeg?

[英]How to export vuelayers map to png or jpeg?

How would I adapt @ghettovoice JSFiddle that saves a map to PDF to save the map to a JPEG or PNG?我将如何调整将map保存到 PDF 的 @ghettovoice JSFiddle 以将 map 保存到 JPEG 或 PNG? I have no idea how to attempt this problem so ideally if you know hoe to do it you can explain the logic behind it.我不知道如何完美地尝试这个问题,如果你知道如何去做,你可以解释它背后的逻辑。

        exportMap: function () {
            var map = this.$refs.map

            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
                navigator.msSaveBlob(mapCanvas.msToBlob(), 'map.png');
                } else {
                var link = document.getElementById('image-download');
                link.href = mapCanvas.toDataURL();
                link.click();
                }
            });
            map.renderSync();

        }

The problem was a combination of missing dependencies (namely FileSaver.js and fakerator.js) and a cross origin server block (CORS block) (Browsers automatically prevent httpRequests to a different domain name unless the server allows it).问题是缺少依赖项(即 FileSaver.js 和 fakerator.js)和跨源服务器块(CORS 块)(浏览器自动阻止 httpRequests 到不同的域名,除非服务器允许)的组合。 The first one is fixed by installing the packages while the second one is resolved by setting the crossOrigin Attribute of the ImageWMSLayer to null in my case but possibly to 'Anonymous' for other sources.第一个是通过安装软件包来解决的,而第二个是通过将null的 crossOrigin 属性设置为 null 来解决的,但对于其他来源可能设置为“匿名”。 Hope this helped someone else:)希望这对其他人有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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