defaults
Your first animation
Bounce a ball with two classes, then throw one along an arc on a canvas.
Edit this pageThis page assumes toon is on your page; see Install toon. toon.css animates page elements with classes, and toon.js animates on <canvas>. Use either one, or both.
Your first CSS animation
Section titled “Your first CSS animation”With toon.css included, two classes make a bouncing ball. A second element under it makes its contact shadow (the shadow on the ground, which shrinks as the ball rises). The ball jumps 140px, so the box around it leaves that much room above. That’s the left ball, and the first block of code.
The right ball is the same ball, tuned with custom properties on the element (or on :root, to change everything at once). It gets more squash (it flattens further when it lands), a higher bounce and a slower tempo. --toon-tempo multiplies every duration, so a higher tempo is slower:
tuned: more squash, higher, slower
<!-- The ball jumps 140px, so its box keeps that much room above it. --><div style="display:grid; justify-items:center; padding-top:150px"> <div class="toon-solid toon-bounce" style="width:48px; height:48px"></div> <div class="toon-shadow toon-bounce-shadow" style="width:56px; height:12px"></div></div>
<div style="display:grid; justify-items:center; padding-top:210px"> <div class="toon-solid toon-bounce" style="width:48px; height:48px; --toon-squash:.4; --toon-height:200px; --toon-tempo:1.3"></div> <div class="toon-shadow toon-bounce-shadow" style="width:56px; height:12px; --toon-tempo:1.3"></div></div>Your first Canvas animation
Section titled “Your first Canvas animation”With toon.js included, this throws a ball back and forth along an arc (the curved path a thrown thing follows). Each frame works out the ball’s speed and direction, so it stretches the way it’s moving:
<script src="toon.js"></script><canvas id="c" style="width:100%; height:300px" role="img" aria-label="A ball is thrown back and forth along an arc"></canvas>
<script>const { createStage, tween, arc, ease, squash, withSquash, drawSolid } = Toon;const stage = createStage(document.getElementById('c'));// Reduced motion: freeze the clock, so the ball holds still.if (matchMedia('(prefers-reduced-motion: reduce)').matches) Toon.ticker.timeScale = 0;// The arc spans the canvas, so build it from the stage's current size (it follows the window).const fly = (t) => arc({ x: 40, y: stage.height - 40 }, { x: stage.width - 40, y: stage.height - 40 }, { height: 160 })(t);
let t = 0;tween({ duration: 1000, ease: ease.linear, repeat: Infinity, yoyo: true, onUpdate: (v) => (t = v) });
let last = fly(0);stage.loop((dt) => { // like ticker.add, but pauses while off screen stage.clear(); const p = fly(t); // velocity in px/s → stretch along the direction of travel const s = squash((p.x - last.x) / (dt || 1), (p.y - last.y) / (dt || 1), { amount: 0.0005 }); last = p; withSquash(stage.ctx, p.x, p.y, s, (c) => drawSolid(c, { r: 20 }));});</script>Next steps
Section titled “Next steps”You’ve made a ball bounce and a ball fly along an arc. Pick what to build next: