Episode 45
The Halfway Moonwalk
Moon goes from 20 to 100 in 4 seconds. Equal parts of the time take equal parts of the move. This is linear interpolation.
















Episode transcript
A moon with two positions
Duck sees the moon jumping across stage between two endpoint positions. Robot has the two keyframe cards.
Diagram values: KEYFRAME t=0s x=20; KEYFRAME t=4s x=100
Duck: Moon jumps. I would prefer it to glide.
Robot is oriented in the x direction. The x values are increasing from left to right. The two ends are fixed.
Diagram values: LINEAR; 0s <= t <=4s; 20 ->100
Robot: Same time steps. Same distance steps.
Duck is 1/4 through a 4 second timeline. That's the first second.
Diagram values: u=(1-0)/(4-0)=1/4
Duck: 1 second and it's a quarter of the way.
The distance from endpoint to endpoint, 80 units, is split into four equal pieces. The first result is concealed.
Diagram values: 100-20=80; 1/4 OF80
Robot: What is one fourth of 80?
Measure the fraction
Duck puts moon to forty (after moving 20 from starting position 20)
Diagram values: t=1s; x=20+(1/4)*80=40
Duck: 20. Since it begins at 20, that means the moon is at 40.
Robot compares displacement 20 and final position 40.
Diagram values: START20; MOVE20; POSITION40
Robot: It depends where you start. The fraction is not the position.
Duck puts it 60 (halfway between the two fixed points) at 2 seconds.
Diagram values: t=2s; u=1/2; x=60
Duck: 60, it should be half way at 2 seconds.
Robot shows linear interpolation formula with the moon at sixty.
Diagram values: x=x0+u*(x1-x0); NO EASING
Robot: Equal parts of the time take equal parts of the move.
Another moment between
Duck sets clock for 3 sec. and moon for 80.
Diagram values: t=3s; u=3/4; x=80
Duck: 3 seconds = 3 quarters trip.
4 seconds and 2 keyframes. 100 is the second keyframe. duck catches the prop at the rail stop.
Diagram values: t=4s; u=1; x=100
Robot: At 4s u equals 1. You're at the second keyframe.
Duck: where's moon at 5 seconds? keyframe times (with labels) are 0 and 4 seconds.
Diagram values: t=5s; KEYFRAMES: 0s AND 4s
Duck: And 5 seconds?
The 5 second card is outside that range.
Diagram values: ONLY 0s <=t<=4s; NO EXTRAPOLATION
Robot: The 5 second card is outside that range.
A return journey
A separate rail is at the 90 start position second two. It ends at 30 end position second five.
Diagram values: t0=2s x0=90; t1=5s x1=30; QUERY t=3s
Duck: At three seconds, where?
There's also an arrow indicating the entire sixty unit shift to the left from end to end.
Diagram values: u=(3-2)/(5-2)=1/3; x1-x0=-60
Robot: So you've got 1 second in 3. Walk 1/3 of the way to the other side.
Duck sets practice moon to seventy, second three, twenty units left of start.
Diagram values: x=90+(1/3)*(-60)=70; t=3s x=70
Duck: 90 - 20 = 70
Duck takes a step to his left on the practice rail.
Diagram values: 2s <=t<=5s; LINEAR POSITION
Robot: If the moon travels to the left, the same applies.
The lesson
We have 2 key frames showing us the position of the moon at 2 points in time. If we want to interpolate a position between them we first calculate the fractional time elapsed u=(t-t0)/(t1-t0) We then add to the initial position the change in total position multiplied by this fraction. x=x0+u*(x1-x0) We are increasing horizontal coords to the right. The total change from 20 at 0 seconds to 100 at 4 seconds is 80. If it's 1 second into the time, the fraction of time elapsed is a quarter. So move from the initial position by 20 units. We get to 40. After 2 seconds, we are at 60. After 3 seconds, we are at 80. After 4 seconds we are at 100. If the time steps are equal, then the change in position is equal.
This same formula works in between whole seconds.
From 90 at 2 seconds to 30 at 5 seconds the total change is -60.
At 3 seconds we've elapsed 1 of the 3 seconds. So u = 1/3. Move was -20. So position is now 70.
We've only defined this linear position formula between key frame times. We've not made any decisions about what happens beyond the last key frame. Nor have we added easing.
Try it
This is linear interpolation.
New animation: interpolate between keyframe 1 and keyframe 2. keyframe 1: 12 at 1 second. keyframe 2: 44 at 5 seconds. what's the position at 4 seconds?
Answer
(4-1)/(5-1) = 3/4. That's the fraction of the interval. Not the number of seconds. Total position change: 44-12 = 32. 3/4 * 32 = 24. So the starting position + 24 = 12 + 24 = 36. Position at 4 secs is 36.
Sources
W3C CSS Values: combining values and interpolation (opens in a new tab)