Created
April 29, 2023 05:43
-
-
Save dsetzer/db50397e99e8e4de915c27e851c69767 to your computer and use it in GitHub Desktop.
Map a value from one range to another range
This file contains 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
/** | |
* 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