Created
February 17, 2020 13:13
-
-
Save MoritzKn/793b99f4f6b7d46d96640e6b333dcfac to your computer and use it in GitHub Desktop.
Rubber Band Effect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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