Before jumping, you crouch. Before throwing, you pull your arm back. Anticipation is a small move in the opposite direction of the main action. It prepares the viewer’s eye, so the action reads clearly even when it’s fast.
Press Jump or click the canvas, then turn anticipation off and compare.
Show the demo's source
The complete page: it loads toon.css and toon.js, plus demo-theme.js (theme colors), demo-runtime.js (gives the demo its own clock) and, for the characters, cast.js.
<!doctype html><html lang="en" data-style="saturday"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>02 · Anticipation — toon</title><link rel="stylesheet" href="../src/toon.css"><link rel="stylesheet" href="demo.css"><style> body { font-size: 15px; line-height: 1.5; max-width: var(--demo-max-width); margin: 2rem auto; padding: 0 1rem; } canvas { display: block; width: 100%; height: 320px; margin: var(--space-sm) 0; cursor: pointer; }</style></head><body><h1>Anticipation</h1><p>Before a big action, a small opposite move (the crouch) tells the viewer what is about to happen. </p><label><input id="antic" type="checkbox" checked> Anticipation (crouch before the jump)</label><canvas id="c" style="cursor:pointer" role="img" aria-label="Dot crouches down, then jumps and lands. With anticipation switched off, Dot jumps without crouching first."></canvas><p class="demo-try"><button type="button" class="demo-act" id="act">Jump</button> Press <i>Jump</i> or click the canvas, then turn anticipation off and compare.</p>
<script src="../src/toon.js"></script><script src="demo-theme.js"></script><script src="cast-shapes.js"></script><script src="cast.js"></script><script src="demo-runtime.js"></script><script>ToonDemos.register('02-anticipation', (document, Toon, DemoTheme) => { const T = DemoTheme; // design tokens, live (see demo-theme.js) const { createStage, ticker, tween, sequence, ease, withSquash, groundShadow } = Toon; const stage = createStage(document.getElementById('c')); const { ctx } = stage; // Reduced motion opens on the crouch, the pose that shows the principle (nothing moves until Play and Jump). const still = matchMedia('(prefers-reduced-motion: reduce)').matches; const hero = { y: 0, sy: still ? 0.62 : 1 }; // y = height above ground; sx = 1/sy keeps volume const set = v => Object.assign(hero, v); let mood = still ? 'focused' : 'neutral'; const to = (pose, duration, e, m) => () => { if (m) mood = m; return tween({ from: { ...hero }, to: pose, duration, ease: e, onUpdate: set }); };
let busy = false; async function jump() { if (busy) return; busy = true; await sequence( document.getElementById('antic').checked ? to({ y: 0, sy: 0.62 }, 280, ease.outQuad, 'focused') // [ANTICIPATION] crouch: wind up opposite to the jump : null, to({ y: 30, sy: 1.3 }, 90, ease.linear, 'happy'), // take-off: stretch to({ y: 170, sy: 1 }, 320, ease.outQuad), // rise, slowing into the apex to({ y: 0, sy: 1.15 }, 300, ease.inQuad), // fall, speeding up to({ y: 0, sy: 0.7 }, 70, ease.outQuad, 'surprised'), // land: squash to({ y: 0, sy: 1 }, 420, ease.outElastic), // recover ); mood = 'neutral'; busy = false; } stage.canvas.addEventListener('click', jump);
const character = (c, R) => Cast.draw(c, 'dot', { x: 0, y: 0, h: R * 2, fill: T.primary, ink: T.ink, mood });
stage.loop(() => { const R = 40, x = stage.width / 2, ground = stage.height - 40; stage.clear(); ctx.fillStyle = T.ground; ctx.fillRect(0, ground, stage.width, 40); ctx.fillStyle = T.groundLine; ctx.fillRect(0, ground, stage.width, 2); groundShadow(ctx, x, ground, R, hero.y, { maxHeight: 250 }); withSquash(ctx, x, ground - R - hero.y, { sx: 1 / hero.sy, sy: hero.sy }, c => character(c, R), { x: 0, y: R }); });
document.getElementById('act').addEventListener('click', jump);});</script></body></html>Anticipation in CSS
Section titled “Anticipation in CSS”To make a control wind up before it acts, add one of the three anticipation presets. This snippet runs as-is:
<link rel="stylesheet" href="toon.css">
<button class="toon-press" style="padding:.6em 1.2em"> Press and hold</button><button class="toon-anticipate" style="padding:.6em 1.2em"> Hover, focus or tap</button><!-- The ball travels 160px: give it room. --><div style="width:100%; max-width:220px; height:40px"> <div id="ball" style="--toon-distance:160px; width:40px; height:40px; border-radius:50%; background:var(--toon-color)"></div></div><button id="launch" style="padding:.6em 1.2em">Launch</button>
<script>// Replay the one-shot launch: remove the class, then add it back.const ball = document.getElementById('ball');document.getElementById('launch').onclick = () => { ball.classList.remove('toon-launch'); void ball.offsetWidth; // let the browser see the change ball.classList.add('toon-launch');};</script>Below, hold the first button, hover, tap or tab to the second, and press Launch to replay the ball:
Press and hold the first button, hover, tap or tab to the second, and press Launch.
.toon-press dips while held and springs past its size on release. .toon-anticipate crouches before it rises, and .toon-launch pulls back before it shoots forward. For a transition of your own, the --toon-ease-anticipate easing dips backwards first.
Anticipation in Canvas
Section titled “Anticipation in Canvas”Write the action as beats with sequence, and make the first beat the wind-up. Each beat is a tween of the ball’s height and vertical scale. For a single tween, ease.anticipate(s) puts the dip inside the easing curve itself. s sets how far it dips: 1.7 by default, 0 for none.
<script src="toon.js"></script><canvas style="width:100%; height:180px" role="img" aria-label="A ball crouches low, then springs up into a jump, lands with a squash and settles, over and over"></canvas>
<script>const { createStage, tween, wait, sequence, ease, withSquash, groundShadow } = Toon;const stage = createStage(document.querySelector('canvas'));const r = 24; // px// y: height above the floor (px). sy: vertical scale; the// width scale is 1 / sy, so the ball keeps its volume.// Start crouched: the pose that shows the principle.const ball = { y: 0, sy: 0.62 };const to = (pose, ms, e) => () => tween({ from: { ...ball }, to: pose, duration: ms, ease: e, onUpdate: (v) => Object.assign(ball, v),});4 collapsed lines
// Reduced motion: freeze the clock, so the ball holds its crouch.if (matchMedia('(prefers-reduced-motion: reduce)').matches) { Toon.ticker.timeScale = 0;}
async function jump() { await sequence( to({ y: 0, sy: 0.62 }, 280, ease.outQuad), // crouch to({ y: 20, sy: 1.3 }, 90, ease.linear), // take off to({ y: 90, sy: 1 }, 300, ease.outQuad), // rise to({ y: 0, sy: 1.15 }, 280, ease.inQuad), // fall to({ y: 0, sy: 0.75 }, 70, ease.outQuad), // land to({ y: 0, sy: 1 }, 400, ease.outElastic), // settle () => wait(500), ); jump();}jump();
stage.loop(() => { const ctx = stage.ctx, x = stage.width / 2; const floor = stage.height - 24;4 collapsed lines
// Colors from the page: --toon-color fill, text color for ink. const fill = getComputedStyle(document.documentElement) .getPropertyValue('--toon-color').trim() || '#ff6b4a'; const ink = getComputedStyle(document.body).color; stage.clear(); ctx.fillStyle = ink; ctx.fillRect(0, floor, stage.width, 2); groundShadow(ctx, x, floor, r, ball.y); const s = { sx: 1 / ball.sy, sy: ball.sy }; // Scale from the base, so the crouch stays on the floor. withSquash(ctx, x, floor - r - ball.y, s, (c) => { c.beginPath(); c.arc(0, 0, r, 0, Math.PI * 2); c.fillStyle = fill; c.fill(); c.lineWidth = 2.5; c.strokeStyle = ink; c.stroke(); }, { x: 0, y: r });});</script>Anticipation: tuning tips
Section titled “Anticipation: tuning tips”- Size the wind-up to the action. A small action needs only a small wind-up. For a big leap, the crouch is the longest beat before it: 280 ms against a 90 ms take-off in the Canvas snippet.
- Give fast actions more anticipation. If the main move lasts only a few frames, the wind-up is what makes it readable.
- Move against the action. A wind-up reads only when it goes the other way: down before up, back before forward.
- Keep it subtle in interfaces. The presets are sized to read in a demo:
.toon-pressdips about 9%. On a business screen, add.toon-subtleto halve every wind-up, to about 4%.
The Dot demo also shows squash and stretch (Dot stretches on take-off and squashes on landing) and slow in and slow out (the rise slows into the top of the jump).
For every option, see the Canvas timing and sequencing functions and the CSS anticipation presets.