Skip to content

Instantly share code, notes, and snippets.

@dsetzer
Created April 29, 2023 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsetzer/db50397e99e8e4de915c27e851c69767 to your computer and use it in GitHub Desktop.
Save dsetzer/db50397e99e8e4de915c27e851c69767 to your computer and use it in GitHub Desktop.
Map a value from one range to another range
/**
* Map a value from one range to another
* @param {number} value - The value to map
* @param {number} inMin - The minimum value of the input range
* @param {number} inMax - The maximum value of the input range
* @param {number} outMin - The minimum value of the output range
* @param {number} outMax - The maximum value of the output range
* @returns {number} The mapped value
*/
const mapRange = (value, inMin, inMax, outMin, outMax) => {
return ((value - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;
};
// Example usage:
const inputValue = 50;
const mappedValue = mapRange(inputValue, 0, 100, -1, 1);
console.log(mappedValue); // Output: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment