Timing is how long an action takes; spacing is how the motion is spread across that time. Together they tell the viewer how heavy something is: a bowling ball drops hard and barely bounces, a rubber ball rebounds fast, and a balloon drifts. In the demo, Brick plays the bowling ball, Dot the rubber ball and Zip the balloon.
Switch on "on twos" (12 drawings/s) or slow motion.
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>09 · Timing — 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: 340px; margin: var(--space-sm) 0; } .controls { display: flex; gap: 1.5rem; flex-wrap: wrap; }</style></head><body><h1>Timing</h1><p>The number of frames an action takes tells us how heavy something is. Same drop, three weights: Brick (heavy as a bowling ball), Dot (a rubber ball) and Zip (light as a balloon).</p><div class="controls"> <label><input id="twos" type="checkbox"> On twos (12 fps)</label> <label>Time scale <input id="scale" type="range" min="0.1" max="2" step="0.05" value="1"> <output id="out">1×</output></label></div><canvas id="c" role="img" aria-label="Brick, Dot and Zip drop side by side. Brick falls hard and barely bounces, Dot rebounds quickly, and Zip drifts down slowly like a balloon."></canvas><p class="demo-try">Switch on "on twos" (12 drawings/s) or slow motion.</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('09-timing', (document, Toon, DemoTheme) => { const T = DemoTheme; // design tokens, live (see demo-theme.js) const { createStage, ticker, quantize, impact, squash, withSquash, groundShadow } = Toon; const stage = createStage(document.getElementById('c')); const { ctx } = stage; const twos = document.getElementById('twos'), scale = document.getElementById('scale'), out = document.getElementById('out'); // [TIMING] global clock. setSpeed = ticker.timeScale, except it leaves a reduced-motion pause in place (demo-theme.js). scale.oninput = () => { T.setSpeed(+scale.value); out.textContent = scale.value + '×'; };
// [TIMING] weight lives in gravity, restitution, drag and how much it deforms const things = [ { name: 'Brick · heavy', who: 'brick', color: 'neutral', r: 32, g: 2600, e: 0.2, drag: 0, squash: 0.05 }, { name: 'Dot · bouncy', who: 'dot', color: 'primary', r: 26, g: 1800, e: 0.78, drag: 0, squash: 0.4 }, { name: 'Zip · light', who: 'zip', color: 'tertiary', r: 30, g: 300, e: 0.45, drag: 1.6, squash: 0.25 }, ].map(o => ({ ...o, y: 0, vy: 0, hold: 0, out: 0 })); const HOLD = 0.07; let since = 99, lastDrawn = -1;
stage.loop((dt, t) => { const ground = stage.height - 50; since += dt; if (since > 4.5) { since = 0; things.forEach(o => Object.assign(o, { y: 40, vy: 0, hold: 0 })); } for (const o of things) { const floor = ground - o.r; if (o.hold > 0) { o.hold -= dt; if (o.hold <= 0) o.vy = -o.out; continue; } o.vy += o.g * dt; o.vy *= 1 - o.drag * dt; o.y += o.vy * dt; if (o.y >= floor) { o.y = floor; o.out = o.vy * o.e; o.vy = 0; o.hold = o.out > 40 ? HOLD : 0; o.hit = o.out; } } if (twos.checked) { const q = quantize(t, 12); if (q === lastDrawn) return; lastDrawn = q; } // hold drawings
stage.clear(); ctx.fillStyle = T.ground; ctx.fillRect(0, ground, stage.width, 50); ctx.fillStyle = T.groundLine; ctx.fillRect(0, ground, stage.width, 2); things.forEach((o, i) => { const x = stage.width * (i + 1) / 4; const s = o.hold > 0 ? impact(Math.sin((1 - o.hold / HOLD) * Math.PI) * o.squash * Math.min(1, o.hit / 500)) : squash(0, o.vy, { amount: o.squash * 0.0015 }); groundShadow(ctx, x, ground, o.r, ground - o.r - o.y, { maxHeight: 280 }); const mood = o.who === 'brick' ? 'neutral' : o.hold > 0 || o.who === 'zip' ? 'happy' : 'neutral'; // Brick stays unflappable withSquash(ctx, x, o.y, s, c => Cast.draw(c, o.who, { x: 0, y: 0, h: o.r * 2, fill: T[o.color], ink: T.ink, trail: false, mood, look: { x: 0, y: Math.max(-1, Math.min(1, o.vy / 800)) } }), o.hold > 0 ? { x: 0, y: o.r } : undefined); ctx.fillStyle = T.muted; ctx.font = '13px system-ui'; ctx.textAlign = 'center'; ctx.fillText(stage.width < 480 ? o.name.split(' · ')[0] : o.name, x, ground + 30); // phones: names only, so labels don't collide }); });});</script></body></html>Timing in CSS
Section titled “Timing in CSS”Set --toon-tempo on a container to retime every preset inside it. Higher is slower. This snippet runs as-is:
<link rel="stylesheet" href="toon.css">
<!-- --toon-tempo on a container retimes everything in it. --><div id="scene" style="--toon-tempo:1; display:grid; justify-items:center; padding-top:110px"> <div class="toon-bounce" style="--toon-height:100px; width:48px; height:48px; border-radius:50%; background:var(--toon-color)"></div> <div class="toon-shadow toon-bounce-shadow" style="width:56px; height:12px"></div></div><label>Tempo <input id="tempo" type="range" min="0.5" max="2" step="0.1" value="1" aria-valuetext="1, 800 ms per bounce"> <output id="out">800 ms</output></label>
<script>// Higher tempo is slower: each bounce takes 800 ms × tempo.const tempo = document.getElementById('tempo');tempo.oninput = () => { const ms = Math.round(800 * tempo.value); document.getElementById('scene').style .setProperty('--toon-tempo', tempo.value); document.getElementById('out').textContent = `${ms} ms`; tempo.setAttribute('aria-valuetext', `${tempo.value}, ${ms} ms per bounce`);};</script>Drag the slider below: the ball and its shadow change speed together, because both read the tempo from the container:
Every preset lasts --toon-duration (800 ms) times --toon-tempo. The .toon-heavy and .toon-light modifiers set the tempo to 1.6 and 0.6, and change the squash to match.
Timing in Canvas
Section titled “Timing in Canvas”Weight comes from gravity, bounciness and drag, so this snippet gives each object its own values. quantize() holds time on 12 drawings per second. Tick On twos to animate on twos (holding each drawing for two frames), the hand-drawn rhythm of 24 fps film.
<script src="toon.js"></script><canvas style="width:100%; height:180px" role="img" aria-label="A bowling ball, a rubber ball and a balloon drop side by side. The bowling ball lands hard and barely bounces, the rubber ball rebounds high, and the balloon drifts down slowly"></canvas><label><input id="twos" type="checkbox"> On twos (12 drawings per second)</label>
<script>const { createStage, quantize, impact, squash, withSquash } = Toon;const stage = createStage(document.querySelector('canvas'));const twos = document.getElementById('twos');// Weight is gravity (px/s²), bounce (0 to 1) and drag (per s).const things = [ { name: 'bowling', r: 20, g: 2600, bounce: 0.2, drag: 0, squash: 0.06 }, { name: 'rubber', r: 16, g: 1800, bounce: 0.75, drag: 0, squash: 0.4 }, { name: 'balloon', r: 20, g: 300, bounce: 0.4, drag: 1.6, squash: 0.2 },];const HOLD = 0.07; // seconds squashed on the floorlet since = 0, drawn = -1;const drop = () => things.forEach((o) => Object.assign(o, { y: o.r + 10, vy: 0, hold: 0 }));drop();
function update(dt) { if ((since += dt) > 4) { since = 0; drop(); } // every 4 s for (const o of things) { const floor = stage.height - 30 - o.r; if (o.hold > 0) { // on the floor: hold, then rebound if ((o.hold -= dt) <= 0) o.vy = -o.out; continue; } o.vy += o.g * dt; o.vy *= 1 - o.drag * dt; o.y += o.vy * dt; if (o.y >= floor) { o.y = floor; o.out = o.vy * o.bounce; o.vy = 0; o.hold = o.out > 40 ? HOLD : 0; } }}6 collapsed lines
// Reduced motion: run 0.45 s ahead, then freeze the clock.// The bowling ball has landed; the balloon is still high.if (matchMedia('(prefers-reduced-motion: reduce)').matches) { for (let i = 0; i < 27; i++) update(1 / 60); Toon.ticker.timeScale = 0;}
stage.loop((dt, time) => { update(dt); if (twos.checked) { // hold each drawing for 2 frames const q = quantize(time, 12); if (q === drawn) return; drawn = q; } const ctx = stage.ctx, ground = stage.height - 30;3 collapsed lines
const fill = getComputedStyle(document.documentElement) .getPropertyValue('--toon-color').trim() || '#ff6b4a'; const ink = getComputedStyle(document.body).color; stage.clear(); ctx.fillStyle = ink; ctx.fillRect(0, ground, stage.width, 2); ctx.font = '12px system-ui'; ctx.textAlign = 'center'; things.forEach((o, i) => { const x = stage.width * (i + 1) / 4; const k = Math.sin((1 - o.hold / HOLD) * Math.PI); const s = o.hold > 0 ? impact(k * o.squash) : squash(0, o.vy, { amount: o.squash * 0.0015 }); const pivot = o.hold > 0 ? { x: 0, y: o.r } : undefined; withSquash(ctx, x, o.y, s, (c) => { c.beginPath(); c.arc(0, 0, o.r, 0, Math.PI * 2); c.globalAlpha = o.name === 'balloon' ? 0.45 : 1; c.fillStyle = o.name === 'bowling' ? ink : fill; c.fill(); c.globalAlpha = 1; c.lineWidth = 2.5; c.strokeStyle = ink; c.stroke(); }, pivot); ctx.fillStyle = ink; ctx.fillText(o.name, x, ground + 20); });});</script>Timing: tuning tips
Section titled “Timing: tuning tips”- Make heavy things slow to start and quick to stop. A bowling ball takes a while to get going, lands abruptly, deforms little and barely rebounds.
- Let light things react at once. A balloon or a leaf floats, flutters and overshoots, so give it low gravity and plenty of drag.
- Keep interface timing short. Use 150–250 ms for small feedback and 300–500 ms for larger moves across the screen. Anything longer starts to feel slow.
- Check timing in slow motion. Set
Toon.ticker.timeScaleto 0.25 to slow every animation on the clock, like the demo’s Time scale slider. In CSS, a--toon-tempoof 4 on:rootdoes the same.
The demo also shows squash and stretch: each object deforms as much as its material allows.
For every option, see the Canvas timing functions and the CSS custom properties.