All files / math move-towards.ts

100% Statements 4/4
100% Branches 2/2
100% Functions 1/1
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 217x                               17x 17x 17x    
const MS_PER_SECOND = 1000;
 
/**
 * Moves a value towards a target at a limited rate (a slew-rate limiter), whatever the frame rate: a needle
 * or a rudder that cannot jump.
 *
 * @param current - The current value.
 * @param target - The value to reach.
 * @param maxRatePerSecond - The largest change per second.
 * @param deltaMs - The time elapsed since the previous call, in milliseconds.
 * @returns The new value, never beyond `target`.
 * @example
 * moveTowards(0, 10, 5, 1000); // 5
 * moveTowards(8, 10, 5, 1000); // 10
 */
export function moveTowards(current: number, target: number, maxRatePerSecond: number, deltaMs: number): number {
  const maxStep = (maxRatePerSecond * deltaMs) / MS_PER_SECOND;
  const difference = target - current;
  return Math.abs(difference) <= maxStep ? target : current + Math.sign(difference) * maxStep;
}