An AI skill that turns books into animated, narrated, interactive web experiences
JavaScript ai-agentsai-educationanimationclaudeclaude-codeclaude-skillsedtecheducationinteractive-textbookscormtext-to-video
feat(science): pixel-art images for ch09 section 1
yzrcea41921945c
diff --git a/site/science/ch09/index.html b/site/science/ch09/index.htmlindex 482aca7..14eeafb 100644--- a/site/science/ch09/index.html+++ b/site/science/ch09/index.html@@ -45,9 +45,11 @@ const groundView = (lift, z) => ({ x: t => EX + (ER + lift) * Math.cos(rimAngle(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 });- phHouse(rim(-9), { s: .17 }); phTree(rim(7), { s: .19 }); // the narration's buildings, trees and you+ const gr = ER - 3, ga = .4; // ±23° of smooth ground under the rim scene (the pixel coast is stepped)+ path(E._spin, `M${-gr * Math.sin(ga)} ${-gr * Math.cos(ga)}A${gr} ${gr} 0 0 1 ${gr * Math.sin(ga)} ${-gr * Math.cos(ga)}`, { stroke: PH.land, 'stroke-width': 6, 'stroke-linecap': 'butt' });+ phSprite(rim(-9), 'house', { s: .25 }); phSprite(rim(7), 'tree', { s: .22 }); // the narration's buildings, trees and youconst you = rim(0);- phPerson(you, { s: .17 });+ phPerson(you, { s: .25 });clockRun(t => put(E._spin, { r: ESPIN * t }), D);return { E, you };}@@ -80,7 +82,7 @@ const BEATS = [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: -200, y: 540, s: 1.3 }); phTree(w, { x: 1820, y: 548, s: 1.2, kind: 1 });+ phSprite(w, 'tree', { x: -200, y: 540 }); phSprite(w, 'tree', { x: 1820, y: 548 });const X0 = 200, U = 120, TY = 600, R = 40, xm = v => X0 + U * v;const track = G(w);phBlock(track, X0 - 30, TY, U * 10 + 60, 16, '#8a6f55', { top: 4, ol: 2 });@@ -137,7 +139,7 @@ const BEATS = [put(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 ? 'walk' : 'stand', d / 24);+ put(you, { x: 1160 + d }); phPose(you, d > 0 && d < 260 ? 'walk' : 'stand', d / 22 * Math.PI);}, D);}],['roadside', 'Standing by the road', (m, D) => {@@ -146,16 +148,16 @@ const BEATS = [const road = phRoad(cam);const you = phPerson(road.side, { x: 760, y: ROAD.side, o: 0 });show(you, 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, `truck: ${speedStr(V)}`, { y: 380, fill: PH.truck, o: 0 });+ show(phLabel(road.front, 'you', { x: 760, y: 336, fill: PH.you, o: 0 }), m('stand'));+ const truck = phTruck(road.vehicles, { y: ROAD.far });+ const tl = phLabel(road.front, `truck: ${speedStr(V)}`, { y: 350, 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'));+ pop(phPin(road.front, { x: 760, y: 304, s: 0, o: 0 }), m('speed'));show(tl, m('speed'));const t0 = m('truck', -.3);clockRun(t => {const x = -250 + V * Math.max(0, t - t0);- put(truck, { x }); phRoll(truck, x / .85); put(tl, { x: x + 10 });+ put(truck, { x }); phRoll(truck, x); put(tl, { x: x + 10 });}, D);}],['incar', 'Riding beside the truck', (m, D) => {@@ -166,25 +168,25 @@ const BEATS = [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);- phPerson(road.side, { shirt: PH.friend, x: FR, y: ROAD.side });- const truck = phTruck(road.vehicles, { y: ROAD.far, s: .85 });+ phPerson(road.side, { role: 'friend', x: FR, y: ROAD.side });+ const truck = phTruck(road.vehicles, { y: ROAD.far });const car = phCar(road.vehicles, { y: ROAD.near });// Speed labels live in screen space and are kept inside the frame (cam.track); wrappers fade them in.const tag = (fill, t0) => { const g = G(p, { o: 0 }); show(g, t0); return phLabel(g, '', { fill }); };const fl = tag(PH.friend, .4), tl = tag(PH.truck, tc + 1.6), cl = tag(PH.car, tc + 1.6);const sl = phLabel(p, 'road and trees: ← 30 mph', { x: 70, y: 862, size: 30, fill: COL.dim, anchor: 'start', o: 0 });show(sl, m('back'));- const pinA = phPin(road.side, { x: FR, y: 236, o: 0 }), pinB = phPin(car, { x: 30, y: -112, s: 0, o: 0 });+ const pinA = phPin(road.side, { x: FR, y: 304, 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 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)}`; cam.track(fl, FR, 272, t);- tl.textContent = `truck: ${speedStr(rel)}`; cam.track(tl, x + 10, 380, t);- cl.textContent = `your car: ${speedStr(rel)}`; cam.track(cl, x + 10, ROAD.bot + 56, t);+ phRoll(truck, x + 330); phRoll(car, x + 330);+ phText(fl, `friend: ${speedStr(-camV)}`); cam.track(fl, FR, 336, t);+ phText(tl, `truck: ${speedStr(rel)}`); cam.track(tl, x + 10, 350, t);+ phText(cl, `your car: ${speedStr(rel)}`); cam.track(cl, x + 10, ROAD.bot + 56, t);}, D);}],['rule', 'Motion is relative', (m, D) => {@@ -194,15 +196,15 @@ const BEATS = [const cam = camera(p, D, { box, x: 1200, y: 560, z: .45 });if (ride) cam.cut({ x: t => splitX(t) + 10 }, 0);const r = phRoad(cam, { hills: false });- phPerson(r.scenery, { shirt: PH.friend, x: 480, y: ROAD.side });- if (!ride) phPin(r.scenery, { x: 480, y: 296, s: 1.1 });- const truck = phTruck(r.vehicles, { y: ROAD.far, s: .85 }), car = phCar(r.vehicles, { y: ROAD.near });+ phPerson(r.scenery, { role: 'friend', x: 480, y: ROAD.side });+ if (!ride) phPin(r.scenery, { x: 480, y: 330, s: 1.1 });+ const truck = phTruck(r.vehicles, { y: ROAD.far }), 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 });+ const lab = phLabel(r.front, ride ? 'truck: 0 mph' : 'truck: 30 mph →', { y: 340, size: 56, fill: PH.truck });clockRun(t => {const x = splitX(t);put(truck, { x }); put(car, { x: x + 10 }); put(lab, { x: x + 10 });- phRoll(truck, x / .85); phRoll(car, x);+ phRoll(truck, x); phRoll(car, x);}, 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);@@ -226,7 +228,7 @@ const BEATS = [for (let i = 0; i < 40; 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);+ const rimRing = mk('rect', { x: -15, y: -36, width: 30, height: 40, rx: 6, fill: 'none', stroke: PH.ref, 'stroke-width': 2.5 }, you);put(rimRing, { o: 0 });const arcs = G(cam.world, { o: 0 });const arc = (a1, a2) => {@@ -266,7 +268,7 @@ const BEATS = [const train = phCarriage(p, { x: 720, y: GY, w: 1000 });put(train, { o: 0 }); show(train, .2);const wl = train._wins[2], wx = 720 + wl;- phCarWin(train, 2, { shirt: COL.int, hair: '#2d1e16', x: wl - 24, y: -96, s: .6 });+ phSprite(train, 'maya_seated', { x: wl - 20, y: -144 });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 });@@ -276,8 +278,8 @@ const BEATS = [phLabel(p, 'train: 60 mph →', { x: 520, y: GY - 300, size: 30, fill: COL.real });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 });+ phPerson(p, { role: 'friend', x: 1430, y: GY + 18 });+ phLabel(p, 'Leo, on the platform', { x: 1430, y: GY - 124, 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,diff --git a/site/science/img/CREDITS.md b/site/science/img/CREDITS.mdnew file mode 100644index 0000000..707e2cf--- /dev/null+++ b/site/science/img/CREDITS.md@@ -0,0 +1,11 @@+# Image credits++| File | Source | Author | Licence | Changes |+| --- | --- | --- | --- | --- |+| car.png | [Pixel Vehicle Pack](https://opengameart.org/content/pixel-vehicle-pack) (`rounded_green`) | Kenney ([kenney.nl](https://www.kenney.nl)) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) | Scaled ×8 (nearest neighbour) |+| truck.png | [Pixel Vehicle Pack](https://opengameart.org/content/pixel-vehicle-pack) (`truckdelivery`) | Kenney | CC0 1.0 | Cab recoloured blue, scaled ×8 |+| you*.png, friend*.png | [Pixel Vehicle Pack](https://opengameart.org/content/pixel-vehicle-pack) (`man`, `man_walk1`, `man_walk2`) | Kenney | CC0 1.0 | Shirt recoloured (lilac, coral), scaled ×8 |+| maya_seated.png | [Pixel Vehicle Pack](https://opengameart.org/content/pixel-vehicle-pack) (`woman_convertible`) | Kenney | CC0 1.0 | Top recoloured yellow, scaled ×8 |+| lamp.png | [Pixel Vehicle Pack](https://opengameart.org/content/pixel-vehicle-pack) (`light`) | Kenney | CC0 1.0 | Scaled ×8 |+| earth.png | [North Pole (orthographic projection with shade)](https://commons.wikimedia.org/wiki/File:North_Pole_(orthographic_projection_with_shade).svg) | Ecrusized (Wikimedia Commons) | Public domain | Land outline only (borders, graticule, rivers and shade removed), turned 160°, reduced to 96 × 96 cells, recoloured (ocean, land, Greenland and polar ice), scaled ×8 |+| tree.png, house.png | Papermorph (drawn for this book) | — | Same as the project | — |diff --git a/site/science/img/car.png b/site/science/img/car.pngnew file mode 100644index 0000000..b9fd7c3Binary files /dev/null and b/site/science/img/car.png differdiff --git a/site/science/img/earth.png b/site/science/img/earth.pngnew file mode 100644index 0000000..3875220Binary files /dev/null and b/site/science/img/earth.png differdiff --git a/site/science/img/friend.png b/site/science/img/friend.pngnew file mode 100644index 0000000..d00f5fbBinary files /dev/null and b/site/science/img/friend.png differdiff --git a/site/science/img/friend_walk1.png b/site/science/img/friend_walk1.pngnew file mode 100644index 0000000..51c75caBinary files /dev/null and b/site/science/img/friend_walk1.png differdiff --git a/site/science/img/friend_walk2.png b/site/science/img/friend_walk2.pngnew file mode 100644index 0000000..2c7f19aBinary files /dev/null and b/site/science/img/friend_walk2.png differdiff --git a/site/science/img/house.png b/site/science/img/house.pngnew file mode 100644index 0000000..25a26ccBinary files /dev/null and b/site/science/img/house.png differdiff --git a/site/science/img/lamp.png b/site/science/img/lamp.pngnew file mode 100644index 0000000..dec9e62Binary files /dev/null and b/site/science/img/lamp.png differdiff --git a/site/science/img/maya_seated.png b/site/science/img/maya_seated.pngnew file mode 100644index 0000000..5ec6a5dBinary files /dev/null and b/site/science/img/maya_seated.png differdiff --git a/site/science/img/tree.png b/site/science/img/tree.pngnew file mode 100644index 0000000..79110d5Binary files /dev/null and b/site/science/img/tree.png differdiff --git a/site/science/img/truck.png b/site/science/img/truck.pngnew file mode 100644index 0000000..8451122Binary files /dev/null and b/site/science/img/truck.png differdiff --git a/site/science/img/you.png b/site/science/img/you.pngnew file mode 100644index 0000000..e9f2748Binary files /dev/null and b/site/science/img/you.png differdiff --git a/site/science/img/you_walk1.png b/site/science/img/you_walk1.pngnew file mode 100644index 0000000..5791773Binary files /dev/null and b/site/science/img/you_walk1.png differdiff --git a/site/science/img/you_walk2.png b/site/science/img/you_walk2.pngnew file mode 100644index 0000000..ee72ed0Binary files /dev/null and b/site/science/img/you_walk2.png differdiff --git a/site/science/lib/figures.js b/site/science/lib/figures.jsindex 2d0feec..b79bb44 100644--- a/site/science/lib/figures.js+++ b/site/science/lib/figures.js@@ -1,26 +1,22 @@-// 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.+// Papermorph science book: the scene set. Load after engine.js.+// Style: pixel art. Vehicles, people, lamps, trees, houses and Earth are sprites in ../img/ (Kenney CC0 sprites and our+// own pixel art; credits in img/CREDITS.md), drawn at PX world px per sprite pixel with image-rendering: pixelated.+// Shapes that animate or teach stay SVG: road, wheel hubs, pins, labels, arrows, ball, stopwatch, notebook, Sun.// 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: '#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',+ out: '#14201c', road: '#3a4541', walk: '#55625b', grass: '#2d4636', line: '#ece8dc', hill1: '#22352d', hill2: '#283f34',+ glass: '#c8e0ff', metal: '#7d8a86', ocean: '#3f7fb0', land: '#4ea05a', sky: '#4f7f9c',you: COL.real, friend: COL.nat, truck: COL.whole, car: COL.irr, ref: COL.task, change: COL.rat,};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.+// A flat block with an optional lighter top band and darker right/bottom bands (and outline, ol = 0 for none).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);@@ -28,25 +24,6 @@ function phBlock(p, x, y, w, h, c, { top = 4, right = 0, bottom = 0, ol = OL } =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 } = {}) {@@ -55,7 +32,8 @@ function phLabel(p, str, { x = 0, y = 0, size = 30, fill = COL.chalk, anchor = 't.setAttribute('stroke-linejoin', 'round');return t;}-// Reference-point pin (an on-screen marker, not part of the world's look): the tip touches (0, 0).+const phText = (t, s) => { if (t.textContent !== s) t.textContent = s; }; // per-frame text: write only on change+// Reference-point pin (an on-screen marker): 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() });@@ -64,217 +42,80 @@ function phPin(p, o = {}) {return g;}-/* ---------- people ---------- */-// 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 } = {}) {+/* ---------- sprites ---------- */+const PX = 8; // world px per sprite pixel+const PH_SPR = { car: [32, 13], truck: [44, 24], lamp: [7, 27], tree: [16, 20], house: [18, 14], maya_seated: [7, 7],+ you: [9, 15], you_walk1: [7, 15], you_walk2: [11, 15], friend: [9, 15], friend_walk1: [7, 15], friend_walk2: [11, 15] };+function phSprite(p, name, { x = 0, y = 0, s = 1, o = 1 } = {}) {const g = G(p, { x, y, s, o });- 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 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;-}-// [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 }));-}-// 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);-}+ g._img = mk('image', { style: 'image-rendering: pixelated' }, g);+ phFrame(g, name);+ return g;+}+// Show another picture (animation frame) in a sprite; its bottom centre stays at the origin.+function phFrame(g, name) {+ if (g._name === name) return;+ const [w, h] = PH_SPR[name], a = { href: `../img/${name}.png`, x: -w * PX / 2, y: -h * PX, width: w * PX, height: h * PX };+ for (const k in a) g._img.setAttribute(k, a[k]);+ g._name = name;+}+// People (120 px tall): role 'you' (lilac) or 'friend' (coral). Poses: stand, walk (two frames per stride; phase in radians).+const PH_PERSON_H = 120;+const phPerson = (p, { role = 'you', ...o } = {}) => Object.assign(phSprite(p, role, o), { _role: role });+const phPose = (person, pose = 'stand', phase = 0) =>+ phFrame(person, pose === 'walk' ? `${person._role}_walk${((Math.floor(phase / Math.PI) % 2) + 2) % 2 + 1}` : person._role);-/* ---------- vehicles ---------- */-// 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 });- 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;+// Vehicles. Each wheel's 2×2 hub gets one light pixel that steps round it as the vehicle rolls (see phRoll).+const PH_HUBS = { car: [[3, 9], [17, 9]], truck: [[5, 20], [11, 20], [30, 20]] }; // hub top-left, in sprite px+function phVehicle(p, name, o) {+ const g = phSprite(p, name, o), [w, h] = PH_SPR[name];+ g._hubs = PH_HUBS[name].map(([hx, hy]) => {+ const x = -w * PX / 2 + hx * PX, y = -h * PX + hy * PX;+ mk('rect', { x, y, width: 2 * PX, height: 2 * PX, fill: '#3e4751' }, g);+ return Object.assign(mk('rect', { x, y, width: PX, height: PX, fill: PH.glass }, g), { _x: x, _y: y });+ });return g;}-const phRoll = (v, dist) => v._wheels.forEach(w => put(w, { r: dist / w._r * 180 / Math.PI }));-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 });- 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;+const phCar = (p, o) => phVehicle(p, 'car', o), phTruck = (p, o) => phVehicle(p, 'truck', o);+// Wheels turn with the distance rolled: a quarter turn per quarter of the wheel's circumference (radius 24), clockwise going right.+function phRoll(v, dist) {+ const k = ((Math.floor(dist / 24 / (Math.PI / 2)) % 4) + 4) % 4;+ if (k === v._k) return;+ const [dx, dy] = [[0, 0], [1, 0], [1, 1], [0, 1]][v._k = k];+ v._hubs.forEach(r => { r.setAttribute('x', r._x + dx * PX); r.setAttribute('y', r._y + dy * PX); });}-// 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 });- 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, 262 px tall; g._wins holds window centres (x). Window tops at y = -232.+// Passenger railway carriage in flat pixel blocks, `w` long, 278 px tall; g._wins holds window centres (x).function phCarriage(p, { x = 0, y = 0, w = 960, color = COL.real } = {}) {const g = G(p, { x, y });const x0 = -w / 2;- 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);+ phBlock(g, x0, -262, w, 222, color, { top: 0, bottom: 32, ol: 0 });+ phBlock(g, x0 + 16, -278, w - 32, 16, dk(color, .35), { top: 0, ol: 0 });+ mk('rect', { x: x0, y: -118, width: w, height: 8, fill: lt(color, .45) }, g);g._wins = [];for (let wx = x0 + 110; wx < w / 2 - 60; wx += 170) {- 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]]);+ mk('rect', { x: wx - 64, y: -232, width: 128, height: 96, fill: dk(color, .35) }, g);+ mk('rect', { x: wx - 56, y: -224, width: 112, height: 80, fill: PH.glass }, g);+ mk('rect', { x: wx + 16, y: -224, width: 16, height: 80, fill: '#ffffff', opacity: .5 }, g);g._wins.push(wx);}- path(g, `M${w / 2 - 34} -250V-46M${w / 2 - 6} -250V-46`, { stroke: dk(color, .4), 'stroke-width': 2 });+ mk('rect', { x: w / 2 - 40, y: -250, width: 32, height: 200, fill: dk(color, .2) }, g);[x0 + 110, w / 2 - 110].forEach(bx => {- phBlock(g, bx - 80, -46, 160, 14, '#3b4744', { top: 3, ol: 2 });- [-46, 46].forEach(o => phWheel(g, bx + o, -22, 20));+ mk('rect', { x: bx - 80, y: -48, width: 160, height: 16, fill: '#3b4744' }, g);+ [-46, 46].forEach(o => mk('rect', { x: bx + o - 16, y: -40, width: 32, height: 32, fill: '#3e4751' }, g));});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 ---------- */-// 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 phLamp(p, { x = 0, y = 0, s = 1 } = {}) {- const g = G(p, { x, y, s });- 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 });- 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;-}// 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' });+ return path(p, d + `V${y + depth}Z`, { fill, stroke: 'none' });}// 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:+// can run for any distance. 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 = [300, 1100, 1900], lamps = [1500], hills = true, x0 = -1600, x1 = 5600 } = {}) {@@ -282,25 +123,22 @@ function phRoad(cam, { trees = [300, 1100, 1900], lamps = [1500], hills = true,if (hills) phHills(cam.layer(.3, W), { y: 380, amp: 44, x0, x1, fill: PH.hill1, seed: 1 });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 }));+ rep(trees).forEach(x => phSprite(scenery, 'tree', { x, y: ROAD.side }));+ rep(lamps).forEach(x => phSprite(scenery, 'lamp', { 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));- });+ path(surface, rep(Array.from({ length: 15 }, (_, i) => i * 160)).map(x => `M${x} ${ROAD.mid - 4}h72v8h-72Z`).join(''), { fill: PH.line });+ phBlock(surface, x0, ROAD.bot, x1 - x0, 700, PH.grass, { top: 8, ol: 0 });+ path(surface, rep(Array.from({ length: 10 }, (_, i) => i * 240)).map(x => {+ const k = Math.round((((x % W) + W) % W) / 240), gx = x + 60 + (k % 2) * 70, gy = ROAD.bot + 56 + (k % 3) * 32;+ return `M${gx} ${gy}h8v-8h8v-8h8v16h8v-8h8v8Z`;+ }).join(''), { fill: lt(PH.grass, .14) });return { scenery, side, vehicles: G(cam.world), front: G(cam.world) };}/* ---------- objects ---------- */-// Soccer ball of radius r, centred on the origin: move the group, rotate ball._spin; shading and shadow stay put.+// Soccer ball of radius r, centred on the origin: move the group, rotate ball._spin; highlight and shadow stay put.function phBall(p, r = 40, o = {}) {const g = G(p, o);mk('ellipse', { cy: r, rx: r * .9, ry: r * .12, fill: '#000', opacity: .25 }, g);@@ -313,11 +151,7 @@ function phBall(p, r = 40, o = {}) {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', { cx: -r * .38, cy: -r * .42, r: r * .2, fill: '#fff', opacity: .55 }, g);mk('circle', { r, fill: 'none', ...phLine(3) }, g);g._spin = sp;return g;@@ -325,10 +159,9 @@ function phBall(p, r = 40, o = {}) {// 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 });- phBlock(g, -12, -r - 24, 24, 18, PH.lamp, { top: 4, ol: 2 });- mk('circle', { r: r + 9, fill: PH.lamp, ...phLine() }, g);+ phBlock(g, -12, -r - 24, 24, 18, PH.metal, { top: 4, ol: 2 });+ mk('circle', { r: r + 9, fill: PH.metal, ...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);@@ -363,47 +196,19 @@ function phSun(p, { x = 0, y = 0, r = 54 } = {}) {g._rays = rays;return g;}-// 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);- }-}-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 }), 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);+ const g = G(p, { x, y });+ mk('circle', { r, fill: PH.ocean, ...phLine(2) }, g);+ [[-.55, -.45, .45, .35], [.05, .1, .5, .4], [-.45, .25, .3, .25]].forEach(([lx, ly, w, h]) => mk('rect', { x: lx * r, y: ly * r, width: w * r, height: h * r, fill: PH.land }, g));return g;}-// 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: 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);+// Earth seen from far above the North Pole: the rim is the equator. One pixel image (img/earth.png, ocean included,+// turned so land is at the top of the rim); no SVG disc, which is slow in Firefox at close-up zoom.+// Rotate g._spin (degrees; negative = counterclockwise on screen).+function phEarthPolar(p, { x = 0, y = 0, R = 220 } = {}) {+ const g = G(p, { x, y }), spin = G(g);+ mk('image', { href: '../img/earth.png', x: -R, y: -R, width: 2 * R, height: 2 * R, style: 'image-rendering: pixelated' }, spin);g._spin = spin;return g;}