Skip to content

Instantly share code, notes, and snippets.

@jefflau
Created June 15, 2018 09:49
Show Gist options
  • Save jefflau/33f9d63e78466e4e8b382c6bec0918e5 to your computer and use it in GitHub Desktop.
Save jefflau/33f9d63e78466e4e8b382c6bec0918e5 to your computer and use it in GitHub Desktop.
Modulate function - good for parallax on scroll or movement on scroll
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