tennis ball, .22
Squash & stretch
Make things feel soft or heavy: they flatten on impact and stretch when fast.
Edit this pageAn object flattens when it hits something and stretches when it moves fast. The trick that keeps it believable is constant volume: as it gets wider it gets shorter. Every squash and stretch in toon keeps its width scale times its height scale at 1.
Drag Exaggeration to 0 for a rigid ball, or toward 2 for rubber.
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>01 · Squash & stretch — 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; } output { min-width: 3.5ch; font-variant-numeric: tabular-nums; }</style></head><body><h1>Squash & stretch</h1><p>Dot flattens on contact and stretches along its velocity, keeping its volume: as it gets wider it gets shorter. Also on show: <b>timing</b> (gravity sets the beat), <b>slow in and slow out</b> (the trail dots bunch up at the top of each bounce) and <b>arcs</b> (the trail is a curve). The CSS version is in <a href="01-squash-and-stretch-css.html">Squash & stretch in CSS</a>.</p><label>Exaggeration <input id="amt" type="range" min="0" max="2" step="0.05" value="1" aria-valuetext="1 times"> <output id="out" for="amt">×1</output></label><canvas id="c" role="img" aria-label="Dot, a round character, bounces along the floor: stretched on the way down, flattened on landing, leaving a trail of dots that bunch together at the top of each bounce. With reduced motion on, the canvas shows three still poses instead: stretched, squashed and round."></canvas><p class="demo-try">Drag Exaggeration to 0 for a rigid ball, or toward 2 for rubber.</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('01-squash-and-stretch', (document, Toon, DemoTheme) => { const T = DemoTheme; // design tokens, live (see demo-theme.js) const { createStage, squash, impact, withSquash, groundShadow } = Toon; const stage = createStage(document.getElementById('c')); const { ctx } = stage; const amt = document.getElementById('amt'), out = document.getElementById('out'); amt.addEventListener('input', () => { // [EXAGGERATION] multiplies every squash and stretch out.textContent = `×${+amt.value}`; amt.setAttribute('aria-valuetext', `${+amt.value} times`); }); const R = 26, G = 2000, BOUNCE = 950, CONTACT = 0.09; // [TIMING] gravity + launch speed = rhythm; 90 ms on the floor let x = 0, y = 60, vx = 240, vy = 0, contact = 0, played = false; const trail = []; const dotWith = (mood, look) => (c) => Cast.draw(c, 'dot', { x: 0, y: 0, h: R * 2, fill: T.primary, ink: T.ink, mood, look });
// Reduced motion, before Play: the three key shapes side by side, so the principle shows without motion. function poses(ground, k) { const W = stage.width, floor = ground - R; [['stretched', W * 0.2, floor - 90, squash(0, 950, { amount: 0.0005, exaggeration: k }), undefined, 'neutral'], ['squashed', W * 0.5, floor, impact(0.4, k), { x: 0, y: R }, 'happy'], ['round', W * 0.8, floor - 170, { sx: 1, sy: 1 }, undefined, 'neutral']].forEach(([label, px, py, s, pivot, mood]) => { groundShadow(ctx, px, ground, R, ground - R - py, { maxHeight: 260 }); withSquash(ctx, px, py, s, dotWith(mood, { x: 0.7, y: 0 }), pivot); ctx.fillStyle = T.muted; ctx.font = '13px system-ui'; ctx.textAlign = 'center'; ctx.fillText(label, px, ground + 20); }); }
stage.loop(dt => { const W = stage.width, ground = stage.height - 30, k = +amt.value; if (dt > 0) played = true; stage.clear(); ctx.fillStyle = T.ground; ctx.fillRect(0, ground, W, stage.height - ground); ctx.fillStyle = T.groundLine; ctx.fillRect(0, ground, W, 2); if (!played) return poses(ground, k);
if (contact > 0) { // hold a few frames on the ground while squashed contact -= dt; y = ground - R; if (contact <= 0) vy = -BOUNCE; } else { vy += G * dt; y += vy * dt; if (y >= ground - R) { y = ground - R; contact = CONTACT; } } x += vx * dt; if (x > W + R) { x = -R; trail.length = 0; } if (dt > 0) { trail.push({ x, y }); if (trail.length > 100) trail.shift(); } // paused: keep the trail as it is
trail.forEach((p, i) => { // [ARCS] + [SLOW-IN-OUT] spacing made visible ctx.fillStyle = T.alpha(T.primary, (i / trail.length) * 0.6); ctx.beginPath(); ctx.arc(p.x, p.y, 2.5, 0, 7); ctx.fill(); }); groundShadow(ctx, x, ground, R, ground - R - y, { maxHeight: 260 }); const mood = contact > 0 ? 'happy' : 'neutral'; // a grin on every landing const look = { x: Math.sign(vx) * 0.7, y: Math.max(-1, Math.min(1, vy / 900)) }; // eyes lead the motion const dot = dotWith(mood, look);
// [SQUASH] On the floor: flatten, eased in and out over the // contact time. In the air: stretch along the velocity. const s = contact > 0 ? impact(Math.sin((1 - contact / CONTACT) * Math.PI) * 0.4, k) : squash(vx, vy, { amount: 0.0005, exaggeration: k }); // Pin the bottom of the shape to the floor while it squashes. const pivot = contact > 0 ? { x: 0, y: R } : undefined; withSquash(ctx, x, y, s, dot, pivot); });});</script></body></html>Squash & stretch in CSS
Section titled “Squash & stretch in CSS”Link toon.css, then add .toon-bounce to a sized element. Add .toon-bounce-shadow to its shadow. This snippet runs as-is:
<link rel="stylesheet" href="toon.css">
<!-- The ball bounces 140px high: leave room above it. --><div style="display:grid; justify-items:center; padding-top:150px"> <div class="toon-bounce" style="width:56px; height:56px; border-radius:50%; background:var(--toon-color)"></div> <div class="toon-shadow toon-bounce-shadow" style="width:64px; height:14px"></div></div>Below, the snippet’s ball is the one labelled default. The other two add .toon-heavy and .toon-light to change the material:
.toon-heavybowling ball, .06
tennis ball, .22
.toon-lightrubber, .38
.toon-bounce moves the ball the way gravity would: slow at the top, fast at the floor. It squashes at contact, stretches at top speed and goes round near the top. It scales from the bottom edge, so the squash stays on the ground. --toon-height sets how high it bounces (140px by default).
Squash & stretch in Canvas
Section titled “Squash & stretch in Canvas”This ball runs as-is. impact() flattens it while it’s on the floor. squash() stretches it along its velocity, so a ball thrown sideways stretches sideways. withSquash() draws it, with a pivot that keeps its base on the floor.
<script src="toon.js"></script><canvas style="width:100%; height:180px" role="img" aria-label="A ball bounces in place: it stretches as it rises and falls, and flattens on the floor"></canvas>
<script>const { createStage, squash, impact, withSquash, groundShadow } = Toon;const stage = createStage(document.querySelector('canvas'));const r = 24, gravity = 2000, launch = 600; // px, px/s², px/sconst HOLD = 0.09; // seconds squashed on the floor// y: height above the floor. Start mid-squash, on the floor.let y = 0, vy = 0, held = HOLD / 2;4 collapsed lines
// Reduced motion: freeze the clock, so the ball holds its squash.if (matchMedia('(prefers-reduced-motion: reduce)').matches) { Toon.ticker.timeScale = 0;}
stage.loop((dt) => { if (held > 0) { // on the floor: hold, then launch held -= dt; if (held <= 0) vy = launch; } else { // in the air: gravity vy -= gravity * dt; y += vy * dt; if (y <= 0) { y = 0; vy = 0; held = HOLD; } } // Squash on the floor (eased in and out), stretch in the air. const s = held > 0 ? impact(Math.sin((1 - held / HOLD) * Math.PI) * 0.35) : squash(0, vy, { amount: 0.0005 }); // While squashing, pin the ball's base to the floor. const pivot = held > 0 ? { x: 0, y: r } : undefined;
const ctx = stage.ctx, x = stage.width / 2; const floor = stage.height - 24;4 collapsed lines
// Colors from the page: --toon-color for the ball, text 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, y); withSquash(ctx, x, floor - r - 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(); }, pivot);});</script>Squash & stretch: tuning tips
Section titled “Squash & stretch: tuning tips”- Hold the contact for a few frames. Keeping the ball on the ground for about 70–90 ms while squashed reads much better than squashing as it leaves.
- Match squash to the material.
--toon-squashis.22by default, about a tennis ball. Use.toon-heavyfor a bowling ball (.06) and.toon-lightfor rubber (.38). Rigid objects shouldn’t deform at all. - Keep interfaces subtle. A button that squashes like rubber looks broken. Around .1 reads as pressable; save .38 and more for characters.
- Avoid deforming at rest. Squash and stretch belong to impacts and high speed. An object that sits still should keep its shape.
The Dot demo also shows timing (gravity sets the beat), slow in and slow out (the trail dots bunch up at the top of each bounce) and arcs (the trail is a smooth curve).
For every option, see the Canvas squash functions and the CSS bounce preset.