Skip to content

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

feat(engine): listen and find in the engine, tapWords gone, beds that last

yzrcca549271b3b
diff --git a/.claude/skills/papermorph/references/core/engine.md b/.claude/skills/papermorph/references/core/engine.md
index 1806683..370623e 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)), and a picture book's point-and-read moment `tapWords(things, done)` ([picture-books/stories.md](../picture-books/stories.md#point-and-read)).
+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 listen-and-find stop `findWords(things, asks, done)` ([picture-books/stories.md](../picture-books/stories.md#listen-and-find)).
## 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; a `tapWords` beat holds as long as its words take, [core/publish.md](publish.md#video-export)). 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; `video`, also read only by `export.py`, leaves a beat out or cuts it short, [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.
@@ -115,7 +115,7 @@ Firefox repaints SVG on the main thread and is the slowest common browser for th
Space play/pause, and once the chapter has ended replay, ←/→ previous/next beat (Shift+←/→ during a question), Home restart, C captions (a question's narration is captioned too, above its band or beside its corner card, until the narration ends); a beat's last caption clears a second after its narration, so it does not sit on a closing title; a line in another language with a `sub` (`tts.py`, `eleven.py`) shows its subtitle even with captions off, `<`/`>` speed (0.75–1.5×, remembered), volume (remembered, except muted), F full screen: the stage fills the screen, and the controls lie over its foot, showing while the pointer moves or a key is pressed, while paused and while the pointer is on them (a touch screen, with no hover, keeps them under the stage); full screen also turns the screen to the stage's orientation where the browser can lock it (Android, not iOS), and on a phone held across the stage, starting the chapter goes full screen that way, so a landscape lesson is not a strip on an upright phone, ? help (it lists the question keys only in a chapter with questions, and a film with questions can leave them out with `CHAPTER.questionKeys = false` when its moments have their own controls). Clicking a step segment jumps to that point of the beat (`seekInto(i, frac)`); the next beat's clip preloads while one plays. `seek(i, play)` rebuilds and jumps; `start(i, play)` begins a beat from the current picture. While audio plays, the beat clock follows `audio.currentTime`. In a hidden tab (another tab or window in front) a timer keeps the clock going, so the chapter plays on from beat to beat. `?beat=N&t=S` in the URL opens a frozen frame for review, without captions (N is kept within the chapter's steps); on a question step the answer box takes focus only once Space starts the lesson. With `?review` in the URL, N pauses and asks for a note on the current moment and Shift+N copies the chapter's notes, one line each with the `?beat=N&t=S` that reopens it, so a reviewer can collect notes while watching and hand them over at once. After the end, on the finish card or a film's last frame, Enter opens `CHAPTER.next` (replays when there is none) and R replays.
-**Sound effects and music**, in films only: in a chapter with a question, `sfx()` and `music()` are silent, unless the chapter sets `CHAPTER.film = true`: a film with interactive moments (a documentary where the reader fires a rifle). There, while a question beat waits for the reader after its narration, the music plays on (it rises to 70 %, as in a lead-in) and is not pulled back for the time it ran on, and a click on its step bar seeks into it like any other beat; the next beat's tracks take over as usual (`fade` 0 is a cut). `sfx(beat, t, name, vol)` plays `audio/sfx/<name>.mp3` when that beat's clock passes `t` in normal playback (a seek past it fires nothing), at half volume (vol 2 is full); effects are fetched and decoded when `sfx()` is called, before the first click; one still loading when it is due plays late, from its start, for up to a second. A long ambience listed in `SFX_BED` with its length (`SFX_BED.birds = 10`) is joined part-way after a seek. `music(file, beats, gain, fade)` is a looping track under the listed beats (all of them by default; called above `BEATS`, it takes them all when `boot()` runs, and a listed id that `BEATS` lacks is named in a console warning). Its place is the length of its earlier beats as they really ran plus the clock; a beat under music gives back from its closing hold the time the clock stood still (a clip getting going), so the film keeps the music's time. It ducks to 30 % for the whole beat, voice or not, and rises to 70 % only in the `pre` lead-in of the beats in `window.MUSIC_UP_IN_PRE` (`true` for all); it crossfades over `fade` s (0.8; 0 is a cut), and `window.MUSIC_OFF = { beat: t => true }` fades it out for a moment. Both go through one Web Audio graph started by the reader's first touch, click or key, so ducks, fades and the volume slider reach them on phones too (on iOS the slider does not reach the narration). A pause holds the music's level and a seek stops the effects. When a clip loading or buffering holds the clock, the music runs on, then eases back (5 % slower or faster), or jumps back once if it is more than a second out. Safari and every iOS browser stall a media element at each speed change, so there the music keeps the film's speed and is put back by a seek once it is a quarter second out, aimed ahead by the lag it showed after the last one. `export.py` reads `SFX`, `BGM` and these settings and mixes the film by the same rules.
+**Sound effects and music**, in films only: in a chapter with a question, `sfx()` and `music()` are silent, unless the chapter sets `CHAPTER.film = true`: a film with interactive moments (a documentary where the reader fires a rifle). There, while a question beat waits for the reader after its narration, the music plays on (it rises to 70 %, as in a lead-in) and is not pulled back for the time it ran on, the beat's continuous motion (`clockRun(f, D, { tail: true })`, a picture book's `stopClock`) runs on past its end at `P.t + P.idle` (the seconds waited, held by a pause), so puppets breathe and blink while a child looks, its beds play on, and a click on its step bar seeks into it like any other beat; the next beat's tracks take over as usual (`fade` 0 is a cut). `sfx(beat, t, name, vol)` plays `audio/sfx/<name>.mp3` when that beat's clock passes `t` in normal playback (a seek past it fires nothing), at half volume (vol 2 is full); effects are fetched and decoded when `sfx()` is called, before the first click; one still loading when it is due plays late, from its start, for up to a second. Effects past a beat's end never play. A bed, `sfx(beat, t, name, vol, { loop: true })`, is laid again every its file's length less 0.5 s (`BED_OVERLAP`), so its soft ends overlap, until the beat ends, and on through a film's ask wait; `{ loop: true, to: s }` stops it at `s` on the beat clock; a seek or a late load joins it where it would be. A long sound played once, listed in `SFX_BED` with its length (`SFX_BED.thunder = 6`) or given the length as the fifth argument, is joined part-way after a seek. `music(file, beats, gain, fade)` is a looping track under the listed beats (all of them by default; called above `BEATS`, it takes them all when `boot()` runs, and a listed id that `BEATS` lacks is named in a console warning). Its place is the length of its earlier beats as they really ran plus the clock; a beat under music gives back from its closing hold the time the clock stood still (a clip getting going), so the film keeps the music's time. It ducks to 30 % for the whole beat, voice or not, and rises to 70 % only in the `pre` lead-in of the beats in `window.MUSIC_UP_IN_PRE` (`true` for all); it crossfades over `fade` s (0.8; 0 is a cut), and `window.MUSIC_OFF = { beat: t => true }` fades it out for a moment. Both go through one Web Audio graph started by the reader's first touch, click or key, so ducks, fades and the volume slider reach them on phones too (on iOS the slider does not reach the narration). A pause holds the music's level and a seek stops the effects. When a clip loading or buffering holds the clock, the music runs on, then eases back (5 % slower or faster), or jumps back once if it is more than a second out. Safari and every iOS browser stall a media element at each speed change, so there the music keeps the film's speed and is put back by a seek once it is a quarter second out, aimed ahead by the lag it showed after the last one. `export.py` reads `SFX`, `BGM` and these settings and mixes the film by the same rules.
Progress is saved under `animebook:progress:<book-path>`, the key the cover and a picture book's series page read; a book can set another with `--progress-key` in `lib/book.css`, and its cover must then read that key. Books served in different folders have separate saved places and completion lists. Saved progress is optional: malformed JSON, `null` or a non-object value starts with empty progress, and only an array of positive integer chapter numbers counts for completion.
@@ -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 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.
+- **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 findWords FIND findMouth findSaw 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/sound.md b/.claude/skills/papermorph/references/core/sound.md
index dda47cb..f0fcdcb 100644
--- a/.claude/skills/papermorph/references/core/sound.md
+++ b/.claude/skills/papermorph/references/core/sound.md
@@ -49,7 +49,7 @@ Voices carry every book and everything else sits under them: in early cuts music
`sfx.py` synthesizes effects as small MP3s, free and consistent (knock, door, creak, bell, chop, crash, coin, rain, wind, room tone, surf, gulls, crickets, footsteps, a dog, whoosh, bang, tick, thud and more; `--list` names them all). A sound it lacks goes in the book's own `books/<book>/sfx_recipes.py`, passed with `--recipes`: each top-level function without required arguments is a recipe named after it, returning samples at `sfx.SR`, built from sfx's own helpers (details in the script's header). Keep them soft and natural: the first rain, a high hiss, read as scary; pink noise with patter and taps on umbrellas read as rain.
- Place an effect on a beat's clock with the engine's `sfx(beat, t, name, volume)` (in a picture book each scene is a beat); it plays at half volume under the voices (vol 2 is full), a pause holds it and the next beat stops it. Cut an effect to its shot: six seconds of rain under a one-second shot spills into the next.
-- A long bed (wind, room tone, a park's 10 s `birds`) goes in `SFX_BED` with its length (`SFX_BED.birds = 10`) so a seek joins it part-way; a looping bed overlaps its seams by about half a second; room tone runs under the silences too, where its absence is heard first.
+- A bed (wind, room tone, a river, a park's 10 s `birds`) is called once with `{ loop: true }`: `sfx(scene, 0, 'river', .5, { loop: true })` lays the file again every its length less half a second, so a file with soft ends (about 0.3 s) overlaps its seams, and runs to the end of the scene however long it is, through a listen-and-find wait too; a seek joins it where it would be. `{ loop: true, to: t }` stops it at `t` on the scene's clock, where the picture cuts somewhere it does not belong (the forest outside, then Grandma's room inside: Little Red Riding Hood's `scene5.js`). Room tone runs under the silences too, where its absence is heard first. A long sound played once (a 6 s thunder roll) goes in `SFX_BED` with its length (`SFX_BED.thunder = 6`) so a seek joins it part-way.
## Music
@@ -57,7 +57,7 @@ Voices carry every book and everything else sits under them: in early cuts music
- **Where it comes from.** Use tracks the book may redistribute (generated for it, or openly licensed) and credit them. `commons.py --audio` finds public-domain recordings. A composition can be centuries old while its recording is still protected: Commons marks some 1950s–60s recordings public domain under EU rules the US does not follow (protected there until 2067), and `commons.py`'s licence check cannot see this. Prefer recordings their performers released (PDM-owner, CC0), Musopen, or US military bands. Generated music (Lyria) is made outside the Skill for now.
- **Cut to the film.** `music.py cut SRC site/<book>/chNN/audio/bgm/NAME.mp3 chNN --url …/<book>/` cuts a recording so its last note lands on the film's end (the last moment it is within 10 LU of its body, so a long decay rings past the end; `--end` sets it by hand) and levels it to −16 LUFS. `--rests` splices out its rests and `--even` evens out its quiet phrase ends (a generated track usually needs it too: its quiet passages fall into the voice's gaps just as a recording's do); `--lift T` raises a hole neither fixes. The cut fits one length: after any change to a beat's length, cut again (`gaps` says "recut").
-- **Level by ear, check by measure.** Leave `?bgm=` on the page so the user can set the level by ear; start around 0.4: with the ducking to 30 %, a −16 LUFS track then sits about 20 LU under a −14 LUFS voice, the level the user chose for the first short. The gain is relative to the track's own loudness, so give both. `music.py gaps chNN --url …` lists the bed's loudness in each voice-free gap and after the last word and marks the holes; `--mp4` measures the export the same way. A measurement catches gross errors; the ear decides.
+- **Level by ear, check by measure.** Leave `?bgm=` on the page so the user can set the level by ear; start around 0.4: with the ducking to 30 %, a −16 LUFS track then sits about 20 LU under a −14 LUFS voice, the level the user chose for the first short. The gain is relative to the track's own loudness, so give both. `music.py gaps chNN --url …` lists the bed's loudness in each voice-free gap and after the last word and marks the holes. It plays the bed as the engine does, over the whole film: every track under its own beats, looping where the film is longer, silent in `MUSIC_OFF` moments (marked as meant, not as holes). It names the beats where a film waits for the reader: the music runs on there, so after a wait the track is further on than measured. `--mp4` measures the export the same way, on the video's timeline. A measurement catches gross errors; the ear decides.
## Captions and other languages
diff --git a/.claude/skills/papermorph/references/picture-books/stories.md b/.claude/skills/papermorph/references/picture-books/stories.md
index 9a86bc3..ab554a6 100644
--- a/.claude/skills/papermorph/references/picture-books/stories.md
+++ b/.claude/skills/papermorph/references/picture-books/stories.md
@@ -12,7 +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
+- Listen and find
- Team
- Review rounds
- Delivering
@@ -51,7 +51,7 @@ uv run --with edge-tts python "$SKILL/scripts/tts.py" "$N" "$A"
python3 "$SKILL/scripts/level.py" "$A"/*.mp3
python3 "$SKILL/scripts/mouths.py" "$N" "$A"
python3 "$SKILL/scripts/join.py" "$A" scene1=s01,s02,s02b,s03 --words "$N"
-uv run --with numpy python "$SKILL/scripts/sfx.py" "site/$BOOK/ch01/audio/sfx" knock
+uv run --with numpy python "$SKILL/scripts/sfx.py" "site/$BOOK/ch01/audio/sfx" knock chime
```
Keep these commands in `tools/<book>/audio.sh` and rerun it after any narration change ([Sound](#sound)). As copied they voice the kit's sample episode, a check that the folder works; the episode's script then replaces the narration file, and its scenes the `join.py` list.
@@ -100,35 +100,42 @@ 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
+## Listen and find
-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.
+A picture book for young children learning a language can stop and play a finding game in the picture: the character asks for one thing at a time ("Can you find the bird? Touch it!"), the child touches it, and it hops, shows **bird** in big letters and says it. *The Little Horse Crosses the River* stops twice (by the river, at the mill), *Little Red Riding Hood* once (in the meadow, after the wolf has gone). It suits a story read to children of about four to seven who are learning the words; each stop is a pause in the film, so one or two per episode, at a moment where the story rests, 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 words.** Three or four things to ask for, and every other thing the child can see in that picture listed too, because a child taps what catches the eye: six to eight concrete nouns in all, things the child probably half knows (tree, flower, basket, bird, butterfly, sun). 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: no translation, no sentence beyond the ask. The beat's narration is one line in the narrator's voice that tells the child what to do ("Help her look! Listen, and touch what she asks for."); then the character speaks.
-**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 (a rectangle, or a path traced round an odd outline such as a winding path or a pond):
+**The beat.** It is an `ask` beat with `findWords(things, asks, 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 kit's `chapter.html` has both. The scene fills the things 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 (a rectangle, or a path traced round an odd outline such as a winding path or a pond). The asks are in order, the last one found leads on (Little Horse's last ask is the ox, and the next scene opens on "There's Uncle Ox!"):
```js
-let WOODS = []; // the things to tap, filled by scene4w
+let MEADOW = []; // the things, filled by scene4w
function scene4w(p, D) {
- const cam = paperCam(p, 'woods');
- const basket = prop(cam.stage, BASKET, { transform: 'translate(640 700)' });
+ const cam = paperCam(p, 'meadow');
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
+ const basket = prop(cam.stage, BASKET), red = kidPuppet(cam.stage, { coat: '#c94a3c' });
+ MEADOW = [{ g: basket, word: 'basket', side: 'right' }, { g: tree, word: 'tree' }, { g: red.g, word: 'Red Riding Hood', clip: 'q-me', talk: true }];
+ stopClock(D, (q, f) => {
+ cam.apply(q, f);
+ tf(basket, 'translate(640 700)');
+ // in the game (q > D) she says the lines findWords gives her, and blinks only between them
+ red.pose({ x: 900, y: 800, mouth: q > D ? findMouth() : 'X', shut: !FIND.saying && q % 4 > 3.85 }, f);
+ });
}
// in BEATS:
-['scene4w', 'In the woods', (m, D) => scene4w(panel(0), D), { lead: false, pre: PRE4W, ask: done => tapWords(WOODS, done) }],
+const FIND_MEADOW = [{ word: 'tree', clip: 'q-tree' }, { word: 'basket', clip: 'q-basket' }];
+['scene4w', 'In the meadow', (m, D) => scene4w(panel(0), D), { lead: false, pre: PRE4W, ask: done => findWords(MEADOW, FIND_MEADOW, done), video: false }],
```
-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) that follows its thing every frame, so a target may be a puppet that walks about or a bird that flies. A tap hops the thing (the hop adds to the transform the scene sets every frame, so a prop or puppet posed every frame hops in place), shows its word beside it, above where there is room, and says the word with the music ducked; tapping again says it again. Where the word would cover something the child should see, a face above the basket Red holds, give the thing a side: `{ g: basket, word: 'basket', side: 'below' }` (or `'left'`, `'right'`, `'above'`), and the word goes there, kept on the stage. 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`.
+Once the narration is over the character asks for the first thing. Every thing answers a tap, with a hit box at least 120 px square (a child's finger on a phone; smaller boxes lie on top of bigger ones) that follows its thing every frame, so a target may be a puppet that walks or a bird that flies. A wrong thing hops, shows its word and says its own name, then the short ask comes again ("Where's the bird?"); nothing is ever called wrong. Each miss helps more: at the second the thing asked for shakes, from the third paper stars circle it; a tap on nothing leaves a ripple, and two of those count as a miss. A tap anywhere inside the asked thing's box finds it, even where a smaller thing lies on top. With no tap for 5 s it shakes, and if nobody finds it in 20 s the character says "Look! There it is!" and it counts as found, so a child who never taps is never stuck. A found thing hops, shows its word, says it and chimes; after the last one comes the praise and the film goes on by itself. A new tap cuts off whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps; Enter otherwise skips the stop. Where the word would cover something the child should see, give the thing a side: `{ g: basket, word: 'basket', side: 'right' }` (or `'left'`, `'above'`, `'below'`). 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 character acts it.** While the page waits, the scene's `stopClock` runs on past the beat's end (q > D), so pose everyone as a function of `q` that keeps living there: a breath, a blink every few seconds, a bird pecking along a branch. While the character speaks, `FIND.saying` names the clip and `findMouth()` gives its mouth for this frame; turn the eyes to the child while it asks, to `FIND.target` (the thing asked for) while it waits, and to `findSaw()` (the thing just found, for a moment after). A scene can do more with a found thing by watching `FIND.found`: Red's flower flies into her bouquet, and any flower in the meadow counts as the flower, because the scene wraps the one the child touches into the single `g` it gave `findWords` (`site/little-red-riding-hood/ch01/scene4w.js`). The hop and the shake are the CSS `translate` property, which adds to the transform the scene sets every frame; a thing the scene moves through a parent can copy it (`getComputedStyle(g).translate`, as the mill's wheel does in Little Horse's `scene8.js`).
-**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)).
+**The clips** are plain narration entries, voiced with the rest (`tts.py` or `eleven.py`), each played alone and left out of `join.py`'s list; run `level.py` on them with the other clips. The things' names in the narrator's voice: `"w-bird": "bird"` makes `audio/<lang>/w-bird.mp3` (a thing's clip is `'w-' + its word`, lower case, spaces as hyphens; `clip:` names another). In the character's voice: each ask (`clip:` of the ask, "Can you find the bird? Touch it!"), its short ask again (`r-bird`, "Where's the bird?"; `again:` names another), the praise (`q-done`, "You found them all!"), the give-up line (`q-there`, "Look! There it is!"), and the character's own answer when tapped (`q-me`, "That's me!", with `talk: true`, so the mouth moves). `findWords(things, asks, done, { end, there })` names other praise and give-up clips (Little Horse's river stop ends on `q-uncle`). The character's lines are captioned from their timings when captions are on, so their entries need cues as every clip's do. The chime is `audio/sfx/chime.mp3` (`sfx.py chime`).
-**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, a clip that will not load, and a thing a finger cannot reach because something lies over it (naming what: the counter's card in the bottom right corner, say), which `check.py` lists.
+**The video.** `export.py` cannot tap, and a game is no fun to watch: give the stop's beat `video: false`, or `video: S` to keep its lead-in line and cut there, and the video goes straight on to the next scene ([core/publish.md](../core/publish.md#video-export)).
+
+**Check it** as a child would. `?beat=N` opens the beat; play its narration and wait: nothing is tappable until it ends. Then play the game in the browser by mouse and by keys: a wrong thing, nothing, the right thing; miss three times and watch the help grow; wait out a give-up. Does every word sit beside its thing and not over it, does every clip sound, does the character's mouth move with its lines and its eyes go where they should, does the film go on after the praise? Seek out of the beat in the middle of a line and back: no box, word or caption may stay behind. `lint.py` names a clip that is missing, 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, a clip that will not load and a side it does not know, which `check.py` lists.
## Team
diff --git a/.claude/skills/papermorph/scripts/engine.py b/.claude/skills/papermorph/scripts/engine.py
index d1c80bc..c2669d0 100644
--- a/.claude/skills/papermorph/scripts/engine.py
+++ b/.claude/skills/papermorph/scripts/engine.py
@@ -20,7 +20,7 @@ with lib/mathjax.js keeps it in step like the rest.
import argparse, filecmp, io, shutil, sys, tarfile, tempfile, urllib.request
from pathlib import Path
-DEFAULT = 'DozenTwelve/[email protected]' # follows the latest compatible release
+DEFAULT = 'DozenTwelve/[email protected]' # follows the latest compatible release
OWN = 'book.css'
LATEX = {'vendor/mathjax/tex-svg-full.js': 'mathjax.js', 'vendor/mathjax/LICENSE': 'mathjax-LICENSE.txt'}
diff --git a/AGENTS.md b/AGENTS.md
index 82e25dc..3307cdc 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_tapwords.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_find.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_find.py b/engine/check_find.py
new file mode 100644
index 0000000..c09914c
--- /dev/null
+++ b/engine/check_find.py
@@ -0,0 +1,204 @@
+#!/usr/bin/env python3
+"""Listen-and-find regression: a small film whose ask beat is findWords(), played headless in Chromium.
+
+ uv run --with playwright engine/check_find.py
+
+No server is needed: the check answers every request itself with this engine, a page written here and real MP3s from
+Little Horse (one word clip is left missing on purpose). It checks that the things turn tappable only once the
+narration is over; the character's ask with its caption and mouth (FIND.saying); a wrong tap (it says its name, then
+the short ask); blank taps (ripples, two count as a miss); the shake at the second miss and the stars at the third; a
+tap inside the asked thing's box under a smaller thing; the give-up line; the praise and the film going on; the keys;
+the warnings; that the scene keeps drawing and a bed keeps playing (and is laid again) while the page waits, and a bed
+with `to` stops there; and that a seek away mid-stop clears everything, the caption too, and a seek back starts afresh.
+"""
+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"
+HORSE = SITE / "little-horse/ch01/audio"
+LONG = SITE / "math-notebook/ch01/audio/en/intro.mp3" # the music
+PAGE = """<!doctype html>
+<html lang="en">
+<head><meta charset="utf-8"><title>findWords 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: 'Listen and find', minutes: 1, language: 'en', film: true };
+window.TIMINGS = { 'q-ball': { dur: 1.28, marks: {}, cues: [[0, 'Can you find the ball?']] } };
+let THINGS = [];
+sfx('q', 0, 'bed', .5, { loop: true }); // a 2 s file: laid again every 1.5 s, through the wait
+sfx('q', 0, 'cut', .5, { loop: true, to: .5 }); // stopped at 0.5 s on the beat clock
+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: 600, width: 80, height: 60, fill: '#86c9e8' }, p);
+ THINGS = [{ g: ball, word: 'ball', side: 'right' }, { g: tree, word: 'tree' }, { g: bee, word: 'bee', side: 'top' },
+ { g: cup, word: 'Ice cream' }, { g: tree, word: '' }, { g: document.createElementNS(NS, 'g'), word: 'ghost' }];
+}
+music('audio/bgm/m.mp3', ['q', 'after'], .5);
+const ASKS = ['ball', 'tree', 'bee'].map(w => ({ word: w, clip: 'q-' + w }));
+const BEATS = [
+ ['q', 'Look', (m, D) => picture(panel(0), D), { dur: 1, ask: done => findWords(THINGS, ASKS, done, { hintAfter: 1.5, giveUpAfter: 3 }) }],
+ ['after', 'After', () => {}, { dur: 6 }],
+].map(([id, title, run, extra]) => ({ id, title, run, ...extra }));
+boot();
+</script>
+</body>
+</html>"""
+CLIPS = {"q-": HORSE / "en/r-bird.mp3", "r-": HORSE / "en/w-ox.mp3", "w-": HORSE / "en/w-bird.mp3"}
+
+
+async def answer(route):
+ path = urlparse(route.request.url).path.split("/__find__/")[1]
+ name = path.rsplit("/", 1)[-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 in ("ch01/audio/sfx/bed.mp3", "ch01/audio/sfx/cut.mp3"):
+ await route.fulfill(path=HORSE / "sfx/trot.mp3")
+ elif path == "ch01/audio/sfx/chime.mp3":
+ await route.fulfill(path=HORSE / "sfx/chime.mp3")
+ elif path.startswith("ch01/audio/en/") and name[:2] in CLIPS and name != "w-ice-cream.mp3":
+ await route.fulfill(path=CLIPS[name[:2]])
+ 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 = [], []
+ 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)
+ 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("**/__find__/**", answer)
+ await page.goto("http://find.invalid/__find__/ch01/")
+ await page.wait_for_function("typeof P !== 'undefined' && typeof BEATS !== 'undefined'")
+ js = page.evaluate
+ hits = "[...document.querySelectorAll('#stage .findw rect[role=button]')]"
+ word = "document.querySelector('#ui .card.word')?.textContent"
+ state = "window.FIND_STATE?.k"
+
+ async def tap_at(x_js, wait=80):
+ """A real click at the middle of a drawn thing, or at a stage point [x, y]."""
+ r = await js(f"""(() => {{ const t = {x_js}, s = $('stage').getBoundingClientRect();
+ if (Array.isArray(t)) return [s.x + t[0] * s.width / SW, s.y + t[1] * s.height / SH];
+ const r = t.getBoundingClientRect(); return [r.x + r.width / 2, r.y + r.height / 2]; }})()""")
+ await page.mouse.click(*r)
+ await page.wait_for_timeout(wait)
+
+ async def said(clip, timeout=4000):
+ await page.wait_for_function(f"PLAYED.includes({clip!r})", timeout=timeout)
+
+ await js("setCaptions(true, false)")
+ 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, "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.tabIndex === 0)")
+ assert await js("document.getElementById('stage').getAttribute('role')") == "group"
+ assert await js(f"{hits}.every(g => {{ const r = g.getBBox(); return r.width >= 120 && r.height >= 120; }})"), "a hit box under 120 px"
+ await said("q-ball.mp3")
+ assert await js("FIND.saying") == "q-ball" and await js("FIND.target === THINGS[0].g")
+ await page.wait_for_timeout(150)
+ assert await js("$('caption').textContent") == "Can you find the ball?" and not await js("$('caption').hidden"), "the ask has no caption"
+ print("PASS waiting: four things tappable after the narration, labelled, 120 px boxes; the ask said, captioned, FIND.saying and FIND.target set")
+
+ cx = await js("+document.querySelector('#scene circle').getAttribute('cx')")
+ await page.wait_for_timeout(1800)
+ assert await js("+document.querySelector('#scene circle').getAttribute('cx')") > cx + 3, "the scene's motion stopped while waiting"
+ assert await js("$('caption').hidden"), "the ask's caption stayed after it was said"
+ beds = await js("SND.on.filter(s => s._sfx === 'bed').map(s => s._at)")
+ assert beds and max(beds) >= 1.5, f"the bed was not laid again during the wait: {beds}"
+ assert not await js("SND.on.some(s => s._sfx === 'cut')"), "a bed with to: .5 still plays"
+ assert await js("SND.duck") > .6, "the music did not come up while waiting"
+ print(f"PASS the wait: the scene draws on, the bed is laid again (lays at {beds}), a bed with `to` stopped, the music up")
+
+ await tap_at("[800, 700]") # the tree (below the bee's box), while she asks for the ball
+ assert await js(word) == "tree" and await js(state) == 0
+ await said("w-tree.mp3")
+ await said("r-ball.mp3")
+ print("PASS a wrong tap: it hops, shows and says its name, then the short ask comes again; nothing is found")
+
+ await tap_at("[1200, 300]") # nothing there: a ripple; two of them count as a miss
+ assert await js("document.querySelectorAll('#stage circle[stroke=\"#fbf6ea\"]').length") >= 1, "no ripple on a blank tap"
+ await tap_at("[1250, 320]")
+ await page.wait_for_timeout(100)
+ assert await js("THINGS[0].g.getAnimations().length") > 0, "the ball did not shake at the second miss"
+ await tap_at("THINGS[0].g")
+ await page.wait_for_function(f"{state} === 1", timeout=5000)
+ assert await js("PLAYED.includes('w-ball.mp3')") and await js("PLAYED.includes('chime.mp3')")
+ print("PASS blanks and a find: ripples, two blanks are a miss, the shake at the second miss; the ball found, its name and the chime")
+
+ await said("q-tree.mp3")
+ for _ in range(3):
+ await tap_at("THINGS[0].g", 150) # the ball, three times while she asks for the tree
+ assert await js("document.querySelectorAll('#stage g[pointer-events=none] path').length") == 6, "no stars at the third miss"
+ await tap_at("THINGS[2].g") # the bee, in front of the tree: inside the tree's box, so the tree is found
+ await page.wait_for_function(f"{state} === 2", timeout=5000)
+ assert await js("document.querySelectorAll('#stage g[pointer-events=none] path').length") == 0, "the stars stayed"
+ print("PASS stars at the third miss; a tap inside the asked thing's box finds it under a smaller thing")
+
+ await said("q-bee.mp3")
+ await said("q-there.mp3", 6000) # nobody finds the bee: the give-up line, then it counts as found
+ await page.wait_for_function(f"{state} === 3", timeout=5000)
+ await said("q-done.mp3")
+ await page.wait_for_function("P.i === 1", timeout=5000)
+ assert await js("document.querySelectorAll('#stage .findw').length") == 0
+ print("PASS give-up after giveUpAfter s, the praise, and the film goes on by itself")
+
+ await js("seek(0, true)") # back into the stop, and away while she speaks
+ await page.wait_for_function("P.waiting && FIND.saying === 'q-ball'", timeout=5000)
+ await page.wait_for_timeout(150)
+ assert not await js("$('caption').hidden")
+ await js("seek(1, false)")
+ await page.wait_for_timeout(150)
+ left = await js("""[document.querySelectorAll('#stage .findw').length, document.querySelectorAll('#ui .card').length,
+ P.keys === null, SND.word === null, FIND.saying, FIND.target, $('caption').hidden]""")
+ assert left == [0, 0, True, True, None, None, 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 .findw').length") == 1 and await js(state) == 0
+ print("PASS seeks: away mid-line clears hit boxes, word, keys, sound and the caption; back starts afresh")
+
+ await page.wait_for_timeout(1500)
+ 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("ArrowLeft")
+ await page.keyboard.press("Enter") # the ball, focused: found
+ await page.wait_for_function(f"{state} === 1", timeout=5000)
+ await js("document.activeElement.blur()")
+ await page.keyboard.press("Enter") # none focused: the stop is skipped
+ await page.wait_for_timeout(150)
+ assert await js("P.i") == 1, "Enter did not skip the stop"
+ print("PASS keys: arrows go left to right, Enter taps the focused thing, Enter with none focused skips the stop")
+
+ 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',
+ '"bee" has side "top"'):
+ 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, unknown side")
+ assert not errors, errors
+ await browser.close()
+
+
+asyncio.run(main())
diff --git a/engine/check_tapwords.py b/engine/check_tapwords.py
deleted file mode 100644
index e23d3c8..0000000
--- a/engine/check_tapwords.py
+++ /dev/null
@@ -1,205 +0,0 @@
-#!/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
- // a prop at the top (its word goes below) that the scene moves every frame through its own transform, as a puppet's is
- const cup = mk('g', {}, p);
- mk('rect', { width: 80, height: 60, fill: '#86c9e8' }, cup);
- clockRun(t => cup.setAttribute('transform', `translate(${(1300 + t * 20).toFixed(1)} 40)`), D, { tail: true });
- const box = mk('rect', { x: 1380, y: 795, width: 40, height: 20, fill: '#9aaba3' }, p); // under the counter's card
- THINGS = [{ g: ball, word: 'ball', side: 'right' }, { g: tree, word: 'tree' }, { g: bee, word: 'bee', clip: 'w-bee-2', side: 'top' }, { g: cup, word: 'Ice cream' },
- { g: box, word: 'box' }, { 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 === 5", timeout=5000)
- assert sorted(await js(f"{hits}.map(g => g.getAttribute('aria-label'))")) == ["Ice cream", "ball", "bee", "box", "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 / 5"
- 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: five things tappable after the narration, labelled, 120 px boxes, clips requested, 0 / 5, Continue")
-
- cx = await js("+document.querySelector('#scene circle').getAttribute('cx')")
- ring = "+document.querySelector('#stage .tapw [aria-label=\"Ice cream\"] rect').getAttribute('x')"
- rx = await js(ring)
- await page.wait_for_timeout(500)
- assert await js(ring) > rx + 5, "the hit box did not follow a moving thing"
- 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 / 5"
- 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.left >= b.right; })()"""), "the word is not right of the thing (side: 'right')"
- 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 / 5"
- assert await js("PLAYED.at(-1)") == "w-bee-2.mp3"
- await tap_at("THINGS[3].g") # at the top: its word goes below
- 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"
- 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) == "3 / 5"
- print("PASS taps: motion runs on while waiting, hit boxes follow, word on its side of 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, box
- 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) == "4 / 5"
- await page.keyboard.press("ArrowRight")
- await page.keyboard.press("ArrowRight")
- await page.keyboard.press("Space")
- await page.wait_for_timeout(120) # mid-hop: the scene's transform is kept, the hop adds to it
- hop = await js("""[THINGS[3].g.getAttribute('transform'), THINGS[3].g.getBoundingClientRect().top]""")
- assert await js(word) == "Ice cream" and await js("P.i === 0 && P.waiting"), "Space did not tap"
- assert await js(count) == "4 / 5"
- await page.wait_for_timeout(500)
- rest = await js("""[THINGS[3].g.getAttribute('transform'), THINGS[3].g.getBoundingClientRect().top]""")
- assert hop[0].startswith("translate(13") and rest[0].startswith("translate(13") and hop[1] < rest[1] - 4, f"the hop lost the scene's transform: {hop} {rest}"
- await page.keyboard.press("ArrowRight")
- await page.keyboard.press("Enter")
- await page.wait_for_timeout(80)
- assert await js(word) == "box" and await js(count) == "You found them all!"
- assert await js("document.querySelector('.card.words button').className") == "btn go"
- print("PASS keys: arrows move left to right, Enter and Space tap; a hop keeps the scene's own transform; 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',
- '"box" cannot be tapped: <', '"bee" has side "top"'):
- 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, covered by the counter, unknown side")
-
- 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 === 5", timeout=5000)
- assert await js("document.querySelectorAll('#stage .tapw').length") == 1 and await js(count) == "0 / 5"
- 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", "w-box"], 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 / 5"
- await js("P.tapDemo.at(99)")
- assert await js(word) == "box" 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("Object.keys(TRANSLATIONS).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 every interface language (" + ", ".join(await js("Object.keys(TRANSLATIONS)")) + ")")
- assert not errors, errors
- await browser.close()
-
-
-asyncio.run(main())
diff --git a/engine/engine.css b/engine/engine.css
index 7083c22..89ee13b 100644
--- a/engine/engine.css
+++ b/engine/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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.js
index 2177807..0a1fe16 100644
--- a/engine/engine.js
+++ b/engine/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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.css
index 7083c22..89ee13b 100644
--- a/site/elementary-algebra/lib/engine.css
+++ b/site/elementary-algebra/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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.js
index 2177807..0a1fe16 100644
--- a/site/elementary-algebra/lib/engine.js
+++ b/site/elementary-algebra/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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.css
index 7083c22..89ee13b 100644
--- a/site/joke-collection/lib/engine.css
+++ b/site/joke-collection/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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.js
index 2177807..0a1fe16 100644
--- a/site/joke-collection/lib/engine.js
+++ b/site/joke-collection/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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/linear-algebra/lib/engine.css b/site/linear-algebra/lib/engine.css
index 7083c22..89ee13b 100644
--- a/site/linear-algebra/lib/engine.css
+++ b/site/linear-algebra/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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/linear-algebra/lib/engine.js b/site/linear-algebra/lib/engine.js
index 2177807..0a1fe16 100644
--- a/site/linear-algebra/lib/engine.js
+++ b/site/linear-algebra/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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-horse/ch01/index.html b/site/little-horse/ch01/index.html
index b0121c8..11b360e 100644
--- a/site/little-horse/ch01/index.html
+++ b/site/little-horse/ch01/index.html
@@ -19,7 +19,6 @@
<script src="audio/en/mouths.js"></script>
<script src="audio/en/shots.js"></script>
<script src="../lib/shared.js"></script>
-<script src="../lib/find.js"></script>
<script src="scene1.js"></script>
<script src="scene2.js"></script>
<script src="scene3.js"></script>
@@ -36,7 +35,7 @@
const CHAPTER = { number: 1, title: 'The Little Horse Crosses the River', minutes: 4, language: 'en', film: true, questionKeys: false };
try { if (localStorage.getItem('captions') === null) setCaptions(true, false); } catch {} // captions on at the first visit; the reader's own choice is kept
-// The two stops are "listen and find" (lib/find.js): Little Horse asks for one thing at a time; every thing in the
+// The two stops are "listen and find" (the engine's findWords): Little Horse asks for one thing at a time; every thing in the
// picture answers a tap with its name. Only things that move are asked for, so their hop and wiggle can be seen.
// At the river he is looking for help: the ox comes last, and finding him leads into scene 4 ("There's Uncle Ox!").
const FIND_RIVER = ['bird', 'flower', 'butterfly', 'ox'].map((w, i) => ({ word: w, clip: 'q-' + w + (i ? '' : '1') }));
@@ -59,7 +58,6 @@ music('audio/bgm/theme.mp3', undefined, +(new URLSearchParams(location.search).g
window.MUSIC_UP_IN_PRE = true;
window.MUSIC_OFF = { scene7: t => t > PRE7 + 32.7, // faded out by the stare (33.47): "deep!" ends at 32.7
scene9: t => t > PRE9 + TIMINGS.scene9.dur - 2 }; // the film fades out under the last picture
-SFX_BED.river = 10; SFX_BED.birds = 10; SFX_BED.mill = 10; SFX_BED.wind = 8;
// Build every puppet and prop once at load, off screen, so their bitmaps are ready before the first beat needs them.
paperWarm(g => { horsePuppet(g); motherPuppet(g); oxPuppet(g); squirrelPuppet(g); rabbitPuppet(g); prop(g, SACK); millWheel(g); });
boot();
diff --git a/site/little-horse/ch01/scene1.js b/site/little-horse/ch01/scene1.js
index fc4afaf..bb29c74 100644
--- a/site/little-horse/ch01/scene1.js
+++ b/site/little-horse/ch01/scene1.js
@@ -57,7 +57,7 @@ function scene1(p, D) {
.to({ x: 1130, y: 600, z: 2.0 }, PROUD - .2, D + .6 - PROUD + .2); // then him, setting off
cam.soft(T('n01', 10.65), true).soft(M01 - .1, false).soft(BIG - .06, true).soft(HALF - .05, false).soft(MILL - .02, true).soft(FETCH, false);
- sfx('scene1', 0, 'birds', .5); sfx('scene1', 0, 'wind', .25);
+ sfx('scene1', 0, 'birds', .5, { loop: true }); sfx('scene1', 0, 'wind', .25, { loop: true });
sfx('scene1', GAG + .45, 'hoof', .25); sfx('scene1', GAG + .95, 'hoof', .2);
sfx('scene1', H01 + .78, 'hoof', .45);
sfx('scene1', FETCH + .6, 'hoof', .2); sfx('scene1', FETCH + 1.05, 'hoof', .2);
diff --git a/site/little-horse/ch01/scene2.js b/site/little-horse/ch01/scene2.js
index 8ea77a3..086cfec 100644
--- a/site/little-horse/ch01/scene2.js
+++ b/site/little-horse/ch01/scene2.js
@@ -41,7 +41,7 @@ const s2_PUFF = '<g filter="url(#paper)"><circle r="16" fill="#dccaa0"/><circle
function scene2(p, D) {
const at = t => PRE2 + t; // clip time -> beat clock
// ---- sound: beds, a hoof on every diagonal pair's landing, the skid
- sfx('scene2', 0, 'birds', .45); sfx('scene2', 0, 'wind', .25); sfx('scene2', s2_T.cut, 'river', .5);
+ sfx('scene2', 0, 'birds', .45, { loop: true }); sfx('scene2', 0, 'wind', .25, { loop: true }); sfx('scene2', s2_T.cut, 'river', .5, { loop: true });
for (const [cyc, t0, t1, vol] of [[s2_roadCyc, 0, s2_T.cut, .22], [s2_rivCyc, s2_T.cut, s2_T.ride, .3]]) {
let last = Math.floor(cyc(t0) * 2);
for (let t = t0; t < t1; t += 1 / 120) { const k = Math.floor(cyc(t) * 2); if (k > last) { sfx('scene2', +t.toFixed(3), 'hoof', vol); last = k; } }
diff --git a/site/little-horse/ch01/scene3.js b/site/little-horse/ch01/scene3.js
index c3f7ca3..f8a7099 100644
--- a/site/little-horse/ch01/scene3.js
+++ b/site/little-horse/ch01/scene3.js
@@ -1,17 +1,17 @@
-/* scene3.js — By the river: the first stop, a "listen and find" game (lib/find.js).
+/* scene3.js — By the river: the first stop, a "listen and find" game (the engine's findWords).
n03 "Little Horse looked all around. Who could help him? Help him look! Listen, and touch what he asks for."
Wide river shot; on "looked all around" Little Horse turns round to the tree and the flowers and back to the river,
worried; the camera eases in to the game's frame (every thing in, none under another's word). Then he asks for one
- thing at a time (bird, flower, butterfly, ox; find.js speaks and moves his mouth through findMouth()), with his
+ thing at a time (bird, flower, butterfly, ox; findWords speaks and moves his mouth through findMouth()), with his
chin up and eyes to us while he asks, searching about between asks, glancing at each thing as it is found; finding
Uncle Ox ends it ("There's Uncle Ox! Let's ask him!"). The picture lives on meanwhile: the ox chews in the river
facing him, a kingfisher hops along the branch, a butterfly flutters over the flowers.
Squirrel is hidden in the crown (not built) until scene 5. */
'use strict';
const PRE3 = 1.0;
-let RIVER_THINGS = []; // filled by scene3 each time it builds; findWords (lib/find.js) reads it
-sfx('scene3', 0, 'river', .5);
-sfx('scene3', 0, 'birds', .4);
+let RIVER_THINGS = []; // filled by scene3 each time it builds; findWords reads it
+sfx('scene3', 0, 'river', .5, { loop: true });
+sfx('scene3', 0, 'birds', .4, { loop: true });
sfx('scene3', 1.8, 'hoof', .25); // the turn round, and back
sfx('scene3', 3.05, 'hoof', .25);
@@ -109,7 +109,7 @@ function scene3(p, D) {
// searches about between asks (worried, not playing), and glances at each thing as it is found.
const turns = [[1.75, -1], [3.0, 1]];
const faceAt = q => { let a = 1, f = 1; for (const [t0, to] of turns) { if (t0 > q) break; f = s3_turn(q, t0, a, to); a = to; } return f; };
- let saw = null, sawQ = -1e9; // the thing just found (find.js) and when, on the beat clock
+ let saw = null, sawQ = -1e9; // the thing just found (findWords) and when, on the beat clock
stopClock(D, (q, f) => {
cam.apply(q, f);
@@ -134,7 +134,7 @@ function scene3(p, D) {
tail: 6 * Math.sin(q * 1.3) + 10 * s3_bump(q % 6.1, 4.8, .5),
shut: blinking(q, [.9, 3.6, 6.2]) || (wait && !asking && q % 3.7 < 2 / FPS) }, f);
// Uncle Ox chews in the river facing Little Horse, a short rest every 5 s; slow blinks, a tail flick. Found, he
- // stops chewing and lifts his head (find.js hops and shakes his g itself, on top of this pose).
+ // stops chewing and lifts his head (findWords hops and shakes his g itself, on top of this pose).
const rest = Math.min(.8, Math.max(0, q % 5 - 4.2)), chewing = 1.4 * (q - .8 * Math.floor(q / 5) - rest);
const oxSeen = saw === ox.g ? q - sawQ : 1e9;
ox.pose({ x: s3_OX, y: s3_OXY, s: 1.4, turn: -1, grass: true, chew: (rest > 0 && rest < .8) || oxSeen < 2 ? 0 : chewing,
diff --git a/site/little-horse/ch01/scene4.js b/site/little-horse/ch01/scene4.js
index ed8efb5..35a33a5 100644
--- a/site/little-horse/ch01/scene4.js
+++ b/site/little-horse/ch01/scene4.js
@@ -49,8 +49,8 @@ function scene4(p, D) {
const lhRoute = [[0, 400], [W('n04', 6.3), 400], [W('n04', 7.6), 470], [W('n04b', .45), 470], [W('n04b', 1.75), 560]];
const tLift = W('n04b', 2.65);
- sfx('scene4', 0, 'river', .5);
- sfx('scene4', 0, 'birds', .35);
+ sfx('scene4', 0, 'river', .5, { loop: true });
+ sfx('scene4', 0, 'birds', .35, { loop: true });
sfx('scene4', lhRoute[1][0], 'trot', .45);
sfx('scene4', lhRoute[3][0], 'trot', .4);
sfx('scene4', lhRoute[4][0], 'skid', .25);
diff --git a/site/little-horse/ch01/scene5.js b/site/little-horse/ch01/scene5.js
index 02b49fc..55e3ee8 100644
--- a/site/little-horse/ch01/scene5.js
+++ b/site/little-horse/ch01/scene5.js
@@ -102,7 +102,7 @@ function scene5(p, D) {
g: prop(cam.stage, `<g filter="url(#paper)"><path d="M0,-9 C7,-6 8,4 0,9 C-8,4 -7,-6 0,-9 Z" fill="${['#71a056', '#83b264', '#5f8e48', '#94bb7c'][i]}"/><path d="M0,-8 L0,8" stroke="#4f7c3e" stroke-width="1.6"/></g>`) }));
const picture = s5_picture(cam.stage);
- sfx('scene5', 0, 'river', .45); sfx('scene5', 0, 'birds', .35);
+ sfx('scene5', 0, 'river', .45, { loop: true }); sfx('scene5', 0, 'birds', .35, { loop: true });
sfx('scene5', .15, 'squeak', .35); sfx('scene5', s5_UP, 'whoosh', .7); sfx('scene5', s5_LAND, 'hop', .7);
sfx('scene5', T(1.24), 'sting', .35); // "Stop!"
sfx('scene5', tPic, 'pop', .4); // the picture opens
diff --git a/site/little-horse/ch01/scene6.js b/site/little-horse/ch01/scene6.js
index d2d009d..714a83e 100644
--- a/site/little-horse/ch01/scene6.js
+++ b/site/little-horse/ch01/scene6.js
@@ -28,7 +28,7 @@ const S6 = {
S6.vig1 = S6.uncle - .05; S6.vig2 = S6.squirrel - .05; S6.vigOff = S6.it + .55;
/* ---------- sounds ---------- */
-sfx('scene6', 0, 'birds', .5);
+sfx('scene6', 0, 'birds', .5, { loop: true });
// hooves: one soft knock every half stride of the walk (d in the puppet's own units, a stride is 74)
function s6_steps(keys, s, t0, t1, vol) {
let last = null;
diff --git a/site/little-horse/ch01/scene7.js b/site/little-horse/ch01/scene7.js
index 1aac92c..7ac09ab 100644
--- a/site/little-horse/ch01/scene7.js
+++ b/site/little-horse/ch01/scene7.js
@@ -179,7 +179,7 @@ function scene7(p, D) {
[[T(2.2), true], [T(8.75), false], [T(10.5), true], [T(12.45), false], [T(17.65), true]].forEach(([t, on]) => cam.soft(t, on));
/* ---------- sound ---------- */
- sfx('scene7', 0, 'river', .5); sfx('scene7', 0, 'birds', .4);
+ sfx('scene7', 0, 'river', .5, { loop: true }); sfx('scene7', 0, 'birds', .4, { loop: true });
sfx('scene7', .05, 'trot', .4);
sfx('scene7', T(4.88), 'squeak', .35);
sfx('scene7', T(13.75), 'hop', .25); sfx('scene7', T(25.95), 'hop', .22);
diff --git a/site/little-horse/ch01/scene8.js b/site/little-horse/ch01/scene8.js
index 0f6b35e..89c3a1d 100644
--- a/site/little-horse/ch01/scene8.js
+++ b/site/little-horse/ch01/scene8.js
@@ -1,4 +1,4 @@
-/* scene8.js — At the mill, the second "listen and find" stop (lib/find.js). Little Horse walks up to the mill door,
+/* scene8.js — At the mill, the second "listen and find" stop (the engine's findWords). Little Horse walks up to the mill door,
kneels so the sack slides off his back, catches its tie in his teeth and sets it down by the door with a thud, then
stands proud by it ("Little Horse brought the wheat to the mill." — the video keeps the beat to PRE8 + 3.6, this
proud hold with a small nod). On "Let's play again! Listen, and touch what he asks for." he steps back and the camera
@@ -64,7 +64,7 @@ function scene8(p, D) {
];
// sound: the mill race under the whole scene, a hoof at every other footfall, the thud, a quack as the ducks come in
- sfx('scene8', 0, 'mill', .5);
+ sfx('scene8', 0, 'mill', .5, { loop: true });
for (const keys of [s8_IN, s8_BACK]) {
let last = null;
for (let t = Math.max(0, keys[0][0]); t <= keys[1][0] + .2; t += 1 / 24) {
@@ -87,7 +87,7 @@ function scene8(p, D) {
stopClock(D, (q, f) => {
cam.apply(q, f);
wheel(q);
- const hop = getComputedStyle(wheelHit).translate; // the hop and the hint's shake find.js gives the wheel's circle
+ const hop = getComputedStyle(wheelHit).translate; // the hop and the hint's shake findWords gives the wheel's circle
tf(wheelWrap, hop === 'none' ? '' : `translate(${hop.split(' ').map(parseFloat).map(v => v || 0).join(',')})`);
// Little Horse
diff --git a/site/little-horse/ch01/scene9.js b/site/little-horse/ch01/scene9.js
index 5c32e5f..a5ce7c6 100644
--- a/site/little-horse/ch01/scene9.js
+++ b/site/little-horse/ch01/scene9.js
@@ -68,7 +68,7 @@ function scene9(p, D) {
const card = endCard(p, tCard, 'The End', { x: 800, y: 150, size: 72 });
// sound: beds, hooves in, the hops, the wade, the climb
- sfx('scene9', 0, 'river', .5); sfx('scene9', 0, 'birds', .4);
+ sfx('scene9', 0, 'river', .5, { loop: true }); sfx('scene9', 0, 'birds', .4, { loop: true });
[.3, .95, 1.6, 2.3, 3.0, 3.8].forEach(t => sfx('scene9', t, 'hoof', .25));
sfx('scene9', tJump, 'hop', .5); sfx('scene9', tLand, 'pop', .3);
let tWet = tGo, tDry = tGo; while (wade(tWet).x > 1320) tWet += .02; while (wade(tDry).x > 690) tDry += .02; // into and out of the water
diff --git a/site/little-horse/lib/engine.css b/site/little-horse/lib/engine.css
index 7083c22..89ee13b 100644
--- a/site/little-horse/lib/engine.css
+++ b/site/little-horse/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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-horse/lib/engine.js b/site/little-horse/lib/engine.js
index 2177807..0a1fe16 100644
--- a/site/little-horse/lib/engine.js
+++ b/site/little-horse/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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-horse/lib/find.js b/site/little-horse/lib/find.js
deleted file mode 100644
index c2687f2..0000000
--- a/site/little-horse/lib/find.js
+++ /dev/null
@@ -1,179 +0,0 @@
-/* find.js — "listen and find", the book's point-and-read stops (2026-10-31; to move into the engine beside tapWords).
- findWords(things, asks, done, opts): a character asks for one thing at a time ("Can you find the bird? Touch it!").
- Every thing in the picture answers a tap: a wrong one hops and says its own name, then the short question comes
- again ("Where's the bird?"). Nothing is ever called wrong; each miss helps more: at the second the thing asked for
- shakes at once, from the third paper stars circle it and it keeps shaking. A tap on nothing leaves a paper ripple
- (two of them count as a miss). A tap that lands inside the asked thing's box counts as finding it, even where a
- smaller thing's box lies on top. Nobody finds it for `giveUpAfter` s: the character says "Look! There it is!", the
- thing hops and counts as found, so a child who never taps is never stuck. After the last one, the praise line, and
- the film goes on by itself. A new tap cuts off whatever was being said, so lines never pile up.
- things: [{ g, word, side?, clip?, talk? }] as for tapWords (talk: the clip is the character's voice, e.g. 'q-me'
- for the character itself); asks: [{ word, clip, again? }] in order (again defaults to 'r-' + word);
- opts.end: the praise clip; opts.there: the give-up clip.
- While the character speaks, FIND.saying names its clip: a scene poses its mouth with findMouth(), and can turn its
- eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next question). The lines
- are captioned, from their timings, when captions are on. */
-'use strict';
-// The engine keeps a scene living while tapWords waits; it does not for another ask. So find.js remembers each
-// scene's frame function (stopClock, paper.js) and draws it on past the beat's end while the page waits.
-const FIND_CLOCKS = [];
-{ const run = stopClock; stopClock = (D, draw) => { while (FIND_CLOCKS.length && FIND_CLOCKS[0].gen !== P.gen) FIND_CLOCKS.shift(); FIND_CLOCKS.push({ gen: P.gen, D, draw, f: -1 }); run(D, draw); }; }
-const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9; // until the next question starts
-const findMouth = () => FIND.saying ? mouthAt(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
-const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
-const FIND_SIDES = { above: 0, below: 1, right: 2, left: 3 };
-
-function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
- const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';
- const clip = name => { const a = new Audio(`audio/${lang}/${name}.mp3`); a.preload = 'auto'; a.clipName = name; return a; };
- const list = things.filter(t => t.word && t.g instanceof SVGGraphicsElement && stage.contains(t.g))
- .map(t => ({ ...t, a: clip(t.clip || 'w-' + t.word.toLowerCase().replace(/ /g, '-')) }));
- const qs = asks.map(q => ({ ...q, a: clip(q.clip), again: clip(q.again || 'r-' + q.word) }));
- const praise = clip(end), help = clip(there), chime = new Audio('audio/sfx/chime.mp3');
- const L = mk('g', { class: 'findw' }, stage);
- let on = false, alive = true, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
- let misses = 0, blanks = 0, turn = 0, stars = null;
- const now = () => performance.now();
- 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 hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
- const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
- const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
- const want = () => list.find(t => t.word === qs[k]?.word);
-
- // one sound at a time, ducking the music like a tapped word; the character's own lines move its mouth and are
- // captioned; resolves when the sound ends
- const caption = a => {
- if (typeof captions === 'undefined' || !captions) return;
- const line = (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ');
- $('caption').textContent = line; $('caption').hidden = !line;
- };
- const say = (a, talk = false) => new Promise(ok => {
- if (!alive) return ok();
- playing?.pause(); playing = a;
- a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
- FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
- if (talk) caption(a);
- a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) { $('caption').hidden = true; } ok(); };
- SND.word = a;
- a.play().catch(ok);
- });
- let wordTimer = 0;
- const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
- // the word beside its thing, on its `side` or wherever it fits, as tapWords places it
- const showWord = t => {
- word?.remove(); clearTimeout(wordTimer);
- word = h('div', 'card word', t.word); word.setAttribute('aria-hidden', 'true');
- $('ui').append(word);
- const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
- const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side in FIND_SIDES ? spots[FIND_SIDES[t.side]] : spots.find(([x, y], i) => i < 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' });
- };
- const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
- // the hint: a big side-to-side shake (translate, so a scene can copy it onto a picture it moves itself, the mill's wheel)
- const wiggle = t => {
- if (wiggling) return;
- wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
- wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
- };
- // paper stars close round the thing asked for, from the third miss until it is found
- const starsOn = t => {
- if (stars) return;
- stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
- stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
- stars.t = t;
- };
- const starsOff = () => { stars?.remove(); stars = null; };
-
- const ask = async () => {
- lastAct = askedAt = now();
- window.FIND_STATE = { k, of: qs.length }; // for tests
- FIND.target = want()?.g || null;
- if (k >= qs.length) {
- on = false; word?.remove(); FIND.target = null;
- await say(praise, true);
- setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; done(); } }, 900);
- return;
- }
- await say(qs[k].a, true);
- lastAct = askedAt = now();
- };
- const found = async (t, given) => {
- on = false; starsOff(); turn++;
- hop(t); showWord(t);
- if (given) await say(help, true);
- await say(t.a); hideWord(600);
- FIND.found = t.g; FIND.foundAt = now() / 1000;
- chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
- await new Promise(r => setTimeout(r, 700));
- if (!alive) return;
- k++; misses = blanks = 0; on = true; ask();
- };
- const miss = async (t, my) => {
- misses++;
- const w = want();
- if (misses >= 2 && w) wiggle(w);
- if (misses >= 3 && w) starsOn(w);
- if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
- await new Promise(r => setTimeout(r, 250));
- if (my === turn && alive && on) say(qs[k].again, true);
- };
- // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
- const press = (e, t) => {
- e.preventDefault();
- if (!on || !alive) return;
- lastAct = now();
- const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
- if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
- if (t) { const my = ++turn; hop(t); showWord(t); return miss(t, my); }
- const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
- r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
- if (++blanks % 2 === 0) miss(null, ++turn);
- };
- const enable = () => {
- on = true;
- const bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
- bg.addEventListener('pointerdown', e => press(e, null));
- for (const t of list) {
- t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all;outline:none', 'aria-label': t.word, role: 'button' }, L);
- t.hit.addEventListener('pointerdown', e => press(e, t));
- }
- // smaller things on top, so a bird in front of a tree can be reached
- [...list].sort((a, b) => box(b).w * box(b).h - box(a).w * box(a).h).forEach(t => L.append(t.hit));
- place();
- P.keys = e => e.key === 'Enter' ? (done(), true) : false;
- ask();
- };
- const cleanup = () => {
- alive = false; L.remove(); word?.remove(); starsOff();
- for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help]) a.pause();
- if (SND.word) SND.word = null;
- Object.assign(FIND, { saying: null, target: null, found: null });
- };
- P.cleanup.push(cleanup);
- let waitFrom = 0;
- const tick = () => {
- if (!alive) return;
- if (P.gen !== gen) { cleanup(); return; }
- // the scene lives on while the page waits: its frame function at D + the seconds waited, on its own 12 fps steps
- if (P.waiting) {
- waitFrom ||= now();
- for (const c of FIND_CLOCKS) if (c.gen === gen) {
- const q = c.D + (now() - waitFrom) / 1000, f = Math.floor(q * FPS + 1e-6);
- if (f !== c.f) { c.f = f; c.draw(f / FPS, f); }
- }
- }
- if (P.waiting && !on && k === 0 && !list[0].hit) enable();
- if (list[0]?.hit) place();
- if (stars) { const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
- [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); }); }
- const w = want();
- if (on && w && now() - askedAt > giveUpAfter * 1000) found(w, true);
- else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
- requestAnimationFrame(tick);
- };
- requestAnimationFrame(tick);
-}
diff --git a/site/little-horse/lib/shared.js b/site/little-horse/lib/shared.js
index 907bf6c..08e23ca 100644
--- a/site/little-horse/lib/shared.js
+++ b/site/little-horse/lib/shared.js
@@ -52,9 +52,3 @@ function millWheel(parent, x = 1340, y = 650, r = 150) {
const g = prop(parent, `<g filter="url(#paper)">${paddles}<circle r="${r}" fill="none" stroke="#7a5236" stroke-width="14"/><circle r="${r * .62}" fill="none" stroke="#7a5236" stroke-width="8"/>${spokes}<circle r="22" fill="#5a3b24"/></g>`);
return q => tf(g, `translate(${x},${y}) rotate(${(q * 24 % 360).toFixed(1)})`);
}
-
-// Beds (river, birds, wind, mill) are files of about 10 s with soft ends; a scene calls sfx() for one once, and it is
-// laid again every BED_STEP s so it runs to the scene's end (effects past a beat's end never play, and export.py
-// leaves them out too). The page waiting at a point-and-read stop plays no effects; the music carries the wait.
-const BED_STEP = { river: 9.5, mill: 9.5, birds: 9.6, wind: 7.5 };
-{ const once = window.sfx; window.sfx = (beat, t, name, vol) => { once(beat, t, name, vol); if (BED_STEP[name]) for (let k = 1; k * BED_STEP[name] < 60; k++) once(beat, +(t + k * BED_STEP[name]).toFixed(2), name, vol); }; }
diff --git a/site/little-red-riding-hood/ch01/index.html b/site/little-red-riding-hood/ch01/index.html
index f047c53..a2ffad0 100644
--- a/site/little-red-riding-hood/ch01/index.html
+++ b/site/little-red-riding-hood/ch01/index.html
@@ -19,7 +19,6 @@
<script src="audio/en/mouths.js"></script>
<script src="audio/en/shots.js"></script>
<script src="../lib/shared.js"></script>
-<script src="../lib/find.js"></script>
<script src="scene1.js"></script>
<script src="scene2.js"></script>
<script src="scene3.js"></script>
@@ -39,7 +38,7 @@ const CHAPTER = { number: 1, title: 'Little Red Riding Hood', minutes: 4, langua
// A picture book: captions are on unless the reader turned them off.
try { if (localStorage.getItem('captions') === null) setCaptions(true, false); } catch {}
-// The meadow stop is "listen and find" (lib/find.js): Red asks for one thing at a time; every thing in the picture
+// The meadow stop is "listen and find" (the engine's findWords): Red asks for one thing at a time; every thing in the picture
// answers a tap with its name. Only things that move are asked for; a found flower flies into her bouquet.
const FIND_MEADOW = ['flower', 'butterfly', 'bird'].map((w, i) => ({ word: w, clip: 'q-' + w + (i ? '' : '1') }));
/* ---------- beats: [id, title, run(m, D), extra] ---------- */
diff --git a/site/little-red-riding-hood/ch01/scene1.js b/site/little-red-riding-hood/ch01/scene1.js
index a0c9368..064fb6e 100644
--- a/site/little-red-riding-hood/ch01/scene1.js
+++ b/site/little-red-riding-hood/ch01/scene1.js
@@ -79,7 +79,7 @@ function scene1(p, D) {
const puffs = smoke(cam.stage, 932, 84), theDoor = door(cam.stage, DOOR), mom = motherPuppet(cam.stage), red = redPuppet(cam.stage);
// ---- sound ----
- sfx('scene1', 0, 'forest', .4); // the garden's birds and leaves (shared.js lays it again every 9.5 s)
+ sfx('scene1', 0, 'forest', .4, { loop: true }); // a bed: it repeats to the end of the scene
sfx('scene1', DOOR, 'creak', 1.1); sfx('scene1', OUT + .25, 'whoosh', .5); sfx('scene1', LAND, 'step2', .7);
sfx('scene1', CAKE + .2, 'whoosh', .3);
for (let k = 0, t; (t = WALK + WP * (.25 + .5 * k)) < WIPE + .2; k++) sfx('scene1', +t.toFixed(2), 'step' + (1 + k % 3), .7 - .05 * k);
diff --git a/site/little-red-riding-hood/ch01/scene10.js b/site/little-red-riding-hood/ch01/scene10.js
index 34977e5..c15df3e 100644
--- a/site/little-red-riding-hood/ch01/scene10.js
+++ b/site/little-red-riding-hood/ch01/scene10.js
@@ -31,7 +31,7 @@ function scene10(p, D) {
const wipe = trunkWipe(p, D - .4, .5); // time passes: to the table
const backPath = x => 808 - (x - 1380) * 116 / 1620, far = x => .6 * (1 - .78 * Math.min(1, (x - 1380) / 1620));
- sfx('scene10', 0, 'forest', .5, 10);
+ sfx('scene10', 0, 'forest', .5, { loop: true });
sfx('scene10', .05, 'tinkle', 1.4); sfx('scene10', LAND, 'thud', 1.2); sfx('scene10', LAND + .02, 'tinkle', .7);
for (let t = LAND + .3; t < C('s10', .8); t += .44) sfx('scene10', +t.toFixed(2), 'run', 1.1 - .25 * (t - LAND));
sfx('scene10', FOLLOW, 'whoosh', 1.4);
diff --git a/site/little-red-riding-hood/ch01/scene11.js b/site/little-red-riding-hood/ch01/scene11.js
index a7f8cf7..f7e9aa2 100644
--- a/site/little-red-riding-hood/ch01/scene11.js
+++ b/site/little-red-riding-hood/ch01/scene11.js
@@ -63,7 +63,7 @@ function scene11(p, D) {
[C('better11', .2), { ua: -90, fa: -60 }], [C('better11', .5), { ua: -92, fa: -25 }], [C('better11', .8), { ua: -90, fa: -65 }], [C('better11', 1.1), { ua: -92, fa: -25 }], [C('better11', 1.4), { ua: -90, fa: -60 }], [C('s11b', .1), DOWN]];
const reach1 = s11_hand({ x: 2330, y: 655, s: .55, ...REACH, lean: 10 }), reach2 = s11_hand({ x: 2330, y: 655, s: .55, ...REACH2, lean: 10 });
- sfx('scene11', 0, 'room', .4, 10); // relaid every 9.5 s by shared.js (BED_STEP)
+ sfx('scene11', 0, 'room', .4, { loop: true }); // a bed: it repeats to the end of the scene
sfx('scene11', C('better11', .2), 'pop', 1.2); sfx('scene11', C('better11', .2), 'hop', 1.4); sfx('scene11', C('wink11', .25), 'chime', .8);
stopClock(D, (q, f) => {
diff --git a/site/little-red-riding-hood/ch01/scene2.js b/site/little-red-riding-hood/ch01/scene2.js
index 97d4802..40bd0d9 100644
--- a/site/little-red-riding-hood/ch01/scene2.js
+++ b/site/little-red-riding-hood/ch01/scene2.js
@@ -33,7 +33,7 @@ function scene2(p, D) {
.cut({ x: s2_BX + 10, y: s2_BY - 62, z: 3.5 }, CLOSE).to({ x: s2_BX + 4, y: s2_BY - 56 }, CLOSE, D + .6 - CLOSE); // z 3.5 is the layered sets' limit
cam.soft(CLOSE, true);
- sfx('scene2', 0, 'forest', .52); // shared.js lays it again every 9.5 s
+ sfx('scene2', 0, 'forest', .52, { loop: true }); // a bed: it repeats to the end of the scene
for (let k = 0, t; (t = PER * (.25 + .5 * k)) < BUT + 2.4; k++) sfx('scene2', +t.toFixed(2), 'step' + (1 + k % 3), t < BUT ? .6 : .6 - .3 * Math.min(1, (t - BUT) / 2));
sfx('scene2', OPEN - .1, 'rustle', 1.1);
diff --git a/site/little-red-riding-hood/ch01/scene3.js b/site/little-red-riding-hood/ch01/scene3.js
index 3b66d17..5d87a85 100644
--- a/site/little-red-riding-hood/ch01/scene3.js
+++ b/site/little-red-riding-hood/ch01/scene3.js
@@ -11,7 +11,7 @@
const PRE3 = 2.2;
const s3_C = (c, t = 0) => clipAt('scene3', PRE3, c, t);
const s3_BX = 1180, s3_BY = 742, s3_POP = .35, s3_HOP = .8, s3_LAND = 1.2;
-sfx('scene3', 0, 'forest', .5, 10); // the bed: shared.js lays it again every 9.5 s; 10 s long, so a seek joins it part-way
+sfx('scene3', 0, 'forest', .5, { loop: true }); // a bed: it repeats to the end of the scene
sfx('scene3', .3, 'rustle', 1.6);
sfx('scene3', s3_HOP, 'whoosh', .8); sfx('scene3', s3_LAND, 'pad', 1.6);
[[.24, 'step1'], [.71, 'step2'], [1.42, 'step3'], [2.05, 'step1']].forEach(([t, n]) => sfx('scene3', t, n, 1.2));
diff --git a/site/little-red-riding-hood/ch01/scene4.js b/site/little-red-riding-hood/ch01/scene4.js
index 2cd7606..a4d98e4 100644
--- a/site/little-red-riding-hood/ch01/scene4.js
+++ b/site/little-red-riding-hood/ch01/scene4.js
@@ -41,8 +41,8 @@ function scene4(p, D) {
step: C('s04b', 1.42), F: C('s04b', 2.47), G: C('s04b', 6.72), H: C('run4', .53),
};
const PICKS = [C('s04b', 3.6), C('s04b', 4.65), C('s04b', 5.7), C('run4', .8)], PICK = 1.05;
- // ---- sound: the meadow bed (shared.js's BED_STEP lays it again every 9.5 s), feet where they are seen
- sfx('scene4', 0, 'meadow', .6);
+ // ---- sound: the meadow bed (it repeats to the end of the scene), feet where they are seen
+ sfx('scene4', 0, 'meadow', .6, { loop: true });
sfx('scene4', T.step + .5, 'step1', 1.6);
[.35, .7, 1.05].forEach((t, i) => sfx('scene4', T.F + t, 'step' + (2 + i % 2), 1.1));
PICKS.forEach(t => sfx('scene4', t + .4, 'pop', .45));
diff --git a/site/little-red-riding-hood/ch01/scene4w.js b/site/little-red-riding-hood/ch01/scene4w.js
index d987582..ffde0d9 100644
--- a/site/little-red-riding-hood/ch01/scene4w.js
+++ b/site/little-red-riding-hood/ch01/scene4w.js
@@ -1,18 +1,18 @@
-/* scene4w.js — in the meadow: listen and find (lib/find.js, as Little Horse's scenes 3 and 8).
+/* scene4w.js — in the meadow: listen and find (the engine's findWords, as Little Horse's scenes 3 and 8).
s04w "Little Red Riding Hood wanted flowers for Grandma. Help her look! Listen, and touch what she asks for."
Shot 1 (0 – "Help"): medium close, Red bent over one flower, picks it on "flowers", holds it up, and it goes into
her bouquet on "Grandma". Shot 2 (cut on "Help", a little wide): she looks about the meadow in profile, turns round
to us on "Listen", her eyes on the butterfly, while the camera eases in to the game's frame (still from D on). The game: she asks for a flower, the butterfly, the bird
- (find.js speaks; her mouth follows findMouth(), her eyes go to FIND.target while she waits, to us while she asks).
+ (findWords speaks; her mouth follows findMouth(), her eyes go to FIND.target while she waits, to us while she asks).
Every flower in the frame is a flower: the set's big fore flowers are drawn live here (the baked fore layer is
hidden), the small painted ones get a live copy the moment one is touched. The flower the child touches, wherever
- it is, is wrapped into the one "flower" thing find.js knows (s4w_wrap), so it is the one that hops, is found and
+ it is, is wrapped into the one "flower" thing findWords knows (s4w_wrap), so it is the one that hops, is found and
flies into her bunch, and keeps its colour there. The bird pecks and hops along the tree top, the butterfly loops
over the grass, the sun's rays turn, the cloud drifts. Every visible thing answers a tap with its name. */
'use strict';
const PRE4W = 1.0;
-let MEADOW_THINGS = []; // filled by scene4w each time it builds; findWords (lib/find.js) reads it
-sfx('scene4w', 0, 'meadow', .6);
+let MEADOW_THINGS = []; // filled by scene4w each time it builds; findWords reads it
+sfx('scene4w', 0, 'meadow', .6, { loop: true });
sfx('scene4w', PRE4W + 1.55, 'rustle', .5); // the stem snaps
const s4w_RX = 2300, s4w_GY = 795, s4w_S = .6, s4w_B0 = 5; // Red on the grass; flowers in her bunch from scene 4 (5; 7 by scene 7)
@@ -86,7 +86,7 @@ function scene4w(p, D) {
const headF = red.g.querySelector('[data-p="headF"]'), eyes = [...headF.querySelectorAll('.eyeO, .eyeC')];
const basket = red.g.querySelector('[data-p="basketF"]');
- // the one "flower" thing find.js knows: it wraps whichever flower was touched last (the big white one at first)
+ // the one "flower" thing findWords knows: it wraps whichever flower was touched last (the big white one at first)
const fG = document.createElementNS('http://www.w3.org/2000/svg', 'g');
let held = null;
const wrap = f => { if (held === f) return; if (held) fG.replaceWith(held.el); f.el.replaceWith(fG); fG.append(f.el); held = f; };
@@ -120,7 +120,7 @@ function scene4w(p, D) {
smallRecs.set(key, f); flowers.push(f);
return f;
};
- // inside the asked thing's tap box (find.js's hitBox: its box, at least minBox 120 stage px): find.js finds it
+ // inside the asked thing's tap box (findWords's hitBox: its box, at least minBox 120 stage px): findWords finds it
const inAsked = (cx, cy) => {
if (!FIND.target || FIND.target === fG) return false;
const s = $('stage').getBoundingClientRect(), r = FIND.target.getBoundingClientRect(), k = s.width / SW, m = 120 * k;
@@ -132,7 +132,7 @@ function scene4w(p, D) {
const f = flowerAt(e.clientX, e.clientY);
if (!f) return;
wrap(f);
- if (FIND.target === fG) return; // asked for a flower: find.js finds it in fG's box
+ if (FIND.target === fG) return; // asked for a flower: findWords finds it in fG's box
// asked for something else: the flower answers "flower" and hops, as every wrong thing does
const hit = [...document.querySelectorAll('.findw rect')].find(r => r.getAttribute('aria-label') === 'flower');
if (!hit) return;
@@ -155,7 +155,7 @@ function scene4w(p, D) {
cam.apply(q, f);
const v = cam.at(q); OX = -.3 * (v.x - 800); OY = -.3 * (v.y - 450);
const wait = q > D, saying = wait && FIND.saying, breathe = Math.sin(q * 2.3);
- // what find.js has just done: the flower found (it flies), the bird or the butterfly found (they show off)
+ // what findWords has just done: the flower found (it flies), the bird or the butterfly found (they show off)
if (FIND.found === fG && gotQ === null) {
gotQ = q; got = held; got.picked = true; got.from = headAt(got);
wrap(foreRecs[s4w_HERO].picked ? foreRecs.find(r => !r.picked && r.x > 2600 && r.x < 3300) : foreRecs[s4w_HERO]);
diff --git a/site/little-red-riding-hood/ch01/scene5.js b/site/little-red-riding-hood/ch01/scene5.js
index b403198..74476d7 100644
--- a/site/little-red-riding-hood/ch01/scene5.js
+++ b/site/little-red-riding-hood/ch01/scene5.js
@@ -30,8 +30,9 @@ document.body.insertAdjacentHTML('beforeend', `<svg width="0" height="0" style="
${[30, 55, 80].map(k => `<filter id="s5_dark${k}" color-interpolation-filters="sRGB"><feComponentTransfer><feFuncR type="linear" slope="${k / 100}"/><feFuncG type="linear" slope="${k / 100}"/><feFuncB type="linear" slope="${(k + 6) / 100}"/></feComponentTransfer></filter>`).join('')}
</defs></svg>`);
-sfx('scene5', 0, 'forest', .5);
-sfx('scene5', s5_IN, 'room', .4);
+// the beds follow the cuts: the forest outside (A, B, D), the room inside (C, E)
+sfx('scene5', 0, 'forest', .5, { loop: true, to: s5_IN }); sfx('scene5', s5_OUT, 'forest', .5, { loop: true, to: s5_DOOR });
+sfx('scene5', s5_IN, 'room', .4, { loop: true, to: s5_OUT }); sfx('scene5', s5_DOOR, 'room', .4, { loop: true });
[1.0, 1.44].forEach(t => sfx('scene5', t, 'run', 1.3));
sfx('scene5', 1.9, 'skid', 1.3);
[0, 1, 2].forEach(k => sfx('scene5', +(s5_TIP + s5_TIPP * (.25 + k / 2)).toFixed(2), 'pad', 1));
diff --git a/site/little-red-riding-hood/ch01/scene6.js b/site/little-red-riding-hood/ch01/scene6.js
index 5caf08e..55bf326 100644
--- a/site/little-red-riding-hood/ch01/scene6.js
+++ b/site/little-red-riding-hood/ch01/scene6.js
@@ -14,7 +14,7 @@ const PRE6 = 1.2;
const s6_C = t => clipAt('scene6', PRE6, 's06', t);
const s6_WALK = { t0: s6_C(2.2), period: .88, steps: 11, x0: 262 }; // the wolf's creep from the door to the bed's head
const s6_SHUT0 = Math.round(s6_C(5.38) * FPS) / FPS, s6_SLAM = s6_SHUT0 + 5 / FPS; // the wardrobe door pulled to, on a frame
-sfx('scene6', 0, 'room', .4, 10); // the room bed (shared.js relays it every 9.5 s)
+sfx('scene6', 0, 'room', .4, { loop: true }); // a bed: it repeats to the end of the scene
sfx('scene6', s6_C(3.52), 'hop', 1.4); sfx('scene6', s6_C(3.64), 'whoosh', 1); sfx('scene6', s6_C(4.3), 'creak', .8);
sfx('scene6', s6_C(4.65), 'hop', 1); // she lands in the wardrobe
sfx('scene6', s6_C(5.02), 'pad', .9); // the glasses land on the pillow (in the insert)
diff --git a/site/little-red-riding-hood/ch01/scene7.js b/site/little-red-riding-hood/ch01/scene7.js
index 5370ff4..35f70a0 100644
--- a/site/little-red-riding-hood/ch01/scene7.js
+++ b/site/little-red-riding-hood/ch01/scene7.js
@@ -31,8 +31,8 @@ const s7_chopAt = q => { for (const [a, b] of s7_CHOPPING) if (q >= a && q < a +
const s7_WALK_IN = [.7, -80, .95, 24, 4], s7_WALK_ON = [s7_CUT.E + .12, 244, .8, 24.4, 7], s7_STEP_IN = [s7_C('door7', .1), 262, .95, 20, 2];
const s7_falls = ([t0, , period, , steps]) => Array.from({ length: steps }, (_, k) => +(t0 + period * (.25 + k / 2)).toFixed(2));
-sfx('scene7', 0, 'forest', .5);
-sfx('scene7', s7_CUT.H, 'room', .4);
+sfx('scene7', 0, 'forest', .5, { loop: true });
+sfx('scene7', s7_CUT.H, 'room', .4, { loop: true });
for (const [a, b] of s7_CHOPPING) for (let k = 0; a + (k + .85) * s7_CHOP < Math.min(a + Math.ceil((b - a) / s7_CHOP) * s7_CHOP, s7_END); k++) {
const t = +(Math.ceil((a + (k + .85) * s7_CHOP) * FPS - 1e-6) / FPS).toFixed(3); // on the frame the blade lands
sfx('scene7', t, t >= s7_CUT.H ? 'chopin' : 'chop', t >= s7_CUT.H ? 1 : t >= s7_CUT.F || between(t, s7_CUT.B, s7_CUT.T) ? .9 : 1.4); // softer under her line
diff --git a/site/little-red-riding-hood/ch01/scene8.js b/site/little-red-riding-hood/ch01/scene8.js
index 97bbdaa..bd06103 100644
--- a/site/little-red-riding-hood/ch01/scene8.js
+++ b/site/little-red-riding-hood/ch01/scene8.js
@@ -28,7 +28,7 @@ document.body.insertAdjacentHTML('beforeend', `<svg width="0" height="0" style="
const s8_FEAR = [{ X: 'A', C: 'H', D: 'E', G: 'B' }, { X: 's8W', C: 'H', D: 'E', G: 'B' },
{ X: 's8W', A: 's8W', B: 's8sB', C: 's8sH', D: 's8sE', E: 's8sE', F: 's8sF', G: 's8sB', H: 's8sH' }];
{ const C = (c, t = 0) => clipAt('scene8', PRE8, c, t);
- sfx('scene8', 0, 'room', .4, 10); // BED_STEP (shared.js) lays it again every 9.5 s
+ sfx('scene8', 0, 'room', .4, { loop: true }); // a bed: it repeats to the end of the scene
// the woodcutter still chopping outside, through the wall, in the gaps; quiet from the umbrella on
[.35, C('s08', 2.75), C('s08c', -.6), C('s08d', -.55), C('umbrella8', .2), C('umbrella8', 1.05)].forEach(t => sfx('scene8', +t.toFixed(2), 'chopin', .9));
sfx('scene8', +(C('s08f', 1.8) - .34).toFixed(2), 'sting', 2); // its chord lands on "EAT"
diff --git a/site/little-red-riding-hood/ch01/scene9.js b/site/little-red-riding-hood/ch01/scene9.js
index 2769442..6c4f50b 100644
--- a/site/little-red-riding-hood/ch01/scene9.js
+++ b/site/little-red-riding-hood/ch01/scene9.js
@@ -15,7 +15,7 @@ const s9_T = (() => {
WOOD: C('bonk9', .6), SHOO: C('s09b', 1.0), LEAP, CRASH: LEAP + .8 };
})();
{ const C = s9_C, T = s9_T;
- sfx('scene9', 0, 'room', .4, 10); // BED_STEP (shared.js) lays it again every 9.5 s
+ sfx('scene9', 0, 'room', .4, { loop: true }); // a bed: it repeats to the end of the scene
const fx = (t, n, v) => sfx('scene9', +t.toFixed(2), n, v);
fx(T.HIT, 'thud', 2); fx(T.LAND, 'thud', 1.2);
fx(T.BANG - .38, 'knock', .8); fx(T.BANG - .2, 'knock', .9); fx(T.BANG, 'bang', 2); fx(T.LANDG, 'hop', 1.4);
diff --git a/site/little-red-riding-hood/lib/engine.css b/site/little-red-riding-hood/lib/engine.css
index 7083c22..89ee13b 100644
--- a/site/little-red-riding-hood/lib/engine.css
+++ b/site/little-red-riding-hood/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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.js
index 2177807..0a1fe16 100644
--- a/site/little-red-riding-hood/lib/engine.js
+++ b/site/little-red-riding-hood/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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/find.js b/site/little-red-riding-hood/lib/find.js
deleted file mode 100644
index c2687f2..0000000
--- a/site/little-red-riding-hood/lib/find.js
+++ /dev/null
@@ -1,179 +0,0 @@
-/* find.js — "listen and find", the book's point-and-read stops (2026-10-31; to move into the engine beside tapWords).
- findWords(things, asks, done, opts): a character asks for one thing at a time ("Can you find the bird? Touch it!").
- Every thing in the picture answers a tap: a wrong one hops and says its own name, then the short question comes
- again ("Where's the bird?"). Nothing is ever called wrong; each miss helps more: at the second the thing asked for
- shakes at once, from the third paper stars circle it and it keeps shaking. A tap on nothing leaves a paper ripple
- (two of them count as a miss). A tap that lands inside the asked thing's box counts as finding it, even where a
- smaller thing's box lies on top. Nobody finds it for `giveUpAfter` s: the character says "Look! There it is!", the
- thing hops and counts as found, so a child who never taps is never stuck. After the last one, the praise line, and
- the film goes on by itself. A new tap cuts off whatever was being said, so lines never pile up.
- things: [{ g, word, side?, clip?, talk? }] as for tapWords (talk: the clip is the character's voice, e.g. 'q-me'
- for the character itself); asks: [{ word, clip, again? }] in order (again defaults to 'r-' + word);
- opts.end: the praise clip; opts.there: the give-up clip.
- While the character speaks, FIND.saying names its clip: a scene poses its mouth with findMouth(), and can turn its
- eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next question). The lines
- are captioned, from their timings, when captions are on. */
-'use strict';
-// The engine keeps a scene living while tapWords waits; it does not for another ask. So find.js remembers each
-// scene's frame function (stopClock, paper.js) and draws it on past the beat's end while the page waits.
-const FIND_CLOCKS = [];
-{ const run = stopClock; stopClock = (D, draw) => { while (FIND_CLOCKS.length && FIND_CLOCKS[0].gen !== P.gen) FIND_CLOCKS.shift(); FIND_CLOCKS.push({ gen: P.gen, D, draw, f: -1 }); run(D, draw); }; }
-const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9; // until the next question starts
-const findMouth = () => FIND.saying ? mouthAt(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
-const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
-const FIND_SIDES = { above: 0, below: 1, right: 2, left: 3 };
-
-function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
- const gen = P.gen, stage = $('stage'), lang = CHAPTER.language || 'en';
- const clip = name => { const a = new Audio(`audio/${lang}/${name}.mp3`); a.preload = 'auto'; a.clipName = name; return a; };
- const list = things.filter(t => t.word && t.g instanceof SVGGraphicsElement && stage.contains(t.g))
- .map(t => ({ ...t, a: clip(t.clip || 'w-' + t.word.toLowerCase().replace(/ /g, '-')) }));
- const qs = asks.map(q => ({ ...q, a: clip(q.clip), again: clip(q.again || 'r-' + q.word) }));
- const praise = clip(end), help = clip(there), chime = new Audio('audio/sfx/chime.mp3');
- const L = mk('g', { class: 'findw' }, stage);
- let on = false, alive = true, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
- let misses = 0, blanks = 0, turn = 0, stars = null;
- const now = () => performance.now();
- 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 hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
- const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
- const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
- const want = () => list.find(t => t.word === qs[k]?.word);
-
- // one sound at a time, ducking the music like a tapped word; the character's own lines move its mouth and are
- // captioned; resolves when the sound ends
- const caption = a => {
- if (typeof captions === 'undefined' || !captions) return;
- const line = (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ');
- $('caption').textContent = line; $('caption').hidden = !line;
- };
- const say = (a, talk = false) => new Promise(ok => {
- if (!alive) return ok();
- playing?.pause(); playing = a;
- a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
- FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
- if (talk) caption(a);
- a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) { $('caption').hidden = true; } ok(); };
- SND.word = a;
- a.play().catch(ok);
- });
- let wordTimer = 0;
- const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
- // the word beside its thing, on its `side` or wherever it fits, as tapWords places it
- const showWord = t => {
- word?.remove(); clearTimeout(wordTimer);
- word = h('div', 'card word', t.word); word.setAttribute('aria-hidden', 'true');
- $('ui').append(word);
- const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
- const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side in FIND_SIDES ? spots[FIND_SIDES[t.side]] : spots.find(([x, y], i) => i < 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' });
- };
- const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
- // the hint: a big side-to-side shake (translate, so a scene can copy it onto a picture it moves itself, the mill's wheel)
- const wiggle = t => {
- if (wiggling) return;
- wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
- wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
- };
- // paper stars close round the thing asked for, from the third miss until it is found
- const starsOn = t => {
- if (stars) return;
- stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
- stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
- stars.t = t;
- };
- const starsOff = () => { stars?.remove(); stars = null; };
-
- const ask = async () => {
- lastAct = askedAt = now();
- window.FIND_STATE = { k, of: qs.length }; // for tests
- FIND.target = want()?.g || null;
- if (k >= qs.length) {
- on = false; word?.remove(); FIND.target = null;
- await say(praise, true);
- setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; done(); } }, 900);
- return;
- }
- await say(qs[k].a, true);
- lastAct = askedAt = now();
- };
- const found = async (t, given) => {
- on = false; starsOff(); turn++;
- hop(t); showWord(t);
- if (given) await say(help, true);
- await say(t.a); hideWord(600);
- FIND.found = t.g; FIND.foundAt = now() / 1000;
- chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
- await new Promise(r => setTimeout(r, 700));
- if (!alive) return;
- k++; misses = blanks = 0; on = true; ask();
- };
- const miss = async (t, my) => {
- misses++;
- const w = want();
- if (misses >= 2 && w) wiggle(w);
- if (misses >= 3 && w) starsOn(w);
- if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
- await new Promise(r => setTimeout(r, 250));
- if (my === turn && alive && on) say(qs[k].again, true);
- };
- // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
- const press = (e, t) => {
- e.preventDefault();
- if (!on || !alive) return;
- lastAct = now();
- const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
- if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
- if (t) { const my = ++turn; hop(t); showWord(t); return miss(t, my); }
- const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
- r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
- if (++blanks % 2 === 0) miss(null, ++turn);
- };
- const enable = () => {
- on = true;
- const bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
- bg.addEventListener('pointerdown', e => press(e, null));
- for (const t of list) {
- t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all;outline:none', 'aria-label': t.word, role: 'button' }, L);
- t.hit.addEventListener('pointerdown', e => press(e, t));
- }
- // smaller things on top, so a bird in front of a tree can be reached
- [...list].sort((a, b) => box(b).w * box(b).h - box(a).w * box(a).h).forEach(t => L.append(t.hit));
- place();
- P.keys = e => e.key === 'Enter' ? (done(), true) : false;
- ask();
- };
- const cleanup = () => {
- alive = false; L.remove(); word?.remove(); starsOff();
- for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help]) a.pause();
- if (SND.word) SND.word = null;
- Object.assign(FIND, { saying: null, target: null, found: null });
- };
- P.cleanup.push(cleanup);
- let waitFrom = 0;
- const tick = () => {
- if (!alive) return;
- if (P.gen !== gen) { cleanup(); return; }
- // the scene lives on while the page waits: its frame function at D + the seconds waited, on its own 12 fps steps
- if (P.waiting) {
- waitFrom ||= now();
- for (const c of FIND_CLOCKS) if (c.gen === gen) {
- const q = c.D + (now() - waitFrom) / 1000, f = Math.floor(q * FPS + 1e-6);
- if (f !== c.f) { c.f = f; c.draw(f / FPS, f); }
- }
- }
- if (P.waiting && !on && k === 0 && !list[0].hit) enable();
- if (list[0]?.hit) place();
- if (stars) { const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
- [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); }); }
- const w = want();
- if (on && w && now() - askedAt > giveUpAfter * 1000) found(w, true);
- else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
- requestAnimationFrame(tick);
- };
- requestAnimationFrame(tick);
-}
diff --git a/site/little-red-riding-hood/lib/shared.js b/site/little-red-riding-hood/lib/shared.js
index feae531..21786de 100644
--- a/site/little-red-riding-hood/lib/shared.js
+++ b/site/little-red-riding-hood/lib/shared.js
@@ -170,9 +170,3 @@ function bigStep(u, L = 166, reach = 30) {
const h = Math.cos(reach * e * r), kFar = Math.max(0, Math.acos(Math.min(1, 2 * h - Math.cos(far * r))) / r - far);
return { near, far, kNear: 75 * lift, kFar, d: 2 * L * Math.sin(reach * Math.PI / 360) * e * 1.6, drop: L * (1 - h) };
}
-
-// Beds (forest, meadow, room) are 10 s files with soft ends that tile every 9.5 s (SOUND.md): a scene calls sfx() for
-// one once, at its start, and it is laid again every BED_STEP s so it runs to the scene's end (effects past a beat's
-// end never play, and export.py leaves them out too). The page waiting at the meadow stop plays no effects.
-const BED_STEP = { forest: 9.5, meadow: 9.5, room: 9.5 };
-{ const once = window.sfx; window.sfx = (beat, t, name, vol, ...more) => { once(beat, t, name, vol, ...more); if (BED_STEP[name]) for (let k = 1; k * BED_STEP[name] < 60; k++) once(beat, +(t + k * BED_STEP[name]).toFixed(2), name, vol, ...more); }; }
diff --git a/site/madness-shorts/lib/engine.css b/site/madness-shorts/lib/engine.css
index 7083c22..89ee13b 100644
--- a/site/madness-shorts/lib/engine.css
+++ b/site/madness-shorts/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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.js
index 2177807..0a1fe16 100644
--- a/site/madness-shorts/lib/engine.js
+++ b/site/madness-shorts/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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.css
index 7083c22..89ee13b 100644
--- a/site/madness/lib/engine.css
+++ b/site/madness/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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.js
index 2177807..0a1fe16 100644
--- a/site/madness/lib/engine.js
+++ b/site/madness/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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.css
index 7083c22..89ee13b 100644
--- a/site/math-notebook/lib/engine.css
+++ b/site/math-notebook/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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.js
index 2177807..0a1fe16 100644
--- a/site/math-notebook/lib/engine.js
+++ b/site/math-notebook/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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.css
index 7083c22..89ee13b 100644
--- a/site/pandemic-shorts/lib/engine.css
+++ b/site/pandemic-shorts/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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.js
index 2177807..0a1fe16 100644
--- a/site/pandemic-shorts/lib/engine.js
+++ b/site/pandemic-shorts/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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.css
index 7083c22..89ee13b 100644
--- a/site/pandemic/lib/engine.css
+++ b/site/pandemic/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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.js
index 2177807..0a1fe16 100644
--- a/site/pandemic/lib/engine.js
+++ b/site/pandemic/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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.css
index 7083c22..89ee13b 100644
--- a/site/physics-notebook/lib/engine.css
+++ b/site/physics-notebook/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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.js
index 2177807..0a1fe16 100644
--- a/site/physics-notebook/lib/engine.js
+++ b/site/physics-notebook/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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/proteus/lib/engine.css b/site/proteus/lib/engine.css
index 7083c22..89ee13b 100644
--- a/site/proteus/lib/engine.css
+++ b/site/proteus/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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/proteus/lib/engine.js b/site/proteus/lib/engine.js
index 2177807..0a1fe16 100644
--- a/site/proteus/lib/engine.js
+++ b/site/proteus/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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.css
index 7083c22..89ee13b 100644
--- a/site/science/lib/engine.css
+++ b/site/science/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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.js
index 2177807..0a1fe16 100644
--- a/site/science/lib/engine.js
+++ b/site/science/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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.css
index 7083c22..89ee13b 100644
--- a/site/shooting-an-elephant/lib/engine.css
+++ b/site/shooting-an-elephant/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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.js
index 2177807..0a1fe16 100644
--- a/site/shooting-an-elephant/lib/engine.js
+++ b/site/shooting-an-elephant/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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.css
index 7083c22..89ee13b 100644
--- a/site/three-little-pigs/lib/engine.css
+++ b/site/three-little-pigs/lib/engine.css
@@ -103,15 +103,10 @@ 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; }
+/* Listen and find (findWords): the word beside a tapped thing. A book can set --word-font for its early-reader letters. */
+.findw rect:focus-visible { stroke: var(--chalk); stroke-width: 5; outline: none; }
#ui > .card.word { pointer-events: none; padding: 2px 36px 10px; border-inline-start-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.js
index 2177807..0a1fe16 100644
--- a/site/three-little-pigs/lib/engine.js
+++ b/site/three-little-pigs/lib/engine.js
@@ -49,7 +49,6 @@ 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}',
@@ -87,7 +86,6 @@ 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}',
@@ -123,7 +121,6 @@ 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}',
@@ -159,7 +156,6 @@ const TRANSLATIONS = {
chapterComplete: '第{n}章 完了', quickChecks: '確認問題', chapterPractice: '章末練習', sources: 'この章の出典', opensSource: '{label}(出典を新しいタブで開く)',
firstTryScore: '{name}:1 回目で {t} 問中 {r} 問正解。', notAttempted: '{name}:未挑戦。',
watchAgain: 'もう一度見る', nextChapter: '次の章へ',
- wordsHeard: '{k} / {n}', allWordsHeard: 'ぜんぶ見つけたね!',
},
// Arabic plurals (Intl.PluralRules 'ar'): one 1, two 2, few 3–10, many 11–99, other 100+ (zero falls back to other).
// The timeline keeps its left-to-right geometry in Arabic (see RTL below), so ← stays "previous step".
@@ -202,7 +198,6 @@ const TRANSLATIONS = {
chapterComplete: 'اكتمل الفصل {n}', quickChecks: 'الأسئلة القصيرة', chapterPractice: 'تمارين الفصل', sources: 'مصادر هذا الفصل', opensSource: '{label}، يفتح المصدر في علامة تبويب جديدة',
firstTryScore: '{name}: {r} من {t} صحيحة من المحاولة الأولى.', notAttempted: '{name}: لم تُحاوَل.',
watchAgain: 'شاهد مرة أخرى', nextChapter: 'الفصل التالي',
- wordsHeard: '{k} / {n}', allWordsHeard: 'وجدتها كلها!',
},
};
// <html data-stage="portrait"> turns the stage upright: 900 wide, 1600 tall. Landscape (1600×900) otherwise.
@@ -1075,126 +1070,214 @@ 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', side: 'below' }]
-// 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), clip names audio/<lang>/<clip>.mp3, 'w-' + the word by default, and side puts the word
-// 'above', 'below', 'left' or 'right' of its thing (kept on the stage); without it the word goes above, or wherever fits. 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
+/* ---------- listen and find ---------- */
+// A picture book's point-and-read stop, for young children learning words: a film's ask beat (CHAPTER.film),
+// ask: done => findWords(THINGS, ASKS, done, { end: 'q-done', there: 'q-there' })
+// THINGS = [{ g, word, side?, clip?, talk? }], 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); clip names audio/<lang>/<clip>.mp3, 'w-' + the
+// word by default; side puts the word 'above', 'below', 'left' or 'right' of its thing (kept on the stage), else above
+// or wherever it fits; talk: true says the clip is the character's own voice ('q-me' when the reader taps the
+// character), so it moves the mouth and is captioned. ASKS = [{ word, clip, again? }] in order: clip asks for the thing
+// ("Can you find the bird? Touch it!"), again is the short ask after a wrong tap ("Where's the bird?"), 'r-' + the word
+// by default. Options: end, the praise after the last one ('q-done'); there, the give-up line ('q-there'); hintAfter,
+// giveUpAfter (s); minBox, the smallest hit box in stage px (a child's finger on a phone).
+// Once the narration is over (P.waiting) the character asks for one thing at a time. Every thing answers a tap: a wrong
+// one hops and says its own name, then the short ask comes again. Nothing is ever called wrong; each miss helps more: at
+// the second the thing asked for shakes, from the third paper stars circle it and it keeps shaking. A tap on nothing
+// leaves a paper ripple (two count as a miss). A tap inside the asked thing's box finds it, even where a smaller thing's
+// box lies on top. No tap for hintAfter s: it shakes; nobody finds it for giveUpAfter s: the character says the `there`
+// line, it hops and counts as found, so a child who never taps is never stuck. Found, it hops, shows its word and says
+// it, a chime (audio/sfx/chime.mp3); after the last one the praise, and the film goes on by itself. A new tap cuts off
+// whatever was being said, so lines never pile up. Tab or ← → reach the things from the keyboard and Enter or Space taps
+// the one focused; Enter with none focused skips the stop. Seeking or replaying clears it all.
+// While the character speaks, FIND.saying names its clip: the scene poses the mouth with findMouth() and can turn the
+// eyes to FIND.target (the thing asked for) or findSaw() (the thing just found, until the next ask). The character's
+// lines are captioned from their timings when captions are on. A scene that does more with a found thing (a flower
+// flying into a bouquet) watches FIND.found; the hit boxes are <rect>s in a <g class="findw"> over the stage, each with
+// its thing's word as aria-label. The scene keeps drawing while the page waits (the film's ask beats, below).
+// The video (export.py) cannot tap: give the beat video: false, or a number of seconds to cut it there.
+const FIND = { saying: null, t0: 0, target: null, found: null, foundAt: -1e9 }, FIND_SEEN = .9;
+let askLine = ''; // the caption of the character's line while a film's ask waits (ui() shows it)
+const mouthOf = (clip, t) => { let m = 'X'; if (t < 0) return m; for (const [at, k] of window.MOUTHS?.[clip] || []) { if (at > t) break; m = k; } return m; };
+const findMouth = () => FIND.saying ? mouthOf(FIND.saying, performance.now() / 1000 - FIND.t0) : 'X';
+const findSaw = () => performance.now() / 1000 - FIND.foundAt < FIND_SEEN ? FIND.found : null;
const SIDES = { above: 0, below: 1, right: 2, left: 3 }; // where a word goes, in the order tried
-const wordClip = word => 'w-' + word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
-function tapWords(things, done) {
+const slug = word => word.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '');
+function findWords(things, asks, done, { end = 'q-done', there = 'q-there', hintAfter = 5, giveUpAfter = 20, minBox = 120 } = {}) {
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');
+ if (!CHAPTER.film) console.warn('findWords: CHAPTER.film is not true, so the chapter plays as a lesson: no music or effects');
+ const clip = (name, who) => {
+ const a = new Audio(`audio/${lang}/${name}.mp3`);
+ a.preload = 'auto'; a.clipName = name;
+ a.onerror = () => console.warn(`findWords: ${who} has no clip at audio/${lang}/${name}.mp3`);
+ return a;
+ };
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`);
+ if (why) console.warn(`findWords: ${t.word ? `"${t.word}"` : 'a thing'} ${why}: left out`);
+ if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`findWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
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`);
- if (t.side && !Object.hasOwn(SIDES, t.side)) console.warn(`tapWords: "${t.word}" has side "${t.side}": use above, below, left or right; its word goes where it fits`);
- return { g: t.g, word: t.word, clip, a, side: Object.hasOwn(SIDES, t.side ?? '') ? t.side : null, heard: false };
+ }).map(t => ({ ...t, a: clip(t.clip || 'w-' + slug(t.word), `"${t.word}"`) }));
+ const qs = asks.map(q => {
+ if (!list.some(t => t.word === q.word)) console.warn(`findWords: the ask for "${q.word}" has no thing with that word: it is found when its time runs out`);
+ return { ...q, a: clip(q.clip, `the ask for "${q.word}"`), again: clip(q.again || 'r-' + slug(q.word), `the short ask for "${q.word}"`) };
});
- 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 praise = clip(end, 'the praise (end)'), help = clip(there, 'the give-up line (there)'), chime = new Audio('audio/sfx/chime.mp3');
+ const L = mk('g', { class: 'findw' }, stage); // the hit boxes, over the whole picture
+ let on = false, alive = true, finished = false, k = 0, word = null, lastAct = 0, askedAt = 0, wiggling = null, playing = null;
+ let misses = 0, blanks = 0, turn = 0, stars = null, enabled = false;
+ const now = () => performance.now();
+ const finish = () => { if (finished || P.gen !== gen) return; finished = true; done(); };
+ // a thing's box in stage px (its picture may move: puppets idle), and its hit box, at least minBox 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: on its side, or above it, else below, right or left, wherever it fits on the stage
- // (slid along that side to stay on it)
- word?.remove();
+ const hitBox = t => { const b = box(t), w = Math.max(minBox, b.w), hh = Math.max(minBox, b.h); return { x: b.x + (b.w - w) / 2, y: b.y + (b.h - hh) / 2, w, h: hh }; };
+ const place = () => { for (const t of list) { const b = hitBox(t); for (const [key, v] of Object.entries({ x: b.x, y: b.y, width: b.w, height: b.h })) t.hit.setAttribute(key, v.toFixed(1)); } };
+ const stagePoint = e => { const s = stage.getBoundingClientRect(); return [(e.clientX - s.left) * SW / s.width, (e.clientY - s.top) * SH / s.height]; };
+ const want = () => list.find(t => t.word === qs[k]?.word);
+ // one sound at a time, ducking the music like the narration (SND.word); the character's own lines move its mouth and
+ // are captioned; resolves when the sound ends
+ const say = (a, talk = false) => new Promise(ok => {
+ if (!alive) return ok();
+ playing?.pause(); playing = a;
+ a.currentTime = 0; a.volume = +($('volume')?.value ?? 1);
+ FIND.saying = talk ? a.clipName : null; FIND.t0 = now() / 1000;
+ askLine = talk ? (TIMINGS[a.clipName]?.cues || []).map(c => c[1]).join(' ') : '';
+ a.onended = () => { if (SND.word === a) SND.word = null; if (FIND.saying === a.clipName) FIND.saying = null; if (talk) askLine = ''; ok(); };
+ SND.word = a;
+ a.play().catch(ok);
+ });
+ let wordTimer = 0;
+ const hideWord = (after = 1500) => { clearTimeout(wordTimer); const w = word; wordTimer = setTimeout(() => w?.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 300 }).finished.then(() => w.remove()), after); };
+ // the word beside its thing: on its side, or above it, else below, right or left, wherever it fits on the stage
+ const showWord = t => {
+ word?.remove(); clearTimeout(wordTimer);
word = h('div', 'card word', t.word);
word.setAttribute('aria-hidden', 'true'); // the thing's own label already says the word
$('ui').append(word);
// gap: clear of the thing's hop; m: the stage's margin
const b = box(t), w = word.offsetWidth, hh = word.offsetHeight, gap = 30, m = 16, cx = b.x + (b.w - w) / 2, cy = b.y + (b.h - hh) / 2;
const spots = [[cx, b.y - hh - gap], [cx, b.y + b.h + gap], [b.x + b.w + gap, cy], [b.x - w - gap, cy]];
- const [x, y] = t.side ? spots[SIDES[t.side]] : spots.find(([x, y], k) => k < 2 ? y >= m && y + hh <= SH - m : x >= m && x + w <= SW - m) || spots[0];
+ const [x, y] = t.side in SIDES ? spots[SIDES[t.side]] : spots.find(([x, y], i) => i < 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
- // the hop is the CSS translate property, which adds to the transform the scene sets (a prop posed every frame keeps its place)
- 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' });
+ };
+ // the hop and the shake are the CSS translate property, which adds to the transform the scene sets; a scene can copy
+ // it onto a picture it moves itself (getComputedStyle(g).translate)
+ const hop = t => t.g.animate([{ translate: '0 0' }, { translate: '0 -22px', offset: .3 }, { translate: '0 0' }], { duration: 450, easing: 'ease-out' });
+ const wiggle = t => {
+ if (wiggling) return;
+ wiggling = t.g.animate([{ translate: '0 0' }, { translate: '-20px -6px' }, { translate: '20px 0' }, { translate: '-16px -4px' }, { translate: '12px 0' }, { translate: '0 0' }], { duration: 750 });
+ wiggling.onfinish = () => { wiggling = null; lastAct = now() - (hintAfter - (misses >= 3 ? 0 : 2.5)) * 1000; };
+ };
+ // paper stars close round the thing asked for, from the third miss until it is found
+ const starsOn = t => {
+ if (stars) return;
+ stars = mk('g', { 'pointer-events': 'none' }, L.parentNode);
+ stars.innerHTML = Array.from({ length: 6 }, () => `<path d="M0,-28 L8,-8 L28,-8 L12,6 L18,26 L0,14 L-18,26 L-12,6 L-28,-8 L-8,-8 Z" fill="#f4c542" stroke="#b8862a" stroke-width="3"/>`).join('');
+ stars.t = t;
+ };
+ const starsOff = () => { stars?.remove(); stars = null; };
+ const ask = async () => {
+ lastAct = askedAt = now();
+ window.FIND_STATE = { k, of: qs.length }; // for tests (engine/check_find.py)
+ FIND.target = want()?.g || null;
+ if (k >= qs.length) {
+ on = false; word?.remove(); FIND.target = null;
+ await say(praise, true);
+ setTimeout(() => { if (alive && P.gen === gen) { window.FIND_STATE.done = true; finish(); } }, 900);
+ return;
+ }
+ await say(qs[k].a, true);
+ lastAct = askedAt = now();
+ };
+ const found = async (t, given) => {
+ on = false; starsOff(); turn++;
+ if (t) { hop(t); showWord(t); }
+ if (given) await say(help, true);
+ if (t) { await say(t.a); hideWord(600); FIND.found = t.g; FIND.foundAt = now() / 1000; }
+ chime.currentTime = 0; chime.volume = .5 * +($('volume')?.value ?? 1); chime.play().catch(() => {});
+ await new Promise(r => setTimeout(r, 700));
+ if (!alive) return;
+ k++; misses = blanks = 0; on = true; ask();
+ };
+ const miss = async (t, my) => {
+ misses++;
+ const w = want();
+ if (misses >= 2 && w) wiggle(w);
+ if (misses >= 3 && w) starsOn(w);
+ if (t) { await say(t.a, !!t.talk); if (my !== turn) return; hideWord(); }
+ await new Promise(r => setTimeout(r, 250));
+ if (my === turn && alive && on) say(qs[k].again, true);
+ };
+ // a thing tapped (or chosen from the keyboard): the one asked for is found, any other says its name
+ const tapThing = t => {
+ if (!on || !alive) return;
+ lastAct = now();
+ if (t === want()) return found(t);
+ const my = ++turn; hop(t); showWord(t); miss(t, my);
+ };
+ // every tap: inside the asked thing's box it is found, whatever lies on top; else the thing tapped, or nothing
+ const press = (e, t) => {
+ e.preventDefault();
+ if (!on || !alive) return;
+ lastAct = now();
+ const [x, y] = stagePoint(e), w = want(), b = w && hitBox(w);
+ if (b && x >= b.x && x <= b.x + b.w && y >= b.y && y <= b.y + b.h) return found(w);
+ if (t) return tapThing(t);
+ const r = mk('circle', { cx: x, cy: y, r: 10, fill: 'none', stroke: '#fbf6ea', 'stroke-width': 4, 'pointer-events': 'none' }, L.parentNode);
+ r.animate([{ r: 10, opacity: .9 }, { r: 46, opacity: 0 }], { duration: 500, easing: 'ease-out' }).finished.then(() => r.remove());
+ if (++blanks % 2 === 0) miss(null, ++turn);
};
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 bg = mk('rect', { x: 0, y: 0, width: SW, height: SH, fill: 'transparent', style: 'pointer-events:all' }, L);
+ bg.addEventListener('pointerdown', e => press(e, null));
+ for (const t of list) {
+ t.hit = mk('rect', { fill: 'transparent', style: 'cursor:pointer;pointer-events:all', 'aria-label': t.word, role: 'button', tabindex: 0 }, L);
+ t.hit.addEventListener('pointerdown', e => press(e, t));
+ keyAct(t.hit, () => tapThing(t));
+ }
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); }
+ 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(`findWords: "${t.word}" is not in the picture`);
+ // smaller things on top, so a bird in front of a tree can be reached
+ [...list].sort((a, c) => at.get(c).w * at.get(c).h - at.get(a).w * at.get(a).h).forEach(t => L.append(t.hit));
place();
- // the arrows go through the things from left to right
+ // ← → go through the things from left to right; Enter with none focused skips the stop
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]));
- // a thing whose box is covered where a finger lands (a card, a layer over the stage) cannot be tapped: once the
- // counter's card has come in, try each box's middle and four points around it, and name what covers it
- setTimeout(() => { if (alive) for (const t of list) {
- const r = t.ring.getBoundingClientRect(), pts = [[.5, .5], [.3, .3], [.7, .3], [.3, .7], [.7, .7]];
- const tops = pts.map(([u, v]) => document.elementFromPoint(r.left + u * r.width, r.top + v * r.height));
- if (!tops.some(e => t.hit.contains(e))) {
- const e = tops[0], cls = e?.getAttribute('class')?.split(' ')[0];
- const what = !e ? 'it is off screen' : e.closest('.hit') ? `"${e.closest('.hit').getAttribute('aria-label')}" covers it`
- : `<${e.tagName.toLowerCase()}${e.id ? '#' + e.id : cls ? '.' + cls : ''}> covers it`;
- console.warn(`tapWords: "${t.word}" cannot be tapped: ${what}`);
- }
- } }, 600);
+ let cur = -1;
+ P.keys = e => {
+ if (e.key === 'Enter') return finish(), true;
+ const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
+ if (!d || !order.length) return false;
+ const i = order.findIndex(t => t.hit === document.activeElement);
+ cur = i >= 0 ? (i + d + order.length) % order.length : d > 0 ? 0 : order.length - 1;
+ order[cur].hit.focus();
+ return true;
+ };
+ ask();
};
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;
+ alive = false; on = false; L.remove(); word?.remove(); starsOff(); clearTimeout(wordTimer); wiggling?.cancel();
+ for (const a of [...list.map(t => t.a), ...qs.map(q => q.a), ...qs.map(q => q.again), praise, help, chime]) { a.onerror = null; a.pause(); }
+ SND.word = null; askLine = '';
+ Object.assign(FIND, { saying: null, target: null, found: null, foundAt: -1e9 });
};
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();
+ if (P.waiting && !enabled) { enabled = true; enable(); }
+ if (list[0]?.hit) place();
+ if (stars) {
+ const b = box(stars.t), cx = b.x + b.w / 2, cy = b.y + b.h / 2, R = Math.max(b.w, b.h) * .38 + 24, a0 = now() / 900;
+ [...stars.children].forEach((st, i) => { const a = a0 + i * Math.PI / 3; st.setAttribute('transform', `translate(${(cx + R * Math.cos(a)).toFixed(1)},${(cy + R * .8 * Math.sin(a)).toFixed(1)}) rotate(${(a * 60).toFixed(0)})`); });
+ }
+ const w = want();
+ if (on && now() - askedAt > giveUpAfter * 1000) found(w, true);
+ else if (on && w && now() - lastAct > hintAfter * 1000) wiggle(w);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@@ -1364,8 +1447,12 @@ const blanks = rowsIn => (body, api) => {
/* ---------- sound effects and music ----------
sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback
- (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length
- (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.
+ (a seek past it fires nothing), at half volume (vol 2 is full). Effects past a beat's end never play. A long one-off
+ sound is listed in SFX_BED with its length (SFX_BED.birds = 10), or given it as `bed`: a seek into it joins it part-way.
+ A bed of ambience that lasts: sfx(beat, at, 'river', .5, { loop: true }) lays the file again every (its length −
+ BED_OVERLAP) s, so a 10 s file with soft ends runs to the beat's end, and through a film's ask wait; { loop: true,
+ to: t } stops it at t on the beat clock (a forest bed under an indoor close-up). A seek joins it where it would be.
+ A beat function runs again at every seek; an effect is listed once.
An effect still loading when it is due plays late, joined part-way, for up to a second (or its bed).
music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the
length of its earlier beats plus the clock. It ducks to 30 % under the whole beat, or 70 % in the music-only lead-in
@@ -1381,8 +1468,10 @@ const everyBeat = () => { try { return BEATS.map(b => b.id); } catch { return un
// window properties, not declarations: a book with its own older sfx() or BGM declares them over these
Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,
sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {
+ const { loop = false, to = null } = typeof bed === 'object' && bed || {};
+ if (typeof bed !== 'number') bed = SND.bed[name] || 0;
const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);
- if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });
+ if (e) Object.assign(e, { vol, bed, loop, to }); else SND.sfx.push({ beat, at, name, vol, bed, loop, to });
sfxLoad(name);
},
// without beats, every beat: boot() fills them in when music() comes before BEATS
@@ -1401,15 +1490,25 @@ function sfxLoad(name) {
const ctx = SND.ctx || (window.OfflineAudioContext && (DECODER ??= new OfflineAudioContext(2, 1, 44100)));
if (ctx && !s.decoded) s.decoded = s.data.then(d => d && ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});
}
-function sfxPlay(e, late) {
+function sfxPlay(e, late, at = e.at, t = P.t) {
const ctx = SND.ctx, src = ctx.createBufferSource(), g = ctx.createGain();
- src.buffer = SND.buf[e.name].b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;
+ src.buffer = SND.buf[e.name].b; src._sfx = e.name; src._e = e; src._at = at; g.gain.value = src._vol = .5 * e.vol;
src.playbackRate.value = P.rate;
src.connect(g).connect(SND.out);
- src.start(0, late && !e.bed ? 0 : Math.max(0, P.t - e.at)); // a late one-shot starts from its attack
+ src.start(0, late && !e.bed ? 0 : Math.max(0, t - at)); // a late one-shot starts from its attack
SND.on.push(src);
src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };
}
+// A bed (sfx's { loop: true }) is laid at `at` and again every BED_OVERLAP s short of its file's length, so the soft ends
+// of one lay and the next overlap, until `to` on the beat clock, else the beat's end and on through an ask wait. Each
+// frame the lay due now is playing, or starts where it should be by now: a seek, a late load and a wait all join it.
+const BED_OVERLAP = .5;
+function bedLay(e, t) {
+ const b = SND.buf[e.name]?.b;
+ if (!b || t < e.at || t >= (e.to ?? Infinity)) return;
+ const step = Math.max(.5, b.duration - BED_OVERLAP), at = e.at + Math.floor((t - e.at) / step) * step;
+ if (t - at < b.duration - .05 && !SND.on.some(s => s._e === e && Math.abs(s._at - at) < 1e-6)) sfxPlay(e, false, at, t);
+}
function bgmWire(m) {
m.g = SND.ctx.createGain();
SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);
@@ -1444,26 +1543,31 @@ function soundTick(dt) {
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
+ // the sound's clock: the beat's, and on past its end while a film's ask waits (P.idle), where only the beds play
+ const T = P.t + (P.waiting ? P.idle : 0);
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
+ const same = L.i === P.i && T >= L.t && T - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
- const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
- if (on && ctx) {
+ const from = same ? L.t : T < .25 ? -1 : T, seeked = !same || L.pend;
+ if ((on || wait) && ctx) {
for (const e of SND.sfx) {
- if (e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;
+ if (e.beat !== id) continue;
+ if (e.loop) { bedLay(e, T); continue; }
+ if (!on || !(e.at > from && e.at <= T || seeked && e.bed && e.at < T && T < e.at + e.bed)) continue;
if (SND.buf[e.name]?.b) sfxPlay(e); else SND.wait.push(e);
}
- SND.wait = SND.wait.filter(e => P.t - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
+ if (on) SND.wait = SND.wait.filter(e => T - e.at < (e.bed || 1) && !(SND.buf[e.name]?.b && (sfxPlay(e, true), true)));
for (const s of SND.on) if (s.playbackRate.value !== P.rate) s.playbackRate.value = P.rate;
}
- SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
+ for (const s of [...SND.on]) if (s._e?.loop && T >= (s._e.to ?? Infinity)) s.stop(); // a bed stopped mid-beat (onended drops it)
+ SND.last = { i: P.i, t: on || wait ? T : from, pend: !(on || wait) && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
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);
- // a word the reader tapped (tapWords) ducks the music at once, as the narration does
+ // a word said at a listen-and-find stop (findWords) 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) {
@@ -1508,7 +1612,7 @@ function soundTick(dt) {
}
/* ---------- player ---------- */
-const P = { i: 0, t: 0, end: 1, back: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
+const P = { i: 0, t: 0, end: 1, back: 0, idle: 0, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };
function runBeat(i) {
TW = [];
const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0; // pre: seconds of picture before the narration starts
@@ -1580,7 +1684,7 @@ function start(i, play) {
stopAudio(); clearCards();
// P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
const gen = P.gen = (P.gen || 0) + 1;
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
+ Object.assign(P, { i, t: 0, idle: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0, back: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1641,6 +1745,15 @@ function setPlaying(v) {
a.play().then(() => { if (P.t < (BEATS[P.i].pre || 0)) { a.pause(); a.currentTime = 0; } a.muted = false; }, () => { a.muted = false; });
} else a.pause();
}
+// A film's ask beat waiting for the reader (CHAPTER.film): P.idle counts the seconds it has waited, playing (a pause
+// holds it, as it holds the music), and the beat's continuous motion (stopClock, clockRun with tail: true) runs on past
+// its end at P.t + P.idle, so puppets breathe, blink and talk while a child looks; its beds play on (soundTick).
+// export.py calls P.idleAt(s) to draw a frame s seconds into the wait.
+function idleAt(s) {
+ P.idle = s;
+ for (const w of TW) if (w.tail === true && w.dur) w.pf((P.t + s - w.t0) / w.dur);
+}
+P.idleAt = idleAt;
function beatDone() {
// Steps finish; continuous motion (tails) stays where the clock is: a beat under music ends up to HOLD early, and
// jumping its tails to their end would move them a few px on the last frame
@@ -1693,7 +1806,7 @@ function frameStep(now, most) {
const back = P.back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
evalTo(P.t);
if (P.t >= P.end - back) beatDone();
- }
+ } else if (P.playing && P.waiting && !P.done && CHAPTER.film) idleAt(P.idle + dt * P.rate);
soundTick(dt);
syncLinks();
ui();
@@ -1730,7 +1843,8 @@ function ui() {
// a question's caption shows while its narration plays, clear of the cards; a full-screen card already shows the
// question, so it has none
const asking = !!b.ask;
- if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
+ if (P.started && !P.done && asking && P.waiting) text = captions ? askLine : ''; // a listen-and-find character's line
+ else if (P.started && !P.done && !(asking && $('ui').querySelector('.card.screen'))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
// words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
// cue of a beat whose timings say forced: true, from tts.py or eleven.py)
@@ -1927,7 +2041,7 @@ return { TRANSLATIONS, PORTRAIT, SW, SH, LANGUAGE, translate, DECIMAL_SIGN, isSh
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,
+ h, rich, card, mascotEl, quiz, qlayer, keyAct, choice, mcq, tap, tapEls, pickEls, findWords, FIND, findMouth, findSaw, grid, rat, gcd, lowest, blanks,
SND, P, HOLD, clearCards, seek, seekInto, start, setSpeed, setPlaying, setCaptions, hideCover, restart, boot,
finishCard };
})();
@@ -1954,7 +2068,7 @@ const TRANSLATIONS = ENGINE.TRANSLATIONS, PORTRAIT = ENGINE.PORTRAIT, SW = ENGIN
uiW = 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,
- tapWords = ENGINE.tapWords, grid = ENGINE.grid, rat = ENGINE.rat, gcd = ENGINE.gcd, lowest = ENGINE.lowest, blanks = ENGINE.blanks,
+ findWords = ENGINE.findWords, FIND = ENGINE.FIND, findMouth = ENGINE.findMouth, findSaw = ENGINE.findSaw, 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;