Skip to content

Squash & stretch

Make things feel soft or heavy: they flatten on impact and stretch when fast.

Edit this page

An 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.

Now showing: Dot bounceOpen the Dot bounce demo on its own page (opens in a new tab)
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 &amp; 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 &amp; 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 &amp; 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>

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:

Now showing: CSS bounceOpen the CSS bounce demo on its own page (opens in a new tab)

.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).

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.

  • 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-squash is .22 by default, about a tennis ball. Use .toon-heavy for a bowling ball (.06) and .toon-light for 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.