liveBook · Manning

Animation

As explained in Math for Web Design

A web animation is the programmed or styled change of visual properties over time in a web interface.

A dialog that enters at a constant speed can feel abrupt, while one that slows as it reaches its destination feels more natural. Web animation changes visual properties over time; the useful calculation is often not just where an element should end up, but where it should be at each moment along the way. section 12.1.1 section 1.2.2

For a timed movement, divide elapsed time by duration to get progress from 0 to 1. A linear dialog slide places the element at start + distance × progress. Replacing progress with 1 − (1 − progress)³ makes it cover more distance early and slow near the end. Both versions update the dialog on successive requestAnimationFrame calls; the changed calculation produces the different feel. section 12.1.1

Give feedback quickly

A tooltip should respond promptly to hover or keyboard focus. A 0.15-second CSS transition using cubic-bezier(0.4, 0.0, 0.2, 1) changes its opacity and position. For an interactive effect, choose the duration and curve together: a short duration and fast initial response help the change feel immediate. section 11.3.3

Different effects call for different calculations:

  • Circular motion: advance an angle each frame, then calculate x = centerX + radius × cos(angle) and y = centerY + radius × sin(angle). Apply the coordinates with a CSS transform. A smaller angle increment slows the orbit; a negative one reverses it. For a simple orbit, CSS can instead animate a registered angle from 0deg to 360deg and use cos() and sin() to position the element. section 9.2.2
  • A bounce: add gravity to vertical velocity, add velocity to position, and reverse and reduce velocity when the object reaches the floor. This gives a falling object successive, smaller rebounds. section 12.3.1
  • Scroll feedback: divide window.scrollY by document height minus viewport height to obtain progress from 0 to 1. Use it to set a reading indicator’s width, percentage label, and red-to-green hue; check that the scrollable height is positive before dividing. section 13.3.1
  • Celebration: give canvas confetti pieces randomized starting velocities, then update their positions, rotation, gravity-driven fall, and drag each frame. A brief shower can mark completion of a long task. section 14.2.3
Listing 12.2 Animating a dialog onto the screen with an ease-out curve
function openDialog() {
    if (isAnimating) return;
    isAnimating = true;

    let dialog = document.getElementById('dialog');
    let start = -300;
    let end = (window.innerHeight / 2) - (dialog.offsetHeight / 2);
    let distance = end - start;
    let duration = 1000;
    let startTime = Date.now();

    function animate() {
        let elapsed = Date.now() - startTime;
        let progress = elapsed / duration;

        if (progress >= 1) {
            isAnimating = false;
            return;
        }
        const eased = 1 - Math.pow(1 - progress, 3);  #1
        let currentTop = start + eased * distance;
        dialog.style.top = currentTop + 'px';

        requestAnimationFrame(animate);
    }

    animate();
}

Compare the eased dialog calculation with the linear version: one changed progress value alters the motion without changing its start, destination, or duration. section 12.1.1

Start with the visual job: immediate feedback, an orbit, a physical-looking bounce, reading progress, or a moment of celebration. Then choose the changing input—time, angle, velocity, or scroll position—and connect its calculation to a CSS property, transform, or canvas drawing. section 9.2.2 section 13.3.1

How Math for Web Design covers it

How deep it goes
Works through frame-by-frame motion, easing, bouncing, and scroll calculations, alongside CSS transitions and a CSS orbit.
Examples
Demonstrates an entering dialog, hover tooltip, circular orbit, bouncing ball, reading progress indicator, and confetti shower.
What it assumes
Assumes familiarity with CSS and JavaScript while developing the needed progress, coordinate, and velocity calculations through examples.
What you can do after
Build it from scratch using CSS or JavaScript calculations to control timing, position, feedback, and scroll-driven visual changes.
How it connects
Connects animation to trigonometry, cubic Bézier curves, physics simulation, scroll position, and canvas drawing.

Also explained in

Compare how 6 books explain animation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math for Web Design. The section links point to the parts of the book it draws on.