An AI skill that turns books into animated, narrated, interactive web experiences
JavaScript ai-agentsai-educationanimationclaudeclaude-codeclaude-skillsedtecheducationinteractive-textbookscormtext-to-video
feat(engine): tapWords, a point-and-read ask beat for picture books
yzr7dba552d7de3
diff --git a/.claude/skills/papermorph/assets/templates/picture-book/chapter.html b/.claude/skills/papermorph/assets/templates/picture-book/chapter.htmlindex 3ed93ab..19da7b5 100644--- a/.claude/skills/papermorph/assets/templates/picture-book/chapter.html+++ b/.claude/skills/papermorph/assets/templates/picture-book/chapter.html@@ -28,7 +28,9 @@ try { if (localStorage.getItem('captions') === null) setCaptions(true, false); }/* ---------- beats: one per scene ----------[id, title, run, { lead: false, pre }]: id names audio/en/<id>.mp3 (the joined scene clip) and its TIMINGS entry;- `pre` is the seconds of picture before the speech starts (the scene's PRE constant). */+ `pre` is the seconds of picture before the speech starts (the scene's PRE constant).+ A point-and-read stop for young readers (references/picture-books/stories.md, "Point and read") is one more scene whose+ beat ends { …, ask: done => tapWords(THINGS, done) }, with film: true and questionKeys: false in CHAPTER. */const BEATS = [['scene1', 'The visit', (m, D) => scene1(panel(0), D), { lead: false, pre: PRE1 }],].map(([id, title, run, extra]) => ({ id, title, run, ...extra }));diff --git a/.claude/skills/papermorph/references/core/engine.md b/.claude/skills/papermorph/references/core/engine.mdindex 0134a48..ef5f686 100644--- a/.claude/skills/papermorph/references/core/engine.md+++ b/.claude/skills/papermorph/references/core/engine.md@@ -17,7 +17,7 @@ The maths helpers (number lines, tiles, fraction rows, planes, chance objects, t- Player: keys, captions, effects and music, progress- Traps-The question types and their API (`quiz()`, cards, grading) are in [lessons/questions.md](../lessons/questions.md); a film's interactive moment uses its own `ask` ([documentary.md](../films/documentary.md#interactive-moments)).+The question types and their API (`quiz()`, cards, grading) are in [lessons/questions.md](../lessons/questions.md); a film's interactive moment uses its own `ask` ([documentary.md](../films/documentary.md#interactive-moments)), and a picture book's point-and-read moment `tapWords(things, done)` ([picture-books/stories.md](../picture-books/stories.md#point-and-read)).## Stage and state@@ -81,7 +81,7 @@ A book whose formulas need more than `M()` parts (indices, sums, matrices, accen- **`pre`** in the fourth slot (`{ pre: 1.2 }`) runs the picture that many seconds before the narration starts: an action, then the line. Picture books use it on every scene.- **`minDur`** in the fourth slot (`{ minDur: 9.4 }`) is the least the whole beat lasts, hold included, as `?lengths` reports it: two mirrored beats (two answers treated alike) get the longer one's length.- **`dur`** in the fourth slot (`{ dur: 2 }`) makes a beat with no narration: no clip and no `TIMINGS` entry (the engine adds `{ dur, marks: {}, cues: [], silent: true }`), the picture runs `dur` seconds on wall time, then holds. A wordless card (a series card, an end card) needs no silence clip. A beat with a `TIMINGS` entry ignores `dur`.-- **`hold`** in the fourth slot of an `ask` beat in a film (`CHAPTER.film`) is read only by `export.py`: the seconds the video holds the waiting picture before it plays on as if the reader had acted (1.5 by default). The page ignores it.+- **`hold`** in the fourth slot of an `ask` beat in a film (`CHAPTER.film`) is read only by `export.py`: the seconds the video holds the waiting picture before it plays on as if the reader had acted (1.5 by default; a `tapWords` beat holds as long as its words take, [core/publish.md](publish.md#video-export)). The page ignores it.- **The end of a film**: a chapter whose last beat asks nothing (a picture book) is finished when that beat ends: it is marked done and, with `CHAPTER.next`, offers the next chapter (Enter).- **Seeking replays**: jumping to beat N rebuilds the scene and runs earlier beats instantly, so make `run` deterministic: create objects and schedule tweens through the timeline, with inputs from chapter data and replayed `S` state. Create interactions inside `ask`; use `fx`/`fxp` for answer effects.- Constants used in a `BEATS` literal are evaluated when it is defined, so define them above `BEATS`. Data used only inside an `ask` closure may sit below.@@ -129,7 +129,7 @@ Keep `<link rel="expect" href="#bar" blocking="render">` in every chapter's head- A `prog` or `clockRun` that redraws only when a key changes (a 12 fps frame number, a step index) must draw from the key's own value (`f / FPS`), not the exact `t` that crossed it: playing, seeking and `?beat=N&t=S` reach the same key at different `t`, and must show the same picture.- In a JS string, double each TeX backslash (`'\\frac{a}{b}'`) or use `` String.raw`\frac{a}{b}` ``: `'\text'` is a tab followed by "ext".- Write a formula wholly in TeX, an `eqLine` sign included (`sym: TX('\\le')`): TeX glyphs are smaller and lighter than `M()` text.-- **Global names.** The engine keeps its own state and helpers in a closure: a chapter may use any other name (`clock`, `fit`, `frame`, `last`, `ui`, `segs`, …; an older engine declared those too), and the engine calls its own functions: replacing one from outside (`window.start = …`) no longer reaches the engine; watch `P` instead. The names it puts in global scope are the API: `TRANSLATIONS PORTRAIT SW SH LANGUAGE translate DECIMAL_SIGN isShortcut NS UI BOOK bookSetting COL MATH NAME BAND RIGHT TOPR SCREEN SORT_SCREEN $m mk st put render G path T cite ctx2d textW F R E TX $tex texGraphic texPart OV M chip tokens layRow collapse mathEl io out back lin tw prog show hide draw pop pulse stream mix evalTo soft clockRun camera scene S AXIS X Y0 num reset tick dot arrow panel retire header fx fxp finishFx pulseFx shakeFx floatText TF $ kbd sources uiW SCORE h rich card mascotEl quiz qlayer keyAct choice mcq tap tapEls pickEls grid rat gcd lowest blanks SND P HOLD clearCards seek seekInto start setSpeed setPlaying setCaptions hideCover restart boot ENGINE seeking captions finishCard BOOK_PROGRESS_KEY progress`, plus the window properties `sfx music SFX SFX_BED BGM MathJax`. A chapter that declares one of them again stops with an error; prefix chapter helpers that would (`rect`, `popIn`, `eqLine`, `slot` from math.js too, when the chapter loads it). `lint.py` lists every clash, each name of a `const a = 1, b = 2` included. `finishCard` and `progress` stay assignable for `scorm-bridge.js`, which wraps them.+- **Global names.** The engine keeps its own state and helpers in a closure: a chapter may use any other name (`clock`, `fit`, `frame`, `last`, `ui`, `segs`, …; an older engine declared those too), and the engine calls its own functions: replacing one from outside (`window.start = …`) no longer reaches the engine; watch `P` instead. The names it puts in global scope are the API: `TRANSLATIONS PORTRAIT SW SH LANGUAGE translate DECIMAL_SIGN isShortcut NS UI BOOK bookSetting COL MATH NAME BAND RIGHT TOPR SCREEN SORT_SCREEN $m mk st put render G path T cite ctx2d textW F R E TX $tex texGraphic texPart OV M chip tokens layRow collapse mathEl io out back lin tw prog show hide draw pop pulse stream mix evalTo soft clockRun camera scene S AXIS X Y0 num reset tick dot arrow panel retire header fx fxp finishFx pulseFx shakeFx floatText TF $ kbd sources uiW SCORE h rich card mascotEl quiz qlayer keyAct choice mcq tap tapEls pickEls tapWords grid rat gcd lowest blanks SND P HOLD clearCards seek seekInto start setSpeed setPlaying setCaptions hideCover restart boot ENGINE seeking captions finishCard BOOK_PROGRESS_KEY progress`, plus the window properties `sfx music SFX SFX_BED BGM MathJax`. A chapter that declares one of them again stops with an error; prefix chapter helpers that would (`rect`, `popIn`, `eqLine`, `slot` from math.js too, when the chapter loads it). `lint.py` lists every clash, each name of a `const a = 1, b = 2` included. `finishCard` and `progress` stay assignable for `scorm-bridge.js`, which wraps them.- Dragging: 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/core/publish.md b/.claude/skills/papermorph/references/core/publish.mdindex caba6aa..a9ea895 100644--- a/.claude/skills/papermorph/references/core/publish.md+++ b/.claude/skills/papermorph/references/core/publish.md@@ -42,7 +42,7 @@ uv run --with playwright --with numpy SKILL/scripts/export.py chNN --url http://It renders the beats to `OUT/chNN.mp4` (1080p, or 1080 × 1920 on the upright stage; 30 fps) plus `OUT/chNN.srt` to upload with it. Frames come from seeking the beat clock and the sound is mixed by the page's own rules (each beat's `pre`, effects and music), so the video matches the page.-- A lesson's question beats are left out and its video ends on a card pointing to the interactive lesson. In a film with `CHAPTER.film` they stay: each holds its picture for the beat's `hold` s with the music up at 70 %, as the page plays it while waiting, then the next beat plays as if the reader had acted ([documentary.md](../films/documentary.md#interactive-moments)).+- A lesson's question beats are left out and its video ends on a card pointing to the interactive lesson. In a film with `CHAPTER.film` they stay: each holds its picture for the beat's `hold` s with the music up at 70 %, as the page plays it while waiting, then the next beat plays as if the reader had acted ([documentary.md](../films/documentary.md#interactive-moments)). A picture book's point-and-read beat (`tapWords`, [picture-books/stories.md](../picture-books/stories.md#point-and-read)) holds instead while it shows each word beside its thing and says it, one every two seconds, with the music ducked under each word, plus a second at the end (a `hold` set on the beat still wins); a word whose clip is missing is named in the run's output and left silent.- A mix that would clip is turned down as a whole, and the run says by how much; odd sizes are cut to even ones (an upright `--height 720` gives 404 × 720). The video is written beside the old one and renamed when done, so a failed run leaves an earlier `chNN.mp4` alone.- Frames are shot by `--jobs` browsers at once (default half the CPU threads, at most 8) on the GPU where there is one, and encoded on the first hardware H.264 encoder that works, else libx264; the run prints which. An 82 s upright short took 45 s on a 20-thread PC with an NVIDIA card, 1 min 22 s there with no GPU and libx264, and 3 min with one browser; `--height 720 --fps 10` makes a quick check copy.- Video files are large (tens of megabytes for a 1080p episode, more than a static host such as Cloudflare Pages takes per file): upload them to the video site, not into `site/`.diff --git a/.claude/skills/papermorph/references/picture-books/stories.md b/.claude/skills/papermorph/references/picture-books/stories.mdindex 975d362..3b7ddcb 100644--- a/.claude/skills/papermorph/references/picture-books/stories.md+++ b/.claude/skills/papermorph/references/picture-books/stories.md@@ -12,6 +12,7 @@ This file covers the story, the shots, the sound and the team; [puppets.md](pupp- The script comes first- Scenes, shots, cuts- Sound+- Point and read- Team- Review rounds- Delivering@@ -99,6 +100,36 @@ Voices, levels, effects and music work as in [core/sound.md](../core/sound.md);- **Effects and music** go on the scene clock (`sfx(scene, t, …)`, `music(…, [scene ids], …)`). When silence is the joke, `window.MUSIC_OFF = { scene: t => … }` fades the music out for the stare.- **Captions** are on at a reader's first visit (`chapter.html` does this); after that the reader's own choice is remembered. A caption ends at the first real pause after its words; a hole inside a sentence keeps it up. When that gives words away before they are said (a punchline after a pause), split the caption at the hole in `shots.js` and hold a punchline's caption into the silence after it; do it in the audio script after `join.py`, so a rerun keeps it.+## Point and read++A picture book for young children learning a language can stop once and let the child touch the things in the picture to hear their names, as a point-and-read pen does: Little Red Riding Hood stops in the woods, and a tap on the basket shows **basket** in big letters and says it. It suits a story read to children of about four to seven who are learning the words; it is a pause in the film, so one per episode is plenty, at a moment where the story rests (Red in the meadow once the wolf has gone), never inside a joke's timing. Offer it when the user's readers are young language learners; most picture books have none.++**The words.** Six to eight concrete nouns the child can see in that one picture and probably half knows: tree, flower, basket, bird, mushroom, butterfly. Each should be a whole thing drawn clearly apart from the others, so the child finds it and the word plainly belongs to it; a part too small to find (a coat's button) or an idea (*quiet*) has nothing to point at. The word is the whole lesson: a tap shows the word and says it once, with no sentence and no translation. The beat's narration is one prompt line in the narrator's voice that tells the child what to do ("Little Red Riding Hood saw so many things in the woods. Touch them to hear their names!").++**The beat.** It is the episode's one `ask` beat, with `tapWords(things, done)` as its ask, so the chapter is a film with a question: `film: true` in `CHAPTER` keeps its music and effects, and `questionKeys: false` keeps a lesson's question keys out of the help ([core/engine.md](../core/engine.md#player)). The scene fills the list each time it builds the picture; `g` is what the child taps: a puppet's `g`, a `prop()`, or, for something painted into a baked set, a transparent shape laid over it:++```js+let WOODS = []; // the things to tap, filled by scene4w+function scene4w(p, D) {+ const cam = paperCam(p, 'woods');+ const basket = prop(cam.stage, BASKET, { transform: 'translate(640 700)' });+ const tree = mk('rect', { x: 90, y: 120, width: 260, height: 560, fill: 'transparent' }, cam.stage); // painted into the set+ const red = kidPuppet(cam.stage, { coat: '#c94a3c' });+ WOODS = [{ g: tree, word: 'tree' }, { g: basket, word: 'basket' }, { g: red.g, word: 'girl' }];+ stopClock(D, (q, f) => { cam.apply(q, f); red.pose({ x: 900, y: 800, shut: q % 4 > 3.85 }, f); }); // blinks every 4 s, while it waits too+}+// in BEATS:+['scene4w', 'In the woods', (m, D) => scene4w(panel(0), D), { lead: false, pre: PRE4W, ask: done => tapWords(WOODS, done) }],+```++Once the prompt's narration is over the things turn tappable, each with a hit box at least 120 px square (a child's finger on a phone; smaller boxes lie on top of bigger ones). A tap hops the thing, shows its word beside it, above where there is room, and says the word with the music ducked; tapping again says it again. A counter and Continue (Enter) wait in the corner, and Continue lights up with a short praise once every thing has been heard; the child can go on at any time. ← and → move between the things and Enter or Space taps, and each thing is named by its word for a screen reader. While the page waits, the scene's `stopClock` runs on past the beat's end, so pose the puppets as a function of `q` that keeps living there: a breath, a blink every few seconds, a glance. A book with its own early-reader typeface sets `--word-font` in `lib/book.css`.++**The word clips** are plain narration entries, voiced by `tts.py` with the rest and in the same voice: `"w-basket": "basket"` in `narration.<lang>.json` makes `audio/<lang>/w-basket.mp3`. A thing's clip is `'w-' + its word` (lower case, spaces as hyphens: *ice cream* says `w-ice-cream`); `clip: 'w-red'` names another. They stay out of `join.py`'s list, since each plays alone; run `level.py` on them with the other clips.++**The video.** `export.py` cannot tap, so it shows and says the words in turn, as if a child were tapping them ([core/publish.md](../core/publish.md#video-export)).++**Check it** as a child would. `?beat=N` opens the beat; play its prompt and wait: nothing is tappable until the narration ends. Then tap every thing in the browser, by mouse and with the arrows and Enter: is every word beside its thing and not over it, does every word sound, does the counter count, does Continue light up and go on once? Step back into the beat and out of it mid-wait and replay it: no box, card or word may stay behind. `lint.py` names a word whose clip file is missing (it reads the `word: '…'` written in the scene, so write words as literals), and the page warns in the console about a thing with no word, an element that is not on the stage or not in the picture, and a clip that will not load, which `check.py` lists.+## Team[core/team.md](../core/team.md)'s shape, split by scene instead of by step. The **strong** coordinator writes the script with the user and builds what every scene shares: cast, sets and their painting, the episode's helpers, the voices and the joined audio with its word times (`join.py --words`). Then it writes one brief for all scene workers:diff --git a/.claude/skills/papermorph/scripts/export.py b/.claude/skills/papermorph/scripts/export.pyindex e5e502b..3bc3526 100644--- a/.claude/skills/papermorph/scripts/export.py+++ b/.claude/skills/papermorph/scripts/export.py@@ -11,7 +11,9 @@ of their scene, as a scene change stops them) and the BGM tracks with their croschecks and practice) are left out, and then the video ends on a card saying they are in the interactive lesson(--link adds its address); in a film with interactive moments (CHAPTER.film, e.g. the reader fires a rifle) they stay in,each holding its last picture for its `hold` seconds (1.5 by default, the music up at 70 % as the page plays it while-it waits) before the next beat plays as if the reader had acted. With --link and no question beats left out, the end+it waits) before the next beat plays as if the reader had acted. A point-and-read beat (tapWords) holds while it shows+and says each word in turn, 2 s apart (its `hold`, if set, still wins); a word clip that is missing is named and+left silent. With --link and no question beats left out, the endcard shows the title and the link only. A mix that would clip is turned down as a whole (the run prints by how much).Odd sizes (an upright --height 720 is 405 wide) are cut to even ones, as H.264 needs. The video is written toOUT/chNN.tmp.mp4 and renamed when done, so a failed run leaves an earlier OUT/chNN.mp4 alone. Captions are not burned@@ -120,7 +122,7 @@ async def main(a):if errs:sys.exit(f'page errors: {errs}')info = await pg.evaluate("""({lang: CHAPTER.language || 'en', film: !!CHAPTER.film,- beats: BEATS.map(b => ({id: b.id, ask: !!b.ask, pre: b.pre || 0, hold: b.hold ?? 1.5})),+ beats: BEATS.map(b => ({id: b.id, ask: !!b.ask, pre: b.pre || 0, hold: b.hold ?? null})),bgm: typeof BGM === 'undefined' ? [] : BGM.map(m => ({src: m.a.src, beats: m.beats, gain: m.gain, fade: m.fade ?? .8})),upInPre: window.MUSIC_UP_IN_PRE || false})""")# a film's question beats (CHAPTER.film: the reader acts, e.g. fires) stay in the video: their picture holds `hold` s@@ -135,11 +137,17 @@ async def main(a):# Pass 1: the sound track and the captions, beat by beat at the beat's length on screen.end_s = END_SECONDS if asks or a.link else 0 # a picture book ends on its own last scenelengths, starts, counts, music_off, cues, offset, waits, silent = [], [], [], [], [], 0.0, {}, set()+ words, spoken = {}, [] # a point-and-read beat (tapWords): its word clips and the seconds between them; when each is saidfor i, bid in beats:await open_beat(pg, base, i)end, T = await pg.evaluate('[P.end, TIMINGS[BEATS[P.i].id]]')if T.get('silent'): silent.add(bid) # { dur } in the beat's fourth slot: no clip, its sound is silence- if info['beats'][i]['ask']: waits[bid] = end; end += info['beats'][i]['hold'] # from P.end the page waits+ if info['beats'][i]['ask']: # from P.end the page waits+ waits[bid], hold = end, info['beats'][i]['hold']+ if demo := await pg.evaluate('P.tapDemo ? [P.tapDemo.clips, P.tapDemo.step] : null'):+ words[bid] = demo+ if hold is None: hold = len(demo[0]) * demo[1] + 1+ end += 1.5 if hold is None else hold# a scene lists its sfx() when its beat is built, so they are collected beat by beatfor e in await pg.evaluate(SFX_JS):if e not in info['sfx']:@@ -159,6 +167,11 @@ async def main(a):for k, (i, bid) in enumerate(beats):o, pre = starts[k], info['beats'][i]['pre']if bid not in silent: put(track, o + pre, decode(f'{a.url}{a.chapter}/audio/{info["lang"]}/{bid}.mp3'), 1, o + lengths[k])+ for n, name in enumerate(words.get(bid, [[]])[0]): # the words, said in turn while the picture holds+ src, at = f'{a.url}{a.chapter}/audio/{info["lang"]}/{name}.mp3', waits[bid] + n * words[bid][1]+ try: said = decode(src)+ except subprocess.CalledProcessError: print(f'{bid}: no word clip {src}: that word is silent'); continue+ put(track, o + at, said, 1, o + lengths[k]); spoken.append((bid, at, at + len(said) / RATE))for e in info['sfx']: # effects sit at half volume under the voices; a new scene stops themif e['beat'] == bid and 0 <= e['at'] < lengths[k]:put(track, o + e['at'], decode(e['src']), .5 * e['vol'], o + lengths[k])@@ -171,6 +184,8 @@ async def main(a):frames_off = [x for m in music_off for x in m]beat_len = {bid: lengths[k] for k, (_, bid) in enumerate(beats)}beat_pre = {x['id']: x['pre'] for x in info['beats']}+ # a word said while the picture holds ducks the music at once, as the page ducks it under a tapped word+ saying = lambda bid, t: any(b == bid and t0 <= t < t1 for b, t0, t1 in spoken)for m in info['bgm']:song, v, duck, pos = decode(m['src']), 0.0, .3, 0.0gain = np.zeros(n); at = np.zeros(n, int)@@ -179,8 +194,8 @@ async def main(a):current = bid in m['beats'] and not frames_off[f]v = max(0.0, min(1.0, v + (1 if current else -1) / fps / m['fade'])) if m['fade'] > 0 else float(current) # 0: a cutup = info['upInPre']- want = .7 if (up and (up is True or bid in up) and t < beat_pre[bid]) or t >= waits.get(bid, 1e9) else .3- duck += max(-1 / fps, min(1 / fps, want - duck))+ want = .7 if (up and (up is True or bid in up) and t < beat_pre[bid]) or t >= waits.get(bid, 1e9) and not saying(bid, t) else .3+ duck += max(-(4 if want < duck and saying(bid, t) else 1) / fps, min(1 / fps, want - duck))if current: pos = sum(beat_len.get(b, 0) for b in m['beats'][:m['beats'].index(bid)]) + telse: pos += 1 / fpsgain[f], at[f] = v * m['gain'] * duck, round(pos * RATE) % len(song)@@ -217,6 +232,9 @@ async def main(a):# depend on the frames before: each browser steps every frame since its last shot, as one browser wouldawait w.evaluate('ts => { for (const t of ts) { P.t = t; evalTo(t); } }',[g / a.fps for g in range(last[w] + 1, f + 1)])+ bid = info['beats'][i]['id']+ if bid in words and f / a.fps >= waits[bid]: # the words in turn, as if tapped+ await w.evaluate('s => P.tapDemo?.at(s)', f / a.fps - waits[bid])last[w] = freturn await w.screenshot(clip=clip, type='jpeg', quality=92)diff --git a/.claude/skills/papermorph/scripts/lint.py b/.claude/skills/papermorph/scripts/lint.pyindex d8f11b3..8ba1524 100644--- a/.claude/skills/papermorph/scripts/lint.py+++ b/.claude/skills/papermorph/scripts/lint.py@@ -15,7 +15,10 @@ Errors (exit status 1):mark m('name') is not a mark of any beatstale the narration's text differs from timings.js, or a narration [[mark]] is missing there: run tts.py againstyle style.json is not valid JSON or lacks schema, tier, book, words, look or voice-Warnings: a beat's m('name') not among that beat's own marks (beats built by shared helpers can mislead this);+ word a point-and-read thing (tapWords) has no clip: audio/<lang>/<clip>.mp3, w-<word>.mp3 by default (its word+ belongs in the narration as "w-<word>": "<word>", voiced by tts.py)+Warnings: a chapter that calls tapWords() without CHAPTER.film true, or with words not written as literals+ (word: 'basket'), whose clips were not checked; a beat's m('name') not among that beat's own marks (beats built by shared helpers can mislead this);a mark in timings.js that the narration lacks (added by hand: the next tts.py or eleven.py run drops it, so apage-only time belongs in the page); no beats recognised in a chapter; no narration file to check against.--content defaults to content/<book> (content/ when that is missing and content/chNN exists: the first book's legacy@@ -40,6 +43,7 @@ PARAMS = re.compile(r'function\s*[\w$]*\s*\(([^)]*)\)|\(([^()]*)\)\s*=>|([A-Za-zMETHOD = re.compile(r'^\s*([A-Za-z_$][\w$]*)\s*\([^()]*\)\s*\{', re.M) # name(a, b) { … } in an objectDESTRUCT = re.compile(r'(?:const|let|var)\s*([\[{][^;]*?[\]}])\s*(?:=|of\b|in\b)') # const {a, b: c, d = 1} = …, for (const [x, y] of …)DKEY = re.compile(r'[A-Za-z_$][\w$]*\s*:|=[^,\]}]*') # in a pattern, a key (b: c declares c) and a default (= 1) declare nothing+WORD = re.compile(r'(?<![\w$.])(word|clip)\s*:\s*([\'"`])') # a point-and-read thing's word: 'basket', clip: 'w-basket'USE = re.compile(r'(?<![\w$.])([A-Za-z_$][\w$]*)(?![\w$])(?!\s*:)') # a name, not a property or an object key@@ -210,6 +214,28 @@ def lint_chapter(ch, content, out):err('mark', f"m('{k}') in beat {b} is not a mark of any beat")elif b in T and k not in T[b].get('marks', {}):warn('mark', f"m('{k}') in beat {b} is not one of its marks")+ # point-and-read things (tapWords): each says its clip, the word's own unless clip: names another+ if any(re.search(r'(?<![\w$.])tapWords\s*\(', depth0(c)[0]) for c in own):+ if not re.search(r'\bfilm\s*[:=]\s*true', inline):+ warn('word', 'calls tapWords() but CHAPTER.film is not true: the chapter plays as a lesson, without music or effects')+ found, loose = 0, 0+ for c in own:+ code = depth0(c)[0]+ loose += len(re.findall(r'(?<![\w$.])word\s*:\s*(?![\'"`\s])', code)) # word: name, not a literal+ at = [(m.start(), m.group(1), c[m.end():c.index(m.group(2), m.end())]) for m in WORD.finditer(code)]+ for k, (pos, key, text) in enumerate(at):+ if key != 'word':+ continue+ found += 1+ # its clip: a clip: in the same object, between the braces around the word+ lo, hi = code.rfind('{', 0, pos), code.find('}', pos)+ clip = next((t for p, kk, t in at if kk == 'clip' and lo < p < hi), None)+ clip = clip or 'w-' + re.sub(r'[\W_]+', '-', text.lower()).strip('-') # as the engine names it+ if not (ch / 'audio' / lang / f'{clip}.mp3').exists():+ err('word', f'"{text}" has no audio/{lang}/{clip}.mp3: add "{clip}": "{text}" to the narration and run tts.py')+ if loose or not found:+ warn('word', f"calls tapWords() with {loose or 'no'} word{'s' if loose != 1 else ''} not written as word: 'literal': "+ "their clips were not checked (the page warns about a missing one when the beat starts)")# narration against the generated audionar = content / ch.name / f'narration.{lang}.json'if nar.exists():diff --git a/AGENTS.md b/AGENTS.mdindex d641830..82e25dc 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -29,5 +29,5 @@ CI (`.github/workflows/checks.yml`) runs these on every pull request; run them b- `python3 .github/scripts/check_skill.py` checks links and anchors, that `SKILL.md` links every reference and names every script, that scripts compile, and that flags and paths named in the docs exist.- `python3 .github/scripts/check_site.py` (needs Node) checks that every book's engine copy matches `engine/`, runs `lint.py` on every book, and runs `node --check` on every script, inline page scripts included.- `pipx run ruff check --select F,E9 --ignore F401 .claude/skills/papermorph/scripts .github engine` catches undefined names and other pyflakes errors in the Python tools.-- After an engine or page change, serve the site (`python3 .claude/skills/papermorph/scripts/serve.py 8765 site`), play the chapters you touched in a browser, and run `python3 .claude/skills/papermorph/scripts/lint.py site/<book>` and the engine's own checks (`uv run --with playwright engine/check_i18n.py`, `engine/check_latex.py`; `engine/check_volume.py` with the site served on 8767).+- After an engine or page change, serve the site (`python3 .claude/skills/papermorph/scripts/serve.py 8765 site`), play the chapters you touched in a browser, and run `python3 .claude/skills/papermorph/scripts/lint.py site/<book>` and the engine's own checks (`uv run --with playwright engine/check_i18n.py`, `engine/check_latex.py`, `engine/check_tapwords.py`; `engine/check_volume.py` with the site served on 8767).- After a large change to the Skill, have a fresh agent make a short film with the Skill alone, and sort what it trips on under the rules above. If a problem fits none of them, a rule is missing.diff --git a/engine/check_tapwords.py b/engine/check_tapwords.pynew file mode 100644index 0000000..0ba3b9e--- /dev/null+++ b/engine/check_tapwords.py@@ -0,0 +1,188 @@+#!/usr/bin/env python3+"""Point-and-read regression: a small film whose ask beat is tapWords(), played headless.++ uv run --with playwright engine/check_tapwords.py++No server is needed: the check answers every request itself with this engine, a page written here and real MP3s from+existing books (one word clip is left missing on purpose). It checks that the things turn tappable only once the+narration is over, that a tap shows the word beside the thing (not over it), says its clip and ducks the music, the+counter and Continue, the keys (arrows, Enter, Space), the warnings for a thing without a word, off the stage or+without a clip, the cleanup on a seek and a replay, that Continue goes on once, and the video's P.tapDemo.+"""+import asyncio+from pathlib import Path+from urllib.parse import urlparse++from playwright.async_api import async_playwright++ENGINE = Path(__file__).resolve().parent+SITE = ENGINE.parent / "site"+LONG = SITE / "math-notebook/ch01/audio/en/intro.mp3" # the music+WORD = SITE / "little-red-riding-hood/ch01/audio/en/s01c.mp3" # every word clip but one+PAGE = """<!doctype html>+<html lang="en">+<head><meta charset="utf-8"><title>tapWords check</title><link rel="stylesheet" href="../lib/engine.css"></head>+<body>+<script src="../lib/engine.js"></script>+<script>+'use strict';+const CHAPTER = { number: 1, title: 'Point and read', minutes: 1, language: 'en', film: true };+let THINGS = [];+function picture(p, D) {+ const sun = mk('circle', { cx: 100, cy: 100, r: 10, fill: '#f0b45a' }, p); // continuous motion: it runs on while the page waits+ clockRun(t => sun.setAttribute('cx', 100 + t * 10), D, { tail: true });+ const ball = mk('circle', { cx: 300, cy: 500, r: 60, fill: '#c94a3c' }, p);+ const tree = mk('rect', { x: 700, y: 150, width: 200, height: 600, fill: '#3e7f4c' }, p);+ const bee = mk('circle', { cx: 800, cy: 400, r: 12, fill: '#f0b45a' }, p); // small, in front of the tree+ const cup = mk('rect', { x: 1300, y: 40, width: 80, height: 60, fill: '#86c9e8' }, p); // at the top: its word goes below+ THINGS = [{ g: ball, word: 'ball' }, { g: tree, word: 'tree' }, { g: bee, word: 'bee', clip: 'w-bee-2' }, { g: cup, word: 'Ice cream' },+ { g: tree, word: '' }, { g: document.createElementNS(NS, 'g'), word: 'ghost' }];+}+music('audio/bgm/m.mp3', ['q', 'after', 'end'], .5);+const BEATS = [+ ['q', 'Look', (m, D) => picture(panel(0), D), { dur: 1, ask: done => tapWords(THINGS, done) }],+ ['after', 'After', () => {}, { dur: 6 }],+ ['end', 'End', () => {}, { dur: 6 }],+].map(([id, title, run, extra]) => ({ id, title, run, ...extra }));+boot();+</script>+</body>+</html>"""+++async def answer(route):+ path = urlparse(route.request.url).path.split("/__tapwords__/")[1]+ if path == "ch01/":+ await route.fulfill(content_type="text/html", body=PAGE)+ elif path.startswith("lib/"):+ await route.fulfill(path=ENGINE / path[4:])+ elif path == "ch01/audio/bgm/m.mp3":+ await route.fulfill(path=LONG)+ elif path.startswith("ch01/audio/en/w-") and path != "ch01/audio/en/w-ice-cream.mp3":+ await route.fulfill(path=WORD)+ else:+ await route.fulfill(status=404, body="")+++async def main():+ async with async_playwright() as p:+ browser = await p.chromium.launch(args=["--autoplay-policy=no-user-gesture-required"])+ page = await browser.new_page(viewport={"width": 1640, "height": 1000})+ errors, warnings, asked = [], [], []+ page.on("pageerror", lambda e: errors.append(str(e)))+ page.on("console", lambda m: (warnings if m.type == "warning" else errors).append(m.text)+ if m.type in ("warning", "error") and "404" not in m.text else None)+ page.on("request", lambda r: asked.append(urlparse(r.url).path.rsplit("/", 1)[-1]))+ await page.add_init_script("""window.PLAYED = [];+ const play = HTMLMediaElement.prototype.play;+ HTMLMediaElement.prototype.play = function () { PLAYED.push(this.src.split('/').pop()); return play.call(this); };""")+ await page.route("**/__tapwords__/**", answer)+ await page.goto("http://tapwords.invalid/__tapwords__/ch01/")+ await page.wait_for_function("typeof P !== 'undefined' && typeof BEATS !== 'undefined'")+ js = page.evaluate+ hits = "[...document.querySelectorAll('#stage .tapw .hit')]"+ count = "document.querySelector('.card.words .count')?.textContent"+ word = "document.querySelector('#ui .card.word')?.textContent"++ async def tap_at(selector_js):+ """A real click at the middle of a drawn thing."""+ r = await js(f"(() => {{ const r = {selector_js}.getBoundingClientRect(); return [r.x + r.width / 2, r.y + r.height / 2]; }})()")+ await page.mouse.click(*r)+ await page.wait_for_timeout(80)++ await page.locator("#cover").click()+ await page.wait_for_timeout(200)+ assert await js("P.playing && !P.waiting"), "the film did not start"+ assert await js(f"{hits}.length") == 0 and await js(count) is None, "things were tappable during the narration"+ await page.wait_for_function(f"P.waiting && {hits}.length === 4", timeout=5000)+ assert sorted(await js(f"{hits}.map(g => g.getAttribute('aria-label'))")) == ["Ice cream", "ball", "bee", "tree"]+ assert await js(f"{hits}.every(g => g.getAttribute('role') === 'button' && g.tabIndex === 0)")+ assert await js("document.getElementById('stage').getAttribute('role')") == "group", "the things are hidden from screen readers"+ assert await js(f"{hits}.every(g => {{ const r = g.getBBox(); return r.width >= 120 && r.height >= 120; }})"), "a hit box under 120 px"+ assert {"w-ball.mp3", "w-tree.mp3", "w-bee-2.mp3", "w-ice-cream.mp3"} <= set(asked), f"word clips not requested: {asked}"+ assert await js(count) == "0 / 4"+ assert "Continue" in await js("document.querySelector('.card.words button').textContent")+ assert await js("document.querySelector('.card.words button').className") == "btn quiet"+ print("PASS waiting: four things tappable after the narration, labelled, 120 px boxes, clips requested, 0 / 4, Continue")++ cx = await js("+document.querySelector('#scene circle').getAttribute('cx')")+ await page.wait_for_timeout(500)+ assert await js("SND.duck") > .6, "the music did not come up while waiting"+ assert await js("+document.querySelector('#scene circle').getAttribute('cx')") > cx + 3, "the scene's motion stopped while waiting"+ await tap_at("THINGS[0].g")+ assert await js(word) == "ball" and await js(count) == "1 / 4"+ assert "w-ball.mp3" in await js("PLAYED"), "the word was not said"+ assert await js("THINGS[0].g.getAnimations().length") > 0, "the thing did not hop"+ assert await js("""(() => { const a = document.querySelector('#ui .card.word').getBoundingClientRect(), b = THINGS[0].g.getBoundingClientRect();+ return a.right <= b.left || a.left >= b.right || a.bottom <= b.top || a.top >= b.bottom; })()"""), "the word covers the thing"+ await page.wait_for_timeout(250)+ assert await js("SND.duck") < .5, "the music did not duck under the word"+ await tap_at("THINGS[2].g") # the bee, in front of the tree: the smaller box is on top+ assert await js(word) == "bee" and await js(count) == "2 / 4"+ assert await js("PLAYED.at(-1)") == "w-bee-2.mp3"+ await tap_at("THINGS[0].g") # again: says it again, counts once+ assert await js("PLAYED.at(-1)") == "w-ball.mp3" and await js(count) == "2 / 4"+ print("PASS taps: motion runs on while waiting, word beside the thing, clip played, music ducked, hop, smaller thing on top, a second tap says it again")++ await page.keyboard.press("ArrowRight") # left to right: ball, tree, bee, ice cream+ await page.keyboard.press("ArrowRight")+ assert await js("document.activeElement.getAttribute('aria-label')") == "tree"+ await page.keyboard.press("Enter")+ await page.wait_for_timeout(80)+ assert await js(word) == "tree" and await js(count) == "3 / 4"+ await page.keyboard.press("ArrowRight")+ await page.keyboard.press("ArrowRight")+ await page.keyboard.press("Space")+ await page.wait_for_timeout(80)+ assert await js(word) == "Ice cream" and await js("P.i === 0 && P.waiting"), "Space did not tap"+ assert await js(count) == "You found them all!"+ assert await js("document.querySelector('.card.words button').className") == "btn go"+ assert await js("""(() => { const a = document.querySelector('#ui .card.word').getBoundingClientRect(), b = THINGS[3].g.getBoundingClientRect();+ return a.top >= b.bottom; })()"""), "a thing at the top did not get its word below"+ print("PASS keys: arrows move left to right, Enter and Space tap; all heard: praise and Continue lit")++ await page.wait_for_timeout(300)+ for w in ('a thing has no word', '"ghost" is not an element on the stage', '"Ice cream" has no clip at audio/en/w-ice-cream.mp3'):+ assert any(w in x for x in warnings), f"no warning: {w}\n{warnings}"+ print("PASS warnings: no word, not on the stage, missing clip")++ await js("seek(1, false)")+ await page.wait_for_timeout(100)+ left = await js("""[document.querySelectorAll('#stage .tapw').length, document.querySelectorAll('#ui .card').length,+ P.keys === null, P.tapDemo === undefined, SND.word === null]""")+ assert left == [0, 0, True, True, True], f"a seek away left {left}"+ for _ in range(2): # a replay, twice: one set of things, nothing counted from before+ await js("seek(0, true)")+ await page.wait_for_timeout(50)+ await page.wait_for_function(f"P.waiting && {hits}.length === 4", timeout=5000)+ assert await js("document.querySelectorAll('#stage .tapw').length") == 1 and await js(count) == "0 / 4"+ print("PASS cleanup: a seek away clears hit boxes, cards, keys and the word; a replay starts afresh")++ await js("document.activeElement.blur()")+ await page.keyboard.press("Enter")+ await page.keyboard.press("Enter")+ await page.wait_for_timeout(150)+ assert await js("P.i") == 1 and await js("document.querySelectorAll('#stage .tapw').length") == 0, "Continue did not go on once"+ print("PASS Continue: Enter goes on to the next beat once, before every thing was heard")++ await js("seek(0, false)")+ await page.wait_for_timeout(50)+ demo = await js("P.tapDemo && [P.tapDemo.clips, P.tapDemo.step]")+ assert demo == [["w-ball", "w-tree", "w-bee-2", "w-ice-cream"], 2], demo+ said = await js("PLAYED.length")+ await js("P.tapDemo.at(0)")+ assert await js(word) == "ball"+ await js("P.tapDemo.at(2.1)")+ assert await js(word) == "tree" and await js(count) == "2 / 4"+ await js("P.tapDemo.at(99)")+ assert await js(word) == "Ice cream" and await js(count) == "You found them all!"+ assert await js("PLAYED.length") == said, "the video's words played sound"+ print("PASS video: P.tapDemo lists the clips and shows the words in turn, silently")+ missing = await js("['en', 'ru', 'zh', 'ja'].flatMap(l => ['wordsHeard', 'allWordsHeard'].filter(k => !TRANSLATIONS[l][k]).map(k => l + '.' + k))")+ assert not missing, f"interface strings missing: {missing}"+ print("PASS interface strings in en, ru, zh and ja")+ assert not errors, errors+ await browser.close()+++asyncio.run(main())diff --git a/engine/engine.css b/engine/engine.cssindex 7fc7cc5..bf42cc9 100644--- a/engine/engine.css+++ b/engine/engine.css@@ -103,6 +103,15 @@ button[hidden] { display: none; }.hit:hover .ring, .hit:focus .ring { stroke-opacity: .7; }.hit.bad .ring { stroke: var(--bad); stroke-opacity: 1; }.hit.good .ring { stroke: var(--good); stroke-opacity: 1; stroke-width: 4; }+/* Point and read (tapWords): hit boxes over the picture, the word beside a tapped thing, the counter and Continue.+ A book can set --word-font for its early-reader letters. */+.tapw .ring { stroke-width: 5; }+#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-left-width: 1.5px; border-radius: 30px; font: 600 96px/1.15 var(--word-font, var(--ui)); white-space: nowrap; animation: word-in .3s cubic-bezier(.3, 1.5, .6, 1); }+@keyframes word-in { from { transform: scale(.6); opacity: 0; } }+.card.words .acts { margin: 0; gap: 18px; }+.card.words .count { font-size: 24px; font-weight: 600; color: var(--dim); }+.card.words.lit .count { color: var(--good); }+.card.words.lit .btn.go { animation: word-in .4s cubic-bezier(.3, 1.5, .6, 1); }.tok { cursor: grab; touch-action: none; } /* a finger drags the chip instead of scrolling the page *//* Controls strip, part of the frame. */diff --git a/engine/engine.js b/engine/engine.jsindex b59277b..8f8c9bd 100644--- a/engine/engine.js+++ b/engine/engine.js@@ -49,6 +49,7 @@ const TRANSLATIONS = {chapterComplete: 'Chapter {n} complete', quickChecks: 'Quick checks', chapterPractice: 'Chapter practice', sources: 'Sources in this chapter', opensSource: '{label}, opens the source in a new tab',firstTryScore: '{name}: {r} of {t} right on the first try.', notAttempted: '{name}: not attempted.',watchAgain: 'Watch again', nextChapter: 'Next chapter',+ wordsHeard: '{k} / {n}', allWordsHeard: 'You found them all!',},ru: {decimalSign: ',', stageLabel: 'Анимация урока', stageLabelTitled: 'Анимация урока: {title}',@@ -86,6 +87,7 @@ const TRANSLATIONS = {chapterComplete: 'Глава {n} пройдена', quickChecks: 'Проверь себя', chapterPractice: 'Практика по главе', sources: 'Источники этой главы', opensSource: '{label}, источник откроется в новой вкладке',firstTryScore: '{name}: {r} из {t} верно с первой попытки.', notAttempted: '{name}: не выполнялось.',watchAgain: 'Смотреть снова', nextChapter: 'Следующая глава',+ wordsHeard: '{k} / {n}', allWordsHeard: 'Все найдены!',},zh: {decimalSign: '.', stageLabel: '课程动画', stageLabelTitled: '课程动画:{title}',@@ -121,6 +123,7 @@ const TRANSLATIONS = {chapterComplete: '第 {n} 章完成', quickChecks: '小测', chapterPractice: '本章练习', sources: '本章引用的文献', opensSource: '{label},在新标签页中打开来源',firstTryScore: '{name}:第一次就答对 {r} / {t}。', notAttempted: '{name}:未作答。',watchAgain: '再看一遍', nextChapter: '下一章',+ wordsHeard: '{k} / {n}', allWordsHeard: '全都找到啦!',},ja: {decimalSign: '.', stageLabel: '授業アニメーション', stageLabelTitled: '授業アニメーション:{title}',@@ -156,6 +159,7 @@ const TRANSLATIONS = {chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',watchAgain: 'もう一度見る', nextChapter: '次の章へ',+ wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',},};// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.@@ -951,35 +955,42 @@ function mcq(id, onRight) {if (!it) throw new Error(`mcq: no item "${id}" in items.js`);return { id, prompt: it.prompt, build: choice(it.opts, it.right, it.why, it.no, onRight) };}-// The shared body of the stage tap questions: each target ring is a focusable button that answers on click or Enter,-// and ← / → move the focus between them. targets: [{ g, label, ok, wrong }], wrong() shakes and returns the explanation.-const stageTap = (api, targets, show, yes, onRight, hint) => {- let locked = false, cur = -1;- const gs = targets.map(({ g, label, ok, wrong }) => {+// Stage targets the reader taps: each g turns into a focusable button named by its label that acts on a click, Enter or+// Space. Returns key(e) for P.keys: ← / → move the focus between them (from the one focused, if any).+function tappable(items) { // [{ g, label, act }]+ for (const { g, label, act } of items) {g.classList.add('hit');g.setAttribute('tabindex', 0);g.setAttribute('role', 'button');g.setAttribute('aria-label', label);- const act = () => {- if (locked) return;- if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }- else { g.classList.add('bad'); api.grade(false, wrong()); }- };g.addEventListener('click', act);keyAct(g, act);- return g;- });+ }+ let cur = -1;+ return e => {+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;+ if (!d || !items.length) return false;+ const on = items.findIndex(it => it.g === document.activeElement);+ if (on >= 0) cur = on;+ cur = cur < 0 ? (d > 0 ? 0 : items.length - 1) : (cur + d + items.length) % items.length;+ items[cur].g.focus();+ return true;+ };+}+// The shared body of the stage tap questions: each target ring answers on click or Enter (tappable), wrong() shakes+// and returns the explanation. targets: [{ g, label, ok, wrong }].+const stageTap = (api, targets, show, yes, onRight, hint) => {+ let locked = false;+ const move = tappable(targets.map(({ g, label, ok, wrong }) => ({ g, label, act: () => {+ if (locked) return;+ if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }+ else { g.classList.add('bad'); api.grade(false, wrong()); }+ } })));return {- reveal() { gs[show].classList.add('good'); onRight?.(); return yes; },+ reveal() { targets[show].g.classList.add('good'); onRight?.(); return yes; },lock() { locked = true; },hint,- key(e) {- const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;- if (!d || locked) return false;- cur = cur < 0 ? (d > 0 ? 0 : gs.length - 1) : (cur + d + gs.length) % gs.length;- gs[cur].focus();- return true;- },+ key: e => !locked && move(e),};};// Tap a point on the stage number line.@@ -1015,6 +1026,113 @@ const pickEls = (items, right, yes, no, onRight) => (body, api) => {return stageTap(api, targets, right, yes, onRight,[kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClick')]);};+/* ---------- point and read ---------- */+// A picture book's point-and-read moment, for young children learning words: a film's ask beat (CHAPTER.film),+// ask: done => tapWords(THINGS, done) THINGS = [{ g, word: 'basket', clip: 'w-basket' }]+// which the scene fills as it builds the picture; g is what the reader taps (a puppet, a prop, or a transparent shape+// over part of a baked set) and clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default. Once the narration is+// over (P.waiting) every thing turns tappable: a tap hops it, shows its word in large letters beside it and says it, on+// wall time at the player volume with the music ducked under it; tapping again says it again. A counter and Continue+// (Enter) wait in the corner; Continue lights up once every thing has been heard. Seeking or replaying clears it all.+// While the page waits, the scene's continuous motion (stopClock, clockRun with tail: true) runs on past the beat's+// end on wall time, so puppets posed as a function of time keep breathing and blinking.+// The video (export.py) cannot tap: P.tapDemo.at(s) shows the words one by one, DEMO_STEP s apart, while it holds.+const TAP_MIN = 120, DEMO_STEP = 2; // TAP_MIN: the smallest hit box in stage px, a child's finger on a phone+const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');+function tapWords(things, done) {+ const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';+ if (!CHAPTER.film) console.warn('tapWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');+ const list = things.filter(t => {+ const why = !t.word ? 'has no word' : !(t.g instanceof SVGGraphicsElement && stage.contains(t.g)) ? 'is not an element on the stage' : '';+ if (why) console.warn(`tapWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);+ return !why;+ }).map(t => {+ const clip = t.clip || wordClip(t.word), a = new Audio(`audio/${lang}/${clip}.mp3`);+ a.preload = 'auto';+ a.onerror = () => console.warn(`tapWords: "${t.word}" has no clip at audio/${lang}/${clip}.mp3`);+ return { g: t.g, word: t.word, clip, a, heard: false };+ });+ const L = mk('g', { class: 'tapw' }, stage); // the hit boxes, over the whole picture+ let on = false, alive = true, finished = false, word = null, shown = -1, bar, count, go;+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least TAP_MIN square+ const box = t => {+ const s = stage.getBoundingClientRect(), r = t.g.getBoundingClientRect(), kx = SW / s.width, ky = SH / s.height;+ return { x: (r.left - s.left) * kx, y: (r.top - s.top) * ky, w: r.width * kx, h: r.height * ky };+ };+ const place = () => {+ for (const t of list) {+ const b = box(t), w = Math.max(TAP_MIN, b.w), hh = Math.max(TAP_MIN, b.h);+ for (const [k, v] of Object.entries({ x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, width: w, height: hh })) t.ring.setAttribute(k, v.toFixed(1));+ }+ };+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };+ const idle = s => { for (const w of TW) if (w.tail === true && w.dur) w.pf(1 + s / w.dur); }; // s seconds past the end+ let since = 0;+ const tapIt = (k, quiet) => {+ const t = list[k];+ shown = k;+ t.heard = true;+ const n = list.filter(x => x.heard).length, all = n === list.length;+ count.textContent = all ? translate('allWordsHeard') : translate('wordsHeard', { k: n, n: list.length });+ if (all && !go.classList.contains('go')) { go.className = 'btn go'; bar.classList.add('lit'); }+ // the word beside the thing: above it, else below, right or left, wherever it fits on the stage (slid along that+ // side to stay on it)+ word?.remove();+ word = h('div', 'card word', t.word);+ word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word+ $('ui').append(word);+ const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;+ const spots = [[cx, b.y - hh - m], [cx, b.y + b.h + m], [b.x + b.w + m, cy], [b.x - w - m, cy]];+ const [x, y] = spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];+ Object.assign(word.style, { left: Math.max(m, Math.min(SW - m - w, x)) + 'px', top: Math.max(m, Math.min(SH - m - hh, y)) + 'px' });+ if (quiet) { word.style.animation = go.style.animation = 'none'; return; } // a video's frame shows it at rest+ for (const x of list) if (x !== t) x.a.pause();+ t.a.currentTime = 0;+ t.a.volume = +($('volume')?.value ?? 1);+ t.a.play().catch(() => {});+ SND.word = t.a; // soundTick ducks the music under it+ t.g.animate([{ translate: '0 0' }, { translate: '0 -18px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });+ t.ring.animate([{ strokeOpacity: 1 }, { strokeOpacity: 0 }], { duration: 700, easing: 'ease-out' });+ };+ const enable = () => {+ on = true;+ since = performance.now();+ linkStage(); // the stage is no longer one picture to a screen reader: the things are its buttons+ const at = new Map(list.map(t => [t, box(t)]));+ for (const [t, b] of at) if (b.x + b.w <= 0 || b.x >= SW || b.y + b.h <= 0 || b.y >= SH || !b.w || !b.h) console.warn(`tapWords: "${t.word}" is not in the picture`);+ // smaller boxes on top, so a thing standing in front of a bigger one can still be tapped+ for (const t of [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h)) { t.hit = G(L); t.ring = mk('rect', { class: 'ring', rx: 22 }, t.hit); }+ place();+ // the arrows go through the things from left to right+ const order = [...list].sort((a, c) => at.get(a).x + at.get(a).w / 2 - at.get(c).x - at.get(c).w / 2);+ const move = tappable(order.map(t => ({ g: t.hit, label: t.word, act: () => { if (alive) tapIt(list.indexOf(t)); } })));+ P.keys = e => e.key === 'Enter' ? (finish(), true) : move(e);+ bar = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'words');+ Object.assign(bar.style, { left: 'auto', right: '40px', width: 'auto' });+ count = h('span', 'count', translate('wordsHeard', { k: 0, n: list.length }));+ count.setAttribute('role', 'status');+ go = h('button', 'btn quiet', [translate('continue'), kbd('Enter')]);+ go.onclick = finish;+ bar.append(h('div', 'acts', [count, go]));+ };+ const cleanup = () => {+ alive = false;+ L.remove();+ for (const t of list) { t.a.onerror = null; t.a.pause(); t.a.removeAttribute('src'); t.a.load(); if (SND.word === t.a) SND.word = null; }+ if (P.tapDemo?.gen === gen) delete P.tapDemo;+ };+ P.cleanup.push(cleanup);+ P.tapDemo = { gen, clips: list.map(t => t.clip), step: DEMO_STEP, at(s) { if (!on) enable(); idle(s); const k = Math.min(list.length - 1, Math.floor(s / DEMO_STEP)); if (k >= 0 && k !== shown) { list.slice(0, k).forEach(t => { t.heard = true; }); tapIt(k, true); } } };+ const tick = () => {+ if (!alive) return;+ if (P.gen !== gen) { cleanup(); return; } // a seek away, or the beat replayed+ if (P.waiting && !on) enable();+ if (on && P.waiting) idle((performance.now() - since) / 1000);+ if (on) place();+ requestAnimationFrame(tick);+ };+ requestAnimationFrame(tick);+}// Rows of toggles. items: { parts, ans: [keys], why }. parts are math parts (strings, F, R) unless text: true,// where they are rich text (strings and $m(...)). cols: [[key, label, colour?, shortcut?]]. multi: several per row.const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) => {@@ -1279,7 +1397,9 @@ function soundTick(dt) {if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);- if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));+ // a word the reader tapped (tapWords) ducks the music at once, as the narration does+ const saying = !!SND.word && !SND.word.paused && !SND.word.ended;+ if (!held) SND.duck += Math.max(-dt * (saying ? 4 : 1), Math.min(dt, (wait && !saying || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));for (const m of SND.bgm) {const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says@@ -1741,7 +1861,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isShR, E, TX, $tex, texGraphic, texPart, OV, M, chip, tokens, layRow, collapse, mathEl, io, out, back, lin, tw, prog,show, hide, draw, pop, pulse, stream, mix, evalTo, soft, clockRun, camera, scene, S, AXIS, X, Y0, num, reset, tick,dot, arrow, panel, retire, header, fx, fxp, finishFx, pulseFx, shakeFx, floatText, TF, $, kbd, sources, uiW, SCORE,- h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, grid, rat, gcd, lowest, blanks,+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, tapWords, grid, rat, gcd, lowest, blanks,SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,finishCard };})();@@ -1768,7 +1888,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGINuiW = ENGINE.uiW, SCORE = ENGINE.SCORE, h = ENGINE.h, rich = ENGINE.rich, card = ENGINE.card,mascotEl = ENGINE.mascotEl, quiz = ENGINE.quiz, qlayer = ENGINE.qlayer, keyAct = ENGINE.keyAct,choice = ENGINE.choice, mcq = ENGINE.mcq, tap = ENGINE.tap, tapEls = ENGINE.tapEls, pickEls = ENGINE.pickEls,- grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,+ tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,SND = ENGINE.SND, P = ENGINE.P, HOLD = ENGINE.HOLD, clearCards = ENGINE.clearCards, seek = ENGINE.seek,seekInto = ENGINE.seekInto, start = ENGINE.start, setSpeed = ENGINE.setSpeed, setPlaying = ENGINE.setPlaying,setCaptions = ENGINE.setCaptions, hideCover = ENGINE.hideCover, restart = ENGINE.restart, boot = ENGINE.boot;diff --git a/site/elementary-algebra/lib/engine.css b/site/elementary-algebra/lib/engine.cssindex 7fc7cc5..bf42cc9 100644--- a/site/elementary-algebra/lib/engine.css+++ b/site/elementary-algebra/lib/engine.css@@ -103,6 +103,15 @@ button[hidden] { display: none; }.hit:hover .ring, .hit:focus .ring { stroke-opacity: .7; }.hit.bad .ring { stroke: var(--bad); stroke-opacity: 1; }.hit.good .ring { stroke: var(--good); stroke-opacity: 1; stroke-width: 4; }+/* Point and read (tapWords): hit boxes over the picture, the word beside a tapped thing, the counter and Continue.+ A book can set --word-font for its early-reader letters. */+.tapw .ring { stroke-width: 5; }+#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-left-width: 1.5px; border-radius: 30px; font: 600 96px/1.15 var(--word-font, var(--ui)); white-space: nowrap; animation: word-in .3s cubic-bezier(.3, 1.5, .6, 1); }+@keyframes word-in { from { transform: scale(.6); opacity: 0; } }+.card.words .acts { margin: 0; gap: 18px; }+.card.words .count { font-size: 24px; font-weight: 600; color: var(--dim); }+.card.words.lit .count { color: var(--good); }+.card.words.lit .btn.go { animation: word-in .4s cubic-bezier(.3, 1.5, .6, 1); }.tok { cursor: grab; touch-action: none; } /* a finger drags the chip instead of scrolling the page *//* Controls strip, part of the frame. */diff --git a/site/elementary-algebra/lib/engine.js b/site/elementary-algebra/lib/engine.jsindex b59277b..8f8c9bd 100644--- a/site/elementary-algebra/lib/engine.js+++ b/site/elementary-algebra/lib/engine.js@@ -49,6 +49,7 @@ const TRANSLATIONS = {chapterComplete: 'Chapter {n} complete', quickChecks: 'Quick checks', chapterPractice: 'Chapter practice', sources: 'Sources in this chapter', opensSource: '{label}, opens the source in a new tab',firstTryScore: '{name}: {r} of {t} right on the first try.', notAttempted: '{name}: not attempted.',watchAgain: 'Watch again', nextChapter: 'Next chapter',+ wordsHeard: '{k} / {n}', allWordsHeard: 'You found them all!',},ru: {decimalSign: ',', stageLabel: 'Анимация урока', stageLabelTitled: 'Анимация урока: {title}',@@ -86,6 +87,7 @@ const TRANSLATIONS = {chapterComplete: 'Глава {n} пройдена', quickChecks: 'Проверь себя', chapterPractice: 'Практика по главе', sources: 'Источники этой главы', opensSource: '{label}, источник откроется в новой вкладке',firstTryScore: '{name}: {r} из {t} верно с первой попытки.', notAttempted: '{name}: не выполнялось.',watchAgain: 'Смотреть снова', nextChapter: 'Следующая глава',+ wordsHeard: '{k} / {n}', allWordsHeard: 'Все найдены!',},zh: {decimalSign: '.', stageLabel: '课程动画', stageLabelTitled: '课程动画:{title}',@@ -121,6 +123,7 @@ const TRANSLATIONS = {chapterComplete: '第 {n} 章完成', quickChecks: '小测', chapterPractice: '本章练习', sources: '本章引用的文献', opensSource: '{label},在新标签页中打开来源',firstTryScore: '{name}:第一次就答对 {r} / {t}。', notAttempted: '{name}:未作答。',watchAgain: '再看一遍', nextChapter: '下一章',+ wordsHeard: '{k} / {n}', allWordsHeard: '全都找到啦!',},ja: {decimalSign: '.', stageLabel: '授業アニメーション', stageLabelTitled: '授業アニメーション:{title}',@@ -156,6 +159,7 @@ const TRANSLATIONS = {chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',watchAgain: 'もう一度見る', nextChapter: '次の章へ',+ wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',},};// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.@@ -951,35 +955,42 @@ function mcq(id, onRight) {if (!it) throw new Error(`mcq: no item "${id}" in items.js`);return { id, prompt: it.prompt, build: choice(it.opts, it.right, it.why, it.no, onRight) };}-// The shared body of the stage tap questions: each target ring is a focusable button that answers on click or Enter,-// and ← / → move the focus between them. targets: [{ g, label, ok, wrong }], wrong() shakes and returns the explanation.-const stageTap = (api, targets, show, yes, onRight, hint) => {- let locked = false, cur = -1;- const gs = targets.map(({ g, label, ok, wrong }) => {+// Stage targets the reader taps: each g turns into a focusable button named by its label that acts on a click, Enter or+// Space. Returns key(e) for P.keys: ← / → move the focus between them (from the one focused, if any).+function tappable(items) { // [{ g, label, act }]+ for (const { g, label, act } of items) {g.classList.add('hit');g.setAttribute('tabindex', 0);g.setAttribute('role', 'button');g.setAttribute('aria-label', label);- const act = () => {- if (locked) return;- if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }- else { g.classList.add('bad'); api.grade(false, wrong()); }- };g.addEventListener('click', act);keyAct(g, act);- return g;- });+ }+ let cur = -1;+ return e => {+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;+ if (!d || !items.length) return false;+ const on = items.findIndex(it => it.g === document.activeElement);+ if (on >= 0) cur = on;+ cur = cur < 0 ? (d > 0 ? 0 : items.length - 1) : (cur + d + items.length) % items.length;+ items[cur].g.focus();+ return true;+ };+}+// The shared body of the stage tap questions: each target ring answers on click or Enter (tappable), wrong() shakes+// and returns the explanation. targets: [{ g, label, ok, wrong }].+const stageTap = (api, targets, show, yes, onRight, hint) => {+ let locked = false;+ const move = tappable(targets.map(({ g, label, ok, wrong }) => ({ g, label, act: () => {+ if (locked) return;+ if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }+ else { g.classList.add('bad'); api.grade(false, wrong()); }+ } })));return {- reveal() { gs[show].classList.add('good'); onRight?.(); return yes; },+ reveal() { targets[show].g.classList.add('good'); onRight?.(); return yes; },lock() { locked = true; },hint,- key(e) {- const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;- if (!d || locked) return false;- cur = cur < 0 ? (d > 0 ? 0 : gs.length - 1) : (cur + d + gs.length) % gs.length;- gs[cur].focus();- return true;- },+ key: e => !locked && move(e),};};// Tap a point on the stage number line.@@ -1015,6 +1026,113 @@ const pickEls = (items, right, yes, no, onRight) => (body, api) => {return stageTap(api, targets, right, yes, onRight,[kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClick')]);};+/* ---------- point and read ---------- */+// A picture book's point-and-read moment, for young children learning words: a film's ask beat (CHAPTER.film),+// ask: done => tapWords(THINGS, done) THINGS = [{ g, word: 'basket', clip: 'w-basket' }]+// which the scene fills as it builds the picture; g is what the reader taps (a puppet, a prop, or a transparent shape+// over part of a baked set) and clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default. Once the narration is+// over (P.waiting) every thing turns tappable: a tap hops it, shows its word in large letters beside it and says it, on+// wall time at the player volume with the music ducked under it; tapping again says it again. A counter and Continue+// (Enter) wait in the corner; Continue lights up once every thing has been heard. Seeking or replaying clears it all.+// While the page waits, the scene's continuous motion (stopClock, clockRun with tail: true) runs on past the beat's+// end on wall time, so puppets posed as a function of time keep breathing and blinking.+// The video (export.py) cannot tap: P.tapDemo.at(s) shows the words one by one, DEMO_STEP s apart, while it holds.+const TAP_MIN = 120, DEMO_STEP = 2; // TAP_MIN: the smallest hit box in stage px, a child's finger on a phone+const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');+function tapWords(things, done) {+ const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';+ if (!CHAPTER.film) console.warn('tapWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');+ const list = things.filter(t => {+ const why = !t.word ? 'has no word' : !(t.g instanceof SVGGraphicsElement && stage.contains(t.g)) ? 'is not an element on the stage' : '';+ if (why) console.warn(`tapWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);+ return !why;+ }).map(t => {+ const clip = t.clip || wordClip(t.word), a = new Audio(`audio/${lang}/${clip}.mp3`);+ a.preload = 'auto';+ a.onerror = () => console.warn(`tapWords: "${t.word}" has no clip at audio/${lang}/${clip}.mp3`);+ return { g: t.g, word: t.word, clip, a, heard: false };+ });+ const L = mk('g', { class: 'tapw' }, stage); // the hit boxes, over the whole picture+ let on = false, alive = true, finished = false, word = null, shown = -1, bar, count, go;+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least TAP_MIN square+ const box = t => {+ const s = stage.getBoundingClientRect(), r = t.g.getBoundingClientRect(), kx = SW / s.width, ky = SH / s.height;+ return { x: (r.left - s.left) * kx, y: (r.top - s.top) * ky, w: r.width * kx, h: r.height * ky };+ };+ const place = () => {+ for (const t of list) {+ const b = box(t), w = Math.max(TAP_MIN, b.w), hh = Math.max(TAP_MIN, b.h);+ for (const [k, v] of Object.entries({ x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, width: w, height: hh })) t.ring.setAttribute(k, v.toFixed(1));+ }+ };+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };+ const idle = s => { for (const w of TW) if (w.tail === true && w.dur) w.pf(1 + s / w.dur); }; // s seconds past the end+ let since = 0;+ const tapIt = (k, quiet) => {+ const t = list[k];+ shown = k;+ t.heard = true;+ const n = list.filter(x => x.heard).length, all = n === list.length;+ count.textContent = all ? translate('allWordsHeard') : translate('wordsHeard', { k: n, n: list.length });+ if (all && !go.classList.contains('go')) { go.className = 'btn go'; bar.classList.add('lit'); }+ // the word beside the thing: above it, else below, right or left, wherever it fits on the stage (slid along that+ // side to stay on it)+ word?.remove();+ word = h('div', 'card word', t.word);+ word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word+ $('ui').append(word);+ const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;+ const spots = [[cx, b.y - hh - m], [cx, b.y + b.h + m], [b.x + b.w + m, cy], [b.x - w - m, cy]];+ const [x, y] = spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];+ Object.assign(word.style, { left: Math.max(m, Math.min(SW - m - w, x)) + 'px', top: Math.max(m, Math.min(SH - m - hh, y)) + 'px' });+ if (quiet) { word.style.animation = go.style.animation = 'none'; return; } // a video's frame shows it at rest+ for (const x of list) if (x !== t) x.a.pause();+ t.a.currentTime = 0;+ t.a.volume = +($('volume')?.value ?? 1);+ t.a.play().catch(() => {});+ SND.word = t.a; // soundTick ducks the music under it+ t.g.animate([{ translate: '0 0' }, { translate: '0 -18px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });+ t.ring.animate([{ strokeOpacity: 1 }, { strokeOpacity: 0 }], { duration: 700, easing: 'ease-out' });+ };+ const enable = () => {+ on = true;+ since = performance.now();+ linkStage(); // the stage is no longer one picture to a screen reader: the things are its buttons+ const at = new Map(list.map(t => [t, box(t)]));+ for (const [t, b] of at) if (b.x + b.w <= 0 || b.x >= SW || b.y + b.h <= 0 || b.y >= SH || !b.w || !b.h) console.warn(`tapWords: "${t.word}" is not in the picture`);+ // smaller boxes on top, so a thing standing in front of a bigger one can still be tapped+ for (const t of [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h)) { t.hit = G(L); t.ring = mk('rect', { class: 'ring', rx: 22 }, t.hit); }+ place();+ // the arrows go through the things from left to right+ const order = [...list].sort((a, c) => at.get(a).x + at.get(a).w / 2 - at.get(c).x - at.get(c).w / 2);+ const move = tappable(order.map(t => ({ g: t.hit, label: t.word, act: () => { if (alive) tapIt(list.indexOf(t)); } })));+ P.keys = e => e.key === 'Enter' ? (finish(), true) : move(e);+ bar = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'words');+ Object.assign(bar.style, { left: 'auto', right: '40px', width: 'auto' });+ count = h('span', 'count', translate('wordsHeard', { k: 0, n: list.length }));+ count.setAttribute('role', 'status');+ go = h('button', 'btn quiet', [translate('continue'), kbd('Enter')]);+ go.onclick = finish;+ bar.append(h('div', 'acts', [count, go]));+ };+ const cleanup = () => {+ alive = false;+ L.remove();+ for (const t of list) { t.a.onerror = null; t.a.pause(); t.a.removeAttribute('src'); t.a.load(); if (SND.word === t.a) SND.word = null; }+ if (P.tapDemo?.gen === gen) delete P.tapDemo;+ };+ P.cleanup.push(cleanup);+ P.tapDemo = { gen, clips: list.map(t => t.clip), step: DEMO_STEP, at(s) { if (!on) enable(); idle(s); const k = Math.min(list.length - 1, Math.floor(s / DEMO_STEP)); if (k >= 0 && k !== shown) { list.slice(0, k).forEach(t => { t.heard = true; }); tapIt(k, true); } } };+ const tick = () => {+ if (!alive) return;+ if (P.gen !== gen) { cleanup(); return; } // a seek away, or the beat replayed+ if (P.waiting && !on) enable();+ if (on && P.waiting) idle((performance.now() - since) / 1000);+ if (on) place();+ requestAnimationFrame(tick);+ };+ requestAnimationFrame(tick);+}// Rows of toggles. items: { parts, ans: [keys], why }. parts are math parts (strings, F, R) unless text: true,// where they are rich text (strings and $m(...)). cols: [[key, label, colour?, shortcut?]]. multi: several per row.const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) => {@@ -1279,7 +1397,9 @@ function soundTick(dt) {if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);- if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));+ // a word the reader tapped (tapWords) ducks the music at once, as the narration does+ const saying = !!SND.word && !SND.word.paused && !SND.word.ended;+ if (!held) SND.duck += Math.max(-dt * (saying ? 4 : 1), Math.min(dt, (wait && !saying || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));for (const m of SND.bgm) {const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says@@ -1741,7 +1861,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isShR, E, TX, $tex, texGraphic, texPart, OV, M, chip, tokens, layRow, collapse, mathEl, io, out, back, lin, tw, prog,show, hide, draw, pop, pulse, stream, mix, evalTo, soft, clockRun, camera, scene, S, AXIS, X, Y0, num, reset, tick,dot, arrow, panel, retire, header, fx, fxp, finishFx, pulseFx, shakeFx, floatText, TF, $, kbd, sources, uiW, SCORE,- h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, grid, rat, gcd, lowest, blanks,+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, tapWords, grid, rat, gcd, lowest, blanks,SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,finishCard };})();@@ -1768,7 +1888,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGINuiW = ENGINE.uiW, SCORE = ENGINE.SCORE, h = ENGINE.h, rich = ENGINE.rich, card = ENGINE.card,mascotEl = ENGINE.mascotEl, quiz = ENGINE.quiz, qlayer = ENGINE.qlayer, keyAct = ENGINE.keyAct,choice = ENGINE.choice, mcq = ENGINE.mcq, tap = ENGINE.tap, tapEls = ENGINE.tapEls, pickEls = ENGINE.pickEls,- grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,+ tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,SND = ENGINE.SND, P = ENGINE.P, HOLD = ENGINE.HOLD, clearCards = ENGINE.clearCards, seek = ENGINE.seek,seekInto = ENGINE.seekInto, start = ENGINE.start, setSpeed = ENGINE.setSpeed, setPlaying = ENGINE.setPlaying,setCaptions = ENGINE.setCaptions, hideCover = ENGINE.hideCover, restart = ENGINE.restart, boot = ENGINE.boot;diff --git a/site/joke-collection/lib/engine.css b/site/joke-collection/lib/engine.cssindex 7fc7cc5..bf42cc9 100644--- a/site/joke-collection/lib/engine.css+++ b/site/joke-collection/lib/engine.css@@ -103,6 +103,15 @@ button[hidden] { display: none; }.hit:hover .ring, .hit:focus .ring { stroke-opacity: .7; }.hit.bad .ring { stroke: var(--bad); stroke-opacity: 1; }.hit.good .ring { stroke: var(--good); stroke-opacity: 1; stroke-width: 4; }+/* Point and read (tapWords): hit boxes over the picture, the word beside a tapped thing, the counter and Continue.+ A book can set --word-font for its early-reader letters. */+.tapw .ring { stroke-width: 5; }+#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-left-width: 1.5px; border-radius: 30px; font: 600 96px/1.15 var(--word-font, var(--ui)); white-space: nowrap; animation: word-in .3s cubic-bezier(.3, 1.5, .6, 1); }+@keyframes word-in { from { transform: scale(.6); opacity: 0; } }+.card.words .acts { margin: 0; gap: 18px; }+.card.words .count { font-size: 24px; font-weight: 600; color: var(--dim); }+.card.words.lit .count { color: var(--good); }+.card.words.lit .btn.go { animation: word-in .4s cubic-bezier(.3, 1.5, .6, 1); }.tok { cursor: grab; touch-action: none; } /* a finger drags the chip instead of scrolling the page *//* Controls strip, part of the frame. */diff --git a/site/joke-collection/lib/engine.js b/site/joke-collection/lib/engine.jsindex b59277b..8f8c9bd 100644--- a/site/joke-collection/lib/engine.js+++ b/site/joke-collection/lib/engine.js@@ -49,6 +49,7 @@ const TRANSLATIONS = {chapterComplete: 'Chapter {n} complete', quickChecks: 'Quick checks', chapterPractice: 'Chapter practice', sources: 'Sources in this chapter', opensSource: '{label}, opens the source in a new tab',firstTryScore: '{name}: {r} of {t} right on the first try.', notAttempted: '{name}: not attempted.',watchAgain: 'Watch again', nextChapter: 'Next chapter',+ wordsHeard: '{k} / {n}', allWordsHeard: 'You found them all!',},ru: {decimalSign: ',', stageLabel: 'Анимация урока', stageLabelTitled: 'Анимация урока: {title}',@@ -86,6 +87,7 @@ const TRANSLATIONS = {chapterComplete: 'Глава {n} пройдена', quickChecks: 'Проверь себя', chapterPractice: 'Практика по главе', sources: 'Источники этой главы', opensSource: '{label}, источник откроется в новой вкладке',firstTryScore: '{name}: {r} из {t} верно с первой попытки.', notAttempted: '{name}: не выполнялось.',watchAgain: 'Смотреть снова', nextChapter: 'Следующая глава',+ wordsHeard: '{k} / {n}', allWordsHeard: 'Все найдены!',},zh: {decimalSign: '.', stageLabel: '课程动画', stageLabelTitled: '课程动画:{title}',@@ -121,6 +123,7 @@ const TRANSLATIONS = {chapterComplete: '第 {n} 章完成', quickChecks: '小测', chapterPractice: '本章练习', sources: '本章引用的文献', opensSource: '{label},在新标签页中打开来源',firstTryScore: '{name}:第一次就答对 {r} / {t}。', notAttempted: '{name}:未作答。',watchAgain: '再看一遍', nextChapter: '下一章',+ wordsHeard: '{k} / {n}', allWordsHeard: '全都找到啦!',},ja: {decimalSign: '.', stageLabel: '授業アニメーション', stageLabelTitled: '授業アニメーション:{title}',@@ -156,6 +159,7 @@ const TRANSLATIONS = {chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',watchAgain: 'もう一度見る', nextChapter: '次の章へ',+ wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',},};// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.@@ -951,35 +955,42 @@ function mcq(id, onRight) {if (!it) throw new Error(`mcq: no item "${id}" in items.js`);return { id, prompt: it.prompt, build: choice(it.opts, it.right, it.why, it.no, onRight) };}-// The shared body of the stage tap questions: each target ring is a focusable button that answers on click or Enter,-// and ← / → move the focus between them. targets: [{ g, label, ok, wrong }], wrong() shakes and returns the explanation.-const stageTap = (api, targets, show, yes, onRight, hint) => {- let locked = false, cur = -1;- const gs = targets.map(({ g, label, ok, wrong }) => {+// Stage targets the reader taps: each g turns into a focusable button named by its label that acts on a click, Enter or+// Space. Returns key(e) for P.keys: ← / → move the focus between them (from the one focused, if any).+function tappable(items) { // [{ g, label, act }]+ for (const { g, label, act } of items) {g.classList.add('hit');g.setAttribute('tabindex', 0);g.setAttribute('role', 'button');g.setAttribute('aria-label', label);- const act = () => {- if (locked) return;- if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }- else { g.classList.add('bad'); api.grade(false, wrong()); }- };g.addEventListener('click', act);keyAct(g, act);- return g;- });+ }+ let cur = -1;+ return e => {+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;+ if (!d || !items.length) return false;+ const on = items.findIndex(it => it.g === document.activeElement);+ if (on >= 0) cur = on;+ cur = cur < 0 ? (d > 0 ? 0 : items.length - 1) : (cur + d + items.length) % items.length;+ items[cur].g.focus();+ return true;+ };+}+// The shared body of the stage tap questions: each target ring answers on click or Enter (tappable), wrong() shakes+// and returns the explanation. targets: [{ g, label, ok, wrong }].+const stageTap = (api, targets, show, yes, onRight, hint) => {+ let locked = false;+ const move = tappable(targets.map(({ g, label, ok, wrong }) => ({ g, label, act: () => {+ if (locked) return;+ if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }+ else { g.classList.add('bad'); api.grade(false, wrong()); }+ } })));return {- reveal() { gs[show].classList.add('good'); onRight?.(); return yes; },+ reveal() { targets[show].g.classList.add('good'); onRight?.(); return yes; },lock() { locked = true; },hint,- key(e) {- const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;- if (!d || locked) return false;- cur = cur < 0 ? (d > 0 ? 0 : gs.length - 1) : (cur + d + gs.length) % gs.length;- gs[cur].focus();- return true;- },+ key: e => !locked && move(e),};};// Tap a point on the stage number line.@@ -1015,6 +1026,113 @@ const pickEls = (items, right, yes, no, onRight) => (body, api) => {return stageTap(api, targets, right, yes, onRight,[kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClick')]);};+/* ---------- point and read ---------- */+// A picture book's point-and-read moment, for young children learning words: a film's ask beat (CHAPTER.film),+// ask: done => tapWords(THINGS, done) THINGS = [{ g, word: 'basket', clip: 'w-basket' }]+// which the scene fills as it builds the picture; g is what the reader taps (a puppet, a prop, or a transparent shape+// over part of a baked set) and clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default. Once the narration is+// over (P.waiting) every thing turns tappable: a tap hops it, shows its word in large letters beside it and says it, on+// wall time at the player volume with the music ducked under it; tapping again says it again. A counter and Continue+// (Enter) wait in the corner; Continue lights up once every thing has been heard. Seeking or replaying clears it all.+// While the page waits, the scene's continuous motion (stopClock, clockRun with tail: true) runs on past the beat's+// end on wall time, so puppets posed as a function of time keep breathing and blinking.+// The video (export.py) cannot tap: P.tapDemo.at(s) shows the words one by one, DEMO_STEP s apart, while it holds.+const TAP_MIN = 120, DEMO_STEP = 2; // TAP_MIN: the smallest hit box in stage px, a child's finger on a phone+const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');+function tapWords(things, done) {+ const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';+ if (!CHAPTER.film) console.warn('tapWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');+ const list = things.filter(t => {+ const why = !t.word ? 'has no word' : !(t.g instanceof SVGGraphicsElement && stage.contains(t.g)) ? 'is not an element on the stage' : '';+ if (why) console.warn(`tapWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);+ return !why;+ }).map(t => {+ const clip = t.clip || wordClip(t.word), a = new Audio(`audio/${lang}/${clip}.mp3`);+ a.preload = 'auto';+ a.onerror = () => console.warn(`tapWords: "${t.word}" has no clip at audio/${lang}/${clip}.mp3`);+ return { g: t.g, word: t.word, clip, a, heard: false };+ });+ const L = mk('g', { class: 'tapw' }, stage); // the hit boxes, over the whole picture+ let on = false, alive = true, finished = false, word = null, shown = -1, bar, count, go;+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least TAP_MIN square+ const box = t => {+ const s = stage.getBoundingClientRect(), r = t.g.getBoundingClientRect(), kx = SW / s.width, ky = SH / s.height;+ return { x: (r.left - s.left) * kx, y: (r.top - s.top) * ky, w: r.width * kx, h: r.height * ky };+ };+ const place = () => {+ for (const t of list) {+ const b = box(t), w = Math.max(TAP_MIN, b.w), hh = Math.max(TAP_MIN, b.h);+ for (const [k, v] of Object.entries({ x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, width: w, height: hh })) t.ring.setAttribute(k, v.toFixed(1));+ }+ };+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };+ const idle = s => { for (const w of TW) if (w.tail === true && w.dur) w.pf(1 + s / w.dur); }; // s seconds past the end+ let since = 0;+ const tapIt = (k, quiet) => {+ const t = list[k];+ shown = k;+ t.heard = true;+ const n = list.filter(x => x.heard).length, all = n === list.length;+ count.textContent = all ? translate('allWordsHeard') : translate('wordsHeard', { k: n, n: list.length });+ if (all && !go.classList.contains('go')) { go.className = 'btn go'; bar.classList.add('lit'); }+ // the word beside the thing: above it, else below, right or left, wherever it fits on the stage (slid along that+ // side to stay on it)+ word?.remove();+ word = h('div', 'card word', t.word);+ word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word+ $('ui').append(word);+ const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;+ const spots = [[cx, b.y - hh - m], [cx, b.y + b.h + m], [b.x + b.w + m, cy], [b.x - w - m, cy]];+ const [x, y] = spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];+ Object.assign(word.style, { left: Math.max(m, Math.min(SW - m - w, x)) + 'px', top: Math.max(m, Math.min(SH - m - hh, y)) + 'px' });+ if (quiet) { word.style.animation = go.style.animation = 'none'; return; } // a video's frame shows it at rest+ for (const x of list) if (x !== t) x.a.pause();+ t.a.currentTime = 0;+ t.a.volume = +($('volume')?.value ?? 1);+ t.a.play().catch(() => {});+ SND.word = t.a; // soundTick ducks the music under it+ t.g.animate([{ translate: '0 0' }, { translate: '0 -18px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });+ t.ring.animate([{ strokeOpacity: 1 }, { strokeOpacity: 0 }], { duration: 700, easing: 'ease-out' });+ };+ const enable = () => {+ on = true;+ since = performance.now();+ linkStage(); // the stage is no longer one picture to a screen reader: the things are its buttons+ const at = new Map(list.map(t => [t, box(t)]));+ for (const [t, b] of at) if (b.x + b.w <= 0 || b.x >= SW || b.y + b.h <= 0 || b.y >= SH || !b.w || !b.h) console.warn(`tapWords: "${t.word}" is not in the picture`);+ // smaller boxes on top, so a thing standing in front of a bigger one can still be tapped+ for (const t of [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h)) { t.hit = G(L); t.ring = mk('rect', { class: 'ring', rx: 22 }, t.hit); }+ place();+ // the arrows go through the things from left to right+ const order = [...list].sort((a, c) => at.get(a).x + at.get(a).w / 2 - at.get(c).x - at.get(c).w / 2);+ const move = tappable(order.map(t => ({ g: t.hit, label: t.word, act: () => { if (alive) tapIt(list.indexOf(t)); } })));+ P.keys = e => e.key === 'Enter' ? (finish(), true) : move(e);+ bar = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'words');+ Object.assign(bar.style, { left: 'auto', right: '40px', width: 'auto' });+ count = h('span', 'count', translate('wordsHeard', { k: 0, n: list.length }));+ count.setAttribute('role', 'status');+ go = h('button', 'btn quiet', [translate('continue'), kbd('Enter')]);+ go.onclick = finish;+ bar.append(h('div', 'acts', [count, go]));+ };+ const cleanup = () => {+ alive = false;+ L.remove();+ for (const t of list) { t.a.onerror = null; t.a.pause(); t.a.removeAttribute('src'); t.a.load(); if (SND.word === t.a) SND.word = null; }+ if (P.tapDemo?.gen === gen) delete P.tapDemo;+ };+ P.cleanup.push(cleanup);+ P.tapDemo = { gen, clips: list.map(t => t.clip), step: DEMO_STEP, at(s) { if (!on) enable(); idle(s); const k = Math.min(list.length - 1, Math.floor(s / DEMO_STEP)); if (k >= 0 && k !== shown) { list.slice(0, k).forEach(t => { t.heard = true; }); tapIt(k, true); } } };+ const tick = () => {+ if (!alive) return;+ if (P.gen !== gen) { cleanup(); return; } // a seek away, or the beat replayed+ if (P.waiting && !on) enable();+ if (on && P.waiting) idle((performance.now() - since) / 1000);+ if (on) place();+ requestAnimationFrame(tick);+ };+ requestAnimationFrame(tick);+}// Rows of toggles. items: { parts, ans: [keys], why }. parts are math parts (strings, F, R) unless text: true,// where they are rich text (strings and $m(...)). cols: [[key, label, colour?, shortcut?]]. multi: several per row.const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) => {@@ -1279,7 +1397,9 @@ function soundTick(dt) {if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);- if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));+ // a word the reader tapped (tapWords) ducks the music at once, as the narration does+ const saying = !!SND.word && !SND.word.paused && !SND.word.ended;+ if (!held) SND.duck += Math.max(-dt * (saying ? 4 : 1), Math.min(dt, (wait && !saying || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));for (const m of SND.bgm) {const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says@@ -1741,7 +1861,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isShR, E, TX, $tex, texGraphic, texPart, OV, M, chip, tokens, layRow, collapse, mathEl, io, out, back, lin, tw, prog,show, hide, draw, pop, pulse, stream, mix, evalTo, soft, clockRun, camera, scene, S, AXIS, X, Y0, num, reset, tick,dot, arrow, panel, retire, header, fx, fxp, finishFx, pulseFx, shakeFx, floatText, TF, $, kbd, sources, uiW, SCORE,- h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, grid, rat, gcd, lowest, blanks,+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, tapWords, grid, rat, gcd, lowest, blanks,SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,finishCard };})();@@ -1768,7 +1888,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGINuiW = ENGINE.uiW, SCORE = ENGINE.SCORE, h = ENGINE.h, rich = ENGINE.rich, card = ENGINE.card,mascotEl = ENGINE.mascotEl, quiz = ENGINE.quiz, qlayer = ENGINE.qlayer, keyAct = ENGINE.keyAct,choice = ENGINE.choice, mcq = ENGINE.mcq, tap = ENGINE.tap, tapEls = ENGINE.tapEls, pickEls = ENGINE.pickEls,- grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,+ tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,SND = ENGINE.SND, P = ENGINE.P, HOLD = ENGINE.HOLD, clearCards = ENGINE.clearCards, seek = ENGINE.seek,seekInto = ENGINE.seekInto, start = ENGINE.start, setSpeed = ENGINE.setSpeed, setPlaying = ENGINE.setPlaying,setCaptions = ENGINE.setCaptions, hideCover = ENGINE.hideCover, restart = ENGINE.restart, boot = ENGINE.boot;diff --git a/site/little-red-riding-hood/lib/engine.css b/site/little-red-riding-hood/lib/engine.cssindex 7fc7cc5..bf42cc9 100644--- a/site/little-red-riding-hood/lib/engine.css+++ b/site/little-red-riding-hood/lib/engine.css@@ -103,6 +103,15 @@ button[hidden] { display: none; }.hit:hover .ring, .hit:focus .ring { stroke-opacity: .7; }.hit.bad .ring { stroke: var(--bad); stroke-opacity: 1; }.hit.good .ring { stroke: var(--good); stroke-opacity: 1; stroke-width: 4; }+/* Point and read (tapWords): hit boxes over the picture, the word beside a tapped thing, the counter and Continue.+ A book can set --word-font for its early-reader letters. */+.tapw .ring { stroke-width: 5; }+#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-left-width: 1.5px; border-radius: 30px; font: 600 96px/1.15 var(--word-font, var(--ui)); white-space: nowrap; animation: word-in .3s cubic-bezier(.3, 1.5, .6, 1); }+@keyframes word-in { from { transform: scale(.6); opacity: 0; } }+.card.words .acts { margin: 0; gap: 18px; }+.card.words .count { font-size: 24px; font-weight: 600; color: var(--dim); }+.card.words.lit .count { color: var(--good); }+.card.words.lit .btn.go { animation: word-in .4s cubic-bezier(.3, 1.5, .6, 1); }.tok { cursor: grab; touch-action: none; } /* a finger drags the chip instead of scrolling the page *//* Controls strip, part of the frame. */diff --git a/site/little-red-riding-hood/lib/engine.js b/site/little-red-riding-hood/lib/engine.jsindex b59277b..8f8c9bd 100644--- a/site/little-red-riding-hood/lib/engine.js+++ b/site/little-red-riding-hood/lib/engine.js@@ -49,6 +49,7 @@ const TRANSLATIONS = {chapterComplete: 'Chapter {n} complete', quickChecks: 'Quick checks', chapterPractice: 'Chapter practice', sources: 'Sources in this chapter', opensSource: '{label}, opens the source in a new tab',firstTryScore: '{name}: {r} of {t} right on the first try.', notAttempted: '{name}: not attempted.',watchAgain: 'Watch again', nextChapter: 'Next chapter',+ wordsHeard: '{k} / {n}', allWordsHeard: 'You found them all!',},ru: {decimalSign: ',', stageLabel: 'Анимация урока', stageLabelTitled: 'Анимация урока: {title}',@@ -86,6 +87,7 @@ const TRANSLATIONS = {chapterComplete: 'Глава {n} пройдена', quickChecks: 'Проверь себя', chapterPractice: 'Практика по главе', sources: 'Источники этой главы', opensSource: '{label}, источник откроется в новой вкладке',firstTryScore: '{name}: {r} из {t} верно с первой попытки.', notAttempted: '{name}: не выполнялось.',watchAgain: 'Смотреть снова', nextChapter: 'Следующая глава',+ wordsHeard: '{k} / {n}', allWordsHeard: 'Все найдены!',},zh: {decimalSign: '.', stageLabel: '课程动画', stageLabelTitled: '课程动画:{title}',@@ -121,6 +123,7 @@ const TRANSLATIONS = {chapterComplete: '第 {n} 章完成', quickChecks: '小测', chapterPractice: '本章练习', sources: '本章引用的文献', opensSource: '{label},在新标签页中打开来源',firstTryScore: '{name}:第一次就答对 {r} / {t}。', notAttempted: '{name}:未作答。',watchAgain: '再看一遍', nextChapter: '下一章',+ wordsHeard: '{k} / {n}', allWordsHeard: '全都找到啦!',},ja: {decimalSign: '.', stageLabel: '授業アニメーション', stageLabelTitled: '授業アニメーション:{title}',@@ -156,6 +159,7 @@ const TRANSLATIONS = {chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',watchAgain: 'もう一度見る', nextChapter: '次の章へ',+ wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',},};// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.@@ -951,35 +955,42 @@ function mcq(id, onRight) {if (!it) throw new Error(`mcq: no item "${id}" in items.js`);return { id, prompt: it.prompt, build: choice(it.opts, it.right, it.why, it.no, onRight) };}-// The shared body of the stage tap questions: each target ring is a focusable button that answers on click or Enter,-// and ← / → move the focus between them. targets: [{ g, label, ok, wrong }], wrong() shakes and returns the explanation.-const stageTap = (api, targets, show, yes, onRight, hint) => {- let locked = false, cur = -1;- const gs = targets.map(({ g, label, ok, wrong }) => {+// Stage targets the reader taps: each g turns into a focusable button named by its label that acts on a click, Enter or+// Space. Returns key(e) for P.keys: ← / → move the focus between them (from the one focused, if any).+function tappable(items) { // [{ g, label, act }]+ for (const { g, label, act } of items) {g.classList.add('hit');g.setAttribute('tabindex', 0);g.setAttribute('role', 'button');g.setAttribute('aria-label', label);- const act = () => {- if (locked) return;- if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }- else { g.classList.add('bad'); api.grade(false, wrong()); }- };g.addEventListener('click', act);keyAct(g, act);- return g;- });+ }+ let cur = -1;+ return e => {+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;+ if (!d || !items.length) return false;+ const on = items.findIndex(it => it.g === document.activeElement);+ if (on >= 0) cur = on;+ cur = cur < 0 ? (d > 0 ? 0 : items.length - 1) : (cur + d + items.length) % items.length;+ items[cur].g.focus();+ return true;+ };+}+// The shared body of the stage tap questions: each target ring answers on click or Enter (tappable), wrong() shakes+// and returns the explanation. targets: [{ g, label, ok, wrong }].+const stageTap = (api, targets, show, yes, onRight, hint) => {+ let locked = false;+ const move = tappable(targets.map(({ g, label, ok, wrong }) => ({ g, label, act: () => {+ if (locked) return;+ if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }+ else { g.classList.add('bad'); api.grade(false, wrong()); }+ } })));return {- reveal() { gs[show].classList.add('good'); onRight?.(); return yes; },+ reveal() { targets[show].g.classList.add('good'); onRight?.(); return yes; },lock() { locked = true; },hint,- key(e) {- const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;- if (!d || locked) return false;- cur = cur < 0 ? (d > 0 ? 0 : gs.length - 1) : (cur + d + gs.length) % gs.length;- gs[cur].focus();- return true;- },+ key: e => !locked && move(e),};};// Tap a point on the stage number line.@@ -1015,6 +1026,113 @@ const pickEls = (items, right, yes, no, onRight) => (body, api) => {return stageTap(api, targets, right, yes, onRight,[kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClick')]);};+/* ---------- point and read ---------- */+// A picture book's point-and-read moment, for young children learning words: a film's ask beat (CHAPTER.film),+// ask: done => tapWords(THINGS, done) THINGS = [{ g, word: 'basket', clip: 'w-basket' }]+// which the scene fills as it builds the picture; g is what the reader taps (a puppet, a prop, or a transparent shape+// over part of a baked set) and clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default. Once the narration is+// over (P.waiting) every thing turns tappable: a tap hops it, shows its word in large letters beside it and says it, on+// wall time at the player volume with the music ducked under it; tapping again says it again. A counter and Continue+// (Enter) wait in the corner; Continue lights up once every thing has been heard. Seeking or replaying clears it all.+// While the page waits, the scene's continuous motion (stopClock, clockRun with tail: true) runs on past the beat's+// end on wall time, so puppets posed as a function of time keep breathing and blinking.+// The video (export.py) cannot tap: P.tapDemo.at(s) shows the words one by one, DEMO_STEP s apart, while it holds.+const TAP_MIN = 120, DEMO_STEP = 2; // TAP_MIN: the smallest hit box in stage px, a child's finger on a phone+const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');+function tapWords(things, done) {+ const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';+ if (!CHAPTER.film) console.warn('tapWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');+ const list = things.filter(t => {+ const why = !t.word ? 'has no word' : !(t.g instanceof SVGGraphicsElement && stage.contains(t.g)) ? 'is not an element on the stage' : '';+ if (why) console.warn(`tapWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);+ return !why;+ }).map(t => {+ const clip = t.clip || wordClip(t.word), a = new Audio(`audio/${lang}/${clip}.mp3`);+ a.preload = 'auto';+ a.onerror = () => console.warn(`tapWords: "${t.word}" has no clip at audio/${lang}/${clip}.mp3`);+ return { g: t.g, word: t.word, clip, a, heard: false };+ });+ const L = mk('g', { class: 'tapw' }, stage); // the hit boxes, over the whole picture+ let on = false, alive = true, finished = false, word = null, shown = -1, bar, count, go;+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least TAP_MIN square+ const box = t => {+ const s = stage.getBoundingClientRect(), r = t.g.getBoundingClientRect(), kx = SW / s.width, ky = SH / s.height;+ return { x: (r.left - s.left) * kx, y: (r.top - s.top) * ky, w: r.width * kx, h: r.height * ky };+ };+ const place = () => {+ for (const t of list) {+ const b = box(t), w = Math.max(TAP_MIN, b.w), hh = Math.max(TAP_MIN, b.h);+ for (const [k, v] of Object.entries({ x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, width: w, height: hh })) t.ring.setAttribute(k, v.toFixed(1));+ }+ };+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };+ const idle = s => { for (const w of TW) if (w.tail === true && w.dur) w.pf(1 + s / w.dur); }; // s seconds past the end+ let since = 0;+ const tapIt = (k, quiet) => {+ const t = list[k];+ shown = k;+ t.heard = true;+ const n = list.filter(x => x.heard).length, all = n === list.length;+ count.textContent = all ? translate('allWordsHeard') : translate('wordsHeard', { k: n, n: list.length });+ if (all && !go.classList.contains('go')) { go.className = 'btn go'; bar.classList.add('lit'); }+ // the word beside the thing: above it, else below, right or left, wherever it fits on the stage (slid along that+ // side to stay on it)+ word?.remove();+ word = h('div', 'card word', t.word);+ word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word+ $('ui').append(word);+ const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;+ const spots = [[cx, b.y - hh - m], [cx, b.y + b.h + m], [b.x + b.w + m, cy], [b.x - w - m, cy]];+ const [x, y] = spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];+ Object.assign(word.style, { left: Math.max(m, Math.min(SW - m - w, x)) + 'px', top: Math.max(m, Math.min(SH - m - hh, y)) + 'px' });+ if (quiet) { word.style.animation = go.style.animation = 'none'; return; } // a video's frame shows it at rest+ for (const x of list) if (x !== t) x.a.pause();+ t.a.currentTime = 0;+ t.a.volume = +($('volume')?.value ?? 1);+ t.a.play().catch(() => {});+ SND.word = t.a; // soundTick ducks the music under it+ t.g.animate([{ translate: '0 0' }, { translate: '0 -18px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });+ t.ring.animate([{ strokeOpacity: 1 }, { strokeOpacity: 0 }], { duration: 700, easing: 'ease-out' });+ };+ const enable = () => {+ on = true;+ since = performance.now();+ linkStage(); // the stage is no longer one picture to a screen reader: the things are its buttons+ const at = new Map(list.map(t => [t, box(t)]));+ for (const [t, b] of at) if (b.x + b.w <= 0 || b.x >= SW || b.y + b.h <= 0 || b.y >= SH || !b.w || !b.h) console.warn(`tapWords: "${t.word}" is not in the picture`);+ // smaller boxes on top, so a thing standing in front of a bigger one can still be tapped+ for (const t of [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h)) { t.hit = G(L); t.ring = mk('rect', { class: 'ring', rx: 22 }, t.hit); }+ place();+ // the arrows go through the things from left to right+ const order = [...list].sort((a, c) => at.get(a).x + at.get(a).w / 2 - at.get(c).x - at.get(c).w / 2);+ const move = tappable(order.map(t => ({ g: t.hit, label: t.word, act: () => { if (alive) tapIt(list.indexOf(t)); } })));+ P.keys = e => e.key === 'Enter' ? (finish(), true) : move(e);+ bar = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'words');+ Object.assign(bar.style, { left: 'auto', right: '40px', width: 'auto' });+ count = h('span', 'count', translate('wordsHeard', { k: 0, n: list.length }));+ count.setAttribute('role', 'status');+ go = h('button', 'btn quiet', [translate('continue'), kbd('Enter')]);+ go.onclick = finish;+ bar.append(h('div', 'acts', [count, go]));+ };+ const cleanup = () => {+ alive = false;+ L.remove();+ for (const t of list) { t.a.onerror = null; t.a.pause(); t.a.removeAttribute('src'); t.a.load(); if (SND.word === t.a) SND.word = null; }+ if (P.tapDemo?.gen === gen) delete P.tapDemo;+ };+ P.cleanup.push(cleanup);+ P.tapDemo = { gen, clips: list.map(t => t.clip), step: DEMO_STEP, at(s) { if (!on) enable(); idle(s); const k = Math.min(list.length - 1, Math.floor(s / DEMO_STEP)); if (k >= 0 && k !== shown) { list.slice(0, k).forEach(t => { t.heard = true; }); tapIt(k, true); } } };+ const tick = () => {+ if (!alive) return;+ if (P.gen !== gen) { cleanup(); return; } // a seek away, or the beat replayed+ if (P.waiting && !on) enable();+ if (on && P.waiting) idle((performance.now() - since) / 1000);+ if (on) place();+ requestAnimationFrame(tick);+ };+ requestAnimationFrame(tick);+}// Rows of toggles. items: { parts, ans: [keys], why }. parts are math parts (strings, F, R) unless text: true,// where they are rich text (strings and $m(...)). cols: [[key, label, colour?, shortcut?]]. multi: several per row.const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) => {@@ -1279,7 +1397,9 @@ function soundTick(dt) {if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);- if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));+ // a word the reader tapped (tapWords) ducks the music at once, as the narration does+ const saying = !!SND.word && !SND.word.paused && !SND.word.ended;+ if (!held) SND.duck += Math.max(-dt * (saying ? 4 : 1), Math.min(dt, (wait && !saying || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));for (const m of SND.bgm) {const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says@@ -1741,7 +1861,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isShR, E, TX, $tex, texGraphic, texPart, OV, M, chip, tokens, layRow, collapse, mathEl, io, out, back, lin, tw, prog,show, hide, draw, pop, pulse, stream, mix, evalTo, soft, clockRun, camera, scene, S, AXIS, X, Y0, num, reset, tick,dot, arrow, panel, retire, header, fx, fxp, finishFx, pulseFx, shakeFx, floatText, TF, $, kbd, sources, uiW, SCORE,- h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, grid, rat, gcd, lowest, blanks,+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, tapWords, grid, rat, gcd, lowest, blanks,SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,finishCard };})();@@ -1768,7 +1888,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGINuiW = ENGINE.uiW, SCORE = ENGINE.SCORE, h = ENGINE.h, rich = ENGINE.rich, card = ENGINE.card,mascotEl = ENGINE.mascotEl, quiz = ENGINE.quiz, qlayer = ENGINE.qlayer, keyAct = ENGINE.keyAct,choice = ENGINE.choice, mcq = ENGINE.mcq, tap = ENGINE.tap, tapEls = ENGINE.tapEls, pickEls = ENGINE.pickEls,- grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,+ tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,SND = ENGINE.SND, P = ENGINE.P, HOLD = ENGINE.HOLD, clearCards = ENGINE.clearCards, seek = ENGINE.seek,seekInto = ENGINE.seekInto, start = ENGINE.start, setSpeed = ENGINE.setSpeed, setPlaying = ENGINE.setPlaying,setCaptions = ENGINE.setCaptions, hideCover = ENGINE.hideCover, restart = ENGINE.restart, boot = ENGINE.boot;diff --git a/site/madness-shorts/lib/engine.css b/site/madness-shorts/lib/engine.cssindex 7fc7cc5..bf42cc9 100644--- a/site/madness-shorts/lib/engine.css+++ b/site/madness-shorts/lib/engine.css@@ -103,6 +103,15 @@ button[hidden] { display: none; }.hit:hover .ring, .hit:focus .ring { stroke-opacity: .7; }.hit.bad .ring { stroke: var(--bad); stroke-opacity: 1; }.hit.good .ring { stroke: var(--good); stroke-opacity: 1; stroke-width: 4; }+/* Point and read (tapWords): hit boxes over the picture, the word beside a tapped thing, the counter and Continue.+ A book can set --word-font for its early-reader letters. */+.tapw .ring { stroke-width: 5; }+#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-left-width: 1.5px; border-radius: 30px; font: 600 96px/1.15 var(--word-font, var(--ui)); white-space: nowrap; animation: word-in .3s cubic-bezier(.3, 1.5, .6, 1); }+@keyframes word-in { from { transform: scale(.6); opacity: 0; } }+.card.words .acts { margin: 0; gap: 18px; }+.card.words .count { font-size: 24px; font-weight: 600; color: var(--dim); }+.card.words.lit .count { color: var(--good); }+.card.words.lit .btn.go { animation: word-in .4s cubic-bezier(.3, 1.5, .6, 1); }.tok { cursor: grab; touch-action: none; } /* a finger drags the chip instead of scrolling the page *//* Controls strip, part of the frame. */diff --git a/site/madness-shorts/lib/engine.js b/site/madness-shorts/lib/engine.jsindex b59277b..8f8c9bd 100644--- a/site/madness-shorts/lib/engine.js+++ b/site/madness-shorts/lib/engine.js@@ -49,6 +49,7 @@ const TRANSLATIONS = {chapterComplete: 'Chapter {n} complete', quickChecks: 'Quick checks', chapterPractice: 'Chapter practice', sources: 'Sources in this chapter', opensSource: '{label}, opens the source in a new tab',firstTryScore: '{name}: {r} of {t} right on the first try.', notAttempted: '{name}: not attempted.',watchAgain: 'Watch again', nextChapter: 'Next chapter',+ wordsHeard: '{k} / {n}', allWordsHeard: 'You found them all!',},ru: {decimalSign: ',', stageLabel: 'Анимация урока', stageLabelTitled: 'Анимация урока: {title}',@@ -86,6 +87,7 @@ const TRANSLATIONS = {chapterComplete: 'Глава {n} пройдена', quickChecks: 'Проверь себя', chapterPractice: 'Практика по главе', sources: 'Источники этой главы', opensSource: '{label}, источник откроется в новой вкладке',firstTryScore: '{name}: {r} из {t} верно с первой попытки.', notAttempted: '{name}: не выполнялось.',watchAgain: 'Смотреть снова', nextChapter: 'Следующая глава',+ wordsHeard: '{k} / {n}', allWordsHeard: 'Все найдены!',},zh: {decimalSign: '.', stageLabel: '课程动画', stageLabelTitled: '课程动画:{title}',@@ -121,6 +123,7 @@ const TRANSLATIONS = {chapterComplete: '第 {n} 章完成', quickChecks: '小测', chapterPractice: '本章练习', sources: '本章引用的文献', opensSource: '{label},在新标签页中打开来源',firstTryScore: '{name}:第一次就答对 {r} / {t}。', notAttempted: '{name}:未作答。',watchAgain: '再看一遍', nextChapter: '下一章',+ wordsHeard: '{k} / {n}', allWordsHeard: '全都找到啦!',},ja: {decimalSign: '.', stageLabel: '授業アニメーション', stageLabelTitled: '授業アニメーション:{title}',@@ -156,6 +159,7 @@ const TRANSLATIONS = {chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',watchAgain: 'もう一度見る', nextChapter: '次の章へ',+ wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',},};// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.@@ -951,35 +955,42 @@ function mcq(id, onRight) {if (!it) throw new Error(`mcq: no item "${id}" in items.js`);return { id, prompt: it.prompt, build: choice(it.opts, it.right, it.why, it.no, onRight) };}-// The shared body of the stage tap questions: each target ring is a focusable button that answers on click or Enter,-// and ← / → move the focus between them. targets: [{ g, label, ok, wrong }], wrong() shakes and returns the explanation.-const stageTap = (api, targets, show, yes, onRight, hint) => {- let locked = false, cur = -1;- const gs = targets.map(({ g, label, ok, wrong }) => {+// Stage targets the reader taps: each g turns into a focusable button named by its label that acts on a click, Enter or+// Space. Returns key(e) for P.keys: ← / → move the focus between them (from the one focused, if any).+function tappable(items) { // [{ g, label, act }]+ for (const { g, label, act } of items) {g.classList.add('hit');g.setAttribute('tabindex', 0);g.setAttribute('role', 'button');g.setAttribute('aria-label', label);- const act = () => {- if (locked) return;- if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }- else { g.classList.add('bad'); api.grade(false, wrong()); }- };g.addEventListener('click', act);keyAct(g, act);- return g;- });+ }+ let cur = -1;+ return e => {+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;+ if (!d || !items.length) return false;+ const on = items.findIndex(it => it.g === document.activeElement);+ if (on >= 0) cur = on;+ cur = cur < 0 ? (d > 0 ? 0 : items.length - 1) : (cur + d + items.length) % items.length;+ items[cur].g.focus();+ return true;+ };+}+// The shared body of the stage tap questions: each target ring answers on click or Enter (tappable), wrong() shakes+// and returns the explanation. targets: [{ g, label, ok, wrong }].+const stageTap = (api, targets, show, yes, onRight, hint) => {+ let locked = false;+ const move = tappable(targets.map(({ g, label, ok, wrong }) => ({ g, label, act: () => {+ if (locked) return;+ if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }+ else { g.classList.add('bad'); api.grade(false, wrong()); }+ } })));return {- reveal() { gs[show].classList.add('good'); onRight?.(); return yes; },+ reveal() { targets[show].g.classList.add('good'); onRight?.(); return yes; },lock() { locked = true; },hint,- key(e) {- const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;- if (!d || locked) return false;- cur = cur < 0 ? (d > 0 ? 0 : gs.length - 1) : (cur + d + gs.length) % gs.length;- gs[cur].focus();- return true;- },+ key: e => !locked && move(e),};};// Tap a point on the stage number line.@@ -1015,6 +1026,113 @@ const pickEls = (items, right, yes, no, onRight) => (body, api) => {return stageTap(api, targets, right, yes, onRight,[kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClick')]);};+/* ---------- point and read ---------- */+// A picture book's point-and-read moment, for young children learning words: a film's ask beat (CHAPTER.film),+// ask: done => tapWords(THINGS, done) THINGS = [{ g, word: 'basket', clip: 'w-basket' }]+// which the scene fills as it builds the picture; g is what the reader taps (a puppet, a prop, or a transparent shape+// over part of a baked set) and clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default. Once the narration is+// over (P.waiting) every thing turns tappable: a tap hops it, shows its word in large letters beside it and says it, on+// wall time at the player volume with the music ducked under it; tapping again says it again. A counter and Continue+// (Enter) wait in the corner; Continue lights up once every thing has been heard. Seeking or replaying clears it all.+// While the page waits, the scene's continuous motion (stopClock, clockRun with tail: true) runs on past the beat's+// end on wall time, so puppets posed as a function of time keep breathing and blinking.+// The video (export.py) cannot tap: P.tapDemo.at(s) shows the words one by one, DEMO_STEP s apart, while it holds.+const TAP_MIN = 120, DEMO_STEP = 2; // TAP_MIN: the smallest hit box in stage px, a child's finger on a phone+const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');+function tapWords(things, done) {+ const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';+ if (!CHAPTER.film) console.warn('tapWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');+ const list = things.filter(t => {+ const why = !t.word ? 'has no word' : !(t.g instanceof SVGGraphicsElement && stage.contains(t.g)) ? 'is not an element on the stage' : '';+ if (why) console.warn(`tapWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);+ return !why;+ }).map(t => {+ const clip = t.clip || wordClip(t.word), a = new Audio(`audio/${lang}/${clip}.mp3`);+ a.preload = 'auto';+ a.onerror = () => console.warn(`tapWords: "${t.word}" has no clip at audio/${lang}/${clip}.mp3`);+ return { g: t.g, word: t.word, clip, a, heard: false };+ });+ const L = mk('g', { class: 'tapw' }, stage); // the hit boxes, over the whole picture+ let on = false, alive = true, finished = false, word = null, shown = -1, bar, count, go;+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least TAP_MIN square+ const box = t => {+ const s = stage.getBoundingClientRect(), r = t.g.getBoundingClientRect(), kx = SW / s.width, ky = SH / s.height;+ return { x: (r.left - s.left) * kx, y: (r.top - s.top) * ky, w: r.width * kx, h: r.height * ky };+ };+ const place = () => {+ for (const t of list) {+ const b = box(t), w = Math.max(TAP_MIN, b.w), hh = Math.max(TAP_MIN, b.h);+ for (const [k, v] of Object.entries({ x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, width: w, height: hh })) t.ring.setAttribute(k, v.toFixed(1));+ }+ };+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };+ const idle = s => { for (const w of TW) if (w.tail === true && w.dur) w.pf(1 + s / w.dur); }; // s seconds past the end+ let since = 0;+ const tapIt = (k, quiet) => {+ const t = list[k];+ shown = k;+ t.heard = true;+ const n = list.filter(x => x.heard).length, all = n === list.length;+ count.textContent = all ? translate('allWordsHeard') : translate('wordsHeard', { k: n, n: list.length });+ if (all && !go.classList.contains('go')) { go.className = 'btn go'; bar.classList.add('lit'); }+ // the word beside the thing: above it, else below, right or left, wherever it fits on the stage (slid along that+ // side to stay on it)+ word?.remove();+ word = h('div', 'card word', t.word);+ word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word+ $('ui').append(word);+ const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;+ const spots = [[cx, b.y - hh - m], [cx, b.y + b.h + m], [b.x + b.w + m, cy], [b.x - w - m, cy]];+ const [x, y] = spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];+ Object.assign(word.style, { left: Math.max(m, Math.min(SW - m - w, x)) + 'px', top: Math.max(m, Math.min(SH - m - hh, y)) + 'px' });+ if (quiet) { word.style.animation = go.style.animation = 'none'; return; } // a video's frame shows it at rest+ for (const x of list) if (x !== t) x.a.pause();+ t.a.currentTime = 0;+ t.a.volume = +($('volume')?.value ?? 1);+ t.a.play().catch(() => {});+ SND.word = t.a; // soundTick ducks the music under it+ t.g.animate([{ translate: '0 0' }, { translate: '0 -18px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });+ t.ring.animate([{ strokeOpacity: 1 }, { strokeOpacity: 0 }], { duration: 700, easing: 'ease-out' });+ };+ const enable = () => {+ on = true;+ since = performance.now();+ linkStage(); // the stage is no longer one picture to a screen reader: the things are its buttons+ const at = new Map(list.map(t => [t, box(t)]));+ for (const [t, b] of at) if (b.x + b.w <= 0 || b.x >= SW || b.y + b.h <= 0 || b.y >= SH || !b.w || !b.h) console.warn(`tapWords: "${t.word}" is not in the picture`);+ // smaller boxes on top, so a thing standing in front of a bigger one can still be tapped+ for (const t of [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h)) { t.hit = G(L); t.ring = mk('rect', { class: 'ring', rx: 22 }, t.hit); }+ place();+ // the arrows go through the things from left to right+ const order = [...list].sort((a, c) => at.get(a).x + at.get(a).w / 2 - at.get(c).x - at.get(c).w / 2);+ const move = tappable(order.map(t => ({ g: t.hit, label: t.word, act: () => { if (alive) tapIt(list.indexOf(t)); } })));+ P.keys = e => e.key === 'Enter' ? (finish(), true) : move(e);+ bar = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'words');+ Object.assign(bar.style, { left: 'auto', right: '40px', width: 'auto' });+ count = h('span', 'count', translate('wordsHeard', { k: 0, n: list.length }));+ count.setAttribute('role', 'status');+ go = h('button', 'btn quiet', [translate('continue'), kbd('Enter')]);+ go.onclick = finish;+ bar.append(h('div', 'acts', [count, go]));+ };+ const cleanup = () => {+ alive = false;+ L.remove();+ for (const t of list) { t.a.onerror = null; t.a.pause(); t.a.removeAttribute('src'); t.a.load(); if (SND.word === t.a) SND.word = null; }+ if (P.tapDemo?.gen === gen) delete P.tapDemo;+ };+ P.cleanup.push(cleanup);+ P.tapDemo = { gen, clips: list.map(t => t.clip), step: DEMO_STEP, at(s) { if (!on) enable(); idle(s); const k = Math.min(list.length - 1, Math.floor(s / DEMO_STEP)); if (k >= 0 && k !== shown) { list.slice(0, k).forEach(t => { t.heard = true; }); tapIt(k, true); } } };+ const tick = () => {+ if (!alive) return;+ if (P.gen !== gen) { cleanup(); return; } // a seek away, or the beat replayed+ if (P.waiting && !on) enable();+ if (on && P.waiting) idle((performance.now() - since) / 1000);+ if (on) place();+ requestAnimationFrame(tick);+ };+ requestAnimationFrame(tick);+}// Rows of toggles. items: { parts, ans: [keys], why }. parts are math parts (strings, F, R) unless text: true,// where they are rich text (strings and $m(...)). cols: [[key, label, colour?, shortcut?]]. multi: several per row.const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) => {@@ -1279,7 +1397,9 @@ function soundTick(dt) {if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);- if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));+ // a word the reader tapped (tapWords) ducks the music at once, as the narration does+ const saying = !!SND.word && !SND.word.paused && !SND.word.ended;+ if (!held) SND.duck += Math.max(-dt * (saying ? 4 : 1), Math.min(dt, (wait && !saying || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));for (const m of SND.bgm) {const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says@@ -1741,7 +1861,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isShR, E, TX, $tex, texGraphic, texPart, OV, M, chip, tokens, layRow, collapse, mathEl, io, out, back, lin, tw, prog,show, hide, draw, pop, pulse, stream, mix, evalTo, soft, clockRun, camera, scene, S, AXIS, X, Y0, num, reset, tick,dot, arrow, panel, retire, header, fx, fxp, finishFx, pulseFx, shakeFx, floatText, TF, $, kbd, sources, uiW, SCORE,- h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, grid, rat, gcd, lowest, blanks,+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, tapWords, grid, rat, gcd, lowest, blanks,SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,finishCard };})();@@ -1768,7 +1888,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGINuiW = ENGINE.uiW, SCORE = ENGINE.SCORE, h = ENGINE.h, rich = ENGINE.rich, card = ENGINE.card,mascotEl = ENGINE.mascotEl, quiz = ENGINE.quiz, qlayer = ENGINE.qlayer, keyAct = ENGINE.keyAct,choice = ENGINE.choice, mcq = ENGINE.mcq, tap = ENGINE.tap, tapEls = ENGINE.tapEls, pickEls = ENGINE.pickEls,- grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,+ tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,SND = ENGINE.SND, P = ENGINE.P, HOLD = ENGINE.HOLD, clearCards = ENGINE.clearCards, seek = ENGINE.seek,seekInto = ENGINE.seekInto, start = ENGINE.start, setSpeed = ENGINE.setSpeed, setPlaying = ENGINE.setPlaying,setCaptions = ENGINE.setCaptions, hideCover = ENGINE.hideCover, restart = ENGINE.restart, boot = ENGINE.boot;diff --git a/site/madness/lib/engine.css b/site/madness/lib/engine.cssindex 7fc7cc5..bf42cc9 100644--- a/site/madness/lib/engine.css+++ b/site/madness/lib/engine.css@@ -103,6 +103,15 @@ button[hidden] { display: none; }.hit:hover .ring, .hit:focus .ring { stroke-opacity: .7; }.hit.bad .ring { stroke: var(--bad); stroke-opacity: 1; }.hit.good .ring { stroke: var(--good); stroke-opacity: 1; stroke-width: 4; }+/* Point and read (tapWords): hit boxes over the picture, the word beside a tapped thing, the counter and Continue.+ A book can set --word-font for its early-reader letters. */+.tapw .ring { stroke-width: 5; }+#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-left-width: 1.5px; border-radius: 30px; font: 600 96px/1.15 var(--word-font, var(--ui)); white-space: nowrap; animation: word-in .3s cubic-bezier(.3, 1.5, .6, 1); }+@keyframes word-in { from { transform: scale(.6); opacity: 0; } }+.card.words .acts { margin: 0; gap: 18px; }+.card.words .count { font-size: 24px; font-weight: 600; color: var(--dim); }+.card.words.lit .count { color: var(--good); }+.card.words.lit .btn.go { animation: word-in .4s cubic-bezier(.3, 1.5, .6, 1); }.tok { cursor: grab; touch-action: none; } /* a finger drags the chip instead of scrolling the page *//* Controls strip, part of the frame. */diff --git a/site/madness/lib/engine.js b/site/madness/lib/engine.jsindex b59277b..8f8c9bd 100644--- a/site/madness/lib/engine.js+++ b/site/madness/lib/engine.js@@ -49,6 +49,7 @@ const TRANSLATIONS = {chapterComplete: 'Chapter {n} complete', quickChecks: 'Quick checks', chapterPractice: 'Chapter practice', sources: 'Sources in this chapter', opensSource: '{label}, opens the source in a new tab',firstTryScore: '{name}: {r} of {t} right on the first try.', notAttempted: '{name}: not attempted.',watchAgain: 'Watch again', nextChapter: 'Next chapter',+ wordsHeard: '{k} / {n}', allWordsHeard: 'You found them all!',},ru: {decimalSign: ',', stageLabel: 'Анимация урока', stageLabelTitled: 'Анимация урока: {title}',@@ -86,6 +87,7 @@ const TRANSLATIONS = {chapterComplete: 'Глава {n} пройдена', quickChecks: 'Проверь себя', chapterPractice: 'Практика по главе', sources: 'Источники этой главы', opensSource: '{label}, источник откроется в новой вкладке',firstTryScore: '{name}: {r} из {t} верно с первой попытки.', notAttempted: '{name}: не выполнялось.',watchAgain: 'Смотреть снова', nextChapter: 'Следующая глава',+ wordsHeard: '{k} / {n}', allWordsHeard: 'Все найдены!',},zh: {decimalSign: '.', stageLabel: '课程动画', stageLabelTitled: '课程动画:{title}',@@ -121,6 +123,7 @@ const TRANSLATIONS = {chapterComplete: '第 {n} 章完成', quickChecks: '小测', chapterPractice: '本章练习', sources: '本章引用的文献', opensSource: '{label},在新标签页中打开来源',firstTryScore: '{name}:第一次就答对 {r} / {t}。', notAttempted: '{name}:未作答。',watchAgain: '再看一遍', nextChapter: '下一章',+ wordsHeard: '{k} / {n}', allWordsHeard: '全都找到啦!',},ja: {decimalSign: '.', stageLabel: '授業アニメーション', stageLabelTitled: '授業アニメーション:{title}',@@ -156,6 +159,7 @@ const TRANSLATIONS = {chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',watchAgain: 'もう一度見る', nextChapter: '次の章へ',+ wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',},};// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.@@ -951,35 +955,42 @@ function mcq(id, onRight) {if (!it) throw new Error(`mcq: no item "${id}" in items.js`);return { id, prompt: it.prompt, build: choice(it.opts, it.right, it.why, it.no, onRight) };}-// The shared body of the stage tap questions: each target ring is a focusable button that answers on click or Enter,-// and ← / → move the focus between them. targets: [{ g, label, ok, wrong }], wrong() shakes and returns the explanation.-const stageTap = (api, targets, show, yes, onRight, hint) => {- let locked = false, cur = -1;- const gs = targets.map(({ g, label, ok, wrong }) => {+// Stage targets the reader taps: each g turns into a focusable button named by its label that acts on a click, Enter or+// Space. Returns key(e) for P.keys: ← / → move the focus between them (from the one focused, if any).+function tappable(items) { // [{ g, label, act }]+ for (const { g, label, act } of items) {g.classList.add('hit');g.setAttribute('tabindex', 0);g.setAttribute('role', 'button');g.setAttribute('aria-label', label);- const act = () => {- if (locked) return;- if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }- else { g.classList.add('bad'); api.grade(false, wrong()); }- };g.addEventListener('click', act);keyAct(g, act);- return g;- });+ }+ let cur = -1;+ return e => {+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;+ if (!d || !items.length) return false;+ const on = items.findIndex(it => it.g === document.activeElement);+ if (on >= 0) cur = on;+ cur = cur < 0 ? (d > 0 ? 0 : items.length - 1) : (cur + d + items.length) % items.length;+ items[cur].g.focus();+ return true;+ };+}+// The shared body of the stage tap questions: each target ring answers on click or Enter (tappable), wrong() shakes+// and returns the explanation. targets: [{ g, label, ok, wrong }].+const stageTap = (api, targets, show, yes, onRight, hint) => {+ let locked = false;+ const move = tappable(targets.map(({ g, label, ok, wrong }) => ({ g, label, act: () => {+ if (locked) return;+ if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }+ else { g.classList.add('bad'); api.grade(false, wrong()); }+ } })));return {- reveal() { gs[show].classList.add('good'); onRight?.(); return yes; },+ reveal() { targets[show].g.classList.add('good'); onRight?.(); return yes; },lock() { locked = true; },hint,- key(e) {- const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;- if (!d || locked) return false;- cur = cur < 0 ? (d > 0 ? 0 : gs.length - 1) : (cur + d + gs.length) % gs.length;- gs[cur].focus();- return true;- },+ key: e => !locked && move(e),};};// Tap a point on the stage number line.@@ -1015,6 +1026,113 @@ const pickEls = (items, right, yes, no, onRight) => (body, api) => {return stageTap(api, targets, right, yes, onRight,[kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClick')]);};+/* ---------- point and read ---------- */+// A picture book's point-and-read moment, for young children learning words: a film's ask beat (CHAPTER.film),+// ask: done => tapWords(THINGS, done) THINGS = [{ g, word: 'basket', clip: 'w-basket' }]+// which the scene fills as it builds the picture; g is what the reader taps (a puppet, a prop, or a transparent shape+// over part of a baked set) and clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default. Once the narration is+// over (P.waiting) every thing turns tappable: a tap hops it, shows its word in large letters beside it and says it, on+// wall time at the player volume with the music ducked under it; tapping again says it again. A counter and Continue+// (Enter) wait in the corner; Continue lights up once every thing has been heard. Seeking or replaying clears it all.+// While the page waits, the scene's continuous motion (stopClock, clockRun with tail: true) runs on past the beat's+// end on wall time, so puppets posed as a function of time keep breathing and blinking.+// The video (export.py) cannot tap: P.tapDemo.at(s) shows the words one by one, DEMO_STEP s apart, while it holds.+const TAP_MIN = 120, DEMO_STEP = 2; // TAP_MIN: the smallest hit box in stage px, a child's finger on a phone+const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');+function tapWords(things, done) {+ const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';+ if (!CHAPTER.film) console.warn('tapWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');+ const list = things.filter(t => {+ const why = !t.word ? 'has no word' : !(t.g instanceof SVGGraphicsElement && stage.contains(t.g)) ? 'is not an element on the stage' : '';+ if (why) console.warn(`tapWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);+ return !why;+ }).map(t => {+ const clip = t.clip || wordClip(t.word), a = new Audio(`audio/${lang}/${clip}.mp3`);+ a.preload = 'auto';+ a.onerror = () => console.warn(`tapWords: "${t.word}" has no clip at audio/${lang}/${clip}.mp3`);+ return { g: t.g, word: t.word, clip, a, heard: false };+ });+ const L = mk('g', { class: 'tapw' }, stage); // the hit boxes, over the whole picture+ let on = false, alive = true, finished = false, word = null, shown = -1, bar, count, go;+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least TAP_MIN square+ const box = t => {+ const s = stage.getBoundingClientRect(), r = t.g.getBoundingClientRect(), kx = SW / s.width, ky = SH / s.height;+ return { x: (r.left - s.left) * kx, y: (r.top - s.top) * ky, w: r.width * kx, h: r.height * ky };+ };+ const place = () => {+ for (const t of list) {+ const b = box(t), w = Math.max(TAP_MIN, b.w), hh = Math.max(TAP_MIN, b.h);+ for (const [k, v] of Object.entries({ x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, width: w, height: hh })) t.ring.setAttribute(k, v.toFixed(1));+ }+ };+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };+ const idle = s => { for (const w of TW) if (w.tail === true && w.dur) w.pf(1 + s / w.dur); }; // s seconds past the end+ let since = 0;+ const tapIt = (k, quiet) => {+ const t = list[k];+ shown = k;+ t.heard = true;+ const n = list.filter(x => x.heard).length, all = n === list.length;+ count.textContent = all ? translate('allWordsHeard') : translate('wordsHeard', { k: n, n: list.length });+ if (all && !go.classList.contains('go')) { go.className = 'btn go'; bar.classList.add('lit'); }+ // the word beside the thing: above it, else below, right or left, wherever it fits on the stage (slid along that+ // side to stay on it)+ word?.remove();+ word = h('div', 'card word', t.word);+ word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word+ $('ui').append(word);+ const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;+ const spots = [[cx, b.y - hh - m], [cx, b.y + b.h + m], [b.x + b.w + m, cy], [b.x - w - m, cy]];+ const [x, y] = spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];+ Object.assign(word.style, { left: Math.max(m, Math.min(SW - m - w, x)) + 'px', top: Math.max(m, Math.min(SH - m - hh, y)) + 'px' });+ if (quiet) { word.style.animation = go.style.animation = 'none'; return; } // a video's frame shows it at rest+ for (const x of list) if (x !== t) x.a.pause();+ t.a.currentTime = 0;+ t.a.volume = +($('volume')?.value ?? 1);+ t.a.play().catch(() => {});+ SND.word = t.a; // soundTick ducks the music under it+ t.g.animate([{ translate: '0 0' }, { translate: '0 -18px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });+ t.ring.animate([{ strokeOpacity: 1 }, { strokeOpacity: 0 }], { duration: 700, easing: 'ease-out' });+ };+ const enable = () => {+ on = true;+ since = performance.now();+ linkStage(); // the stage is no longer one picture to a screen reader: the things are its buttons+ const at = new Map(list.map(t => [t, box(t)]));+ for (const [t, b] of at) if (b.x + b.w <= 0 || b.x >= SW || b.y + b.h <= 0 || b.y >= SH || !b.w || !b.h) console.warn(`tapWords: "${t.word}" is not in the picture`);+ // smaller boxes on top, so a thing standing in front of a bigger one can still be tapped+ for (const t of [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h)) { t.hit = G(L); t.ring = mk('rect', { class: 'ring', rx: 22 }, t.hit); }+ place();+ // the arrows go through the things from left to right+ const order = [...list].sort((a, c) => at.get(a).x + at.get(a).w / 2 - at.get(c).x - at.get(c).w / 2);+ const move = tappable(order.map(t => ({ g: t.hit, label: t.word, act: () => { if (alive) tapIt(list.indexOf(t)); } })));+ P.keys = e => e.key === 'Enter' ? (finish(), true) : move(e);+ bar = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'words');+ Object.assign(bar.style, { left: 'auto', right: '40px', width: 'auto' });+ count = h('span', 'count', translate('wordsHeard', { k: 0, n: list.length }));+ count.setAttribute('role', 'status');+ go = h('button', 'btn quiet', [translate('continue'), kbd('Enter')]);+ go.onclick = finish;+ bar.append(h('div', 'acts', [count, go]));+ };+ const cleanup = () => {+ alive = false;+ L.remove();+ for (const t of list) { t.a.onerror = null; t.a.pause(); t.a.removeAttribute('src'); t.a.load(); if (SND.word === t.a) SND.word = null; }+ if (P.tapDemo?.gen === gen) delete P.tapDemo;+ };+ P.cleanup.push(cleanup);+ P.tapDemo = { gen, clips: list.map(t => t.clip), step: DEMO_STEP, at(s) { if (!on) enable(); idle(s); const k = Math.min(list.length - 1, Math.floor(s / DEMO_STEP)); if (k >= 0 && k !== shown) { list.slice(0, k).forEach(t => { t.heard = true; }); tapIt(k, true); } } };+ const tick = () => {+ if (!alive) return;+ if (P.gen !== gen) { cleanup(); return; } // a seek away, or the beat replayed+ if (P.waiting && !on) enable();+ if (on && P.waiting) idle((performance.now() - since) / 1000);+ if (on) place();+ requestAnimationFrame(tick);+ };+ requestAnimationFrame(tick);+}// Rows of toggles. items: { parts, ans: [keys], why }. parts are math parts (strings, F, R) unless text: true,// where they are rich text (strings and $m(...)). cols: [[key, label, colour?, shortcut?]]. multi: several per row.const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) => {@@ -1279,7 +1397,9 @@ function soundTick(dt) {if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);- if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));+ // a word the reader tapped (tapWords) ducks the music at once, as the narration does+ const saying = !!SND.word && !SND.word.paused && !SND.word.ended;+ if (!held) SND.duck += Math.max(-dt * (saying ? 4 : 1), Math.min(dt, (wait && !saying || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));for (const m of SND.bgm) {const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says@@ -1741,7 +1861,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isShR, E, TX, $tex, texGraphic, texPart, OV, M, chip, tokens, layRow, collapse, mathEl, io, out, back, lin, tw, prog,show, hide, draw, pop, pulse, stream, mix, evalTo, soft, clockRun, camera, scene, S, AXIS, X, Y0, num, reset, tick,dot, arrow, panel, retire, header, fx, fxp, finishFx, pulseFx, shakeFx, floatText, TF, $, kbd, sources, uiW, SCORE,- h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, grid, rat, gcd, lowest, blanks,+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, tapWords, grid, rat, gcd, lowest, blanks,SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,finishCard };})();@@ -1768,7 +1888,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGINuiW = ENGINE.uiW, SCORE = ENGINE.SCORE, h = ENGINE.h, rich = ENGINE.rich, card = ENGINE.card,mascotEl = ENGINE.mascotEl, quiz = ENGINE.quiz, qlayer = ENGINE.qlayer, keyAct = ENGINE.keyAct,choice = ENGINE.choice, mcq = ENGINE.mcq, tap = ENGINE.tap, tapEls = ENGINE.tapEls, pickEls = ENGINE.pickEls,- grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,+ tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,SND = ENGINE.SND, P = ENGINE.P, HOLD = ENGINE.HOLD, clearCards = ENGINE.clearCards, seek = ENGINE.seek,seekInto = ENGINE.seekInto, start = ENGINE.start, setSpeed = ENGINE.setSpeed, setPlaying = ENGINE.setPlaying,setCaptions = ENGINE.setCaptions, hideCover = ENGINE.hideCover, restart = ENGINE.restart, boot = ENGINE.boot;diff --git a/site/math-notebook/lib/engine.css b/site/math-notebook/lib/engine.cssindex 7fc7cc5..bf42cc9 100644--- a/site/math-notebook/lib/engine.css+++ b/site/math-notebook/lib/engine.css@@ -103,6 +103,15 @@ button[hidden] { display: none; }.hit:hover .ring, .hit:focus .ring { stroke-opacity: .7; }.hit.bad .ring { stroke: var(--bad); stroke-opacity: 1; }.hit.good .ring { stroke: var(--good); stroke-opacity: 1; stroke-width: 4; }+/* Point and read (tapWords): hit boxes over the picture, the word beside a tapped thing, the counter and Continue.+ A book can set --word-font for its early-reader letters. */+.tapw .ring { stroke-width: 5; }+#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-left-width: 1.5px; border-radius: 30px; font: 600 96px/1.15 var(--word-font, var(--ui)); white-space: nowrap; animation: word-in .3s cubic-bezier(.3, 1.5, .6, 1); }+@keyframes word-in { from { transform: scale(.6); opacity: 0; } }+.card.words .acts { margin: 0; gap: 18px; }+.card.words .count { font-size: 24px; font-weight: 600; color: var(--dim); }+.card.words.lit .count { color: var(--good); }+.card.words.lit .btn.go { animation: word-in .4s cubic-bezier(.3, 1.5, .6, 1); }.tok { cursor: grab; touch-action: none; } /* a finger drags the chip instead of scrolling the page *//* Controls strip, part of the frame. */diff --git a/site/math-notebook/lib/engine.js b/site/math-notebook/lib/engine.jsindex b59277b..8f8c9bd 100644--- a/site/math-notebook/lib/engine.js+++ b/site/math-notebook/lib/engine.js@@ -49,6 +49,7 @@ const TRANSLATIONS = {chapterComplete: 'Chapter {n} complete', quickChecks: 'Quick checks', chapterPractice: 'Chapter practice', sources: 'Sources in this chapter', opensSource: '{label}, opens the source in a new tab',firstTryScore: '{name}: {r} of {t} right on the first try.', notAttempted: '{name}: not attempted.',watchAgain: 'Watch again', nextChapter: 'Next chapter',+ wordsHeard: '{k} / {n}', allWordsHeard: 'You found them all!',},ru: {decimalSign: ',', stageLabel: 'Анимация урока', stageLabelTitled: 'Анимация урока: {title}',@@ -86,6 +87,7 @@ const TRANSLATIONS = {chapterComplete: 'Глава {n} пройдена', quickChecks: 'Проверь себя', chapterPractice: 'Практика по главе', sources: 'Источники этой главы', opensSource: '{label}, источник откроется в новой вкладке',firstTryScore: '{name}: {r} из {t} верно с первой попытки.', notAttempted: '{name}: не выполнялось.',watchAgain: 'Смотреть снова', nextChapter: 'Следующая глава',+ wordsHeard: '{k} / {n}', allWordsHeard: 'Все найдены!',},zh: {decimalSign: '.', stageLabel: '课程动画', stageLabelTitled: '课程动画:{title}',@@ -121,6 +123,7 @@ const TRANSLATIONS = {chapterComplete: '第 {n} 章完成', quickChecks: '小测', chapterPractice: '本章练习', sources: '本章引用的文献', opensSource: '{label},在新标签页中打开来源',firstTryScore: '{name}:第一次就答对 {r} / {t}。', notAttempted: '{name}:未作答。',watchAgain: '再看一遍', nextChapter: '下一章',+ wordsHeard: '{k} / {n}', allWordsHeard: '全都找到啦!',},ja: {decimalSign: '.', stageLabel: '授業アニメーション', stageLabelTitled: '授業アニメーション:{title}',@@ -156,6 +159,7 @@ const TRANSLATIONS = {chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',watchAgain: 'もう一度見る', nextChapter: '次の章へ',+ wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',},};// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.@@ -951,35 +955,42 @@ function mcq(id, onRight) {if (!it) throw new Error(`mcq: no item "${id}" in items.js`);return { id, prompt: it.prompt, build: choice(it.opts, it.right, it.why, it.no, onRight) };}-// The shared body of the stage tap questions: each target ring is a focusable button that answers on click or Enter,-// and ← / → move the focus between them. targets: [{ g, label, ok, wrong }], wrong() shakes and returns the explanation.-const stageTap = (api, targets, show, yes, onRight, hint) => {- let locked = false, cur = -1;- const gs = targets.map(({ g, label, ok, wrong }) => {+// Stage targets the reader taps: each g turns into a focusable button named by its label that acts on a click, Enter or+// Space. Returns key(e) for P.keys: ← / → move the focus between them (from the one focused, if any).+function tappable(items) { // [{ g, label, act }]+ for (const { g, label, act } of items) {g.classList.add('hit');g.setAttribute('tabindex', 0);g.setAttribute('role', 'button');g.setAttribute('aria-label', label);- const act = () => {- if (locked) return;- if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }- else { g.classList.add('bad'); api.grade(false, wrong()); }- };g.addEventListener('click', act);keyAct(g, act);- return g;- });+ }+ let cur = -1;+ return e => {+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;+ if (!d || !items.length) return false;+ const on = items.findIndex(it => it.g === document.activeElement);+ if (on >= 0) cur = on;+ cur = cur < 0 ? (d > 0 ? 0 : items.length - 1) : (cur + d + items.length) % items.length;+ items[cur].g.focus();+ return true;+ };+}+// The shared body of the stage tap questions: each target ring answers on click or Enter (tappable), wrong() shakes+// and returns the explanation. targets: [{ g, label, ok, wrong }].+const stageTap = (api, targets, show, yes, onRight, hint) => {+ let locked = false;+ const move = tappable(targets.map(({ g, label, ok, wrong }) => ({ g, label, act: () => {+ if (locked) return;+ if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }+ else { g.classList.add('bad'); api.grade(false, wrong()); }+ } })));return {- reveal() { gs[show].classList.add('good'); onRight?.(); return yes; },+ reveal() { targets[show].g.classList.add('good'); onRight?.(); return yes; },lock() { locked = true; },hint,- key(e) {- const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;- if (!d || locked) return false;- cur = cur < 0 ? (d > 0 ? 0 : gs.length - 1) : (cur + d + gs.length) % gs.length;- gs[cur].focus();- return true;- },+ key: e => !locked && move(e),};};// Tap a point on the stage number line.@@ -1015,6 +1026,113 @@ const pickEls = (items, right, yes, no, onRight) => (body, api) => {return stageTap(api, targets, right, yes, onRight,[kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClick')]);};+/* ---------- point and read ---------- */+// A picture book's point-and-read moment, for young children learning words: a film's ask beat (CHAPTER.film),+// ask: done => tapWords(THINGS, done) THINGS = [{ g, word: 'basket', clip: 'w-basket' }]+// which the scene fills as it builds the picture; g is what the reader taps (a puppet, a prop, or a transparent shape+// over part of a baked set) and clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default. Once the narration is+// over (P.waiting) every thing turns tappable: a tap hops it, shows its word in large letters beside it and says it, on+// wall time at the player volume with the music ducked under it; tapping again says it again. A counter and Continue+// (Enter) wait in the corner; Continue lights up once every thing has been heard. Seeking or replaying clears it all.+// While the page waits, the scene's continuous motion (stopClock, clockRun with tail: true) runs on past the beat's+// end on wall time, so puppets posed as a function of time keep breathing and blinking.+// The video (export.py) cannot tap: P.tapDemo.at(s) shows the words one by one, DEMO_STEP s apart, while it holds.+const TAP_MIN = 120, DEMO_STEP = 2; // TAP_MIN: the smallest hit box in stage px, a child's finger on a phone+const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');+function tapWords(things, done) {+ const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';+ if (!CHAPTER.film) console.warn('tapWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');+ const list = things.filter(t => {+ const why = !t.word ? 'has no word' : !(t.g instanceof SVGGraphicsElement && stage.contains(t.g)) ? 'is not an element on the stage' : '';+ if (why) console.warn(`tapWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);+ return !why;+ }).map(t => {+ const clip = t.clip || wordClip(t.word), a = new Audio(`audio/${lang}/${clip}.mp3`);+ a.preload = 'auto';+ a.onerror = () => console.warn(`tapWords: "${t.word}" has no clip at audio/${lang}/${clip}.mp3`);+ return { g: t.g, word: t.word, clip, a, heard: false };+ });+ const L = mk('g', { class: 'tapw' }, stage); // the hit boxes, over the whole picture+ let on = false, alive = true, finished = false, word = null, shown = -1, bar, count, go;+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least TAP_MIN square+ const box = t => {+ const s = stage.getBoundingClientRect(), r = t.g.getBoundingClientRect(), kx = SW / s.width, ky = SH / s.height;+ return { x: (r.left - s.left) * kx, y: (r.top - s.top) * ky, w: r.width * kx, h: r.height * ky };+ };+ const place = () => {+ for (const t of list) {+ const b = box(t), w = Math.max(TAP_MIN, b.w), hh = Math.max(TAP_MIN, b.h);+ for (const [k, v] of Object.entries({ x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, width: w, height: hh })) t.ring.setAttribute(k, v.toFixed(1));+ }+ };+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };+ const idle = s => { for (const w of TW) if (w.tail === true && w.dur) w.pf(1 + s / w.dur); }; // s seconds past the end+ let since = 0;+ const tapIt = (k, quiet) => {+ const t = list[k];+ shown = k;+ t.heard = true;+ const n = list.filter(x => x.heard).length, all = n === list.length;+ count.textContent = all ? translate('allWordsHeard') : translate('wordsHeard', { k: n, n: list.length });+ if (all && !go.classList.contains('go')) { go.className = 'btn go'; bar.classList.add('lit'); }+ // the word beside the thing: above it, else below, right or left, wherever it fits on the stage (slid along that+ // side to stay on it)+ word?.remove();+ word = h('div', 'card word', t.word);+ word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word+ $('ui').append(word);+ const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;+ const spots = [[cx, b.y - hh - m], [cx, b.y + b.h + m], [b.x + b.w + m, cy], [b.x - w - m, cy]];+ const [x, y] = spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];+ Object.assign(word.style, { left: Math.max(m, Math.min(SW - m - w, x)) + 'px', top: Math.max(m, Math.min(SH - m - hh, y)) + 'px' });+ if (quiet) { word.style.animation = go.style.animation = 'none'; return; } // a video's frame shows it at rest+ for (const x of list) if (x !== t) x.a.pause();+ t.a.currentTime = 0;+ t.a.volume = +($('volume')?.value ?? 1);+ t.a.play().catch(() => {});+ SND.word = t.a; // soundTick ducks the music under it+ t.g.animate([{ translate: '0 0' }, { translate: '0 -18px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });+ t.ring.animate([{ strokeOpacity: 1 }, { strokeOpacity: 0 }], { duration: 700, easing: 'ease-out' });+ };+ const enable = () => {+ on = true;+ since = performance.now();+ linkStage(); // the stage is no longer one picture to a screen reader: the things are its buttons+ const at = new Map(list.map(t => [t, box(t)]));+ for (const [t, b] of at) if (b.x + b.w <= 0 || b.x >= SW || b.y + b.h <= 0 || b.y >= SH || !b.w || !b.h) console.warn(`tapWords: "${t.word}" is not in the picture`);+ // smaller boxes on top, so a thing standing in front of a bigger one can still be tapped+ for (const t of [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h)) { t.hit = G(L); t.ring = mk('rect', { class: 'ring', rx: 22 }, t.hit); }+ place();+ // the arrows go through the things from left to right+ const order = [...list].sort((a, c) => at.get(a).x + at.get(a).w / 2 - at.get(c).x - at.get(c).w / 2);+ const move = tappable(order.map(t => ({ g: t.hit, label: t.word, act: () => { if (alive) tapIt(list.indexOf(t)); } })));+ P.keys = e => e.key === 'Enter' ? (finish(), true) : move(e);+ bar = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'words');+ Object.assign(bar.style, { left: 'auto', right: '40px', width: 'auto' });+ count = h('span', 'count', translate('wordsHeard', { k: 0, n: list.length }));+ count.setAttribute('role', 'status');+ go = h('button', 'btn quiet', [translate('continue'), kbd('Enter')]);+ go.onclick = finish;+ bar.append(h('div', 'acts', [count, go]));+ };+ const cleanup = () => {+ alive = false;+ L.remove();+ for (const t of list) { t.a.onerror = null; t.a.pause(); t.a.removeAttribute('src'); t.a.load(); if (SND.word === t.a) SND.word = null; }+ if (P.tapDemo?.gen === gen) delete P.tapDemo;+ };+ P.cleanup.push(cleanup);+ P.tapDemo = { gen, clips: list.map(t => t.clip), step: DEMO_STEP, at(s) { if (!on) enable(); idle(s); const k = Math.min(list.length - 1, Math.floor(s / DEMO_STEP)); if (k >= 0 && k !== shown) { list.slice(0, k).forEach(t => { t.heard = true; }); tapIt(k, true); } } };+ const tick = () => {+ if (!alive) return;+ if (P.gen !== gen) { cleanup(); return; } // a seek away, or the beat replayed+ if (P.waiting && !on) enable();+ if (on && P.waiting) idle((performance.now() - since) / 1000);+ if (on) place();+ requestAnimationFrame(tick);+ };+ requestAnimationFrame(tick);+}// Rows of toggles. items: { parts, ans: [keys], why }. parts are math parts (strings, F, R) unless text: true,// where they are rich text (strings and $m(...)). cols: [[key, label, colour?, shortcut?]]. multi: several per row.const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) => {@@ -1279,7 +1397,9 @@ function soundTick(dt) {if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);- if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));+ // a word the reader tapped (tapWords) ducks the music at once, as the narration does+ const saying = !!SND.word && !SND.word.paused && !SND.word.ended;+ if (!held) SND.duck += Math.max(-dt * (saying ? 4 : 1), Math.min(dt, (wait && !saying || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));for (const m of SND.bgm) {const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says@@ -1741,7 +1861,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isShR, E, TX, $tex, texGraphic, texPart, OV, M, chip, tokens, layRow, collapse, mathEl, io, out, back, lin, tw, prog,show, hide, draw, pop, pulse, stream, mix, evalTo, soft, clockRun, camera, scene, S, AXIS, X, Y0, num, reset, tick,dot, arrow, panel, retire, header, fx, fxp, finishFx, pulseFx, shakeFx, floatText, TF, $, kbd, sources, uiW, SCORE,- h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, grid, rat, gcd, lowest, blanks,+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, tapWords, grid, rat, gcd, lowest, blanks,SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,finishCard };})();@@ -1768,7 +1888,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGINuiW = ENGINE.uiW, SCORE = ENGINE.SCORE, h = ENGINE.h, rich = ENGINE.rich, card = ENGINE.card,mascotEl = ENGINE.mascotEl, quiz = ENGINE.quiz, qlayer = ENGINE.qlayer, keyAct = ENGINE.keyAct,choice = ENGINE.choice, mcq = ENGINE.mcq, tap = ENGINE.tap, tapEls = ENGINE.tapEls, pickEls = ENGINE.pickEls,- grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,+ tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,SND = ENGINE.SND, P = ENGINE.P, HOLD = ENGINE.HOLD, clearCards = ENGINE.clearCards, seek = ENGINE.seek,seekInto = ENGINE.seekInto, start = ENGINE.start, setSpeed = ENGINE.setSpeed, setPlaying = ENGINE.setPlaying,setCaptions = ENGINE.setCaptions, hideCover = ENGINE.hideCover, restart = ENGINE.restart, boot = ENGINE.boot;diff --git a/site/pandemic-shorts/lib/engine.css b/site/pandemic-shorts/lib/engine.cssindex 7fc7cc5..bf42cc9 100644--- a/site/pandemic-shorts/lib/engine.css+++ b/site/pandemic-shorts/lib/engine.css@@ -103,6 +103,15 @@ button[hidden] { display: none; }.hit:hover .ring, .hit:focus .ring { stroke-opacity: .7; }.hit.bad .ring { stroke: var(--bad); stroke-opacity: 1; }.hit.good .ring { stroke: var(--good); stroke-opacity: 1; stroke-width: 4; }+/* Point and read (tapWords): hit boxes over the picture, the word beside a tapped thing, the counter and Continue.+ A book can set --word-font for its early-reader letters. */+.tapw .ring { stroke-width: 5; }+#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-left-width: 1.5px; border-radius: 30px; font: 600 96px/1.15 var(--word-font, var(--ui)); white-space: nowrap; animation: word-in .3s cubic-bezier(.3, 1.5, .6, 1); }+@keyframes word-in { from { transform: scale(.6); opacity: 0; } }+.card.words .acts { margin: 0; gap: 18px; }+.card.words .count { font-size: 24px; font-weight: 600; color: var(--dim); }+.card.words.lit .count { color: var(--good); }+.card.words.lit .btn.go { animation: word-in .4s cubic-bezier(.3, 1.5, .6, 1); }.tok { cursor: grab; touch-action: none; } /* a finger drags the chip instead of scrolling the page *//* Controls strip, part of the frame. */diff --git a/site/pandemic-shorts/lib/engine.js b/site/pandemic-shorts/lib/engine.jsindex b59277b..8f8c9bd 100644--- a/site/pandemic-shorts/lib/engine.js+++ b/site/pandemic-shorts/lib/engine.js@@ -49,6 +49,7 @@ const TRANSLATIONS = {chapterComplete: 'Chapter {n} complete', quickChecks: 'Quick checks', chapterPractice: 'Chapter practice', sources: 'Sources in this chapter', opensSource: '{label}, opens the source in a new tab',firstTryScore: '{name}: {r} of {t} right on the first try.', notAttempted: '{name}: not attempted.',watchAgain: 'Watch again', nextChapter: 'Next chapter',+ wordsHeard: '{k} / {n}', allWordsHeard: 'You found them all!',},ru: {decimalSign: ',', stageLabel: 'Анимация урока', stageLabelTitled: 'Анимация урока: {title}',@@ -86,6 +87,7 @@ const TRANSLATIONS = {chapterComplete: 'Глава {n} пройдена', quickChecks: 'Проверь себя', chapterPractice: 'Практика по главе', sources: 'Источники этой главы', opensSource: '{label}, источник откроется в новой вкладке',firstTryScore: '{name}: {r} из {t} верно с первой попытки.', notAttempted: '{name}: не выполнялось.',watchAgain: 'Смотреть снова', nextChapter: 'Следующая глава',+ wordsHeard: '{k} / {n}', allWordsHeard: 'Все найдены!',},zh: {decimalSign: '.', stageLabel: '课程动画', stageLabelTitled: '课程动画:{title}',@@ -121,6 +123,7 @@ const TRANSLATIONS = {chapterComplete: '第 {n} 章完成', quickChecks: '小测', chapterPractice: '本章练习', sources: '本章引用的文献', opensSource: '{label},在新标签页中打开来源',firstTryScore: '{name}:第一次就答对 {r} / {t}。', notAttempted: '{name}:未作答。',watchAgain: '再看一遍', nextChapter: '下一章',+ wordsHeard: '{k} / {n}', allWordsHeard: '全都找到啦!',},ja: {decimalSign: '.', stageLabel: '授業アニメーション', stageLabelTitled: '授業アニメーション:{title}',@@ -156,6 +159,7 @@ const TRANSLATIONS = {chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',watchAgain: 'もう一度見る', nextChapter: '次の章へ',+ wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',},};// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.@@ -951,35 +955,42 @@ function mcq(id, onRight) {if (!it) throw new Error(`mcq: no item "${id}" in items.js`);return { id, prompt: it.prompt, build: choice(it.opts, it.right, it.why, it.no, onRight) };}-// The shared body of the stage tap questions: each target ring is a focusable button that answers on click or Enter,-// and ← / → move the focus between them. targets: [{ g, label, ok, wrong }], wrong() shakes and returns the explanation.-const stageTap = (api, targets, show, yes, onRight, hint) => {- let locked = false, cur = -1;- const gs = targets.map(({ g, label, ok, wrong }) => {+// Stage targets the reader taps: each g turns into a focusable button named by its label that acts on a click, Enter or+// Space. Returns key(e) for P.keys: ← / → move the focus between them (from the one focused, if any).+function tappable(items) { // [{ g, label, act }]+ for (const { g, label, act } of items) {g.classList.add('hit');g.setAttribute('tabindex', 0);g.setAttribute('role', 'button');g.setAttribute('aria-label', label);- const act = () => {- if (locked) return;- if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }- else { g.classList.add('bad'); api.grade(false, wrong()); }- };g.addEventListener('click', act);keyAct(g, act);- return g;- });+ }+ let cur = -1;+ return e => {+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;+ if (!d || !items.length) return false;+ const on = items.findIndex(it => it.g === document.activeElement);+ if (on >= 0) cur = on;+ cur = cur < 0 ? (d > 0 ? 0 : items.length - 1) : (cur + d + items.length) % items.length;+ items[cur].g.focus();+ return true;+ };+}+// The shared body of the stage tap questions: each target ring answers on click or Enter (tappable), wrong() shakes+// and returns the explanation. targets: [{ g, label, ok, wrong }].+const stageTap = (api, targets, show, yes, onRight, hint) => {+ let locked = false;+ const move = tappable(targets.map(({ g, label, ok, wrong }) => ({ g, label, act: () => {+ if (locked) return;+ if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }+ else { g.classList.add('bad'); api.grade(false, wrong()); }+ } })));return {- reveal() { gs[show].classList.add('good'); onRight?.(); return yes; },+ reveal() { targets[show].g.classList.add('good'); onRight?.(); return yes; },lock() { locked = true; },hint,- key(e) {- const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;- if (!d || locked) return false;- cur = cur < 0 ? (d > 0 ? 0 : gs.length - 1) : (cur + d + gs.length) % gs.length;- gs[cur].focus();- return true;- },+ key: e => !locked && move(e),};};// Tap a point on the stage number line.@@ -1015,6 +1026,113 @@ const pickEls = (items, right, yes, no, onRight) => (body, api) => {return stageTap(api, targets, right, yes, onRight,[kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClick')]);};+/* ---------- point and read ---------- */+// A picture book's point-and-read moment, for young children learning words: a film's ask beat (CHAPTER.film),+// ask: done => tapWords(THINGS, done) THINGS = [{ g, word: 'basket', clip: 'w-basket' }]+// which the scene fills as it builds the picture; g is what the reader taps (a puppet, a prop, or a transparent shape+// over part of a baked set) and clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default. Once the narration is+// over (P.waiting) every thing turns tappable: a tap hops it, shows its word in large letters beside it and says it, on+// wall time at the player volume with the music ducked under it; tapping again says it again. A counter and Continue+// (Enter) wait in the corner; Continue lights up once every thing has been heard. Seeking or replaying clears it all.+// While the page waits, the scene's continuous motion (stopClock, clockRun with tail: true) runs on past the beat's+// end on wall time, so puppets posed as a function of time keep breathing and blinking.+// The video (export.py) cannot tap: P.tapDemo.at(s) shows the words one by one, DEMO_STEP s apart, while it holds.+const TAP_MIN = 120, DEMO_STEP = 2; // TAP_MIN: the smallest hit box in stage px, a child's finger on a phone+const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');+function tapWords(things, done) {+ const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';+ if (!CHAPTER.film) console.warn('tapWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');+ const list = things.filter(t => {+ const why = !t.word ? 'has no word' : !(t.g instanceof SVGGraphicsElement && stage.contains(t.g)) ? 'is not an element on the stage' : '';+ if (why) console.warn(`tapWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);+ return !why;+ }).map(t => {+ const clip = t.clip || wordClip(t.word), a = new Audio(`audio/${lang}/${clip}.mp3`);+ a.preload = 'auto';+ a.onerror = () => console.warn(`tapWords: "${t.word}" has no clip at audio/${lang}/${clip}.mp3`);+ return { g: t.g, word: t.word, clip, a, heard: false };+ });+ const L = mk('g', { class: 'tapw' }, stage); // the hit boxes, over the whole picture+ let on = false, alive = true, finished = false, word = null, shown = -1, bar, count, go;+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least TAP_MIN square+ const box = t => {+ const s = stage.getBoundingClientRect(), r = t.g.getBoundingClientRect(), kx = SW / s.width, ky = SH / s.height;+ return { x: (r.left - s.left) * kx, y: (r.top - s.top) * ky, w: r.width * kx, h: r.height * ky };+ };+ const place = () => {+ for (const t of list) {+ const b = box(t), w = Math.max(TAP_MIN, b.w), hh = Math.max(TAP_MIN, b.h);+ for (const [k, v] of Object.entries({ x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, width: w, height: hh })) t.ring.setAttribute(k, v.toFixed(1));+ }+ };+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };+ const idle = s => { for (const w of TW) if (w.tail === true && w.dur) w.pf(1 + s / w.dur); }; // s seconds past the end+ let since = 0;+ const tapIt = (k, quiet) => {+ const t = list[k];+ shown = k;+ t.heard = true;+ const n = list.filter(x => x.heard).length, all = n === list.length;+ count.textContent = all ? translate('allWordsHeard') : translate('wordsHeard', { k: n, n: list.length });+ if (all && !go.classList.contains('go')) { go.className = 'btn go'; bar.classList.add('lit'); }+ // the word beside the thing: above it, else below, right or left, wherever it fits on the stage (slid along that+ // side to stay on it)+ word?.remove();+ word = h('div', 'card word', t.word);+ word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word+ $('ui').append(word);+ const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;+ const spots = [[cx, b.y - hh - m], [cx, b.y + b.h + m], [b.x + b.w + m, cy], [b.x - w - m, cy]];+ const [x, y] = spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];+ Object.assign(word.style, { left: Math.max(m, Math.min(SW - m - w, x)) + 'px', top: Math.max(m, Math.min(SH - m - hh, y)) + 'px' });+ if (quiet) { word.style.animation = go.style.animation = 'none'; return; } // a video's frame shows it at rest+ for (const x of list) if (x !== t) x.a.pause();+ t.a.currentTime = 0;+ t.a.volume = +($('volume')?.value ?? 1);+ t.a.play().catch(() => {});+ SND.word = t.a; // soundTick ducks the music under it+ t.g.animate([{ translate: '0 0' }, { translate: '0 -18px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });+ t.ring.animate([{ strokeOpacity: 1 }, { strokeOpacity: 0 }], { duration: 700, easing: 'ease-out' });+ };+ const enable = () => {+ on = true;+ since = performance.now();+ linkStage(); // the stage is no longer one picture to a screen reader: the things are its buttons+ const at = new Map(list.map(t => [t, box(t)]));+ for (const [t, b] of at) if (b.x + b.w <= 0 || b.x >= SW || b.y + b.h <= 0 || b.y >= SH || !b.w || !b.h) console.warn(`tapWords: "${t.word}" is not in the picture`);+ // smaller boxes on top, so a thing standing in front of a bigger one can still be tapped+ for (const t of [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h)) { t.hit = G(L); t.ring = mk('rect', { class: 'ring', rx: 22 }, t.hit); }+ place();+ // the arrows go through the things from left to right+ const order = [...list].sort((a, c) => at.get(a).x + at.get(a).w / 2 - at.get(c).x - at.get(c).w / 2);+ const move = tappable(order.map(t => ({ g: t.hit, label: t.word, act: () => { if (alive) tapIt(list.indexOf(t)); } })));+ P.keys = e => e.key === 'Enter' ? (finish(), true) : move(e);+ bar = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'words');+ Object.assign(bar.style, { left: 'auto', right: '40px', width: 'auto' });+ count = h('span', 'count', translate('wordsHeard', { k: 0, n: list.length }));+ count.setAttribute('role', 'status');+ go = h('button', 'btn quiet', [translate('continue'), kbd('Enter')]);+ go.onclick = finish;+ bar.append(h('div', 'acts', [count, go]));+ };+ const cleanup = () => {+ alive = false;+ L.remove();+ for (const t of list) { t.a.onerror = null; t.a.pause(); t.a.removeAttribute('src'); t.a.load(); if (SND.word === t.a) SND.word = null; }+ if (P.tapDemo?.gen === gen) delete P.tapDemo;+ };+ P.cleanup.push(cleanup);+ P.tapDemo = { gen, clips: list.map(t => t.clip), step: DEMO_STEP, at(s) { if (!on) enable(); idle(s); const k = Math.min(list.length - 1, Math.floor(s / DEMO_STEP)); if (k >= 0 && k !== shown) { list.slice(0, k).forEach(t => { t.heard = true; }); tapIt(k, true); } } };+ const tick = () => {+ if (!alive) return;+ if (P.gen !== gen) { cleanup(); return; } // a seek away, or the beat replayed+ if (P.waiting && !on) enable();+ if (on && P.waiting) idle((performance.now() - since) / 1000);+ if (on) place();+ requestAnimationFrame(tick);+ };+ requestAnimationFrame(tick);+}// Rows of toggles. items: { parts, ans: [keys], why }. parts are math parts (strings, F, R) unless text: true,// where they are rich text (strings and $m(...)). cols: [[key, label, colour?, shortcut?]]. multi: several per row.const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) => {@@ -1279,7 +1397,9 @@ function soundTick(dt) {if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);- if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));+ // a word the reader tapped (tapWords) ducks the music at once, as the narration does+ const saying = !!SND.word && !SND.word.paused && !SND.word.ended;+ if (!held) SND.duck += Math.max(-dt * (saying ? 4 : 1), Math.min(dt, (wait && !saying || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));for (const m of SND.bgm) {const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says@@ -1741,7 +1861,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isShR, E, TX, $tex, texGraphic, texPart, OV, M, chip, tokens, layRow, collapse, mathEl, io, out, back, lin, tw, prog,show, hide, draw, pop, pulse, stream, mix, evalTo, soft, clockRun, camera, scene, S, AXIS, X, Y0, num, reset, tick,dot, arrow, panel, retire, header, fx, fxp, finishFx, pulseFx, shakeFx, floatText, TF, $, kbd, sources, uiW, SCORE,- h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, grid, rat, gcd, lowest, blanks,+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, tapWords, grid, rat, gcd, lowest, blanks,SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,finishCard };})();@@ -1768,7 +1888,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGINuiW = ENGINE.uiW, SCORE = ENGINE.SCORE, h = ENGINE.h, rich = ENGINE.rich, card = ENGINE.card,mascotEl = ENGINE.mascotEl, quiz = ENGINE.quiz, qlayer = ENGINE.qlayer, keyAct = ENGINE.keyAct,choice = ENGINE.choice, mcq = ENGINE.mcq, tap = ENGINE.tap, tapEls = ENGINE.tapEls, pickEls = ENGINE.pickEls,- grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,+ tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,SND = ENGINE.SND, P = ENGINE.P, HOLD = ENGINE.HOLD, clearCards = ENGINE.clearCards, seek = ENGINE.seek,seekInto = ENGINE.seekInto, start = ENGINE.start, setSpeed = ENGINE.setSpeed, setPlaying = ENGINE.setPlaying,setCaptions = ENGINE.setCaptions, hideCover = ENGINE.hideCover, restart = ENGINE.restart, boot = ENGINE.boot;diff --git a/site/pandemic/lib/engine.css b/site/pandemic/lib/engine.cssindex 7fc7cc5..bf42cc9 100644--- a/site/pandemic/lib/engine.css+++ b/site/pandemic/lib/engine.css@@ -103,6 +103,15 @@ button[hidden] { display: none; }.hit:hover .ring, .hit:focus .ring { stroke-opacity: .7; }.hit.bad .ring { stroke: var(--bad); stroke-opacity: 1; }.hit.good .ring { stroke: var(--good); stroke-opacity: 1; stroke-width: 4; }+/* Point and read (tapWords): hit boxes over the picture, the word beside a tapped thing, the counter and Continue.+ A book can set --word-font for its early-reader letters. */+.tapw .ring { stroke-width: 5; }+#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-left-width: 1.5px; border-radius: 30px; font: 600 96px/1.15 var(--word-font, var(--ui)); white-space: nowrap; animation: word-in .3s cubic-bezier(.3, 1.5, .6, 1); }+@keyframes word-in { from { transform: scale(.6); opacity: 0; } }+.card.words .acts { margin: 0; gap: 18px; }+.card.words .count { font-size: 24px; font-weight: 600; color: var(--dim); }+.card.words.lit .count { color: var(--good); }+.card.words.lit .btn.go { animation: word-in .4s cubic-bezier(.3, 1.5, .6, 1); }.tok { cursor: grab; touch-action: none; } /* a finger drags the chip instead of scrolling the page *//* Controls strip, part of the frame. */diff --git a/site/pandemic/lib/engine.js b/site/pandemic/lib/engine.jsindex b59277b..8f8c9bd 100644--- a/site/pandemic/lib/engine.js+++ b/site/pandemic/lib/engine.js@@ -49,6 +49,7 @@ const TRANSLATIONS = {chapterComplete: 'Chapter {n} complete', quickChecks: 'Quick checks', chapterPractice: 'Chapter practice', sources: 'Sources in this chapter', opensSource: '{label}, opens the source in a new tab',firstTryScore: '{name}: {r} of {t} right on the first try.', notAttempted: '{name}: not attempted.',watchAgain: 'Watch again', nextChapter: 'Next chapter',+ wordsHeard: '{k} / {n}', allWordsHeard: 'You found them all!',},ru: {decimalSign: ',', stageLabel: 'Анимация урока', stageLabelTitled: 'Анимация урока: {title}',@@ -86,6 +87,7 @@ const TRANSLATIONS = {chapterComplete: 'Глава {n} пройдена', quickChecks: 'Проверь себя', chapterPractice: 'Практика по главе', sources: 'Источники этой главы', opensSource: '{label}, источник откроется в новой вкладке',firstTryScore: '{name}: {r} из {t} верно с первой попытки.', notAttempted: '{name}: не выполнялось.',watchAgain: 'Смотреть снова', nextChapter: 'Следующая глава',+ wordsHeard: '{k} / {n}', allWordsHeard: 'Все найдены!',},zh: {decimalSign: '.', stageLabel: '课程动画', stageLabelTitled: '课程动画:{title}',@@ -121,6 +123,7 @@ const TRANSLATIONS = {chapterComplete: '第 {n} 章完成', quickChecks: '小测', chapterPractice: '本章练习', sources: '本章引用的文献', opensSource: '{label},在新标签页中打开来源',firstTryScore: '{name}:第一次就答对 {r} / {t}。', notAttempted: '{name}:未作答。',watchAgain: '再看一遍', nextChapter: '下一章',+ wordsHeard: '{k} / {n}', allWordsHeard: '全都找到啦!',},ja: {decimalSign: '.', stageLabel: '授業アニメーション', stageLabelTitled: '授業アニメーション:{title}',@@ -156,6 +159,7 @@ const TRANSLATIONS = {chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',watchAgain: 'もう一度見る', nextChapter: '次の章へ',+ wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',},};// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.@@ -951,35 +955,42 @@ function mcq(id, onRight) {if (!it) throw new Error(`mcq: no item "${id}" in items.js`);return { id, prompt: it.prompt, build: choice(it.opts, it.right, it.why, it.no, onRight) };}-// The shared body of the stage tap questions: each target ring is a focusable button that answers on click or Enter,-// and ← / → move the focus between them. targets: [{ g, label, ok, wrong }], wrong() shakes and returns the explanation.-const stageTap = (api, targets, show, yes, onRight, hint) => {- let locked = false, cur = -1;- const gs = targets.map(({ g, label, ok, wrong }) => {+// Stage targets the reader taps: each g turns into a focusable button named by its label that acts on a click, Enter or+// Space. Returns key(e) for P.keys: ← / → move the focus between them (from the one focused, if any).+function tappable(items) { // [{ g, label, act }]+ for (const { g, label, act } of items) {g.classList.add('hit');g.setAttribute('tabindex', 0);g.setAttribute('role', 'button');g.setAttribute('aria-label', label);- const act = () => {- if (locked) return;- if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }- else { g.classList.add('bad'); api.grade(false, wrong()); }- };g.addEventListener('click', act);keyAct(g, act);- return g;- });+ }+ let cur = -1;+ return e => {+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;+ if (!d || !items.length) return false;+ const on = items.findIndex(it => it.g === document.activeElement);+ if (on >= 0) cur = on;+ cur = cur < 0 ? (d > 0 ? 0 : items.length - 1) : (cur + d + items.length) % items.length;+ items[cur].g.focus();+ return true;+ };+}+// The shared body of the stage tap questions: each target ring answers on click or Enter (tappable), wrong() shakes+// and returns the explanation. targets: [{ g, label, ok, wrong }].+const stageTap = (api, targets, show, yes, onRight, hint) => {+ let locked = false;+ const move = tappable(targets.map(({ g, label, ok, wrong }) => ({ g, label, act: () => {+ if (locked) return;+ if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }+ else { g.classList.add('bad'); api.grade(false, wrong()); }+ } })));return {- reveal() { gs[show].classList.add('good'); onRight?.(); return yes; },+ reveal() { targets[show].g.classList.add('good'); onRight?.(); return yes; },lock() { locked = true; },hint,- key(e) {- const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;- if (!d || locked) return false;- cur = cur < 0 ? (d > 0 ? 0 : gs.length - 1) : (cur + d + gs.length) % gs.length;- gs[cur].focus();- return true;- },+ key: e => !locked && move(e),};};// Tap a point on the stage number line.@@ -1015,6 +1026,113 @@ const pickEls = (items, right, yes, no, onRight) => (body, api) => {return stageTap(api, targets, right, yes, onRight,[kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClick')]);};+/* ---------- point and read ---------- */+// A picture book's point-and-read moment, for young children learning words: a film's ask beat (CHAPTER.film),+// ask: done => tapWords(THINGS, done) THINGS = [{ g, word: 'basket', clip: 'w-basket' }]+// which the scene fills as it builds the picture; g is what the reader taps (a puppet, a prop, or a transparent shape+// over part of a baked set) and clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default. Once the narration is+// over (P.waiting) every thing turns tappable: a tap hops it, shows its word in large letters beside it and says it, on+// wall time at the player volume with the music ducked under it; tapping again says it again. A counter and Continue+// (Enter) wait in the corner; Continue lights up once every thing has been heard. Seeking or replaying clears it all.+// While the page waits, the scene's continuous motion (stopClock, clockRun with tail: true) runs on past the beat's+// end on wall time, so puppets posed as a function of time keep breathing and blinking.+// The video (export.py) cannot tap: P.tapDemo.at(s) shows the words one by one, DEMO_STEP s apart, while it holds.+const TAP_MIN = 120, DEMO_STEP = 2; // TAP_MIN: the smallest hit box in stage px, a child's finger on a phone+const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');+function tapWords(things, done) {+ const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';+ if (!CHAPTER.film) console.warn('tapWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');+ const list = things.filter(t => {+ const why = !t.word ? 'has no word' : !(t.g instanceof SVGGraphicsElement && stage.contains(t.g)) ? 'is not an element on the stage' : '';+ if (why) console.warn(`tapWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);+ return !why;+ }).map(t => {+ const clip = t.clip || wordClip(t.word), a = new Audio(`audio/${lang}/${clip}.mp3`);+ a.preload = 'auto';+ a.onerror = () => console.warn(`tapWords: "${t.word}" has no clip at audio/${lang}/${clip}.mp3`);+ return { g: t.g, word: t.word, clip, a, heard: false };+ });+ const L = mk('g', { class: 'tapw' }, stage); // the hit boxes, over the whole picture+ let on = false, alive = true, finished = false, word = null, shown = -1, bar, count, go;+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least TAP_MIN square+ const box = t => {+ const s = stage.getBoundingClientRect(), r = t.g.getBoundingClientRect(), kx = SW / s.width, ky = SH / s.height;+ return { x: (r.left - s.left) * kx, y: (r.top - s.top) * ky, w: r.width * kx, h: r.height * ky };+ };+ const place = () => {+ for (const t of list) {+ const b = box(t), w = Math.max(TAP_MIN, b.w), hh = Math.max(TAP_MIN, b.h);+ for (const [k, v] of Object.entries({ x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, width: w, height: hh })) t.ring.setAttribute(k, v.toFixed(1));+ }+ };+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };+ const idle = s => { for (const w of TW) if (w.tail === true && w.dur) w.pf(1 + s / w.dur); }; // s seconds past the end+ let since = 0;+ const tapIt = (k, quiet) => {+ const t = list[k];+ shown = k;+ t.heard = true;+ const n = list.filter(x => x.heard).length, all = n === list.length;+ count.textContent = all ? translate('allWordsHeard') : translate('wordsHeard', { k: n, n: list.length });+ if (all && !go.classList.contains('go')) { go.className = 'btn go'; bar.classList.add('lit'); }+ // the word beside the thing: above it, else below, right or left, wherever it fits on the stage (slid along that+ // side to stay on it)+ word?.remove();+ word = h('div', 'card word', t.word);+ word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word+ $('ui').append(word);+ const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;+ const spots = [[cx, b.y - hh - m], [cx, b.y + b.h + m], [b.x + b.w + m, cy], [b.x - w - m, cy]];+ const [x, y] = spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];+ Object.assign(word.style, { left: Math.max(m, Math.min(SW - m - w, x)) + 'px', top: Math.max(m, Math.min(SH - m - hh, y)) + 'px' });+ if (quiet) { word.style.animation = go.style.animation = 'none'; return; } // a video's frame shows it at rest+ for (const x of list) if (x !== t) x.a.pause();+ t.a.currentTime = 0;+ t.a.volume = +($('volume')?.value ?? 1);+ t.a.play().catch(() => {});+ SND.word = t.a; // soundTick ducks the music under it+ t.g.animate([{ translate: '0 0' }, { translate: '0 -18px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });+ t.ring.animate([{ strokeOpacity: 1 }, { strokeOpacity: 0 }], { duration: 700, easing: 'ease-out' });+ };+ const enable = () => {+ on = true;+ since = performance.now();+ linkStage(); // the stage is no longer one picture to a screen reader: the things are its buttons+ const at = new Map(list.map(t => [t, box(t)]));+ for (const [t, b] of at) if (b.x + b.w <= 0 || b.x >= SW || b.y + b.h <= 0 || b.y >= SH || !b.w || !b.h) console.warn(`tapWords: "${t.word}" is not in the picture`);+ // smaller boxes on top, so a thing standing in front of a bigger one can still be tapped+ for (const t of [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h)) { t.hit = G(L); t.ring = mk('rect', { class: 'ring', rx: 22 }, t.hit); }+ place();+ // the arrows go through the things from left to right+ const order = [...list].sort((a, c) => at.get(a).x + at.get(a).w / 2 - at.get(c).x - at.get(c).w / 2);+ const move = tappable(order.map(t => ({ g: t.hit, label: t.word, act: () => { if (alive) tapIt(list.indexOf(t)); } })));+ P.keys = e => e.key === 'Enter' ? (finish(), true) : move(e);+ bar = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'words');+ Object.assign(bar.style, { left: 'auto', right: '40px', width: 'auto' });+ count = h('span', 'count', translate('wordsHeard', { k: 0, n: list.length }));+ count.setAttribute('role', 'status');+ go = h('button', 'btn quiet', [translate('continue'), kbd('Enter')]);+ go.onclick = finish;+ bar.append(h('div', 'acts', [count, go]));+ };+ const cleanup = () => {+ alive = false;+ L.remove();+ for (const t of list) { t.a.onerror = null; t.a.pause(); t.a.removeAttribute('src'); t.a.load(); if (SND.word === t.a) SND.word = null; }+ if (P.tapDemo?.gen === gen) delete P.tapDemo;+ };+ P.cleanup.push(cleanup);+ P.tapDemo = { gen, clips: list.map(t => t.clip), step: DEMO_STEP, at(s) { if (!on) enable(); idle(s); const k = Math.min(list.length - 1, Math.floor(s / DEMO_STEP)); if (k >= 0 && k !== shown) { list.slice(0, k).forEach(t => { t.heard = true; }); tapIt(k, true); } } };+ const tick = () => {+ if (!alive) return;+ if (P.gen !== gen) { cleanup(); return; } // a seek away, or the beat replayed+ if (P.waiting && !on) enable();+ if (on && P.waiting) idle((performance.now() - since) / 1000);+ if (on) place();+ requestAnimationFrame(tick);+ };+ requestAnimationFrame(tick);+}// Rows of toggles. items: { parts, ans: [keys], why }. parts are math parts (strings, F, R) unless text: true,// where they are rich text (strings and $m(...)). cols: [[key, label, colour?, shortcut?]]. multi: several per row.const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) => {@@ -1279,7 +1397,9 @@ function soundTick(dt) {if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);- if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));+ // a word the reader tapped (tapWords) ducks the music at once, as the narration does+ const saying = !!SND.word && !SND.word.paused && !SND.word.ended;+ if (!held) SND.duck += Math.max(-dt * (saying ? 4 : 1), Math.min(dt, (wait && !saying || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));for (const m of SND.bgm) {const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says@@ -1741,7 +1861,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isShR, E, TX, $tex, texGraphic, texPart, OV, M, chip, tokens, layRow, collapse, mathEl, io, out, back, lin, tw, prog,show, hide, draw, pop, pulse, stream, mix, evalTo, soft, clockRun, camera, scene, S, AXIS, X, Y0, num, reset, tick,dot, arrow, panel, retire, header, fx, fxp, finishFx, pulseFx, shakeFx, floatText, TF, $, kbd, sources, uiW, SCORE,- h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, grid, rat, gcd, lowest, blanks,+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, tapWords, grid, rat, gcd, lowest, blanks,SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,finishCard };})();@@ -1768,7 +1888,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGINuiW = ENGINE.uiW, SCORE = ENGINE.SCORE, h = ENGINE.h, rich = ENGINE.rich, card = ENGINE.card,mascotEl = ENGINE.mascotEl, quiz = ENGINE.quiz, qlayer = ENGINE.qlayer, keyAct = ENGINE.keyAct,choice = ENGINE.choice, mcq = ENGINE.mcq, tap = ENGINE.tap, tapEls = ENGINE.tapEls, pickEls = ENGINE.pickEls,- grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,+ tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,SND = ENGINE.SND, P = ENGINE.P, HOLD = ENGINE.HOLD, clearCards = ENGINE.clearCards, seek = ENGINE.seek,seekInto = ENGINE.seekInto, start = ENGINE.start, setSpeed = ENGINE.setSpeed, setPlaying = ENGINE.setPlaying,setCaptions = ENGINE.setCaptions, hideCover = ENGINE.hideCover, restart = ENGINE.restart, boot = ENGINE.boot;diff --git a/site/physics-notebook/lib/engine.css b/site/physics-notebook/lib/engine.cssindex 7fc7cc5..bf42cc9 100644--- a/site/physics-notebook/lib/engine.css+++ b/site/physics-notebook/lib/engine.css@@ -103,6 +103,15 @@ button[hidden] { display: none; }.hit:hover .ring, .hit:focus .ring { stroke-opacity: .7; }.hit.bad .ring { stroke: var(--bad); stroke-opacity: 1; }.hit.good .ring { stroke: var(--good); stroke-opacity: 1; stroke-width: 4; }+/* Point and read (tapWords): hit boxes over the picture, the word beside a tapped thing, the counter and Continue.+ A book can set --word-font for its early-reader letters. */+.tapw .ring { stroke-width: 5; }+#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-left-width: 1.5px; border-radius: 30px; font: 600 96px/1.15 var(--word-font, var(--ui)); white-space: nowrap; animation: word-in .3s cubic-bezier(.3, 1.5, .6, 1); }+@keyframes word-in { from { transform: scale(.6); opacity: 0; } }+.card.words .acts { margin: 0; gap: 18px; }+.card.words .count { font-size: 24px; font-weight: 600; color: var(--dim); }+.card.words.lit .count { color: var(--good); }+.card.words.lit .btn.go { animation: word-in .4s cubic-bezier(.3, 1.5, .6, 1); }.tok { cursor: grab; touch-action: none; } /* a finger drags the chip instead of scrolling the page *//* Controls strip, part of the frame. */diff --git a/site/physics-notebook/lib/engine.js b/site/physics-notebook/lib/engine.jsindex b59277b..8f8c9bd 100644--- a/site/physics-notebook/lib/engine.js+++ b/site/physics-notebook/lib/engine.js@@ -49,6 +49,7 @@ const TRANSLATIONS = {chapterComplete: 'Chapter {n} complete', quickChecks: 'Quick checks', chapterPractice: 'Chapter practice', sources: 'Sources in this chapter', opensSource: '{label}, opens the source in a new tab',firstTryScore: '{name}: {r} of {t} right on the first try.', notAttempted: '{name}: not attempted.',watchAgain: 'Watch again', nextChapter: 'Next chapter',+ wordsHeard: '{k} / {n}', allWordsHeard: 'You found them all!',},ru: {decimalSign: ',', stageLabel: 'Анимация урока', stageLabelTitled: 'Анимация урока: {title}',@@ -86,6 +87,7 @@ const TRANSLATIONS = {chapterComplete: 'Глава {n} пройдена', quickChecks: 'Проверь себя', chapterPractice: 'Практика по главе', sources: 'Источники этой главы', opensSource: '{label}, источник откроется в новой вкладке',firstTryScore: '{name}: {r} из {t} верно с первой попытки.', notAttempted: '{name}: не выполнялось.',watchAgain: 'Смотреть снова', nextChapter: 'Следующая глава',+ wordsHeard: '{k} / {n}', allWordsHeard: 'Все найдены!',},zh: {decimalSign: '.', stageLabel: '课程动画', stageLabelTitled: '课程动画:{title}',@@ -121,6 +123,7 @@ const TRANSLATIONS = {chapterComplete: '第 {n} 章完成', quickChecks: '小测', chapterPractice: '本章练习', sources: '本章引用的文献', opensSource: '{label},在新标签页中打开来源',firstTryScore: '{name}:第一次就答对 {r} / {t}。', notAttempted: '{name}:未作答。',watchAgain: '再看一遍', nextChapter: '下一章',+ wordsHeard: '{k} / {n}', allWordsHeard: '全都找到啦!',},ja: {decimalSign: '.', stageLabel: '授業アニメーション', stageLabelTitled: '授業アニメーション:{title}',@@ -156,6 +159,7 @@ const TRANSLATIONS = {chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',watchAgain: 'もう一度見る', nextChapter: '次の章へ',+ wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',},};// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.@@ -951,35 +955,42 @@ function mcq(id, onRight) {if (!it) throw new Error(`mcq: no item "${id}" in items.js`);return { id, prompt: it.prompt, build: choice(it.opts, it.right, it.why, it.no, onRight) };}-// The shared body of the stage tap questions: each target ring is a focusable button that answers on click or Enter,-// and ← / → move the focus between them. targets: [{ g, label, ok, wrong }], wrong() shakes and returns the explanation.-const stageTap = (api, targets, show, yes, onRight, hint) => {- let locked = false, cur = -1;- const gs = targets.map(({ g, label, ok, wrong }) => {+// Stage targets the reader taps: each g turns into a focusable button named by its label that acts on a click, Enter or+// Space. Returns key(e) for P.keys: ← / → move the focus between them (from the one focused, if any).+function tappable(items) { // [{ g, label, act }]+ for (const { g, label, act } of items) {g.classList.add('hit');g.setAttribute('tabindex', 0);g.setAttribute('role', 'button');g.setAttribute('aria-label', label);- const act = () => {- if (locked) return;- if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }- else { g.classList.add('bad'); api.grade(false, wrong()); }- };g.addEventListener('click', act);keyAct(g, act);- return g;- });+ }+ let cur = -1;+ return e => {+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;+ if (!d || !items.length) return false;+ const on = items.findIndex(it => it.g === document.activeElement);+ if (on >= 0) cur = on;+ cur = cur < 0 ? (d > 0 ? 0 : items.length - 1) : (cur + d + items.length) % items.length;+ items[cur].g.focus();+ return true;+ };+}+// The shared body of the stage tap questions: each target ring answers on click or Enter (tappable), wrong() shakes+// and returns the explanation. targets: [{ g, label, ok, wrong }].+const stageTap = (api, targets, show, yes, onRight, hint) => {+ let locked = false;+ const move = tappable(targets.map(({ g, label, ok, wrong }) => ({ g, label, act: () => {+ if (locked) return;+ if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }+ else { g.classList.add('bad'); api.grade(false, wrong()); }+ } })));return {- reveal() { gs[show].classList.add('good'); onRight?.(); return yes; },+ reveal() { targets[show].g.classList.add('good'); onRight?.(); return yes; },lock() { locked = true; },hint,- key(e) {- const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;- if (!d || locked) return false;- cur = cur < 0 ? (d > 0 ? 0 : gs.length - 1) : (cur + d + gs.length) % gs.length;- gs[cur].focus();- return true;- },+ key: e => !locked && move(e),};};// Tap a point on the stage number line.@@ -1015,6 +1026,113 @@ const pickEls = (items, right, yes, no, onRight) => (body, api) => {return stageTap(api, targets, right, yes, onRight,[kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClick')]);};+/* ---------- point and read ---------- */+// A picture book's point-and-read moment, for young children learning words: a film's ask beat (CHAPTER.film),+// ask: done => tapWords(THINGS, done) THINGS = [{ g, word: 'basket', clip: 'w-basket' }]+// which the scene fills as it builds the picture; g is what the reader taps (a puppet, a prop, or a transparent shape+// over part of a baked set) and clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default. Once the narration is+// over (P.waiting) every thing turns tappable: a tap hops it, shows its word in large letters beside it and says it, on+// wall time at the player volume with the music ducked under it; tapping again says it again. A counter and Continue+// (Enter) wait in the corner; Continue lights up once every thing has been heard. Seeking or replaying clears it all.+// While the page waits, the scene's continuous motion (stopClock, clockRun with tail: true) runs on past the beat's+// end on wall time, so puppets posed as a function of time keep breathing and blinking.+// The video (export.py) cannot tap: P.tapDemo.at(s) shows the words one by one, DEMO_STEP s apart, while it holds.+const TAP_MIN = 120, DEMO_STEP = 2; // TAP_MIN: the smallest hit box in stage px, a child's finger on a phone+const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');+function tapWords(things, done) {+ const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';+ if (!CHAPTER.film) console.warn('tapWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');+ const list = things.filter(t => {+ const why = !t.word ? 'has no word' : !(t.g instanceof SVGGraphicsElement && stage.contains(t.g)) ? 'is not an element on the stage' : '';+ if (why) console.warn(`tapWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);+ return !why;+ }).map(t => {+ const clip = t.clip || wordClip(t.word), a = new Audio(`audio/${lang}/${clip}.mp3`);+ a.preload = 'auto';+ a.onerror = () => console.warn(`tapWords: "${t.word}" has no clip at audio/${lang}/${clip}.mp3`);+ return { g: t.g, word: t.word, clip, a, heard: false };+ });+ const L = mk('g', { class: 'tapw' }, stage); // the hit boxes, over the whole picture+ let on = false, alive = true, finished = false, word = null, shown = -1, bar, count, go;+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least TAP_MIN square+ const box = t => {+ const s = stage.getBoundingClientRect(), r = t.g.getBoundingClientRect(), kx = SW / s.width, ky = SH / s.height;+ return { x: (r.left - s.left) * kx, y: (r.top - s.top) * ky, w: r.width * kx, h: r.height * ky };+ };+ const place = () => {+ for (const t of list) {+ const b = box(t), w = Math.max(TAP_MIN, b.w), hh = Math.max(TAP_MIN, b.h);+ for (const [k, v] of Object.entries({ x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, width: w, height: hh })) t.ring.setAttribute(k, v.toFixed(1));+ }+ };+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };+ const idle = s => { for (const w of TW) if (w.tail === true && w.dur) w.pf(1 + s / w.dur); }; // s seconds past the end+ let since = 0;+ const tapIt = (k, quiet) => {+ const t = list[k];+ shown = k;+ t.heard = true;+ const n = list.filter(x => x.heard).length, all = n === list.length;+ count.textContent = all ? translate('allWordsHeard') : translate('wordsHeard', { k: n, n: list.length });+ if (all && !go.classList.contains('go')) { go.className = 'btn go'; bar.classList.add('lit'); }+ // the word beside the thing: above it, else below, right or left, wherever it fits on the stage (slid along that+ // side to stay on it)+ word?.remove();+ word = h('div', 'card word', t.word);+ word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word+ $('ui').append(word);+ const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;+ const spots = [[cx, b.y - hh - m], [cx, b.y + b.h + m], [b.x + b.w + m, cy], [b.x - w - m, cy]];+ const [x, y] = spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];+ Object.assign(word.style, { left: Math.max(m, Math.min(SW - m - w, x)) + 'px', top: Math.max(m, Math.min(SH - m - hh, y)) + 'px' });+ if (quiet) { word.style.animation = go.style.animation = 'none'; return; } // a video's frame shows it at rest+ for (const x of list) if (x !== t) x.a.pause();+ t.a.currentTime = 0;+ t.a.volume = +($('volume')?.value ?? 1);+ t.a.play().catch(() => {});+ SND.word = t.a; // soundTick ducks the music under it+ t.g.animate([{ translate: '0 0' }, { translate: '0 -18px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });+ t.ring.animate([{ strokeOpacity: 1 }, { strokeOpacity: 0 }], { duration: 700, easing: 'ease-out' });+ };+ const enable = () => {+ on = true;+ since = performance.now();+ linkStage(); // the stage is no longer one picture to a screen reader: the things are its buttons+ const at = new Map(list.map(t => [t, box(t)]));+ for (const [t, b] of at) if (b.x + b.w <= 0 || b.x >= SW || b.y + b.h <= 0 || b.y >= SH || !b.w || !b.h) console.warn(`tapWords: "${t.word}" is not in the picture`);+ // smaller boxes on top, so a thing standing in front of a bigger one can still be tapped+ for (const t of [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h)) { t.hit = G(L); t.ring = mk('rect', { class: 'ring', rx: 22 }, t.hit); }+ place();+ // the arrows go through the things from left to right+ const order = [...list].sort((a, c) => at.get(a).x + at.get(a).w / 2 - at.get(c).x - at.get(c).w / 2);+ const move = tappable(order.map(t => ({ g: t.hit, label: t.word, act: () => { if (alive) tapIt(list.indexOf(t)); } })));+ P.keys = e => e.key === 'Enter' ? (finish(), true) : move(e);+ bar = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'words');+ Object.assign(bar.style, { left: 'auto', right: '40px', width: 'auto' });+ count = h('span', 'count', translate('wordsHeard', { k: 0, n: list.length }));+ count.setAttribute('role', 'status');+ go = h('button', 'btn quiet', [translate('continue'), kbd('Enter')]);+ go.onclick = finish;+ bar.append(h('div', 'acts', [count, go]));+ };+ const cleanup = () => {+ alive = false;+ L.remove();+ for (const t of list) { t.a.onerror = null; t.a.pause(); t.a.removeAttribute('src'); t.a.load(); if (SND.word === t.a) SND.word = null; }+ if (P.tapDemo?.gen === gen) delete P.tapDemo;+ };+ P.cleanup.push(cleanup);+ P.tapDemo = { gen, clips: list.map(t => t.clip), step: DEMO_STEP, at(s) { if (!on) enable(); idle(s); const k = Math.min(list.length - 1, Math.floor(s / DEMO_STEP)); if (k >= 0 && k !== shown) { list.slice(0, k).forEach(t => { t.heard = true; }); tapIt(k, true); } } };+ const tick = () => {+ if (!alive) return;+ if (P.gen !== gen) { cleanup(); return; } // a seek away, or the beat replayed+ if (P.waiting && !on) enable();+ if (on && P.waiting) idle((performance.now() - since) / 1000);+ if (on) place();+ requestAnimationFrame(tick);+ };+ requestAnimationFrame(tick);+}// Rows of toggles. items: { parts, ans: [keys], why }. parts are math parts (strings, F, R) unless text: true,// where they are rich text (strings and $m(...)). cols: [[key, label, colour?, shortcut?]]. multi: several per row.const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) => {@@ -1279,7 +1397,9 @@ function soundTick(dt) {if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);- if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));+ // a word the reader tapped (tapWords) ducks the music at once, as the narration does+ const saying = !!SND.word && !SND.word.paused && !SND.word.ended;+ if (!held) SND.duck += Math.max(-dt * (saying ? 4 : 1), Math.min(dt, (wait && !saying || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));for (const m of SND.bgm) {const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says@@ -1741,7 +1861,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isShR, E, TX, $tex, texGraphic, texPart, OV, M, chip, tokens, layRow, collapse, mathEl, io, out, back, lin, tw, prog,show, hide, draw, pop, pulse, stream, mix, evalTo, soft, clockRun, camera, scene, S, AXIS, X, Y0, num, reset, tick,dot, arrow, panel, retire, header, fx, fxp, finishFx, pulseFx, shakeFx, floatText, TF, $, kbd, sources, uiW, SCORE,- h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, grid, rat, gcd, lowest, blanks,+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, tapWords, grid, rat, gcd, lowest, blanks,SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,finishCard };})();@@ -1768,7 +1888,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGINuiW = ENGINE.uiW, SCORE = ENGINE.SCORE, h = ENGINE.h, rich = ENGINE.rich, card = ENGINE.card,mascotEl = ENGINE.mascotEl, quiz = ENGINE.quiz, qlayer = ENGINE.qlayer, keyAct = ENGINE.keyAct,choice = ENGINE.choice, mcq = ENGINE.mcq, tap = ENGINE.tap, tapEls = ENGINE.tapEls, pickEls = ENGINE.pickEls,- grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,+ tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,SND = ENGINE.SND, P = ENGINE.P, HOLD = ENGINE.HOLD, clearCards = ENGINE.clearCards, seek = ENGINE.seek,seekInto = ENGINE.seekInto, start = ENGINE.start, setSpeed = ENGINE.setSpeed, setPlaying = ENGINE.setPlaying,setCaptions = ENGINE.setCaptions, hideCover = ENGINE.hideCover, restart = ENGINE.restart, boot = ENGINE.boot;diff --git a/site/science/lib/engine.css b/site/science/lib/engine.cssindex 7fc7cc5..bf42cc9 100644--- a/site/science/lib/engine.css+++ b/site/science/lib/engine.css@@ -103,6 +103,15 @@ button[hidden] { display: none; }.hit:hover .ring, .hit:focus .ring { stroke-opacity: .7; }.hit.bad .ring { stroke: var(--bad); stroke-opacity: 1; }.hit.good .ring { stroke: var(--good); stroke-opacity: 1; stroke-width: 4; }+/* Point and read (tapWords): hit boxes over the picture, the word beside a tapped thing, the counter and Continue.+ A book can set --word-font for its early-reader letters. */+.tapw .ring { stroke-width: 5; }+#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-left-width: 1.5px; border-radius: 30px; font: 600 96px/1.15 var(--word-font, var(--ui)); white-space: nowrap; animation: word-in .3s cubic-bezier(.3, 1.5, .6, 1); }+@keyframes word-in { from { transform: scale(.6); opacity: 0; } }+.card.words .acts { margin: 0; gap: 18px; }+.card.words .count { font-size: 24px; font-weight: 600; color: var(--dim); }+.card.words.lit .count { color: var(--good); }+.card.words.lit .btn.go { animation: word-in .4s cubic-bezier(.3, 1.5, .6, 1); }.tok { cursor: grab; touch-action: none; } /* a finger drags the chip instead of scrolling the page *//* Controls strip, part of the frame. */diff --git a/site/science/lib/engine.js b/site/science/lib/engine.jsindex b59277b..8f8c9bd 100644--- a/site/science/lib/engine.js+++ b/site/science/lib/engine.js@@ -49,6 +49,7 @@ const TRANSLATIONS = {chapterComplete: 'Chapter {n} complete', quickChecks: 'Quick checks', chapterPractice: 'Chapter practice', sources: 'Sources in this chapter', opensSource: '{label}, opens the source in a new tab',firstTryScore: '{name}: {r} of {t} right on the first try.', notAttempted: '{name}: not attempted.',watchAgain: 'Watch again', nextChapter: 'Next chapter',+ wordsHeard: '{k} / {n}', allWordsHeard: 'You found them all!',},ru: {decimalSign: ',', stageLabel: 'Анимация урока', stageLabelTitled: 'Анимация урока: {title}',@@ -86,6 +87,7 @@ const TRANSLATIONS = {chapterComplete: 'Глава {n} пройдена', quickChecks: 'Проверь себя', chapterPractice: 'Практика по главе', sources: 'Источники этой главы', opensSource: '{label}, источник откроется в новой вкладке',firstTryScore: '{name}: {r} из {t} верно с первой попытки.', notAttempted: '{name}: не выполнялось.',watchAgain: 'Смотреть снова', nextChapter: 'Следующая глава',+ wordsHeard: '{k} / {n}', allWordsHeard: 'Все найдены!',},zh: {decimalSign: '.', stageLabel: '课程动画', stageLabelTitled: '课程动画:{title}',@@ -121,6 +123,7 @@ const TRANSLATIONS = {chapterComplete: '第 {n} 章完成', quickChecks: '小测', chapterPractice: '本章练习', sources: '本章引用的文献', opensSource: '{label},在新标签页中打开来源',firstTryScore: '{name}:第一次就答对 {r} / {t}。', notAttempted: '{name}:未作答。',watchAgain: '再看一遍', nextChapter: '下一章',+ wordsHeard: '{k} / {n}', allWordsHeard: '全都找到啦!',},ja: {decimalSign: '.', stageLabel: '授業アニメーション', stageLabelTitled: '授業アニメーション:{title}',@@ -156,6 +159,7 @@ const TRANSLATIONS = {chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',watchAgain: 'もう一度見る', nextChapter: '次の章へ',+ wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',},};// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.@@ -951,35 +955,42 @@ function mcq(id, onRight) {if (!it) throw new Error(`mcq: no item "${id}" in items.js`);return { id, prompt: it.prompt, build: choice(it.opts, it.right, it.why, it.no, onRight) };}-// The shared body of the stage tap questions: each target ring is a focusable button that answers on click or Enter,-// and ← / → move the focus between them. targets: [{ g, label, ok, wrong }], wrong() shakes and returns the explanation.-const stageTap = (api, targets, show, yes, onRight, hint) => {- let locked = false, cur = -1;- const gs = targets.map(({ g, label, ok, wrong }) => {+// Stage targets the reader taps: each g turns into a focusable button named by its label that acts on a click, Enter or+// Space. Returns key(e) for P.keys: ← / → move the focus between them (from the one focused, if any).+function tappable(items) { // [{ g, label, act }]+ for (const { g, label, act } of items) {g.classList.add('hit');g.setAttribute('tabindex', 0);g.setAttribute('role', 'button');g.setAttribute('aria-label', label);- const act = () => {- if (locked) return;- if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }- else { g.classList.add('bad'); api.grade(false, wrong()); }- };g.addEventListener('click', act);keyAct(g, act);- return g;- });+ }+ let cur = -1;+ return e => {+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;+ if (!d || !items.length) return false;+ const on = items.findIndex(it => it.g === document.activeElement);+ if (on >= 0) cur = on;+ cur = cur < 0 ? (d > 0 ? 0 : items.length - 1) : (cur + d + items.length) % items.length;+ items[cur].g.focus();+ return true;+ };+}+// The shared body of the stage tap questions: each target ring answers on click or Enter (tappable), wrong() shakes+// and returns the explanation. targets: [{ g, label, ok, wrong }].+const stageTap = (api, targets, show, yes, onRight, hint) => {+ let locked = false;+ const move = tappable(targets.map(({ g, label, ok, wrong }) => ({ g, label, act: () => {+ if (locked) return;+ if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }+ else { g.classList.add('bad'); api.grade(false, wrong()); }+ } })));return {- reveal() { gs[show].classList.add('good'); onRight?.(); return yes; },+ reveal() { targets[show].g.classList.add('good'); onRight?.(); return yes; },lock() { locked = true; },hint,- key(e) {- const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;- if (!d || locked) return false;- cur = cur < 0 ? (d > 0 ? 0 : gs.length - 1) : (cur + d + gs.length) % gs.length;- gs[cur].focus();- return true;- },+ key: e => !locked && move(e),};};// Tap a point on the stage number line.@@ -1015,6 +1026,113 @@ const pickEls = (items, right, yes, no, onRight) => (body, api) => {return stageTap(api, targets, right, yes, onRight,[kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClick')]);};+/* ---------- point and read ---------- */+// A picture book's point-and-read moment, for young children learning words: a film's ask beat (CHAPTER.film),+// ask: done => tapWords(THINGS, done) THINGS = [{ g, word: 'basket', clip: 'w-basket' }]+// which the scene fills as it builds the picture; g is what the reader taps (a puppet, a prop, or a transparent shape+// over part of a baked set) and clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default. Once the narration is+// over (P.waiting) every thing turns tappable: a tap hops it, shows its word in large letters beside it and says it, on+// wall time at the player volume with the music ducked under it; tapping again says it again. A counter and Continue+// (Enter) wait in the corner; Continue lights up once every thing has been heard. Seeking or replaying clears it all.+// While the page waits, the scene's continuous motion (stopClock, clockRun with tail: true) runs on past the beat's+// end on wall time, so puppets posed as a function of time keep breathing and blinking.+// The video (export.py) cannot tap: P.tapDemo.at(s) shows the words one by one, DEMO_STEP s apart, while it holds.+const TAP_MIN = 120, DEMO_STEP = 2; // TAP_MIN: the smallest hit box in stage px, a child's finger on a phone+const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');+function tapWords(things, done) {+ const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';+ if (!CHAPTER.film) console.warn('tapWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');+ const list = things.filter(t => {+ const why = !t.word ? 'has no word' : !(t.g instanceof SVGGraphicsElement && stage.contains(t.g)) ? 'is not an element on the stage' : '';+ if (why) console.warn(`tapWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);+ return !why;+ }).map(t => {+ const clip = t.clip || wordClip(t.word), a = new Audio(`audio/${lang}/${clip}.mp3`);+ a.preload = 'auto';+ a.onerror = () => console.warn(`tapWords: "${t.word}" has no clip at audio/${lang}/${clip}.mp3`);+ return { g: t.g, word: t.word, clip, a, heard: false };+ });+ const L = mk('g', { class: 'tapw' }, stage); // the hit boxes, over the whole picture+ let on = false, alive = true, finished = false, word = null, shown = -1, bar, count, go;+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least TAP_MIN square+ const box = t => {+ const s = stage.getBoundingClientRect(), r = t.g.getBoundingClientRect(), kx = SW / s.width, ky = SH / s.height;+ return { x: (r.left - s.left) * kx, y: (r.top - s.top) * ky, w: r.width * kx, h: r.height * ky };+ };+ const place = () => {+ for (const t of list) {+ const b = box(t), w = Math.max(TAP_MIN, b.w), hh = Math.max(TAP_MIN, b.h);+ for (const [k, v] of Object.entries({ x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, width: w, height: hh })) t.ring.setAttribute(k, v.toFixed(1));+ }+ };+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };+ const idle = s => { for (const w of TW) if (w.tail === true && w.dur) w.pf(1 + s / w.dur); }; // s seconds past the end+ let since = 0;+ const tapIt = (k, quiet) => {+ const t = list[k];+ shown = k;+ t.heard = true;+ const n = list.filter(x => x.heard).length, all = n === list.length;+ count.textContent = all ? translate('allWordsHeard') : translate('wordsHeard', { k: n, n: list.length });+ if (all && !go.classList.contains('go')) { go.className = 'btn go'; bar.classList.add('lit'); }+ // the word beside the thing: above it, else below, right or left, wherever it fits on the stage (slid along that+ // side to stay on it)+ word?.remove();+ word = h('div', 'card word', t.word);+ word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word+ $('ui').append(word);+ const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;+ const spots = [[cx, b.y - hh - m], [cx, b.y + b.h + m], [b.x + b.w + m, cy], [b.x - w - m, cy]];+ const [x, y] = spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];+ Object.assign(word.style, { left: Math.max(m, Math.min(SW - m - w, x)) + 'px', top: Math.max(m, Math.min(SH - m - hh, y)) + 'px' });+ if (quiet) { word.style.animation = go.style.animation = 'none'; return; } // a video's frame shows it at rest+ for (const x of list) if (x !== t) x.a.pause();+ t.a.currentTime = 0;+ t.a.volume = +($('volume')?.value ?? 1);+ t.a.play().catch(() => {});+ SND.word = t.a; // soundTick ducks the music under it+ t.g.animate([{ translate: '0 0' }, { translate: '0 -18px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });+ t.ring.animate([{ strokeOpacity: 1 }, { strokeOpacity: 0 }], { duration: 700, easing: 'ease-out' });+ };+ const enable = () => {+ on = true;+ since = performance.now();+ linkStage(); // the stage is no longer one picture to a screen reader: the things are its buttons+ const at = new Map(list.map(t => [t, box(t)]));+ for (const [t, b] of at) if (b.x + b.w <= 0 || b.x >= SW || b.y + b.h <= 0 || b.y >= SH || !b.w || !b.h) console.warn(`tapWords: "${t.word}" is not in the picture`);+ // smaller boxes on top, so a thing standing in front of a bigger one can still be tapped+ for (const t of [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h)) { t.hit = G(L); t.ring = mk('rect', { class: 'ring', rx: 22 }, t.hit); }+ place();+ // the arrows go through the things from left to right+ const order = [...list].sort((a, c) => at.get(a).x + at.get(a).w / 2 - at.get(c).x - at.get(c).w / 2);+ const move = tappable(order.map(t => ({ g: t.hit, label: t.word, act: () => { if (alive) tapIt(list.indexOf(t)); } })));+ P.keys = e => e.key === 'Enter' ? (finish(), true) : move(e);+ bar = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'words');+ Object.assign(bar.style, { left: 'auto', right: '40px', width: 'auto' });+ count = h('span', 'count', translate('wordsHeard', { k: 0, n: list.length }));+ count.setAttribute('role', 'status');+ go = h('button', 'btn quiet', [translate('continue'), kbd('Enter')]);+ go.onclick = finish;+ bar.append(h('div', 'acts', [count, go]));+ };+ const cleanup = () => {+ alive = false;+ L.remove();+ for (const t of list) { t.a.onerror = null; t.a.pause(); t.a.removeAttribute('src'); t.a.load(); if (SND.word === t.a) SND.word = null; }+ if (P.tapDemo?.gen === gen) delete P.tapDemo;+ };+ P.cleanup.push(cleanup);+ P.tapDemo = { gen, clips: list.map(t => t.clip), step: DEMO_STEP, at(s) { if (!on) enable(); idle(s); const k = Math.min(list.length - 1, Math.floor(s / DEMO_STEP)); if (k >= 0 && k !== shown) { list.slice(0, k).forEach(t => { t.heard = true; }); tapIt(k, true); } } };+ const tick = () => {+ if (!alive) return;+ if (P.gen !== gen) { cleanup(); return; } // a seek away, or the beat replayed+ if (P.waiting && !on) enable();+ if (on && P.waiting) idle((performance.now() - since) / 1000);+ if (on) place();+ requestAnimationFrame(tick);+ };+ requestAnimationFrame(tick);+}// Rows of toggles. items: { parts, ans: [keys], why }. parts are math parts (strings, F, R) unless text: true,// where they are rich text (strings and $m(...)). cols: [[key, label, colour?, shortcut?]]. multi: several per row.const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) => {@@ -1279,7 +1397,9 @@ function soundTick(dt) {if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);- if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));+ // a word the reader tapped (tapWords) ducks the music at once, as the narration does+ const saying = !!SND.word && !SND.word.paused && !SND.word.ended;+ if (!held) SND.duck += Math.max(-dt * (saying ? 4 : 1), Math.min(dt, (wait && !saying || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));for (const m of SND.bgm) {const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says@@ -1741,7 +1861,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isShR, E, TX, $tex, texGraphic, texPart, OV, M, chip, tokens, layRow, collapse, mathEl, io, out, back, lin, tw, prog,show, hide, draw, pop, pulse, stream, mix, evalTo, soft, clockRun, camera, scene, S, AXIS, X, Y0, num, reset, tick,dot, arrow, panel, retire, header, fx, fxp, finishFx, pulseFx, shakeFx, floatText, TF, $, kbd, sources, uiW, SCORE,- h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, grid, rat, gcd, lowest, blanks,+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, tapWords, grid, rat, gcd, lowest, blanks,SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,finishCard };})();@@ -1768,7 +1888,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGINuiW = ENGINE.uiW, SCORE = ENGINE.SCORE, h = ENGINE.h, rich = ENGINE.rich, card = ENGINE.card,mascotEl = ENGINE.mascotEl, quiz = ENGINE.quiz, qlayer = ENGINE.qlayer, keyAct = ENGINE.keyAct,choice = ENGINE.choice, mcq = ENGINE.mcq, tap = ENGINE.tap, tapEls = ENGINE.tapEls, pickEls = ENGINE.pickEls,- grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,+ tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,SND = ENGINE.SND, P = ENGINE.P, HOLD = ENGINE.HOLD, clearCards = ENGINE.clearCards, seek = ENGINE.seek,seekInto = ENGINE.seekInto, start = ENGINE.start, setSpeed = ENGINE.setSpeed, setPlaying = ENGINE.setPlaying,setCaptions = ENGINE.setCaptions, hideCover = ENGINE.hideCover, restart = ENGINE.restart, boot = ENGINE.boot;diff --git a/site/shooting-an-elephant/lib/engine.css b/site/shooting-an-elephant/lib/engine.cssindex 7fc7cc5..bf42cc9 100644--- a/site/shooting-an-elephant/lib/engine.css+++ b/site/shooting-an-elephant/lib/engine.css@@ -103,6 +103,15 @@ button[hidden] { display: none; }.hit:hover .ring, .hit:focus .ring { stroke-opacity: .7; }.hit.bad .ring { stroke: var(--bad); stroke-opacity: 1; }.hit.good .ring { stroke: var(--good); stroke-opacity: 1; stroke-width: 4; }+/* Point and read (tapWords): hit boxes over the picture, the word beside a tapped thing, the counter and Continue.+ A book can set --word-font for its early-reader letters. */+.tapw .ring { stroke-width: 5; }+#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-left-width: 1.5px; border-radius: 30px; font: 600 96px/1.15 var(--word-font, var(--ui)); white-space: nowrap; animation: word-in .3s cubic-bezier(.3, 1.5, .6, 1); }+@keyframes word-in { from { transform: scale(.6); opacity: 0; } }+.card.words .acts { margin: 0; gap: 18px; }+.card.words .count { font-size: 24px; font-weight: 600; color: var(--dim); }+.card.words.lit .count { color: var(--good); }+.card.words.lit .btn.go { animation: word-in .4s cubic-bezier(.3, 1.5, .6, 1); }.tok { cursor: grab; touch-action: none; } /* a finger drags the chip instead of scrolling the page *//* Controls strip, part of the frame. */diff --git a/site/shooting-an-elephant/lib/engine.js b/site/shooting-an-elephant/lib/engine.jsindex b59277b..8f8c9bd 100644--- a/site/shooting-an-elephant/lib/engine.js+++ b/site/shooting-an-elephant/lib/engine.js@@ -49,6 +49,7 @@ const TRANSLATIONS = {chapterComplete: 'Chapter {n} complete', quickChecks: 'Quick checks', chapterPractice: 'Chapter practice', sources: 'Sources in this chapter', opensSource: '{label}, opens the source in a new tab',firstTryScore: '{name}: {r} of {t} right on the first try.', notAttempted: '{name}: not attempted.',watchAgain: 'Watch again', nextChapter: 'Next chapter',+ wordsHeard: '{k} / {n}', allWordsHeard: 'You found them all!',},ru: {decimalSign: ',', stageLabel: 'Анимация урока', stageLabelTitled: 'Анимация урока: {title}',@@ -86,6 +87,7 @@ const TRANSLATIONS = {chapterComplete: 'Глава {n} пройдена', quickChecks: 'Проверь себя', chapterPractice: 'Практика по главе', sources: 'Источники этой главы', opensSource: '{label}, источник откроется в новой вкладке',firstTryScore: '{name}: {r} из {t} верно с первой попытки.', notAttempted: '{name}: не выполнялось.',watchAgain: 'Смотреть снова', nextChapter: 'Следующая глава',+ wordsHeard: '{k} / {n}', allWordsHeard: 'Все найдены!',},zh: {decimalSign: '.', stageLabel: '课程动画', stageLabelTitled: '课程动画:{title}',@@ -121,6 +123,7 @@ const TRANSLATIONS = {chapterComplete: '第 {n} 章完成', quickChecks: '小测', chapterPractice: '本章练习', sources: '本章引用的文献', opensSource: '{label},在新标签页中打开来源',firstTryScore: '{name}:第一次就答对 {r} / {t}。', notAttempted: '{name}:未作答。',watchAgain: '再看一遍', nextChapter: '下一章',+ wordsHeard: '{k} / {n}', allWordsHeard: '全都找到啦!',},ja: {decimalSign: '.', stageLabel: '授業アニメーション', stageLabelTitled: '授業アニメーション:{title}',@@ -156,6 +159,7 @@ const TRANSLATIONS = {chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',watchAgain: 'もう一度見る', nextChapter: '次の章へ',+ wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',},};// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.@@ -951,35 +955,42 @@ function mcq(id, onRight) {if (!it) throw new Error(`mcq: no item "${id}" in items.js`);return { id, prompt: it.prompt, build: choice(it.opts, it.right, it.why, it.no, onRight) };}-// The shared body of the stage tap questions: each target ring is a focusable button that answers on click or Enter,-// and ← / → move the focus between them. targets: [{ g, label, ok, wrong }], wrong() shakes and returns the explanation.-const stageTap = (api, targets, show, yes, onRight, hint) => {- let locked = false, cur = -1;- const gs = targets.map(({ g, label, ok, wrong }) => {+// Stage targets the reader taps: each g turns into a focusable button named by its label that acts on a click, Enter or+// Space. Returns key(e) for P.keys: ← / → move the focus between them (from the one focused, if any).+function tappable(items) { // [{ g, label, act }]+ for (const { g, label, act } of items) {g.classList.add('hit');g.setAttribute('tabindex', 0);g.setAttribute('role', 'button');g.setAttribute('aria-label', label);- const act = () => {- if (locked) return;- if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }- else { g.classList.add('bad'); api.grade(false, wrong()); }- };g.addEventListener('click', act);keyAct(g, act);- return g;- });+ }+ let cur = -1;+ return e => {+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;+ if (!d || !items.length) return false;+ const on = items.findIndex(it => it.g === document.activeElement);+ if (on >= 0) cur = on;+ cur = cur < 0 ? (d > 0 ? 0 : items.length - 1) : (cur + d + items.length) % items.length;+ items[cur].g.focus();+ return true;+ };+}+// The shared body of the stage tap questions: each target ring answers on click or Enter (tappable), wrong() shakes+// and returns the explanation. targets: [{ g, label, ok, wrong }].+const stageTap = (api, targets, show, yes, onRight, hint) => {+ let locked = false;+ const move = tappable(targets.map(({ g, label, ok, wrong }) => ({ g, label, act: () => {+ if (locked) return;+ if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }+ else { g.classList.add('bad'); api.grade(false, wrong()); }+ } })));return {- reveal() { gs[show].classList.add('good'); onRight?.(); return yes; },+ reveal() { targets[show].g.classList.add('good'); onRight?.(); return yes; },lock() { locked = true; },hint,- key(e) {- const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;- if (!d || locked) return false;- cur = cur < 0 ? (d > 0 ? 0 : gs.length - 1) : (cur + d + gs.length) % gs.length;- gs[cur].focus();- return true;- },+ key: e => !locked && move(e),};};// Tap a point on the stage number line.@@ -1015,6 +1026,113 @@ const pickEls = (items, right, yes, no, onRight) => (body, api) => {return stageTap(api, targets, right, yes, onRight,[kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClick')]);};+/* ---------- point and read ---------- */+// A picture book's point-and-read moment, for young children learning words: a film's ask beat (CHAPTER.film),+// ask: done => tapWords(THINGS, done) THINGS = [{ g, word: 'basket', clip: 'w-basket' }]+// which the scene fills as it builds the picture; g is what the reader taps (a puppet, a prop, or a transparent shape+// over part of a baked set) and clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default. Once the narration is+// over (P.waiting) every thing turns tappable: a tap hops it, shows its word in large letters beside it and says it, on+// wall time at the player volume with the music ducked under it; tapping again says it again. A counter and Continue+// (Enter) wait in the corner; Continue lights up once every thing has been heard. Seeking or replaying clears it all.+// While the page waits, the scene's continuous motion (stopClock, clockRun with tail: true) runs on past the beat's+// end on wall time, so puppets posed as a function of time keep breathing and blinking.+// The video (export.py) cannot tap: P.tapDemo.at(s) shows the words one by one, DEMO_STEP s apart, while it holds.+const TAP_MIN = 120, DEMO_STEP = 2; // TAP_MIN: the smallest hit box in stage px, a child's finger on a phone+const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');+function tapWords(things, done) {+ const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';+ if (!CHAPTER.film) console.warn('tapWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');+ const list = things.filter(t => {+ const why = !t.word ? 'has no word' : !(t.g instanceof SVGGraphicsElement && stage.contains(t.g)) ? 'is not an element on the stage' : '';+ if (why) console.warn(`tapWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);+ return !why;+ }).map(t => {+ const clip = t.clip || wordClip(t.word), a = new Audio(`audio/${lang}/${clip}.mp3`);+ a.preload = 'auto';+ a.onerror = () => console.warn(`tapWords: "${t.word}" has no clip at audio/${lang}/${clip}.mp3`);+ return { g: t.g, word: t.word, clip, a, heard: false };+ });+ const L = mk('g', { class: 'tapw' }, stage); // the hit boxes, over the whole picture+ let on = false, alive = true, finished = false, word = null, shown = -1, bar, count, go;+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least TAP_MIN square+ const box = t => {+ const s = stage.getBoundingClientRect(), r = t.g.getBoundingClientRect(), kx = SW / s.width, ky = SH / s.height;+ return { x: (r.left - s.left) * kx, y: (r.top - s.top) * ky, w: r.width * kx, h: r.height * ky };+ };+ const place = () => {+ for (const t of list) {+ const b = box(t), w = Math.max(TAP_MIN, b.w), hh = Math.max(TAP_MIN, b.h);+ for (const [k, v] of Object.entries({ x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, width: w, height: hh })) t.ring.setAttribute(k, v.toFixed(1));+ }+ };+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };+ const idle = s => { for (const w of TW) if (w.tail === true && w.dur) w.pf(1 + s / w.dur); }; // s seconds past the end+ let since = 0;+ const tapIt = (k, quiet) => {+ const t = list[k];+ shown = k;+ t.heard = true;+ const n = list.filter(x => x.heard).length, all = n === list.length;+ count.textContent = all ? translate('allWordsHeard') : translate('wordsHeard', { k: n, n: list.length });+ if (all && !go.classList.contains('go')) { go.className = 'btn go'; bar.classList.add('lit'); }+ // the word beside the thing: above it, else below, right or left, wherever it fits on the stage (slid along that+ // side to stay on it)+ word?.remove();+ word = h('div', 'card word', t.word);+ word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word+ $('ui').append(word);+ const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;+ const spots = [[cx, b.y - hh - m], [cx, b.y + b.h + m], [b.x + b.w + m, cy], [b.x - w - m, cy]];+ const [x, y] = spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];+ Object.assign(word.style, { left: Math.max(m, Math.min(SW - m - w, x)) + 'px', top: Math.max(m, Math.min(SH - m - hh, y)) + 'px' });+ if (quiet) { word.style.animation = go.style.animation = 'none'; return; } // a video's frame shows it at rest+ for (const x of list) if (x !== t) x.a.pause();+ t.a.currentTime = 0;+ t.a.volume = +($('volume')?.value ?? 1);+ t.a.play().catch(() => {});+ SND.word = t.a; // soundTick ducks the music under it+ t.g.animate([{ translate: '0 0' }, { translate: '0 -18px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });+ t.ring.animate([{ strokeOpacity: 1 }, { strokeOpacity: 0 }], { duration: 700, easing: 'ease-out' });+ };+ const enable = () => {+ on = true;+ since = performance.now();+ linkStage(); // the stage is no longer one picture to a screen reader: the things are its buttons+ const at = new Map(list.map(t => [t, box(t)]));+ for (const [t, b] of at) if (b.x + b.w <= 0 || b.x >= SW || b.y + b.h <= 0 || b.y >= SH || !b.w || !b.h) console.warn(`tapWords: "${t.word}" is not in the picture`);+ // smaller boxes on top, so a thing standing in front of a bigger one can still be tapped+ for (const t of [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h)) { t.hit = G(L); t.ring = mk('rect', { class: 'ring', rx: 22 }, t.hit); }+ place();+ // the arrows go through the things from left to right+ const order = [...list].sort((a, c) => at.get(a).x + at.get(a).w / 2 - at.get(c).x - at.get(c).w / 2);+ const move = tappable(order.map(t => ({ g: t.hit, label: t.word, act: () => { if (alive) tapIt(list.indexOf(t)); } })));+ P.keys = e => e.key === 'Enter' ? (finish(), true) : move(e);+ bar = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'words');+ Object.assign(bar.style, { left: 'auto', right: '40px', width: 'auto' });+ count = h('span', 'count', translate('wordsHeard', { k: 0, n: list.length }));+ count.setAttribute('role', 'status');+ go = h('button', 'btn quiet', [translate('continue'), kbd('Enter')]);+ go.onclick = finish;+ bar.append(h('div', 'acts', [count, go]));+ };+ const cleanup = () => {+ alive = false;+ L.remove();+ for (const t of list) { t.a.onerror = null; t.a.pause(); t.a.removeAttribute('src'); t.a.load(); if (SND.word === t.a) SND.word = null; }+ if (P.tapDemo?.gen === gen) delete P.tapDemo;+ };+ P.cleanup.push(cleanup);+ P.tapDemo = { gen, clips: list.map(t => t.clip), step: DEMO_STEP, at(s) { if (!on) enable(); idle(s); const k = Math.min(list.length - 1, Math.floor(s / DEMO_STEP)); if (k >= 0 && k !== shown) { list.slice(0, k).forEach(t => { t.heard = true; }); tapIt(k, true); } } };+ const tick = () => {+ if (!alive) return;+ if (P.gen !== gen) { cleanup(); return; } // a seek away, or the beat replayed+ if (P.waiting && !on) enable();+ if (on && P.waiting) idle((performance.now() - since) / 1000);+ if (on) place();+ requestAnimationFrame(tick);+ };+ requestAnimationFrame(tick);+}// Rows of toggles. items: { parts, ans: [keys], why }. parts are math parts (strings, F, R) unless text: true,// where they are rich text (strings and $m(...)). cols: [[key, label, colour?, shortcut?]]. multi: several per row.const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) => {@@ -1279,7 +1397,9 @@ function soundTick(dt) {if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);- if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));+ // a word the reader tapped (tapWords) ducks the music at once, as the narration does+ const saying = !!SND.word && !SND.word.paused && !SND.word.ended;+ if (!held) SND.duck += Math.max(-dt * (saying ? 4 : 1), Math.min(dt, (wait && !saying || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));for (const m of SND.bgm) {const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says@@ -1741,7 +1861,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isShR, E, TX, $tex, texGraphic, texPart, OV, M, chip, tokens, layRow, collapse, mathEl, io, out, back, lin, tw, prog,show, hide, draw, pop, pulse, stream, mix, evalTo, soft, clockRun, camera, scene, S, AXIS, X, Y0, num, reset, tick,dot, arrow, panel, retire, header, fx, fxp, finishFx, pulseFx, shakeFx, floatText, TF, $, kbd, sources, uiW, SCORE,- h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, grid, rat, gcd, lowest, blanks,+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, tapWords, grid, rat, gcd, lowest, blanks,SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,finishCard };})();@@ -1768,7 +1888,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGINuiW = ENGINE.uiW, SCORE = ENGINE.SCORE, h = ENGINE.h, rich = ENGINE.rich, card = ENGINE.card,mascotEl = ENGINE.mascotEl, quiz = ENGINE.quiz, qlayer = ENGINE.qlayer, keyAct = ENGINE.keyAct,choice = ENGINE.choice, mcq = ENGINE.mcq, tap = ENGINE.tap, tapEls = ENGINE.tapEls, pickEls = ENGINE.pickEls,- grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,+ tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,SND = ENGINE.SND, P = ENGINE.P, HOLD = ENGINE.HOLD, clearCards = ENGINE.clearCards, seek = ENGINE.seek,seekInto = ENGINE.seekInto, start = ENGINE.start, setSpeed = ENGINE.setSpeed, setPlaying = ENGINE.setPlaying,setCaptions = ENGINE.setCaptions, hideCover = ENGINE.hideCover, restart = ENGINE.restart, boot = ENGINE.boot;diff --git a/site/three-little-pigs/lib/engine.css b/site/three-little-pigs/lib/engine.cssindex 7fc7cc5..bf42cc9 100644--- a/site/three-little-pigs/lib/engine.css+++ b/site/three-little-pigs/lib/engine.css@@ -103,6 +103,15 @@ button[hidden] { display: none; }.hit:hover .ring, .hit:focus .ring { stroke-opacity: .7; }.hit.bad .ring { stroke: var(--bad); stroke-opacity: 1; }.hit.good .ring { stroke: var(--good); stroke-opacity: 1; stroke-width: 4; }+/* Point and read (tapWords): hit boxes over the picture, the word beside a tapped thing, the counter and Continue.+ A book can set --word-font for its early-reader letters. */+.tapw .ring { stroke-width: 5; }+#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-left-width: 1.5px; border-radius: 30px; font: 600 96px/1.15 var(--word-font, var(--ui)); white-space: nowrap; animation: word-in .3s cubic-bezier(.3, 1.5, .6, 1); }+@keyframes word-in { from { transform: scale(.6); opacity: 0; } }+.card.words .acts { margin: 0; gap: 18px; }+.card.words .count { font-size: 24px; font-weight: 600; color: var(--dim); }+.card.words.lit .count { color: var(--good); }+.card.words.lit .btn.go { animation: word-in .4s cubic-bezier(.3, 1.5, .6, 1); }.tok { cursor: grab; touch-action: none; } /* a finger drags the chip instead of scrolling the page *//* Controls strip, part of the frame. */diff --git a/site/three-little-pigs/lib/engine.js b/site/three-little-pigs/lib/engine.jsindex b59277b..8f8c9bd 100644--- a/site/three-little-pigs/lib/engine.js+++ b/site/three-little-pigs/lib/engine.js@@ -49,6 +49,7 @@ const TRANSLATIONS = {chapterComplete: 'Chapter {n} complete', quickChecks: 'Quick checks', chapterPractice: 'Chapter practice', sources: 'Sources in this chapter', opensSource: '{label}, opens the source in a new tab',firstTryScore: '{name}: {r} of {t} right on the first try.', notAttempted: '{name}: not attempted.',watchAgain: 'Watch again', nextChapter: 'Next chapter',+ wordsHeard: '{k} / {n}', allWordsHeard: 'You found them all!',},ru: {decimalSign: ',', stageLabel: 'Анимация урока', stageLabelTitled: 'Анимация урока: {title}',@@ -86,6 +87,7 @@ const TRANSLATIONS = {chapterComplete: 'Глава {n} пройдена', quickChecks: 'Проверь себя', chapterPractice: 'Практика по главе', sources: 'Источники этой главы', opensSource: '{label}, источник откроется в новой вкладке',firstTryScore: '{name}: {r} из {t} верно с первой попытки.', notAttempted: '{name}: не выполнялось.',watchAgain: 'Смотреть снова', nextChapter: 'Следующая глава',+ wordsHeard: '{k} / {n}', allWordsHeard: 'Все найдены!',},zh: {decimalSign: '.', stageLabel: '课程动画', stageLabelTitled: '课程动画:{title}',@@ -121,6 +123,7 @@ const TRANSLATIONS = {chapterComplete: '第 {n} 章完成', quickChecks: '小测', chapterPractice: '本章练习', sources: '本章引用的文献', opensSource: '{label},在新标签页中打开来源',firstTryScore: '{name}:第一次就答对 {r} / {t}。', notAttempted: '{name}:未作答。',watchAgain: '再看一遍', nextChapter: '下一章',+ wordsHeard: '{k} / {n}', allWordsHeard: '全都找到啦!',},ja: {decimalSign: '.', stageLabel: '授業アニメーション', stageLabelTitled: '授業アニメーション:{title}',@@ -156,6 +159,7 @@ const TRANSLATIONS = {chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',watchAgain: 'もう一度見る', nextChapter: '次の章へ',+ wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',},};// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.@@ -951,35 +955,42 @@ function mcq(id, onRight) {if (!it) throw new Error(`mcq: no item "${id}" in items.js`);return { id, prompt: it.prompt, build: choice(it.opts, it.right, it.why, it.no, onRight) };}-// The shared body of the stage tap questions: each target ring is a focusable button that answers on click or Enter,-// and ← / → move the focus between them. targets: [{ g, label, ok, wrong }], wrong() shakes and returns the explanation.-const stageTap = (api, targets, show, yes, onRight, hint) => {- let locked = false, cur = -1;- const gs = targets.map(({ g, label, ok, wrong }) => {+// Stage targets the reader taps: each g turns into a focusable button named by its label that acts on a click, Enter or+// Space. Returns key(e) for P.keys: ← / → move the focus between them (from the one focused, if any).+function tappable(items) { // [{ g, label, act }]+ for (const { g, label, act } of items) {g.classList.add('hit');g.setAttribute('tabindex', 0);g.setAttribute('role', 'button');g.setAttribute('aria-label', label);- const act = () => {- if (locked) return;- if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }- else { g.classList.add('bad'); api.grade(false, wrong()); }- };g.addEventListener('click', act);keyAct(g, act);- return g;- });+ }+ let cur = -1;+ return e => {+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;+ if (!d || !items.length) return false;+ const on = items.findIndex(it => it.g === document.activeElement);+ if (on >= 0) cur = on;+ cur = cur < 0 ? (d > 0 ? 0 : items.length - 1) : (cur + d + items.length) % items.length;+ items[cur].g.focus();+ return true;+ };+}+// The shared body of the stage tap questions: each target ring answers on click or Enter (tappable), wrong() shakes+// and returns the explanation. targets: [{ g, label, ok, wrong }].+const stageTap = (api, targets, show, yes, onRight, hint) => {+ let locked = false;+ const move = tappable(targets.map(({ g, label, ok, wrong }) => ({ g, label, act: () => {+ if (locked) return;+ if (ok) { g.classList.add('good'); onRight?.(); api.grade(true, yes); }+ else { g.classList.add('bad'); api.grade(false, wrong()); }+ } })));return {- reveal() { gs[show].classList.add('good'); onRight?.(); return yes; },+ reveal() { targets[show].g.classList.add('good'); onRight?.(); return yes; },lock() { locked = true; },hint,- key(e) {- const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;- if (!d || locked) return false;- cur = cur < 0 ? (d > 0 ? 0 : gs.length - 1) : (cur + d + gs.length) % gs.length;- gs[cur].focus();- return true;- },+ key: e => !locked && move(e),};};// Tap a point on the stage number line.@@ -1015,6 +1026,113 @@ const pickEls = (items, right, yes, no, onRight) => (body, api) => {return stageTap(api, targets, right, yes, onRight,[kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClick')]);};+/* ---------- point and read ---------- */+// A picture book's point-and-read moment, for young children learning words: a film's ask beat (CHAPTER.film),+// ask: done => tapWords(THINGS, done) THINGS = [{ g, word: 'basket', clip: 'w-basket' }]+// which the scene fills as it builds the picture; g is what the reader taps (a puppet, a prop, or a transparent shape+// over part of a baked set) and clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default. Once the narration is+// over (P.waiting) every thing turns tappable: a tap hops it, shows its word in large letters beside it and says it, on+// wall time at the player volume with the music ducked under it; tapping again says it again. A counter and Continue+// (Enter) wait in the corner; Continue lights up once every thing has been heard. Seeking or replaying clears it all.+// While the page waits, the scene's continuous motion (stopClock, clockRun with tail: true) runs on past the beat's+// end on wall time, so puppets posed as a function of time keep breathing and blinking.+// The video (export.py) cannot tap: P.tapDemo.at(s) shows the words one by one, DEMO_STEP s apart, while it holds.+const TAP_MIN = 120, DEMO_STEP = 2; // TAP_MIN: the smallest hit box in stage px, a child's finger on a phone+const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');+function tapWords(things, done) {+ const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';+ if (!CHAPTER.film) console.warn('tapWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');+ const list = things.filter(t => {+ const why = !t.word ? 'has no word' : !(t.g instanceof SVGGraphicsElement && stage.contains(t.g)) ? 'is not an element on the stage' : '';+ if (why) console.warn(`tapWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);+ return !why;+ }).map(t => {+ const clip = t.clip || wordClip(t.word), a = new Audio(`audio/${lang}/${clip}.mp3`);+ a.preload = 'auto';+ a.onerror = () => console.warn(`tapWords: "${t.word}" has no clip at audio/${lang}/${clip}.mp3`);+ return { g: t.g, word: t.word, clip, a, heard: false };+ });+ const L = mk('g', { class: 'tapw' }, stage); // the hit boxes, over the whole picture+ let on = false, alive = true, finished = false, word = null, shown = -1, bar, count, go;+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least TAP_MIN square+ const box = t => {+ const s = stage.getBoundingClientRect(), r = t.g.getBoundingClientRect(), kx = SW / s.width, ky = SH / s.height;+ return { x: (r.left - s.left) * kx, y: (r.top - s.top) * ky, w: r.width * kx, h: r.height * ky };+ };+ const place = () => {+ for (const t of list) {+ const b = box(t), w = Math.max(TAP_MIN, b.w), hh = Math.max(TAP_MIN, b.h);+ for (const [k, v] of Object.entries({ x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, width: w, height: hh })) t.ring.setAttribute(k, v.toFixed(1));+ }+ };+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };+ const idle = s => { for (const w of TW) if (w.tail === true && w.dur) w.pf(1 + s / w.dur); }; // s seconds past the end+ let since = 0;+ const tapIt = (k, quiet) => {+ const t = list[k];+ shown = k;+ t.heard = true;+ const n = list.filter(x => x.heard).length, all = n === list.length;+ count.textContent = all ? translate('allWordsHeard') : translate('wordsHeard', { k: n, n: list.length });+ if (all && !go.classList.contains('go')) { go.className = 'btn go'; bar.classList.add('lit'); }+ // the word beside the thing: above it, else below, right or left, wherever it fits on the stage (slid along that+ // side to stay on it)+ word?.remove();+ word = h('div', 'card word', t.word);+ word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word+ $('ui').append(word);+ const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;+ const spots = [[cx, b.y - hh - m], [cx, b.y + b.h + m], [b.x + b.w + m, cy], [b.x - w - m, cy]];+ const [x, y] = spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];+ Object.assign(word.style, { left: Math.max(m, Math.min(SW - m - w, x)) + 'px', top: Math.max(m, Math.min(SH - m - hh, y)) + 'px' });+ if (quiet) { word.style.animation = go.style.animation = 'none'; return; } // a video's frame shows it at rest+ for (const x of list) if (x !== t) x.a.pause();+ t.a.currentTime = 0;+ t.a.volume = +($('volume')?.value ?? 1);+ t.a.play().catch(() => {});+ SND.word = t.a; // soundTick ducks the music under it+ t.g.animate([{ translate: '0 0' }, { translate: '0 -18px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });+ t.ring.animate([{ strokeOpacity: 1 }, { strokeOpacity: 0 }], { duration: 700, easing: 'ease-out' });+ };+ const enable = () => {+ on = true;+ since = performance.now();+ linkStage(); // the stage is no longer one picture to a screen reader: the things are its buttons+ const at = new Map(list.map(t => [t, box(t)]));+ for (const [t, b] of at) if (b.x + b.w <= 0 || b.x >= SW || b.y + b.h <= 0 || b.y >= SH || !b.w || !b.h) console.warn(`tapWords: "${t.word}" is not in the picture`);+ // smaller boxes on top, so a thing standing in front of a bigger one can still be tapped+ for (const t of [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h)) { t.hit = G(L); t.ring = mk('rect', { class: 'ring', rx: 22 }, t.hit); }+ place();+ // the arrows go through the things from left to right+ const order = [...list].sort((a, c) => at.get(a).x + at.get(a).w / 2 - at.get(c).x - at.get(c).w / 2);+ const move = tappable(order.map(t => ({ g: t.hit, label: t.word, act: () => { if (alive) tapIt(list.indexOf(t)); } })));+ P.keys = e => e.key === 'Enter' ? (finish(), true) : move(e);+ bar = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'words');+ Object.assign(bar.style, { left: 'auto', right: '40px', width: 'auto' });+ count = h('span', 'count', translate('wordsHeard', { k: 0, n: list.length }));+ count.setAttribute('role', 'status');+ go = h('button', 'btn quiet', [translate('continue'), kbd('Enter')]);+ go.onclick = finish;+ bar.append(h('div', 'acts', [count, go]));+ };+ const cleanup = () => {+ alive = false;+ L.remove();+ for (const t of list) { t.a.onerror = null; t.a.pause(); t.a.removeAttribute('src'); t.a.load(); if (SND.word === t.a) SND.word = null; }+ if (P.tapDemo?.gen === gen) delete P.tapDemo;+ };+ P.cleanup.push(cleanup);+ P.tapDemo = { gen, clips: list.map(t => t.clip), step: DEMO_STEP, at(s) { if (!on) enable(); idle(s); const k = Math.min(list.length - 1, Math.floor(s / DEMO_STEP)); if (k >= 0 && k !== shown) { list.slice(0, k).forEach(t => { t.heard = true; }); tapIt(k, true); } } };+ const tick = () => {+ if (!alive) return;+ if (P.gen !== gen) { cleanup(); return; } // a seek away, or the beat replayed+ if (P.waiting && !on) enable();+ if (on && P.waiting) idle((performance.now() - since) / 1000);+ if (on) place();+ requestAnimationFrame(tick);+ };+ requestAnimationFrame(tick);+}// Rows of toggles. items: { parts, ans: [keys], why }. parts are math parts (strings, F, R) unless text: true,// where they are rich text (strings and $m(...)). cols: [[key, label, colour?, shortcut?]]. multi: several per row.const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) => {@@ -1279,7 +1397,9 @@ function soundTick(dt) {if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);- if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));+ // a word the reader tapped (tapWords) ducks the music at once, as the narration does+ const saying = !!SND.word && !SND.word.paused && !SND.word.ended;+ if (!held) SND.duck += Math.max(-dt * (saying ? 4 : 1), Math.min(dt, (wait && !saying || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));for (const m of SND.bgm) {const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says@@ -1741,7 +1861,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isShR, E, TX, $tex, texGraphic, texPart, OV, M, chip, tokens, layRow, collapse, mathEl, io, out, back, lin, tw, prog,show, hide, draw, pop, pulse, stream, mix, evalTo, soft, clockRun, camera, scene, S, AXIS, X, Y0, num, reset, tick,dot, arrow, panel, retire, header, fx, fxp, finishFx, pulseFx, shakeFx, floatText, TF, $, kbd, sources, uiW, SCORE,- h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, grid, rat, gcd, lowest, blanks,+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, tapWords, grid, rat, gcd, lowest, blanks,SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,finishCard };})();@@ -1768,7 +1888,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGINuiW = ENGINE.uiW, SCORE = ENGINE.SCORE, h = ENGINE.h, rich = ENGINE.rich, card = ENGINE.card,mascotEl = ENGINE.mascotEl, quiz = ENGINE.quiz, qlayer = ENGINE.qlayer, keyAct = ENGINE.keyAct,choice = ENGINE.choice, mcq = ENGINE.mcq, tap = ENGINE.tap, tapEls = ENGINE.tapEls, pickEls = ENGINE.pickEls,- grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,+ tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,SND = ENGINE.SND, P = ENGINE.P, HOLD = ENGINE.HOLD, clearCards = ENGINE.clearCards, seek = ENGINE.seek,seekInto = ENGINE.seekInto, start = ENGINE.start, setSpeed = ENGINE.setSpeed, setPlaying = ENGINE.setPlaying,setCaptions = ENGINE.setCaptions, hideCover = ENGINE.hideCover, restart = ENGINE.restart, boot = ENGINE.boot;