Remap

Translate a value from one range to another

Remap is the combination of normalize and lerp in a single operation. It takes a value from a source range and proportionally maps it to a target range — no manual normalization step required.

This is one of the most versatile tools in creative coding. Sensor readings to screen coordinates, mouse position to rotation angle, time to opacity — any situation where you need to translate between different scales is a job for remap.

Internally, the value is first clamped to the source range, then normalized to 0–1, and finally interpolated to the target range. This makes remap safe by default: input values outside the source range won't produce unexpected results.

function remap(
  origMin: number, origMax: number,
  targetMin: number, targetMax: number,
  value: number
): number {
  const clamped = Math.min(Math.max(origMin, value), origMax);
  const t = (clamped - origMin) / (origMax - origMin);
  return targetMin + t * (targetMax - targetMin);
}

Patterns using Remap