1.38.0lerp
Linearly interpolates between a start and end number using a normalized interpolation factor.
lerp(start, end, t):numberParameters
startnumberrequiredThe starting value.
endnumberrequiredThe ending value.
tnumberrequiredThe interpolation factor. A value of 0 returns start, while a value of 1 returns end. Values outside this range extrapolate beyond the endpoints.
Returns
numberReturns the linearly interpolated value between start and end.
Errors
Throws a RangeError if start is NaN.
Throws a RangeError if end is NaN.
Throws a RangeError if t is NaN.
Examples
Basic usage
Interpolates halfway between 0 and 100.
const result = lerp(0, 100, 0.5);
console.log(result);
// 50Start value
A factor of 0 returns the start value.
console.log(lerp(10, 20, 0));
// 10End value
A factor of 1 returns the end value.
console.log(lerp(10, 20, 1));
// 20Quarter interpolation
A factor of 0.25 moves one quarter of the way from start to end.
console.log(lerp(0, 100, 0.25));
// 25Negative values
Interpolation also works with negative start and end values.
console.log(lerp(-100, -20, 0.5));
// -60Reverse range
The start value can be greater than the end value.
console.log(lerp(100, 0, 0.25));
// 75Extrapolation
Values of t outside the 0 to 1 range extrapolate beyond the start and end values.
console.log(lerp(0, 100, 1.5));
// 150
console.log(lerp(0, 100, -0.5));
// -50NaN start
A NaN start value is rejected.
lerp(NaN, 100, 0.5);
// RangeError: lerp: start must not be NaNNaN end
A NaN end value is rejected.
lerp(0, NaN, 0.5);
// RangeError: lerp: end must not be NaNNaN interpolation factor
A NaN interpolation factor is rejected.
lerp(0, 100, NaN);
// RangeError: lerp: t must not be NaN