Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rubber Band Effect
function rubberBandEffect(
val: number,
lowerBounds: number,
upperBounds: number,
padding: number = 25,
): number {
const lowerLimit = lowerBounds - padding;
const upperLimit = upperBounds + padding;
if (val < lowerLimit) {
const offset = Math.abs(val - lowerBounds);
const newOffset = (Math.log2(offset / padding) + 1) * padding;
return lowerBounds - newOffset;
} else if (val > upperLimit) {
const offset = val - upperBounds;
const newOffset = (Math.log2(offset / padding) + 1) * padding;
return upperBounds + newOffset;
}
return val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment