Skip to content

Instantly share code, notes, and snippets.

@benatwork
Last active December 13, 2018 22:54
Show Gist options
  • Save benatwork/8aab3af01cae36a771eeaf6e1c384d85 to your computer and use it in GitHub Desktop.
Save benatwork/8aab3af01cae36a771eeaf6e1c384d85 to your computer and use it in GitHub Desktop.
// Restricts a number to the minimum/maximum
function clamp(value, min, max) {
return Math.min(max, Math.max(min, value));
}
// Linear interpolation
function lerp(value, min, max) {
return min * (1 - value) + max * value;
}
// Maps 1 set of numbers to another
function map(value, min1, max1, min2, max2) {
return lerp(normalize(value, min1, max1), min2, max2);
}
// Turns your value into a percentage normalize(5, 0, 10) = 0.5
function normalize(value, min, max) {
return(value - min) / (max - min);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment