Reputation: 401
I have an OpenLayers map which has WMS layers with time steps. I am trying to create a loop which updates the time on each step and once rendered it saves the image. Currently I have taken from the official OpenLayers documentation which creates a canvas element which then is populated by the map element. I have a loop that seems to work but it keeps downloading only the first step (even though I know my update time function works).
mapToCanvasList: function () {
for(let i = 0 ; i < 10 ; i++)
{
this.renderflag = false;
this.setTimeSurface10();
this.map.renderSync();
this.myCallback();
this.map.on('rendercomplete', this.flagCallback());
if (this.renderflag) continue;
}
},
flagCallback: function () {
this.renderflag = true;
},
myCallback: function () {
this.renderflag = false
var mapCanvas = document.createElement('canvas');
var divElement = document.querySelector(".map");
mapCanvas.width = divElement.offsetWidth;//size[0];
mapCanvas.height = divElement.offsetHeight;//size[1];
var 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});
mapCanvas.remove();
this.renderflag = true;
},
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" });
}
Upvotes: 0
Views: 126
Reputation: 1421
ol-ext extension has a ol/control/VideoRecorder
to save maps as a video: https://viglino.github.io/ol-ext/examples/misc/map.control.videorecorder.html
You can dig in the code to see own it runs.
Upvotes: 1