between.js
Version:
Lightweight ES6 JavaScript tweening engine
3 lines (2 loc) • 8.51 kB
JavaScript
/* Between.js v0.1.0 */
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):t.Between=n()}(this,function(){"use strict";function l(t){return(l="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 r(t,n){for(var e=0;e<n.length;e++){var r=n[e];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function n(t,n,e){return n&&r(t.prototype,n),e&&r(t,e),t}function f(t,n,e){return n in t?Object.defineProperty(t,n,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[n]=e,t}function h(t){return(h=Object.getPrototypeOf||function(t){return t.__proto__})(t)}function o(t,n){return(o=Object.setPrototypeOf||function(t,n){return t.__proto__=n,t})(t,n)}function u(t,n,e){return(u="undefined"!=typeof Reflect&&Reflect.construct?Reflect.construct:function(t,n,e){var r=[null];r.push.apply(r,n);var u=new(t.bind.apply(t,r));return e&&o(u,e.prototype),u}).apply(null,arguments)}function p(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}var t=function(o){var i={},a=[];(o=o||this).on=function(t,n,e){return(i[t]=i[t]||[]).push([n,e]),o},o.off=function(t,n){t||(i={});for(var e=i[t]||a,r=e.length=n?e.length:0;r--;)n==e[r][0]&&e.splice(r,1);return o},o.emit=function(t){for(var n,e=i[t]||a,r=0<e.length?e.slice(0,e.length):e,u=0;n=r[u++];)n[0].apply(n[1],a.slice.call(arguments,1));return o}};var i=function(t,n,e){return t*(1-e)+n*e},e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function a(t,n){return t(n={exports:{}},n.exports),n.exports}for(var c=a(function(t){var n=t.exports={Linear:{None:function(t){return t}},Quadratic:{In:function(t){return t*t},Out:function(t){return t*(2-t)},InOut:function(t){return(t*=2)<1?.5*t*t:-.5*(--t*(t-2)-1)}},Cubic:{In:function(t){return t*t*t},Out:function(t){return--t*t*t+1},InOut:function(t){return(t*=2)<1?.5*t*t*t:.5*((t-=2)*t*t+2)}},Quartic:{In:function(t){return t*t*t*t},Out:function(t){return 1- --t*t*t*t},InOut:function(t){return(t*=2)<1?.5*t*t*t*t:-.5*((t-=2)*t*t*t-2)}},Quintic:{In:function(t){return t*t*t*t*t},Out:function(t){return--t*t*t*t*t+1},InOut:function(t){return(t*=2)<1?.5*t*t*t*t*t:.5*((t-=2)*t*t*t*t+2)}},Sinusoidal:{In:function(t){return 1-Math.cos(t*Math.PI/2)},Out:function(t){return Math.sin(t*Math.PI/2)},InOut:function(t){return.5*(1-Math.cos(Math.PI*t))}},Exponential:{In:function(t){return 0===t?0:Math.pow(1024,t-1)},Out:function(t){return 1===t?1:1-Math.pow(2,-10*t)},InOut:function(t){return 0===t?0:1===t?1:(t*=2)<1?.5*Math.pow(1024,t-1):.5*(2-Math.pow(2,-10*(t-1)))}},Circular:{In:function(t){return 1-Math.sqrt(1-t*t)},Out:function(t){return Math.sqrt(1- --t*t)},InOut:function(t){return(t*=2)<1?-.5*(Math.sqrt(1-t*t)-1):.5*(Math.sqrt(1-(t-=2)*t)+1)}},Elastic:{In:function(t){var n,e=.1;return 0===t?0:1===t?1:(!e||e<1?(e=1,n=.1):n=.4*Math.asin(1/e)/(2*Math.PI),-e*Math.pow(2,10*(t-=1))*Math.sin((t-n)*(2*Math.PI)/.4))},Out:function(t){var n,e=.1;return 0===t?0:1===t?1:(!e||e<1?(e=1,n=.1):n=.4*Math.asin(1/e)/(2*Math.PI),e*Math.pow(2,-10*t)*Math.sin((t-n)*(2*Math.PI)/.4)+1)},InOut:function(t){var n,e=.1;return 0===t?0:1===t?1:(!e||e<1?(e=1,n=.1):n=.4*Math.asin(1/e)/(2*Math.PI),(t*=2)<1?e*Math.pow(2,10*(t-=1))*Math.sin((t-n)*(2*Math.PI)/.4)*-.5:e*Math.pow(2,-10*(t-=1))*Math.sin((t-n)*(2*Math.PI)/.4)*.5+1)}},Back:{In:function(t){return t*t*(2.70158*t-1.70158)},Out:function(t){return--t*t*(2.70158*t+1.70158)+1},InOut:function(t){var n=2.5949095;return(t*=2)<1?t*t*((n+1)*t-n)*.5:.5*((t-=2)*t*((n+1)*t+n)+2)}},Bounce:{In:function(t){return 1-n.Bounce.Out(1-t)},Out:function(t){return t<1/2.75?7.5625*t*t:t<2/2.75?7.5625*(t-=1.5/2.75)*t+.75:t<2.5/2.75?7.5625*(t-=2.25/2.75)*t+.9375:7.5625*(t-=2.625/2.75)*t+.984375},InOut:function(t){return t<.5?.5*n.Bounce.In(2*t):.5*n.Bounce.Out(2*t-1)+.5}}}}),s=(c.Linear,c.Quadratic,c.Cubic,c.Quartic,c.Quintic,c.Sinusoidal,c.Exponential,c.Circular,c.Elastic,c.Back,c.Bounce,a(function(i){(function(){var t,n,e,r,u,o;"undefined"!=typeof performance&&null!==performance&&performance.now?i.exports=function(){return performance.now()}:"undefined"!=typeof process&&null!==process&&process.hrtime?(i.exports=function(){return(t()-u)/1e6},n=process.hrtime,r=(t=function(){var t;return 1e9*(t=n())[0]+t[1]})(),o=1e9*process.uptime(),u=r-o):Date.now?(i.exports=function(){return Date.now()-e},e=Date.now()):(i.exports=function(){return(new Date).getTime()-e},e=(new Date).getTime())}).call(e)})),m="undefined"==typeof window?e:window,d=["moz","webkit"],y="AnimationFrame",v=m["request"+y],b=m["cancel"+y]||m["cancelRequest"+y],w=0;!v&&w<d.length;w++)v=m[d[w]+"Request"+y],b=m[d[w]+"Cancel"+y]||m[d[w]+"CancelRequest"+y];if(!v||!b){var g=0,O=0,M=[];v=function(t){if(0===M.length){var n=s(),e=Math.max(0,1e3/60-(n-g));g=e+n,setTimeout(function(){for(var t=M.slice(0),n=M.length=0;n<t.length;n++)if(!t[n].cancelled)try{t[n].callback(g)}catch(t){setTimeout(function(){throw t},0)}},Math.round(e))}return M.push({handle:++O,callback:t,cancelled:!1}),O},b=function(t){for(var n=0;n<M.length;n++)M[n].handle===t&&(M[n].cancelled=!0)}}var I=function(t){return v.call(m,t)};I.cancel=function(){b.apply(m,arguments)},I.polyfill=function(t){t||(t=m),t.requestAnimationFrame=v,t.cancelAnimationFrame=b};var _,P,T=[],k=Symbol("type"),j=Symbol("completed"),x=Date.now();!function t(){I(t),_=Date.now(),P=_-x;for(var n=0;n<T.length;n++)T[n][j]||T[n].update(P);x=_}();var V=function(t){function s(e,t){var n,r,u,o;!function(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}(this,s),u=this;var i=(r=!(o=h(s).call(this))||"object"!=typeof o&&"function"!=typeof o?p(u):o).plugin=Object.values(s._plugins).reduce(function(t,n){return t||n&&n.test&&n.test(e)&&n},!1),a=i&&i.name||("object"===l(e)?Array.isArray(e)?"array":"object":"number");if(i){var c=i.initialize(e,t);e=c.startValue,t=c.destValue,r.data=c.data}return Object.assign(p(p(r)),(f(n={duration:1e3,localTime:0,startValue:e,destValue:t,loopMode:"once",loopFunction:s.DEFAULT_LOOP,ease:function(t){return t},value:"array"===a?[].concat(e):"object"===a?Object.assign({},e):e},j,!1),f(n,k,a),n)),T.push(p(p(r))),r}return n(s,null,[{key:"between",value:function(){for(var t=arguments.length,n=new Array(t),e=0;e<t;e++)n[e]=arguments[e];return u(s,n)}}]),n(s,[{key:"easing",value:function(t){return this.ease=t,this}},{key:"time",value:function(t){return this.duration=t,this}},{key:"loop",value:function(){for(var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"once",n="__loop_".concat(t),e=arguments.length,r=new Array(1<e?e-1:0),u=1;u<e;u++)r[u-1]=arguments[u];return this.loopFunction=n in this?Object.assign({},s.DEFAULT_LOOP,this[n].apply(this,r)):s.DEFAULT_LOOP,this}},{key:"__loop_repeat",value:function(t){var n=this,e=t;return this.times=0,{complete:function(t){n.localTime=0,Number.isInteger(e)&&++n.times===e?t():Number.isInteger(e)||++n.times}}}},{key:"__loop_bounce",value:function(t){var n=this,e=t,r=1;return this.times=0,{complete:function(t){n.localTime=0,r=-r,Number.isInteger(e)&&++n.times===e?t():Number.isInteger(e)||++n.times},progress:function(t){return 0<r?t:1-t}}}},{key:"update",value:function(t){var n=this;0===this.localTime&&this.emit("start",this.value,this);var e=this.ease(this.loopFunction.progress(Math.min(1,this.localTime/this.duration)));switch(this[k]){case"array":for(var r=0;r<this.value.length;r++)this.value[r]=i(this.startValue[r],this.destValue[r],e);break;case"object":for(var u in this.startValue)this.value[u]=i(this.startValue[u],this.destValue[u],e);break;case"number":this.value=i(this.startValue,this.destValue,e);break;default:this.plugin?this.value=this.plugin.interpolate(this.startValue,this.destValue,e,this.data):console.warn("Between: startValue type was unrecognized.")}this.emit("update",this.value,this,t),this.localTime>=this.duration&&this.loopFunction.complete(function(){n[j]=!0,n.emit("complete",n.value,n)}),this.localTime+=t}}]),function(t,n){if("function"!=typeof n&&null!==n)throw new TypeError("Super expression must either be null or a function");o(t.prototype,n&&n.prototype),n&&o(t,n)}(s,t),s}(t);return f(V,"DEFAULT_LOOP",{complete:function(t){return t()},progress:function(t){return t}}),V.Easing=c,V._plugins={},V});