damping-tween
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -38,6 +38,15 @@ /*! | ||
} | ||
get value() { | ||
return this._currentValues[this._keys[0]]; | ||
} | ||
get endValue() { | ||
return this._endValues[this._keys[0]]; | ||
} | ||
setValues(values, immediate = false) { | ||
this._keys.forEach((key) => this.setValue(key, values[key], immediate)); | ||
this._keys.forEach((key) => this.setValueByKey(key, values[key], immediate)); | ||
} | ||
setValue(key, value, immediate = false) { | ||
setValue(value, immediate = false) { | ||
this.setValueByKey(this._keys[0], value, immediate); | ||
} | ||
setValueByKey(key, value, immediate = false) { | ||
if (!isNumber(this._endValues[key])) | ||
@@ -55,14 +64,2 @@ return; | ||
} | ||
get value() { | ||
return this._currentValues[this._keys[0]]; | ||
} | ||
get endValue() { | ||
return this._endValues[this._keys[0]]; | ||
} | ||
set value(value) { | ||
this.setValue(this._keys[0], value); | ||
} | ||
set endValue(value) { | ||
this.setValue(this._keys[0], value); | ||
} | ||
stop() { | ||
@@ -69,0 +66,0 @@ this.setValues(this._currentValues, true); |
@@ -7,2 +7,2 @@ /*! | ||
*/ | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.DampingTween=factory())})(this,(function(){"use strict";const FPS_60=1/.016;class DampingTween{constructor(values,dampingFactor=.1){this.active=false;this.dampingFactor=dampingFactor;const _values=isNumber(values)?{value:values}:Object.keys(values).reduce(((__values,key)=>{if(!isNumber(values[key]))return __values;__values[key]=values[key];return __values}),{});this._keys=Object.keys(_values);this._currentValues=Object.assign({},_values);this._endValues=Object.assign({},_values);this._deltaValues=this._keys.reduce(((__values,key)=>{__values[key]=0;return __values}),{})}get values(){return Object.assign({},this._currentValues)}get endValues(){return Object.assign({},this._endValues)}setValues(values,immediate=false){this._keys.forEach((key=>this.setValue(key,values[key],immediate)))}setValue(key,value,immediate=false){if(!isNumber(this._endValues[key]))return;if(approxEquals(value,this._endValues[key]))return;if(immediate&&value===this._currentValues[key]&&value===this._endValues[key])return;this._endValues[key]=value;if(immediate){this._currentValues[key]=value}this.active=true}get value(){return this._currentValues[this._keys[0]]}get endValue(){return this._endValues[this._keys[0]]}set value(value){this.setValue(this._keys[0],value)}set endValue(value){this.setValue(this._keys[0],value)}stop(){this.setValues(this._currentValues,true)}update(delta){const shouldUpdate=this.active;const lerpRatio=1-Math.exp(-this.dampingFactor*delta*FPS_60);this._keys.forEach((key=>this._deltaValues[key]=this._endValues[key]-this._currentValues[key]));const needsUpdate=this._keys.some((key=>!approxZero(this._deltaValues[key])));if(needsUpdate){this._keys.forEach((key=>{this._currentValues[key]+=this._deltaValues[key]*lerpRatio}));this.active=true}else{Object.assign(this._currentValues,this._endValues);this.active=false}return shouldUpdate}}function isNumber(value){return typeof value==="number"&&isFinite(value)}const EPSILON=1e-5;function approxZero(number){return Math.abs(number)<EPSILON}function approxEquals(a,b){return approxZero(a-b)}return DampingTween})); | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.DampingTween=factory())})(this,(function(){"use strict";const FPS_60=1/.016;class DampingTween{constructor(values,dampingFactor=.1){this.active=false;this.dampingFactor=dampingFactor;const _values=isNumber(values)?{value:values}:Object.keys(values).reduce(((__values,key)=>{if(!isNumber(values[key]))return __values;__values[key]=values[key];return __values}),{});this._keys=Object.keys(_values);this._currentValues=Object.assign({},_values);this._endValues=Object.assign({},_values);this._deltaValues=this._keys.reduce(((__values,key)=>{__values[key]=0;return __values}),{})}get values(){return Object.assign({},this._currentValues)}get endValues(){return Object.assign({},this._endValues)}get value(){return this._currentValues[this._keys[0]]}get endValue(){return this._endValues[this._keys[0]]}setValues(values,immediate=false){this._keys.forEach((key=>this.setValueByKey(key,values[key],immediate)))}setValue(value,immediate=false){this.setValueByKey(this._keys[0],value,immediate)}setValueByKey(key,value,immediate=false){if(!isNumber(this._endValues[key]))return;if(approxEquals(value,this._endValues[key]))return;if(immediate&&value===this._currentValues[key]&&value===this._endValues[key])return;this._endValues[key]=value;if(immediate){this._currentValues[key]=value}this.active=true}stop(){this.setValues(this._currentValues,true)}update(delta){const shouldUpdate=this.active;const lerpRatio=1-Math.exp(-this.dampingFactor*delta*FPS_60);this._keys.forEach((key=>this._deltaValues[key]=this._endValues[key]-this._currentValues[key]));const needsUpdate=this._keys.some((key=>!approxZero(this._deltaValues[key])));if(needsUpdate){this._keys.forEach((key=>{this._currentValues[key]+=this._deltaValues[key]*lerpRatio}));this.active=true}else{Object.assign(this._currentValues,this._endValues);this.active=false}return shouldUpdate}}function isNumber(value){return typeof value==="number"&&isFinite(value)}const EPSILON=1e-5;function approxZero(number){return Math.abs(number)<EPSILON}function approxEquals(a,b){return approxZero(a-b)}return DampingTween})); |
@@ -32,6 +32,15 @@ /*! | ||
} | ||
get value() { | ||
return this._currentValues[this._keys[0]]; | ||
} | ||
get endValue() { | ||
return this._endValues[this._keys[0]]; | ||
} | ||
setValues(values, immediate = false) { | ||
this._keys.forEach((key) => this.setValue(key, values[key], immediate)); | ||
this._keys.forEach((key) => this.setValueByKey(key, values[key], immediate)); | ||
} | ||
setValue(key, value, immediate = false) { | ||
setValue(value, immediate = false) { | ||
this.setValueByKey(this._keys[0], value, immediate); | ||
} | ||
setValueByKey(key, value, immediate = false) { | ||
if (!isNumber(this._endValues[key])) | ||
@@ -49,14 +58,2 @@ return; | ||
} | ||
get value() { | ||
return this._currentValues[this._keys[0]]; | ||
} | ||
get endValue() { | ||
return this._endValues[this._keys[0]]; | ||
} | ||
set value(value) { | ||
this.setValue(this._keys[0], value); | ||
} | ||
set endValue(value) { | ||
this.setValue(this._keys[0], value); | ||
} | ||
stop() { | ||
@@ -63,0 +60,0 @@ this.setValues(this._currentValues, true); |
@@ -14,6 +14,7 @@ interface Values { | ||
readonly endValues: Values; | ||
readonly value: number; | ||
readonly endValue: number; | ||
setValues(values: Values, immediate?: boolean): void; | ||
setValue(key: string, value: number, immediate?: boolean): void; | ||
value: number; | ||
endValue: number; | ||
setValue(value: number, immediate?: boolean): void; | ||
setValueByKey(key: string, value: number, immediate?: boolean): void; | ||
stop(): void; | ||
@@ -20,0 +21,0 @@ update(delta: number): boolean; |
{ | ||
"name": "damping-tween", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": "Yomotsu", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -54,48 +54,42 @@ const FPS_60 = 1 / 0.016; | ||
setValues( values: Values, immediate = false ): void { | ||
get value(): number { | ||
this._keys.forEach( ( key ) => this.setValue( key, values[ key ], immediate ) ); | ||
return this._currentValues[ this._keys[ 0 ] ]; | ||
} | ||
setValue( key: string, value: number, immediate = false ): void { | ||
get endValue(): number { | ||
if ( ! isNumber( this._endValues[ key ] ) ) return; | ||
if ( approxEquals( value, this._endValues[ key ] ) ) return; | ||
if ( immediate && value === this._currentValues[ key ] && value === this._endValues[ key ] ) return; | ||
return this._endValues[ this._keys[ 0 ] ]; | ||
this._endValues[ key ] = value; | ||
} | ||
if ( immediate ) { | ||
setValues( values: Values, immediate = false ): void { | ||
this._currentValues[ key ] = value; | ||
this._keys.forEach( ( key ) => this.setValueByKey( key, values[ key ], immediate ) ); | ||
} | ||
this.active = true; | ||
} | ||
get value(): number { | ||
setValue( value: number, immediate = false ): void { | ||
return this._currentValues[ this._keys[ 0 ] ]; | ||
this.setValueByKey( this._keys[ 0 ], value, immediate ); | ||
} | ||
get endValue(): number { | ||
setValueByKey( key: string, value: number, immediate = false ): void { | ||
return this._endValues[ this._keys[ 0 ] ]; | ||
if ( ! isNumber( this._endValues[ key ] ) ) return; | ||
if ( approxEquals( value, this._endValues[ key ] ) ) return; | ||
if ( immediate && value === this._currentValues[ key ] && value === this._endValues[ key ] ) return; | ||
} | ||
this._endValues[ key ] = value; | ||
set value( value: number ) { | ||
if ( immediate ) { | ||
this.setValue( this._keys[ 0 ], value ); | ||
this._currentValues[ key ] = value; | ||
} | ||
} | ||
set endValue( value: number ) { | ||
this.active = true; | ||
this.setValue( this._keys[ 0 ], value ); | ||
} | ||
@@ -102,0 +96,0 @@ |
15619
354