Ensure that all animations complete

This commit is contained in:
Tim Schaub
2016-11-05 17:31:49 -06:00
parent 3d26d3bdc6
commit c452164b8a
3 changed files with 64 additions and 10 deletions

View File

@@ -665,6 +665,7 @@ ol.TransformFunction;
* rotationAnchor: (ol.Coordinate|undefined), * rotationAnchor: (ol.Coordinate|undefined),
* start: number, * start: number,
* duration: number, * duration: number,
* done: boolean,
* easing: function(number):number, * easing: function(number):number,
* callback: (function(boolean)|undefined) * callback: (function(boolean)|undefined)
* }} * }}

View File

@@ -202,6 +202,7 @@ ol.View.prototype.animate = function(var_args) {
var animation = /** @type {ol.ViewAnimation} */ ({ var animation = /** @type {ol.ViewAnimation} */ ({
start: start, start: start,
done: false,
duration: options.duration || 1000, duration: options.duration || 1000,
easing: options.easing || ol.easing.inAndOut easing: options.easing || ol.easing.inAndOut
}); });
@@ -278,16 +279,21 @@ ol.View.prototype.updateAnimations_ = function() {
var more = false; var more = false;
for (var i = this.animations_.length - 1; i >= 0; --i) { for (var i = this.animations_.length - 1; i >= 0; --i) {
var series = this.animations_[i]; var series = this.animations_[i];
var animation; var seriesDone = true;
for (var j = 0, jj = series.length; j < jj; ++j) { for (var j = 0, jj = series.length; j < jj; ++j) {
var candidate = series[i]; var animation = series[j];
if (candidate.duration > now - candidate.start) { if (animation.done) {
animation = candidate; continue;
break;
} }
} var elapsed = now - animation.start;
if (animation) { var fraction = elapsed / animation.duration;
var progress = animation.easing((now - animation.start) / animation.duration); if (fraction > 1) {
animation.done = true;
fraction = 1;
} else {
seriesDone = false;
}
var progress = animation.easing(fraction);
if (animation.sourceCenter) { if (animation.sourceCenter) {
var x0 = animation.sourceCenter[0]; var x0 = animation.sourceCenter[0];
var y0 = animation.sourceCenter[1]; var y0 = animation.sourceCenter[1];
@@ -313,11 +319,16 @@ ol.View.prototype.updateAnimations_ = function() {
} }
} }
more = true; more = true;
} else { }
this.animations_.pop(); if (seriesDone) {
var completed = this.animations_.pop();
if (this.animations_.length === 0) { if (this.animations_.length === 0) {
this.animating_ = false; this.animating_ = false;
} }
var callback = completed[0].callback;
if (callback) {
callback(true);
}
} }
} }
if (more && this.updateAnimationKey_ === undefined) { if (more && this.updateAnimationKey_ === undefined) {

View File

@@ -301,6 +301,48 @@ describe('ol.View', function() {
}); });
describe('#animate()', function() {
var originalRequestAnimationFrame = window.requestAnimationFrame;
var originalCancelAnimationFrame = window.cancelAnimationFrame;
beforeEach(function() {
window.requestAnimationFrame = function(callback) {
return setTimeout(callback, 1);
};
window.cancelAnimationFrame = function(key) {
return clearTimeout(key);
};
});
afterEach(function() {
window.requestAnimationFrame = originalRequestAnimationFrame;
window.cancelAnimationFrame = originalCancelAnimationFrame;
});
it('can be called to animate view properties', function(done) {
var view = new ol.View({
center: [0, 0],
zoom: 5
});
view.animate({
zoom: 4,
duration: 25
});
expect(view.getAnimating()).to.eql(true);
setTimeout(function() {
expect(view.getCenter()).to.eql([0, 0]);
expect(view.getZoom()).to.eql(4);
expect(view.getAnimating()).to.eql(false);
done();
}, 50);
});
});
describe('#getResolutions', function() { describe('#getResolutions', function() {
var view; var view;
var resolutions = [512, 256, 128, 64, 32, 16]; var resolutions = [512, 256, 128, 64, 32, 16];