TypeScript

clamp / lerp / mapRange

admin by @admin ADMIN
Jun 17, 2026
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Three numeric utilities you reach for constantly in animation, UI math, and audio code. Generic enough to use anywhere, no Math.* dance required.
TypeScript
Raw
export const clamp = (n: number, min: number, max: number): number =>
  Math.min(max, Math.max(min, n));

export const lerp  = (a: number, b: number, t: number): number =>
  a + (b - a) * clamp(t, 0, 1);

export const mapRange = (
  n: number, inMin: number, inMax: number,
  outMin: number, outMax: number,
): number => {
  const t = (n - inMin) / (inMax - inMin);
  return outMin + (outMax - outMin) * t;
};

clamp(15, 0, 10);                     // 10
lerp(0, 100, 0.25);                   // 25
mapRange(50, 0, 100, -1, 1);          // 0
mapRange(750, 0, 1000, 0, 360);       // 270  ← e.g. progress → degrees
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.