我正在嘗試創建一個 for 回圈,在每次迭代時更新 OpenLayers 地圖的引數,一旦完成,它renderedcomplete就會提取 mapcanvas 的背景關系并將其添加到 GIF 物件中。我需要這些同步運行,以便始終允許渲染地圖和圖層,然后才添加背景關系。我目前的 hacky 解決方案是使用固定時間 setInterval,但正如這個問題中所建議的,我應該使用 async/await/Promises。我的問題是如何將我的函式包裝在 Promise 中并確保它們按順序執行,同時保持this對 vue 應用程式的背景關系 ( ) 的訪問權限?
我的 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});
}
uj5u.com熱心網友回復:
感謝 OpenLayers 的 Hocevar 先生的一些幫助(如果可以的話,我建議你在Github 贊助商上支持他)我得到了任何感興趣的人的答案。
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 事件添加 await 承諾可確保回圈等待并執行 myCallback,后者將渲染的背景關系作為幀添加到 GIF 物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/360673.html
標籤:javascript Vue.js 异步 承诺 开放层
上一篇:登錄用戶時顯示CircularProgressIndicator
下一篇:組合-合并多個共享過濾器
