mapbox-gl
Version:
A WebGL interactive maps library
74 lines (55 loc) • 1.97 kB
JavaScript
'use strict';
var util = require('../util/util');
var interpolate = require('../util/interpolate');
module.exports = StyleTransition;
/*
* Represents a transition between two declarations
*/
function StyleTransition(declaration, oldTransition, value) {
this.declaration = declaration;
this.startTime = this.endTime = (new Date()).getTime();
var type = declaration.type;
if ((type === 'string' || type === 'array') && declaration.transitionable) {
this.interp = interpZoomTransitioned;
} else {
this.interp = interpolate[type];
}
this.oldTransition = oldTransition;
this.duration = value.duration || 0;
this.delay = value.delay || 0;
if (!this.instant()) {
this.endTime = this.startTime + this.duration + this.delay;
this.ease = util.easeCubicInOut;
}
if (oldTransition && oldTransition.endTime <= this.startTime) {
// Old transition is done running, so we can
// delete its reference to its old transition.
delete oldTransition.oldTransition;
}
}
StyleTransition.prototype.instant = function() {
return !this.oldTransition || !this.interp || (this.duration === 0 && this.delay === 0);
};
/*
* Return the value of the transitioning property at zoom level `z` and optional time `t`
*/
StyleTransition.prototype.at = function(z, zoomHistory, t) {
var value = this.declaration.calculate(z, zoomHistory, this.duration);
if (this.instant()) return value;
t = t || Date.now();
if (t < this.endTime) {
var oldValue = this.oldTransition.at(z, zoomHistory, this.startTime);
var eased = this.ease((t - this.startTime - this.delay) / this.duration);
value = this.interp(oldValue, value, eased);
}
return value;
};
function interpZoomTransitioned(from, to, t) {
return {
from: from.to,
fromScale: from.toScale,
to: to.to,
toScale: to.toScale,
t: t
};
}