number.map()

x.map(in_min, in_max, out_min, out_max, [limit]) → number

Linearly scales the number from one range to another, with optional clamping to the output range.

The method form of Math.map() — maps the value from the input range to the output range. The optional limit argument clamps the result to the output range; if not specified the output is not limited.

Arguments

NameTypeRequiredDefaultDescription
in_min number yes — Lower bound of the input range.
in_max number yes — Upper bound of the input range.
out_min number yes — Lower bound of the output range.
out_max number yes — Upper bound of the output range.
limit boolean optional false true to clamp the result to the output range.

Returns

number The mapped (and optionally clamped) value.

Example

let waterLevel = 9; // feet

// Map the reservoir level (0–18 ft) to a 0–100 percentage,
// clamped to the output range by the optional true argument.
let reservoirPercentage = waterLevel.map(0, 18, 0, 100, true);
// reservoirPercentage = 50