UNPKG

inrtia

Version:

☄️ lightweight inertia based animation library

1 lines 3.65 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.Inrtia=e()}(this,function(){"use strict";var e=function(t,e,i){return t-(t-e)/i.friction},r=function(t,e,i,s,n,o){n&&this._resetSpeed(o);var a=this._getSpeed(o);return a+=(-(t-e)/(i.friction/100)+-i.rigidity*a)/i.mass*s,this._setSpeed(a,o),t+a*s},i=function(t,e,i,s,n,o){var a=r.call(this,t,e,i,s,n,o);return 0<=a-e?(this._setSpeed(-this._getSpeed(o),o),e):a},y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t=function(){function s(t,e){for(var i=0;i<e.length;i++){var s=e[i];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(t,s.key,s)}}return function(t,e,i){return e&&s(t.prototype,e),i&&s(t,i),t}}();return function(){function v(t){var e=t.value,i=t.interpolation,s=void 0===i?"basic":i,n=t.precisionStop,o=void 0===n?.001:n,a=t.perfectStop,r=void 0!==a&&a,u=t.rigidity,h=void 0===u?.1:u,l=t.friction,p=void 0===l?10:l,c=t.mass,f=void 0===c?.01:c;if(function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,v),this.value=e,this.stopped=!0,this.complex="object"===(void 0===e?"undefined":y(e)),this.complex)if(this.keys=Object.keys(this.value),Array.isArray(e))this.targetValue=e.slice(0);else{this.targetValue={};for(var d=this.keys.length;d--;)this.targetValue[this.keys[d]]=this.value[this.keys[d]]}else this.targetValue=e;this.precisionStop=o,this.perfectStop=r,this.interpolationFn=s instanceof Function?s:this.getInterpolation(s),this.interpolationParams={rigidity:h,friction:p,mass:f}}return t(v,[{key:"getInterpolation",value:function(t){switch(t){case"elastic":return r;case"bounce":return i;default:return e}}},{key:"to",value:function(t){if(this.complex)for(var e=void 0,i=this.keys.length;i--;)t[e=this.keys[i]]&&(this.targetValue[e]=t[e]);else this.targetValue=t;this.stopped&&this._start()}},{key:"_start",value:function(){this.reset=!0,this.stopped=!1,this.lastTime=Date.now()-17}},{key:"update",value:function(){var t=0<arguments.length&&void 0!==arguments[0]&&arguments[0];if(this.stopped)return this.value;var e=Date.now(),i=(t||e-this.lastTime)/1e3,s=!0;if(this.complex)for(var n=void 0,o=this.keys.length;o--;)n=this.keys[o],this.value[n]=this._updateValue(this.value[n],this.targetValue[n],i,n),s=s&&this._needStop(this.value[n],this.targetValue[n],n);else this.value=this._updateValue(this.value,this.targetValue,i,!1),s=this._needStop(this.value,this.targetValue,!1);if(this.reset&&(this.reset=!1),s){if(this.perfectStop)if(this.complex)for(var a=void 0,r=this.keys.length;r--;)a=this.keys[r],this.value[a]=this.targetValue[a];else this.value=this.targetValue;this.stop()}return this.lastTime=e,this.value}},{key:"_updateValue",value:function(t,e,i,s){return this.interpolationFn.call(this,t,e,this.interpolationParams,i,this.reset,s)}},{key:"_needStop",value:function(t,e,i){var s=t-e,n=this._getSpeed(i);return Math.abs(s)<this.precisionStop&&(!!isNaN(n)||Math.abs(n)<this.precisionStop)}},{key:"_setSpeed",value:function(t,e){this.complex?this.speed[e]=t:this.speed=t}},{key:"_getSpeed",value:function(t){return this.complex?this.speed&&this.speed[t]:this.speed}},{key:"_resetSpeed",value:function(t){this.complex?(this.speed||(this.speed={}),this.speed[t]=0):this.speed=0}},{key:"stop",value:function(){this.stopped=!0}},{key:"destroy",value:function(){this.stop(),this.interpolationFn=null,this.interpolationParams=null}}]),v}()});