Test animation callback

This commit is contained in:
Tim Schaub
2016-11-06 07:53:19 -07:00
parent 3acfe79c78
commit 5fb9a2ebf8
3 changed files with 38 additions and 7 deletions

View File

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

View File

@@ -202,7 +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, complete: false,
anchor: options.anchor, anchor: options.anchor,
duration: options.duration || 1000, duration: options.duration || 1000,
easing: options.easing || ol.easing.inAndOut easing: options.easing || ol.easing.inAndOut
@@ -279,19 +279,19 @@ 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 seriesDone = true; var seriesComplete = true;
for (var j = 0, jj = series.length; j < jj; ++j) { for (var j = 0, jj = series.length; j < jj; ++j) {
var animation = series[j]; var animation = series[j];
if (animation.done) { if (animation.complete) {
continue; continue;
} }
var elapsed = now - animation.start; var elapsed = now - animation.start;
var fraction = elapsed / animation.duration; var fraction = elapsed / animation.duration;
if (fraction > 1) { if (fraction > 1) {
animation.done = true; animation.complete = true;
fraction = 1; fraction = 1;
} else { } else {
seriesDone = false; seriesComplete = false;
} }
var progress = animation.easing(fraction); var progress = animation.easing(fraction);
if (animation.sourceCenter) { if (animation.sourceCenter) {
@@ -323,7 +323,7 @@ ol.View.prototype.updateAnimations_ = function() {
} }
more = true; more = true;
} }
if (seriesDone) { if (seriesComplete) {
var completed = this.animations_.pop(); var completed = this.animations_.pop();
if (this.animations_.length === 0) { if (this.animations_.length === 0) {
this.animating_ = false; this.animating_ = false;

View File

@@ -338,7 +338,38 @@ describe('ol.View', function() {
expect(view.getAnimating()).to.eql(false); expect(view.getAnimating()).to.eql(false);
done(); done();
}, 50); }, 50);
});
it('calls a callback when animation completes', function(done) {
var view = new ol.View({
center: [0, 0],
zoom: 0
});
view.animate({
zoom: 1,
duration: 25
}, function(complete) {
expect(complete).to.be(true);
done();
});
});
it('calls callback with false when animation is interrupted', function(done) {
var view = new ol.View({
center: [0, 0],
zoom: 0
});
view.animate({
zoom: 1,
duration: 25
}, function(complete) {
expect(complete).to.be(false);
done();
});
view.setCenter([1, 2]); // interrupt the animation
}); });
}); });