From e6da5b97dcb402d9dbbc5e48f70571a689ff7396 Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Wed, 30 Aug 2017 14:52:50 -0700 Subject: [PATCH 01/14] =?UTF-8?q?feat:=20added=20a=20new=20=E2=80=9Cwindow?= =?UTF-8?q?=E2=80=9D=20property,=20so=20that=20Impetus=20can=20be=20applie?= =?UTF-8?q?d=20to=20child=20iframes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/impetus.js | 34 ++++++++++++++++++---------------- dist/impetus.min.js | 2 +- docs/impetus.min.js | 2 +- src/Impetus.js | 35 +++++++++++++++++------------------ 4 files changed, 37 insertions(+), 36 deletions(-) diff --git a/dist/impetus.js b/dist/impetus.js index c609f23..3c26bed 100644 --- a/dist/impetus.js +++ b/dist/impetus.js @@ -19,13 +19,11 @@ var bounceDeceleration = 0.04; var bounceAcceleration = 0.11; - // fixes weird safari 10 bug where preventDefault is prevented - // @see https://github.com/metafizzy/flickity/issues/457#issuecomment-254501356 - window.addEventListener('touchmove', function () {}); - var Impetus = function Impetus(_ref) { + var _ref$window = _ref.window; + var win = _ref$window === undefined ? window : _ref$window; var _ref$source = _ref.source; - var sourceEl = _ref$source === undefined ? document : _ref$source; + var sourceEl = _ref$source === undefined ? window : _ref$source; var updateCallback = _ref.update; var _ref$multiplier = _ref.multiplier; var multiplier = _ref$multiplier === undefined ? 1 : _ref$multiplier; @@ -49,11 +47,15 @@ var decelerating = false; var trackingPoints = []; + // fixes weird safari 10 bug where preventDefault is prevented + // @see https://github.com/metafizzy/flickity/issues/457#issuecomment-254501356 + win.addEventListener('touchmove', function () {}); + /** * Initialize instance */ (function init() { - sourceEl = typeof sourceEl === 'string' ? document.querySelector(sourceEl) : sourceEl; + sourceEl = typeof sourceEl === 'string' ? win.document.querySelector(sourceEl) : sourceEl; if (!sourceEl) { throw new Error('IMPETUS: source not found.'); } @@ -210,11 +212,11 @@ addTrackingPoint(pointerLastX, pointerLastY); // @see https://developers.google.com/web/updates/2017/01/scrolling-intervention - document.addEventListener('touchmove', onMove, getPassiveSupported() ? { passive: false } : false); - document.addEventListener('touchend', onUp); - document.addEventListener('touchcancel', stopTracking); - document.addEventListener('mousemove', onMove, getPassiveSupported() ? { passive: false } : false); - document.addEventListener('mouseup', onUp); + win.addEventListener('touchmove', onMove, getPassiveSupported() ? { passive: false } : false); + win.addEventListener('touchend', onUp); + win.addEventListener('touchcancel', stopTracking); + win.addEventListener('mousemove', onMove, getPassiveSupported() ? { passive: false } : false); + win.addEventListener('mouseup', onUp); } } @@ -254,11 +256,11 @@ addTrackingPoint(pointerLastX, pointerLastY); startDecelAnim(); - document.removeEventListener('touchmove', onMove); - document.removeEventListener('touchend', onUp); - document.removeEventListener('touchcancel', stopTracking); - document.removeEventListener('mouseup', onUp); - document.removeEventListener('mousemove', onMove); + win.removeEventListener('touchmove', onMove); + win.removeEventListener('touchend', onUp); + win.removeEventListener('touchcancel', stopTracking); + win.removeEventListener('mouseup', onUp); + win.removeEventListener('mousemove', onMove); } /** diff --git a/dist/impetus.min.js b/dist/impetus.min.js index 49fd819..486f502 100644 --- a/dist/impetus.min.js +++ b/dist/impetus.min.js @@ -1 +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")}function o(){var e=!1;try{var t=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,t)}catch(e){}return o=function(){return e},e}var i=.04,u=.11;window.addEventListener("touchmove",function(){});var r=function e(t){function r(){b.call(L,O,G)}function c(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 d(e){var t=c(e);K||N||(K=!0,Q=!1,k=t.id,R=V=t.x,U=j=t.y,W=[],m(R,U),document.addEventListener("touchmove",a,!!o()&&{passive:!1}),document.addEventListener("touchend",f),document.addEventListener("touchcancel",v),document.addEventListener("mousemove",a,!!o()&&{passive:!1}),document.addEventListener("mouseup",f))}function a(e){e.preventDefault();var t=c(e);K&&t.id===k&&(V=t.x,j=t.y,m(R,U),p())}function f(e){var t=c(e);K&&t.id===k&&v()}function v(){K=!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,t=j-U;if(O+=e*g,G+=t*g,F){var n=y();0!==n.x&&(O-=e*h(n.x)*g),0!==n.y&&(G-=t*h(n.y)*g)}else y(!0);r(),R=V,U=j,J=!1}function h(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function p(){J||s(l),J=!0}function y(e){var t=0,n=0;return void 0!==I&&OP&&(t=P-O),void 0!==S&&GD&&(n=D-G),e&&(0!==t&&(O=t>0?I:P),0!==n&&(G=n>0?S:D)),{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/g;z=n/u||0,C=o/u||0;var r=y();(Math.abs(z)>1||Math.abs(C)>1||!r.inBounds)&&(Q=!0,s(w))}function w(){if(Q){z*=B,C*=B,O+=z,G+=C;var e=y();if(Math.abs(z)>H||Math.abs(C)>H||!e.inBounds){if(F){if(0!==e.x)if(e.x*z<=0)z+=e.x*i;else{var t=e.x>0?2.5:-2.5;z=(e.x+t)*u}if(0!==e.y)if(e.y*C<=0)C+=e.y*i;else{var t=e.y>0?2.5:-2.5;C=(e.y+t)*u}}else 0!==e.x&&(O=e.x>0?I:P,z=0),0!==e.y&&(G=e.y>0?S:D,C=0);r(),s(w)}else Q=!1}}var E=t.source,L=void 0===E?document:E,b=t.update,M=t.multiplier,g=void 0===M?1:M,T=t.friction,B=void 0===T?.92:T,q=t.initialValues,X=t.boundX,Y=t.boundY,A=t.bounce,F=void 0===A||A;n(this,e);var I,P,S,D,R,U,V,j,k,z,C,O=0,G=0,H=.3*g,J=!1,K=!1,N=!1,Q=!1,W=[];!function(){if(!(L="string"==typeof L?document.querySelector(L):L))throw new Error("IMPETUS: source not found.");if(!b)throw new Error("IMPETUS: update function not defined.");q&&(q[0]&&(O=q[0]),q[1]&&(G=q[1]),r()),X&&(I=X[0],P=X[1]),Y&&(S=Y[0],D=Y[1]),L.addEventListener("touchstart",d),L.addEventListener("mousedown",d)}(),this.destroy=function(){return L.removeEventListener("touchstart",d),L.removeEventListener("mousedown",d),null},this.pause=function(){K=!1,N=!0},this.resume=function(){N=!1},this.setValues=function(e,t){"number"==typeof e&&(O=e),"number"==typeof t&&(G=t)},this.setMultiplier=function(e){g=e,H=.3*g},this.setBoundX=function(e){I=e[0],P=e[1]},this.setBoundY=function(e){S=e[0],D=e[1]}};t.exports=r;var s=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}()}); \ No newline at end of file +!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")}function i(){var e=!1;try{var t=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,t)}catch(e){}return i=function(){return e},e}var o=.04,r=.11,u=function e(t){function u(){g.call(M,H,J)}function d(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 a(e){var t=d(e);Q||W||(Q=!0,Z=!1,C=t.id,V=k=t.x,j=z=t.y,$=[],m(V,j),L.addEventListener("touchmove",c,!!i()&&{passive:!1}),L.addEventListener("touchend",f),L.addEventListener("touchcancel",v),L.addEventListener("mousemove",c,!!i()&&{passive:!1}),L.addEventListener("mouseup",f))}function c(e){e.preventDefault();var t=d(e);Q&&t.id===C&&(k=t.x,z=t.y,m(V,j),p())}function f(e){var t=d(e);Q&&t.id===C&&v()}function v(){Q=!1,m(V,j),w(),L.removeEventListener("touchmove",c),L.removeEventListener("touchend",f),L.removeEventListener("touchcancel",v),L.removeEventListener("mouseup",f),L.removeEventListener("mousemove",c)}function m(e,t){for(var n=Date.now();$.length>0&&!(n-$[0].time<=100);)$.shift();$.push({x:e,y:t,time:n})}function l(){var e=k-V,t=z-j;if(H+=e*B,J+=t*B,P){var n=y();0!==n.x&&(H-=e*h(n.x)*B),0!==n.y&&(J-=t*h(n.y)*B)}else y(!0);u(),V=k,j=z,N=!1}function h(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function p(){N||s(l),N=!0}function y(e){var t=0,n=0;return void 0!==S&&HD&&(t=D-H),void 0!==R&&JU&&(n=U-J),e&&(0!==t&&(H=t>0?S:D),0!==n&&(J=n>0?R:U)),{x:t,y:n,inBounds:0===t&&0===n}}function w(){var e=$[0],t=$[$.length-1],n=t.x-e.x,i=t.y-e.y,o=t.time-e.time,r=o/15/B;O=n/r||0,G=i/r||0;var u=y();(Math.abs(O)>1||Math.abs(G)>1||!u.inBounds)&&(Z=!0,s(x))}function x(){if(Z){O*=X,G*=X,H+=O,J+=G;var e=y();if(Math.abs(O)>K||Math.abs(G)>K||!e.inBounds){if(P){if(0!==e.x)if(e.x*O<=0)O+=e.x*o;else{var t=e.x>0?2.5:-2.5;O=(e.x+t)*r}if(0!==e.y)if(e.y*G<=0)G+=e.y*o;else{var t=e.y>0?2.5:-2.5;G=(e.y+t)*r}}else 0!==e.x&&(H=e.x>0?S:D,O=0),0!==e.y&&(J=e.y>0?R:U,G=0);u(),s(x)}else Z=!1}}var E=t.window,L=void 0===E?window:E,b=t.source,M=void 0===b?window:b,g=t.update,T=t.multiplier,B=void 0===T?1:T,q=t.friction,X=void 0===q?.92:q,Y=t.initialValues,A=t.boundX,F=t.boundY,I=t.bounce,P=void 0===I||I;n(this,e);var S,D,R,U,V,j,k,z,C,O,G,H=0,J=0,K=.3*B,N=!1,Q=!1,W=!1,Z=!1,$=[];L.addEventListener("touchmove",function(){}),function(){if(!(M="string"==typeof M?L.document.querySelector(M):M))throw new Error("IMPETUS: source not found.");if(!g)throw new Error("IMPETUS: update function not defined.");Y&&(Y[0]&&(H=Y[0]),Y[1]&&(J=Y[1]),u()),A&&(S=A[0],D=A[1]),F&&(R=F[0],U=F[1]),M.addEventListener("touchstart",a),M.addEventListener("mousedown",a)}(),this.destroy=function(){return M.removeEventListener("touchstart",a),M.removeEventListener("mousedown",a),null},this.pause=function(){Q=!1,W=!0},this.resume=function(){W=!1},this.setValues=function(e,t){"number"==typeof e&&(H=e),"number"==typeof t&&(J=t)},this.setMultiplier=function(e){B=e,K=.3*B},this.setBoundX=function(e){S=e[0],D=e[1]},this.setBoundY=function(e){R=e[0],U=e[1]}};t.exports=u;var s=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}()}); \ No newline at end of file diff --git a/docs/impetus.min.js b/docs/impetus.min.js index 49fd819..486f502 100644 --- a/docs/impetus.min.js +++ b/docs/impetus.min.js @@ -1 +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")}function o(){var e=!1;try{var t=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,t)}catch(e){}return o=function(){return e},e}var i=.04,u=.11;window.addEventListener("touchmove",function(){});var r=function e(t){function r(){b.call(L,O,G)}function c(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 d(e){var t=c(e);K||N||(K=!0,Q=!1,k=t.id,R=V=t.x,U=j=t.y,W=[],m(R,U),document.addEventListener("touchmove",a,!!o()&&{passive:!1}),document.addEventListener("touchend",f),document.addEventListener("touchcancel",v),document.addEventListener("mousemove",a,!!o()&&{passive:!1}),document.addEventListener("mouseup",f))}function a(e){e.preventDefault();var t=c(e);K&&t.id===k&&(V=t.x,j=t.y,m(R,U),p())}function f(e){var t=c(e);K&&t.id===k&&v()}function v(){K=!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,t=j-U;if(O+=e*g,G+=t*g,F){var n=y();0!==n.x&&(O-=e*h(n.x)*g),0!==n.y&&(G-=t*h(n.y)*g)}else y(!0);r(),R=V,U=j,J=!1}function h(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function p(){J||s(l),J=!0}function y(e){var t=0,n=0;return void 0!==I&&OP&&(t=P-O),void 0!==S&&GD&&(n=D-G),e&&(0!==t&&(O=t>0?I:P),0!==n&&(G=n>0?S:D)),{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/g;z=n/u||0,C=o/u||0;var r=y();(Math.abs(z)>1||Math.abs(C)>1||!r.inBounds)&&(Q=!0,s(w))}function w(){if(Q){z*=B,C*=B,O+=z,G+=C;var e=y();if(Math.abs(z)>H||Math.abs(C)>H||!e.inBounds){if(F){if(0!==e.x)if(e.x*z<=0)z+=e.x*i;else{var t=e.x>0?2.5:-2.5;z=(e.x+t)*u}if(0!==e.y)if(e.y*C<=0)C+=e.y*i;else{var t=e.y>0?2.5:-2.5;C=(e.y+t)*u}}else 0!==e.x&&(O=e.x>0?I:P,z=0),0!==e.y&&(G=e.y>0?S:D,C=0);r(),s(w)}else Q=!1}}var E=t.source,L=void 0===E?document:E,b=t.update,M=t.multiplier,g=void 0===M?1:M,T=t.friction,B=void 0===T?.92:T,q=t.initialValues,X=t.boundX,Y=t.boundY,A=t.bounce,F=void 0===A||A;n(this,e);var I,P,S,D,R,U,V,j,k,z,C,O=0,G=0,H=.3*g,J=!1,K=!1,N=!1,Q=!1,W=[];!function(){if(!(L="string"==typeof L?document.querySelector(L):L))throw new Error("IMPETUS: source not found.");if(!b)throw new Error("IMPETUS: update function not defined.");q&&(q[0]&&(O=q[0]),q[1]&&(G=q[1]),r()),X&&(I=X[0],P=X[1]),Y&&(S=Y[0],D=Y[1]),L.addEventListener("touchstart",d),L.addEventListener("mousedown",d)}(),this.destroy=function(){return L.removeEventListener("touchstart",d),L.removeEventListener("mousedown",d),null},this.pause=function(){K=!1,N=!0},this.resume=function(){N=!1},this.setValues=function(e,t){"number"==typeof e&&(O=e),"number"==typeof t&&(G=t)},this.setMultiplier=function(e){g=e,H=.3*g},this.setBoundX=function(e){I=e[0],P=e[1]},this.setBoundY=function(e){S=e[0],D=e[1]}};t.exports=r;var s=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}()}); \ No newline at end of file +!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")}function i(){var e=!1;try{var t=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,t)}catch(e){}return i=function(){return e},e}var o=.04,r=.11,u=function e(t){function u(){g.call(M,H,J)}function d(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 a(e){var t=d(e);Q||W||(Q=!0,Z=!1,C=t.id,V=k=t.x,j=z=t.y,$=[],m(V,j),L.addEventListener("touchmove",c,!!i()&&{passive:!1}),L.addEventListener("touchend",f),L.addEventListener("touchcancel",v),L.addEventListener("mousemove",c,!!i()&&{passive:!1}),L.addEventListener("mouseup",f))}function c(e){e.preventDefault();var t=d(e);Q&&t.id===C&&(k=t.x,z=t.y,m(V,j),p())}function f(e){var t=d(e);Q&&t.id===C&&v()}function v(){Q=!1,m(V,j),w(),L.removeEventListener("touchmove",c),L.removeEventListener("touchend",f),L.removeEventListener("touchcancel",v),L.removeEventListener("mouseup",f),L.removeEventListener("mousemove",c)}function m(e,t){for(var n=Date.now();$.length>0&&!(n-$[0].time<=100);)$.shift();$.push({x:e,y:t,time:n})}function l(){var e=k-V,t=z-j;if(H+=e*B,J+=t*B,P){var n=y();0!==n.x&&(H-=e*h(n.x)*B),0!==n.y&&(J-=t*h(n.y)*B)}else y(!0);u(),V=k,j=z,N=!1}function h(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function p(){N||s(l),N=!0}function y(e){var t=0,n=0;return void 0!==S&&HD&&(t=D-H),void 0!==R&&JU&&(n=U-J),e&&(0!==t&&(H=t>0?S:D),0!==n&&(J=n>0?R:U)),{x:t,y:n,inBounds:0===t&&0===n}}function w(){var e=$[0],t=$[$.length-1],n=t.x-e.x,i=t.y-e.y,o=t.time-e.time,r=o/15/B;O=n/r||0,G=i/r||0;var u=y();(Math.abs(O)>1||Math.abs(G)>1||!u.inBounds)&&(Z=!0,s(x))}function x(){if(Z){O*=X,G*=X,H+=O,J+=G;var e=y();if(Math.abs(O)>K||Math.abs(G)>K||!e.inBounds){if(P){if(0!==e.x)if(e.x*O<=0)O+=e.x*o;else{var t=e.x>0?2.5:-2.5;O=(e.x+t)*r}if(0!==e.y)if(e.y*G<=0)G+=e.y*o;else{var t=e.y>0?2.5:-2.5;G=(e.y+t)*r}}else 0!==e.x&&(H=e.x>0?S:D,O=0),0!==e.y&&(J=e.y>0?R:U,G=0);u(),s(x)}else Z=!1}}var E=t.window,L=void 0===E?window:E,b=t.source,M=void 0===b?window:b,g=t.update,T=t.multiplier,B=void 0===T?1:T,q=t.friction,X=void 0===q?.92:q,Y=t.initialValues,A=t.boundX,F=t.boundY,I=t.bounce,P=void 0===I||I;n(this,e);var S,D,R,U,V,j,k,z,C,O,G,H=0,J=0,K=.3*B,N=!1,Q=!1,W=!1,Z=!1,$=[];L.addEventListener("touchmove",function(){}),function(){if(!(M="string"==typeof M?L.document.querySelector(M):M))throw new Error("IMPETUS: source not found.");if(!g)throw new Error("IMPETUS: update function not defined.");Y&&(Y[0]&&(H=Y[0]),Y[1]&&(J=Y[1]),u()),A&&(S=A[0],D=A[1]),F&&(R=F[0],U=F[1]),M.addEventListener("touchstart",a),M.addEventListener("mousedown",a)}(),this.destroy=function(){return M.removeEventListener("touchstart",a),M.removeEventListener("mousedown",a),null},this.pause=function(){Q=!1,W=!0},this.resume=function(){W=!1},this.setValues=function(e,t){"number"==typeof e&&(H=e),"number"==typeof t&&(J=t)},this.setMultiplier=function(e){B=e,K=.3*B},this.setBoundX=function(e){S=e[0],D=e[1]},this.setBoundY=function(e){R=e[0],U=e[1]}};t.exports=u;var s=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}()}); \ No newline at end of file diff --git a/src/Impetus.js b/src/Impetus.js index 920d895..3d71c88 100644 --- a/src/Impetus.js +++ b/src/Impetus.js @@ -3,15 +3,10 @@ const stopThresholdDefault = 0.3; const bounceDeceleration = 0.04; const bounceAcceleration = 0.11; - -// fixes weird safari 10 bug where preventDefault is prevented -// @see https://github.com/metafizzy/flickity/issues/457#issuecomment-254501356 -window.addEventListener('touchmove', function() {}); - - export default class Impetus { constructor({ - source: sourceEl = document, + window: win = window, + source: sourceEl = window, update: updateCallback, multiplier = 1, friction = 0.92, @@ -30,12 +25,16 @@ export default class Impetus { var decelerating = false; var trackingPoints = []; + // fixes weird safari 10 bug where preventDefault is prevented + // @see https://github.com/metafizzy/flickity/issues/457#issuecomment-254501356 + win.addEventListener('touchmove', function() {}); + /** * Initialize instance */ (function init() { - sourceEl = (typeof sourceEl === 'string') ? document.querySelector(sourceEl) : sourceEl; + sourceEl = (typeof sourceEl === 'string') ? win.document.querySelector(sourceEl) : sourceEl; if (!sourceEl) { throw new Error('IMPETUS: source not found.'); } @@ -191,11 +190,11 @@ export default class Impetus { addTrackingPoint(pointerLastX, pointerLastY); // @see https://developers.google.com/web/updates/2017/01/scrolling-intervention - document.addEventListener('touchmove', onMove, getPassiveSupported() ? {passive: false} : false); - document.addEventListener('touchend', onUp); - document.addEventListener('touchcancel', stopTracking); - document.addEventListener('mousemove', onMove, getPassiveSupported() ? {passive: false} : false); - document.addEventListener('mouseup', onUp); + win.addEventListener('touchmove', onMove, getPassiveSupported() ? {passive: false} : false); + win.addEventListener('touchend', onUp); + win.addEventListener('touchcancel', stopTracking); + win.addEventListener('mousemove', onMove, getPassiveSupported() ? {passive: false} : false); + win.addEventListener('mouseup', onUp); } } @@ -235,11 +234,11 @@ export default class Impetus { addTrackingPoint(pointerLastX, pointerLastY); startDecelAnim(); - document.removeEventListener('touchmove', onMove); - document.removeEventListener('touchend', onUp); - document.removeEventListener('touchcancel', stopTracking); - document.removeEventListener('mouseup', onUp); - document.removeEventListener('mousemove', onMove); + win.removeEventListener('touchmove', onMove); + win.removeEventListener('touchend', onUp); + win.removeEventListener('touchcancel', stopTracking); + win.removeEventListener('mouseup', onUp); + win.removeEventListener('mousemove', onMove); } /** From b7728cafb8f21bc83de311c168cd1995a3742b59 Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Wed, 6 Sep 2017 16:08:28 -0700 Subject: [PATCH 02/14] feat: set the axis config param, or use setAxis(), to disable either the x or y axis from scrolling --- dist/impetus.js | 44 ++++++++++++++++++++++++++++++-------------- dist/impetus.min.js | 2 +- docs/impetus.min.js | 2 +- src/Impetus.js | 43 +++++++++++++++++++++++++++++-------------- 4 files changed, 61 insertions(+), 30 deletions(-) diff --git a/dist/impetus.js b/dist/impetus.js index c609f23..6f9fe6f 100644 --- a/dist/impetus.js +++ b/dist/impetus.js @@ -26,6 +26,8 @@ var Impetus = function Impetus(_ref) { var _ref$source = _ref.source; var sourceEl = _ref$source === undefined ? document : _ref$source; + var _ref$axis = _ref.axis; + var axis = _ref$axis === undefined ? false : _ref$axis; var updateCallback = _ref.update; var _ref$multiplier = _ref.multiplier; var multiplier = _ref$multiplier === undefined ? 1 : _ref$multiplier; @@ -163,6 +165,16 @@ boundYmax = boundY[1]; }; + /** + * Set the axis that will be scrolled + * @public + * @param {String|Boolean} axis + */ + this.setAxis = function (val) { + var lowerCaseVal = (val || '').toLowerCase(); + axis = lowerCaseVal === 'x' ? 'x' : lowerCaseVal === 'y' ? 'y' : false; + }; + /** * Executes the update function */ @@ -204,8 +216,8 @@ decelerating = false; pointerId = event.id; - pointerLastX = pointerCurrentX = event.x; - pointerLastY = pointerCurrentY = event.y; + pointerLastX = pointerCurrentX = axis !== 'y' ? event.x : pointerLastX; + pointerLastY = pointerCurrentY = axis !== 'x' ? event.y : pointerLastY; trackingPoints = []; addTrackingPoint(pointerLastX, pointerLastY); @@ -227,8 +239,8 @@ var event = normalizeEvent(ev); if (pointerActive && event.id === pointerId) { - pointerCurrentX = event.x; - pointerCurrentY = event.y; + pointerCurrentX = axis !== 'y' ? event.x : pointerCurrentX; + pointerCurrentY = axis !== 'x' ? event.y : pointerCurrentY; addTrackingPoint(pointerLastX, pointerLastY); requestTick(); } @@ -282,8 +294,8 @@ * Calculate new values, call update function */ function updateAndRender() { - var pointerChangeX = pointerCurrentX - pointerLastX; - var pointerChangeY = pointerCurrentY - pointerLastY; + var pointerChangeX = pointerCurrentX - pointerLastX || 0; // prevent NaN + var pointerChangeY = pointerCurrentY - pointerLastY || 0; targetX += pointerChangeX * multiplier; targetY += pointerChangeY * multiplier; @@ -333,16 +345,20 @@ var xDiff = 0; var yDiff = 0; - if (boundXmin !== undefined && targetX < boundXmin) { - xDiff = boundXmin - targetX; - } else if (boundXmax !== undefined && targetX > boundXmax) { - xDiff = boundXmax - targetX; + if (axis !== 'y') { + if (boundXmin !== undefined && targetX < boundXmin) { + xDiff = boundXmin - targetX; + } else if (boundXmax !== undefined && targetX > boundXmax) { + xDiff = boundXmax - targetX; + } } - if (boundYmin !== undefined && targetY < boundYmin) { - yDiff = boundYmin - targetY; - } else if (boundYmax !== undefined && targetY > boundYmax) { - yDiff = boundYmax - targetY; + if (axis !== 'x') { + if (boundYmin !== undefined && targetY < boundYmin) { + yDiff = boundYmin - targetY; + } else if (boundYmax !== undefined && targetY > boundYmax) { + yDiff = boundYmax - targetY; + } } if (restrict) { diff --git a/dist/impetus.min.js b/dist/impetus.min.js index 49fd819..ad81a50 100644 --- a/dist/impetus.min.js +++ b/dist/impetus.min.js @@ -1 +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")}function o(){var e=!1;try{var t=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,t)}catch(e){}return o=function(){return e},e}var i=.04,u=.11;window.addEventListener("touchmove",function(){});var r=function e(t){function r(){b.call(L,O,G)}function c(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 d(e){var t=c(e);K||N||(K=!0,Q=!1,k=t.id,R=V=t.x,U=j=t.y,W=[],m(R,U),document.addEventListener("touchmove",a,!!o()&&{passive:!1}),document.addEventListener("touchend",f),document.addEventListener("touchcancel",v),document.addEventListener("mousemove",a,!!o()&&{passive:!1}),document.addEventListener("mouseup",f))}function a(e){e.preventDefault();var t=c(e);K&&t.id===k&&(V=t.x,j=t.y,m(R,U),p())}function f(e){var t=c(e);K&&t.id===k&&v()}function v(){K=!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,t=j-U;if(O+=e*g,G+=t*g,F){var n=y();0!==n.x&&(O-=e*h(n.x)*g),0!==n.y&&(G-=t*h(n.y)*g)}else y(!0);r(),R=V,U=j,J=!1}function h(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function p(){J||s(l),J=!0}function y(e){var t=0,n=0;return void 0!==I&&OP&&(t=P-O),void 0!==S&&GD&&(n=D-G),e&&(0!==t&&(O=t>0?I:P),0!==n&&(G=n>0?S:D)),{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/g;z=n/u||0,C=o/u||0;var r=y();(Math.abs(z)>1||Math.abs(C)>1||!r.inBounds)&&(Q=!0,s(w))}function w(){if(Q){z*=B,C*=B,O+=z,G+=C;var e=y();if(Math.abs(z)>H||Math.abs(C)>H||!e.inBounds){if(F){if(0!==e.x)if(e.x*z<=0)z+=e.x*i;else{var t=e.x>0?2.5:-2.5;z=(e.x+t)*u}if(0!==e.y)if(e.y*C<=0)C+=e.y*i;else{var t=e.y>0?2.5:-2.5;C=(e.y+t)*u}}else 0!==e.x&&(O=e.x>0?I:P,z=0),0!==e.y&&(G=e.y>0?S:D,C=0);r(),s(w)}else Q=!1}}var E=t.source,L=void 0===E?document:E,b=t.update,M=t.multiplier,g=void 0===M?1:M,T=t.friction,B=void 0===T?.92:T,q=t.initialValues,X=t.boundX,Y=t.boundY,A=t.bounce,F=void 0===A||A;n(this,e);var I,P,S,D,R,U,V,j,k,z,C,O=0,G=0,H=.3*g,J=!1,K=!1,N=!1,Q=!1,W=[];!function(){if(!(L="string"==typeof L?document.querySelector(L):L))throw new Error("IMPETUS: source not found.");if(!b)throw new Error("IMPETUS: update function not defined.");q&&(q[0]&&(O=q[0]),q[1]&&(G=q[1]),r()),X&&(I=X[0],P=X[1]),Y&&(S=Y[0],D=Y[1]),L.addEventListener("touchstart",d),L.addEventListener("mousedown",d)}(),this.destroy=function(){return L.removeEventListener("touchstart",d),L.removeEventListener("mousedown",d),null},this.pause=function(){K=!1,N=!0},this.resume=function(){N=!1},this.setValues=function(e,t){"number"==typeof e&&(O=e),"number"==typeof t&&(G=t)},this.setMultiplier=function(e){g=e,H=.3*g},this.setBoundX=function(e){I=e[0],P=e[1]},this.setBoundY=function(e){S=e[0],D=e[1]}};t.exports=r;var s=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}()}); \ No newline at end of file +!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")}function o(){var e=!1;try{var t=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,t)}catch(e){}return o=function(){return e},e}var i=.04,u=.11;window.addEventListener("touchmove",function(){});var r=function e(t){function r(){g.call(L,H,J)}function c(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 d(e){var t=c(e);Q||W||(Q=!0,Z=!1,z=t.id,U=j="y"!==M?t.x:U,V=k="x"!==M?t.y:V,$=[],m(U,V),document.addEventListener("touchmove",a,!!o()&&{passive:!1}),document.addEventListener("touchend",f),document.addEventListener("touchcancel",v),document.addEventListener("mousemove",a,!!o()&&{passive:!1}),document.addEventListener("mouseup",f))}function a(e){e.preventDefault();var t=c(e);Q&&t.id===z&&(j="y"!==M?t.x:j,k="x"!==M?t.y:k,m(U,V),y())}function f(e){var t=c(e);Q&&t.id===z&&v()}function v(){Q=!1,m(U,V),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();$.length>0&&!(n-$[0].time<=100);)$.shift();$.push({x:e,y:t,time:n})}function h(){var e=j-U||0,t=k-V||0;if(H+=e*B,J+=t*B,P){var n=p();0!==n.x&&(H-=e*l(n.x)*B),0!==n.y&&(J-=t*l(n.y)*B)}else p(!0);r(),U=j,V=k,N=!1}function l(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function y(){N||s(h),N=!0}function p(e){var t=0,n=0;return"y"!==M&&(void 0!==S&&HC&&(t=C-H)),"x"!==M&&(void 0!==D&&JR&&(n=R-J)),e&&(0!==t&&(H=t>0?S:C),0!==n&&(J=n>0?D:R)),{x:t,y:n,inBounds:0===t&&0===n}}function x(){var e=$[0],t=$[$.length-1],n=t.x-e.x,o=t.y-e.y,i=t.time-e.time,u=i/15/B;O=n/u||0,G=o/u||0;var r=p();(Math.abs(O)>1||Math.abs(G)>1||!r.inBounds)&&(Z=!0,s(w))}function w(){if(Z){O*=A,G*=A,H+=O,J+=G;var e=p();if(Math.abs(O)>K||Math.abs(G)>K||!e.inBounds){if(P){if(0!==e.x)if(e.x*O<=0)O+=e.x*i;else{var t=e.x>0?2.5:-2.5;O=(e.x+t)*u}if(0!==e.y)if(e.y*G<=0)G+=e.y*i;else{var t=e.y>0?2.5:-2.5;G=(e.y+t)*u}}else 0!==e.x&&(H=e.x>0?S:C,O=0),0!==e.y&&(J=e.y>0?D:R,G=0);r(),s(w)}else Z=!1}}var E=t.source,L=void 0===E?document:E,b=t.axis,M=void 0!==b&&b,g=t.update,T=t.multiplier,B=void 0===T?1:T,q=t.friction,A=void 0===q?.92:q,X=t.initialValues,Y=t.boundX,F=t.boundY,I=t.bounce,P=void 0===I||I;n(this,e);var S,C,D,R,U,V,j,k,z,O,G,H=0,J=0,K=.3*B,N=!1,Q=!1,W=!1,Z=!1,$=[];!function(){if(!(L="string"==typeof L?document.querySelector(L):L))throw new Error("IMPETUS: source not found.");if(!g)throw new Error("IMPETUS: update function not defined.");X&&(X[0]&&(H=X[0]),X[1]&&(J=X[1]),r()),Y&&(S=Y[0],C=Y[1]),F&&(D=F[0],R=F[1]),L.addEventListener("touchstart",d),L.addEventListener("mousedown",d)}(),this.destroy=function(){return L.removeEventListener("touchstart",d),L.removeEventListener("mousedown",d),null},this.pause=function(){Q=!1,W=!0},this.resume=function(){W=!1},this.setValues=function(e,t){"number"==typeof e&&(H=e),"number"==typeof t&&(J=t)},this.setMultiplier=function(e){B=e,K=.3*B},this.setBoundX=function(e){S=e[0],C=e[1]},this.setBoundY=function(e){D=e[0],R=e[1]},this.setAxis=function(e){var t=(e||"").toLowerCase();M="x"===t?"x":"y"===t&&"y"}};t.exports=r;var s=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}()}); \ No newline at end of file diff --git a/docs/impetus.min.js b/docs/impetus.min.js index 49fd819..ad81a50 100644 --- a/docs/impetus.min.js +++ b/docs/impetus.min.js @@ -1 +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")}function o(){var e=!1;try{var t=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,t)}catch(e){}return o=function(){return e},e}var i=.04,u=.11;window.addEventListener("touchmove",function(){});var r=function e(t){function r(){b.call(L,O,G)}function c(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 d(e){var t=c(e);K||N||(K=!0,Q=!1,k=t.id,R=V=t.x,U=j=t.y,W=[],m(R,U),document.addEventListener("touchmove",a,!!o()&&{passive:!1}),document.addEventListener("touchend",f),document.addEventListener("touchcancel",v),document.addEventListener("mousemove",a,!!o()&&{passive:!1}),document.addEventListener("mouseup",f))}function a(e){e.preventDefault();var t=c(e);K&&t.id===k&&(V=t.x,j=t.y,m(R,U),p())}function f(e){var t=c(e);K&&t.id===k&&v()}function v(){K=!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,t=j-U;if(O+=e*g,G+=t*g,F){var n=y();0!==n.x&&(O-=e*h(n.x)*g),0!==n.y&&(G-=t*h(n.y)*g)}else y(!0);r(),R=V,U=j,J=!1}function h(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function p(){J||s(l),J=!0}function y(e){var t=0,n=0;return void 0!==I&&OP&&(t=P-O),void 0!==S&&GD&&(n=D-G),e&&(0!==t&&(O=t>0?I:P),0!==n&&(G=n>0?S:D)),{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/g;z=n/u||0,C=o/u||0;var r=y();(Math.abs(z)>1||Math.abs(C)>1||!r.inBounds)&&(Q=!0,s(w))}function w(){if(Q){z*=B,C*=B,O+=z,G+=C;var e=y();if(Math.abs(z)>H||Math.abs(C)>H||!e.inBounds){if(F){if(0!==e.x)if(e.x*z<=0)z+=e.x*i;else{var t=e.x>0?2.5:-2.5;z=(e.x+t)*u}if(0!==e.y)if(e.y*C<=0)C+=e.y*i;else{var t=e.y>0?2.5:-2.5;C=(e.y+t)*u}}else 0!==e.x&&(O=e.x>0?I:P,z=0),0!==e.y&&(G=e.y>0?S:D,C=0);r(),s(w)}else Q=!1}}var E=t.source,L=void 0===E?document:E,b=t.update,M=t.multiplier,g=void 0===M?1:M,T=t.friction,B=void 0===T?.92:T,q=t.initialValues,X=t.boundX,Y=t.boundY,A=t.bounce,F=void 0===A||A;n(this,e);var I,P,S,D,R,U,V,j,k,z,C,O=0,G=0,H=.3*g,J=!1,K=!1,N=!1,Q=!1,W=[];!function(){if(!(L="string"==typeof L?document.querySelector(L):L))throw new Error("IMPETUS: source not found.");if(!b)throw new Error("IMPETUS: update function not defined.");q&&(q[0]&&(O=q[0]),q[1]&&(G=q[1]),r()),X&&(I=X[0],P=X[1]),Y&&(S=Y[0],D=Y[1]),L.addEventListener("touchstart",d),L.addEventListener("mousedown",d)}(),this.destroy=function(){return L.removeEventListener("touchstart",d),L.removeEventListener("mousedown",d),null},this.pause=function(){K=!1,N=!0},this.resume=function(){N=!1},this.setValues=function(e,t){"number"==typeof e&&(O=e),"number"==typeof t&&(G=t)},this.setMultiplier=function(e){g=e,H=.3*g},this.setBoundX=function(e){I=e[0],P=e[1]},this.setBoundY=function(e){S=e[0],D=e[1]}};t.exports=r;var s=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}()}); \ No newline at end of file +!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")}function o(){var e=!1;try{var t=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,t)}catch(e){}return o=function(){return e},e}var i=.04,u=.11;window.addEventListener("touchmove",function(){});var r=function e(t){function r(){g.call(L,H,J)}function c(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 d(e){var t=c(e);Q||W||(Q=!0,Z=!1,z=t.id,U=j="y"!==M?t.x:U,V=k="x"!==M?t.y:V,$=[],m(U,V),document.addEventListener("touchmove",a,!!o()&&{passive:!1}),document.addEventListener("touchend",f),document.addEventListener("touchcancel",v),document.addEventListener("mousemove",a,!!o()&&{passive:!1}),document.addEventListener("mouseup",f))}function a(e){e.preventDefault();var t=c(e);Q&&t.id===z&&(j="y"!==M?t.x:j,k="x"!==M?t.y:k,m(U,V),y())}function f(e){var t=c(e);Q&&t.id===z&&v()}function v(){Q=!1,m(U,V),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();$.length>0&&!(n-$[0].time<=100);)$.shift();$.push({x:e,y:t,time:n})}function h(){var e=j-U||0,t=k-V||0;if(H+=e*B,J+=t*B,P){var n=p();0!==n.x&&(H-=e*l(n.x)*B),0!==n.y&&(J-=t*l(n.y)*B)}else p(!0);r(),U=j,V=k,N=!1}function l(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function y(){N||s(h),N=!0}function p(e){var t=0,n=0;return"y"!==M&&(void 0!==S&&HC&&(t=C-H)),"x"!==M&&(void 0!==D&&JR&&(n=R-J)),e&&(0!==t&&(H=t>0?S:C),0!==n&&(J=n>0?D:R)),{x:t,y:n,inBounds:0===t&&0===n}}function x(){var e=$[0],t=$[$.length-1],n=t.x-e.x,o=t.y-e.y,i=t.time-e.time,u=i/15/B;O=n/u||0,G=o/u||0;var r=p();(Math.abs(O)>1||Math.abs(G)>1||!r.inBounds)&&(Z=!0,s(w))}function w(){if(Z){O*=A,G*=A,H+=O,J+=G;var e=p();if(Math.abs(O)>K||Math.abs(G)>K||!e.inBounds){if(P){if(0!==e.x)if(e.x*O<=0)O+=e.x*i;else{var t=e.x>0?2.5:-2.5;O=(e.x+t)*u}if(0!==e.y)if(e.y*G<=0)G+=e.y*i;else{var t=e.y>0?2.5:-2.5;G=(e.y+t)*u}}else 0!==e.x&&(H=e.x>0?S:C,O=0),0!==e.y&&(J=e.y>0?D:R,G=0);r(),s(w)}else Z=!1}}var E=t.source,L=void 0===E?document:E,b=t.axis,M=void 0!==b&&b,g=t.update,T=t.multiplier,B=void 0===T?1:T,q=t.friction,A=void 0===q?.92:q,X=t.initialValues,Y=t.boundX,F=t.boundY,I=t.bounce,P=void 0===I||I;n(this,e);var S,C,D,R,U,V,j,k,z,O,G,H=0,J=0,K=.3*B,N=!1,Q=!1,W=!1,Z=!1,$=[];!function(){if(!(L="string"==typeof L?document.querySelector(L):L))throw new Error("IMPETUS: source not found.");if(!g)throw new Error("IMPETUS: update function not defined.");X&&(X[0]&&(H=X[0]),X[1]&&(J=X[1]),r()),Y&&(S=Y[0],C=Y[1]),F&&(D=F[0],R=F[1]),L.addEventListener("touchstart",d),L.addEventListener("mousedown",d)}(),this.destroy=function(){return L.removeEventListener("touchstart",d),L.removeEventListener("mousedown",d),null},this.pause=function(){Q=!1,W=!0},this.resume=function(){W=!1},this.setValues=function(e,t){"number"==typeof e&&(H=e),"number"==typeof t&&(J=t)},this.setMultiplier=function(e){B=e,K=.3*B},this.setBoundX=function(e){S=e[0],C=e[1]},this.setBoundY=function(e){D=e[0],R=e[1]},this.setAxis=function(e){var t=(e||"").toLowerCase();M="x"===t?"x":"y"===t&&"y"}};t.exports=r;var s=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}()}); \ No newline at end of file diff --git a/src/Impetus.js b/src/Impetus.js index 920d895..5628ed1 100644 --- a/src/Impetus.js +++ b/src/Impetus.js @@ -12,6 +12,7 @@ window.addEventListener('touchmove', function() {}); export default class Impetus { constructor({ source: sourceEl = document, + axis: axis = false, update: updateCallback, multiplier = 1, friction = 0.92, @@ -145,6 +146,16 @@ export default class Impetus { boundYmax = boundY[1]; }; + /** + * Set the axis that will be scrolled + * @public + * @param {String|Boolean} axis + */ + this.setAxis = function(val) { + const lowerCaseVal = (val || '').toLowerCase(); + axis = lowerCaseVal === 'x' ? 'x' : (lowerCaseVal === 'y' ? 'y' : false); + }; + /** * Executes the update function */ @@ -185,8 +196,8 @@ export default class Impetus { decelerating = false; pointerId = event.id; - pointerLastX = pointerCurrentX = event.x; - pointerLastY = pointerCurrentY = event.y; + pointerLastX = pointerCurrentX = axis !== 'y' ? event.x : pointerLastX; + pointerLastY = pointerCurrentY = axis !== 'x' ? event.y : pointerLastY; trackingPoints = []; addTrackingPoint(pointerLastX, pointerLastY); @@ -208,8 +219,8 @@ export default class Impetus { var event = normalizeEvent(ev); if (pointerActive && event.id === pointerId) { - pointerCurrentX = event.x; - pointerCurrentY = event.y; + pointerCurrentX = axis !== 'y' ? event.x : pointerCurrentX; + pointerCurrentY = axis !== 'x' ? event.y : pointerCurrentY; addTrackingPoint(pointerLastX, pointerLastY); requestTick(); } @@ -263,8 +274,8 @@ export default class Impetus { * Calculate new values, call update function */ function updateAndRender() { - var pointerChangeX = pointerCurrentX - pointerLastX; - var pointerChangeY = pointerCurrentY - pointerLastY; + var pointerChangeX = (pointerCurrentX - pointerLastX) || 0; // prevent NaN + var pointerChangeY = (pointerCurrentY - pointerLastY) || 0; targetX += pointerChangeX * multiplier; targetY += pointerChangeY * multiplier; @@ -317,16 +328,20 @@ export default class Impetus { var xDiff = 0; var yDiff = 0; - if (boundXmin !== undefined && targetX < boundXmin) { - xDiff = boundXmin - targetX; - } else if (boundXmax !== undefined && targetX > boundXmax) { - xDiff = boundXmax - targetX; + if (axis !== 'y') { + if (boundXmin !== undefined && targetX < boundXmin) { + xDiff = boundXmin - targetX; + } else if (boundXmax !== undefined && targetX > boundXmax) { + xDiff = boundXmax - targetX; + } } - if (boundYmin !== undefined && targetY < boundYmin) { - yDiff = boundYmin - targetY; - } else if (boundYmax !== undefined && targetY > boundYmax) { - yDiff = boundYmax - targetY; + if (axis !== 'x') { + if (boundYmin !== undefined && targetY < boundYmin) { + yDiff = boundYmin - targetY; + } else if (boundYmax !== undefined && targetY > boundYmax) { + yDiff = boundYmax - targetY; + } } if (restrict) { From ee41b1753693cc0fce179d2e9a6f54883741cff7 Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Wed, 6 Sep 2017 16:11:44 -0700 Subject: [PATCH 03/14] docs: added axis config param and setAxis() method to the README --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 891f520..92b61a4 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,12 @@ Impetus will register itself as an AMD module if it's available. true Whether to stretch and rebound values when pulled outside the bounds. + + axis + String|Boolean + false + Whether to lock scrolling to a given axis ("x" or "y"), or to not lock at all (false). + @@ -117,6 +123,10 @@ Impetus will register itself as an AMD module if it's available. .setBoundY( <number[2]> ) Adjust the Y bound + + .setAxis( <number[2]> ) + Change the locked axis + .destroy() From 3259f4a9501072b67e7e3a52e6771dd5371b82db Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Wed, 6 Sep 2017 16:13:28 -0700 Subject: [PATCH 04/14] docs: documented the new window config param in the README --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 891f520..05fc49d 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,12 @@ Impetus will register itself as an AMD module if it's available. - This function will be called with the updated x and y values. + + window + Window + window + The window this instance of Impetus will be bound to (useful when dealing with iframes). + multiplier Number From 36b90d552136d1a7a241e874c2f5fbddf8192a28 Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Wed, 6 Sep 2017 17:12:12 -0700 Subject: [PATCH 05/14] =?UTF-8?q?feat:=20changed=20=E2=80=9Caxis=E2=80=9D?= =?UTF-8?q?=20API=20a=20bit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Impetus.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/Impetus.js b/src/Impetus.js index 5628ed1..16e99ff 100644 --- a/src/Impetus.js +++ b/src/Impetus.js @@ -12,7 +12,7 @@ window.addEventListener('touchmove', function() {}); export default class Impetus { constructor({ source: sourceEl = document, - axis: axis = false, + axis: axis = true, update: updateCallback, multiplier = 1, friction = 0.92, @@ -153,7 +153,7 @@ export default class Impetus { */ this.setAxis = function(val) { const lowerCaseVal = (val || '').toLowerCase(); - axis = lowerCaseVal === 'x' ? 'x' : (lowerCaseVal === 'y' ? 'y' : false); + axis = lowerCaseVal === 'x' ? 'x' : (lowerCaseVal === 'y' ? 'y' : !!val); }; /** @@ -196,8 +196,8 @@ export default class Impetus { decelerating = false; pointerId = event.id; - pointerLastX = pointerCurrentX = axis !== 'y' ? event.x : pointerLastX; - pointerLastY = pointerCurrentY = axis !== 'x' ? event.y : pointerLastY; + pointerLastX = pointerCurrentX = isAxisUnlocked('x') ? event.x : pointerLastX; + pointerLastY = pointerCurrentY = isAxisUnlocked('y') ? event.y : pointerLastY; trackingPoints = []; addTrackingPoint(pointerLastX, pointerLastY); @@ -219,8 +219,8 @@ export default class Impetus { var event = normalizeEvent(ev); if (pointerActive && event.id === pointerId) { - pointerCurrentX = axis !== 'y' ? event.x : pointerCurrentX; - pointerCurrentY = axis !== 'x' ? event.y : pointerCurrentY; + pointerCurrentX = isAxisUnlocked('x') ? event.x : pointerCurrentX; + pointerCurrentY = isAxisUnlocked('y') ? event.y : pointerCurrentY; addTrackingPoint(pointerLastX, pointerLastY); requestTick(); } @@ -238,6 +238,15 @@ export default class Impetus { } } + + /** + * Check if the supplied axis is locked + */ + function isAxisUnlocked(testVal) { + return axis === testVal || axis === true; + } + + /** * Stops movement tracking, starts animation */ @@ -328,7 +337,7 @@ export default class Impetus { var xDiff = 0; var yDiff = 0; - if (axis !== 'y') { + if (isAxisUnlocked('x')) { if (boundXmin !== undefined && targetX < boundXmin) { xDiff = boundXmin - targetX; } else if (boundXmax !== undefined && targetX > boundXmax) { @@ -336,7 +345,7 @@ export default class Impetus { } } - if (axis !== 'x') { + if (isAxisUnlocked('y')) { if (boundYmin !== undefined && targetY < boundYmin) { yDiff = boundYmin - targetY; } else if (boundYmax !== undefined && targetY > boundYmax) { From b444aeb663d2876032907667f20ec3350f5bac61 Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Wed, 6 Sep 2017 17:13:15 -0700 Subject: [PATCH 06/14] docs: update axis docs to match new API --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 92b61a4..6511f18 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ Impetus will register itself as an AMD module if it's available. axis String|Boolean false - Whether to lock scrolling to a given axis ("x" or "y"), or to not lock at all (false). + Whether to lock scrolling to a given axis ("x" or "y"), to unlock both (true), or to completely prevent scrolling (false). @@ -124,7 +124,7 @@ Impetus will register itself as an AMD module if it's available. Adjust the Y bound - .setAxis( <number[2]> ) + .setAxis( <string|boolean> ) Change the locked axis From f8dbf3ba9110309270f036231d785deaec6ab8a9 Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Tue, 12 Sep 2017 16:02:58 -0700 Subject: [PATCH 07/14] feat: added getters --- src/Impetus.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/Impetus.js b/src/Impetus.js index 3d71c88..78b0fe7 100644 --- a/src/Impetus.js +++ b/src/Impetus.js @@ -114,6 +114,15 @@ export default class Impetus { } }; + /** + * Retrieve the current x and y values + * @public + * @returns {Number[]} with the x and y values + */ + this.getValues = function() { + return [targetX, targetY]; + }; + /** * Update the multiplier value * @public @@ -124,6 +133,15 @@ export default class Impetus { stopThreshold = stopThresholdDefault * multiplier; }; + /** + * Retrieve the multiplier value + * @public + * @returns {Number} the multiplier value + */ + this.getMultiplier = function() { + return multiplier; + }; + /** * Update boundX value * @public @@ -134,6 +152,15 @@ export default class Impetus { boundXmax = boundX[1]; }; + /** + * Retrieve boundX value + * @public + * @returns {Number[]} boundX + */ + this.getBoundX = function() { + return [boundXmin, boundXmax]; + }; + /** * Update boundY value * @public @@ -144,6 +171,15 @@ export default class Impetus { boundYmax = boundY[1]; }; + /** + * Retrieve boundY value + * @public + * @returns {Number[]} boundY + */ + this.getBoundY = function() { + return [boundYmin, boundYmax]; + }; + /** * Executes the update function */ From d181ac5943e8e774d9ea1322ed13c93b12972eb0 Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Tue, 19 Sep 2017 15:14:42 -0700 Subject: [PATCH 08/14] fix: type error for the argument to setAxis --- src/Impetus.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Impetus.js b/src/Impetus.js index 16e99ff..e2f8cf5 100644 --- a/src/Impetus.js +++ b/src/Impetus.js @@ -152,8 +152,17 @@ export default class Impetus { * @param {String|Boolean} axis */ this.setAxis = function(val) { - const lowerCaseVal = (val || '').toLowerCase(); - axis = lowerCaseVal === 'x' ? 'x' : (lowerCaseVal === 'y' ? 'y' : !!val); + val = typeof val === 'string' ? val.toLowerCase() : val; + axis = val === 'x' ? 'x' : (val === 'y' ? 'y' : !!val); + }; + + /** + * Retrieve the current axis value + * @public + * @returns {String|Boolean} axis + */ + this.getAxis = function() { + return axis || false; }; /** From 0cfebc84fc68235c322d0ad0a7ae6a925a723c6b Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Tue, 19 Sep 2017 15:15:54 -0700 Subject: [PATCH 09/14] feat: getter for the active window --- src/Impetus.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Impetus.js b/src/Impetus.js index 3d71c88..d83e63f 100644 --- a/src/Impetus.js +++ b/src/Impetus.js @@ -144,6 +144,15 @@ export default class Impetus { boundYmax = boundY[1]; }; + /** + * Retrieve the window this instance has been attached to + * @public + * @returns {Window} window + */ + this.getWindow = function() { + return win; + }; + /** * Executes the update function */ From fa43023625d63a9dc4e8b400954f6503277f1a03 Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Wed, 18 Oct 2017 12:09:04 -0700 Subject: [PATCH 10/14] docs: updated default value for the new axis param --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6511f18..3e29e88 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ Impetus will register itself as an AMD module if it's available. axis String|Boolean - false + true Whether to lock scrolling to a given axis ("x" or "y"), to unlock both (true), or to completely prevent scrolling (false). From 089bb33b29247e6307e8deca524e8d3e0907226a Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Wed, 18 Oct 2017 12:15:17 -0700 Subject: [PATCH 11/14] docs: updated the README to include a description of the getters --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 891f520..9c5a4b2 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,22 @@ Impetus will register itself as an AMD module if it's available. + + .getMultiplier() + Retrieve the current multiplier. + + + .getValues() + Retrieve the current x and y output values. + + + .getBoundX() + Retrieve the current X bound + + + .getBoundY() + Retrieve the current Y bound + .pause() Disable movement processing. From 9be2cc163abed5ef49cbeaefb0aee71cdffece3a Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Wed, 18 Oct 2017 12:16:22 -0700 Subject: [PATCH 12/14] docs: updated the README to include the getAxis() method --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 3e29e88..ab6e152 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,10 @@ Impetus will register itself as an AMD module if it's available. + + .getAxis() + Retrieve the current the locked axis + .pause() Disable movement processing. From d8fe7d8ec527edc65bf9ca22fbe9dce2a668a898 Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Wed, 18 Oct 2017 12:17:42 -0700 Subject: [PATCH 13/14] docs: add getWindow method to the README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 05fc49d..2bcb39b 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,10 @@ Impetus will register itself as an AMD module if it's available. + + .getWindow() + Retrieve the window this instance is bound to. + .pause() Disable movement processing. From 7c3adfceb59fdbb27c305d019c1a514b4bf94b93 Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Mon, 30 Oct 2017 19:43:29 -0700 Subject: [PATCH 14/14] build --- dist/impetus.js | 79 +++++++++++++++++++++++++++++++++++++++------ dist/impetus.min.js | 2 +- docs/impetus.min.js | 2 +- 3 files changed, 72 insertions(+), 11 deletions(-) diff --git a/dist/impetus.js b/dist/impetus.js index 851beb1..dcfafeb 100644 --- a/dist/impetus.js +++ b/dist/impetus.js @@ -21,7 +21,7 @@ var Impetus = function Impetus(_ref) { var _ref$axis = _ref.axis; - var axis = _ref$axis === undefined ? false : _ref$axis; + var axis = _ref$axis === undefined ? true : _ref$axis; var _ref$window = _ref.window; var win = _ref$window === undefined ? window : _ref$window; var _ref$source = _ref.source; @@ -137,6 +137,15 @@ } }; + /** + * Retrieve the current x and y values + * @public + * @returns {Number[]} with the x and y values + */ + this.getValues = function () { + return [targetX, targetY]; + }; + /** * Update the multiplier value * @public @@ -147,6 +156,15 @@ stopThreshold = stopThresholdDefault * multiplier; }; + /** + * Retrieve the multiplier value + * @public + * @returns {Number} the multiplier value + */ + this.getMultiplier = function () { + return multiplier; + }; + /** * Update boundX value * @public @@ -157,6 +175,15 @@ boundXmax = boundX[1]; }; + /** + * Retrieve boundX value + * @public + * @returns {Number[]} boundX + */ + this.getBoundX = function () { + return [boundXmin, boundXmax]; + }; + /** * Update boundY value * @public @@ -167,14 +194,41 @@ boundYmax = boundY[1]; }; + /** + * Retrieve boundY value + * @public + * @returns {Number[]} boundY + */ + this.getBoundY = function () { + return [boundYmin, boundYmax]; + }; + /** * Set the axis that will be scrolled * @public * @param {String|Boolean} axis */ this.setAxis = function (val) { - var lowerCaseVal = (val || '').toLowerCase(); - axis = lowerCaseVal === 'x' ? 'x' : lowerCaseVal === 'y' ? 'y' : false; + val = typeof val === 'string' ? val.toLowerCase() : val; + axis = val === 'x' ? 'x' : val === 'y' ? 'y' : !!val; + }; + + /** + * Retrieve the current axis value + * @public + * @returns {String|Boolean} axis + */ + this.getAxis = function () { + return axis || false; + }; + + /** + * Retrieve the window this instance has been attached to + * @public + * @returns {Window} window + */ + this.getWindow = function () { + return win; }; /** @@ -218,8 +272,8 @@ decelerating = false; pointerId = event.id; - pointerLastX = pointerCurrentX = axis !== 'y' ? event.x : pointerLastX; - pointerLastY = pointerCurrentY = axis !== 'x' ? event.y : pointerLastY; + pointerLastX = pointerCurrentX = isAxisUnlocked('x') ? event.x : pointerLastX; + pointerLastY = pointerCurrentY = isAxisUnlocked('y') ? event.y : pointerLastY; trackingPoints = []; addTrackingPoint(pointerLastX, pointerLastY); @@ -241,8 +295,8 @@ var event = normalizeEvent(ev); if (pointerActive && event.id === pointerId) { - pointerCurrentX = axis !== 'y' ? event.x : pointerCurrentX; - pointerCurrentY = axis !== 'x' ? event.y : pointerCurrentY; + pointerCurrentX = isAxisUnlocked('x') ? event.x : pointerCurrentX; + pointerCurrentY = isAxisUnlocked('y') ? event.y : pointerCurrentY; addTrackingPoint(pointerLastX, pointerLastY); requestTick(); } @@ -260,6 +314,13 @@ } } + /** + * Check if the supplied axis is locked + */ + function isAxisUnlocked(testVal) { + return axis === testVal || axis === true; + } + /** * Stops movement tracking, starts animation */ @@ -347,7 +408,7 @@ var xDiff = 0; var yDiff = 0; - if (axis !== 'y') { + if (isAxisUnlocked('x')) { if (boundXmin !== undefined && targetX < boundXmin) { xDiff = boundXmin - targetX; } else if (boundXmax !== undefined && targetX > boundXmax) { @@ -355,7 +416,7 @@ } } - if (axis !== 'x') { + if (isAxisUnlocked('y')) { if (boundYmin !== undefined && targetY < boundYmin) { yDiff = boundYmin - targetY; } else if (boundYmax !== undefined && targetY > boundYmax) { diff --git a/dist/impetus.min.js b/dist/impetus.min.js index 9fdc6f2..7d884bb 100644 --- a/dist/impetus.min.js +++ b/dist/impetus.min.js @@ -1 +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")}function i(){var e=!1;try{var t=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,t)}catch(e){}return i=function(){return e},e}var o=.04,r=.11,u=function e(t){function u(){B.call(T,K,N)}function a(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 d(e){var t=a(e);Z||$||(Z=!0,_=!1,G=t.id,j=z="y"!==L?t.x:j,k=O="x"!==L?t.y:k,ee=[],m(j,k),M.addEventListener("touchmove",c,!!i()&&{passive:!1}),M.addEventListener("touchend",f),M.addEventListener("touchcancel",v),M.addEventListener("mousemove",c,!!i()&&{passive:!1}),M.addEventListener("mouseup",f))}function c(e){e.preventDefault();var t=a(e);Z&&t.id===G&&(z="y"!==L?t.x:z,O="x"!==L?t.y:O,m(j,k),y())}function f(e){var t=a(e);Z&&t.id===G&&v()}function v(){Z=!1,m(j,k),x(),M.removeEventListener("touchmove",c),M.removeEventListener("touchend",f),M.removeEventListener("touchcancel",v),M.removeEventListener("mouseup",f),M.removeEventListener("mousemove",c)}function m(e,t){for(var n=Date.now();ee.length>0&&!(n-ee[0].time<=100);)ee.shift();ee.push({x:e,y:t,time:n})}function h(){var e=z-j||0,t=O-k||0;if(K+=e*A,N+=t*A,C){var n=p();0!==n.x&&(K-=e*l(n.x)*A),0!==n.y&&(N-=t*l(n.y)*A)}else p(!0);u(),j=z,k=O,W=!1}function l(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function y(){W||s(h),W=!0}function p(e){var t=0,n=0;return"y"!==L&&(void 0!==D&&KR&&(t=R-K)),"x"!==L&&(void 0!==U&&NV&&(n=V-N)),e&&(0!==t&&(K=t>0?D:R),0!==n&&(N=n>0?U:V)),{x:t,y:n,inBounds:0===t&&0===n}}function x(){var e=ee[0],t=ee[ee.length-1],n=t.x-e.x,i=t.y-e.y,o=t.time-e.time,r=o/15/A;H=n/r||0,J=i/r||0;var u=p();(Math.abs(H)>1||Math.abs(J)>1||!u.inBounds)&&(_=!0,s(w))}function w(){if(_){H*=Y,J*=Y,K+=H,N+=J;var e=p();if(Math.abs(H)>Q||Math.abs(J)>Q||!e.inBounds){if(C){if(0!==e.x)if(e.x*H<=0)H+=e.x*o;else{var t=e.x>0?2.5:-2.5;H=(e.x+t)*r}if(0!==e.y)if(e.y*J<=0)J+=e.y*o;else{var t=e.y>0?2.5:-2.5;J=(e.y+t)*r}}else 0!==e.x&&(K=e.x>0?D:R,H=0),0!==e.y&&(N=e.y>0?U:V,J=0);u(),s(w)}else _=!1}}var E=t.axis,L=void 0!==E&&E,b=t.window,M=void 0===b?window:b,g=t.source,T=void 0===g?window:g,B=t.update,q=t.multiplier,A=void 0===q?1:q,X=t.friction,Y=void 0===X?.92:X,F=t.initialValues,I=t.boundX,P=t.boundY,S=t.bounce,C=void 0===S||S;n(this,e);var D,R,U,V,j,k,z,O,G,H,J,K=0,N=0,Q=.3*A,W=!1,Z=!1,$=!1,_=!1,ee=[];M.addEventListener("touchmove",function(){}),function(){if(!(T="string"==typeof T?M.document.querySelector(T):T))throw new Error("IMPETUS: source not found.");if(!B)throw new Error("IMPETUS: update function not defined.");F&&(F[0]&&(K=F[0]),F[1]&&(N=F[1]),u()),I&&(D=I[0],R=I[1]),P&&(U=P[0],V=P[1]),T.addEventListener("touchstart",d),T.addEventListener("mousedown",d)}(),this.destroy=function(){return T.removeEventListener("touchstart",d),T.removeEventListener("mousedown",d),null},this.pause=function(){Z=!1,$=!0},this.resume=function(){$=!1},this.setValues=function(e,t){"number"==typeof e&&(K=e),"number"==typeof t&&(N=t)},this.setMultiplier=function(e){A=e,Q=.3*A},this.setBoundX=function(e){D=e[0],R=e[1]},this.setBoundY=function(e){U=e[0],V=e[1]},this.setAxis=function(e){var t=(e||"").toLowerCase();L="x"===t?"x":"y"===t&&"y"}};t.exports=u;var s=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}()}); \ No newline at end of file +!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")}function i(){var e=!1;try{var t=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,t)}catch(e){}return i=function(){return e},e}var o=.04,r=.11,u=function e(t){function u(){A.call(T,K,N)}function c(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 d(e){var t=c(e);$||_||($=!0,ee=!1,G=t.id,k=O=v("x")?t.x:k,z=W=v("y")?t.y:z,te=[],l(k,z),M.addEventListener("touchmove",f,!!i()&&{passive:!1}),M.addEventListener("touchend",a),M.addEventListener("touchcancel",h),M.addEventListener("mousemove",f,!!i()&&{passive:!1}),M.addEventListener("mouseup",a))}function f(e){e.preventDefault();var t=c(e);$&&t.id===G&&(O=v("x")?t.x:O,W=v("y")?t.y:W,l(k,z),p())}function a(e){var t=c(e);$&&t.id===G&&h()}function v(e){return g===e||!0===g}function h(){$=!1,l(k,z),w(),M.removeEventListener("touchmove",f),M.removeEventListener("touchend",a),M.removeEventListener("touchcancel",h),M.removeEventListener("mouseup",a),M.removeEventListener("mousemove",f)}function l(e,t){for(var n=Date.now();te.length>0&&!(n-te[0].time<=100);)te.shift();te.push({x:e,y:t,time:n})}function m(){var e=O-k||0,t=W-z||0;if(K+=e*Y,N+=t*Y,C){var n=x();0!==n.x&&(K-=e*y(n.x)*Y),0!==n.y&&(N-=t*y(n.y)*Y)}else x(!0);u(),k=O,z=W,Z=!1}function y(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function p(){Z||s(m),Z=!0}function x(e){var t=0,n=0;return v("x")&&(void 0!==D&&KR&&(t=R-K)),v("y")&&(void 0!==U&&Nj&&(n=j-N)),e&&(0!==t&&(K=t>0?D:R),0!==n&&(N=n>0?U:j)),{x:t,y:n,inBounds:0===t&&0===n}}function w(){var e=te[0],t=te[te.length-1],n=t.x-e.x,i=t.y-e.y,o=t.time-e.time,r=o/15/Y;H=n/r||0,J=i/r||0;var u=x();(Math.abs(H)>1||Math.abs(J)>1||!u.inBounds)&&(ee=!0,s(E))}function E(){if(ee){H*=F,J*=F,K+=H,N+=J;var e=x();if(Math.abs(H)>Q||Math.abs(J)>Q||!e.inBounds){if(C){if(0!==e.x)if(e.x*H<=0)H+=e.x*o;else{var t=e.x>0?2.5:-2.5;H=(e.x+t)*r}if(0!==e.y)if(e.y*J<=0)J+=e.y*o;else{var t=e.y>0?2.5:-2.5;J=(e.y+t)*r}}else 0!==e.x&&(K=e.x>0?D:R,H=0),0!==e.y&&(N=e.y>0?U:j,J=0);u(),s(E)}else ee=!1}}var L=t.axis,g=void 0===L||L,b=t.window,M=void 0===b?window:b,B=t.source,T=void 0===B?window:B,A=t.update,X=t.multiplier,Y=void 0===X?1:X,q=t.friction,F=void 0===q?.92:q,I=t.initialValues,P=t.boundX,S=t.boundY,V=t.bounce,C=void 0===V||V;n(this,e);var D,R,U,j,k,z,O,W,G,H,J,K=0,N=0,Q=.3*Y,Z=!1,$=!1,_=!1,ee=!1,te=[];M.addEventListener("touchmove",function(){}),function(){if(!(T="string"==typeof T?M.document.querySelector(T):T))throw new Error("IMPETUS: source not found.");if(!A)throw new Error("IMPETUS: update function not defined.");I&&(I[0]&&(K=I[0]),I[1]&&(N=I[1]),u()),P&&(D=P[0],R=P[1]),S&&(U=S[0],j=S[1]),T.addEventListener("touchstart",d),T.addEventListener("mousedown",d)}(),this.destroy=function(){return T.removeEventListener("touchstart",d),T.removeEventListener("mousedown",d),null},this.pause=function(){$=!1,_=!0},this.resume=function(){_=!1},this.setValues=function(e,t){"number"==typeof e&&(K=e),"number"==typeof t&&(N=t)},this.getValues=function(){return[K,N]},this.setMultiplier=function(e){Y=e,Q=.3*Y},this.getMultiplier=function(){return Y},this.setBoundX=function(e){D=e[0],R=e[1]},this.getBoundX=function(){return[D,R]},this.setBoundY=function(e){U=e[0],j=e[1]},this.getBoundY=function(){return[U,j]},this.setAxis=function(e){e="string"==typeof e?e.toLowerCase():e,g="x"===e?"x":"y"===e?"y":!!e},this.getAxis=function(){return g||!1},this.getWindow=function(){return M}};t.exports=u;var s=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}()}); \ No newline at end of file diff --git a/docs/impetus.min.js b/docs/impetus.min.js index 9fdc6f2..7d884bb 100644 --- a/docs/impetus.min.js +++ b/docs/impetus.min.js @@ -1 +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")}function i(){var e=!1;try{var t=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,t)}catch(e){}return i=function(){return e},e}var o=.04,r=.11,u=function e(t){function u(){B.call(T,K,N)}function a(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 d(e){var t=a(e);Z||$||(Z=!0,_=!1,G=t.id,j=z="y"!==L?t.x:j,k=O="x"!==L?t.y:k,ee=[],m(j,k),M.addEventListener("touchmove",c,!!i()&&{passive:!1}),M.addEventListener("touchend",f),M.addEventListener("touchcancel",v),M.addEventListener("mousemove",c,!!i()&&{passive:!1}),M.addEventListener("mouseup",f))}function c(e){e.preventDefault();var t=a(e);Z&&t.id===G&&(z="y"!==L?t.x:z,O="x"!==L?t.y:O,m(j,k),y())}function f(e){var t=a(e);Z&&t.id===G&&v()}function v(){Z=!1,m(j,k),x(),M.removeEventListener("touchmove",c),M.removeEventListener("touchend",f),M.removeEventListener("touchcancel",v),M.removeEventListener("mouseup",f),M.removeEventListener("mousemove",c)}function m(e,t){for(var n=Date.now();ee.length>0&&!(n-ee[0].time<=100);)ee.shift();ee.push({x:e,y:t,time:n})}function h(){var e=z-j||0,t=O-k||0;if(K+=e*A,N+=t*A,C){var n=p();0!==n.x&&(K-=e*l(n.x)*A),0!==n.y&&(N-=t*l(n.y)*A)}else p(!0);u(),j=z,k=O,W=!1}function l(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function y(){W||s(h),W=!0}function p(e){var t=0,n=0;return"y"!==L&&(void 0!==D&&KR&&(t=R-K)),"x"!==L&&(void 0!==U&&NV&&(n=V-N)),e&&(0!==t&&(K=t>0?D:R),0!==n&&(N=n>0?U:V)),{x:t,y:n,inBounds:0===t&&0===n}}function x(){var e=ee[0],t=ee[ee.length-1],n=t.x-e.x,i=t.y-e.y,o=t.time-e.time,r=o/15/A;H=n/r||0,J=i/r||0;var u=p();(Math.abs(H)>1||Math.abs(J)>1||!u.inBounds)&&(_=!0,s(w))}function w(){if(_){H*=Y,J*=Y,K+=H,N+=J;var e=p();if(Math.abs(H)>Q||Math.abs(J)>Q||!e.inBounds){if(C){if(0!==e.x)if(e.x*H<=0)H+=e.x*o;else{var t=e.x>0?2.5:-2.5;H=(e.x+t)*r}if(0!==e.y)if(e.y*J<=0)J+=e.y*o;else{var t=e.y>0?2.5:-2.5;J=(e.y+t)*r}}else 0!==e.x&&(K=e.x>0?D:R,H=0),0!==e.y&&(N=e.y>0?U:V,J=0);u(),s(w)}else _=!1}}var E=t.axis,L=void 0!==E&&E,b=t.window,M=void 0===b?window:b,g=t.source,T=void 0===g?window:g,B=t.update,q=t.multiplier,A=void 0===q?1:q,X=t.friction,Y=void 0===X?.92:X,F=t.initialValues,I=t.boundX,P=t.boundY,S=t.bounce,C=void 0===S||S;n(this,e);var D,R,U,V,j,k,z,O,G,H,J,K=0,N=0,Q=.3*A,W=!1,Z=!1,$=!1,_=!1,ee=[];M.addEventListener("touchmove",function(){}),function(){if(!(T="string"==typeof T?M.document.querySelector(T):T))throw new Error("IMPETUS: source not found.");if(!B)throw new Error("IMPETUS: update function not defined.");F&&(F[0]&&(K=F[0]),F[1]&&(N=F[1]),u()),I&&(D=I[0],R=I[1]),P&&(U=P[0],V=P[1]),T.addEventListener("touchstart",d),T.addEventListener("mousedown",d)}(),this.destroy=function(){return T.removeEventListener("touchstart",d),T.removeEventListener("mousedown",d),null},this.pause=function(){Z=!1,$=!0},this.resume=function(){$=!1},this.setValues=function(e,t){"number"==typeof e&&(K=e),"number"==typeof t&&(N=t)},this.setMultiplier=function(e){A=e,Q=.3*A},this.setBoundX=function(e){D=e[0],R=e[1]},this.setBoundY=function(e){U=e[0],V=e[1]},this.setAxis=function(e){var t=(e||"").toLowerCase();L="x"===t?"x":"y"===t&&"y"}};t.exports=u;var s=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}()}); \ No newline at end of file +!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")}function i(){var e=!1;try{var t=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,t)}catch(e){}return i=function(){return e},e}var o=.04,r=.11,u=function e(t){function u(){A.call(T,K,N)}function c(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 d(e){var t=c(e);$||_||($=!0,ee=!1,G=t.id,k=O=v("x")?t.x:k,z=W=v("y")?t.y:z,te=[],l(k,z),M.addEventListener("touchmove",f,!!i()&&{passive:!1}),M.addEventListener("touchend",a),M.addEventListener("touchcancel",h),M.addEventListener("mousemove",f,!!i()&&{passive:!1}),M.addEventListener("mouseup",a))}function f(e){e.preventDefault();var t=c(e);$&&t.id===G&&(O=v("x")?t.x:O,W=v("y")?t.y:W,l(k,z),p())}function a(e){var t=c(e);$&&t.id===G&&h()}function v(e){return g===e||!0===g}function h(){$=!1,l(k,z),w(),M.removeEventListener("touchmove",f),M.removeEventListener("touchend",a),M.removeEventListener("touchcancel",h),M.removeEventListener("mouseup",a),M.removeEventListener("mousemove",f)}function l(e,t){for(var n=Date.now();te.length>0&&!(n-te[0].time<=100);)te.shift();te.push({x:e,y:t,time:n})}function m(){var e=O-k||0,t=W-z||0;if(K+=e*Y,N+=t*Y,C){var n=x();0!==n.x&&(K-=e*y(n.x)*Y),0!==n.y&&(N-=t*y(n.y)*Y)}else x(!0);u(),k=O,z=W,Z=!1}function y(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function p(){Z||s(m),Z=!0}function x(e){var t=0,n=0;return v("x")&&(void 0!==D&&KR&&(t=R-K)),v("y")&&(void 0!==U&&Nj&&(n=j-N)),e&&(0!==t&&(K=t>0?D:R),0!==n&&(N=n>0?U:j)),{x:t,y:n,inBounds:0===t&&0===n}}function w(){var e=te[0],t=te[te.length-1],n=t.x-e.x,i=t.y-e.y,o=t.time-e.time,r=o/15/Y;H=n/r||0,J=i/r||0;var u=x();(Math.abs(H)>1||Math.abs(J)>1||!u.inBounds)&&(ee=!0,s(E))}function E(){if(ee){H*=F,J*=F,K+=H,N+=J;var e=x();if(Math.abs(H)>Q||Math.abs(J)>Q||!e.inBounds){if(C){if(0!==e.x)if(e.x*H<=0)H+=e.x*o;else{var t=e.x>0?2.5:-2.5;H=(e.x+t)*r}if(0!==e.y)if(e.y*J<=0)J+=e.y*o;else{var t=e.y>0?2.5:-2.5;J=(e.y+t)*r}}else 0!==e.x&&(K=e.x>0?D:R,H=0),0!==e.y&&(N=e.y>0?U:j,J=0);u(),s(E)}else ee=!1}}var L=t.axis,g=void 0===L||L,b=t.window,M=void 0===b?window:b,B=t.source,T=void 0===B?window:B,A=t.update,X=t.multiplier,Y=void 0===X?1:X,q=t.friction,F=void 0===q?.92:q,I=t.initialValues,P=t.boundX,S=t.boundY,V=t.bounce,C=void 0===V||V;n(this,e);var D,R,U,j,k,z,O,W,G,H,J,K=0,N=0,Q=.3*Y,Z=!1,$=!1,_=!1,ee=!1,te=[];M.addEventListener("touchmove",function(){}),function(){if(!(T="string"==typeof T?M.document.querySelector(T):T))throw new Error("IMPETUS: source not found.");if(!A)throw new Error("IMPETUS: update function not defined.");I&&(I[0]&&(K=I[0]),I[1]&&(N=I[1]),u()),P&&(D=P[0],R=P[1]),S&&(U=S[0],j=S[1]),T.addEventListener("touchstart",d),T.addEventListener("mousedown",d)}(),this.destroy=function(){return T.removeEventListener("touchstart",d),T.removeEventListener("mousedown",d),null},this.pause=function(){$=!1,_=!0},this.resume=function(){_=!1},this.setValues=function(e,t){"number"==typeof e&&(K=e),"number"==typeof t&&(N=t)},this.getValues=function(){return[K,N]},this.setMultiplier=function(e){Y=e,Q=.3*Y},this.getMultiplier=function(){return Y},this.setBoundX=function(e){D=e[0],R=e[1]},this.getBoundX=function(){return[D,R]},this.setBoundY=function(e){U=e[0],j=e[1]},this.getBoundY=function(){return[U,j]},this.setAxis=function(e){e="string"==typeof e?e.toLowerCase():e,g="x"===e?"x":"y"===e?"y":!!e},this.getAxis=function(){return g||!1},this.getWindow=function(){return M}};t.exports=u;var s=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}()}); \ No newline at end of file