Skip to content

An AI skill that turns books into animated, narrated, interactive web experiences

feat(science): animate every actor in ch10

yzreef89f738fd0
diff --git a/site/science/ch10/index.html b/site/science/ch10/index.html
index def4a92..18fc729 100644
--- a/site/science/ch10/index.html
+++ b/site/science/ch10/index.html
@@ -10,6 +10,7 @@
<body>
<script src="../lib/engine.js"></script>
<script src="../lib/figures.js"></script>
+<script src="../lib/actors.js"></script>
<script src="../lib/physics.js"></script>
<script src="audio/en/timings.js"></script>
<script>
@@ -22,14 +23,36 @@ const GY = 560; // ground line in side
const ground = p => { phBlock(p, 0, GY, 1600, 340, PH.grass, { top: 8, ol: 0 }); };
const N = 40; // force arrows: 40 px per newton (scaled per scene where noted)
+/* ---------- actors and light context ---------- */
+const park = (p, xs = [90, 1530]) => xs.forEach(x => phSprite(p, 'tree', { x, y: GY + 2 }));
+function rink(p) { // an ice rink: boards behind, a red centre line
+ phBlock(p, 0, GY, 1600, 340, '#cfe8f3', { top: 8, ol: 0 });
+ phBlock(p, 0, GY - 64, 1600, 64, '#eef5f7', { top: 0, ol: 0 });
+ mk('rect', { x: 0, y: GY - 70, width: 1600, height: 8, fill: '#3f6f9e' }, p);
+ mk('rect', { x: 796, y: GY + 8, width: 8, height: 332, fill: '#d24b4b', opacity: .5 }, p);
+}
+function goal(p, x) { // a pixel soccer goal (side view)
+ path(p, `M${x} ${GY}V${GY - 150}H${x + 90}`, { stroke: '#ece8dc', 'stroke-width': 10 });
+ path(p, `M${x + 10} ${GY - 140}L${x + 90} ${GY}M${x + 30} ${GY - 140}L${x + 90} ${GY - 40}`, { stroke: '#ece8dc', 'stroke-width': 2, opacity: .5 });
+}
+// A pusher with a cart (or car) that stays on her hands: at(x, dist) walks her and places the load at her hand.
+function pusher(p, role, load = 'cart') {
+ const a = phActor(p, role, { y: GY, anim: 'push' }), c = load === 'car' ? phCar(p, { y: GY }) : phSprite(p, 'cart', { y: GY });
+ return { a, c, at(x, dist, walk = true) {
+ put(a, { x }); if (walk) a.stride('push', dist); else a.show('push', 0);
+ const [hx] = a.hand('F'); put(c, { x: hx + (load === 'car' ? 126 : 52) }); if (load === 'car') phRoll(c, hx);
+ return hx;
+ } };
+}
+
/* ---------- beats ---------- */
const BEATS = [
/* ---------- part 1: force and net force ---------- */
['intro', 'Force and Newton’s laws', (m, D) => {
const p = panel(0);
- ground(p);
- const cart = phSprite(p, 'cart', { y: GY }), you = phSprite(p, 'you_point', { y: GY }), f = forceArrow(p);
- clockRun(t => { const x = 260 + 20 * t * t; put(cart, { x: x + 110 }); put(you, { x }); f.set(x + 60, GY - 110, 120, 0, 'push'); }, D);
+ ground(p); park(p, [1200, 1460]);
+ const ps = pusher(p, 'you'), f = forceArrow(p);
+ clockRun(t => { const d = 20 * t * t, hx = ps.at(260 + d, d); f.set(hx + 20, GY - 140, 120, 0, 'push'); }, D);
const g = G(p, { o: 0, y: 24 });
T(g, 'Unit 3 · Motion, Forces, and Work · Chapter 10', { x: 800, y: 130, size: 28, fill: COL.dim, anchor: 'middle' });
phLabel(g, 'Force and Newton’s Laws', { x: 800, y: 240, size: 92, weight: 600 });
@@ -38,12 +61,12 @@ const BEATS = [
}],
['force', 'A push or a pull', (m, D) => {
const p = panel(0);
- ground(p);
+ ground(p); park(p, [40, 1560]);
txt(p, 'A force is a push or a pull', 800, 110, .2, { size: 44 });
const t1 = m('push'), t2 = m('pull'), ts = m('shape');
const push = G(p), pull = G(p);
- const c1 = phSprite(push, 'cart', { y: GY }), you = phSprite(push, 'you_point', { y: GY }), f1 = forceArrow(push);
- const c2 = phSprite(pull, 'cart', { y: GY }), fr = phPerson(pull, { role: 'friend', y: GY }), rope = path(pull, '', { stroke: '#c9b38d', 'stroke-width': 5 }), f2 = forceArrow(pull);
+ const ps = pusher(push, 'you'), f1 = forceArrow(push);
+ const pc = phSprite(pull, 'cart', { y: GY }), pa = phActor(pull, 'friend', { y: GY, anim: 'pull' }), rope = path(pull, '', { stroke: '#c9b38d', 'stroke-width': 5 }), f2 = forceArrow(pull);
T(push, 'push', { x: 330, y: 200, size: 32, weight: 700, fill: FORCE, anchor: 'middle' });
T(pull, 'pull', { x: 1100, y: 200, size: 32, weight: 700, fill: FORCE, anchor: 'middle' });
put(pull, { o: 0 }); show(pull, t2 - .2);
@@ -55,11 +78,12 @@ const BEATS = [
show(can, ts); hide(sizes, ts - .3, .3); tw(push, { o: .35 }, ts, .4); tw(pull, { o: .35 }, ts, .4);
txt(p, 'a force can change a shape', 800, 860, ts + .3, { size: 30, fill: COL.chalk });
clockRun(t => {
- const u = Math.max(0, t - t1), x = 150 + Math.min(250, 30 * u * u);
- put(you, { x }); put(c1, { x: x + 110 }); f1.set(x + 60, GY - 110, t > t1 - .2 ? 110 : 0, 0, '');
- const v = Math.max(0, t - t2), x2 = 1150 + Math.min(260, 28 * v * v);
- put(fr, { x: x2 }); if (v > 0 && x2 < 1410) walkTo(fr, x2); else phPose(fr); put(c2, { x: x2 - 240 });
- rope.setAttribute('d', `M${x2 - 180} ${GY - 50}L${x2 - 12} ${GY - 62}`); f2.set(x2 - 180, GY - 110, t > t2 - .2 ? 110 : 0, 0, '');
+ const u = Math.max(0, t - t1), d = Math.min(250, 30 * u * u), hx = ps.at(150 + d, d, d > 0);
+ f1.set(hx + 20, GY - 140, t > t1 - .2 ? 110 : 0, 0, '');
+ const v = Math.max(0, t - t2), d2 = Math.min(260, 28 * v * v), x2 = 1150 + d2;
+ put(pa, { x: x2 }); pa.stride('pull', d2);
+ const [rx, ry] = pa.hand('F'), cx = rx - 150; put(pc, { x: cx });
+ rope.setAttribute('d', `M${cx + 50} ${GY - 60}L${rx} ${ry}`); f2.set(cx + 60, GY - 120, t > t2 - .2 ? 100 : 0, 0, '');
const k = ramp(t, ts + .6, ts + 1.4), w = 60 + 70 * (1 - k);
phFrame(cs, k >= 1 ? 'can_crushed' : 'can');
sq1.set(800 - w - 90, 720, 90, 0, ''); sq2.set(800 + w + 90, 720, 90, 180, '');
@@ -67,7 +91,9 @@ const BEATS = [
}],
['net', 'Net force', (m, D) => {
const p = panel(0);
- ground(p);
+ phBlock(p, 0, GY - 300, 1600, 300, '#26372f', { top: 0, ol: 0 });
+ [200, 600, 1000, 1400].forEach(x => mk('rect', { x, y: GY - 300, width: 6, height: 300, fill: '#1f2e28' }, p));
+ ground(p); crate(p, { x: 170, y: GY, w: 110, h: 90 }); crate(p, { x: 170, y: GY - 90, w: 90, h: 80 }); crate(p, { x: 1440, y: GY, w: 120, h: 100 });
txt(p, 'Net force: the total of all the forces on an object', 800, 110, .2, { size: 40 });
const c = crate(p, { x: 800, y: GY }), A = forceArrow(p), B = forceArrow(p), NF = forceArrow(p, { color: NETF, w: 14, size: 32 });
const ta = m('add'), ts = m('sub'), eq1 = runs(p, [['3 N + 2 N = ', COL.chalk], ['5 N →', NETF]], { x: 800, y: 790, size: 44, weight: 700 }),
@@ -85,20 +111,24 @@ const BEATS = [
}, { lead: false }],
['balance', 'Balanced and unbalanced', (m, D) => {
const p = panel(0);
- ground(p);
- const tb = m('bal'), tu = m('unbal');
- const L = phSprite(p, 'friend_point', { y: GY }), R = phSprite(p, 'you_point', { y: GY }), rope = path(p, '', { stroke: '#c9b38d', 'stroke-width': 7 });
- phFace(R, -1);
- const mark = mk('rect', { x: -6, y: -40, width: 12, height: 40, fill: COL.bad }, G(p)), mg = mark.parentNode;
+ ground(p); park(p, [70, 1540]);
+ const tb = m('bal'), tu = m('unbal'), S = .4 * N;
+ const L = phActor(p, 'friend', { y: GY, anim: 'strain' }), R = phActor(p, 'you', { y: GY, anim: 'strain' }).face(-1);
+ const rope = path(p, '', { stroke: '#c9b38d', 'stroke-width': 7 }), mark = G(p);
+ mk('rect', { x: -6, y: -20, width: 12, height: 40, fill: COL.bad }, mark);
path(p, `M800 ${GY + 10}V${GY + 60}`, { stroke: COL.dim, 'stroke-width': 4, 'stroke-dasharray': '6 6' });
const FL = forceArrow(p), FR = forceArrow(p), NF = forceArrow(p, { color: NETF, w: 14, size: 32 });
const tag = phLabel(p, '', { x: 800, y: 180, size: 48 });
clockRun(t => {
- const pull = t < tu ? 10 : 10 + 5 * ramp(t, tu, tu + .8), u = Math.max(0, t - tu - .8), dx = Math.min(140, 20 * u * u);
- put(L, { x: 380 + dx }); put(R, { x: 1160 + dx }); rope.setAttribute('d', `M${430 + dx} ${GY - 70}H${1110 + dx}`); put(mg, { x: 800 + dx, y: GY - 70 + 20 });
+ const pull = t < tu ? 10 : 10 + 5 * ramp(t, tu, tu + .8), u = Math.max(0, t - tu - .8), dx = Math.min(100, 16 * u * u);
+ const shake = Math.sin(t * 31) * 1.5; // straining: a small shake
+ put(L, { x: 420 + dx + shake }); put(R, { x: 1150 + dx - shake });
+ if (dx > 0) { L.stride('stumble', dx); R.stride('backstep', dx); } else { L.show('strain', t * 3); R.show('strain', t * 3 + 1); }
+ const [lx, ly] = L.hand('F'), [rx, ry] = R.hand('F');
+ rope.setAttribute('d', `M${lx} ${ly}L${rx} ${ry}`); put(mark, { x: (lx + rx) / 2, y: (ly + ry) / 2 });
const on = t > tb - .2;
- FL.set(430 + dx, GY - 120, on ? 10 * N * .6 : 0, 180, '10 N'); FR.set(1110 + dx, GY - 120, on ? pull * N * .6 : 0, 0, `${Math.round(pull)} N`);
- NF.set(700 + dx, GY + 110, t > tu + .9 ? 5 * N * .6 : 0, 0, 'net force: 5 N', 0, 60);
+ FL.set(lx - 20, GY - 150, on ? 10 * S : 0, 180, '10 N'); FR.set(rx + 20, GY - 150, on ? pull * S : 0, 0, `${Math.round(pull)} N`);
+ NF.set(700 + dx, GY + 110, t > tu + .9 ? 5 * S : 0, 0, 'net force: 5 N', 0, -14);
phText(tag, t < tb ? '' : t < tu ? 'balanced: net force = 0 N' : 'unbalanced: net force = 5 N →');
}, D);
}],
@@ -120,11 +150,12 @@ const BEATS = [
}, { lead: false }],
['q1', 'Quick check', (m, D) => {
const p = panel(0);
- ground(p);
- const L = phSprite(p, 'friend_point', { x: 420, y: GY }), R = phSprite(p, 'you_point', { x: 1180, y: GY });
- phFace(R, -1);
- path(p, `M470 ${GY - 70}H1130`, { stroke: '#c9b38d', 'stroke-width': 7 });
- forceArrow(p).set(470, GY - 120, 10 * N * .6, 180, 'brother: 10 N'); forceArrow(p).set(1130, GY - 120, 15 * N * .6, 0, 'you: 15 N');
+ ground(p); park(p, [70, 1540]);
+ const L = phActor(p, 'friend', { x: 420, y: GY, anim: 'strain' }), R = phActor(p, 'you', { x: 1150, y: GY, anim: 'strain' }).face(-1), rope = path(p, '', { stroke: '#c9b38d', 'stroke-width': 7 });
+ const fl = forceArrow(p), fr = forceArrow(p);
+ clockRun(t => { const sh = Math.sin(t * 31) * 1.5; put(L, { x: 420 + sh }); put(R, { x: 1150 - sh }); L.show('strain', t * 3); R.show('strain', t * 3 + 1);
+ const [lx, ly] = L.hand('F'), [rx, ry] = R.hand('F'); rope.setAttribute('d', `M${lx} ${ly}L${rx} ${ry}`);
+ fl.set(lx - 20, GY - 150, 10 * .4 * N, 180, 'brother: 10 N'); fr.set(rx + 20, GY - 150, 15 * .4 * N, 0, 'you: 15 N'); }, D);
}, { ask: done => quiz(Q_CARD, [
{ id: 'c-tug', prompt: ['Tug-of-war: you pull with 15 newtons, your brother pulls the other way with 10 newtons. What is the ', h('b', '', 'net force'), '?'],
build: blanks([{ parts: ['net force = 15 − 10 = ', { box: '5' }, ' newtons'], why: 'Opposite directions subtract: 15 − 10 = 5 newtons, toward you.', hint: 'The forces point in opposite directions, so subtract.' }]) },
@@ -154,8 +185,9 @@ const BEATS = [
/* ---------- part 2: Newton's first law ---------- */
['part2', 'Part 2 · The first law', (m, D) => {
const p = panel(0);
- ground(p);
- phBall(p, 28, { x: 800, y: GY - 28 }); phSprite(p, 'you_point', { x: 690, y: GY });
+ ground(p); park(p, [180]); goal(p, 1380);
+ phBall(p, 28, { x: 800, y: GY - 28 }); const k = phActor(p, 'you', { x: 740, y: GY, anim: 'stand' });
+ clockRun(t => k.show('stand'), D);
const g = G(p, { o: 0, y: 24 });
T(g, 'Chapter 10 · Force and Newton’s laws', { x: 800, y: 130, size: 28, fill: COL.dim, anchor: 'middle' });
phLabel(g, 'The First Law', { x: 800, y: 250, size: 110, weight: 600 });
@@ -166,8 +198,8 @@ const BEATS = [
const p = panel(0);
const tk = m('kick'), ts = m('space'), R = 28, V0 = 360, A = 90, T0 = V0 / A; // grass friction: 90 px/s², stops after 4 s
const field = camera(p, D);
- ground(field.world);
- const ball = phBall(field.world, R, { x: 560, y: GY - R }), kid = phSprite(field.world, 'you_point', { x: 470, y: GY });
+ ground(field.world); park(field.world, [120]); goal(field.world, 1440);
+ const ball = phBall(field.world, R, { x: 560, y: GY - R }), kid = phActor(field.world, 'you', { x: 482, y: GY, anim: 'stand' });
const vA = path(field.world, '', { stroke: VEL, 'stroke-width': 8 }), kick = forceArrow(field.world), fr = forceArrow(field.world);
const stay = txt(p, 'at rest: it stays at rest', 800, 150, m('stay'), { size: 36 });
const roll = txt(p, 'friction from grass and air slows it and stops it', 800, 210, m('stop'), { size: 30, fill: FORCE });
@@ -183,12 +215,12 @@ const BEATS = [
const sl = G(p, { o: 0 }); phLabel(sl, 'deep space: no friction', { x: 800, y: 120, size: 38 }); phLabel(sl, 'velocity stays the same', { x: 800, y: 170, size: 30, fill: VEL });
show(sl, ts + .4); hide(roll, ts, .3); hide(stay, ts, .3);
clockRun(t => {
- const u = Math.max(0, t - tk - .15), tt = Math.min(u, T0), x = 560 + V0 * tt - A * tt * tt / 2, v = u < T0 ? V0 - A * u : 0;
+ const u = Math.max(0, t - tk), tt = Math.min(u, T0), x = 560 + V0 * tt - A * tt * tt / 2, v = t > tk && u < T0 ? V0 - A * u : 0; // at rest until the kick
put(ball, { x }); put(ball._spin, { r: (x - 560) / R * 180 / Math.PI });
arrowAt(vA, x + R + 6, GY - R, v * .35, 0);
- kick.set(560 - R - 110, GY - R, t > tk - .3 && t < tk + .5 ? 100 : 0, 0, 'kick', 0, -14);
+ kick.set(560 - R - 100, GY - R - 70, t > tk - .3 && t < tk + .5 ? 100 : 0, 0, 'kick', 0, -14);
fr.set(x - R - 6, GY - 80, v > 0 && t > tk + .5 ? 70 : 0, 180, 'friction');
- put(kid, { x: 470 + Math.min(40, 80 * Math.max(0, t - tk + .4)) });
+ kid.show(t < tk - .55 || t > tk + .7 ? 'stand' : 'kick', t < tk - .12 ? 0 : t < tk + .12 ? 1 : 2); // wind-up, contact at the word, follow-through
const z = sx(t); put(sb, { x: z }); put(sb._spin, { r: z / R * 180 / Math.PI }); arrowAt(sv, z + R + 6, 450, 110, 0);
}, D);
}, { lead: false }],
@@ -219,8 +251,8 @@ const BEATS = [
const tc = m('catch'), LA = 330, LB = 600;
txt(p, 'More mass → more inertia', 800, 110, .2, { size: 44 });
[LA, LB].forEach(y => phBlock(p, 0, y + 70, 1600, 8, PH.walk, { top: 0, ol: 0 }));
- const ct = phSprite(p, 'friend_point', { x: 1240, y: LA + 70 }), cb = phSprite(p, 'friend_point', { x: 1240, y: LB + 70 });
- phFace(ct, -1); phFace(cb, -1);
+ const ct = phActor(p, 'friend', { x: 1240, y: LA + 70, anim: 'catch' }).face(-1), cb = phActor(p, 'friend', { x: 1240, y: LB + 70, anim: 'catch' }).face(-1);
+ park(p, [1500]);
const tb = mk('circle', { r: 14, fill: '#d7e24a', ...phLine(2) }, G(p)).parentNode, bb = mk('circle', { r: 40, fill: '#e07a2e', ...phLine(2.5) }, G(p)).parentNode;
path(bb, 'M-40 0H40M0 -40V40', { stroke: '#7a3a12', 'stroke-width': 3 });
const vT = path(p, '', { stroke: VEL, 'stroke-width': 8 }), vB = path(p, '', { stroke: VEL, 'stroke-width': 8 }), sT = forceArrow(p), sB = forceArrow(p);
@@ -232,7 +264,8 @@ const BEATS = [
clockRun(t => {
const u = t - tc, xT = u < 0 ? 200 : Math.min(xc, 200 + V * u) + (t > tTen ? Math.min(12, 30 * (t - tTen)) : 0);
const stopT = Math.max(0, Math.min(1.2, t - tTen)), xB = u < 0 ? 200 : t < tTen ? 200 + V * u : xc + V * stopT - V / 2.4 * stopT * stopT;
- put(tb, { x: xT, y: LA + 40 }); put(bb, { x: xB, y: LB + 30 }); put(cb, { x: 1240 + Math.max(0, xB - xc) });
+ put(tb, { x: xT, y: LA + 40 }); put(bb, { x: xB, y: LB + 30 });
+ const push = Math.max(0, xB - xc); put(cb, { x: 1240 + push }); if (push > 2 && t < tTen + 1.2) cb.stride('backstep', push); else cb.show('catch', t > tTen ? 1 : 0); ct.show('catch', 0);
arrowAt(vT, xT + 20, LA - 10, t < tTen && u > 0 ? 90 : 0, 0); arrowAt(vB, xB + 46, LB - 30, t < tTen + 1.2 && u > 0 ? 90 * (t < tTen ? 1 : 1 - stopT / 1.2) : 0, 0);
sT.set(xT + 70, LA + 20, t > tTen && t < tTen + .35 ? 60 : 0, 180, ''); sB.set(xB + 110, LB + 10, t > tTen && t < tTen + 1.2 ? 60 : 0, 180, '');
}, D);
@@ -292,9 +325,9 @@ const BEATS = [
/* ---------- part 3: Newton's second law ---------- */
['part3', 'Part 3 · The second law', (m, D) => {
const p = panel(0);
- ground(p);
- const cart = phSprite(p, 'cart', { y: GY }), you = phSprite(p, 'you_point', { y: GY }), f = forceArrow(p), a = path(p, '', { stroke: ACC, 'stroke-width': 8 });
- clockRun(t => { const x = 300 + 18 * t * t; put(cart, { x: x + 110 }); put(you, { x }); f.set(x + 60, GY - 110, 110, 0, 'force'); arrowAt(a, x + 60, GY - 160, 90, 0); }, D);
+ ground(p); park(p, [1250, 1500]);
+ const ps = pusher(p, 'you'), f = forceArrow(p), a = path(p, '', { stroke: ACC, 'stroke-width': 8 });
+ clockRun(t => { const d = 18 * t * t, hx = ps.at(300 + d, d); f.set(hx + 20, GY - 140, 110, 0, 'force'); arrowAt(a, hx + 20, GY - 180, 90, 0); }, D);
const g = G(p, { o: 0, y: 24 });
T(g, 'Chapter 10 · Force and Newton’s laws', { x: 800, y: 130, size: 28, fill: COL.dim, anchor: 'middle' });
phLabel(g, 'The Second Law', { x: 800, y: 250, size: 110, weight: 600 });
@@ -325,16 +358,21 @@ const BEATS = [
const p = panel(0);
const LA = 400, LB = 720, t0 = m('cart', -.6);
[LA, LB].forEach(y => phBlock(p, 0, y, 1600, 10, PH.walk, { top: 0, ol: 0 }));
- const cart = phSprite(p, 'cart', { y: LA }), y1 = phSprite(p, 'you_point', { y: LA }), car = phCar(p, { y: LB }), y2 = phSprite(p, 'friend_point', { y: LB });
+ mk('rect', { x: 0, y: LB + 10, width: 1600, height: 170, fill: '#3a4541' }, p); // the car lane is a parking lot
+ [300, 700, 1100, 1500].forEach(x => mk('rect', { x, y: LB + 30, width: 8, height: 110, fill: PH.line, opacity: .5 }, p));
+ const A = phActor(p, 'you', { y: LA, anim: 'push' }), cart = phSprite(p, 'cart', { y: LA });
+ const B = phActor(p, 'friend', { y: LB, anim: 'push' }), car = phCar(p, { y: LB });
const f1 = forceArrow(p), f2 = forceArrow(p), a1 = forceArrow(p, { color: ACC, w: 8 }), a2 = forceArrow(p, { color: ACC, w: 8 });
phLabel(p, 'cart: 20 kg', { x: 120, y: LA - 190, size: 30, anchor: 'start' }); phLabel(p, 'car: 1,000 kg', { x: 120, y: LB - 190, size: 30, anchor: 'start' });
const why = G(p, { o: 0 });
phLabel(why, 'a = 200 N ÷ 20 kg = 10 m/s²', { x: 1150, y: LA - 190, size: 30, fill: ACC }); phLabel(why, 'a = 200 N ÷ 1,000 kg = 0.2 m/s²', { x: 1150, y: LB - 190, size: 30, fill: ACC });
show(why, m('why'));
clockRun(t => {
- const u = Math.max(0, t - t0), xa = 220 + Math.min(1100, 10 * 30 * u * u / 2), xb = 220 + .2 * 30 * u * u / 2;
- put(y1, { x: xa }); put(cart, { x: xa + 110 }); f1.set(xa + 60, LA - 110, 120, 0, '200 N'); a1.set(xa + 60, LA - 150, 100, 0, '');
- put(y2, { x: xb }); put(car, { x: xb + 170 }); phRoll(car, xb); f2.set(xb + 40, LB - 120, 120, 0, '200 N'); a2.set(xb + 40, LB - 160, 2, 0, '');
+ const u = Math.max(0, t - t0), da = Math.min(1100, 150 * u * u), db = 3 * u * u;
+ put(A, { x: 220 + da }); A.stride('push', da); const [ha] = A.hand('F'); put(cart, { x: ha + 52 });
+ f1.set(ha + 20, LA - 130, 120, 0, '200 N', 80, 20); a1.set(ha + 20, LA - 200, 100, 0, '');
+ put(B, { x: 200 + db, y: LB + Math.sin(t * 9) * 1.2 }); B.stride('push', db * 4); const [hb] = B.hand('F'); put(car, { x: hb + 126 }); phRoll(car, db);
+ f2.set(hb + 30, LB - 130, 120, 0, '200 N', 80, 20); a2.set(hb + 30, LB - 200, 2, 0, '');
}, D);
}],
['calc', 'Net force = mass × acceleration', (m, D) => {
@@ -361,16 +399,16 @@ const BEATS = [
const xAt = t => { let x = 0; for (let u = 0; u < t; u += .05) x += v(u) * 30 * .05; return x; }; // deterministic sum over fixed steps
const cam = camera(p, D, { x: 800, y: 560 });
cam.cut({ x: t => 300 + xAt(t) + 120 }, 0);
- const road = phRoad(cam), bike = G(road.vehicles), bk = phSprite(bike, 'bike', {}), rd = phSprite(bike, 'rider', { x: -4, y: -40 });
+ const road = phRoad(cam), bike = phBike(road.vehicles, 'you');
const vv = path(road.front, '', { stroke: VEL, 'stroke-width': 8 }), wf = forceArrow(road.front);
const vl = G(p), vlab = phLabel(vl, '', { size: 30, fill: VEL });
- const streaks = G(p, { o: .5 }), sl = Array.from({ length: 9 }, (_, i) => path(streaks, '', { stroke: COL.chalk, 'stroke-width': 4 }));
+ const streaks = G(p, { o: .5 }), sl = Array.from({ length: 9 }, () => path(streaks, '', { stroke: COL.chalk, 'stroke-width': 4 }));
const tag = phLabel(p, '', { x: 800, y: 120, size: 40 });
clockRun(t => {
- const x = 300 + xAt(t), sp = v(t), dir = t < ta - .3 ? 1 : -1;
- put(bike, { x, y: ROAD.near });
- arrowAt(vv, x - 50, ROAD.near - 190, sp * 14, 0); wf.set(dir > 0 ? x - 160 : x + 160, ROAD.near - 100, t > tw1 - .3 ? 110 : 0, dir > 0 ? 0 : 180, 'wind', 0, -10);
- phText(vlab, `velocity: ${sp.toFixed(1)} m/s →`); cam.track(vlab, x, ROAD.near - 220, t);
+ const d = xAt(t), x = 300 + d, sp = v(t), dir = t < ta - .3 ? 1 : -1;
+ put(bike, { x, y: ROAD.near }); bike.ride(d);
+ arrowAt(vv, x - 50, ROAD.near - 200, sp * 14, 0); wf.set(dir > 0 ? x - 170 : x + 170, ROAD.near - 110, t > tw1 - .3 ? 110 : 0, dir > 0 ? 0 : 180, 'wind', 0, -10);
+ phText(vlab, `velocity: ${sp.toFixed(1)} m/s →`); cam.track(vlab, x, ROAD.near - 230, t);
phText(tag, t < tw1 ? '' : dir > 0 ? 'wind at your back: net force forward → speed up' : 'against the wind: net force backward → slow down');
sl.forEach((e, i) => { const yy = 200 + 70 * i, xx = ((i * 337 + 900 * t * dir) % 1800 + 1800) % 1800 - 100; e.setAttribute('d', `M${xx} ${yy}h${70 * dir}`); });
}, D);
@@ -407,11 +445,14 @@ const BEATS = [
/* ---------- part 4: Newton's third law ---------- */
['part4', 'Part 4 · The third law', (m, D) => {
const p = panel(0);
- phBlock(p, 0, GY, 1600, 340, '#cfe8f3', { top: 8, ol: 0 });
- const a = phSprite(p, 'you_point', { y: GY }), b = phSprite(p, 'friend_point', { y: GY }), fa = forceArrow(p), fb2 = forceArrow(p);
- phFace(b, -1);
- clockRun(t => { const d = Math.min(1, t / 1.2), s = 60 * Math.max(0, t - 1.2); put(a, { x: 760 - s }); put(b, { x: 840 + s });
- fa.set(790 - s, GY - 110, t < 1.4 ? 90 : 0, 180, ''); fb2.set(810 + s, GY - 110, t < 1.4 ? 90 : 0, 0, ''); }, D);
+ rink(p);
+ const a = phActor(p, 'you', { y: GY, anim: 'skate' }), b = phActor(p, 'friend', { y: GY, anim: 'skate' }).face(-1), fa = forceArrow(p), fb2 = forceArrow(p);
+ clockRun(t => {
+ const s = 70 * Math.max(0, t - 1.1) - 6 * Math.max(0, t - 1.1) ** 2; // glide apart, slowly easing
+ put(a, { x: 740 - Math.max(0, s), r: t > 1.1 ? -4 : 0 }); put(b, { x: 860 + Math.max(0, s), r: t > 1.1 ? 4 : 0 });
+ a.show('skate', t < 1.1 ? 0 : 1); b.show('skate', t < 1.1 ? 0 : 1);
+ fa.set(760 - s, GY - 150, t < 1.3 ? 90 : 0, 180, ''); fb2.set(840 + s, GY - 150, t < 1.3 ? 90 : 0, 0, '');
+ }, D);
const g = G(p, { o: 0, y: 24 });
T(g, 'Chapter 10 · Force and Newton’s laws', { x: 800, y: 130, size: 28, fill: COL.dim, anchor: 'middle' });
phLabel(g, 'The Third Law', { x: 800, y: 250, size: 110, weight: 600 });
@@ -421,57 +462,64 @@ const BEATS = [
['law3', 'Action and reaction', (m, D) => {
const p = panel(0);
const law = G(p, { o: 0 }); phLabel(law, 'For every action, there is an equal and opposite reaction.', { x: 800, y: 130, size: 40 }); show(law, m('law'));
- phBlock(p, 0, GY, 1600, 340, '#cfe8f3', { top: 8, ol: 0 });
- T(p, 'on ice, so nothing else gets in the way', { x: 800, y: GY + 60, size: 24, fill: '#3b5560', anchor: 'middle' });
- const you = phSprite(p, 'you_point', { x: 700, y: GY }), ball = mk('circle', { r: 30, fill: '#22282a', ...phLine(2) }, G(p)).parentNode;
+ rink(p);
+ const you = phActor(p, 'you', { x: 700, y: GY, anim: 'stand' }), ball = mk('circle', { r: 30, fill: '#22282a', ...phLine(2) }, G(p)).parentNode;
[[-8, -10], [6, -14], [2, 4]].forEach(([x, y]) => mk('circle', { cx: x, cy: y, r: 4, fill: '#5a6266' }, ball));
const act = forceArrow(p, { w: 11 }), rea = forceArrow(p, { w: 11 });
const tb = m('bowl');
const eq = G(p, { o: 0 }); phLabel(eq, 'same size: 50 N and 50 N', { x: 800, y: 230, size: 34 }); show(eq, m('equal'));
const op = G(p, { o: 0 }); phLabel(op, 'opposite directions, on two different objects', { x: 800, y: 280, size: 34 }); show(op, m('opp'));
+ // wind-up (ball at the chest), release at "bowl", then recoil: the light ball flies, you slide back a little
clockRun(t => {
- const u = Math.max(0, t - tb), push = t > tb - .2 && t < tb + .6;
- const xb = 770 + 520 * u, xy = 700 - Math.min(160, 90 * u); // the ball is light and flies; you are heavy and slide a little
- put(ball, { x: u > 0 ? Math.min(1300, xb) : 770, y: GY - 70 }); put(you, { x: xy });
- act.set(Math.min(1300, u > 0 ? xb : 770) + 36, GY - 70, push || t > tb ? 120 : 0, 0, 'on the ball: 50 N', -40, -10);
- rea.set(xy - 50, GY - 150, push || t > tb ? 120 : 0, 180, 'on you: 50 N', 0, -10);
+ const u = Math.max(0, t - tb), xy = 700 - Math.min(160, 90 * u), fr = t < tb - .7 ? null : t < tb ? 0 : t < tb + .35 ? 1 : 2;
+ put(you, { x: xy }); if (fr === null) you.show('stand'); else you.show('throw', fr);
+ let bx, by;
+ if (u <= 0) { you.show('throw', t < tb - .7 ? 0 : 0); [bx, by] = you.hand('F'); bx += 22; by -= 4; }
+ else { const [hx, hy] = [700 + 60, GY - 96]; bx = Math.min(1300, hx + 520 * u); by = Math.min(GY - 30, hy + 60 * u * u); }
+ put(ball, { x: bx, y: by });
+ act.set(Math.min(1300, bx) + 36, by, t > tb ? 120 : 0, 0, 'on the ball: 50 N', -40, -10);
+ rea.set(xy - 50, GY - 150, t > tb ? 120 : 0, 180, 'on you: 50 N', 0, -10);
}, D);
}, { lead: false }],
['pairs', 'Action-reaction pairs', (m, D) => {
const p = panel(0);
const tt = m('tramp'), tr = m('run');
txt(p, 'Action-reaction pairs', 800, 110, .2, { size: 44 });
- // left: a trampoline bounce; right: a runner's foot and the ground
const L = G(p, { o: 0 }); show(L, tt - .3);
+ path(L, 'M120 640V520M120 560H680M680 640V520', { stroke: '#5a6d63', 'stroke-width': 6, opacity: .7 }); // a backyard fence
path(L, 'M180 640V720M620 640V720', { stroke: '#7d8a86', 'stroke-width': 10 });
const bed = path(L, '', { stroke: '#2f3a44', 'stroke-width': 10 });
- const jumper = phSprite(L, 'you', {}), down = forceArrow(L), up = forceArrow(L);
+ const jumper = phActor(L, 'you', { x: 400, anim: 'jump' }), down = forceArrow(L), up = forceArrow(L);
phLabel(L, 'you push down; it pushes you up', { x: 400, y: 790, size: 28 });
const R = G(p, { o: 0 }); show(R, tr - .3);
phBlock(R, 860, 640, 640, 20, '#8a6f55', { top: 4, ol: 0 });
- const runner = phPerson(R, { role: 'friend', y: 640 }), back = forceArrow(R), fwd = forceArrow(R);
+ [900, 1020, 1140, 1260, 1380].forEach(x => mk('rect', { x, y: 664, width: 60, height: 6, fill: PH.line, opacity: .5 }, R)); // running-track lines
+ const runner = phActor(R, 'friend', { y: 640, anim: 'run' }), back = forceArrow(R), fwd = forceArrow(R);
phLabel(R, 'your foot pushes back; the ground pushes you forward', { x: 1180, y: 790, size: 28 });
clockRun(t => {
const ph = ((t - tt) % 1.6 + 1.6) % 1.6, air = Math.max(0, Math.sin(Math.PI * ph / 1.2)), sag = ph > 1.2 ? Math.sin(Math.PI * (ph - 1.2) / .4) : 0, onBed = ph > 1.2;
- bed.setAttribute('d', `M180 640Q400 ${640 + 70 * sag} 620 640`); put(jumper, { x: 400, y: 640 + 35 * sag - 260 * air });
+ bed.setAttribute('d', `M180 640Q400 ${640 + 70 * sag} 620 640`);
+ put(jumper, { y: 640 + 35 * sag - 260 * air }); jumper.show(onBed ? (sag > .5 ? 'crouch' : 'land') : air > .15 ? 'jump' : 'land');
down.set(470, 600 + 35 * sag, onBed && t > tt ? 90 : 0, 90, 'on the trampoline', 110, 40); up.set(330, 690, onBed && t > tt ? 90 : 0, -90, 'on you', -70, 40);
- const u = Math.max(0, t - tr), x = 920 + (100 * u) % 520, step = Math.floor(u * 2.5) % 2;
- put(runner, { x }); if (u > 0) walkTo(runner, x); back.set(x - 10, 660, u > 0 && step ? 80 : 0, 180, 'on the ground', -30, 50); fwd.set(x - 20, 520, u > 0 && step ? 80 : 0, 0, 'on you', 0, 0);
+ const u = Math.max(0, t - tr), d = 260 * u, x = 920 + d % 520;
+ put(runner, { x }); runner.stride('run', d); const push = runner._f % 2 === 0; // after stride: a function of t only
+ back.set(x - 10, 662, u > 0 && push ? 80 : 0, 180, 'on the ground', -30, 50); fwd.set(x - 20, 500, u > 0 && push ? 80 : 0, 0, 'on you', 0, 0);
}, D);
}],
['earth', 'Why Earth doesn’t move', (m, D) => {
const p = panel(0);
- const E = phEarthPolar(p, { x: 520, y: 1180, R: 620 });
- const you = phSprite(p, 'you', { x: 520, y: 560 }), dn = forceArrow(p), up = forceArrow(p);
- phLabel(p, '50 kg', { x: 600, y: 470, size: 28 });
+ phEarthPolar(p, { x: 520, y: 1180, R: 620 });
+ const you = phActor(p, 'you', { x: 520, y: 560, anim: 'stand' }), dn = forceArrow(p), up = forceArrow(p);
+ phLabel(p, '50 kg', { x: 620, y: 470, size: 28 });
const ta = m('mass'), tt = m('tiny');
const a1 = G(p, { o: 0 }); runs(a1, [['on you: ', COL.chalk], ['a = 500 N ÷ 50 kg = ', COL.chalk], ['10 m/s²', ACC]], { x: 1180, y: 300, size: 34, weight: 700, o: 1 }); show(a1, ta + .6);
const a2 = G(p, { o: 0 }); runs(a2, [['on Earth: ', COL.chalk], ['a = 500 N ÷ 6,000,000,000,000,000,000,000,000 kg', COL.chalk]], { x: 1050, y: 380, size: 26, weight: 700, o: 1 });
runs(a2, [['≈ ', COL.chalk], ['0.0000000000000000000001 m/s²', ACC]], { x: 1050, y: 425, size: 30, weight: 700, o: 1 }); show(a2, ta + 2);
txt(p, 'far too small to notice, but it is there', 1050, 500, tt + .4, { size: 30, fill: COL.dim });
+ // crouch, push off (the force pair), fly, land; repeat
clockRun(t => {
- const ph = ((t - .5) % 1.4 + 1.4) % 1.4, h = Math.max(0, Math.sin(Math.PI * ph / 1.1)), push = ph > 1.1 || ph < .12;
- put(you, { x: 520, y: 560 - 180 * h });
+ const ph = ((t - .5) % 1.8 + 1.8) % 1.8, h = ph > .5 && ph < 1.6 ? Math.sin(Math.PI * (ph - .5) / 1.1) : 0, push = ph > .3 && ph < .55;
+ put(you, { y: 560 - 180 * h }); you.show(ph < .3 ? 'crouch' : push ? 'land' : h > .1 ? 'jump' : 'land');
dn.set(560, 530, push ? 100 : 0, 90, 'you push Earth: 500 N', 150, 20); up.set(480, 660, push ? 100 : 0, -90, 'Earth pushes you: 500 N', -170, 40);
}, D);
}, { lead: false }],
@@ -479,8 +527,10 @@ const BEATS = [
const p = panel(0);
ground(p);
phBlock(p, 1000, 160, 60, GY - 160, '#7d6a5a', { top: 0, right: 12, ol: 0 });
- const y = phSprite(p, 'you_point', { x: 900, y: GY });
- forceArrow(p).set(940, GY - 110, 90, 0, 'you: 30 N', 0, -10); forceArrow(p).set(990, GY - 160, 90, 180, 'wall: ?', 0, -10);
+ [220, 320, 420].forEach(y => mk('rect', { x: 1000, y, width: 48, height: 4, fill: '#5e4f42' }, p));
+ const a = phActor(p, 'you', { y: GY, anim: 'push' }), f1 = forceArrow(p), f2 = forceArrow(p);
+ clockRun(t => { a.show('push', 0); put(a, { x: 930, y: GY + Math.sin(t * 9) }); const [hx, hy] = a.hand('F'); put(a, { x: 930 + (1000 - hx) });
+ f1.set(1000, GY - 100, 110, 0, 'you push the wall: 30 N', 120, 0); f2.set(990, GY - 190, 110, 180, 'the wall pushes you: ?', -60, -10); }, D);
}, { ask: done => quiz(Q_CARD, [
{ id: 'c-wall', prompt: ['You push on a wall with 30 newtons. How hard does the wall push back on you?'],
build: blanks([{ parts: ['the wall pushes back with ', { box: '30' }, ' newtons'], why: 'Action and reaction are equal in size and opposite in direction.', hint: 'Third law: equal and opposite.' }]) },
diff --git a/site/science/img/actor_friend.png b/site/science/img/actor_friend.png
new file mode 100644
index 0000000..6936286
Binary files /dev/null and b/site/science/img/actor_friend.png differ
diff --git a/site/science/img/actor_you.png b/site/science/img/actor_you.png
new file mode 100644
index 0000000..5430e68
Binary files /dev/null and b/site/science/img/actor_you.png differ
diff --git a/site/science/img/bicycle.png b/site/science/img/bicycle.png
new file mode 100644
index 0000000..1036557
Binary files /dev/null and b/site/science/img/bicycle.png differ
diff --git a/site/science/lib/actors.js b/site/science/lib/actors.js
new file mode 100644
index 0000000..0149142
--- /dev/null
+++ b/site/science/lib/actors.js
@@ -0,0 +1,3 @@
+// Generated by books/science/images/actors.py: actor sprite-sheet layout and hand positions.
+'use strict';
+const ACTORS = {"W":22,"H":19,"K":8,"X0":11,"anims":{"stand":{"row":0,"n":1,"hands":[{"B":[-0.83,-4.81],"F":[1.05,-4.86]}]},"walk":{"row":1,"n":6,"hands":[{"B":[-0.25,-4.83],"F":[0.75,-4.83]},{"B":[1.09,-4.96],"F":[-0.63,-4.74]},{"B":[1.09,-4.96],"F":[-0.63,-4.74]},{"B":[-0.25,-4.83],"F":[0.75,-4.83]},{"B":[-1.63,-4.74],"F":[2.09,-4.96]},{"B":[-1.63,-4.74],"F":[2.09,-4.96]}]},"run":{"row":2,"n":4,"hands":[{"B":[2.31,-7.33],"F":[3.31,-7.33]},{"B":[3.41,-8.2],"F":[0.78,-5.14]},{"B":[2.31,-7.33],"F":[3.31,-7.33]},{"B":[-0.22,-5.14],"F":[4.41,-8.2]}]},"push":{"row":3,"n":6,"hands":[{"B":[4.94,-7.37],"F":[6.11,-7.86]},{"B":[4.94,-7.19],"F":[6.11,-7.68]},{"B":[4.94,-7.19],"F":[6.11,-7.68]},{"B":[4.94,-7.37],"F":[6.11,-7.86]},{"B":[4.94,-7.19],"F":[6.11,-7.68]},{"B":[4.94,-7.19],"F":[6.11,-7.68]}]},"pull":{"row":4,"n":6,"hands":[{"B":[-1.64,-5.23],"F":[-1.12,-5.56]},{"B":[-1.64,-5.05],"F":[-1.12,-5.38]},{"B":[-1.64,-5.05],"F":[-1.12,-5.38]},{"B":[-1.64,-5.23],"F":[-1.12,-5.56]},{"B":[-1.64,-5.05],"F":[-1.12,-5.38]},{"B":[-1.64,-5.05],"F":[-1.12,-5.38]}]},"strain":{"row":5,"n":2,"hands":[{"B":[1.71,-7.14],"F":[2.77,-7.44]},{"B":[1.51,-7.04],"F":[2.56,-7.34]}]},"stumble":{"row":6,"n":4,"hands":[{"B":[5.01,-7.86],"F":[6.08,-8.3]},{"B":[5.01,-7.66],"F":[6.08,-8.09]},{"B":[5.01,-7.86],"F":[6.08,-8.3]},{"B":[5.01,-7.66],"F":[6.08,-8.09]}]},"backstep":{"row":7,"n":4,"hands":[{"B":[2.0,-7.86],"F":[3.05,-8.15]},{"B":[2.0,-7.69],"F":[3.05,-7.98]},{"B":[2.0,-7.86],"F":[3.05,-8.15]},{"B":[2.0,-7.69],"F":[3.05,-7.98]}]},"kick":{"row":8,"n":3,"hands":[{"B":[2.85,-6.3],"F":[-1.14,-5.29]},{"B":[-3.32,-5.57],"F":[2.48,-5.55]},{"B":[-5.1,-6.48],"F":[2.75,-6.46]}]},"throw":{"row":9,"n":3,"hands":[{"B":[1.51,-7.84],"F":[2.64,-8.02]},{"B":[4.79,-8.39],"F":[5.81,-8.83]},{"B":[2.16,-7.22],"F":[3.37,-7.77]}]},"skate":{"row":10,"n":2,"hands":[{"B":[-1.02,-4.4],"F":[5.54,-6.4]},{"B":[-4.0,-7.35],"F":[4.87,-7.74]}]},"crouch":{"row":11,"n":1,"hands":[{"B":[-1.0,-2.93],"F":[0.66,-2.68]}]},"jump":{"row":12,"n":1,"hands":[{"B":[3.0,-11.15],"F":[3.57,-11.74]}]},"land":{"row":13,"n":1,"hands":[{"B":[3.73,-5.63],"F":[5.04,-6.29]}]},"catch":{"row":14,"n":2,"hands":[{"B":[3.84,-8.46],"F":[4.88,-8.75]},{"B":[2.01,-7.96],"F":[3.08,-8.38]}]},"pedal":{"row":15,"n":8,"hands":[{"B":[3.46,-9.44],"F":[4.62,-9.78]},{"B":[3.46,-9.44],"F":[4.62,-9.78]},{"B":[3.46,-9.44],"F":[4.62,-9.78]},{"B":[3.46,-9.44],"F":[4.62,-9.78]},{"B":[3.46,-9.44],"F":[4.62,-9.78]},{"B":[3.46,-9.44],"F":[4.62,-9.78]},{"B":[3.46,-9.44],"F":[4.62,-9.78]},{"B":[3.46,-9.44],"F":[4.62,-9.78]}]}},"bike":{"wheels":[[-5,-1.5],[6.5,-1.5]],"r":3.1,"crank":[0.5,-3.5999999999999996]}};
diff --git a/site/science/lib/engine.js b/site/science/lib/engine.js
index 9021218..d9cf62c 100644
--- a/site/science/lib/engine.js
+++ b/site/science/lib/engine.js
@@ -1573,7 +1573,10 @@ function start(i, play) {
evalTo(0);
const a = P.audio = new Audio(`audio/${CHAPTER.language || 'en'}/${BEATS[i].id}.mp3`);
a.preload = 'auto';
- a.onerror = () => { if (P.audio === a) soundFailed(); };
+ // An aborted or superseded load (fast stepping, a reload) is not a failure: only a real error for the current clip
+ // shows the note, and the note goes away as soon as a clip plays.
+ a.onerror = () => { if (P.audio === a && a.error && a.error.code !== MediaError.MEDIA_ERR_ABORTED) soundFailed(); };
+ a.onplaying = () => { if (P.audio === a) $('soundNote').hidden = true; };
const b = BEATS[i];
if (b.ask) b.ask(() => { if (P.i === i) { evalTo(Infinity); start(i + 1, true); } });
setPlaying(play);
@@ -1604,7 +1607,7 @@ function frame(now) {
// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.
if (a && !P.afail && !a.ended) {
if (a.currentTime > 0) { P.t = Math.max(P.t, a.currentTime); P.stall = 0; }
- else if ((P.stall += dt) > 4) soundFailed();
+ else if ((P.stall += dt) > 8 && a.readyState < 2) soundFailed(); // still nothing to play after 8 s
} else P.t += dt;
evalTo(P.t);
if (P.t >= P.end) beatDone();
diff --git a/site/science/lib/physics.js b/site/science/lib/physics.js
index 9890a55..1e64f6e 100644
--- a/site/science/lib/physics.js
+++ b/site/science/lib/physics.js
@@ -66,3 +66,43 @@ function crate(p, { x = 0, y = 0, w = 150, h = 130, label = '' } = {}) {
if (label) phLabel(g, label, { y: -h / 2 + 10, size: 30 });
return g;
}
+
+/* ---------- pixel actors (lib/actors.js, img/actor_<role>.png) ---------- */
+// One sprite sheet per role; a frame is chosen by the viewBox of a nested <svg>, so swapping frames never refetches
+// an image. The origin is the feet centre; face(-1) mirrors. Stride tables keep feet from skating: STEP is the
+// distance (world px) the body moves per frame of each gait.
+const STEP = { walk: 11, run: 26, push: 10, pull: 10, stumble: 14, backstep: 13 };
+function phActor(p, role = 'you', { x = 0, y = 0, s = 1, o = 1, anim = 'stand' } = {}) {
+ const A = ACTORS, w = A.W * PX, h = A.H * PX, rows = Object.keys(A.anims).length, cols = Math.max(...Object.values(A.anims).map(a => a.n));
+ const g = G(p, { x, y, s, o }), inner = mk('g', {}, g);
+ const sv = mk('svg', { x: -A.X0 * PX, y: -h, width: w, height: h, viewBox: `0 0 ${w} ${h}` }, inner);
+ mk('image', { href: `../img/actor_${role}.png`, width: w * cols, height: h * rows, style: 'image-rendering: pixelated' }, sv);
+ return Object.assign(g, {
+ _dir: 1,
+ // Show frame k (wrapped) of an animation.
+ show(name, k = 0) {
+ const a = A.anims[name], f = ((Math.floor(k) % a.n) + a.n) % a.n, vb = `${f * w} ${a.row * h} ${w} ${h}`;
+ g._anim = name; g._f = f;
+ if (sv._vb !== vb) sv.setAttribute('viewBox', sv._vb = vb);
+ return g;
+ },
+ // A gait frame from the distance travelled, so the feet keep pace with the ground.
+ stride(name, dist) { return g.show(name, Math.abs(dist) / STEP[name]); },
+ face(dir) { if (dir !== g._dir) { g._dir = dir; if (dir < 0) inner.setAttribute('transform', 'scale(-1,1)'); else inner.removeAttribute('transform'); } return g; },
+ // World position of a hand ('F' front, 'B' back) in the current frame.
+ hand(which = 'F') { const st = g._st, [hx, hy] = A.anims[g._anim].hands[g._f][which]; return [st.x + hx * PX * st.s * g._dir, st.y + hy * PX * st.s]; },
+ }).show(anim);
+}
+// Our bicycle with spinning SVG wheels and a pedalling rider: bike.ride(dist) turns wheels and cranks.
+function phBike(p, role = 'you', o = {}) {
+ const g = G(p, o), B = ACTORS.bike, w = ACTORS.W * PX, h = ACTORS.H * PX;
+ const wheels = B.wheels.map(([wx, wy]) => {
+ const wg = G(g, { x: wx * PX + PX / 2, y: wy * PX }), r = B.r * PX;
+ mk('circle', { r, fill: 'none', stroke: '#2c3337', 'stroke-width': 7 }, wg);
+ path(wg, `M${-r} 0H${r}M0 ${-r}V${r}`, { stroke: '#9aa5a8', 'stroke-width': 3 });
+ return wg;
+ });
+ mk('image', { href: '../img/bicycle.png', x: -ACTORS.X0 * PX, y: -h, width: w, height: h, style: 'image-rendering: pixelated' }, g);
+ const rider = phActor(g, role, { anim: 'pedal' });
+ return Object.assign(g, { ride(dist) { const r = B.r * PX; wheels.forEach(wg => put(wg, { r: dist / r * 180 / Math.PI })); rider.show('pedal', dist / (2 * Math.PI * r * 1.5) * 8); return g; } });
+}