Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. const map=(a,b,c,d,e)=>(a-b)*(e-d)/(c-b)+d,clamp=(a,b,c)=>Math.min(Math.max(a,b),c),Easing={linear:a=>a,easeInOutQuad:a=>.5>a?2*a*a:-1+(4-2*a)*a};function Animation(a){if("object"!=typeof a||"object"==typeof a&&a instanceof Array)throw new Error("The settings passed to Animation must be an object");if(!a.hasOwnProperty("duration"))throw new Error("The settings passed to Animation must have a \"duration\" property set");if(!a.hasOwnProperty("from"))throw new Error("The settings passed to Animation must have a \"from\" property set");if(!a.hasOwnProperty("to"))throw new Error("The settings passed to Animation must have a \"to\" property set");if(a.hasOwnProperty("easing")&&"function"!=typeof a.easing)throw new Error("Invalid easing function passed to settings. Easing function must be one of the functions provided in the Easing object, e.g. Easing.linear");const b=a.duration,c=a.easing||Easing.linear,d={start:0,finish:0},e={from:a.from,to:a.to};let f,g;const h=a=>{d.start=a,d.finish=a+b},i=()=>{f=requestAnimationFrame(i);const a=Date.now();0===d.start+d.finish&&h(a);const b=c(map(a,d.start,d.finish,0,1)),j=clamp(b,0,1);g(map(j,0,1,e.from,e.to)),1===j&&cancelAnimationFrame(f)};return this.animate=a=>{if("function"!=typeof a)throw new Error("The given callback is not a function. A valid function callback must be provided");f=requestAnimationFrame(i),g=a},this}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement