Lerp: Blending Between Two Values
Lerp starts at and goes a fraction of the way to : gives , gives , and lands exactly halfway. It's a weighted average of the two ends, and nearly everything else in animation math is built out of it.
What the symbols mean
- Start value
Where you are at t = 0.
- End value
Where you are at t = 1.
- Blend amount
How far along you are: 0 at the start, 1 at the end. Values outside 0 to 1 extrapolate past the ends.
- The gap
The whole distance from a to b. t says what fraction of it to cover.
What the symbols mean
- Start point
Where the object is at t = 0, like the first camera shot.
- End point
Where it is at t = 1.
- The arrow from start to end
Subtract coordinates to get it; t of it is how far you've moved.
Explanation
Lerp is short for linear interpolation, and it's simply a way to get a value partway between two others. To interpolate is to fill in values between ones you know, and linear means the filled-in values fall on a straight line, so equal steps in give equal steps in the result. In there are only three pieces: (where you start), (where you want to end up) and (how far along you are, from to ). The is the whole gap, and is the part of the gap you've covered so far. That's it. Easing curves, Bézier curves, quaternion slerp: every fancier tool in this topic is built on this one line.
function lerp(a: number, b: number, t: number): number {
return a + t * (b - a);
}
Two ways to write it, and why they agree. Multiply out the first form: . The second form is a weighted average: gets weight , gets weight , and the weights always add to . Think about why that matters. Picture a seesaw with a weight of sitting on and a weight of sitting on : the lerp is the balance point. At , carries three quarters of the weight, so the balance point sits a quarter of the way along, close to . As long as stays between and , both weights are positive and the balance point can never leave the stretch between and .
The two forms are equal on paper but not quite in a computer. With floating-point rounding, at can miss by a hair, while lands on exactly. When an animation must end precisely on its target (a UI element that snaps to a pixel grid), use the weighted form or just set the final value directly.
Outside to . Nothing in the formula stops from being or . At you cover the whole gap and then half of it again, ending past ; at you're behind . That's extrapolation. Sometimes it's on purpose (an overshoot that springs back), but usually it's a bug: a progress value that ran past makes a bar overflow its frame. The next lesson's clamp is the fix.
Lerping points. A point is just several numbers, so lerp each coordinate with the same :
Read the second form with vectors (see Vectors in the Plane): is the arrow from the start to the end, and is the fraction of that arrow. As runs from to , slides along the straight segment between the two points. A cut-scene camera that glides from one framed shot to the next is doing exactly this to its position. There's an animation further down that builds the same moving point both ways at once.
Time and speed. In an animation, is usually elapsed time divided by the duration : , where is the seconds since the move began. Then changes by every second, a constant velocity. That's the honest, slightly robotic feel of a plain lerp: full speed from the first instant, then a dead stop. The Smoothstep lesson and the easing lessons are all about bending so motion starts and stops gently. In the graph of against you'll recognize a line from Linear Equations: slope , intercept .
Lerping colors, and why the middle goes gray. A screen color is three numbers, red, green and blue, each from to . Lerping a color lerps each channel. Try blue to yellow : halfway, every channel is , and equal red, green and blue is gray. It makes sense once you see it: blue and yellow are opposites in RGB, so as two channels climb, the third falls, and halfway they all meet in the middle. Picture the colors as the corners of a cube; the straight path from one corner to the opposite one runs right through the gray center. Even between colors that aren't opposites, the midpoint tends to look duller and darker than a real mix of the two lights, partly because the stored numbers aren't proportional to light (they're gamma-encoded, so averaging them doesn't average the light). Tools that care blend in a color space built to match how we see, such as OKLab, or lerp the hue around the color wheel instead.
A common mistake. Writing x = lerp(x, target, 0.1) every frame and thinking it lerps from the start to the target. It isn't a lerp from start to target at all. Since changes every frame, each frame covers of whatever gap is left, so it slows down forever, never quite arrives, and how fast it moves depends on how many frames the game draws each second. It's a different and useful tool, exponential smoothing, and Smoothing That Doesn't Depend on Frame Rate makes it behave. A true lerp keeps and fixed and moves .
Another one. Feeding seconds in as . For a -second move, s in means , not ; must be a fraction of the move, or the object flies past its target.
Lerp keeps coming back: run it backward and you get inverse lerp and remap; do it twice across a square and you get bilinear interpolation; lerp between lerps and you get a Bézier curve.
See it
Worked examples
A cut-scene camera slides at constant speed from shot A at to shot B at , taking seconds. Where is it s after it starts moving?
- 1
Turn time into a fraction first. Lerp's is how far through the move we are, not a number of seconds: .
Lerp the color blue toward yellow . Find the color at and at . What happens in the middle?
- 1
A color is three numbers, so lerp each channel separately, all with the same .
Practice
Type your answer and check it. Solve them all to complete the lesson.
Compute .
A loading bar's fill starts at the left edge, px, and is full when its right edge reaches px. The download is done. At what , in px, is the fill's right edge?
A notification card slides from px to px using . An overshoot effect pushes to for one frame. Where is the card on that frame, in px?
Find for , and .
Lerp the RGB color orange toward sky blue at . Enter the three channels.
A value is animated with . At it reads , and at it reads . Find and .
A security camera on a rail moves at constant speed from to , in meters, taking seconds. How fast is it moving, in m/s?
The hardest one here. Get it, and you've got this lesson.
Let , and . A point moves by for : two lerps running at once, and a third lerp between them. Find the -coordinate of as an expression in .
Where it's used
Lerp runs wherever something on a screen changes smoothly. A progress bar's width is . A cut-scene camera glides between two framed shots by lerping its position. A cross-fade lerps one image's opacity from to while the next goes from to , and an audio crossfade does the same to volume. Game characters blend a walk animation into a run by lerping joint poses with set by speed, and graphics cards lerp between neighboring texture pixels millions of times a frame. Whenever a designer drags a "mix" slider, there's a lerp underneath.