Recipes
Scripted effects with toon.js that combine several principles: a character jump, a throw along a path, and reduced motion on canvas.
Edit this pageEach recipe uses toon.js and combines several principles in one script. For one class on an ordinary element, see HTML.
Each recipe runs on the screen, and Show code opens its code. The snippets are read from the example pages in examples/recipes/, so what you see is what runs. Only the page styling (the panel, fonts and theme colors) is left out.
A character jump, scripted
Section titled “A character jump, scripted”This uses anticipation, squash and stretch, slow in and slow out and timing. Each tween is one beat, and sequence plays them in order. A withSquash pivot at the ball’s base keeps it planted while it squashes:
<script src="toon.js"></script><canvas style="width:100%; height:300px" role="img" aria-label="A ball crouches, jumps and squashes as it lands"></canvas><button>Jump</button>
<script>const { createStage, tween, sequence, ease, withSquash, drawSolid, groundShadow } = Toon;const stage = createStage(document.querySelector('canvas'));const hero = { y: 0, sy: 1 }; // height off ground, y-scale
// One beat: tween the hero from where it is now to `pose`.const to = (pose, ms, e) => () => tween({ from: { ...hero }, to: pose, duration: ms, ease: e, onUpdate: (v) => Object.assign(hero, v),});let busy = false;async function jump() { if (busy) return; busy = true; await sequence( to({ sy: 0.62 }, 280, ease.outQuad), // crouch to({ y: 30, sy: 1.3 }, 90, ease.linear), // take-off stretch to({ y: 170, sy: 1 }, 320, ease.outQuad), // rise, ease out to({ y: 0, sy: 1.15 }, 300, ease.inQuad), // fall, ease in to({ sy: 0.7 }, 70, ease.outQuad), // land squash to({ sy: 1 }, 420, ease.outElastic), // recover ); busy = false;}
stage.loop(() => { const r = 30, x = stage.width / 2, groundY = stage.height - 24; stage.clear(); groundShadow(stage.ctx, x, groundY, r, hero.y); const scale = { sx: 1 / hero.sy, sy: hero.sy }; // same area // pivot { x: 0, y: r } keeps the base of the ball planted withSquash(stage.ctx, x, groundY - r - hero.y, scale, (c) => drawSolid(c, { r }), { x: 0, y: r });});document.querySelector('button').onclick = jump;// Jump once on load, unless the reader asked for less motion.if (!matchMedia('(prefers-reduced-motion: reduce)').matches) jump();</script>Throwing something along a path
Section titled “Throwing something along a path”Arcs, with squash and stretch along the direction of travel. A quadratic arc run with linear timing follows the same path as a real throw:
The dots show the path. Press Throw to send the ball back.
<script src="toon.js"></script><canvas style="width:100%; height:300px" role="img" aria-label="A ball is thrown back and forth along a dotted arc"></canvas><button>Throw</button>
<script>const stage = Toon.createStage(document.querySelector('canvas'));let fly, t = 0, left = true;
function aim() { // the arc from this side to the other const y = stage.height - 30; const a = { x: 40, y }, b = { x: stage.width - 40, y }; fly = left ? Toon.arc(a, b, { height: 120 }) : Toon.arc(b, a, { height: 120 });}function throwIt() { aim(); left = !left; t = 0; // Linear timing on a quadratic curve = true projectile motion. Toon.tween({ duration: 900, ease: Toon.ease.linear, onUpdate: (v) => (t = v) });}
stage.loop(() => { const ctx = stage.ctx; stage.clear(); ctx.fillStyle = getComputedStyle(document.body).color; // read each frame: follows light/dark for (let i = 0; i <= 24; i++) { // dotted path const p = fly(i / 24); ctx.fillRect(p.x - 1.5, p.y - 1.5, 3, 3); } const a = fly(t), b = fly(Math.min(1, t + 0.01)); const k = 1000 / (0.01 * 900); // progress → px/s const vx = (b.x - a.x) * k, vy = (b.y - a.y) * k; // stretch along the direction of travel const s = Toon.squash(vx, vy, { amount: 0.0006 }); Toon.withSquash(ctx, a.x, a.y, s, (c) => Toon.drawSolid(c, { r: 16 }));});document.querySelector('button').onclick = throwIt;aim(); // show the path straight away// Throw once on load, unless the reader asked for less motion.if (!matchMedia('(prefers-reduced-motion: reduce)').matches) throwIt();</script>Respect reduced motion on canvas
Section titled “Respect reduced motion on canvas”toon.css handles reduced motion for you, and data-motion="play" on an ancestor lets a reader opt back in. On canvas, check the media query yourself and either jump to the end state or slow decorative motion right down:
The clock follows the reader's reduced-motion setting. The checkbox starts at your system setting: tick it to see the frozen clock, untick it to play anyway.
<script src="toon.js"></script><canvas style="width:100%; height:300px" role="img" aria-label="A ball bobs up and down; it stops when motion is reduced"></canvas><label> <input type="checkbox" id="pretend"> Reduce motion</label><p class="note" id="status"></p>
<script>const reduce = matchMedia('(prefers-reduced-motion: reduce)');const pretend = document.querySelector('#pretend');const apply = () => { const on = pretend.checked; // starts at the system setting Toon.ticker.timeScale = on ? 0 : 1; // 0 freezes the clock const pref = reduce.matches ? 'reduced motion' : 'no preference'; document.querySelector('#status').textContent = `Your system setting: ${pref}. The clock is ${on ? 'frozen' : 'running'}.`;};const follow = () => { pretend.checked = reduce.matches; apply(); };follow();reduce.addEventListener('change', follow);pretend.addEventListener('change', apply);
// Something decorative to freeze: a ball bobbing on the clock.const stage = Toon.createStage(document.querySelector('canvas'));stage.loop((dt, time) => { stage.clear(); const y = stage.height / 2 + Math.sin(time * 3) * 60; Toon.drawSolid(stage.ctx, { x: stage.width / 2, y, r: 26 });});</script>When the clock is frozen, tweens stop advancing. Anything that must still end up in its final state, such as the values your onComplete would set, should be set directly instead of animated.