日本語

How to Make a Bouncing Ball Motion with CSS linear() Easing

​​CSS​Animation,Chromium,Playwright

CSS linear() is an easing function that connects the points you list with straight lines. With enough points, you can make motions that a single cubic curve from cubic-bezier() cannot express, such as a ball that bounces on the floor several times before stopping. You can do this with CSS animations alone. This is useful for a falling ball or a springy icon when you want a bouncing motion without a JavaScript animation library.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I paused the animation, moved the playback position (currentTime) in 10 ms steps, and read the element's translate value to examine the motion. I did not test Firefox, Safari, real smartphones, or older browsers that do not support linear().

linear() connects the points you list with straight lines

The arguments of linear() are numbers from 0 to 1 that represent the progress, separated by commas. 0 is the start value of the animation and 1 is the end value. After each number, you can write the point in time at which that value is reached, as a percentage of the animation duration.

css
/* Reach 0.25 by 75% of the time, then reach 1 over the remaining 25% */
animation-timing-function: linear(0, 0.25 75%, 1);

Points without a time are spaced evenly between the surrounding points. In the test environment's Chromium, reading the getComputedStyle() value showed that the omitted times had been filled in.

text
linear(0, 0.25, 1)          -> linear(0 0%, 0.25 50%, 1 100%)
linear(0, 0.5 25% 75%, 1)   -> linear(0 0%, 0.5 25%, 0.5 75%, 1 100%)

If you write two times for one point, the value stays the same between them. The MDN page for linear() says that the typical use of linear() is to place many points to approximate an arbitrary curve. You can also use values outside the range 0 to 1. This article uses both to create a bouncing motion and an overshoot-and-return motion.

There is one caution about writing times. The English and Japanese versions of MDN list a sequence with out-of-order times, such as linear(0, 0.25 80%, 0.5 60%, 1), as an invalid example. On the other hand, the CSS Easing Functions Level 2 specification says that when a point's time is smaller than the time of the point before it, the time is set to the largest of the earlier points' times. The test environment's Chromium did not reject this form and accepted it as linear(0 0%, 0.25 80%, 0.5 80%, 1 100%). The declaration is not ignored, and the value jumps from 0.25 to 0.5 at 80%. When I moved 300px over 1 second, the position was 74.9px at 799 ms and 150px at 800 ms.

The simplest bounce: listing only the landings and peaks

Consider a ball that drops from above, bounces three times on the floor, and stops. Output 0 is the starting height and 1 is the floor. Each bounce rises to one quarter of the previous height (0.25 → 0.0625 → about 0.016). The first bounce takes the same time as the fall, and each bounce after that takes half as long. This ratio assumes that the speed of the rebound halves every time.

If you list only the times when the ball lands and the times when it reaches the top of each bounce, you get these 8 points.

css
.ball {
  animation: drop 1s both;
  animation-timing-function: linear(
    0, 1 36.4%, 0.75 54.5%, 1 72.7%, 0.938 81.8%, 1 90.9%, 0.984 95.5%, 1
  );
}

@keyframes drop {
  from { translate: 0 0; }
  to   { translate: 0 300px; }
}

Each time the value returns to 1, the ball touches the floor, and when it is less than 1, the ball bounces up. When I read the position every 10 ms in the test environment, the direction of motion changed 6 times: 3 landings and 3 peaks.

However, because the points are joined by straight lines, the ball moves at a constant speed in each segment. In the first fall segment, it moved 82.4px every 100 ms. It does not look like it accelerates under gravity.

Adding points to the fall to make it look accelerated

If you divide the fall and the bounces into fine segments using points along parabolas, the motion gets closer to accelerating as it falls and slowing down as it rises. I did not calculate the point values by hand. I generated them with the following Node.js script. The fall is split into 8 segments, and the bounce segments get fewer points as the bounces get smaller.

js
// Turn a ball falling and bouncing into a list of linear() points
// Output 0 = starting height, 1 = floor. e = restitution coefficient (ratio of rebound speed)
// Usage: node gen.js <e> <number of bounces> <points in the fall segment>
const e = Number(process.argv[2] ?? 0.5);
const bounces = Number(process.argv[3] ?? 3);
const n = Number(process.argv[4] ?? 8);
// Duration of each segment (fall, bounce 1, bounce 2, ...) when the fall is 1
const segs = [1];
for (let i = 1; i <= bounces; i++) segs.push(2 * e ** i);
const total = segs.reduce((a, b) => a + b, 0);
const pts = [[0, 0]];
let start = 0;
segs.forEach((len, i) => {
  const k = i === 0 ? n : Math.max(2, Math.round(n * e ** i)); // smaller bounces get fewer points
  for (let j = 1; j <= k; j++) {
    const s = j / k;                     // fraction within the segment
    let h;                               // height above the floor (starting height = 1)
    if (i === 0) h = 1 - s * s;          // fall along a parabola
    else { const u = 2 * s - 1; h = e ** (2 * i) * (1 - u * u); } // rise and fall along a parabola
    pts.push([(start + len * s) / total, 1 - h]);
  }
  start += len;
});
const r = (x, d) => Number(x.toFixed(d));
const out = pts.map(([t, y], i) => i === 0 ? '0' : i === pts.length - 1 ? '1' : `${r(y, 3)} ${r(t * 100, 1)}%`);
console.log(`linear(${out.join(', ')})`);
console.error(`points: ${pts.length}`);

Running node gen.js 0.5 3 8 produced the following 17 points. (node gen.js 0.5 3 1 gives the 8 points from the previous section.)

css
.ball {
  animation-timing-function: linear(
    0, 0.016 4.5%, 0.063 9.1%, 0.141 13.6%, 0.25 18.2%, 0.391 22.7%,
    0.563 27.3%, 0.766 31.8%, 1 36.4%, 0.813 45.5%, 0.75 54.5%,
    0.813 63.6%, 1 72.7%, 0.938 81.8%, 1 90.9%, 0.984 95.5%, 1
  );
}

For the two versions, I read the position every 10 ms in the test environment while dropping the ball 300px over 1 second, and graphed the results.

Line graph. The horizontal axis is elapsed animation time and the vertical axis is position. The light blue line with 17 points starts slowly, speeds up as it falls, reaches the floor at about 36%, and then bounces 3 times. The gray line with 8 points is a straight diagonal until it reaches the floor.
Ball position when 17 and 8 points are used in linear() (values measured every 10 ms in Chromium)

The difference is clear when you compare the positions during the fall.

Elapsed 8 points 17 points
100 ms 82.4px 23.6px
200 ms 164.8px 91.9px
300 ms 247.3px 205.4px

With 17 points, the distance moved every 100 ms increases: 23.6px, 68.3px, and 113.5px. The time when the ball touches the floor and the time when it reaches the top of the bounce are the same for both versions.

I calculated the positions in JavaScript following the output calculation steps in the specification. The largest difference from Chromium's positions was 0.0005px for both versions.

The specification's linear() examples also include a "bounce" version that lands on the floor at the same 36.4%, 72.7%, and 90.9% points, provided as a value for a custom property --bounce.

How it looks next to ease-in

I recorded a ball dropped with ease-in next to a ball dropped with the 17-point linear(). Both animations are 1.2 seconds long.

Animation. The "Drop" button at the top is clicked twice with the cursor. Each time, the gray ball on the left with ease-in falls straight to the floor and stops, while the light blue ball on the right with linear() bounces 3 times on the floor before stopping. When the ball lands, the "Landed n times" label at the upper right briefly turns pink.
Motion of pressing a button to drop the ball (left: ease-in, right: linear() with 17 points)

For the recording, I reproduced the button press (the cursor and the button's appearance) with a script. The animation was paused, and after each press I moved the playback position back to the start and advanced it 40 ms at a time, capturing one frame per step. I counted the number of floor landings by checking between frames in 1 ms steps. ease-in landed once, and linear() landed four times (once for the fall and three times for the bounces).

Overshooting and returning with values above 1

If you want a notification badge or icon to go slightly past its target and then settle back, use values greater than 1.

css
.badge {
  animation-timing-function: linear(0, 1.25 40%, 0.9 65%, 1.04 85%, 1);
}

In an animation that moves 300px, the element reached 375px (1.25 times the end value) at 40% of the time, went back past 300px, and then stopped at 300px. The direction of motion changed 3 times.

cubic-bezier() can also overshoot if you set the second and fourth values (the y values) outside the range 0 to 1. The specification's section on cubic-bezier() restricts only the first and third values (the x values) to the range 0 to 1. In the test environment, cubic-bezier(1.2, 0, 0.6, 1) was invalid, while cubic-bezier(0.3, 1.6, 0.6, 1) worked. However, when I moved the element the same way with cubic-bezier(0.3, 1.6, 0.6, 1), there was only one overshoot, at 333.1px, and the direction changed only once. If you want the motion to go back and forth several times, linear() is better because you can add as many turning points as you need.

Using it with transition as well

linear() can be used not only in animations but also as the easing for a transition.

css
.ball {
  translate: 0 0;
  transition: translate 1s linear(
    0, 1 36.4%, 0.75 54.5%, 1 72.7%, 0.938 81.8%, 1 90.9%, 0.984 95.5%, 1
  );
}

.ball.on {
  translate: 0 300px;
}

In the test environment, adding .on produced 300px (the floor) at 364 ms, 225px (the first peak) at 545 ms, and 300px again at 727 ms. Writing the 17-point linear() gave the same positions at the same times.

Browser support and reduced-motion settings

According to MDN's compatibility data (@mdn/browser-compat-data 8.1.8), linear() is supported in Chrome 113, Edge 113, Firefox 112, Safari 17.2, and Safari on iOS 17.2.

For older browsers, use ease-in for the fall, and rewrite it with @supports only when linear() is available. A long list of points can be stored in a custom property and reused. For people who have chosen reduced motion in their device settings, you can stop the bouncing with prefers-reduced-motion.

css
:root {
  --bounce: linear(
    0, 1 36.4%, 0.75 54.5%, 1 72.7%, 0.938 81.8%, 1 90.9%, 0.984 95.5%, 1
  );
}

.ball {
  animation: drop 1s ease-in both;
}

@supports (animation-timing-function: linear(0, 1)) {
  .ball {
    animation-timing-function: var(--bounce);
  }
}

@media (prefers-reduced-motion: reduce) {
  .ball {
    animation: none;
    translate: 0 300px;
  }
}

In the test environment's Chromium, the declaration inside @supports was applied, and the computed value was the linear() from --bounce. When I set Playwright to reduced motion (reducedMotion: 'reduce'), no animations ran, and the ball was at the floor position (300px) from the start.

​Posted by 86