【问题标题】:OpenLayers synchronous execution through promise/await in a Vue serverless app在 Vue 无服务器应用程序中通过 promise/await 同步执行 OpenLayers
【发布时间】:2021-12-24 20:32:31
【问题描述】:

我正在尝试创建一个 for 循环,该循环在每次迭代时更新 OpenLayers 地图的参数,一旦它是renderedcomplete,它就会提取地图画布的上下文并将其添加到 GIF 对象中。我需要这些同步运行,以便始终允许渲染地图和图层,然后才能添加上下文。目前我的 hacky 解决方案是使用固定时间 setInterval,但正如 question 中所建议的那样,我应该使用 async/await/Promises。我的问题是如何将我的函数包装在 Promise 中并确保它们按顺序执行,同时保持对 vue 应用程序上下文 (this) 的访问?

我的 for 循环看起来像:

for(let i = 0 ; i < this.dateArraySurface10.length - 1 ; i++)
{
    waterfall([
        this.setTimeSurface10(),
        this.map.renderSync(),
        this.myCallback(),
    ],    
    function(err){
        console.log("Waterfall error : ",err);
    });
}

函数在哪里:

setTimeSurface10: function () {
    if (this.currentTimeSurface10 === null) {
        this.currentTimeSurface10 = this.startTimeSurface10;
    } else if (this.currentTimeSurface10 >= this.endTimeSurface10) {
        this.currentTimeSurface10 = this.startTimeSurface10;
    } else {
        this.currentTimeSurface10 = new Date(
            this.currentTimeSurface10.setMinutes(this.currentTimeSurface10.getMinutes() + 60)
        );
    }
    this.surface10.getSource().updateParams({ TIME: this.currentTimeSurface10.toISOString().split(".")[0] + "Z" });
},
myCallback: function () {
    const mapCanvas = document.createElement('canvas');
    const divElement = document.querySelector(".map");
    mapCanvas.width = divElement.offsetWidth;//size[0];
    mapCanvas.height = divElement.offsetHeight;//size[1];
    const mapContext = mapCanvas.getContext('2d');
    Array.prototype.forEach.call(
        document.querySelectorAll('.ol-layer canvas'),
        function (canvas) {
            if (canvas.width > 0) {
                const opacity = canvas.parentNode.style.opacity;
                mapContext.globalAlpha = opacity === '' ? 1 : Number(opacity);
                const transform = canvas.style.transform;
                const matrix = transform
                                        .match(/^matrix\(([^\(]*)\)$/)[1] //eslint-disable-line
                                        .split(',')
                                        .map(Number);
                CanvasRenderingContext2D.prototype.setTransform.apply(mapContext,matrix);
                mapContext.drawImage(canvas, 0, 0);
            }
        }
    );
    this.gif.addFrame(mapCanvas, {copy:true, delay: 200});
}

【问题讨论】:

    标签: javascript vue.js asynchronous promise openlayers


    【解决方案1】:

    感谢 OpenLayers 的 Hocevar 先生的帮助(如果可以的话,我建议您在 Github Sponsor 上支持他)我得到了任何感兴趣的人的答案。

    async mapToCanvasList() {
        for(let i = 0 ; i < this.dateArraySurface10.length - 1 ; i++)
        {
            this.setTimeSurface10();
            await new Promise(resolve => this.map.once('rendercomplete', resolve));
            this.myCallback();
        }
        this.gif.on('finished', function(blob) {
            window.open(URL.createObjectURL(blob));
        });
        this.gif.render();
    },
    myCallback: function () {
        const mapCanvas = document.createElement('canvas');
        const divElement = document.querySelector(".map");
        mapCanvas.width = divElement.offsetWidth;//size[0];
        mapCanvas.height = divElement.offsetHeight;//size[1];
        const mapContext = mapCanvas.getContext('2d');
        Array.prototype.forEach.call(
            document.querySelectorAll('.ol-layer canvas'),
            function (canvas) {
                if (canvas.width > 0) {
                    const opacity = canvas.parentNode.style.opacity;
                    mapContext.globalAlpha = opacity === '' ? 1 : Number(opacity);
                    const transform = canvas.style.transform;
                    const matrix = transform
                                            .match(/^matrix\(([^\(]*)\)$/)[1] //eslint-disable-line
                                            .split(',')
                                            .map(Number);
                    CanvasRenderingContext2D.prototype.setTransform.apply(mapContext,matrix);
                    mapContext.drawImage(canvas, 0, 0);
                }
            }
        );
        this.gif.addFrame(mapCanvas, {copy:true, delay: 200});
    },
    

    如您所见,异步渲染整个方法并为 rendercomplete 事件添加等待承诺可确保循环等待并执行 myCallback,将渲染的上下文作为帧添加到 GIF 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多