From 1e21c898bfb2c9f28173aad152c83bf478d9d2f6 Mon Sep 17 00:00:00 2001 From: thatcher Date: Thu, 29 Dec 2011 18:16:51 -0500 Subject: [PATCH] removing psuedo-private accessors from Spring class --- openseadragon.js | 82 ++++++++++++++++++++++++++++++++++-------------- src/rectangle.js | 42 ++++++++++++++++--------- src/spring.js | 40 +++++++++++++++++------ 3 files changed, 116 insertions(+), 48 deletions(-) diff --git a/openseadragon.js b/openseadragon.js index 3c5f080c..2b3aeb9b 100644 --- a/openseadragon.js +++ b/openseadragon.js @@ -2968,11 +2968,11 @@ $.ButtonGroup.prototype = { (function( $ ){ -$.Rect = function(x, y, width, height) { - this.x = typeof (x) == "number" ? x : 0; - this.y = typeof (y) == "number" ? y : 0; - this.width = typeof (width) == "number" ? width : 0; - this.height = typeof (height) == "number" ? height : 0; +$.Rect = function( x, y, width, height ) { + this.x = typeof ( x ) == "number" ? x : 0; + this.y = typeof ( y ) == "number" ? y : 0; + this.width = typeof ( width ) == "number" ? width : 0; + this.height = typeof ( height ) == "number" ? height : 0; }; $.Rect.prototype = { @@ -2981,31 +2981,43 @@ $.Rect.prototype = { }, getTopLeft: function() { - return new $.Point(this.x, this.y); + return new $.Point( this.x, this.y ); }, getBottomRight: function() { - return new $.Point(this.x + this.width, this.y + this.height); + return new $.Point( + this.x + this.width, + this.y + this.height + ); }, getCenter: function() { - return new $.Point(this.x + this.width / 2.0, - this.y + this.height / 2.0); + return new $.Point( + this.x + this.width / 2.0, + this.y + this.height / 2.0 + ); }, getSize: function() { - return new $.Point(this.width, this.height); + return new $.Point( this.width, this.height ); }, equals: function(other) { - return (other instanceof $.Rect) && - (this.x === other.x) && (this.y === other.y) && - (this.width === other.width) && (this.height === other.height); + return + ( other instanceof $.Rect ) && + ( this.x === other.x ) && + ( this.y === other.y ) && + ( this.width === other.width ) && + ( this.height === other.height ); }, toString: function() { - return "[" + this.x + "," + this.y + "," + this.width + "x" + - this.height + "]"; + return "[" + + this.x + "," + + this.y + "," + + this.width + "x" + + this.height + + "]"; } }; @@ -3026,11 +3038,31 @@ $.DisplayRect.prototype.constructor = $.DisplayRect; (function( $ ){ -$.Spring = function(initialValue, config) { - this._currentValue = typeof (initialValue) == "number" ? initialValue : 0; +$.Spring = function( options ) { + var args = arguments; + + if( typeof( options ) != 'object' ){ + //allows backward compatible use of ( initialValue, config ) as + //constructor parameters + options = { + initial: args.length && typeof ( args[ 0 ] ) == "number" ? + args[ 0 ] : + 0, + springStiffness: args.length > 1 ? + args[ 1 ].springStiffness : + 5.0, + animationTime: args.length > 1 ? + args[ 1 ].animationTime : + 1.5, + }; + } + + $.extend( true, this, options ); + + + this._currentValue = typeof ( this.initial ) == "number" ? this.initial : 0; this._startValue = this._currentValue; this._targetValue = this._currentValue; - this.config = config; this._currentTime = new Date().getTime(); // always work in milliseconds this._startTime = this._currentTime; @@ -3038,10 +3070,6 @@ $.Spring = function(initialValue, config) { }; $.Spring.prototype = { - _transform: function(x) { - var s = this.config.springStiffness; - return (1.0 - Math.exp(-x * s)) / (1.0 - Math.exp(-s)); - }, getCurrent: function() { return this._currentValue; }, @@ -3061,7 +3089,7 @@ $.Spring.prototype = { this._startValue = this._currentValue; this._startTime = this._currentTime; this._targetValue = target; - this._targetTime = this._startTime + 1000 * this.config.animationTime; + this._targetTime = this._startTime + 1000 * this.animationTime; }, shiftBy: function(delta) { @@ -3073,10 +3101,16 @@ $.Spring.prototype = { this._currentTime = new Date().getTime(); this._currentValue = (this._currentTime >= this._targetTime) ? this._targetValue : this._startValue + (this._targetValue - this._startValue) * - this._transform((this._currentTime - this._startTime) / (this._targetTime - this._startTime)); + transform( this.springStiffness, (this._currentTime - this._startTime) / (this._targetTime - this._startTime)); } } + +function transform( stiffness, x ) { + return ( 1.0 - Math.exp( stiffness * -x ) ) / + ( 1.0 - Math.exp( -stiffness ) ); +}; + }( OpenSeadragon )); (function( $ ){ diff --git a/src/rectangle.js b/src/rectangle.js index 37d79152..5dd91d18 100644 --- a/src/rectangle.js +++ b/src/rectangle.js @@ -1,11 +1,11 @@ (function( $ ){ -$.Rect = function(x, y, width, height) { - this.x = typeof (x) == "number" ? x : 0; - this.y = typeof (y) == "number" ? y : 0; - this.width = typeof (width) == "number" ? width : 0; - this.height = typeof (height) == "number" ? height : 0; +$.Rect = function( x, y, width, height ) { + this.x = typeof ( x ) == "number" ? x : 0; + this.y = typeof ( y ) == "number" ? y : 0; + this.width = typeof ( width ) == "number" ? width : 0; + this.height = typeof ( height ) == "number" ? height : 0; }; $.Rect.prototype = { @@ -14,31 +14,43 @@ $.Rect.prototype = { }, getTopLeft: function() { - return new $.Point(this.x, this.y); + return new $.Point( this.x, this.y ); }, getBottomRight: function() { - return new $.Point(this.x + this.width, this.y + this.height); + return new $.Point( + this.x + this.width, + this.y + this.height + ); }, getCenter: function() { - return new $.Point(this.x + this.width / 2.0, - this.y + this.height / 2.0); + return new $.Point( + this.x + this.width / 2.0, + this.y + this.height / 2.0 + ); }, getSize: function() { - return new $.Point(this.width, this.height); + return new $.Point( this.width, this.height ); }, equals: function(other) { - return (other instanceof $.Rect) && - (this.x === other.x) && (this.y === other.y) && - (this.width === other.width) && (this.height === other.height); + return + ( other instanceof $.Rect ) && + ( this.x === other.x ) && + ( this.y === other.y ) && + ( this.width === other.width ) && + ( this.height === other.height ); }, toString: function() { - return "[" + this.x + "," + this.y + "," + this.width + "x" + - this.height + "]"; + return "[" + + this.x + "," + + this.y + "," + + this.width + "x" + + this.height + + "]"; } }; diff --git a/src/spring.js b/src/spring.js index 988b6c4e..608138b9 100644 --- a/src/spring.js +++ b/src/spring.js @@ -1,11 +1,31 @@ (function( $ ){ -$.Spring = function(initialValue, config) { - this._currentValue = typeof (initialValue) == "number" ? initialValue : 0; +$.Spring = function( options ) { + var args = arguments; + + if( typeof( options ) != 'object' ){ + //allows backward compatible use of ( initialValue, config ) as + //constructor parameters + options = { + initial: args.length && typeof ( args[ 0 ] ) == "number" ? + args[ 0 ] : + 0, + springStiffness: args.length > 1 ? + args[ 1 ].springStiffness : + 5.0, + animationTime: args.length > 1 ? + args[ 1 ].animationTime : + 1.5, + }; + } + + $.extend( true, this, options ); + + + this._currentValue = typeof ( this.initial ) == "number" ? this.initial : 0; this._startValue = this._currentValue; this._targetValue = this._currentValue; - this.config = config; this._currentTime = new Date().getTime(); // always work in milliseconds this._startTime = this._currentTime; @@ -13,10 +33,6 @@ $.Spring = function(initialValue, config) { }; $.Spring.prototype = { - _transform: function(x) { - var s = this.config.springStiffness; - return (1.0 - Math.exp(-x * s)) / (1.0 - Math.exp(-s)); - }, getCurrent: function() { return this._currentValue; }, @@ -36,7 +52,7 @@ $.Spring.prototype = { this._startValue = this._currentValue; this._startTime = this._currentTime; this._targetValue = target; - this._targetTime = this._startTime + 1000 * this.config.animationTime; + this._targetTime = this._startTime + 1000 * this.animationTime; }, shiftBy: function(delta) { @@ -48,8 +64,14 @@ $.Spring.prototype = { this._currentTime = new Date().getTime(); this._currentValue = (this._currentTime >= this._targetTime) ? this._targetValue : this._startValue + (this._targetValue - this._startValue) * - this._transform((this._currentTime - this._startTime) / (this._targetTime - this._startTime)); + transform( this.springStiffness, (this._currentTime - this._startTime) / (this._targetTime - this._startTime)); } } + +function transform( stiffness, x ) { + return ( 1.0 - Math.exp( stiffness * -x ) ) / + ( 1.0 - Math.exp( -stiffness ) ); +}; + }( OpenSeadragon ));