From b395f1793d1bc50a7a9656c2a4d3a5cef1d145f3 Mon Sep 17 00:00:00 2001 From: Ian Gilman Date: Tue, 17 Mar 2015 16:43:25 -0700 Subject: [PATCH] Logarithmic option for springs; used for zoomSpring --- src/spring.js | 61 +++++++++++++++++++++++++++++++++++++++++++++---- src/viewport.js | 1 + 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/spring.js b/src/spring.js index 3320ecc2..466cf15b 100644 --- a/src/spring.js +++ b/src/spring.js @@ -72,6 +72,11 @@ $.Spring = function( options ) { }; } + if (options.log) { + this._log = true; + delete options.log; + } + $.extend( true, this, options); /** @@ -108,6 +113,12 @@ $.Spring = function( options ) { value: this.current.value, time: this.current.time }; + + if (this._log) { + this.start._logValue = Math.log(this.start.value); + this.target._logValue = Math.log(this.target.value); + this.current._logValue = Math.log(this.current.value); + } }; $.Spring.prototype = /** @lends OpenSeadragon.Spring.prototype */{ @@ -119,6 +130,12 @@ $.Spring.prototype = /** @lends OpenSeadragon.Spring.prototype */{ resetTo: function( target ) { this.start.value = this.target.value = this.current.value = target; this.start.time = this.target.time = this.current.time = $.now(); + + if (this._log) { + this.start._logValue = Math.log(this.start.value); + this.target._logValue = Math.log(this.target.value); + this.current._logValue = Math.log(this.current.value); + } }, /** @@ -130,6 +147,11 @@ $.Spring.prototype = /** @lends OpenSeadragon.Spring.prototype */{ this.start.time = this.current.time; this.target.value = target; this.target.time = this.start.time + 1000 * this.animationTime; + + if (this._log) { + this.start._logValue = Math.log(this.start.value); + this.target._logValue = Math.log(this.target.value); + } }, /** @@ -139,6 +161,21 @@ $.Spring.prototype = /** @lends OpenSeadragon.Spring.prototype */{ shiftBy: function( delta ) { this.start.value += delta; this.target.value += delta; + + if (this._log) { + this.start._logValue = Math.log(this.start.value); + this.target._logValue = Math.log(this.target.value); + } + }, + + setLog: function(value) { + this._log = value; + + if (this._log) { + this.start._logValue = Math.log(this.start.value); + this.target._logValue = Math.log(this.target.value); + this.current._logValue = Math.log(this.current.value); + } }, /** @@ -146,15 +183,31 @@ $.Spring.prototype = /** @lends OpenSeadragon.Spring.prototype */{ */ update: function() { this.current.time = $.now(); - this.current.value = (this.current.time >= this.target.time) ? - this.target.value : - this.start.value + - ( this.target.value - this.start.value ) * + + var startValue, targetValue; + if (this._log) { + startValue = this.start._logValue; + targetValue = this.target._logValue; + } else { + startValue = this.start.value; + targetValue = this.target.value; + } + + var currentValue = (this.current.time >= this.target.time) ? + targetValue : + startValue + + ( targetValue - startValue ) * transform( this.springStiffness, ( this.current.time - this.start.time ) / ( this.target.time - this.start.time ) ); + + if (this._log) { + this.current.value = Math.exp(currentValue); + } else { + this.current.value = currentValue; + } } }; diff --git a/src/viewport.js b/src/viewport.js index ff91ad29..6b246f37 100644 --- a/src/viewport.js +++ b/src/viewport.js @@ -127,6 +127,7 @@ $.Viewport = function( options ) { animationTime: this.animationTime }); this.zoomSpring = new $.Spring({ + log: true, initial: 1, springStiffness: this.springStiffness, animationTime: this.animationTime