← Math
Math

Curves and easing

Why doesn’t the JavaScript drawer move like the CSS one?

Map elapsed time to animation progress and handle an interrupted return.

Outcome

Clamp time progress, evaluate a cubic Bézier easing by solving for its parameter, and reverse an animation over a duration that matches the distance covered.

Terminology (5)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Progress
Elapsed time divided by duration, clamped to 0 to 1.
Easing function
Maps time progress to value progress; its slope is the relative speed.
Cubic Bézier
A curve from (0, 0) to (1, 1) shaped by two control points, written cubic-bezier(x1, y1, x2, y2) in CSS.
Parametric curve
A curve traced by a parameter t: here x(t) is time progress and y(t) is value progress.
Control point
A point that pulls the curve toward it without the curve passing through it.

Why this matters

Motion in an interface is a function from time to position. Designers specify it as a curve, browsers evaluate it for CSS, and anything animated outside CSS has to evaluate it the same way or the product moves inconsistently.

This lesson assumes fractions, polynomials and the idea of a slope.

Easing maps time to a value

Interpolation chooses a value between a start and an end. An easing changes the fraction of that distance travelled at each fraction of elapsed time. Its slope determines how quickly the value changes, which is why a curve can start gently or settle slowly.

A CSS cubic Bézier token describes a two-coordinate curve: horizontal position is time progress and vertical position is value progress. A parameter traces the curve. Evaluating the easing means finding the curve point for the requested time, then using its value coordinate. An interrupted animation also needs a policy for its new start and duration.

01Time progress and value progress

Time progress and value progress

Your design system has a motion token, cubic-bezier(0.4, 0, 0.2, 1), used by CSS transitions everywhere. A filter drawer animated from JavaScript was meant to use the same token, but it moves differently, sometimes snaps shut a frame after opening, and takes a full 300 ms to close after a quick tap.

Time progress p is the elapsed time over the duration: 150 ms into a 300 ms animation is 0.5. An easing function turns it into value progress, and the value is start + (end − start) × progress. The simplest smooth ease, smoothstep 3p² − 2p³, starts and ends with zero slope, so the drawer starts and stops gently.

Frame callbacks can arrive before the start or after the end. smoothstep is just a cubic outside 0 to 1, and at p = 1.5 it is back to 0, so unclamped progress snaps the drawer shut on a late frame.

02A cubic Bézier is a curve, not a function of time

A cubic Bézier is a curve, not a function of time

Predict the three lines.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// cubic-bezier(0.4, 0, 0.2, 1): where is the curve at parameter t = 0.2, 0.5 and 0.8?
const bez = (a: number, b: number, t: number) =>
  3 * (1 - t) * (1 - t) * t * a + 3 * (1 - t) * t * t * b + t * t * t;

for (const t of [0.2, 0.5, 0.8]) {
  console.log(
    `t=${t} time=${bez(0.4, 0.2, t).toFixed(3)} value=${bez(0, 1, t).toFixed(3)}`
  );
}

CSS cubic-bezier(x1, y1, x2, y2) defines a curve from (0, 0) to (1, 1) traced by a parameter t. With endpoints 0 and 1, each coordinate is 3(1 − t)²t a + 3(1 − t)t² b + t³, using its two control coordinates a,b. Both coordinates depend on t: x(t) is the time progress and y(t) the value progress. At t = 0.5 this curve is at time 0.35, so reading y(0.5) as the value at half time is wrong.

The Web Animations API takes the same easing strings as CSS, so el.animate(frames, { easing: "cubic-bezier(0.4, 0, 0.2, 1)" }) matches a CSS transition without solving anything yourself.

03Find the parameter for the time

Find the parameter for the time

With x1 and x2 between 0 and 1, x(t) only grows, so for any time progress there is one t. In exact arithmetic 60 halvings bound the parameter interval by 2⁻⁶⁰; binary64 rounding limits further refinement. The lab holds exact endpoints at 0 and 1. At time progress 0.3, 0.5 and 0.7 the standard token gives 0.3673, 0.7756 and 0.9382: most of the movement happens early, and the slope near the end is shallow, so the drawer settles.

What does cubic-bezier(0.25, 0.25, 0.75, 0.75) do?

04Reverse from where it is

Reverse from where it is

Tapping the toggle at 120 ms reverses the drawer at 196.35 px, which is 61% of the way. For a first reversal of this nonovershooting, two-state transition with unchanged 300ms duration and no delay, CSS shortens the reverse duration by the covered fraction: 196.35/320 ≈ 0.6136, giving 184.08ms instead of 300. The lab restarts the same easing from that current value back to the start, and holds the start immediately when the covered fraction is zero. Restarting a full 300 ms close after a quick tap feels sluggish, and a fresh animation from 320 would jump. Repeated CSS reversals track additional shortening state; arbitrary overshooting curves need a different policy. This lab uses positive durations, distinct endpoints and control values in[0,1].

05What the curve does not decide

What the curve does not decide

06Match the drawer to CSS

Match the drawer to CSS

Match the drawer’s JavaScript interpolation to the design token, then handle its first interrupted return without jumping or restarting a full-length close.

A design system’s motion token

Clamp progress, evaluate cubic-bezier, then reverse from where it is.

  1. easy · M-EASING-V4-1 Hold the endpoints
  2. medium · M-EASING-V4-2 Evaluate cubic-bezier
  3. hard · M-EASING-V4-3 Reverse mid-animation
In the frontend The drawer in Next.js and Svelte Optional

The Web Animations API accepts the CSS easing string; reversing playback retraces the original curve. Svelte’s fly transition accepts a function of progress, so the CSS curve must be solved first. These APIs own their interruption timing; neither snippet claims to implement the lab’s shortened-duration restart policy.

Estimate.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// One motion token for CSS and for JavaScript-driven animation. The Web Animations API reverses
// from the current position, so a drawer closed mid-way does not jump.
import { useCallback, useRef } from "react";

const STANDARD = "cubic-bezier(0.4, 0, 0.2, 1)";

export function useDrawer(ref: React.RefObject<HTMLElement | null>) {
  const animation = useRef<Animation | undefined>(undefined);
  const wanted = useRef<boolean | undefined>(undefined);

  return useCallback(
    (open: boolean) => {
      if (wanted.current === open) return;

      wanted.current = open;

      if (animation.current && animation.current.playState === "running") {
        animation.current.reverse();

        return;
      }

      animation.current?.cancel();
      animation.current = ref.current?.animate(
        [{ transform: "translateX(-320px)" }, { transform: "translateX(0)" }],
        {
          duration: 300,
          easing: STANDARD,
          fill: "forwards",
          direction: open ? "normal" : "reverse"
        }
      );
    },
    [ref]
  );
}

Check this idea later
An easing’s slope is steepest near the start. When does the element move fastest?
A 300ms transition has its first reversal after covering 25% of its distance, under this lab’s nonovershooting rule. How long should the reverse take?
Sources

Values above were run in Node 22, Go 1.27, Rust 1.99 and Apple clang 17. The drawer and the token are a worked model.

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.