CyrefJS
CCyrefJS
فا
Back to documentation
Number/Since 1.38.0

lerp

Linearly interpolates between a start and end number using a normalized interpolation factor.

lerp(start, end, t):number

Parameters

startnumberrequired

The starting value.

endnumberrequired

The ending value.

tnumberrequired

The interpolation factor. A value of 0 returns start, while a value of 1 returns end. Values outside this range extrapolate beyond the endpoints.

Returns

number

Returns 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

01

Basic usage

Interpolates halfway between 0 and 100.

TypeScript
const result = lerp(0, 100, 0.5);

console.log(result);
// 50
02

Start value

A factor of 0 returns the start value.

TypeScript
console.log(lerp(10, 20, 0));
// 10
03

End value

A factor of 1 returns the end value.

TypeScript
console.log(lerp(10, 20, 1));
// 20
04

Quarter interpolation

A factor of 0.25 moves one quarter of the way from start to end.

TypeScript
console.log(lerp(0, 100, 0.25));
// 25
05

Negative values

Interpolation also works with negative start and end values.

TypeScript
console.log(lerp(-100, -20, 0.5));
// -60
06

Reverse range

The start value can be greater than the end value.

TypeScript
console.log(lerp(100, 0, 0.25));
// 75
07

Extrapolation

Values of t outside the 0 to 1 range extrapolate beyond the start and end values.

TypeScript
console.log(lerp(0, 100, 1.5));
// 150

console.log(lerp(0, 100, -0.5));
// -50
08

NaN start

A NaN start value is rejected.

TypeScript
lerp(NaN, 100, 0.5);
// RangeError: lerp: start must not be NaN
09

NaN end

A NaN end value is rejected.

TypeScript
lerp(0, NaN, 0.5);
// RangeError: lerp: end must not be NaN
10

NaN interpolation factor

A NaN interpolation factor is rejected.

TypeScript
lerp(0, 100, NaN);
// RangeError: lerp: t must not be NaN
Built with@cyref/js·Official documentation