Skip to content

Commit

Permalink
merged bounce branch
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbateman committed Sep 26, 2015
2 parents 6d6a757 + 4a55f45 commit 811887a
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 25 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ Impetus will register itself as an AMD module if it's available.
<td>-</td>
<td>Array of low and high values. X values will remain within these bounds</td>
</tr>
<tr>
<th scope="row" align="left">bounce</th>
<td>Boolean</td>
<td>true</td>
<td>Whether to stretch and rebound values when pulled outside the bounds</td>
</tr>
</tbody>
</table>

Expand Down
135 changes: 114 additions & 21 deletions impetus.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
var targetY = 0;
var multiplier = 1;
var friction = 0.92;
var stopThresholdDefault = 0.4;
var stopThresholdDefault = 0.3;
var stopThreshold = stopThresholdDefault;
var ticking = false;
var pointerActive = false;
var paused = false;
var decelerating = false;
var trackingPoints = [];
var bounce = true;
var bounceDeceleration = 0.04;
var bounceAcceleration = 0.11;


/**
Expand Down Expand Up @@ -193,17 +196,41 @@
* Calculate new values, call update function
*/
var update = function() {
targetX += (pointerCurrentX - pointerLastX) * multiplier;
targetY += (pointerCurrentY - pointerLastY) * multiplier;
var pointerChangeX = pointerCurrentX - pointerLastX;
var pointerChangeY = pointerCurrentY - pointerLastY;

targetX += pointerChangeX * multiplier;
targetY += pointerChangeY * multiplier;

if (bounce) {
var diff = checkBounds();
if (diff.x !== 0) {
targetX -= pointerChangeX * dragOutOfBoundsMultiplier(diff.x) * multiplier;
}
if (diff.y !== 0) {
targetY -= pointerChangeY * dragOutOfBoundsMultiplier(diff.y) * multiplier;
}
} else {
checkBounds(true);
}

checkBounds();
callUpdateCallback();

pointerLastX = pointerCurrentX;
pointerLastY = pointerCurrentY;
ticking = false;
};


/**
* Returns a value from around 0.5 to 1, based on distance
* @param {Number} val
*/
var dragOutOfBoundsMultiplier = function(val) {
return 0.000005 * Math.pow(val, 2) + 0.0001 * val + 0.55;
};


/**
* prevents animating faster than current framerate
*/
Expand All @@ -228,24 +255,44 @@
}
};


/**
* Keep values in bounds, if available
* Determine position relative to bounds
* @param {Boolean} restrict Whether to restrict target to bounds
*/
var checkBounds = function() {
var checkBounds = function(restrict) {
var xDiff = 0;
var yDiff = 0;

if (boundXmin !== undefined && targetX < boundXmin) {
targetX = boundXmin;
}
if (boundXmax !== undefined && targetX > boundXmax) {
targetX = boundXmax;
xDiff = boundXmin - targetX;
} else if (boundXmax !== undefined && targetX > boundXmax) {
xDiff = boundXmax - targetX;
}

if (boundYmin !== undefined && targetY < boundYmin) {
targetY = boundYmin;
yDiff = boundYmin - targetY;
} else if (boundYmax !== undefined && targetY > boundYmax) {
yDiff = boundYmax - targetY;
}
if (boundYmax !== undefined && targetY > boundYmax) {
targetY = boundYmax;

if (restrict) {
if (xDiff !== 0) {
targetX = (xDiff > 0) ? boundXmin : boundXmax;
}
if (yDiff !== 0) {
targetY = (yDiff > 0) ? boundYmin : boundYmax;
}
}

return {
x: xDiff,
y: yDiff,
inBounds: xDiff === 0 && yDiff === 0
};
};


/**
* Initialize animation of values coming to a stop
*/
Expand All @@ -259,15 +306,18 @@

var D = (timeOffset / 15) / multiplier;

decVelX = xOffset / D;
decVelY = yOffset / D;
decVelX = (xOffset / D) || 0; // prevent NaN
decVelY = (yOffset / D) || 0;

var diff = checkBounds();

if (Math.abs(decVelX) > 1 || Math.abs(decVelY) > 1) {
if ((Math.abs(decVelX) > 1 || Math.abs(decVelY) > 1) || !diff.inBounds){
decelerating = true;
requestAnimFrame(stepDecelAnim);
}
};


/**
* Animates values slowing down
*/
Expand All @@ -279,13 +329,51 @@
decVelX *= friction;
decVelY *= friction;

if (Math.abs(decVelX) > stopThreshold || Math.abs(decVelY) > stopThreshold) {
targetX += decVelX;
targetY += decVelY;
targetX += decVelX;
targetY += decVelY;

var diff = checkBounds();

if ((Math.abs(decVelX) > stopThreshold || Math.abs(decVelY) > stopThreshold) || !diff.inBounds) {

if (checkBounds()) {
decelerating = false;
if (bounce) {
var reboundAdjust = 2.5;

if (diff.x !== 0) {
if (diff.x * decVelX <= 0) {
decVelX += diff.x * bounceDeceleration;
} else {
var adjust = (diff.x > 0) ? reboundAdjust : -reboundAdjust;
decVelX = (diff.x + adjust) * bounceAcceleration;
}
}
if (diff.y !== 0) {
if (diff.y * decVelY <= 0) {
decVelY += diff.y * bounceDeceleration;
} else {
var adjust = (diff.y > 0) ? reboundAdjust : -reboundAdjust;
decVelY = (diff.y + adjust) * bounceAcceleration;
}
}
} else {
if (diff.x !== 0) {
if (diff.x > 0) {
targetX = boundXmin;
} else {
targetX = boundXmax;
}
decVelX = 0;
}
if (diff.y !== 0) {
if (diff.y > 0) {
targetY = boundYmin;
} else {
targetY = boundYmax;
}
decVelY = 0;
}
}

callUpdateCallback();

requestAnimFrame(stepDecelAnim);
Expand All @@ -295,6 +383,8 @@
};




/**
* Initialize instance
*/
Expand All @@ -321,6 +411,9 @@
if (typeof cfg.friction !== 'undefined') {
friction = cfg.friction || friction;
}
if (cfg.bounce === false) {
bounce = false;
}

if (cfg.initialValues) {
if (cfg.initialValues[0]) {
Expand Down
6 changes: 3 additions & 3 deletions 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 package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Impetus",
"version": "0.7.0",
"version": "0.8.0",
"main": "impetus.js",
"homepage": "http://chrisbateman.github.io/impetus/",
"author": "Chris Bateman",
Expand Down

0 comments on commit 811887a

Please sign in to comment.