Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements event #38

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
dist
.idea
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
});
Expand Down Expand Up @@ -39,11 +39,29 @@ Impetus will register itself as an AMD module if it's available.
<td>Element reference or query string for the target on which to listen for movement.</td>
</tr>
<tr>
<th scope="row" align="left"><code>update</code> (required)</th>
<th scope="row" align="left"><code>onStart</code></th>
<td><code>function(x, y)</code></td>
<td>-</td>
<td>This function will be called when starting to drag the element</td>
</tr>
<tr>
<th scope="row" align="left"><code>onUpdate</code> (required)</th>
<td><code>function(x, y)</code></td>
<td>-</td>
<td>This function will be called with the updated <var>x</var> and <var>y</var> values.</td>
</tr>
<tr>
<th scope="row" align="left"><code>onStartDecelerating</code></th>
<td><code>function(x, y)</code></td>
<td>-</td>
<td>This function will be called when the deceleration begun (and drag has ended)</td>
</tr>
<tr>
<th scope="row" align="left"><code>onEndDecelerating</code></th>
<td><code>function(x, y)</code></td>
<td>-</td>
<td>This function will be called when the deceleration has ended</td>
</tr>
<tr>
<th scope="row" align="left"><code>multiplier</code></th>
<td><code>Number</code></td>
Expand Down
40 changes: 39 additions & 1 deletion dist/impetus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -200,6 +233,7 @@
function onDown(ev) {
var event = normalizeEvent(ev);
if (!pointerActive && !paused) {
callStartCallback();
pointerActive = true;
decelerating = false;
pointerId = event.id;
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -446,6 +483,7 @@
requestAnimFrame(stepDecelAnim);
} else {
decelerating = false;
callEndDeceleratingCallback();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/impetus.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/impetus.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading