An AI skill that turns books into animated, narrated, interactive web experiences
JavaScript ai-agentsai-educationanimationclaudeclaude-codeclaude-skillsedtecheducationinteractive-textbookscormtext-to-video
fix(engine): review fixes for the shared engine
yzrfb2a8b4ff095
diff --git a/.claude/skills/papermorph/assets/engine/engine.css b/.claude/skills/papermorph/assets/engine/engine.cssindex fd097e4..05b6794 100644--- a/.claude/skills/papermorph/assets/engine/engine.css+++ b/.claude/skills/papermorph/assets/engine/engine.css@@ -175,6 +175,11 @@ svg.m { display: inline-block; overflow: visible; }.card.screen { display: flex; flex-direction: column; }.screen .head { order: 1; } .screen .body { order: 2; }.screen .fb { order: 3; max-width: 1080px; margin: 26px 0 0; font-size: 24px; } /* feedback right under the answers */+/* Practice screens: the bottom 130 px hold the buttons. Long answers scroll inside the body, so the feedback under+ them stays in view after a wrong answer adds a 'why' line to each row. */+.card.screen:not(.sort):not(.fin) { padding-bottom: 130px; }+.card.screen:not(.sort):not(.fin) > .head, .card.screen:not(.sort):not(.fin) > .fb { flex: none; }+.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }.screen .acts { position: absolute; right: 70px; bottom: 48px; }.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }diff --git a/.claude/skills/papermorph/assets/engine/engine.js b/.claude/skills/papermorph/assets/engine/engine.jsindex 84ba624..381c3e8 100644--- a/.claude/skills/papermorph/assets/engine/engine.js+++ b/.claude/skills/papermorph/assets/engine/engine.js@@ -85,7 +85,7 @@ const UI = '"Avenir Next","Segoe UI","Helvetica Neue",Arial,sans-serif';// Book settings (lib/book.css, next to engine.css and imported by it): --math-font for numbers and formulas,// --progress-key for the localStorage key the cover and chapters share.const BOOK = getComputedStyle(document.documentElement);-const bookSetting = name => BOOK.getPropertyValue(name).trim().replace(/^(["'])(.*)\1$/, '$2');+const bookSetting = name => BOOK.getPropertyValue(name).trim().replace(/^(["'])([^"']*)\1$/, '$2'); // unquote a lone string onlyconst MATH = bookSetting('--math-font') || '"STIX Two Text","Cambria Math","Iowan Old Style",Palatino,Georgia,serif';const NAME = { nat: 'Natural', whole: 'Whole', int: 'Integers', rat: 'Rational', irr: 'Irrational', real: 'Real' };// Card positions inside the 1600×900 picture.@@ -139,7 +139,7 @@ const F = (n, d) => ({ f: [String(n), String(d)] }); // fractionconst R = (x, i) => ({ r: x && x.f ? x : String(x), i: i && String(i) }); // root of a number or of F(n, d); i = index, e.g. 3 for a cube rootconst E = x => ({ sup: String(x) }); // exponent, raised after the part before itconst OV = x => ({ ov: String(x) }); // digits under a repeat bar: ['0.', OV('3')] is 0.333…-// Math expression from parts: 'text' | F(n,d) | R(x) | E(exp). Baseline at y; anchor start/middle/end.+// Math expression from parts: 'text' | F(n,d) | R(x) | E(exp) | OV(digits). Baseline at y; anchor start/middle/end.function M(parent, parts, { x = 0, y = 0, size = 36, fill = COL.chalk, anchor = 'middle', o = 1, s = 1 } = {}) {const g = G(parent, { x, y, o, s });const inner = mk('g', {}, g);@@ -945,7 +945,10 @@ function panel(t0) {}// Take a beat's old picture away on wall time: a 150 ms fade, then out of rendering. On the beat clock the fade would// wait for the new clip's audio to start (a few hundred ms in Firefox) with the old, frozen scene still showing.+// While seeking, the pictures passed on the way were never seen: they go at once.+let seeking = false;function retire(e) {+ if (seeking) { e.style.display = 'none'; return; }e.style.transition = 'opacity .15s linear';e.style.opacity = '0';setTimeout(() => { e.style.display = 'none'; }, 160);@@ -1280,7 +1283,11 @@ const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) =return { it, bs, mark, why, picked, el, played: false };});const single = rows.length === 1;- const showCur = () => rows.forEach((r, i) => r.el.classList.toggle('cur', !single && !locked && i === cur));+ const showCur = () => rows.forEach((r, i) => {+ const on = !single && !locked && i === cur;+ r.el.classList.toggle('cur', on);+ if (on && r.el.isConnected) r.el.scrollIntoView({ block: 'nearest' }); // a long practice list scrolls+ });showCur();body.append(wrap);const judge = r => {@@ -1574,8 +1581,11 @@ function clearCards() {// Rebuild the scene from scratch and fast-forward every earlier beat, so any step starts from its exact picture.function seek(i, play = P.playing) {stopAudio(); clearCards(); reset();- for (let k = 0; k < i; k++) { runBeat(k); evalTo(Infinity); }- start(i, play);+ seeking = true;+ try {+ for (let k = 0; k < i; k++) { runBeat(k); evalTo(Infinity); }+ start(i, play);+ } finally { seeking = false; }}function start(i, play) {stopAudio(); clearCards();@@ -1588,7 +1598,11 @@ function start(i, play) {// An aborted or superseded load (fast stepping, a reload) is not a failure: only a real error for the current clip// shows the note, and the note goes away as soon as a clip plays.a.onerror = () => { if (P.audio === a && a.error && a.error.code !== MediaError.MEDIA_ERR_ABORTED) soundFailed(); };- a.onplaying = () => { if (P.audio === a) $('soundNote').hidden = true; };+ a.onplaying = () => {+ if (P.audio !== a) return;+ $('soundNote').hidden = true;+ if (P.afail) { P.afail = false; a.currentTime = P.t; } // sound came back late: rejoin the picture where it is+ };const b = BEATS[i];if (b.ask) b.ask(() => { if (P.i === i) { evalTo(Infinity); start(i + 1, true); } });setPlaying(play);@@ -1619,7 +1633,7 @@ function frame(now) {// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.if (a && !P.afail && !a.ended) {if (a.currentTime > 0) { P.t = Math.max(P.t, a.currentTime); P.stall = 0; }- else if ((P.stall += dt) > 8 && a.readyState < 2) soundFailed(); // still nothing to play after 8 s+ else if ((P.stall += dt) > (a.readyState < 2 ? 8 : 15)) soundFailed(); // nothing to play after 8 s, or stuck for 15} else P.t += dt;evalTo(P.t);if (P.t >= P.end) beatDone();diff --git a/.claude/skills/papermorph/references/engine.md b/.claude/skills/papermorph/references/engine.mdindex 1eaac74..83e8e34 100644--- a/.claude/skills/papermorph/references/engine.md+++ b/.claude/skills/papermorph/references/engine.md@@ -4,7 +4,7 @@ A chapter page loads the engine and its `audio/<lang>/timings.js`, defines `CHAPEvery book keeps a copy of the shared engine in `lib/` (`engine.*`, `volume.*`), so the book runs on its own. Change the engine in `assets/engine/`, then copy it to the books with `python3 SKILL/scripts/sync_engine.py site` (`--check` lists copies that differ); a book's copy is not edited by hand. The book's own settings and style overrides live in `lib/book.css`, imported by `engine.css` and never overwritten: `--math-font` for numbers and formulas, `--progress-key` when the cover uses a key other than the default.-Many helpers are mathematical because the first book was algebra. Use those that fit, implement new ones in the chapter as needed, and share them in the book's engine when another chapter uses them.+Many helpers are mathematical because the first book was algebra. Use those that fit, implement new ones in the chapter as needed, and move what several chapters use into the book's own library file (`lib/<name>.js`), not into the engine copy.## Stage and state@@ -12,7 +12,7 @@ Many helpers are mathematical because the first book was algebra. Use those that- Every animated element carries a state object written to the DOM by `render()`: `x y` (translate), `s` (scale), `r` (rotate, degrees), `o` (opacity), `d` (stroke drawn, 0–1), `a_<attr>` (numeric attribute, e.g. `'a_stroke-width'`), `c_<attr>` (hex colour). Give a key a starting value before tweening it; an `a_`/`c_` key without one starts from the element's attribute.- Scale and rotation are about the element's own origin: draw around (0,0) and place with `x/y`.- `S` holds objects shared across beats (`S.panel`, `S.line`, …). `reset()` clears it, so anything a later beat needs must be stored on `S`.-- Palette `COL`: `chalk dim faint task good bad board` plus six hues `nat whole int rat irr real`. Fonts: `UI` (text), `MATH` (serif).+- Palette `COL`: `chalk dim faint task good bad board` plus six hues `nat whole int rat irr real`. Fonts: `UI` (text), `MATH` (serif unless the book's `--math-font` says otherwise).## Drawing@@ -91,7 +91,7 @@ quiz(position, [{ id: 'c-mean', prompt: ['Find the mean of ', $m('7, 3, 9'), '.'Space play/pause, ←/→ previous/next beat (Shift+←/→ during a question), Home restart, C captions, F full screen, ? help. `seek(i, play)` rebuilds and jumps; `start(i, play)` begins a beat from the current picture. While audio plays, the beat clock follows `audio.currentTime`. `?beat=N&t=S` in the URL opens a frozen frame for review. The finish card's Enter opens `CHAPTER.next`, R replays.-Progress uses the stable legacy key `animebook:progress:<book-path>` (keep it when renaming the project so existing readers retain their progress); the cover template reads the same key. Books served in different folders have separate saved places and completion lists.+Progress uses the stable legacy key `animebook:progress:<book-path>` (keep it when renaming the project so existing readers retain their progress); the cover template reads the same key. A book can set another key with `--progress-key` in `lib/book.css`; its cover must then read that key. Books served in different folders have separate saved places and completion lists.Treat saved progress as optional: malformed JSON, `null` or a non-object value starts with empty progress; only an array of positive integer chapter numbers is used for completion. Preserve each existing book's key when applying engine fixes.@@ -101,7 +101,7 @@ Keep `<link rel="expect" href="#bar" blocking="render">` in every chapter's head- `M()` italicises 1–2 letter lowercase runs (variables) and runs of capitals, but keeps common short English words upright (by, my, an, if, in, is, of, or, …; capitalised: By, If, In, …). In maths write `by`, `my`, `an`, `By` to keep them italic. Units inside `M()` turn italic too: write them as words in `T()` text ("meters"). Never put an English sentence in `M()`.- Prompts, `grid` text rows and `choice` options are HTML: plain strings render as text, so wrap math in `$m(...)`.-- Prefix chapter-specific helpers when their names might collide with engine globals (`rect`, `popIn`, `eqLine`, `fit`, `slot`, `Y0`, …); a redeclaration stops the page script.+- Prefix chapter-specific helpers when their names might collide with engine globals (`rect`, `popIn`, `eqLine`, `fit`, `slot`, `Y0`, `OV`, `camera`, `soft`, `clockRun`, `retire`, `seeking`, `BOOK`, `bookSetting`, …); a redeclaration stops the page script.- Drag handling: re-parent (append) the element before `setPointerCapture`; moving a node drops its capture.- A card whose content is too long for `BAND` should become several `blanks` rows in a wider side card.- When a question beat is continued early, the engine finishes the beat's pending tweens first; don't rely on half-finished fades.diff --git a/.claude/skills/papermorph/references/review.md b/.claude/skills/papermorph/references/review.mdindex 1287a0e..1cf289e 100644--- a/.claude/skills/papermorph/references/review.md+++ b/.claude/skills/papermorph/references/review.md@@ -29,6 +29,6 @@ Report the delivery result and any unresolved defects in a few lines. A new visuFor changes to playback, question types or grading, run existing tests that exercise the changed behavior. If needed, adapt `assets/templates/e2e.py` into `tools/<book>/e2e_chNN.py`; set `URL` to the actual chapter URL. Check the relevant wrong/right feedback, Show answer and continuation, or pause/replay/seek behavior. Shared timing/replay changes warrant the whole book's blank check and existing affected tests; ordinary new chapters use the delivery pass above.-For scoring changes, verify a wrong first attempt stays recorded after leaving and revisiting the question, and that restarting clears it. For progress changes, check book isolation and loading with missing or malformed stored data. Apply common fixes to the affected book engines and the Skill engine/template, preserving book-specific helpers and legacy keys.+For scoring changes, verify a wrong first attempt stays recorded after leaving and revisiting the question, and that restarting clears it. For progress changes, check book isolation and loading with missing or malformed stored data. Make common engine fixes in `assets/engine/` and copy them to the books with `scripts/sync_engine.py`; book-specific settings stay in each book's `lib/book.css`.Test details: keyboard first, plus mouse for changed drag handling; Enter checks and Esc leaves answer boxes; press Esc before S. Multi-row questions score per row. Use R on the finish card (Enter opens the next chapter). Python's server lacks Range requests, so let audio play naturally instead of setting `audio.currentTime`.diff --git a/.claude/skills/papermorph/scripts/sync_engine.py b/.claude/skills/papermorph/scripts/sync_engine.pyindex ae8ee66..c50e036 100644--- a/.claude/skills/papermorph/scripts/sync_engine.py+++ b/.claude/skills/papermorph/scripts/sync_engine.py@@ -19,7 +19,10 @@ OWN = 'book.css'def books(target: Path):if (target / 'lib' / 'engine.js').exists():return [target]- return sorted(p.parent.parent for p in target.glob('*/lib/engine.js')) or [target] # or a new book+ found = sorted(p.parent.parent for p in target.glob('*/lib/engine.js'))+ if not found and (target / 'lib').is_dir(): # e.g. site/ with the bookshelf's lib/, before any book exists+ sys.exit(f'{target}/lib has no engine.js and no book folder below it has one: pass the book folder itself')+ return found or [target] # or a new bookdef main():@@ -27,7 +30,7 @@ def main():ap.add_argument('target', type=Path, help='a book folder, or a folder of books')ap.add_argument('--check', action='store_true', help='only report differing copies')a = ap.parse_args()- shared = sorted(p for p in ENGINE.iterdir() if p.is_file() and p.name != OWN)+ shared = sorted(p for p in ENGINE.iterdir() if p.is_file() and p.name != OWN and not p.name.startswith('.'))stale = []for book in books(a.target):lib = book / 'lib'diff --git a/site/elementary-algebra/lib/engine.css b/site/elementary-algebra/lib/engine.cssindex fd097e4..05b6794 100644--- a/site/elementary-algebra/lib/engine.css+++ b/site/elementary-algebra/lib/engine.css@@ -175,6 +175,11 @@ svg.m { display: inline-block; overflow: visible; }.card.screen { display: flex; flex-direction: column; }.screen .head { order: 1; } .screen .body { order: 2; }.screen .fb { order: 3; max-width: 1080px; margin: 26px 0 0; font-size: 24px; } /* feedback right under the answers */+/* Practice screens: the bottom 130 px hold the buttons. Long answers scroll inside the body, so the feedback under+ them stays in view after a wrong answer adds a 'why' line to each row. */+.card.screen:not(.sort):not(.fin) { padding-bottom: 130px; }+.card.screen:not(.sort):not(.fin) > .head, .card.screen:not(.sort):not(.fin) > .fb { flex: none; }+.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }.screen .acts { position: absolute; right: 70px; bottom: 48px; }.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }diff --git a/site/elementary-algebra/lib/engine.js b/site/elementary-algebra/lib/engine.jsindex 84ba624..381c3e8 100644--- a/site/elementary-algebra/lib/engine.js+++ b/site/elementary-algebra/lib/engine.js@@ -85,7 +85,7 @@ const UI = '"Avenir Next","Segoe UI","Helvetica Neue",Arial,sans-serif';// Book settings (lib/book.css, next to engine.css and imported by it): --math-font for numbers and formulas,// --progress-key for the localStorage key the cover and chapters share.const BOOK = getComputedStyle(document.documentElement);-const bookSetting = name => BOOK.getPropertyValue(name).trim().replace(/^(["'])(.*)\1$/, '$2');+const bookSetting = name => BOOK.getPropertyValue(name).trim().replace(/^(["'])([^"']*)\1$/, '$2'); // unquote a lone string onlyconst MATH = bookSetting('--math-font') || '"STIX Two Text","Cambria Math","Iowan Old Style",Palatino,Georgia,serif';const NAME = { nat: 'Natural', whole: 'Whole', int: 'Integers', rat: 'Rational', irr: 'Irrational', real: 'Real' };// Card positions inside the 1600×900 picture.@@ -139,7 +139,7 @@ const F = (n, d) => ({ f: [String(n), String(d)] }); // fractionconst R = (x, i) => ({ r: x && x.f ? x : String(x), i: i && String(i) }); // root of a number or of F(n, d); i = index, e.g. 3 for a cube rootconst E = x => ({ sup: String(x) }); // exponent, raised after the part before itconst OV = x => ({ ov: String(x) }); // digits under a repeat bar: ['0.', OV('3')] is 0.333…-// Math expression from parts: 'text' | F(n,d) | R(x) | E(exp). Baseline at y; anchor start/middle/end.+// Math expression from parts: 'text' | F(n,d) | R(x) | E(exp) | OV(digits). Baseline at y; anchor start/middle/end.function M(parent, parts, { x = 0, y = 0, size = 36, fill = COL.chalk, anchor = 'middle', o = 1, s = 1 } = {}) {const g = G(parent, { x, y, o, s });const inner = mk('g', {}, g);@@ -945,7 +945,10 @@ function panel(t0) {}// Take a beat's old picture away on wall time: a 150 ms fade, then out of rendering. On the beat clock the fade would// wait for the new clip's audio to start (a few hundred ms in Firefox) with the old, frozen scene still showing.+// While seeking, the pictures passed on the way were never seen: they go at once.+let seeking = false;function retire(e) {+ if (seeking) { e.style.display = 'none'; return; }e.style.transition = 'opacity .15s linear';e.style.opacity = '0';setTimeout(() => { e.style.display = 'none'; }, 160);@@ -1280,7 +1283,11 @@ const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) =return { it, bs, mark, why, picked, el, played: false };});const single = rows.length === 1;- const showCur = () => rows.forEach((r, i) => r.el.classList.toggle('cur', !single && !locked && i === cur));+ const showCur = () => rows.forEach((r, i) => {+ const on = !single && !locked && i === cur;+ r.el.classList.toggle('cur', on);+ if (on && r.el.isConnected) r.el.scrollIntoView({ block: 'nearest' }); // a long practice list scrolls+ });showCur();body.append(wrap);const judge = r => {@@ -1574,8 +1581,11 @@ function clearCards() {// Rebuild the scene from scratch and fast-forward every earlier beat, so any step starts from its exact picture.function seek(i, play = P.playing) {stopAudio(); clearCards(); reset();- for (let k = 0; k < i; k++) { runBeat(k); evalTo(Infinity); }- start(i, play);+ seeking = true;+ try {+ for (let k = 0; k < i; k++) { runBeat(k); evalTo(Infinity); }+ start(i, play);+ } finally { seeking = false; }}function start(i, play) {stopAudio(); clearCards();@@ -1588,7 +1598,11 @@ function start(i, play) {// An aborted or superseded load (fast stepping, a reload) is not a failure: only a real error for the current clip// shows the note, and the note goes away as soon as a clip plays.a.onerror = () => { if (P.audio === a && a.error && a.error.code !== MediaError.MEDIA_ERR_ABORTED) soundFailed(); };- a.onplaying = () => { if (P.audio === a) $('soundNote').hidden = true; };+ a.onplaying = () => {+ if (P.audio !== a) return;+ $('soundNote').hidden = true;+ if (P.afail) { P.afail = false; a.currentTime = P.t; } // sound came back late: rejoin the picture where it is+ };const b = BEATS[i];if (b.ask) b.ask(() => { if (P.i === i) { evalTo(Infinity); start(i + 1, true); } });setPlaying(play);@@ -1619,7 +1633,7 @@ function frame(now) {// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.if (a && !P.afail && !a.ended) {if (a.currentTime > 0) { P.t = Math.max(P.t, a.currentTime); P.stall = 0; }- else if ((P.stall += dt) > 8 && a.readyState < 2) soundFailed(); // still nothing to play after 8 s+ else if ((P.stall += dt) > (a.readyState < 2 ? 8 : 15)) soundFailed(); // nothing to play after 8 s, or stuck for 15} else P.t += dt;evalTo(P.t);if (P.t >= P.end) beatDone();diff --git a/site/math-notebook/lib/engine.css b/site/math-notebook/lib/engine.cssindex fd097e4..05b6794 100644--- a/site/math-notebook/lib/engine.css+++ b/site/math-notebook/lib/engine.css@@ -175,6 +175,11 @@ svg.m { display: inline-block; overflow: visible; }.card.screen { display: flex; flex-direction: column; }.screen .head { order: 1; } .screen .body { order: 2; }.screen .fb { order: 3; max-width: 1080px; margin: 26px 0 0; font-size: 24px; } /* feedback right under the answers */+/* Practice screens: the bottom 130 px hold the buttons. Long answers scroll inside the body, so the feedback under+ them stays in view after a wrong answer adds a 'why' line to each row. */+.card.screen:not(.sort):not(.fin) { padding-bottom: 130px; }+.card.screen:not(.sort):not(.fin) > .head, .card.screen:not(.sort):not(.fin) > .fb { flex: none; }+.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }.screen .acts { position: absolute; right: 70px; bottom: 48px; }.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }diff --git a/site/math-notebook/lib/engine.js b/site/math-notebook/lib/engine.jsindex 84ba624..381c3e8 100644--- a/site/math-notebook/lib/engine.js+++ b/site/math-notebook/lib/engine.js@@ -85,7 +85,7 @@ const UI = '"Avenir Next","Segoe UI","Helvetica Neue",Arial,sans-serif';// Book settings (lib/book.css, next to engine.css and imported by it): --math-font for numbers and formulas,// --progress-key for the localStorage key the cover and chapters share.const BOOK = getComputedStyle(document.documentElement);-const bookSetting = name => BOOK.getPropertyValue(name).trim().replace(/^(["'])(.*)\1$/, '$2');+const bookSetting = name => BOOK.getPropertyValue(name).trim().replace(/^(["'])([^"']*)\1$/, '$2'); // unquote a lone string onlyconst MATH = bookSetting('--math-font') || '"STIX Two Text","Cambria Math","Iowan Old Style",Palatino,Georgia,serif';const NAME = { nat: 'Natural', whole: 'Whole', int: 'Integers', rat: 'Rational', irr: 'Irrational', real: 'Real' };// Card positions inside the 1600×900 picture.@@ -139,7 +139,7 @@ const F = (n, d) => ({ f: [String(n), String(d)] }); // fractionconst R = (x, i) => ({ r: x && x.f ? x : String(x), i: i && String(i) }); // root of a number or of F(n, d); i = index, e.g. 3 for a cube rootconst E = x => ({ sup: String(x) }); // exponent, raised after the part before itconst OV = x => ({ ov: String(x) }); // digits under a repeat bar: ['0.', OV('3')] is 0.333…-// Math expression from parts: 'text' | F(n,d) | R(x) | E(exp). Baseline at y; anchor start/middle/end.+// Math expression from parts: 'text' | F(n,d) | R(x) | E(exp) | OV(digits). Baseline at y; anchor start/middle/end.function M(parent, parts, { x = 0, y = 0, size = 36, fill = COL.chalk, anchor = 'middle', o = 1, s = 1 } = {}) {const g = G(parent, { x, y, o, s });const inner = mk('g', {}, g);@@ -945,7 +945,10 @@ function panel(t0) {}// Take a beat's old picture away on wall time: a 150 ms fade, then out of rendering. On the beat clock the fade would// wait for the new clip's audio to start (a few hundred ms in Firefox) with the old, frozen scene still showing.+// While seeking, the pictures passed on the way were never seen: they go at once.+let seeking = false;function retire(e) {+ if (seeking) { e.style.display = 'none'; return; }e.style.transition = 'opacity .15s linear';e.style.opacity = '0';setTimeout(() => { e.style.display = 'none'; }, 160);@@ -1280,7 +1283,11 @@ const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) =return { it, bs, mark, why, picked, el, played: false };});const single = rows.length === 1;- const showCur = () => rows.forEach((r, i) => r.el.classList.toggle('cur', !single && !locked && i === cur));+ const showCur = () => rows.forEach((r, i) => {+ const on = !single && !locked && i === cur;+ r.el.classList.toggle('cur', on);+ if (on && r.el.isConnected) r.el.scrollIntoView({ block: 'nearest' }); // a long practice list scrolls+ });showCur();body.append(wrap);const judge = r => {@@ -1574,8 +1581,11 @@ function clearCards() {// Rebuild the scene from scratch and fast-forward every earlier beat, so any step starts from its exact picture.function seek(i, play = P.playing) {stopAudio(); clearCards(); reset();- for (let k = 0; k < i; k++) { runBeat(k); evalTo(Infinity); }- start(i, play);+ seeking = true;+ try {+ for (let k = 0; k < i; k++) { runBeat(k); evalTo(Infinity); }+ start(i, play);+ } finally { seeking = false; }}function start(i, play) {stopAudio(); clearCards();@@ -1588,7 +1598,11 @@ function start(i, play) {// An aborted or superseded load (fast stepping, a reload) is not a failure: only a real error for the current clip// shows the note, and the note goes away as soon as a clip plays.a.onerror = () => { if (P.audio === a && a.error && a.error.code !== MediaError.MEDIA_ERR_ABORTED) soundFailed(); };- a.onplaying = () => { if (P.audio === a) $('soundNote').hidden = true; };+ a.onplaying = () => {+ if (P.audio !== a) return;+ $('soundNote').hidden = true;+ if (P.afail) { P.afail = false; a.currentTime = P.t; } // sound came back late: rejoin the picture where it is+ };const b = BEATS[i];if (b.ask) b.ask(() => { if (P.i === i) { evalTo(Infinity); start(i + 1, true); } });setPlaying(play);@@ -1619,7 +1633,7 @@ function frame(now) {// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.if (a && !P.afail && !a.ended) {if (a.currentTime > 0) { P.t = Math.max(P.t, a.currentTime); P.stall = 0; }- else if ((P.stall += dt) > 8 && a.readyState < 2) soundFailed(); // still nothing to play after 8 s+ else if ((P.stall += dt) > (a.readyState < 2 ? 8 : 15)) soundFailed(); // nothing to play after 8 s, or stuck for 15} else P.t += dt;evalTo(P.t);if (P.t >= P.end) beatDone();diff --git a/site/science/lib/engine.css b/site/science/lib/engine.cssindex fd097e4..05b6794 100644--- a/site/science/lib/engine.css+++ b/site/science/lib/engine.css@@ -175,6 +175,11 @@ svg.m { display: inline-block; overflow: visible; }.card.screen { display: flex; flex-direction: column; }.screen .head { order: 1; } .screen .body { order: 2; }.screen .fb { order: 3; max-width: 1080px; margin: 26px 0 0; font-size: 24px; } /* feedback right under the answers */+/* Practice screens: the bottom 130 px hold the buttons. Long answers scroll inside the body, so the feedback under+ them stays in view after a wrong answer adds a 'why' line to each row. */+.card.screen:not(.sort):not(.fin) { padding-bottom: 130px; }+.card.screen:not(.sort):not(.fin) > .head, .card.screen:not(.sort):not(.fin) > .fb { flex: none; }+.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }.screen .acts { position: absolute; right: 70px; bottom: 48px; }.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }diff --git a/site/science/lib/engine.js b/site/science/lib/engine.jsindex 84ba624..381c3e8 100644--- a/site/science/lib/engine.js+++ b/site/science/lib/engine.js@@ -85,7 +85,7 @@ const UI = '"Avenir Next","Segoe UI","Helvetica Neue",Arial,sans-serif';// Book settings (lib/book.css, next to engine.css and imported by it): --math-font for numbers and formulas,// --progress-key for the localStorage key the cover and chapters share.const BOOK = getComputedStyle(document.documentElement);-const bookSetting = name => BOOK.getPropertyValue(name).trim().replace(/^(["'])(.*)\1$/, '$2');+const bookSetting = name => BOOK.getPropertyValue(name).trim().replace(/^(["'])([^"']*)\1$/, '$2'); // unquote a lone string onlyconst MATH = bookSetting('--math-font') || '"STIX Two Text","Cambria Math","Iowan Old Style",Palatino,Georgia,serif';const NAME = { nat: 'Natural', whole: 'Whole', int: 'Integers', rat: 'Rational', irr: 'Irrational', real: 'Real' };// Card positions inside the 1600×900 picture.@@ -139,7 +139,7 @@ const F = (n, d) => ({ f: [String(n), String(d)] }); // fractionconst R = (x, i) => ({ r: x && x.f ? x : String(x), i: i && String(i) }); // root of a number or of F(n, d); i = index, e.g. 3 for a cube rootconst E = x => ({ sup: String(x) }); // exponent, raised after the part before itconst OV = x => ({ ov: String(x) }); // digits under a repeat bar: ['0.', OV('3')] is 0.333…-// Math expression from parts: 'text' | F(n,d) | R(x) | E(exp). Baseline at y; anchor start/middle/end.+// Math expression from parts: 'text' | F(n,d) | R(x) | E(exp) | OV(digits). Baseline at y; anchor start/middle/end.function M(parent, parts, { x = 0, y = 0, size = 36, fill = COL.chalk, anchor = 'middle', o = 1, s = 1 } = {}) {const g = G(parent, { x, y, o, s });const inner = mk('g', {}, g);@@ -945,7 +945,10 @@ function panel(t0) {}// Take a beat's old picture away on wall time: a 150 ms fade, then out of rendering. On the beat clock the fade would// wait for the new clip's audio to start (a few hundred ms in Firefox) with the old, frozen scene still showing.+// While seeking, the pictures passed on the way were never seen: they go at once.+let seeking = false;function retire(e) {+ if (seeking) { e.style.display = 'none'; return; }e.style.transition = 'opacity .15s linear';e.style.opacity = '0';setTimeout(() => { e.style.display = 'none'; }, 160);@@ -1280,7 +1283,11 @@ const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) =return { it, bs, mark, why, picked, el, played: false };});const single = rows.length === 1;- const showCur = () => rows.forEach((r, i) => r.el.classList.toggle('cur', !single && !locked && i === cur));+ const showCur = () => rows.forEach((r, i) => {+ const on = !single && !locked && i === cur;+ r.el.classList.toggle('cur', on);+ if (on && r.el.isConnected) r.el.scrollIntoView({ block: 'nearest' }); // a long practice list scrolls+ });showCur();body.append(wrap);const judge = r => {@@ -1574,8 +1581,11 @@ function clearCards() {// Rebuild the scene from scratch and fast-forward every earlier beat, so any step starts from its exact picture.function seek(i, play = P.playing) {stopAudio(); clearCards(); reset();- for (let k = 0; k < i; k++) { runBeat(k); evalTo(Infinity); }- start(i, play);+ seeking = true;+ try {+ for (let k = 0; k < i; k++) { runBeat(k); evalTo(Infinity); }+ start(i, play);+ } finally { seeking = false; }}function start(i, play) {stopAudio(); clearCards();@@ -1588,7 +1598,11 @@ function start(i, play) {// An aborted or superseded load (fast stepping, a reload) is not a failure: only a real error for the current clip// shows the note, and the note goes away as soon as a clip plays.a.onerror = () => { if (P.audio === a && a.error && a.error.code !== MediaError.MEDIA_ERR_ABORTED) soundFailed(); };- a.onplaying = () => { if (P.audio === a) $('soundNote').hidden = true; };+ a.onplaying = () => {+ if (P.audio !== a) return;+ $('soundNote').hidden = true;+ if (P.afail) { P.afail = false; a.currentTime = P.t; } // sound came back late: rejoin the picture where it is+ };const b = BEATS[i];if (b.ask) b.ask(() => { if (P.i === i) { evalTo(Infinity); start(i + 1, true); } });setPlaying(play);@@ -1619,7 +1633,7 @@ function frame(now) {// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.if (a && !P.afail && !a.ended) {if (a.currentTime > 0) { P.t = Math.max(P.t, a.currentTime); P.stall = 0; }- else if ((P.stall += dt) > 8 && a.readyState < 2) soundFailed(); // still nothing to play after 8 s+ else if ((P.stall += dt) > (a.readyState < 2 ? 8 : 15)) soundFailed(); // nothing to play after 8 s, or stuck for 15} else P.t += dt;evalTo(P.t);if (P.t >= P.end) beatDone();