Even a 2-D animation should feel like it has depth (solid drawing: treating shapes as volumes with weight and light). Shading shows where the light is and that the shape is round. A contact shadow shows where the object sits in space and how high it is.
Move your pointer over the canvas, or use the Light direction slider, to move the light.
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>11 · Solid drawing — 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-bottom: var(--space-sm); }</style></head><body><h1>Solid drawing</h1><p>Treat shapes as volumes in 3-D space with weight and light. Left: a flat disc. Right: the same timing with shading, a spinning surface, and a contact shadow.</p><canvas id="c" style="cursor:crosshair" role="img" aria-label="Two balls bounce side by side: on the left a flat disc, on the right a shaded, spinning globe with a contact shadow. The light direction can be changed."></canvas><p class="demo-try"><label class="demo-act-range">Light direction <input id="lightDir" type="range" min="0" max="360" step="5" value="225"></label> Move your pointer over the canvas, or use the <i>Light direction</i> slider, to move the light.</p>
<script src="../src/toon.js"></script><script src="demo-theme.js"></script><script src="demo-runtime.js"></script><script>ToonDemos.register('11-solid-drawing', (document, Toon, DemoTheme) => { const T = DemoTheme; // design tokens, live (see demo-theme.js) const { createStage, ticker, drawSolid, groundShadow, withSquash, impact, clamp } = Toon; const stage = createStage(document.getElementById('c')); const { ctx } = stage; let light = { x: -0.45, y: -0.55 }; stage.canvas.addEventListener('pointermove', e => { const r = stage.canvas.getBoundingClientRect(); const dx = (e.clientX - r.left) - stage.width * 0.7, dy = (e.clientY - r.top) - stage.height * 0.4; const d = Math.hypot(dx, dy) || 1, m = Math.min(0.7, d / 200); light = { x: (dx / d) * m, y: (dy / d) * m }; });
/** [SOLID] a spinning globe: shaded sphere + meridians projected as half-ellipses. */ function globe(c, r, spin) { drawSolid(c, { r, color: T.secondary, light }); c.save(); c.beginPath(); c.arc(0, 0, r, 0, 7); c.clip(); c.strokeStyle = '#fff'; c.lineWidth = 3; for (let i = 0; i < 8; i++) { const lon = spin + (i * Math.PI) / 4, z = Math.cos(lon); // z > 0 → faces the viewer if (z <= 0) continue; c.globalAlpha = z * 0.6; c.beginPath(); c.ellipse(0, 0, Math.abs(Math.sin(lon)) * r + 0.01, r, 0, -Math.PI / 2, Math.PI / 2, Math.sin(lon) < 0); c.stroke(); } c.globalAlpha = 0.6; c.beginPath(); c.moveTo(-r, 0); c.lineTo(r, 0); c.stroke(); c.restore(); }
stage.loop((dt, t) => { const ground = stage.height - 40, R = 48, H = 170; const ph = (t * 0.9) % 1, h = 4 * ph * (1 - ph) * H; // parabolic bounce const hit = ph < 0.06 || ph > 0.94 ? Math.cos(Math.min(ph, 1 - ph) / 0.06 * Math.PI / 2) : 0; const s = impact(hit * 0.25); stage.clear(); ctx.fillStyle = T.ground; ctx.fillRect(0, ground, stage.width, 40); ctx.fillStyle = T.groundLine; ctx.fillRect(0, ground, stage.width, 2); // flat version const fx = stage.width * 0.28; ctx.fillStyle = T.secondary; ctx.beginPath(); ctx.arc(fx, ground - R - h, R, 0, 7); ctx.fill(); // solid version: shadow pushed away from the light, sphere, rotating surface const sx = stage.width * 0.7; groundShadow(ctx, sx - light.x * 40, ground, R, h, { maxHeight: 260, alpha: 0.4 }); withSquash(ctx, sx, ground - R - h, s, c => globe(c, R, t * 2.2), { x: 0, y: R }); ctx.fillStyle = T.muted; ctx.font = '13px system-ui'; ctx.textAlign = 'center'; ctx.fillText('flat', fx, ground + 26); ctx.fillText('solid', sx, ground + 26); }); const lightDir = document.getElementById('lightDir'); lightDir.addEventListener('input', () => { const a = lightDir.value * Math.PI / 180; light = { x: Math.cos(a) * 0.6, y: Math.sin(a) * 0.6 }; });});</script></body></html>Solid drawing in CSS
Section titled “Solid drawing in CSS”To shade a ball, add .toon-solid to a sized element and put a .toon-shadow under it. 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-solid toon-bounce" style="width:64px; height:64px"></div> <!-- Overlap the ball's base, so the shadow sits under it. --> <div class="toon-shadow toon-bounce-shadow" style="width:76px; height:18px; margin-top:-9px"></div></div>Below, the snippet’s ball is the one on the right, next to a flat disc and the same ball at rest:
.toon-solidat rest
.toon-bouncein motion
.toon-solid builds a highlight, a core shadow and a lighter rim from --toon-color, lit from the top left. .toon-bounce-shadow keeps the shadow in time with .toon-bounce, so it shrinks and fades as the ball rises.
Solid drawing in Canvas
Section titled “Solid drawing in Canvas”drawSolid() paints a shaded sphere from one color and a light direction. shade() lightens or darkens a color, so the box’s faces can follow the same light. groundShadow() shrinks and fades the shadow with height.
<script src="toon.js"></script><canvas style="width:100%; height:180px" role="img" aria-label="A shaded box sits on the floor next to a shaded ball that bounces. Both are lit from the top left, and the ball's shadow shrinks and fades as it rises"></canvas>
<script>const { createStage, drawSolid, groundShadow, shade } = Toon;const stage = createStage(document.querySelector('canvas'));const r = 28, H = 90, BEAT = 1.1; // px, px, seconds per bounceconst light = { x: -0.45, y: -0.55 }; // up and to the left4 collapsed lines
// Reduced motion: freeze the clock; the ball rests on the floor.if (matchMedia('(prefers-reduced-motion: reduce)').matches) { Toon.ticker.timeScale = 0;}
// A box under the same light: the top faces it (lighter), the// right side turns away (darker). x: left edge, y: the floor.function box(ctx, x, y, s, color) { const d = s * 0.35; // depth, drawn up and right const face = (fill, ...pts) => { ctx.beginPath(); for (const [px, py] of pts) ctx.lineTo(px, py); ctx.fillStyle = fill; ctx.fill(); }; face(color, [x, y], [x + s, y], [x + s, y - s], [x, y - s]); face(shade(color, 0.35), [x, y - s], [x + s, y - s], [x + s + d, y - s - d], [x + d, y - s - d]); face(shade(color, -0.35), [x + s, y], [x + s + d, y - d], [x + s + d, y - s - d], [x + s, y - s]);}
stage.loop((dt, t) => { const ctx = stage.ctx, x = stage.width / 2; const floor = stage.height - 24;4 collapsed lines
// Colors from the page: --toon-color for shapes, text for ink. const color = getComputedStyle(document.documentElement) .getPropertyValue('--toon-color').trim() || '#ff6b4a'; const ink = getComputedStyle(document.body).color; // Height above the floor: a parabola, one bounce per BEAT. const p = (t / BEAT) % 1, h = 4 * p * (1 - p) * H;
stage.clear(); ctx.fillStyle = ink; ctx.fillRect(0, floor, stage.width, 2); groundShadow(ctx, x - 46, floor, 34, 0); // under the box box(ctx, x - 80, floor, 56, color); groundShadow(ctx, x + 60, floor, r, h, { maxHeight: H * 2 }); const y = floor - r - h; // ball center drawSolid(ctx, { x: x + 60, y, r, color, light });});</script>Solid drawing: tuning tips
Section titled “Solid drawing: tuning tips”- Keep one light source. Every highlight in the scene sits on the same side, and every shadow on the other. The CSS preset always lights from the top left, so pass the same
lighttodrawSolid()when you mix the two. - Anchor the shadow to the ground. The shadow stays on the floor and changes size and opacity. It never rises with the object, or the object seems to float.
- Push the shadow away from the light. Offsetting it a few px to the side opposite the light, as the globe demo does, makes the light direction easier to read.
- Keep volume constant. Solid drawing and squash and stretch work together: the shape deforms, but it never gains or loses mass.
For every option, see the Canvas drawing functions and the CSS .toon-solid shape.