diff --git a/.gitignore b/.gitignore
index 76add87..fa49873 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
node_modules
-dist
\ No newline at end of file
+dist
+.idea
\ No newline at end of file
diff --git a/README.md b/README.md
index 891f520..c3ddbc3 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@ Impetus will probably never support anything other than simple momentum. If you
```javascript
var myImpetus = new Impetus({
source: myNode,
- update: function(x, y) {
+ onUpdate: function(x, y) {
// whatever you want to do with the values
}
});
@@ -39,11 +39,29 @@ Impetus will register itself as an AMD module if it's available.
Element reference or query string for the target on which to listen for movement. |
- update (required) |
+ onStart |
+ function(x, y) |
+ - |
+ This function will be called when starting to drag the element |
+
+
+ onUpdate (required) |
function(x, y) |
- |
This function will be called with the updated x and y values. |
+
+ onStartDecelerating |
+ function(x, y) |
+ - |
+ This function will be called when the deceleration begun (and drag has ended) |
+
+
+ onEndDecelerating |
+ function(x, y) |
+ - |
+ This function will be called when the deceleration has ended |
+
multiplier |
Number |
diff --git a/dist/impetus.js b/dist/impetus.js
index c609f23..ab8b1f8 100644
--- a/dist/impetus.js
+++ b/dist/impetus.js
@@ -26,7 +26,10 @@
var Impetus = function Impetus(_ref) {
var _ref$source = _ref.source;
var sourceEl = _ref$source === undefined ? document : _ref$source;
- var updateCallback = _ref.update;
+ var startCallback = _ref.onStart;
+ var updateCallback = _ref.onUpdate;
+ var startDeceleratingCallback = _ref.onStartDecelerating;
+ var endDeceleratingCallback = _ref.onEndDecelerating;
var _ref$multiplier = _ref.multiplier;
var multiplier = _ref$multiplier === undefined ? 1 : _ref$multiplier;
var _ref$friction = _ref.friction;
@@ -170,6 +173,36 @@
updateCallback.call(sourceEl, targetX, targetY);
}
+ /**
+ * Executes the start function
+ */
+ function callStartCallback() {
+ if (!startCallback) {
+ return;
+ }
+ startCallback.call(sourceEl, targetX, targetY);
+ }
+
+ /**
+ * Executes the start decelerating function
+ */
+ function callStartDeceleratingCallback() {
+ if (!startDeceleratingCallback) {
+ return;
+ }
+ startDeceleratingCallback.call(sourceEl, targetX, targetY);
+ }
+
+ /**
+ * Executes the end decelerating function
+ */
+ function callEndDeceleratingCallback() {
+ if (!endDeceleratingCallback) {
+ return;
+ }
+ endDeceleratingCallback.call(sourceEl, targetX, targetY);
+ }
+
/**
* Creates a custom normalized event object from touch and mouse events
* @param {Event} ev
@@ -200,6 +233,7 @@
function onDown(ev) {
var event = normalizeEvent(ev);
if (!pointerActive && !paused) {
+ callStartCallback();
pointerActive = true;
decelerating = false;
pointerId = event.id;
@@ -379,9 +413,12 @@
var diff = checkBounds();
+ callStartDeceleratingCallback();
if (Math.abs(decVelX) > 1 || Math.abs(decVelY) > 1 || !diff.inBounds) {
decelerating = true;
requestAnimFrame(stepDecelAnim);
+ } else {
+ callEndDeceleratingCallback();
}
}
@@ -446,6 +483,7 @@
requestAnimFrame(stepDecelAnim);
} else {
decelerating = false;
+ callEndDeceleratingCallback();
}
}
}
diff --git a/dist/impetus.min.js b/dist/impetus.min.js
index 49fd819..4005253 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,n){if("function"==typeof define&&define.amd)define(["exports","module"],n);else if("undefined"!=typeof exports&&"undefined"!=typeof module)n(exports,module);else{var t={exports:{}};n(t.exports,t),e.Impetus=t.exports}}(this,function(e,n){"use strict";function t(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}function o(){var e=!1;try{var n=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,n)}catch(e){}return o=function(){return e},e}var i=.04,u=.11;window.addEventListener("touchmove",function(){});var r=function e(n){function r(){B.call(M,Q,W)}function s(){T&&T.call(M,Q,W)}function d(){S&&S.call(M,Q,W)}function a(){q&&q.call(M,Q,W)}function f(e){if("touchmove"===e.type||"touchstart"===e.type||"touchend"===e.type){var n=e.targetTouches[0]||e.changedTouches[0];return{x:n.clientX,y:n.clientY,id:n.identifier}}return{x:e.clientX,y:e.clientY,id:null}}function v(e){var n=f(e);_||ee||(s(),_=!0,ne=!1,J=n.id,C=G=n.x,O=H=n.y,te=[],p(C,O),document.addEventListener("touchmove",m,!!o()&&{passive:!1}),document.addEventListener("touchend",l),document.addEventListener("touchcancel",h),document.addEventListener("mousemove",m,!!o()&&{passive:!1}),document.addEventListener("mouseup",l))}function m(e){e.preventDefault();var n=f(e);_&&n.id===J&&(G=n.x,H=n.y,p(C,O),w())}function l(e){var n=f(e);_&&n.id===J&&h()}function h(){_=!1,p(C,O),L(),document.removeEventListener("touchmove",m),document.removeEventListener("touchend",l),document.removeEventListener("touchcancel",h),document.removeEventListener("mouseup",l),document.removeEventListener("mousemove",m)}function p(e,n){for(var t=Date.now();te.length>0&&!(t-te[0].time<=100);)te.shift();te.push({x:e,y:n,time:t})}function y(){var e=G-C,n=H-O;if(Q+=e*X,W+=n*X,R){var t=E();0!==t.x&&(Q-=e*x(t.x)*X),0!==t.y&&(W-=n*x(t.y)*X)}else E(!0);r(),C=G,O=H,$=!1}function x(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function w(){$||c(y),$=!0}function E(e){var n=0,t=0;return void 0!==V&&Qj&&(n=j-Q),void 0!==k&&Wz&&(t=z-W),e&&(0!==n&&(Q=n>0?V:j),0!==t&&(W=t>0?k:z)),{x:n,y:t,inBounds:0===n&&0===t}}function L(){var e=te[0],n=te[te.length-1],t=n.x-e.x,o=n.y-e.y,i=n.time-e.time,u=i/15/X;K=t/u||0,N=o/u||0;var r=E();d(),Math.abs(K)>1||Math.abs(N)>1||!r.inBounds?(ne=!0,c(b)):a()}function b(){if(ne){K*=A,N*=A,Q+=K,W+=N;var e=E();if(Math.abs(K)>Z||Math.abs(N)>Z||!e.inBounds){if(R){if(0!==e.x)if(e.x*K<=0)K+=e.x*i;else{var n=e.x>0?2.5:-2.5;K=(e.x+n)*u}if(0!==e.y)if(e.y*N<=0)N+=e.y*i;else{var n=e.y>0?2.5:-2.5;N=(e.y+n)*u}}else 0!==e.x&&(Q=e.x>0?V:j,K=0),0!==e.y&&(W=e.y>0?k:z,N=0);r(),c(b)}else ne=!1,a()}}var g=n.source,M=void 0===g?document:g,T=n.onStart,B=n.onUpdate,S=n.onStartDecelerating,q=n.onEndDecelerating,D=n.multiplier,X=void 0===D?1:D,Y=n.friction,A=void 0===Y?.92:Y,F=n.initialValues,I=n.boundX,P=n.boundY,U=n.bounce,R=void 0===U||U;t(this,e);var V,j,k,z,C,O,G,H,J,K,N,Q=0,W=0,Z=.3*X,$=!1,_=!1,ee=!1,ne=!1,te=[];!function(){if(!(M="string"==typeof M?document.querySelector(M):M))throw new Error("IMPETUS: source not found.");if(!B)throw new Error("IMPETUS: update function not defined.");F&&(F[0]&&(Q=F[0]),F[1]&&(W=F[1]),r()),I&&(V=I[0],j=I[1]),P&&(k=P[0],z=P[1]),M.addEventListener("touchstart",v),M.addEventListener("mousedown",v)}(),this.destroy=function(){return M.removeEventListener("touchstart",v),M.removeEventListener("mousedown",v),null},this.pause=function(){_=!1,ee=!0},this.resume=function(){ee=!1},this.setValues=function(e,n){"number"==typeof e&&(Q=e),"number"==typeof n&&(W=n)},this.setMultiplier=function(e){X=e,Z=.3*X},this.setBoundX=function(e){V=e[0],j=e[1]},this.setBoundY=function(e){k=e[0],z=e[1]}};n.exports=r;var c=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..4005253 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,n){if("function"==typeof define&&define.amd)define(["exports","module"],n);else if("undefined"!=typeof exports&&"undefined"!=typeof module)n(exports,module);else{var t={exports:{}};n(t.exports,t),e.Impetus=t.exports}}(this,function(e,n){"use strict";function t(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}function o(){var e=!1;try{var n=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,n)}catch(e){}return o=function(){return e},e}var i=.04,u=.11;window.addEventListener("touchmove",function(){});var r=function e(n){function r(){B.call(M,Q,W)}function s(){T&&T.call(M,Q,W)}function d(){S&&S.call(M,Q,W)}function a(){q&&q.call(M,Q,W)}function f(e){if("touchmove"===e.type||"touchstart"===e.type||"touchend"===e.type){var n=e.targetTouches[0]||e.changedTouches[0];return{x:n.clientX,y:n.clientY,id:n.identifier}}return{x:e.clientX,y:e.clientY,id:null}}function v(e){var n=f(e);_||ee||(s(),_=!0,ne=!1,J=n.id,C=G=n.x,O=H=n.y,te=[],p(C,O),document.addEventListener("touchmove",m,!!o()&&{passive:!1}),document.addEventListener("touchend",l),document.addEventListener("touchcancel",h),document.addEventListener("mousemove",m,!!o()&&{passive:!1}),document.addEventListener("mouseup",l))}function m(e){e.preventDefault();var n=f(e);_&&n.id===J&&(G=n.x,H=n.y,p(C,O),w())}function l(e){var n=f(e);_&&n.id===J&&h()}function h(){_=!1,p(C,O),L(),document.removeEventListener("touchmove",m),document.removeEventListener("touchend",l),document.removeEventListener("touchcancel",h),document.removeEventListener("mouseup",l),document.removeEventListener("mousemove",m)}function p(e,n){for(var t=Date.now();te.length>0&&!(t-te[0].time<=100);)te.shift();te.push({x:e,y:n,time:t})}function y(){var e=G-C,n=H-O;if(Q+=e*X,W+=n*X,R){var t=E();0!==t.x&&(Q-=e*x(t.x)*X),0!==t.y&&(W-=n*x(t.y)*X)}else E(!0);r(),C=G,O=H,$=!1}function x(e){return 5e-6*Math.pow(e,2)+1e-4*e+.55}function w(){$||c(y),$=!0}function E(e){var n=0,t=0;return void 0!==V&&Qj&&(n=j-Q),void 0!==k&&Wz&&(t=z-W),e&&(0!==n&&(Q=n>0?V:j),0!==t&&(W=t>0?k:z)),{x:n,y:t,inBounds:0===n&&0===t}}function L(){var e=te[0],n=te[te.length-1],t=n.x-e.x,o=n.y-e.y,i=n.time-e.time,u=i/15/X;K=t/u||0,N=o/u||0;var r=E();d(),Math.abs(K)>1||Math.abs(N)>1||!r.inBounds?(ne=!0,c(b)):a()}function b(){if(ne){K*=A,N*=A,Q+=K,W+=N;var e=E();if(Math.abs(K)>Z||Math.abs(N)>Z||!e.inBounds){if(R){if(0!==e.x)if(e.x*K<=0)K+=e.x*i;else{var n=e.x>0?2.5:-2.5;K=(e.x+n)*u}if(0!==e.y)if(e.y*N<=0)N+=e.y*i;else{var n=e.y>0?2.5:-2.5;N=(e.y+n)*u}}else 0!==e.x&&(Q=e.x>0?V:j,K=0),0!==e.y&&(W=e.y>0?k:z,N=0);r(),c(b)}else ne=!1,a()}}var g=n.source,M=void 0===g?document:g,T=n.onStart,B=n.onUpdate,S=n.onStartDecelerating,q=n.onEndDecelerating,D=n.multiplier,X=void 0===D?1:D,Y=n.friction,A=void 0===Y?.92:Y,F=n.initialValues,I=n.boundX,P=n.boundY,U=n.bounce,R=void 0===U||U;t(this,e);var V,j,k,z,C,O,G,H,J,K,N,Q=0,W=0,Z=.3*X,$=!1,_=!1,ee=!1,ne=!1,te=[];!function(){if(!(M="string"==typeof M?document.querySelector(M):M))throw new Error("IMPETUS: source not found.");if(!B)throw new Error("IMPETUS: update function not defined.");F&&(F[0]&&(Q=F[0]),F[1]&&(W=F[1]),r()),I&&(V=I[0],j=I[1]),P&&(k=P[0],z=P[1]),M.addEventListener("touchstart",v),M.addEventListener("mousedown",v)}(),this.destroy=function(){return M.removeEventListener("touchstart",v),M.removeEventListener("mousedown",v),null},this.pause=function(){_=!1,ee=!0},this.resume=function(){ee=!1},this.setValues=function(e,n){"number"==typeof e&&(Q=e),"number"==typeof n&&(W=n)},this.setMultiplier=function(e){X=e,Z=.3*X},this.setBoundX=function(e){V=e[0],j=e[1]},this.setBoundY=function(e){k=e[0],z=e[1]}};n.exports=r;var c=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/index.htm b/docs/index.htm
index d9125c6..caad69c 100644
--- a/docs/index.htm
+++ b/docs/index.htm
@@ -182,7 +182,7 @@ Impetus.js
Usage
new Impetus({
source: myNode,
- update: function(x, y) {
+ onUpdate: function(x, y) {
// whatever you want to do with the values
}
});
@@ -206,7 +206,7 @@ Usage
initialValues: [dotParent.offsetWidth/2 - 25, 45],
boundX: [0, dotParent.offsetWidth - dotEl.offsetWidth],
boundY: [0, dotParent.offsetHeight - dotEl.offsetHeight],
- update: function(x, y) {
+ onUpdate: function(x, y) {
this.style.left = x + 'px';
this.style.top = y + 'px';
}
@@ -215,7 +215,7 @@ Usage
new Impetus({
source: '#Cube',
multiplier: 0.7,
- update: function(x, y) {
+ onUpdate: function(x, y) {
this.style.transform = this.style.webkitTransform = 'translateZ(-120px) rotateY('+x+'deg) rotateX('+(-y)+'deg)';
}
});
diff --git a/package.json b/package.json
index 48d25b1..0b05713 100644
--- a/package.json
+++ b/package.json
@@ -1,13 +1,13 @@
{
"name": "impetus",
- "version": "0.8.7",
+ "version": "1.0.0",
"main": "dist/impetus.js",
"homepage": "http://chrisbateman.github.io/impetus/",
"description": "Add momentum to anything. It's like iScroll, except not for scrolling. Supports mouse and touch events.",
"scripts": {
- "prebuild": "rm -rf dist && mkdir dist",
+ "prebuild": "rimraf dist && mkdir dist",
"build": "babel src/Impetus.js --modules umd -o dist/impetus.js && uglifyjs dist/impetus.js --comments -cmo dist/impetus.min.js",
- "postbuild": "cp dist/impetus.min.js docs",
+ "postbuild": "cpx dist/impetus.min.js docs/",
"version": "npm run build && git add dist docs -f"
},
"keywords": [
@@ -25,7 +25,9 @@
},
"devDependencies": {
"babel": "^5.8.23",
- "uglify-js": "^2.4.24"
+ "cpx": "^1.5.0",
+ "uglify-js": "^2.4.24",
+ "rimraf": "^2.6.2"
},
"engines": {
"npm": ">=2.13.0"
diff --git a/src/Impetus.js b/src/Impetus.js
index 920d895..b442867 100644
--- a/src/Impetus.js
+++ b/src/Impetus.js
@@ -12,7 +12,10 @@ window.addEventListener('touchmove', function() {});
export default class Impetus {
constructor({
source: sourceEl = document,
- update: updateCallback,
+ onStart: startCallback,
+ onUpdate: updateCallback,
+ onStartDecelerating: startDeceleratingCallback,
+ onEndDecelerating: endDeceleratingCallback,
multiplier = 1,
friction = 0.92,
initialValues,
@@ -152,6 +155,36 @@ export default class Impetus {
updateCallback.call(sourceEl, targetX, targetY);
}
+ /**
+ * Executes the start function
+ */
+ function callStartCallback() {
+ if (!startCallback) {
+ return;
+ }
+ startCallback.call(sourceEl, targetX, targetY);
+ }
+
+ /**
+ * Executes the start decelerating function
+ */
+ function callStartDeceleratingCallback() {
+ if (!startDeceleratingCallback) {
+ return;
+ }
+ startDeceleratingCallback.call(sourceEl, targetX, targetY);
+ }
+
+ /**
+ * Executes the end decelerating function
+ */
+ function callEndDeceleratingCallback() {
+ if (!endDeceleratingCallback) {
+ return;
+ }
+ endDeceleratingCallback.call(sourceEl, targetX, targetY);
+ }
+
/**
* Creates a custom normalized event object from touch and mouse events
* @param {Event} ev
@@ -181,6 +214,7 @@ export default class Impetus {
function onDown(ev) {
var event = normalizeEvent(ev);
if (!pointerActive && !paused) {
+ callStartCallback();
pointerActive = true;
decelerating = false;
pointerId = event.id;
@@ -364,9 +398,12 @@ export default class Impetus {
var diff = checkBounds();
+ callStartDeceleratingCallback();
if ((Math.abs(decVelX) > 1 || Math.abs(decVelY) > 1) || !diff.inBounds){
decelerating = true;
requestAnimFrame(stepDecelAnim);
+ } else {
+ callEndDeceleratingCallback();
}
}
@@ -432,6 +469,7 @@ export default class Impetus {
requestAnimFrame(stepDecelAnim);
} else {
decelerating = false;
+ callEndDeceleratingCallback();
}
}
}