animate-value
Version:
🏃 animates a numeric value
2 lines (1 loc) • 1.88 kB
JavaScript
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):e.animateValue=n()}(this,function(){"use strict";var e=function(){return Date.now()},n=function(n,t){var o=n+e(),a=o+t;return{start:o,end:a}},t=function(e){return function(n){return Math.pow(n,e)}},o=function(e){return function(n){return 1-Math.abs(Math.pow(n-1,e))}},a=function(e){return function(n){return n<.5?t(e)(2*n)/2:o(e)(2*n-1)/2+.5}},i={linear:a(1),easeInQuad:t(2),easeOutQuad:o(2),easeInOutQuad:a(2),easeInCubic:t(3),easeOutCubic:o(3),easeInOutCubic:a(3),easeInQuart:t(4),easeOutQuart:o(4),easeInOutQuart:a(4),easeInQuint:t(5),easeOutQuint:o(5),easeInOutQuint:a(5)},r=function(e,n,t){var o=(Date.now()-e)/n;return i[t](o)},u=function t(o,a,i){var u=o,f=u.start,d=u.end,s=u.easing,c=u.to,v=u.from,l=u.duration,m=u.change,p=u.done,O=u.loop,Q=u.rewind,g=u.loopDelay;if(f&&d){var I=r(f,l,s);if(o.value=v+(c-v)*I,e()<=d)m&&e()>=f&&m(o.value),a=requestAnimationFrame(function(){return t(o,a,i)});else if(m&&m(o.to),O){if(Q){var h=o,w=h.from,y=h.to;o.from=y,o.to=w}var b=g;if(o=Object.assign({},o,n(b,l)),o.iteration){o.iteration+=1;var D=o,j=D.iteration;Q&&(j/=2),O===!0||j<=O?a=requestAnimationFrame(function(){return t(o,a,i)}):(p&&p(),i())}}else p&&p()}},f=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return new Promise(function(t){var o=e.from,a=void 0===o?0:o,i=e.to,r=void 0===i?1:i,f=e.easing,d=void 0===f?"easeInQuad":f,s=e.duration,c=void 0===s?600:s,v=e.delay,l=void 0===v?0:v,m=e.loopDelay,p=void 0===m?0:m,O=e.rewind,Q=void 0!==O&&O,g=e.change,I=e.done,h=e.loop,w=void 0===h?1:h;w===!1&&(w=1);var y={from:a,to:r,duration:c,delay:l,loopDelay:p,loop:w,easing:d,rewind:Q,change:g,done:I};y=Object.assign({},y,n(l,c)),y.iteration=1;var b=requestAnimationFrame(function(){return u(y,b,t)})})};return f});