removing psuedo-private accessors from Spring class

This commit is contained in:
thatcher 2011-12-29 18:16:51 -05:00
parent dc841a6294
commit 1e21c898bf
3 changed files with 116 additions and 48 deletions

View File

@ -2985,12 +2985,17 @@ $.Rect.prototype = {
}, },
getBottomRight: function() { 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() { getCenter: function() {
return new $.Point(this.x + this.width / 2.0, return new $.Point(
this.y + this.height / 2.0); this.x + this.width / 2.0,
this.y + this.height / 2.0
);
}, },
getSize: function() { getSize: function() {
@ -2998,14 +3003,21 @@ $.Rect.prototype = {
}, },
equals: function(other) { equals: function(other) {
return (other instanceof $.Rect) && return
(this.x === other.x) && (this.y === other.y) && ( other instanceof $.Rect ) &&
(this.width === other.width) && (this.height === other.height); ( this.x === other.x ) &&
( this.y === other.y ) &&
( this.width === other.width ) &&
( this.height === other.height );
}, },
toString: function() { toString: function() {
return "[" + this.x + "," + this.y + "," + this.width + "x" + return "[" +
this.height + "]"; this.x + "," +
this.y + "," +
this.width + "x" +
this.height +
"]";
} }
}; };
@ -3026,11 +3038,31 @@ $.DisplayRect.prototype.constructor = $.DisplayRect;
(function( $ ){ (function( $ ){
$.Spring = function(initialValue, config) { $.Spring = function( options ) {
this._currentValue = typeof (initialValue) == "number" ? initialValue : 0; 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._startValue = this._currentValue;
this._targetValue = this._currentValue; this._targetValue = this._currentValue;
this.config = config;
this._currentTime = new Date().getTime(); // always work in milliseconds this._currentTime = new Date().getTime(); // always work in milliseconds
this._startTime = this._currentTime; this._startTime = this._currentTime;
@ -3038,10 +3070,6 @@ $.Spring = function(initialValue, config) {
}; };
$.Spring.prototype = { $.Spring.prototype = {
_transform: function(x) {
var s = this.config.springStiffness;
return (1.0 - Math.exp(-x * s)) / (1.0 - Math.exp(-s));
},
getCurrent: function() { getCurrent: function() {
return this._currentValue; return this._currentValue;
}, },
@ -3061,7 +3089,7 @@ $.Spring.prototype = {
this._startValue = this._currentValue; this._startValue = this._currentValue;
this._startTime = this._currentTime; this._startTime = this._currentTime;
this._targetValue = target; this._targetValue = target;
this._targetTime = this._startTime + 1000 * this.config.animationTime; this._targetTime = this._startTime + 1000 * this.animationTime;
}, },
shiftBy: function(delta) { shiftBy: function(delta) {
@ -3073,10 +3101,16 @@ $.Spring.prototype = {
this._currentTime = new Date().getTime(); this._currentTime = new Date().getTime();
this._currentValue = (this._currentTime >= this._targetTime) ? this._targetValue : this._currentValue = (this._currentTime >= this._targetTime) ? this._targetValue :
this._startValue + (this._targetValue - this._startValue) * 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 )); }( OpenSeadragon ));
(function( $ ){ (function( $ ){

View File

@ -18,12 +18,17 @@ $.Rect.prototype = {
}, },
getBottomRight: function() { 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() { getCenter: function() {
return new $.Point(this.x + this.width / 2.0, return new $.Point(
this.y + this.height / 2.0); this.x + this.width / 2.0,
this.y + this.height / 2.0
);
}, },
getSize: function() { getSize: function() {
@ -31,14 +36,21 @@ $.Rect.prototype = {
}, },
equals: function(other) { equals: function(other) {
return (other instanceof $.Rect) && return
(this.x === other.x) && (this.y === other.y) && ( other instanceof $.Rect ) &&
(this.width === other.width) && (this.height === other.height); ( this.x === other.x ) &&
( this.y === other.y ) &&
( this.width === other.width ) &&
( this.height === other.height );
}, },
toString: function() { toString: function() {
return "[" + this.x + "," + this.y + "," + this.width + "x" + return "[" +
this.height + "]"; this.x + "," +
this.y + "," +
this.width + "x" +
this.height +
"]";
} }
}; };

View File

@ -1,11 +1,31 @@
(function( $ ){ (function( $ ){
$.Spring = function(initialValue, config) { $.Spring = function( options ) {
this._currentValue = typeof (initialValue) == "number" ? initialValue : 0; 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._startValue = this._currentValue;
this._targetValue = this._currentValue; this._targetValue = this._currentValue;
this.config = config;
this._currentTime = new Date().getTime(); // always work in milliseconds this._currentTime = new Date().getTime(); // always work in milliseconds
this._startTime = this._currentTime; this._startTime = this._currentTime;
@ -13,10 +33,6 @@ $.Spring = function(initialValue, config) {
}; };
$.Spring.prototype = { $.Spring.prototype = {
_transform: function(x) {
var s = this.config.springStiffness;
return (1.0 - Math.exp(-x * s)) / (1.0 - Math.exp(-s));
},
getCurrent: function() { getCurrent: function() {
return this._currentValue; return this._currentValue;
}, },
@ -36,7 +52,7 @@ $.Spring.prototype = {
this._startValue = this._currentValue; this._startValue = this._currentValue;
this._startTime = this._currentTime; this._startTime = this._currentTime;
this._targetValue = target; this._targetValue = target;
this._targetTime = this._startTime + 1000 * this.config.animationTime; this._targetTime = this._startTime + 1000 * this.animationTime;
}, },
shiftBy: function(delta) { shiftBy: function(delta) {
@ -48,8 +64,14 @@ $.Spring.prototype = {
this._currentTime = new Date().getTime(); this._currentTime = new Date().getTime();
this._currentValue = (this._currentTime >= this._targetTime) ? this._targetValue : this._currentValue = (this._currentTime >= this._targetTime) ? this._targetValue :
this._startValue + (this._targetValue - this._startValue) * 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 )); }( OpenSeadragon ));