diff --git a/README.md b/README.md index 891f520..ab6e152 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 + true + Whether to lock scrolling to a given axis ("x" or "y"), to unlock both (true), or to completely prevent scrolling (false). + @@ -93,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. @@ -117,6 +127,10 @@ Impetus will register itself as an AMD module if it's available. .setBoundY( <number[2]> ) Adjust the Y bound + + .setAxis( <string|boolean> ) + Change the locked axis + .destroy() 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..e2f8cf5 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 = true, update: updateCallback, multiplier = 1, friction = 0.92, @@ -145,6 +146,25 @@ export default class Impetus { boundYmax = boundY[1]; }; + /** + * Set the axis that will be scrolled + * @public + * @param {String|Boolean} axis + */ + this.setAxis = function(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; + }; + /** * Executes the update function */ @@ -185,8 +205,8 @@ export default class Impetus { decelerating = false; pointerId = event.id; - pointerLastX = pointerCurrentX = event.x; - pointerLastY = pointerCurrentY = event.y; + pointerLastX = pointerCurrentX = isAxisUnlocked('x') ? event.x : pointerLastX; + pointerLastY = pointerCurrentY = isAxisUnlocked('y') ? event.y : pointerLastY; trackingPoints = []; addTrackingPoint(pointerLastX, pointerLastY); @@ -208,8 +228,8 @@ export default class Impetus { var event = normalizeEvent(ev); if (pointerActive && event.id === pointerId) { - pointerCurrentX = event.x; - pointerCurrentY = event.y; + pointerCurrentX = isAxisUnlocked('x') ? event.x : pointerCurrentX; + pointerCurrentY = isAxisUnlocked('y') ? event.y : pointerCurrentY; addTrackingPoint(pointerLastX, pointerLastY); requestTick(); } @@ -227,6 +247,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 */ @@ -263,8 +292,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 +346,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 (isAxisUnlocked('x')) { + 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 (isAxisUnlocked('y')) { + if (boundYmin !== undefined && targetY < boundYmin) { + yDiff = boundYmin - targetY; + } else if (boundYmax !== undefined && targetY > boundYmax) { + yDiff = boundYmax - targetY; + } } if (restrict) {