Skip to content

Solid drawing

Make flat shapes feel 3-D: shading, volume, contact shadows.

Edit this page

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.

Now showing: Solid drawingOpen the Solid drawing 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>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>

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:

Now showing: CSS solid drawingOpen the CSS solid drawing demo on its own page (opens in a new tab)

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

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.

  • 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 light to drawSolid() 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.