Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e6da5b9
feat: added a new “window” property, so that Impetus can be applied t…
Aug 30, 2017
b7728ca
feat: set the axis config param, or use setAxis(), to disable either …
Sep 6, 2017
8175e75
Merge branch 'master' into merged
Sep 6, 2017
ee41b17
docs: added axis config param and setAxis() method to the README
Sep 6, 2017
3259f4a
docs: documented the new window config param in the README
Sep 6, 2017
232be25
Merge branch 'disable-axis' into merged
Sep 6, 2017
6ff8929
Merge branch 'set-window' into merged
Sep 6, 2017
36b90d5
feat: changed “axis” API a bit
Sep 7, 2017
b444aeb
docs: update axis docs to match new API
Sep 7, 2017
47ae833
Merge branch 'disable-axis' into merged
Sep 7, 2017
f8dbf3b
feat: added getters
Sep 12, 2017
d181ac5
fix: type error for the argument to setAxis
Sep 19, 2017
0cfebc8
feat: getter for the active window
Sep 19, 2017
ae5672e
Merge branch 'getters' into merged
Sep 19, 2017
11b5fe5
Merge branch 'getters' into merged
Sep 19, 2017
a356b20
Merge branch 'disable-axis' into merged
Sep 19, 2017
fa43023
docs: updated default value for the new axis param
Oct 18, 2017
089bb33
docs: updated the README to include a description of the getters
Oct 18, 2017
9be2cc1
docs: updated the README to include the getAxis() method
Oct 18, 2017
d8fe7d8
docs: add getWindow method to the README
Oct 18, 2017
6c4c0a7
Merge branch 'getters' into merged
Oct 18, 2017
874134d
Merge branch 'set-window' into merged
Oct 18, 2017
a233dc6
Merge branch 'disable-axis' into merged
Oct 18, 2017
7c3adfc
build
Oct 31, 2017
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
48 changes: 44 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ Impetus will register itself as an AMD module if it's available.
<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>window</code></th>
<td><code>Window</code></td>
<td><code>window</code></td>
<td>The window this instance of Impetus will be bound to (useful when dealing with iframes).</td>
</tr>
<tr>
<th scope="row" align="left"><code>multiplier</code></th>
<td><code>Number</code></td>
Expand Down Expand Up @@ -80,6 +86,12 @@ Impetus will register itself as an AMD module if it's available.
<td><code>true</code></td>
<td>Whether to stretch and rebound values when pulled outside the bounds.</td>
</tr>
<tr>
<th scope="row" align="left"><code>axis</code></th>
<td><code>String|Boolean</code></td>
<td><code>true</code></td>
<td>Whether to lock scrolling to a given axis ("x" or "y"), to unlock both (true), or to completely prevent scrolling (false).</td>
</tr>
</tbody>
</table>

Expand All @@ -94,12 +106,32 @@ Impetus will register itself as an AMD module if it's available.
</thead>
<tbody>
<tr>
<th scope="row" align="left"><code>.pause()</code></th>
<td>Disable movement processing.</td>
<th scope="row" align="left"><code>.getAxis()</code></th>
<td>Retrieve the current the locked axis</td>
</tr>
<tr>
<th scope="row" align="left"><code>.resume()</code></th>
<td>Re-enable movement processing.</td>
<th scope="row" align="left"><code>.getMultiplier()</code></th>
<td>Retrieve the current <var>multiplier</var>.</td>
</tr>
<tr>
<th scope="row" align="left"><code>.getValues()</code></th>
<td>Retrieve the current <var>x</var> and <var>y</var> output values.</td>
</tr>
<tr>
<th scope="row" align="left"><code>.getBoundX()</code></th>
<td>Retrieve the current X bound</td>
</tr>
<tr>
<th scope="row" align="left"><code>.getBoundY()</code></th>
<td>Retrieve the current Y bound</td>
</tr>
<tr>
<th scope="row" align="left"><code>.getWindow()</code></th>
<td>Retrieve the <var>window</var> this instance is bound to.</td>
</tr>
<tr>
<th scope="row" align="left"><code>.setAxis( &lt;string|boolean&gt; )</code></th>
<td>Change the locked axis</td>
</tr>
<tr>
<th scope="row" align="left"><code>.setMultiplier( &lt;number&gt; )</code></th>
Expand All @@ -117,6 +149,14 @@ Impetus will register itself as an AMD module if it's available.
<th scope="row" align="left"><code>.setBoundY( &lt;number[2]&gt; )</code></th>
<td>Adjust the Y bound</td>
</tr>
<tr>
<th scope="row" align="left"><code>.pause()</code></th>
<td>Disable movement processing.</td>
</tr>
<tr>
<th scope="row" align="left"><code>.resume()</code></th>
<td>Re-enable movement processing.</td>
</tr>
<tr>
<th scope="row" align="left"><code>.destroy()</code></th>
<td>
Expand Down
139 changes: 109 additions & 30 deletions dist/impetus.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
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$axis = _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;
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;
Expand All @@ -49,11 +49,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.');
}
Expand Down Expand Up @@ -133,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
Expand All @@ -143,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
Expand All @@ -153,6 +175,15 @@
boundXmax = boundX[1];
};

/**
* Retrieve boundX value
* @public
* @returns {Number[]} boundX
*/
this.getBoundX = function () {
return [boundXmin, boundXmax];
};

/**
* Update boundY value
* @public
Expand All @@ -163,6 +194,43 @@
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) {
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;
};

/**
* Executes the update function
*/
Expand Down Expand Up @@ -204,17 +272,17 @@
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);

// @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);
}
}

Expand All @@ -227,8 +295,8 @@
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();
}
Expand All @@ -246,6 +314,13 @@
}
}

/**
* Check if the supplied axis is locked
*/
function isAxisUnlocked(testVal) {
return axis === testVal || axis === true;
}

/**
* Stops movement tracking, starts animation
*/
Expand All @@ -254,11 +329,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);
}

/**
Expand All @@ -282,8 +357,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;
Expand Down Expand Up @@ -333,16 +408,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 (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) {
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.

Loading