-
Notifications
You must be signed in to change notification settings - Fork 0
/
impetus.min.js
1 lines (1 loc) · 3.18 KB
/
impetus.min.js
1
!function(e,t){if("function"==typeof define&&define.amd)define(["exports","module"],t);else if("undefined"!=typeof exports&&"undefined"!=typeof module)t(exports,module);else{var n={exports:{}};t(n.exports,n),e.Impetus=n.exports}}(this,function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var o=.3,i=.04,u=.11,r=function d(e){function t(){b.call(L,G,H)}function r(e){if("touchmove"===e.type||"touchstart"===e.type||"touchend"===e.type){var t=e.targetTouches[0]||e.changedTouches[0];return{x:t.clientX,y:t.clientY,id:t.identifier}}return{x:e.clientX,y:e.clientY,id:null}}function s(e){var t=r(e);N||O||(N=!0,Q=!1,z=t.id,R=V=t.x,U=k=t.y,W=[],m(R,U),document.addEventListener("touchmove",a),document.addEventListener("touchend",f),document.addEventListener("touchcancel",v),document.addEventListener("mousemove",a),document.addEventListener("mouseup",f))}function a(e){e.preventDefault();var t=r(e);N&&t.id===z&&(V=t.x,k=t.y,m(R,U),p())}function f(e){var t=r(e);N&&t.id===z&&v()}function v(){N=!1,m(R,U),x(),document.removeEventListener("touchmove",a),document.removeEventListener("touchend",f),document.removeEventListener("touchcancel",v),document.removeEventListener("mouseup",f),document.removeEventListener("mousemove",a)}function m(e,t){for(var n=Date.now();W.length>0&&!(n-W[0].time<=100);)W.shift();W.push({x:e,y:t,time:n})}function l(){var e=V-R,n=k-U;if(G+=e*T,H+=n*T,S){var o=y();0!==o.x&&(G-=e*h(o.x)*T),0!==o.y&&(H-=n*h(o.y)*T)}else y(!0);t(),R=V,U=k,K=!1}function h(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function p(){K||c(l),K=!0}function y(e){var t=0,n=0;return void 0!==X&&X>G?t=X-G:void 0!==Y&&G>Y&&(t=Y-G),void 0!==D&&D>H?n=D-H:void 0!==P&&H>P&&(n=P-H),e&&(0!==t&&(G=t>0?X:Y),0!==n&&(H=n>0?D:P)),{x:t,y:n,inBounds:0===t&&0===n}}function x(){var e=W[0],t=W[W.length-1],n=t.x-e.x,o=t.y-e.y,i=t.time-e.time,u=i/15/T;C=n/u||0,j=o/u||0;var r=y();(Math.abs(C)>1||Math.abs(j)>1||!r.inBounds)&&(Q=!0,c(w))}function w(){if(Q){C*=q,j*=q,G+=C,H+=j;var e=y();if(Math.abs(C)>J||Math.abs(j)>J||!e.inBounds){if(S){var n=2.5;if(0!==e.x)if(e.x*C<=0)C+=e.x*i;else{var o=e.x>0?n:-n;C=(e.x+o)*u}if(0!==e.y)if(e.y*j<=0)j+=e.y*i;else{var o=e.y>0?n:-n;j=(e.y+o)*u}}else 0!==e.x&&(G=e.x>0?X:Y,C=0),0!==e.y&&(H=e.y>0?D:P,j=0);t(),c(w)}else Q=!1}}var E=e.source,L=void 0===E?document:E,b=e.update,M=e.multiplier,T=void 0===M?1:M,g=e.friction,q=void 0===g?.92:g,A=e.initialValues,B=e.boundX,F=e.boundY,I=e.bounce,S=void 0===I?!0:I;n(this,d);var X,Y,D,P,R,U,V,k,z,C,j,G=0,H=0,J=o*T,K=!1,N=!1,O=!1,Q=!1,W=[];!function(){if(L="string"==typeof L?document.querySelector(L):L,!L)throw new Error("IMPETUS: source not found.");if(!b)throw new Error("IMPETUS: update function not defined.");A&&(A[0]&&(G=A[0]),A[1]&&(H=A[1]),t()),B&&(X=B[0],Y=B[1]),F&&(D=F[0],P=F[1]),L.addEventListener("touchstart",s),L.addEventListener("mousedown",s)}(),this.pause=function(){N=!1,O=!0},this.resume=function(){O=!1},this.setValues=function(e,t){"number"==typeof e&&(G=e),"number"==typeof t&&(H=t)},this.setMultiplier=function(e){T=e,J=o*T}};t.exports=r;var c=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}()});