The number to map.
The lower bound of the input range.
The upper bound of the input range.
The lower bound of the target range.
The upper bound of the target range.
If true
, constrains the output to [start2, stop2]
. Defaults to false
.
The mapped value, optionally constrained to the target range.
// Map 5 from the range [0, 10] to [0, 100]
const result = mapValue(5, 0, 10, 0, 100); // Returns 50
// Map 15 from the range [0, 10] to [0, 100], with constraints
const constrained = mapValue(15, 0, 10, 0, 100, true); // Returns 100
// Reverse mapping
const reverse = mapValue(5, 0, 10, 100, 0); // Returns 50
// Reverse mapping with constraints
const constrainedReverse = mapValue(15, 0, 10, 100, 0, true); // Returns 100
Maps a number from one range to another.
The function transforms a value
n
from the range[start1, stop1]
to the range[start2, stop2]
. Optionally, the output can be constrained to the bounds of the target range.