An AI skill that turns books into animated, narrated, interactive web experiences
JavaScript ai-agentsai-educationanimationclaudeclaude-codeclaude-skillsedtecheducationinteractive-textbookscormtext-to-video
style(science): polish ch09 figures and camera work
yzr73c07101a509
diff --git a/site/science/ch09/index.html b/site/science/ch09/index.htmlindex f4aaf5a..446bf3d 100644--- a/site/science/ch09/index.html+++ b/site/science/ch09/index.html@@ -17,11 +17,10 @@const CHAPTER = { number: 9, title: 'Motion · Part 1: Motion and Relative Motion', minutes: 4, language: 'en' };/* ---------- chapter helpers ---------- */-const V = 150; // 30 mph on screen, in px per second (same scale in every road beat)+const V = 150; // 30 mph on screen, in world px per second (same scale in every road beat)const MPH = v => Math.round(Math.abs(v) / V * 30);const speedStr = rel => { const n = MPH(rel); return n === 0 ? '0 mph' : rel > 0 ? `${n} mph →` : `← ${n} mph`; };const Q_CARD = { x: 96, y: 586, w: 1408, cls: 'band' }; // band card lifted above the usual BAND, for longer feedback-const smooth = q => q <= 0 ? 0 : q >= 1 ? 1 : q * q * (3 - 2 * q);const txt = (p, s, x, y, t0, { size = 32, fill = COL.chalk, anchor = 'middle', weight = 600 } = {}) => { const e = T(p, s, { x, y, size, fill, anchor, weight, o: 0 }); show(e, t0); return e; };// One line with coloured runs: [[text, colour?], …].function runs(p, parts, { x, y, size = 40, anchor = 'middle', weight = 600, o = 0 }) {@@ -29,56 +28,82 @@ function runs(p, parts, { x, y, size = 40, anchor = 'middle', weight = 600, o =parts.forEach(([s, c]) => { const sp = mk('tspan', { fill: c || COL.chalk }, t); sp.textContent = s; });return t;}-// Run a world clock through the whole beat: f(τ) with τ in seconds from the beat start.-const clockRun = (f, D) => { f(0); prog(q => f(q * D), 0, D, lin); };function arrowPath(x1, y, x2) { const d = Math.sign(x2 - x1); return `M${x1} ${y}H${x2}M${x2 - 16 * d} ${y - 11}L${x2} ${y}L${x2 - 16 * d} ${y + 11}`; }-// "Measured from: …" tag at the top left.-function fromTag(p, str, t0) {+// "Measured from: …" tag at the left, top by default (screen space).+function fromTag(p, str, t0, dur = .5, y = 136) {const g = G(p, { o: 0 });- phPin(g, { x: 112, y: 150, s: .8 });- runs(g, [['Measured from: ', COL.dim], [str, PH.ref]], { x: 146, y: 136, size: 34, anchor: 'start', o: 1 });- show(g, t0);+ phPin(g, { x: 112, y: y + 14, s: .8 });+ runs(g, [['Measured from: ', COL.dim], [str, PH.ref]], { x: 146, y, size: 34, anchor: 'start', o: 1 });+ show(g, t0, dur);return g;}+// Earth from above the North Pole, in a camera's world. The overview camera (800, 450, z 1) shows it at (EX, EY).+const EX = 430, EY = 450, ER = 200, ESPIN = -14; // spin in degrees per second; negative = counterclockwise+const rimAngle = t => (-90 + ESPIN * t) * Math.PI / 180; // where "you" are on the rim at time t+// A camera locked onto you, rolling with the ground, `lift` world px above it.+const groundView = (lift, z) => ({ x: t => EX + (ER + lift) * Math.cos(rimAngle(t)), y: t => EY + (ER + lift) * Math.sin(rimAngle(t)), z, r: t => ESPIN * t });+function earthWorld(cam, D) {+ const E = phEarthPolar(cam.world, { x: EX, y: EY, R: ER });+ const rim = deg => G(G(E._spin, { r: deg }), { y: -ER });+ phTree(rim(-15), { s: .19, kind: 1 }); phHouse(rim(-9), { s: .17 }); phTree(rim(7), { s: .19 }); phBush(rim(12), { s: .22 });+ const you = rim(0);+ phPerson(you, { s: .17 });+ clockRun(t => put(E._spin, { r: ESPIN * t }), D);+ return { E, you };+}+// Street for the split screen: truck and car loop round the wrapping street (one loop = ROAD.W, so the follow camera's+// jump at the loop is invisible). The truck enters the roadside view at t = 2.4 s.+// The roadside view needs no wrap, so its truck loops sooner (L = 1850) and is back on screen before the beat ends.+const loopX = (t, L = ROAD.W) => -200 + ((520 + V * (t - 2.4)) % L + L) % L;/* ---------- beats ---------- */const BEATS = [['intro', 'Motion', (m, D) => {- const g = G(scene, { o: 0, y: 24 });- T(g, 'Unit 3 · Motion, Forces, and Work · Chapter 9', { x: 800, y: 250, size: 28, fill: COL.dim, anchor: 'middle' });- T(g, 'Motion', { x: 800, y: 390, size: 120, weight: 600, anchor: 'middle' });+ const p = panel(0);+ const cam = camera(p, D, { x: 800, y: 285, z: .9 });+ const road = phRoad(cam);+ const carX = t => -260 + 190 * t;+ const car = phCar(road.vehicles, { y: ROAD.near });+ const jog = phPerson(road.side, { shirt: COL.nat, y: ROAD.side });+ cam.to({ x: t => carX(t) + 330 }, 2.4, 2); // lock on: from here the camera rides with the car+ clockRun(t => {+ const x = carX(t); put(car, { x }); phRoll(car, x);+ const jx = 380 + 95 * t; put(jog, { x: jx }); phPose(jog, 'run', jx / 22);+ }, D);+ const g = G(p, { o: 0, y: 24 });+ T(g, 'Unit 3 · Motion, Forces, and Work · Chapter 9', { x: 800, y: 130, size: 28, fill: COL.dim, anchor: 'middle' });+ phLabel(g, 'Motion', { x: 800, y: 250, size: 120, weight: 600 });tw(g, { o: 1, y: 0 }, .2, 1.1, out);- const sub = txt(scene, 'Part 1 · Motion and relative motion', 800, 480, m('sub'), { size: 36, fill: COL.dim });- const road = G(scene, { o: 0 });- mk('rect', { x: 0, y: 700, width: 1600, height: 90, fill: PH.road }, road);- path(road, 'M0 745H1600', { stroke: PH.line, 'stroke-width': 4, 'stroke-dasharray': '60 80', 'stroke-linecap': 'butt' });- const car = phCar(road, { y: 772, s: .8, x: -150 });- show(road, .4, .6);- clockRun(t => { const x = -150 + V * 1.2 * t; put(car, { x }); phRoll(car, (x + 150) / .8); }, D);- [g, sub, road].forEach(e => hide(e, D - .9, .8));+ txt(p, 'Part 1 · Motion and relative motion', 800, 320, m('sub'), { size: 36, fill: COL.dim });}],['position', 'Motion is a change of position', (m, D) => {const p = panel(0);+ const cam = camera(p, D, { x: 800, y: 420, z: .55 });+ cam.to({ x: 800, y: 450, z: 1 }, .1, 1.6); // wide shot of the park, pushing in to the track+ const w = cam.world;+ phHills(w, { y: 470, amp: 30, x0: -1400, x1: 3000, fill: PH.hill2, seed: 2 });+ phBlock(w, -1400, 520, 4400, 1200, PH.grass, { top: 6, ol: 0 });+ phTree(w, { x: -300, y: 540, s: 1.3 }); phTree(w, { x: -90, y: 548, kind: 1 }); phBush(w, { x: 40, y: 578 });+ phTree(w, { x: 1720, y: 540, s: 1.2, kind: 1 }); phTree(w, { x: 1960, y: 548, s: 1.3 }); phBush(w, { x: 1560, y: 572 });+ phPerson(w, { shirt: COL.whole, x: 80, y: 560, s: .9 });const X0 = 200, U = 120, TY = 600, R = 40, xm = v => X0 + U * v;- txt(p, 'Position: where something is, measured from a starting mark', 800, 150, .2, { size: 34, fill: COL.dim });- const track = G(p, { o: 0 });- mk('rect', { x: X0 - 30, y: TY, width: U * 10 + 60, height: 14, rx: 6, fill: '#6b5a48' }, track);+ const track = G(w);+ phBlock(track, X0 - 30, TY, U * 10 + 60, 16, '#8a6f55', { top: 4, ol: 2 });for (let v = 0; v <= 10; v++) {- path(track, `M${xm(v)} ${TY + 14}V${TY + 32}`, { stroke: COL.dim, 'stroke-width': 3 });- T(track, String(v), { x: xm(v), y: TY + 68, size: 28, fill: COL.dim, anchor: 'middle' });+ mk('rect', { x: xm(v) - 2, y: TY + 16, width: 4, height: 16, fill: COL.dim }, track);+ T(track, String(v), { x: xm(v), y: TY + 68, size: 28, fill: COL.chalk, anchor: 'middle' });}T(track, 'meters', { x: xm(10) + 40, y: TY + 68, size: 26, fill: COL.dim });T(track, 'zero mark', { x: xm(0), y: TY + 104, size: 24, fill: COL.dim, anchor: 'middle' });- show(track, .2);- const ghost = mk('circle', { cx: xm(2), cy: TY - R, r: R, fill: 'none', stroke: COL.dim, 'stroke-width': 3, 'stroke-dasharray': '8 8' }, p);+ txt(p, 'Position: where something is, measured from a starting mark', 800, 150, 1.4, { size: 34, fill: COL.dim });+ const ghost = mk('circle', { cx: xm(2), cy: TY - R, r: R, fill: 'none', stroke: COL.dim, 'stroke-width': 3, 'stroke-dasharray': '8 8' }, w);put(ghost, { o: 0 });- const ball = phBall(p, R, { x: xm(2), y: TY - R, o: 0 });- show(ball, .3);- const at2 = txt(p, 'position: 2 m', xm(2), TY - 110, m('pos'), { size: 32 });+ const ball = phBall(w, R, { x: xm(2), y: TY - R });+ const at2 = txt(w, 'position: 2 m', xm(2), TY - 110, m('pos'), { size: 32 });hide(at2, m('roll'), .4); show(ghost, m('roll'), .4);- prog(q => { const x = xm(2) + U * 5 * q; put(ball, { x, r: (x - xm(2)) / R * 180 / Math.PI }); }, m('roll', .1), 2.2, io);- txt(p, 'position: 7 m', xm(7), TY - 110, m('now'), { size: 32 });- const ch = G(p, { o: 0 });+ prog(q => { const x = xm(2) + U * 5 * q; put(ball, { x }); put(ball._spin, { r: (x - xm(2)) / R * 180 / Math.PI }); }, m('roll', .1), 2.2, io);+ txt(w, 'position: 7 m', xm(7), TY - 110, m('now'), { size: 32 });+ const ch = G(w, { o: 0 });path(ch, arrowPath(xm(2), TY + 150, xm(7)), { stroke: PH.change, 'stroke-width': 5 });T(ch, 'change of position: 5 m', { x: xm(4.5), y: TY + 200, size: 32, fill: PH.change, weight: 700, anchor: 'middle' });show(ch, m('change'));@@ -108,120 +133,110 @@ const BEATS = [const earth = phEarthSmall(f2, { r: 22 });T(f2, 'not to scale', { x: 990, y: 565, size: 20, fill: COL.dim, anchor: 'end' });const f3 = frame(1300, m('you'), 'you, walking');- path(f3, 'M1110 540H1490', { stroke: COL.dim, 'stroke-width': 4 });+ phBlock(f3, 1092, 540, 416, 38, PH.grass, { top: 5, ol: 0 });const you = phPerson(f3, { x: 1160, y: 540, s: .95 });const tp = m('page'), te = m('earth'), ty = m('you');clockRun(t => {const c = Math.max(0, t - tp - .3) % 2.4;- phFlip(book, 180 * smooth(c / 1.4));+ phFlip(book, 180 * soft(Math.min(1, c / 1.4)));const a = (-90 - 40 * Math.max(0, t - te + .5)) * Math.PI / 180; // counterclockwise, seen from above the North Poleput(earth, { x: 800 + 140 * Math.cos(a), y: 395 + 140 * Math.sin(a) });put(sun._rays, { r: 8 * t });const d = Math.min(260, Math.max(0, t - ty) * 85);- put(you, { x: 1160 + d }); phPose(you, d > 0 && d < 260 ? d / 40 : null);+ put(you, { x: 1160 + d }); phPose(you, d > 0 && d < 260 ? 'walk' : 'stand', d / 24);}, D);}],['roadside', 'Standing by the road', (m, D) => {const p = panel(0);- const road = phRoad(p);- for (const k of ['back', 'g', 'vehicles', 'front']) { put(road[k], { o: 0 }); show(road[k], .2, .6); }- const you = phPerson(road.back, { x: 760, y: ROAD.side, o: 0 });+ const cam = camera(p, D);+ cam.to({ x: 780, y: 470, z: 1.05 }, 0, D, lin); // fixed roadside shot with a slow push-in+ const road = phRoad(cam);+ const you = phPerson(road.side, { x: 760, y: ROAD.side, o: 0 });show(you, m('stand'));- const youL = phLabel(road.front, 'you', { x: 760, y: 272, fill: PH.you, o: 0 });- show(youL, m('stand'));+ show(phLabel(road.front, 'you', { x: 760, y: 272, fill: PH.you, o: 0 }), m('stand'));const truck = phTruck(road.vehicles, { y: ROAD.far, s: .85 });- const tl = phLabel(road.front, '', { y: 385, fill: PH.truck, o: 0 });- const tag = fromTag(p, 'you, by the road', m('speed'));- const pin = phPin(road.front, { x: 760, y: 236, s: 0, o: 0 });- pop(pin, m('speed'));+ const tl = phLabel(road.front, `truck: ${speedStr(V)}`, { y: 380, fill: PH.truck, o: 0 });+ fromTag(p, 'you, by the road', m('speed'));+ pop(phPin(road.front, { x: 760, y: 236, s: 0, o: 0 }), m('speed'));show(tl, m('speed'));const t0 = m('truck', -.3);clockRun(t => {- const x = -140 + V * Math.max(0, t - t0);- put(truck, { x }); phRoll(truck, (x + 140) / .85);- put(tl, { x: x + 10 }); tl.textContent = `truck: ${speedStr(V)}`;+ const x = -250 + V * Math.max(0, t - t0);+ put(truck, { x }); phRoll(truck, x / .85); put(tl, { x: x + 10 });}, D);}],['incar', 'Riding beside the truck', (m, D) => {const p = panel(0);- const road = phRoad(p);- const FR = 1100, tc = m('car', .3), r0 = m('ride'), RAMP = 1.6;- const friend = phPerson(road.back, { shirt: PH.friend, x: FR, y: ROAD.side });+ const cam = camera(p, D);+ const road = phRoad(cam);+ const FR = 1100, tc = m('car', .3), r0 = m('ride');+ const xw = t => -330 + V * Math.max(0, t - tc); // world position of the truck and the car (side by side)+ // Hard cut at "ride": the camera now rides in the car (locked on it, a little closer, with a light road shake).+ cam.cut({ x: t => xw(t) + 10, y: 600, z: 1.15 }, r0).shake(1.6, r0, D - r0, 6);+ phPerson(road.side, { shirt: PH.friend, x: FR, y: ROAD.side });const truck = phTruck(road.vehicles, { y: ROAD.far, s: .85 });const car = phCar(road.vehicles, { y: ROAD.near });- const fl = phLabel(road.front, '', { y: 272, fill: PH.friend, o: 0 });- const tl = phLabel(road.front, '', { y: 385, fill: PH.truck, o: 0 });+ const fl = phLabel(road.front, '', { x: FR, y: 272, fill: PH.friend, o: 0 });+ const tl = phLabel(road.front, '', { y: 380, fill: PH.truck, o: 0 });const cl = phLabel(road.front, '', { y: ROAD.bot + 56, fill: PH.car, o: 0 });- const sl = phLabel(road.front, 'road and trees: ← 30 mph', { x: 70, y: 868, size: 30, fill: COL.dim, anchor: 'start', o: 0 });- const pin = phPin(road.front);+ const sl = phLabel(p, 'road and trees: ← 30 mph', { x: 70, y: 862, size: 30, fill: COL.dim, anchor: 'start', o: 0 });show(fl, .4); show(tl, tc + 1.6); show(cl, tc + 1.6); show(sl, m('back'));- const tagA = fromTag(p, 'the roadside', .3), tagB = fromTag(p, 'your car', r0);- hide(tagA, r0, .4);+ const pinA = phPin(road.side, { x: FR, y: 236, o: 0 }), pinB = phPin(car, { x: 30, y: -112, s: 0, o: 0 });+ show(pinA, .3); hide(pinA, r0, 0); pop(pinB, r0 + .3);+ const tagA = fromTag(p, 'the roadside', .3);+ hide(tagA, r0, 0); fromTag(p, 'your car', r0, 0, 800); // bottom: the friend slides through the topclockRun(t => {- const xw = -330 + V * Math.max(0, t - tc); // world position of the truck and the car (side by side)- const u = t - r0, camV = u <= 0 ? 0 : u < RAMP ? V * u / RAMP : V;- const cam = u <= 0 ? 0 : u < RAMP ? V * u * u / (2 * RAMP) : V * RAMP / 2 + V * (u - RAMP);- road.update(cam);- const sx = road.sx(xw);- put(truck, { x: sx }); put(car, { x: sx + 10 });- phRoll(truck, (xw + 330) / .85); phRoll(car, xw + 330);- put(friend, { x: road.sx(FR) });- put(fl, { x: road.sx(FR) }); fl.textContent = `friend: ${speedStr(-camV)}`;- put(tl, { x: sx + 10 }); tl.textContent = `truck: ${speedStr(t < tc ? 0 : V - camV)}`;- put(cl, { x: sx + 10 }); cl.textContent = `your car: ${speedStr(t < tc ? 0 : V - camV)}`;- const k = smooth(u / 1.1), ax = road.sx(FR), ay = 236, bx = sx + 30, by = ROAD.near - 112;- put(pin, { x: ax + (bx - ax) * k, y: ay + (by - ay) * k - Math.sin(k * Math.PI) * 120, o: t < .3 ? 0 : Math.min(1, (t - .3) / .5) });+ const x = xw(t), camV = t < r0 ? 0 : V, rel = t < tc ? 0 : V - camV;+ put(truck, { x }); put(car, { x: x + 10 });+ phRoll(truck, (x + 330) / .85); phRoll(car, x + 330);+ fl.textContent = `friend: ${speedStr(-camV)}`;+ put(tl, { x: x + 10 }); tl.textContent = `truck: ${speedStr(rel)}`;+ put(cl, { x: x + 10 }); cl.textContent = `your car: ${speedStr(rel)}`;}, D);}],['rule', 'Motion is relative', (m, D) => {const p = panel(0);- const mini = (fx, t0, title, build) => {- const g = G(p, { o: 0 });- const id = 'mini' + fx, cp = mk('clipPath', { id }, mk('defs', {}, g));- mk('rect', { x: fx, y: 150, width: 640, height: 320, rx: 20 }, cp);- const clip = G(g); clip.setAttribute('clip-path', `url(#${id})`);- const w = G(clip, { x: fx, y: 150 - .5 * 250, s: .5 });- mk('rect', { x: 0, y: 0, width: 1280, height: 900, fill: '#22332d' }, w);- build(w, g);- mk('rect', { x: fx, y: 150, width: 640, height: 320, rx: 20, fill: 'none', stroke: COL.faint, 'stroke-width': 2 }, g);- phLabel(g, title, { x: fx + 24, y: 196, size: 28, fill: PH.ref, anchor: 'start' });- show(g, t0);- return g;+ // Split screen: the same street through two cameras at once, one standing by the road, one riding in the car.+ const view = (box, t0, title, ride) => {+ const cam = camera(p, D, { box, x: 1200, y: 560, z: .5 });+ const L = ride ? ROAD.W : 1850;+ if (ride) cam.cut({ x: t => loopX(t) + 10 }, 0);+ const r = phRoad(cam, { hills: false });+ phPerson(r.scenery, { shirt: PH.friend, x: 850, y: ROAD.side });+ if (!ride) phPin(r.scenery, { x: 850, y: 296, s: 1.1 });+ const truck = phTruck(r.vehicles, { y: ROAD.far, s: .85 }), car = phCar(r.vehicles, { y: ROAD.near });+ if (ride) phPin(car, { x: 30, y: -112, s: 1.4 });+ const lab = phLabel(r.front, ride ? 'truck: 0 mph' : 'truck: 30 mph →', { y: 380, size: 56, fill: PH.truck });+ clockRun(t => {+ const x = loopX(t, L), dist = 520 + V * (t - 2.4);+ put(truck, { x }); put(car, { x: x + 10 }); put(lab, { x: x + 10 });+ phRoll(truck, dist / .85); phRoll(car, dist);+ }, D);+ put(cam.el, { o: 0 }); show(cam.el, t0);+ show(phLabel(p, title, { x: box[0] + 24, y: box[1] + box[3] - 22, size: 28, fill: PH.ref, anchor: 'start', o: 0 }), t0);};- const streaks = (w, x, y) => path(w, [0, 34, 68].map(d => `M${x - 40} ${y - d}H${x - 130}`).join(''), { stroke: COL.chalk, 'stroke-width': 6, opacity: .6 });- mini(120, m('side', -.4), 'from the roadside', (w, g) => {- const r = phRoad(w, { trees: [80, 1100], lamps: [] });- phPerson(r.back, { x: 330, y: ROAD.side });- phPin(r.front, { x: 330, y: 250, s: 1.4 });- const tr = phTruck(r.vehicles, { x: 860, y: ROAD.far, s: .85 }); streaks(r.vehicles, 860 - 200, ROAD.far - 60);- phLabel(g, 'truck: 30 mph →', { x: 120 + 430, y: 218, size: 28, fill: PH.truck });- });- mini(840, m('car', -.4), 'from the car', (w, g) => {- const r = phRoad(w, { trees: [180, 760, 1200], lamps: [] });- [180, 760, 1200].forEach(x => streaks(r.back, x + 170, ROAD.side - 90));- phTruck(r.vehicles, { x: 640, y: ROAD.far, s: .85 }); phCar(r.vehicles, { x: 650, y: ROAD.near });- phPin(r.front, { x: 680, y: ROAD.near - 112, s: 1.2 });- phLabel(g, 'truck: 0 mph', { x: 840 + 320, y: 218, size: 28, fill: PH.truck });- });- const check = x => { const c = path(p, `M${x} 430l18 18l36 -42`, { stroke: COL.good, 'stroke-width': 9 }, { d: 0 }); draw(c, m('both'), .5); };- check(700); check(1420);- txt(p, 'Both are correct.', 800, 530, m('both'), { size: 34, fill: COL.good });- show(runs(p, [['Motion is ', COL.chalk], ['relative', PH.ref], ['.', COL.chalk]], { x: 800, y: 640, size: 72 }), m('rel'));- show(runs(p, [['It is always described in relation to a ', COL.chalk], ['reference point', PH.ref], ['.', COL.chalk]], { x: 800, y: 730, size: 40, weight: 500 }), m('refpt'));- txt(p, 'reference point = the place or object you measure from', 800, 800, m('refpt', 3), { size: 30, fill: COL.dim, weight: 500 });+ view([80, 120, 700, 380], m('side', -.4), 'from the roadside', false);+ view([820, 120, 700, 380], m('car', -.4), 'from the car', true);+ const check = x => { const c = path(p, `M${x} 455l18 18l36 -42`, { stroke: COL.good, 'stroke-width': 9 }, { d: 0 }); draw(c, m('both'), .5); };+ check(700); check(1440);+ txt(p, 'Both are correct.', 800, 560, m('both'), { size: 34, fill: COL.good });+ show(runs(p, [['Motion is ', COL.chalk], ['relative', PH.ref], ['.', COL.chalk]], { x: 800, y: 660, size: 72 }), m('rel'));+ show(runs(p, [['It is always described in relation to a ', COL.chalk], ['reference point', PH.ref], ['.', COL.chalk]], { x: 800, y: 745, size: 40, weight: 500 }), m('refpt'));+ txt(p, 'reference point = the place or object you measure from', 800, 808, m('refpt', 3), { size: 30, fill: COL.dim, weight: 500 });}],['earth', 'Why we don\'t feel Earth spin', (m, D) => {const p = panel(0);- const EX = 430, EY = 450, ER = 200;- txt(p, 'Seen from far above the North Pole', 70, 80, .2, { size: 32, fill: COL.dim, anchor: 'start' });- const E = phEarthPolar(p, { x: EX, y: EY, R: ER });- put(E, { o: 0 }); show(E, .2);- const rim = G(E._spin);- const onRim = G(rim, { y: -ER });- phHouse(onRim, { x: -50, s: .38 }); phTree(onRim, { x: 46, s: .42 }); phPerson(onRim, { x: 4, s: .38 });- const rimRing = mk('circle', { cx: 4, cy: -34, r: 42, fill: 'none', stroke: PH.ref, 'stroke-width': 4 }, onRim);+ const sky = mk('rect', { width: 1600, height: 900, fill: PH.sky }, p);+ const cam = camera(p, D);+ // Open on the ground, rolling with it (nothing moves), then pull back into space: the spin appears.+ cam.cut(groundView(26, 6), 0).to({ x: 800, y: 450, z: 1, r: 0 }, m('earth'), 3.4);+ const stars = cam.layer(.15);+ for (let i = 0; i < 110; i++) { const k = 2 + 3 * phHash(i, 5); mk('rect', { x: -1400 + 4400 * phHash(i, 1), y: -1000 + 2900 * phHash(i, 2), width: k, height: k, fill: COL.chalk, opacity: .3 + .5 * phHash(i, 3) }, stars); }+ const { you } = earthWorld(cam, D);+ clockRun(t => { const k = Math.min(1, Math.max(0, (cam.at(t).z - 1.6) / 2)); put(sky, { o: k }); put(stars, { o: 1 - k }); }, D);+ const rimRing = mk('rect', { x: -16, y: -40, width: 32, height: 44, rx: 6, fill: 'none', stroke: PH.ref, 'stroke-width': 2.5 }, you);put(rimRing, { o: 0 });- const arcs = G(p, { o: 0 });+ const arcs = G(cam.world, { o: 0 });const arc = (a1, a2) => {const rr = ER + 92, P = a => [EX + rr * Math.cos(a * Math.PI / 180), EY + rr * Math.sin(a * Math.PI / 180)];const [x1, y1] = P(a1), [x2, y2] = P(a2), t = a2 * Math.PI / 180, dx = Math.sin(t), dy = -Math.cos(t);@@ -229,52 +244,49 @@ const BEATS = [path(arcs, `M${x2 - 18 * dx - 10 * dy} ${y2 - 18 * dy + 10 * dx}L${x2} ${y2}L${x2 - 18 * dx + 10 * dy} ${y2 - 18 * dy - 10 * dx}`, { stroke: COL.chalk, 'stroke-width': 5 });};arc(215, 145); arc(35, -35);- show(arcs, m('earth'));- txt(p, 'counterclockwise', EX, 772, m('earth', 1.2), { size: 30, fill: COL.chalk });- const eq = mk('circle', { cx: EX, cy: EY, r: ER, fill: 'none', stroke: COL.int, 'stroke-width': 7, transform: `rotate(-90 ${EX} ${EY})` }, p);+ show(arcs, m('earth', 2.8));+ txt(p, 'Seen from far above the North Pole', 70, 80, m('earth'), { size: 32, fill: COL.dim, anchor: 'start' });+ txt(p, 'counterclockwise', EX, 772, m('earth', 3.2), { size: 30, fill: COL.chalk });+ const eq = mk('circle', { cx: EX, cy: EY, r: ER, fill: 'none', stroke: COL.int, 'stroke-width': 7, transform: `rotate(-90 ${EX} ${EY})` }, cam.world);eq.setAttribute('pathLength', 1); eq.setAttribute('stroke-dasharray', '1 1'); put(eq, { d: 0 }); draw(eq, m('fast'), 1.2);runs(p, [['equator (the rim): ', COL.int], ['more than 1,000 mph', COL.chalk]], { x: EX, y: 818, size: 30, o: 0 });show(p.lastChild, m('fast', .4));- T(p, 'people and buildings drawn much larger', { x: EX, y: 860, size: 22, fill: COL.dim, anchor: 'middle' });+ txt(p, 'people and buildings drawn much larger', EX, 860, m('earth', 3.4), { size: 22, fill: COL.dim, weight: 500 });const rx = 1180;- const q1 = txt(p, 'Why can\'t we feel it?', rx, 200, m('why'), { size: 42 });- const q2 = txt(p, 'Everything near you spins along with Earth.', rx, 260, m('along'), { size: 28, fill: COL.dim, weight: 500 });+ txt(p, 'Why can\'t we feel it?', rx, 200, m('why'), { size: 42 });+ txt(p, 'Everything near you spins along with Earth.', rx, 260, m('along'), { size: 28, fill: COL.dim, weight: 500 });show(rimRing, m('along', 1));- const zoom = G(p, { o: 0 });- const id = 'groundclip', cp = mk('clipPath', { id }, mk('defs', {}, zoom));- mk('rect', { x: 880, y: 310, width: 600, height: 380, rx: 20 }, cp);- const zc = G(zoom); zc.setAttribute('clip-path', `url(#${id})`);- mk('rect', { x: 880, y: 310, width: 600, height: 380, fill: '#2a4a5c' }, zc);- mk('rect', { x: 880, y: 600, width: 600, height: 90, fill: '#3d5c40' }, zc);- phHouse(zc, { x: 1000, y: 610, s: 1.05 }); phTree(zc, { x: 1380, y: 610, s: 1.1 }); phPerson(zc, { x: 1210, y: 620, s: .95 });- phPin(zc, { x: 1214, y: 425, s: .9 });- mk('rect', { x: 880, y: 310, width: 600, height: 380, rx: 20, fill: 'none', stroke: COL.faint, 'stroke-width': 2 }, zoom);- phLabel(zoom, 'from where you stand', { x: 904, y: 352, size: 28, fill: PH.ref, anchor: 'start' });- T(zoom, 'house, tree, ground: 0 mph', { x: rx, y: 740, size: 32, weight: 600, anchor: 'middle' });- T(zoom, 'Nothing seems to move.', { x: rx, y: 790, size: 32, weight: 600, fill: COL.good, anchor: 'middle' });- show(zoom, m('zoom'), .7);- clockRun(t => put(E._spin, { r: -14 * t }), D); // negative = counterclockwise on screen-}],+ // Picture-in-picture: a second camera that stays with you on the ground.+ const box = [880, 300, 600, 380];+ const pip = camera(p, D, { box, bg: PH.sky });+ pip.cut(groundView(20, 6), 0);+ phPin(earthWorld(pip, D).you, { y: -34, s: .3 });+ put(pip.el, { s: 0, o: 0 }); pop(pip.el, m('zoom'));+ show(phLabel(p, 'from where you stand', { x: box[0] + 24, y: box[1] + box[3] - 22, size: 28, fill: PH.ref, anchor: 'start', o: 0 }), m('zoom', .3));+ txt(p, 'house, tree, ground: 0 mph', rx, 740, m('zoom', .3), { size: 32 });+ txt(p, 'Nothing seems to move.', rx, 790, m('zoom', .3), { size: 32, fill: COL.good });+}, { lead: false }], // the ground shot is on screen from the first frame['q1', 'Quick check', (m, D) => {const p = panel(0);const GY = 430;- path(p, `M0 ${GY + 4}H1600`, { stroke: '#6b5a48', 'stroke-width': 8 });+ phBlock(p, 0, GY + 2, 1600, 10, '#6b5a48', { top: 3, ol: 0 });+ path(p, Array.from({ length: 27 }, (_, i) => `M${i * 60 + 10} ${GY + 12}h34`).join(''), { stroke: '#4a3d31', 'stroke-width': 6 });+ path(p, [0, 40, 80].map(k => `M${720 - 520} ${GY - 120 - k}H${720 - 620}`).join(''), { stroke: COL.chalk, 'stroke-width': 6, opacity: .6 });const train = phCarriage(p, { x: 720, y: GY, w: 1000 });put(train, { o: 0 }); show(train, .2);- const wx = 720 + train._wins[2];- const win = G(train);- phWindow(win, `M${train._wins[2] - 62} -232h124v96h-124Z`, { shirt: COL.int, hair: '#2d1e16', x: train._wins[2] - 22, y: -136, s: .85 });- mk('rect', { x: train._wins[2] - 4, y: -152, width: 66, height: 9, fill: '#8a6a4c' }, win);- path(win, `M${train._wins[2] + 30} -152v-26q0 -6 4 -9v-6h8v6q4 3 4 9v26Z`, { fill: '#86c9e8', ...phLine(2) });+ const wl = train._wins[2], wx = 720 + wl;+ phCarWin(train, 2, { shirt: COL.int, hair: '#2d1e16', x: wl - 24, y: -96, s: .6 });+ phBlock(train, wl - 4, -152, 66, 9, '#8a6a4c', { top: 3, ol: 2 });+ phBlock(train, wl + 32, -186, 14, 34, '#86c9e8', { top: 0, right: 4, ol: 2 });+ phBlock(train, wl + 34, -194, 10, 8, COL.whole, { top: 0, ol: 2 });phLabel(p, 'Maya', { x: wx - 22, y: GY - 98, size: 28, fill: COL.int });phLabel(p, 'water bottle', { x: wx + 120, y: GY - 300, size: 24, fill: '#86c9e8' });- path(p, `M${wx + 80} ${GY - 292}L${wx + 38} ${GY - 196}`, { stroke: '#86c9e8', 'stroke-width': 3 });- path(p, [0, 40, 80].map(k => `M${720 - 520} ${GY - 120 - k}H${720 - 620}`).join(''), { stroke: COL.chalk, 'stroke-width': 6, opacity: .6 });+ path(p, `M${wx + 80} ${GY - 292}L${wx + 42} ${GY - 196}`, { stroke: '#86c9e8', 'stroke-width': 3 });phLabel(p, 'train: 60 mph →', { x: 520, y: GY - 300, size: 30, fill: COL.real });- mk('rect', { x: 1260, y: GY + 8, width: 340, height: 110, fill: '#4a524f' }, p);- path(p, `M1260 ${GY + 10}H1600`, { stroke: COL.int, 'stroke-width': 5 });- phPerson(p, { shirt: COL.nat, x: 1430, y: GY + 80 });- phLabel(p, 'Leo, on the platform', { x: 1430, y: GY - 128, size: 26, fill: COL.nat });+ phBlock(p, 1260, GY + 8, 340, 110, '#4a524f', { top: 6, ol: 2 });+ mk('rect', { x: 1260, y: GY + 8, width: 340, height: 6, fill: COL.int }, p);+ phPerson(p, { shirt: COL.nat, x: 1430, y: GY + 18 });+ phLabel(p, 'Leo, on the platform', { x: 1430, y: GY - 172, size: 26, fill: COL.nat });}, { ask: done => quiz(Q_CARD, [{ id: 'c-maya', prompt: ['Measured from ', h('b', '', 'Maya'), ', how fast is the water bottle on her table moving?'],build: choice([['0 mph'], ['60 mph'], ['120 mph']], 0,@@ -294,7 +306,7 @@ const BEATS = [['Motion is relative: it is described from a reference point.', 'w2'],['Different reference points can give different answers, and each can be right.', 'w3']].forEach(([s, mark], i) => {const g = G(p, { x: -16, y: 300 + i * 140, o: 0 });- mk('circle', { cx: 170, cy: -12, r: 26, fill: PH.ref }, g);+ mk('rect', { x: 144, y: -38, width: 52, height: 52, rx: 6, fill: PH.ref }, g);T(g, String(i + 1), { x: 170, y: -1, size: 30, fill: COL.board, weight: 700, anchor: 'middle' });T(g, s, { x: 230, size: 36, weight: 600 });tw(g, { o: 1, x: 0 }, m(mark), .6);diff --git a/site/science/lib/figures.js b/site/science/lib/figures.jsindex 42e93e9..9926cb7 100644--- a/site/science/lib/figures.js+++ b/site/science/lib/figures.js@@ -1,205 +1,343 @@-// Papermorph science book: illustrated scene components (SVG, drawn in code).-// Load after engine.js. Every figure is drawn around its own origin with the ground at y = 0, facing right (+x),-// so it can be placed with put(e, {x, y, s}) and animated with the engine's tweens.+// Papermorph science book: illustrated scene set, drawn in code (SVG) in a blocky, voxel-like style. Load after engine.js.+// Style: everything is built from blocks (rects and straight-edged polygons) with one outline colour (PH.out, OL px at+// scale 1; 1.5 px for small details) and three tones per block: the face, a lighter top band and a darker side or+// bottom band (light from the upper left). Things whose physics depends on being round stay round: wheels, balls,+// Earth's rim, clock faces. A soft ground shadow sits under anything that stands or rolls.+// Every figure is drawn around its own origin with the ground at y = 0, facing right (+x); place it with+// put(e, {x, y, s}) and animate it with the engine's tweens.'use strict';const PH = {- out: '#0e1714', skin: '#e9c49f', hair: '#4b3427', pants: '#5b6d90', shoe: '#ece8dc',- road: '#38433f', verge: '#25382f', line: '#ece8dc', trunk: '#8a6a4c', leaf: '#5f9e6c', leaf2: '#4f8a5d',- lamp: '#9aaba3', glass: '#cfe8f3', trailer: '#d8dedb', tire: '#202825', hub: '#b9c5c1',+ out: '#14201c', skin: '#efc6a0', hair: '#4b3427', pants: '#5b6d90', shoe: '#2f3a44',+ road: '#3a4541', walk: '#55625b', grass: '#2d4636', line: '#ece8dc', hill1: '#22352d', hill2: '#283f34',+ trunk: '#8a6a4c', leaf: '#5f9e6c', lamp: '#9aaba3', glass: '#bfe0ef', trailer: '#dfe4e1', tire: '#20282a', rim: '#c3cecb',+ light: '#f6d27a', tail: '#f08c7a', metal: '#7d8a86', sky: '#4f7f9c', ocean: '#3f7fb0',you: COL.real, friend: COL.nat, truck: COL.whole, car: COL.irr, ref: COL.task, change: COL.rat,};-const phLine = (w = 2.5) => ({ stroke: PH.out, 'stroke-width': w, 'stroke-linejoin': 'round' });+const OL = 2.5;+const dk = (c, k = .22) => mix(c, '#0e1714', k), lt = (c, k = .3) => mix(c, '#ffffff', k);+const phLine = (w = OL) => ({ stroke: PH.out, 'stroke-width': w, 'stroke-linejoin': 'round' });+const phHash = (i, j) => { const v = Math.sin(i * 127.1 + j * 311.7) * 43758.5453; return v - Math.floor(v); };+let phN = 0;++/* ---------- building blocks ---------- */+// A shaded block: face, lighter top band, optional darker right/bottom bands, outline.+function phBlock(p, x, y, w, h, c, { top = 4, right = 0, bottom = 0, ol = OL } = {}) {+ mk('rect', { x, y, width: w, height: h, fill: c }, p);+ if (top) mk('rect', { x, y, width: w, height: Math.min(top, h / 2), fill: lt(c) }, p);+ if (right) mk('rect', { x: x + w - right, y, width: right, height: h, fill: dk(c) }, p);+ if (bottom) mk('rect', { x, y: y + h - bottom, width: w, height: bottom, fill: dk(c) }, p);+ if (ol) mk('rect', { x, y, width: w, height: h, fill: 'none', ...phLine(ol) }, p);+}+const phPts = pts => 'M' + pts.map(q => q.join(' ')).join('L') + 'Z';+const phPoly = (p, pts, fill, ol = OL) => path(p, phPts(pts), { fill, ...(ol ? phLine(ol) : { stroke: 'none' }) });+const phShadow = (p, rx, cx = 0) => mk('ellipse', { cx, rx, ry: Math.min(10, Math.max(3, rx * .07)), fill: '#000', opacity: .22 }, p);+// Rects merged into one silhouette with a single outline; shade(g) draws extra tones clipped to the silhouette.+function phMerge(p, rects, fill, shade) {+ const g = G(p), id = 'phm' + (++phN), cp = mk('clipPath', { id }, mk('defs', {}, g));+ rects.forEach(([x, y, width, height]) => mk('rect', { x, y, width, height, fill: PH.out, stroke: PH.out, 'stroke-width': 2 * OL, 'stroke-linejoin': 'round' }, g));+ rects.forEach(([x, y, width, height]) => { mk('rect', { x, y, width, height, fill }, g); mk('rect', { x, y, width, height }, cp); });+ if (shade) { const c = G(g); c.setAttribute('clip-path', `url(#${id})`); shade(c); }+ return g;+}+// Pixel texture: a few lighter and darker squares on a grid over [x0, x1] × [y0, y1].+function phPixels(p, x0, y0, x1, y1, c, cell = 13, seed = 1) {+ for (let i = 0, x = x0; x < x1; i++, x += cell) for (let j = 0, y = y0; y < y1; j++, y += cell) {+ const h = phHash(i + seed * 17, j + seed * 31);+ if (h > .84) mk('rect', { x, y, width: cell, height: cell, fill: lt(c, .16) }, p);+ else if (h < .12) mk('rect', { x, y, width: cell, height: cell, fill: dk(c, .2) }, p);+ }+}// Text with a dark backing stroke, so it stays legible when something passes behind it.function phLabel(p, str, { x = 0, y = 0, size = 30, fill = COL.chalk, anchor = 'middle', weight = 700, o = 1 } = {}) {const t = T(p, str, { x, y, size, fill, anchor, weight, o });- t.setAttribute('stroke', COL.board); t.setAttribute('stroke-width', 8); t.setAttribute('paint-order', 'stroke');+ t.setAttribute('stroke', COL.board); t.setAttribute('stroke-width', size / 4); t.setAttribute('paint-order', 'stroke');t.setAttribute('stroke-linejoin', 'round');return t;}-// Reference-point pin: the tip touches (0, 0).+// Reference-point pin (an on-screen marker, not part of the world's look): the tip touches (0, 0).function phPin(p, o = {}) {const g = G(p, o);- path(g, 'M0 0C-5 -12 -19 -22 -19 -37A19 19 0 1 1 19 -37C19 -22 5 -12 0 0Z', { fill: PH.ref, ...phLine(2.5) });+ path(g, 'M0 0C-5 -12 -19 -22 -19 -37A19 19 0 1 1 19 -37C19 -22 5 -12 0 0Z', { fill: PH.ref, ...phLine() });+ path(g, 'M-11 -44A12 12 0 0 1 -2 -53', { stroke: lt(PH.ref, .5), 'stroke-width': 3.5 });mk('circle', { cx: 0, cy: -37, r: 7, fill: COL.board }, g);return g;}/* ---------- people ---------- */-// A person about 180 px tall. Limbs are groups that rotate about the hip or shoulder: phPose(person, phase).-function phPerson(p, { shirt = PH.you, pants = PH.pants, hair = PH.hair, x = 0, y = 0, s = 1, o = 1 } = {}) {+// A blocky person, 172 px tall: legs swing from the hip, arms from the shoulder. Poses: stand, walk, run, sit+// (phPose(person, pose, phase); phase in radians, one stride per 2π).+const PH_LEG = 68;+function phPerson(p, { shirt = PH.you, pants = PH.pants, hair = PH.hair, skin = PH.skin, x = 0, y = 0, s = 1, o = 1, pose = 'stand', phase = 0, shadow = true } = {}) {const g = G(p, { x, y, s, o });- const limb = (px, py, parts, shade) => {- const L = G(g, { x: px, y: py });- parts.forEach(([d, col, w]) => path(L, d, { stroke: shade ? mix(col, '#000000', .25) : col, 'stroke-width': w }));+ if (shadow) phShadow(g, 24);+ const b = G(g), topB = G(b, { y: -PH_LEG });+ const arm = (parent, k) => {+ const L = G(parent, { y: -58 });+ phBlock(L, -6.5, -4, 13, 28, dk(shirt, k), { top: 0, right: 3, ol: 0 });+ phBlock(L, -6.5, 24, 13, 32, dk(skin, k), { top: 0, right: 3, ol: 0 });+ mk('rect', { x: -6.5, y: -4, width: 13, height: 60, fill: 'none', ...phLine() }, L);+ return L;+ };+ const leg = k => {+ const L = G(b, { y: -PH_LEG });+ phBlock(L, -7.5, 0, 15, 58, dk(pants, k), { top: 0, right: 3 });+ phBlock(L, -7.5, 56, 19, 12, dk(PH.shoe, k), { top: 2.5 });return L;};- const leg = shade => limb(0, -84, [['M0 0V76', pants, 15], ['M-3 80H13', PH.shoe, 9]], shade);- const arm = shade => limb(0, -130, [['M0 0V24', shirt, 12], ['M0 24V46', PH.skin, 9]], shade);- const legB = leg(true), armB = arm(true);- path(g, 'M-17 -136Q0 -143 17 -136L19 -82Q0 -76 -19 -82Z', { fill: shirt, ...phLine() });- path(g, 'M-5 -144V-136H7V-144', { fill: PH.skin });- mk('circle', { cx: 2, cy: -160, r: 18, fill: PH.skin, ...phLine() }, g);- path(g, 'M-17 -160Q-17 -181 2 -180Q19 -180 21 -165Q11 -171 0 -169Q-7 -162 -17 -154Z', { fill: hair, ...phLine(2) });- mk('circle', { cx: 10, cy: -161, r: 2.4, fill: PH.out }, g);- path(g, 'M6 -151Q11 -148 15 -152', { stroke: PH.out, 'stroke-width': 2 });- const legF = leg(false), armF = arm(false);- g._limbs = { legB, legF, armB, armF };+ const armB = arm(topB, .28), legB = leg(.28), legF = leg(0);+ const topF = G(b, { y: -PH_LEG });+ phBlock(topF, -14, -64, 28, 64, shirt, { top: 5, right: 6 });+ mk('rect', { x: 2, y: -46, width: 7, height: 6, fill: dk(shirt, .12) }, topF);+ phBlock(topF, -14, -8, 28, 8, pants, { top: 0, right: 6 });+ const H = G(topF, { x: 3, y: -86 });+ phBlock(H, -20, -20, 40, 40, skin, { top: 0, bottom: 5, ol: 0 });+ mk('rect', { x: -20, y: -20, width: 40, height: 10, fill: hair }, H);+ mk('rect', { x: -20, y: -10, width: 13, height: 15, fill: hair }, H);+ mk('rect', { x: 13, y: -10, width: 7, height: 4, fill: hair }, H);+ mk('rect', { x: -16, y: -20, width: 24, height: 3, fill: lt(hair, .2) }, H);+ mk('rect', { x: -7, y: -3, width: 6, height: 8, fill: dk(skin, .14) }, H);+ mk('rect', { x: 7, y: -9, width: 9, height: 2.5, fill: dk(hair) }, H);+ mk('rect', { x: 8, y: -5, width: 6, height: 6, fill: PH.out }, H);+ mk('rect', { x: 11, y: -5, width: 2, height: 2, fill: '#fff' }, H);+ mk('rect', { x: 14, y: 3, width: 5, height: 3, fill: '#e98d79', opacity: .5 }, H);+ mk('rect', { x: 9, y: 9, width: 8, height: 2.5, fill: dk(skin, .5) }, H);+ mk('rect', { x: -20, y: -20, width: 40, height: 40, fill: 'none', ...phLine() }, H);+ const armF = arm(topF, 0);+ Object.assign(g, { _limbs: { legB, legF, armB, armF }, _body: b, _top: [topB, topF] });+ phPose(g, pose, phase);return g;}-// Walking pose; phase in radians (one stride per 2π). phase = null stands still.-function phPose(person, phase) {- const L = person._limbs, a = phase == null ? 0 : Math.sin(phase);- put(L.legF, { r: -24 * a }); put(L.legB, { r: 24 * a });- put(L.armF, { r: 20 * a }); put(L.armB, { r: -20 * a });-}-// A window with someone sitting behind it, clipped to the glass.-let phClipN = 0;-function phWindow(p, d, bust) {- const id = 'phclip' + (++phClipN), cp = mk('clipPath', { id }, mk('defs', {}, p));- mk('path', { d }, cp);- path(p, d, { fill: PH.glass });- if (bust) { const w = G(p); w.setAttribute('clip-path', `url(#${id})`); phBust(w, bust); }- path(p, d, { fill: 'none', ...phLine(2) });+// [front leg, back leg, front arm, back arm, body drop, lean]; angles in degrees, + swings backward.+const PH_POSES = {+ stand: () => [0, 0, 4, -4, 0, 0],+ walk: f => { const a = 28 * Math.sin(f); return [-a, a, a, -a, PH_LEG * (1 - Math.cos(a * Math.PI / 180)), 2]; },+ run: f => { const a = 48 * Math.sin(f); return [-a, a, .9 * a, -.9 * a, PH_LEG * (1 - Math.cos(a * Math.PI / 180)) - 6 * Math.abs(Math.cos(f)), 9]; },+ sit: () => [-90, -84, -50, -42, 0, -3],+};+function phPose(person, pose = 'stand', phase = 0) {+ const [lf, lb, af, ab, drop, lean] = PH_POSES[pose](phase), L = person._limbs;+ put(L.legF, { r: lf }); put(L.legB, { r: lb }); put(L.armF, { r: af }); put(L.armB, { r: ab });+ put(person._body, { y: drop }); person._top.forEach(e => put(e, { r: lean }));}-// Head and shoulders, for windows.-function phBust(p, { shirt = PH.you, hair = PH.hair, x = 0, y = 0, s = 1 } = {}) {- const g = G(p, { x, y, s });- path(g, 'M-26 0Q-24 -30 0 -32Q24 -30 26 0Z', { fill: shirt, ...phLine() });- mk('circle', { cx: 2, cy: -50, r: 17, fill: PH.skin, ...phLine() }, g);- path(g, 'M-16 -50Q-16 -70 2 -69Q18 -69 20 -55Q10 -61 0 -59Q-7 -52 -16 -45Z', { fill: hair, ...phLine(2) });- mk('circle', { cx: 10, cy: -51, r: 2.3, fill: PH.out }, g);- return g;+// A window (polygon points) with an optional seated person behind the glass (phPerson options), clipped to it.+function phWindow(p, pts, who) {+ const id = 'phw' + (++phN), cp = mk('clipPath', { id }, mk('defs', {}, p));+ mk('path', { d: phPts(pts) }, cp);+ phPoly(p, pts, PH.glass, 0);+ const w = G(p); w.setAttribute('clip-path', `url(#${id})`);+ if (who) phPerson(w, { pose: 'sit', shadow: false, ...who });+ const xs = pts.map(q => q[0]), ys = pts.map(q => q[1]), x0 = Math.min(...xs), y0 = Math.min(...ys), y1 = Math.max(...ys), h = y1 - y0;+ phPoly(w, [[x0 + h * .9, y0], [x0 + h * 1.2, y0], [x0 + h * .5, y1], [x0 + h * .2, y1]], '#ffffff', 0).setAttribute('opacity', .28);+ phPoly(p, pts, 'none', 2);}/* ---------- vehicles ---------- */-// A wheel whose spokes show rotation: put(w, {r: degrees}).+// A wheel: a 16-sided tyre with a plus-shaped hub, so rotation reads clearly. put(w, {r: degrees}); see phRoll.function phWheel(p, x, y, r) {const g = G(p, { x, y });- mk('circle', { r, fill: PH.tire, ...phLine() }, g);- mk('circle', { r: r * .5, fill: PH.hub }, g);- path(g, [0, 72, 144, 216, 288].map(a => `M0 0L${(r * .5 * Math.cos(a * Math.PI / 180)).toFixed(1)} ${(r * .5 * Math.sin(a * Math.PI / 180)).toFixed(1)}`).join(''), { stroke: PH.tire, 'stroke-width': 3 });- mk('circle', { r: 3.5, fill: PH.tire }, g);+ const ngon = (k, n) => Array.from({ length: n }, (_, i) => { const a = (i + .5) / n * 2 * Math.PI; return [+(k * Math.cos(a)).toFixed(1), +(k * Math.sin(a)).toFixed(1)]; });+ phPoly(g, ngon(r, 16), PH.tire);+ phPoly(g, ngon(r * .6, 8), PH.rim, 1.5);+ mk('rect', { x: -r * .5, y: -r * .1, width: r, height: r * .2, fill: dk(PH.rim, .4) }, g);+ mk('rect', { x: -r * .1, y: -r * .5, width: r * .2, height: r, fill: dk(PH.rim, .4) }, g);+ mk('rect', { x: -r * .16, y: -r * .16, width: r * .32, height: r * .32, fill: lt(PH.rim), ...phLine(1.2) }, g);+ g._r = r;return g;}const phRoll = (v, dist) => v._wheels.forEach(w => put(w, { r: dist / w._r * 180 / Math.PI }));-// Semi truck, about 400 px long and 190 px tall; cab at the front (right).-function phTruck(p, { x = 0, y = 0, s = 1, o = 1, color = PH.truck } = {}) {+const phWell = (p, wx, r) => phPoly(p, [[wx - r - 7, -28], [wx - r - 7, -r - 22], [wx - r + 1, -r - 30], [wx + r - 1, -r - 30], [wx + r + 7, -r - 22], [wx + r + 7, -28]], '#141c1a', 2);+// Family car, 240 px long and 104 px tall; the driver sits at the front window.+function phCar(p, { x = 0, y = 0, s = 1, o = 1, color = PH.car, driver = PH.you } = {}) {const g = G(p, { x, y, s, o });- path(g, 'M52 -150V-188', { stroke: PH.hub, 'stroke-width': 8 });- mk('rect', { x: -235, y: -182, width: 270, height: 136, rx: 6, fill: PH.trailer, ...phLine() }, g);- mk('rect', { x: -235, y: -86, width: 270, height: 12, fill: color }, g);- mk('rect', { x: -235, y: -50, width: 405, height: 14, rx: 4, fill: '#3b4744', ...phLine(2) }, g);- path(g, 'M44 -40V-138Q44 -152 58 -152H118L150 -108H160Q170 -108 170 -96V-40Z', { fill: color, ...phLine() });- phWindow(g, 'M90 -142H116L140 -110H90Z', { shirt: '#e8a0c8', x: 108, y: -106, s: .6 });- path(g, 'M60 -100H82', { stroke: PH.out, 'stroke-width': 3 });- mk('circle', { cx: 163, cy: -82, r: 6, fill: '#f6d27a', ...phLine(2) }, g);- g._wheels = [-200, -150, 80, 136].map(wx => Object.assign(phWheel(g, wx, -24, 24), { _r: 24 }));+ phShadow(g, 122);+ const body = [[-120, -28], [-120, -58], [-112, -68], [-68, -70], [-44, -104], [36, -104], [62, -70], [108, -66], [120, -56], [120, -28]];+ phPoly(g, body, color, 0);+ phPoly(g, [[-120, -44], [120, -44], [120, -28], [-120, -28]], dk(color), 0);+ path(g, 'M-112 -66L-68 -68M62 -68L108 -64', { stroke: lt(color, .4), 'stroke-width': 4 });+ mk('rect', { x: -42, y: -104, width: 76, height: 5, fill: lt(color, .4) }, g);+ phWindow(g, [[-60, -72], [-40, -98], [-6, -98], [-6, -72]]);+ phWindow(g, [[2, -98], [32, -98], [56, -72], [2, -72]], { shirt: driver, x: 21, y: -17, s: .45 });+ path(g, 'M-2 -72V-30M64 -70V-32M-64 -69V-32', { stroke: dk(color, .4), 'stroke-width': 2 });+ [-28, 40].forEach(hx => mk('rect', { x: hx, y: -64, width: 13, height: 4, fill: dk(color, .45) }, g));+ phBlock(g, 60, -80, 9, 8, color, { top: 0, ol: 1.5 });+ phPoly(g, body, 'none');+ [-72, 72].forEach(wx => phWell(g, wx, 22));+ phBlock(g, 110, -62, 10, 9, PH.light, { top: 0, ol: 1.5 });+ phBlock(g, -120, -62, 7, 11, PH.tail, { top: 0, ol: 1.5 });+ phBlock(g, 104, -40, 20, 12, PH.metal, { top: 3, ol: 1.5 });+ phBlock(g, -124, -40, 20, 12, PH.metal, { top: 3, ol: 1.5 });+ g._wheels = [-72, 72].map(wx => phWheel(g, wx, -22, 22));return g;}-// Family car, about 225 px long and 105 px tall; the driver sits at the front window.-function phCar(p, { x = 0, y = 0, s = 1, o = 1, color = PH.car, driver = PH.you } = {}) {+// Semi truck, about 435 px long and 205 px tall; cab at the front (right).+function phTruck(p, { x = 0, y = 0, s = 1, o = 1, color = PH.truck } = {}) {const g = G(p, { x, y, s, o });- const body = 'M-112 -24V-56Q-110 -66 -96 -68L-62 -72L-36 -102Q-31 -107 -20 -107H34Q44 -107 51 -100L78 -72L100 -68Q113 -64 113 -50V-24Z';- path(g, body, { fill: color, ...phLine() });- phWindow(g, 'M-28 -98H-3V-74H-50Z');- phWindow(g, 'M3 -98H30L52 -74H3Z', { shirt: driver, x: 20, y: -62, s: .6 });- path(g, 'M-6 -66V-30M14 -58H26', { stroke: PH.out, 'stroke-width': 2.5 });- mk('circle', { cx: 104, cy: -54, r: 6, fill: '#f6d27a', ...phLine(2) }, g);- mk('rect', { x: -114, y: -60, width: 8, height: 12, rx: 2, fill: COL.bad }, g);- g._wheels = [-68, 70].map(wx => Object.assign(phWheel(g, wx, -22, 22), { _r: 22 }));+ phShadow(g, 222, -35);+ phBlock(g, 48, -206, 9, 64, PH.rim, { top: 0, right: 3 });+ phBlock(g, -250, -190, 284, 138, PH.trailer, { top: 6, bottom: 10 });+ path(g, Array.from({ length: 9 }, (_, i) => `M${-222 + i * 28} -184V-62`).join(''), { stroke: dk(PH.trailer, .1), 'stroke-width': 2 });+ phBlock(g, -250, -98, 284, 12, color, { top: 0, ol: 2 });+ [-174, -78].forEach(hy => mk('rect', { x: -247, y: hy, width: 7, height: 9, fill: PH.metal }, g));+ phBlock(g, -250, -52, 432, 14, '#3b4744', { top: 3 });+ const cab = [[40, -40], [40, -166], [124, -166], [144, -120], [176, -116], [182, -106], [182, -40]];+ phPoly(g, cab, color, 0);+ phPoly(g, [[40, -70], [182, -70], [182, -40], [40, -40]], dk(color), 0);+ mk('rect', { x: 40, y: -166, width: 84, height: 5, fill: lt(color, .4) }, g);+ phWindow(g, [[84, -158], [120, -158], [136, -124], [84, -124]], { shirt: '#e8a0c8', x: 103, y: -54, s: .5 });+ path(g, 'M78 -162V-46M148 -118V-46', { stroke: dk(color, .4), 'stroke-width': 2 });+ mk('rect', { x: 88, y: -110, width: 13, height: 4, fill: dk(color, .45) }, g);+ phPoly(g, cab, 'none');+ phBlock(g, 172, -104, 10, 38, '#3b4744', { top: 0, ol: 1.5 });+ path(g, 'M175 -98H180M175 -90H180M175 -82H180M175 -74H180', { stroke: PH.metal, 'stroke-width': 2 });+ phBlock(g, 172, -62, 11, 10, PH.light, { top: 0, ol: 1.5 });+ phBlock(g, 170, -48, 18, 12, PH.metal, { top: 3, ol: 1.5 });+ phBlock(g, 56, -60, 48, 18, PH.rim, { top: 4, ol: 1.5 });+ [-205, -150, 76, 140].forEach(wx => phWell(g, wx, 24));+ g._wheels = [-205, -150, 76, 140].map(wx => phWheel(g, wx, -24, 24));return g;}-// Passenger railway carriage, `w` long, 250 px tall; returns window centres in g._wins (x, top of the window).+// Passenger railway carriage, `w` long, 262 px tall; g._wins holds window centres (x). Window tops at y = -232.function phCarriage(p, { x = 0, y = 0, w = 960, color = COL.real } = {}) {const g = G(p, { x, y });const x0 = -w / 2;- mk('rect', { x: x0, y: -262, width: w, height: 222, rx: 26, fill: color, ...phLine(3) }, g);- mk('rect', { x: x0, y: -112, width: w, height: 14, fill: mix(color, '#000000', .3) }, g);+ phShadow(g, w / 2);+ phBlock(g, x0, -262, w, 222, color, { top: 0, bottom: 34 });+ phBlock(g, x0 + 10, -276, w - 20, 16, dk(color, .35), { top: 4 });+ mk('rect', { x: x0, y: -118, width: w, height: 12, fill: lt(color, .45) }, g);g._wins = [];for (let wx = x0 + 110; wx < w / 2 - 60; wx += 170) {- mk('rect', { x: wx - 62, y: -232, width: 124, height: 96, rx: 10, fill: PH.glass, ...phLine(2.5) }, g);+ mk('rect', { x: wx - 68, y: -238, width: 136, height: 108, fill: dk(color, .3) }, g);+ phWindow(g, [[wx - 62, -232], [wx + 62, -232], [wx + 62, -136], [wx - 62, -136]]);g._wins.push(wx);}+ path(g, `M${w / 2 - 34} -250V-46M${w / 2 - 6} -250V-46`, { stroke: dk(color, .4), 'stroke-width': 2 });[x0 + 110, w / 2 - 110].forEach(bx => {- mk('rect', { x: bx - 80, y: -42, width: 160, height: 12, rx: 4, fill: '#3b4744' }, g);+ phBlock(g, bx - 80, -46, 160, 14, '#3b4744', { top: 3, ol: 2 });[-46, 46].forEach(o => phWheel(g, bx + o, -22, 20));});return g;}+// Glass (and anyone behind it) for one of a carriage's windows.+const phCarWin = (train, k, who) => { const wx = train._wins[k]; phWindow(train, [[wx - 62, -232], [wx + 62, -232], [wx + 62, -136], [wx - 62, -136]], who); };/* ---------- scenery ---------- */-function phTree(p, { x = 0, y = 0, s = 1 } = {}) {+// A blocky tree. kind 0: broad; kind 1: tall, in tiers.+function phTree(p, { x = 0, y = 0, s = 1, o = 1, leaf = PH.leaf, kind = 0 } = {}) {+ const g = G(p, { x, y, s, o });+ phShadow(g, 46);+ phBlock(g, -9, -88, 18, 88, PH.trunk, { top: 0, right: 5 });+ [[-5, -62], [-1, -30]].forEach(([bx, by]) => mk('rect', { x: bx, y: by, width: 4, height: 8, fill: dk(PH.trunk, .3) }, g));+ const rects = kind ? [[-46, -122, 92, 36], [-36, -154, 72, 34], [-24, -182, 48, 30], [-12, -200, 24, 20]]+ : [[-52, -150, 104, 72], [-32, -176, 64, 28], [-64, -134, 14, 40], [50, -140, 14, 36]];+ phMerge(g, rects, leaf, c => {+ rects.forEach(([rx, ry, rw]) => mk('rect', { x: rx, y: ry, width: rw, height: 6, fill: lt(leaf, .22) }, c));+ phPixels(c, -66, -202, 66, -78, leaf, 13, kind + 1);+ mk('rect', { x: 22, y: -210, width: 50, height: 140, fill: dk(leaf), opacity: .7 }, c);+ mk('rect', { x: -70, y: -96, width: 140, height: 20, fill: dk(leaf), opacity: .7 }, c);+ });+ return g;+}+function phBush(p, { x = 0, y = 0, s = 1, leaf = PH.leaf } = {}) {const g = G(p, { x, y, s });- path(g, 'M-9 0L-6 -74H6L9 0Z', { fill: PH.trunk, ...phLine(2) });- [[-30, -96, 36, PH.leaf2], [30, -100, 34, PH.leaf2], [0, -134, 44, PH.leaf], [-18, -108, 30, PH.leaf], [18, -112, 30, PH.leaf]]- .forEach(([cx, cy, r, fill]) => mk('circle', { cx, cy, r, fill, ...phLine(2) }, g));+ const rects = [[-34, -30, 68, 30], [-20, -44, 40, 16]];+ phMerge(g, rects, dk(leaf, .1), c => { phPixels(c, -34, -44, 34, 0, dk(leaf, .1), 10, 5); mk('rect', { x: 12, y: -44, width: 30, height: 44, fill: dk(leaf, .3), opacity: .6 }, c); });return g;}function phLamp(p, { x = 0, y = 0, s = 1 } = {}) {const g = G(p, { x, y, s });- path(g, 'M0 0V-200Q0 -222 24 -222H42', { stroke: PH.lamp, 'stroke-width': 8 });- path(g, 'M30 -224H58L52 -212H36Z', { fill: '#f3c95c', ...phLine(2) });- mk('rect', { x: -12, y: -6, width: 24, height: 8, rx: 3, fill: PH.lamp }, g);+ phPoly(g, [[34, -213], [56, -213], [92, 0], [0, 0]], PH.light, 0).setAttribute('opacity', .06);+ phBlock(g, -5, -224, 10, 224, PH.lamp, { top: 0, right: 3 });+ phBlock(g, -5, -230, 50, 8, PH.lamp, { top: 2.5 });+ phBlock(g, 28, -232, 32, 14, '#5d6b66', { top: 3 });+ mk('rect', { x: 32, y: -218, width: 24, height: 6, fill: PH.light, ...phLine(1.5) }, g);+ phBlock(g, -11, -10, 22, 10, PH.lamp, { top: 3 });return g;}+// A blocky house with a stepped roof and lit windows.function phHouse(p, { x = 0, y = 0, s = 1, wall = '#d9b48a', roof = '#a65d4b' } = {}) {const g = G(p, { x, y, s });- mk('rect', { x: -70, y: -110, width: 140, height: 110, fill: wall, ...phLine() }, g);- path(g, 'M-86 -106L0 -172L86 -106Z', { fill: roof, ...phLine() });- mk('rect', { x: -14, y: -58, width: 28, height: 58, fill: '#7a5a43', ...phLine(2) }, g);- [-46, 46].forEach(wx => mk('rect', { x: wx - 15, y: -88, width: 30, height: 28, fill: '#f6e3a1', ...phLine(2) }, g));+ phShadow(g, 84);+ phBlock(g, 30, -196, 18, 50, '#8d6e5c', { top: 4, right: 5 });+ phBlock(g, -70, -112, 140, 112, wall, { top: 0, right: 16 });+ phPixels(g, -70, -112, 54, 0, wall, 14, 9);+ [86, 74, 62, 50, 38, 26].forEach((hw, i) => phBlock(g, -hw, -124 - 12 * i, 2 * hw, 12, roof, { top: 3.5, right: 8, ol: 2 }));+ mk('rect', { x: -70, y: -112, width: 140, height: 6, fill: '#000', opacity: .2 }, g);+ phBlock(g, -14, -60, 28, 60, '#7a5a43', { top: 0, right: 5 });+ mk('rect', { x: 6, y: -34, width: 4, height: 4, fill: PH.light }, g);+ [-46, 46].forEach(wx => {+ phBlock(g, wx - 15, -90, 30, 28, '#f6e3a1', { top: 0, ol: 2 });+ path(g, `M${wx} -90V-62M${wx - 15} -76H${wx + 15}`, { stroke: '#8d6e5c', 'stroke-width': 3 });+ mk('rect', { x: wx - 18, y: -62, width: 36, height: 4, fill: lt(wall, .4), ...phLine(1.5) }, g);+ });return g;}-// Side-view road with far/near lanes and wrapping scenery. update(cam) slides every fixed object by the camera position.-const ROAD = { top: 478, far: 566, mid: 640, near: 726, bot: 748, side: 474, wrap: 2100 };-function phRoad(p, { trees = [60, 520, 980, 1440, 1880], lamps = [300, 1260] } = {}) {- const back = G(p), g = G(p);- mk('rect', { x: 0, y: ROAD.side - 6, width: 1600, height: 12, fill: PH.verge }, back);- const items = [...trees.map(x => [phTree(back, { y: ROAD.side }), x]), ...lamps.map(x => [phLamp(back, { y: ROAD.side }), x])];- mk('rect', { x: 0, y: ROAD.top, width: 1600, height: ROAD.bot - ROAD.top, fill: PH.road }, g);- mk('rect', { x: 0, y: ROAD.bot, width: 1600, height: 900 - ROAD.bot, fill: PH.verge }, g);- path(g, `M0 ${ROAD.top + 4}H1600M0 ${ROAD.bot - 4}H1600`, { stroke: PH.line, 'stroke-width': 3, opacity: .7 });- const dash = path(g, `M0 ${ROAD.mid}H1600`, { stroke: PH.line, 'stroke-width': 5, 'stroke-dasharray': '70 90', 'stroke-linecap': 'butt' });- const tufts = path(g, Array.from({ length: 9 }, (_, i) => { const x = i * 250 + 60 + (i % 2) * 70, y = ROAD.bot + 58 + (i % 3) * 30; return `M${x - 26} ${y}Q${x - 28} ${y - 18} ${x - 12} ${y - 20}Q${x - 4} ${y - 36} ${x + 8} ${y - 26}Q${x + 26} ${y - 26} ${x + 26} ${y}Z`; }).join(''),- { fill: PH.leaf2, ...phLine(2) });- const vehicles = G(p), front = G(p);- const road = { back, g, vehicles, front, cam: 0,- update(cam) {- road.cam = cam;- const W = ROAD.wrap, wrap = x => ((x - cam + 250) % W + W) % W - 250;- items.forEach(([e, x]) => put(e, { x: wrap(x) }));- dash.setAttribute('stroke-dashoffset', cam % 160);- put(tufts, { x: -(((cam % 250) + 250) % 250) });- },- // Screen x of a world position.- sx: x => x - road.cam,- };- road.update(0);- return road;+// Stepped (voxel) hills repeating every W px, ridge around y, filled down to y + depth.+function phHills(p, { y, amp = 40, W = ROAD.W, x0, x1, fill, seed = 1, step = 40, q = 16, depth = 700 }) {+ const f = x => y - amp * (.55 * Math.sin(2 * Math.PI * 2 * x / W + seed) + .3 * Math.sin(2 * Math.PI * 5 * x / W + 2 * seed) + .15 * Math.sin(2 * Math.PI * 11 * x / W + 3 * seed));+ let d = `M${x0} ${y + depth}`;+ for (let x = x0; x < x1; x += step) d += `V${Math.round(f(x) / q) * q}H${x + step}`;+ return path(p, d + `V${y + depth}Z`, { fill, stroke: lt(fill, .07), 'stroke-width': 2, 'stroke-linejoin': 'miter' });+}+// Side-view street on a camera (see camera() in the engine). Fixed scenery wraps every ROAD.W world px, so follow shots+// can run for any distance; far hills are parallax layers. Returns the layers to add things to, back to front:+// scenery (wraps; for fixed things that should repeat), side (people on the pavement), vehicles, front (labels).+const ROAD = { top: 478, far: 566, mid: 640, near: 726, bot: 748, side: 474, W: 2400 };+function phRoad(cam, { trees = [60, 540, 1020, 1500, 1980], lamps = [300, 1500], hills = true, x0 = -1600, x1 = 5600 } = {}) {+ const W = ROAD.W, rep = xs => xs.flatMap(v => { const out = []; for (let x = v + Math.floor((x0 - v) / W) * W; x < x1; x += W) out.push(x); return out; });+ if (hills) {+ phHills(cam.layer(.25, W), { y: 330, amp: 50, x0, x1, fill: PH.hill1, seed: 1 });+ phHills(cam.layer(.5, W), { y: 400, amp: 34, x0, x1, fill: PH.hill2, seed: 4 });+ }+ const scenery = cam.layer(1, W);+ phBlock(scenery, x0, 452, x1 - x0, 26, PH.walk, { top: 4, ol: 0 });+ path(scenery, rep(Array.from({ length: 40 }, (_, i) => i * 60)).map(x => `M${x} 457V478`).join(''), { stroke: dk(PH.walk, .2), 'stroke-width': 2 });+ trees.forEach((v, i) => rep([v]).forEach(x => phTree(scenery, { x, y: ROAD.side, kind: i % 2 })));+ rep(lamps).forEach(x => phLamp(scenery, { x, y: ROAD.side }));+ const side = G(cam.world), surface = cam.layer(1, W);+ mk('rect', { x: x0, y: ROAD.top, width: x1 - x0, height: ROAD.bot - ROAD.top, fill: PH.road }, surface);+ mk('rect', { x: x0, y: ROAD.top, width: x1 - x0, height: 6, fill: dk(PH.road, .3) }, surface);+ path(surface, `M${x0} ${ROAD.top + 12}H${x1}M${x0} ${ROAD.bot - 8}H${x1}`, { stroke: PH.line, 'stroke-width': 4, opacity: .65 });+ path(surface, rep(Array.from({ length: 15 }, (_, i) => i * 160)).map(x => `M${x} ${ROAD.mid - 3}h70v6h-70Z`).join(''), { fill: PH.line });+ phBlock(surface, x0, ROAD.bot, x1 - x0, 700, PH.grass, { top: 6, ol: 0 });+ rep(Array.from({ length: 10 }, (_, i) => i * 240)).forEach(x => {+ const k = Math.round((((x % W) + W) % W) / 240), gy = ROAD.bot + 52 + (k % 3) * 34, gx = x + 60 + (k % 2) * 70;+ path(surface, `M${gx} ${gy}h6v-14h6v8h6v-18h6v24h6v-10h6v10Z`, { fill: lt(PH.grass, .14) });+ if (k % 3 === 1) [[gx + 50, gy - 6, COL.rat], [gx + 64, gy - 2, COL.int]].forEach(([fx, fy, c]) => mk('rect', { x: fx, y: fy, width: 6, height: 6, fill: c }, surface));+ });+ return { scenery, side, vehicles: G(cam.world), front: G(cam.world) };}/* ---------- objects ---------- */-// Soccer ball of radius r, centred on the origin; rotate with put(ball, {r}).+// Soccer ball of radius r, centred on the origin: move the group, rotate ball._spin; shading and shadow stay put.function phBall(p, r = 40, o = {}) {const g = G(p, o);- mk('circle', { r, fill: '#f4f1e8', ...phLine(3) }, g);+ mk('ellipse', { cy: r, rx: r * .9, ry: r * .12, fill: '#000', opacity: .25 }, g);+ const sp = G(g);+ mk('circle', { r, fill: '#f4f1e8' }, sp);const pent = (cx, cy, k, rot) => Array.from({ length: 5 }, (_, i) => { const a = rot + i * 72 * Math.PI / 180; return `${i ? 'L' : 'M'}${(cx + k * Math.cos(a)).toFixed(1)} ${(cy + k * Math.sin(a)).toFixed(1)}`; }).join('') + 'Z';- path(g, pent(0, 0, r * .3, -Math.PI / 2), { fill: '#2a3532' });+ path(sp, pent(0, 0, r * .3, -Math.PI / 2), { fill: '#2a3532' });[0, 72, 144, 216, 288].forEach(a => {const t = (a - 90) * Math.PI / 180, cx = Math.cos(t) * r * .9, cy = Math.sin(t) * r * .9;- path(g, pent(cx, cy, r * .24, t + Math.PI), { fill: '#2a3532' });- path(g, `M${(Math.cos(t) * r * .3).toFixed(1)} ${(Math.sin(t) * r * .3).toFixed(1)}L${(Math.cos(t) * r * .68).toFixed(1)} ${(Math.sin(t) * r * .68).toFixed(1)}`, { stroke: '#2a3532', 'stroke-width': 2 });+ path(sp, pent(cx, cy, r * .24, t + Math.PI), { fill: '#2a3532' });+ path(sp, `M${(Math.cos(t) * r * .3).toFixed(1)} ${(Math.sin(t) * r * .3).toFixed(1)}L${(Math.cos(t) * r * .68).toFixed(1)} ${(Math.sin(t) * r * .68).toFixed(1)}`, { stroke: '#2a3532', 'stroke-width': 2 });});+ const id = 'phb' + (++phN);+ mk('circle', { r }, mk('clipPath', { id }, mk('defs', {}, g)));+ const sh = G(g); sh.setAttribute('clip-path', `url(#${id})`);+ mk('circle', { cx: r * .35, cy: r * .4, r: r * 1.05, fill: 'none', stroke: '#000', 'stroke-width': r * .5, opacity: .14 }, sh);+ mk('circle', { cx: -r * .38, cy: -r * .42, r: r * .2, fill: '#fff', opacity: .55 }, sh);mk('circle', { r, fill: 'none', ...phLine(3) }, g);+ g._spin = sp;return g;}// Stopwatch; returns the group with ._hand to rotate.function phStopwatch(p, { x = 0, y = 0, r = 60, o = 1 } = {}) {const g = G(p, { x, y, o });- mk('rect', { x: -12, y: -r - 22, width: 24, height: 16, rx: 4, fill: PH.lamp }, g);- mk('circle', { r: r + 8, fill: PH.lamp, ...phLine() }, g);+ phBlock(g, -12, -r - 24, 24, 18, PH.lamp, { top: 4, ol: 2 });+ mk('circle', { r: r + 9, fill: PH.lamp, ...phLine() }, g);mk('circle', { r, fill: '#f4f1e8' }, g);+ mk('circle', { r: r * .96, fill: 'none', stroke: '#000', 'stroke-width': r * .12, opacity: .08 }, g);for (let i = 0; i < 12; i++) { const a = i * Math.PI / 6; path(g, `M${Math.sin(a) * r * .8} ${-Math.cos(a) * r * .8}L${Math.sin(a) * r * .92} ${-Math.cos(a) * r * .92}`, { stroke: PH.out, 'stroke-width': 3 }); }g._hand = path(g, `M0 8V${-r * .78}`, { stroke: COL.bad, 'stroke-width': 4 });mk('circle', { r: 5, fill: PH.out }, g);@@ -210,11 +348,11 @@ function phNotebook(p, { x = 0, y = 0, s = 1 } = {}) {const g = G(p, { x, y, s });const page = (dx, parent) => {const e = mk('g', {}, parent);- mk('rect', { x: dx, y: -70, width: 130, height: 170, rx: 6, fill: '#f4f1e8', ...phLine(2.5) }, e);+ mk('rect', { x: dx, y: -70, width: 130, height: 170, fill: '#f4f1e8', ...phLine() }, e);for (let ly = -40; ly < 95; ly += 20) path(e, `M${dx + 14} ${ly}H${dx + 116}`, { stroke: '#86c9e8', 'stroke-width': 2 });return e;};- mk('rect', { x: -142, y: -78, width: 284, height: 186, rx: 10, fill: '#a65d4b', ...phLine() }, g);+ phBlock(g, -142, -78, 284, 186, '#a65d4b', { top: 0, bottom: 8 });page(-132, g); page(2, g);g._page = page(0, g);return g;@@ -224,41 +362,57 @@ function phFlip(book, deg) {e.setAttribute('transform', `scale(${Math.abs(k) < .02 ? .02 * Math.sign(k || 1) : k},1) skewY(${-8 * Math.sin(deg * Math.PI / 180)})`);e.firstChild.setAttribute('fill', k < 0 ? '#e2ddcf' : '#f4f1e8');}-// A cheerful Sun with rays.+// The Sun with block rays (g._rays turns).function phSun(p, { x = 0, y = 0, r = 54 } = {}) {const g = G(p, { x, y });const rays = G(g);- path(rays, Array.from({ length: 12 }, (_, i) => { const a = i * Math.PI / 6; return `M${Math.cos(a) * (r + 10)} ${Math.sin(a) * (r + 10)}L${Math.cos(a) * (r + 28)} ${Math.sin(a) * (r + 28)}`; }).join(''), { stroke: '#f6c445', 'stroke-width': 6 });+ for (let i = 0; i < 12; i++) mk('rect', { x: r + 8, y: -4, width: 18, height: 8, fill: '#f6c445', transform: `rotate(${i * 30})` }, rays);mk('circle', { r, fill: '#f6c445', ...phLine() }, g);- mk('circle', { r: r * .7, fill: '#f9d978', opacity: .6 }, g);+ mk('circle', { cx: -r * .2, cy: -r * .2, r: r * .62, fill: '#f9d978', opacity: .7 }, g);g._rays = rays;return g;}-// A deterministic wobbly blob, for land masses.-function phBlob(cx, cy, r, seed) {- const n = 12, pts = Array.from({ length: n }, (_, i) => {- const a = i / n * 2 * Math.PI, k = r * (.72 + .28 * Math.sin(seed * (i + 1) * 1.7) * Math.cos(seed + i));- return [cx + k * Math.cos(a), cy + k * Math.sin(a)];- });- return pts.map((q, i) => { const nx = pts[(i + 1) % n]; return `${i ? '' : `M${((q[0] + pts[n - 1][0]) / 2).toFixed(1)} ${((q[1] + pts[n - 1][1]) / 2).toFixed(1)}`}Q${q[0].toFixed(1)} ${q[1].toFixed(1)} ${((q[0] + nx[0]) / 2).toFixed(1)} ${((q[1] + nx[1]) / 2).toFixed(1)}`; }).join('') + 'Z';+// Earth's land as voxel cells: continents are unions of discs (centre and radius as fractions of R) with ragged edges.+const PH_LAND = [[0, -.86, .44], [.6, .22, .3], [-.62, .3, .28], [.12, .64, .22], [-.3, -.22, .16], [.52, -.46, .18]];+function phEarthCells(p, R, cell) {+ for (let i = 0, x = -R; x < R; i++, x += cell) for (let j = 0, y = -R; y < R; j++, y += cell) {+ const cx = x + cell / 2, cy = y + cell / 2, h = phHash(i, j);+ if (Math.hypot(cx, cy) > R + cell) continue;+ let fill = null;+ if (Math.hypot(cx, cy) < R * (.15 + .07 * h)) fill = h > .5 ? '#f4f6f7' : '#dfeaf0';+ else if (PH_LAND.some(([bx, by, br]) => Math.hypot(cx - bx * R, cy - by * R) < br * R * (.8 + .4 * h))) fill = h > .8 ? lt(PH.leaf, .15) : h < .2 ? '#a9a061' : PH.leaf;+ else if (h > .9) fill = lt(PH.ocean, .1);+ if (fill) mk('rect', { x, y, width: cell + .4, height: cell + .4, fill }, p);+ }}-// Small Earth as seen from the side (for orbits).+const phThin = (e, w) => { e.setAttribute('vector-effect', 'non-scaling-stroke'); e.setAttribute('stroke-width', w); return e; };+// Small Earth seen from the side (for orbits).function phEarthSmall(p, { x = 0, y = 0, r = 26 } = {}) {- const g = G(p, { x, y });- mk('circle', { r, fill: '#3f7fb0', ...phLine(2) }, g);- path(g, phBlob(-r * .25, -r * .2, r * .45, 3) + phBlob(r * .45, r * .35, r * .3, 7), { fill: '#6fae74' });+ const g = G(p, { x, y }), id = 'phe' + (++phN);+ mk('circle', { r }, mk('clipPath', { id }, mk('defs', {}, g)));+ mk('circle', { r, fill: PH.ocean }, g);+ const c = G(g); c.setAttribute('clip-path', `url(#${id})`);+ [[-.6, -.5, .5, .4], [.1, .15, .55, .45], [-.4, .3, .3, .25]].forEach(([lx, ly, w, h]) => mk('rect', { x: lx * r, y: ly * r, width: w * r, height: h * r, fill: PH.leaf }, c));+ mk('circle', { cx: r * .3, cy: r * .3, r: r * 1.1, fill: 'none', stroke: '#000', 'stroke-width': r * .5, opacity: .18 }, c);+ mk('circle', { r, fill: 'none', ...phLine(2) }, g);return g;}-// Earth seen from far above the North Pole: the rim is the equator. Rotate g._spin (degrees; negative = counterclockwise on screen).-function phEarthPolar(p, { x = 0, y = 0, R = 220 } = {}) {- const g = G(p, { x, y });+// Earth seen from far above the North Pole: the rim is the equator. Rotate g._spin (degrees; negative = counterclockwise+// on screen). Outlines keep their screen width at any camera zoom.+function phEarthPolar(p, { x = 0, y = 0, R = 220, cell = R / 14 } = {}) {+ const g = G(p, { x, y }), id = 'phE' + (++phN);+ mk('circle', { r: R }, mk('clipPath', { id }, mk('defs', {}, g)));+ mk('circle', { r: R * 1.04, fill: 'none', stroke: '#86c9e8', 'stroke-width': R * .08, opacity: .25 }, g);const spin = G(g);- mk('circle', { r: R, fill: '#3f7fb0', ...phLine(3) }, spin);- path(spin, [[40, .62, R * .26, 2.1], [130, .58, R * .3, 4.3], [215, .64, R * .24, 5.9], [300, .6, R * .28, 8.2], [175, .3, R * .16, 1.3]]- .map(([a, k, r, seed]) => phBlob(Math.cos(a * Math.PI / 180) * R * k, Math.sin(a * Math.PI / 180) * R * k, r, seed)).join(''), { fill: '#6fae74', ...phLine(2) });- path(spin, Array.from({ length: 12 }, (_, i) => { const a = i * Math.PI / 6; return `M0 0L${(Math.cos(a) * R).toFixed(1)} ${(Math.sin(a) * R).toFixed(1)}`; }).join(''), { stroke: '#dfeef5', 'stroke-width': 1.5, opacity: .35 });- [.33, .66].forEach(k => mk('circle', { r: R * k, fill: 'none', stroke: '#dfeef5', 'stroke-width': 1.5, opacity: .35 }, spin));- path(spin, phBlob(0, 0, R * .2, 6.6), { fill: '#f4f6f7', ...phLine(2) });+ mk('circle', { r: R, fill: PH.ocean }, spin);+ const c = G(spin); c.setAttribute('clip-path', `url(#${id})`);+ phEarthCells(c, R, cell);+ path(spin, Array.from({ length: 12 }, (_, i) => { const a = i * Math.PI / 6; return `M0 0L${(Math.cos(a) * R).toFixed(1)} ${(Math.sin(a) * R).toFixed(1)}`; }).join(''), { stroke: '#dfeef5', opacity: .3 })+ .setAttribute('vector-effect', 'non-scaling-stroke');+ [.33, .66].forEach(k => phThin(mk('circle', { r: R * k, fill: 'none', stroke: '#dfeef5', opacity: .3 }, spin), 1.5));+ const shade = G(g); shade.setAttribute('clip-path', `url(#${id})`);+ mk('circle', { r: R, fill: 'none', stroke: '#0e1714', 'stroke-width': R * .14, opacity: .28 }, shade);+ phThin(mk('circle', { r: R, fill: 'none', stroke: PH.out }, g), 3);g._spin = spin;return g;}