|
@@ -19,6 +19,10 @@ function lerp(from, to, p) {
|
|
|
|
|
|
function ease(v) { return v * v * (3 - 2 * v); }
|
|
function ease(v) { return v * v * (3 - 2 * v); }
|
|
|
|
|
|
|
|
+function easeLinear(t, b, c, d) {
|
|
|
|
+ return lerp(b, c, t/d);
|
|
|
|
+}
|
|
|
|
+
|
|
function easeOutExpo(t, b, c, d) {
|
|
function easeOutExpo(t, b, c, d) {
|
|
return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
|
|
return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
|
|
}
|
|
}
|
|
@@ -26,3 +30,147 @@ function easeOutExpo(t, b, c, d) {
|
|
function color(r,g,b,a) {
|
|
function color(r,g,b,a) {
|
|
return 'rgba('+r+','+g+','+b+','+a.toFixed(5)+')';
|
|
return 'rgba('+r+','+g+','+b+','+a.toFixed(5)+')';
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// Ticker
|
|
|
|
+var Ticker = {};
|
|
|
|
+(function(Ticker) {
|
|
|
|
+ var listeners = [];
|
|
|
|
+
|
|
|
|
+ Ticker.addListener = function(listener) {
|
|
|
|
+ listeners.push(listener);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Ticker.removeListener = function(listener) {
|
|
|
|
+ var index = listeners.indexOf(listener);
|
|
|
|
+ listeners.splice(index, 1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var nextFrame =
|
|
|
|
+ window.requestAnimationFrame ||
|
|
|
|
+ window.webkitRequestAnimationFrame ||
|
|
|
|
+ window.mozRequestAnimationFrame ||
|
|
|
|
+ function (callback) {
|
|
|
|
+ setTimeout(function () {
|
|
|
|
+ callback(Date.now())
|
|
|
|
+ }, 10)
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ var then = Date.now();
|
|
|
|
+ function tick(now) {
|
|
|
|
+ var delta = (now - then) / 1000;
|
|
|
|
+ listeners.forEach(function(listener){
|
|
|
|
+ listener(delta);
|
|
|
|
+ })
|
|
|
|
+ then = now;
|
|
|
|
+ nextFrame(tick);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Ticker.start = function() {
|
|
|
|
+ nextFrame(tick);
|
|
|
|
+ }
|
|
|
|
+})(Ticker);
|
|
|
|
+
|
|
|
|
+// Tweens
|
|
|
|
+function BaseTween() {}
|
|
|
|
+BaseTween.prototype.animate = function() {
|
|
|
|
+ var self = this;
|
|
|
|
+ function tick(dt) {
|
|
|
|
+ self.update(dt);
|
|
|
|
+ }
|
|
|
|
+ Ticker.addListener(tick);
|
|
|
|
+ self.promise.then(function() {
|
|
|
|
+ Ticker.removeListener(tick);
|
|
|
|
+ });
|
|
|
|
+ self.update(0);
|
|
|
|
+ return self;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+function Tween(duration, start, end, easing) {
|
|
|
|
+ this.duration = duration;
|
|
|
|
+ this.start = start;
|
|
|
|
+ this.end = end;
|
|
|
|
+ this.easing = easing || easeLinear;
|
|
|
|
+ this.deferred = Q.defer();
|
|
|
|
+ this.timer = 0;
|
|
|
|
+ this.promise = this.deferred.promise;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Tween.prototype = new BaseTween();
|
|
|
|
+
|
|
|
|
+Tween.prototype.update = function(dt) {
|
|
|
|
+ this.timer += dt;
|
|
|
|
+ if(this.timer >= this.duration) {
|
|
|
|
+ this.timer = this.duration;
|
|
|
|
+ this.deferred.resolve();
|
|
|
|
+ }
|
|
|
|
+ this.value = this.easing(this.timer, this.start, this.end, this.duration);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+function PromiseTween(value, promise) {
|
|
|
|
+ this.value = value;
|
|
|
|
+ this.promise = promise;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+PromiseTween.prototype.update = function() {};
|
|
|
|
+
|
|
|
|
+function ComposedTween(tweens) {
|
|
|
|
+ this.tweens = tweens;
|
|
|
|
+ this.promise = Q.all(this.tweens.map(function(t) { return t.promise }));
|
|
|
|
+ this.switch(0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+ComposedTween.prototype = new BaseTween();
|
|
|
|
+
|
|
|
|
+ComposedTween.prototype.switch = function(index) {
|
|
|
|
+ if(index >= this.tweens.length) return;
|
|
|
|
+ var self = this;
|
|
|
|
+ this.current = index;
|
|
|
|
+ this.tweens[index].promise.then(function() {
|
|
|
|
+ self.switch(index + 1);
|
|
|
|
+ });
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+ComposedTween.prototype.update = function(dt) {
|
|
|
|
+ if(this.current >= this.tweens.length) return;
|
|
|
|
+ var active = this.tweens[this.current];
|
|
|
|
+ active.update(dt);
|
|
|
|
+ this.value = active.value;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+function DeferredTween(start) {
|
|
|
|
+ this.lastTarget = start;
|
|
|
|
+ this.tweens = [];
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+DeferredTween.prototype = new BaseTween();
|
|
|
|
+
|
|
|
|
+DeferredTween.prototype.addTween = function(tween) {
|
|
|
|
+ this.tweens.push(tween);
|
|
|
|
+ this.tween = new ComposedTween(this.tweens);
|
|
|
|
+ this.promise = this.tween.promise;
|
|
|
|
+ return this;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+DeferredTween.prototype.to = function(target, duration, easing) {
|
|
|
|
+ easing = easing || easeLinear;
|
|
|
|
+ this.addTween(new Tween(duration, this.lastTarget, target, easing));
|
|
|
|
+ this.lastTarget = target;
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+DeferredTween.prototype.wait = function(duration) {
|
|
|
|
+ return this.to(this.lastTarget, duration);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+DeferredTween.prototype.waitFor = function(promise) {
|
|
|
|
+ return this.addTween(new PromiseTween(this.lastTarget, promise));
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+DeferredTween.prototype.update = function(dt) {
|
|
|
|
+ this.tween.update(dt);
|
|
|
|
+ this.value = this.tween.value;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Tween.from = function(start) {
|
|
|
|
+ return new DeferredTween(start);
|
|
|
|
+};
|