Created
June 15, 2018 09:49
-
-
Save jefflau/33f9d63e78466e4e8b382c6bec0918e5 to your computer and use it in GitHub Desktop.
Modulate function - good for parallax on scroll or movement on scroll
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
export function modulate(value, rangeA, rangeB, limit) { | |
let fromHigh, fromLow, result, toHigh, toLow | |
if (limit == null) { | |
limit = false | |
} | |
fromLow = rangeA[0] | |
fromHigh = rangeA[1] | |
toLow = rangeB[0] | |
toHigh = rangeB[1] | |
result = toLow + ((value - fromLow) / (fromHigh - fromLow)) * (toHigh - toLow) | |
if (limit === true) { | |
if (toLow < toHigh) { | |
if (result < toLow) { | |
return toLow | |
} | |
if (result > toHigh) { | |
return toHigh | |
} | |
} else { | |
if (result > toLow) { | |
return toLow | |
} | |
if (result < toHigh) { | |
return toHigh | |
} | |
} | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment