• 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.

    Parameters

    • n: number

      The number to map.

    • start1: number

      The lower bound of the input range.

    • stop1: number

      The upper bound of the input range.

    • start2: number

      The lower bound of the target range.

    • stop2: number

      The upper bound of the target range.

    • withinBounds: boolean = false

      If true, constrains the output to [start2, stop2]. Defaults to false.

    Returns number

    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