Easing & Timing Functions — Web Animation

Easing (the speed pattern of a movement) turns time into progress, how far the movement has gone. ease-in starts slow then fast (31.5% at half the time)…

A motorbike taxi stopping in front of the homestay doesn't drop from 40 km/h to zero in an instant. It brakes gently: the closer it gets, the slower it goes. In CSS, a movement that is fast at the start and slows down at the end is called ease-out. When the taxi starts moving again, it starts slow and then speeds up: that is ease-in. A movement that keeps the same speed from start to end, like a robot, is called linear. All three are called easing, the speed pattern of a movement (in CSS, a timing function). Easing is the rule that turns time into progress, how far the movement has gone. With easing, you can calculate what share of the way the movement has covered after a given share of the time.

Easing keywords and their curves

Every easing keyword is really a short name for one cubic-bezier curve (a graph of time and progress, explained in the next section). We played a 1-second animation over 100 px, then read its position at three points in time, in Chromium 147, WebKit 26.4 and Firefox 146. The results were identical in all three browsers. Because the distance is 100 px, each px is 1% of the way:

Easing Same as 25% of time 50% of time 75% of time
linear cubic-bezier(0, 0, 1, 1) 25 px 50 px 75 px
ease cubic-bezier(0.25, 0.1, 0.25, 1) 40.9 px 80.2 px 96 px
ease-in cubic-bezier(0.42, 0, 1, 1) 9.3 px 31.5 px 62.2 px
ease-out cubic-bezier(0, 0, 0.58, 1) 37.8 px 68.5 px 90.7 px
ease-in-out cubic-bezier(0.42, 0, 0.58, 1) 12.9 px 50 px 87.1 px

Computing a cubic-bezier

The curve cubic-bezier(x1, y1, x2, y2) is shaped by four points: P0 = (0, 0), P1 = (x1, y1), P2 = (x2, y2), P3 = (1, 1). The x axis is time, the y axis is progress. The curve starts at P0 and ends at P3, while P1 and P2 pull the curve towards them. To compute a point on the curve, a helper number s runs from 0 to 1 along it:

For example, ease-out, which is cubic-bezier(0, 0, 0.58, 1), at s = 0.5:

Meaning: at 34.25% of the time, the motion has covered 50% of the way. The browser works in reverse, because what it knows is the time t:

  1. The browser looks for the s whose x(s) equals t.
  2. With that s, the browser computes y(s): the progress at time t.

Before you look at the table, take a guess. The Terrace Room card enters in 300 ms with ease-out, rising 16 px. At 150 ms, exactly half its time, how many px has the card risen? 8 px?

Time t s found Progress y Card (out of 16 px)
0.25 (75 ms) 0.418 0.378 up 6 px
0.5 (150 ms) 0.626 0.685 up 11 px
0.75 (225 ms) 0.811 0.907 up 14.5 px

At 150 ms the card has already risen 11 px of 16, not 8. The last 5 px take the final 150 ms, slowly. The JavaScript below calculates it the same way as the browser (** raises a number to a power):

function cubicBezier(x1, y1, x2, y2) {
  const x = (s) => 3 * (1 - s) ** 2 * s * x1 + 3 * (1 - s) * s ** 2 * x2 + s ** 3;
  const y = (s) => 3 * (1 - s) ** 2 * s * y1 + 3 * (1 - s) * s ** 2 * y2 + s ** 3;
  return (t) => {
    let lo = 0, hi = 1;
    for (let i = 0; i < 30; i++) {   // find the s where x(s) = t, by halving
      const mid = (lo + hi) / 2;
      if (x(mid) < t) lo = mid; else hi = mid;
    }
    return y((lo + hi) / 2);
  };
}

const easeOut = cubicBezier(0, 0, 0.58, 1);
console.log((16 * easeOut(0.5)).toFixed(1)); // "11.0": px risen at 150 ms

Past the target: y outside 0 to 1

Unlike x, y may be anything. For example, cubic-bezier(0.68, -0.55, 0.27, 1.55): a negative y1 makes the motion go backwards first, and a y2 above 1 makes it overshoot (go past the target) and come back. Measured in all three browsers: the furthest back is −9.3% at 19.8% of the time, the furthest out is 109.3% at 78.6% of the time, and it ends at exactly 100%. A photo gallery slid 300 px with this curve first goes back 28 px, then overshoots 28 px before it stops. A bounce like this feels playful. Use it only sometimes, not on a button pressed every day.

steps(): jumping step by step

steps(n, jump-...) cuts the animation into n stair steps: the progress jumps from one value to the next, with no motion in between. The jump-* keywords (Chrome 77, Firefox 65, Safari 14) set where the jumps happen. Here is the progress of steps(4, ...) that we measured in all three browsers. The column titles are shares of the time, the cells are shares of the progress:

Function 0% 10% 25% 50% 99.9% 100%
steps(4) = steps(4, jump-end) 0 0 25 50 75 100
steps(4, jump-start) 25 25 50 75 100 100
steps(4, jump-none) 0 0 33.3 66.7 100 100
steps(4, jump-both) 20 20 40 60 80 100

An example is the "Booking..." dots on the Book button. The dots appear one by one: none, one, two, then three dots, 300 ms each.

.dots { display: inline-block; animation: dots 1200ms steps(4, jump-none) infinite; }
@keyframes dots {
  from { clip-path: inset(0 100% 0 0); } /* every dot covered */
  to   { clip-path: inset(0 0 0 0); }    /* three dots visible */
}

With jump-none, the 4 steps are 0, 1/3, 2/3 and 1: from none to three dots. With plain steps(4) (jump-end), the last step (three dots) is only reached at the very end of a round. In an infinite animation, the next round starts from none at that same moment, so the three dots are never seen.

linear(): a curve from points

linear() (Chrome 113, Firefox 112, Safari 17.2) joins progress points with straight lines. Each point holds a progress value, which may be followed by its share of the time. linear(0, 0.25 50%, 1) means: start at 0, at 50% of the time the progress is 25%, then end at 1 (100%). We measured it: 12.5% at 25% of the time, 25% at 50%, 62.5% at 75%. It's for imitating curves a cubic-bezier can't draw, such as a spring that overshoots once and settles (comes to rest in its place):

.strip { transition: transform 300ms linear(0, 1.1 60%, 0.97 80%, 1); }

.strip is the row of photos in the gallery. At 60% of the time the photo has covered 110% (10% past). At 80% of the time it's back to 97%, and at 100% it rests in place.

Enter with ease-out, leave with ease-in

An element that enters needs a quick response, then a gentle landing. This way people see right away that something is coming, and their eyes can follow it until it stops. An element that leaves starts slow, then gets faster until it is gone. Nobody is waiting for that element, so it doesn't need to land. The motion documentation of Material Components for Android gives the curves in numbers:

For Name Value
entering the screen emphasized decelerate cubic-bezier(0.05, 0.7, 0.1, 1)
leaving the screen emphasized accelerate cubic-bezier(0.3, 0, 0.8, 0.15)
starting and ending on screen standard cubic-bezier(0.2, 0, 0, 1)

Emphasized decelerate (slowing down) has covered 62.1% at 10% of the time. Emphasized accelerate (speeding up) has covered only 15.4% at half the time. Mrs. Sri's page uses ease-out for what enters and ease-in for what leaves. Both are softer versions of the two emphasized curves above.

Duration: the further, the longer

The same documentation says: "duration should increase as the area/traversal of an animation increases". So a duration gets longer when the moving area or the distance it travels gets bigger. Material 3 has duration tokens (md.sys.motion.duration.*), names for ready-made durations, 50 ms apart. The values: short1 to short4 = 50, 100, 150, 200 ms; medium1 to medium4 = 250 to 400 ms; long1 to long4 = 450 to 600 ms. The durations on Mrs. Sri's page match those tokens:

Moment Distance or area Token Duration
Night button pressed shrinks 4% short2 100 ms
Night button colour doesn't move short3 150 ms
Toast and dialog entering about 60 px, a small area short4 200 ms
Room card entering and photo gallery one card, one screen-wide photo medium2 300 ms

What leaves is made shorter than what enters (the toast leaves in 150 ms, enters in 200 ms), because nobody is waiting for it any more.

⚠️ Curves put in the wrong place

TL;DR: Easing (the speed pattern of a movement) turns time into progress, how far the movement has gone. ease-in starts slow then fast (31.5% at half the time), ease-out starts fast then slow (68.5% at half the time), linear is even. Each of those keywords is a short name for a curve cubic-bezier(x1, y1, x2, y2), with time on the x axis and progress on the y axis. x(s) and y(s) are computed from the curve's four points, with a helper number s running from 0 to 1. For a time t, find the s where x(s) = t, then compute y(s). ease-out at s = 0.5 gives x = 0.3425 and y = 0.5, and a card rising 16 px in 300 ms has risen 11 px at 150 ms. x must be 0 to 1, y may go outside: cubic-bezier(0.68, -0.55, 0.27, 1.55) goes back 9.3%, then overshoots 9.3%. steps(n, jump-*) jumps step by step with no motion in between (jump-none shows both the start and the end), and linear() joins points with straight lines (Chrome 113, Firefox 112, Safari 17.2). An element that enters uses deceleration, a movement that slows down (ease-out, emphasized decelerate cubic-bezier(0.05, 0.7, 0.1, 1)). An element that leaves uses acceleration, a movement that speeds up (ease-in, emphasized accelerate cubic-bezier(0.3, 0, 0.8, 0.15)). Duration grows with distance: 100, 150, 200, 300 ms = the Material 3 (M3) duration tokens short2, short3, short4, medium2.

⚡ Time becomes progress

Guess first: the Terrace Room card rises 16 px in 300 ms with ease-out (fast at the start, slow at the end). At 150 ms, how many px has it risen? 8?

11 px, not 8. ease-out is fast at first, then slows down, so the last 5 px take the final 150 ms, slowly.

Meaning: Easing (the speed pattern of a movement) turns time into progress, how far the movement has gone. ease-in starts slow then fast, ease-out starts fast then slow, linear is even from start to end.

⚡ Past the target

Guess first: cubic-bezier() makes an easing from four numbers, x1, y1, x2, y2: x for time, y for progress. What kind of motion does cubic-bezier(0.68, -0.55, 0.27, 1.55) make?

It goes back first, then overshoots the target and comes back. A photo gallery slid 300 px goes back 28 px, then overshoots 28 px before it stops.

Meaning: x must be 0 to 1, but y may be anything. A negative y1 makes the motion go back first, a y2 above 1 makes it overshoot (go past the target). Use it only sometimes, not on a button pressed every day.

⚡ Jumping step by step

Write:

.dots { display: inline-block; animation: dots 1200ms steps(4, jump-none) infinite; }
@keyframes dots {
  from { clip-path: inset(0 100% 0 0); } /* every dot covered */
  to   { clip-path: inset(0 0 0 0); }    /* three dots visible */
}

Meaning: This code shows the "Booking..." dots one by one, 300 ms each. steps(4, jump-none) makes the progress jump: 0, 1/3, 2/3, 1, with no motion in between. With plain steps(4), the three dots are only reached as the next round starts, so they are never seen in an infinite animation.

⚡ Entering and leaving

Meaning: What enters uses ease-out: a quick response, then a gentle landing. What leaves uses ease-in (starts slow, then speeds up) and is shorter: the toast enters in 200 ms and leaves in 150 ms, because nobody is waiting for it any more.

What you will learn