@braccato/core 1.22.0 → 2.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +202 -607
- package/js/element.js +234 -0
- package/js/renderer.d.ts +157 -0
- package/js/renderer.js +535 -0
- package/package.json +22 -17
- package/pkg/braccato_core_rs.d.ts +236 -0
- package/pkg/braccato_core_rs.js +1735 -0
- package/pkg/braccato_core_rs_bg.wasm +0 -0
- package/pkg/braccato_core_rs_bg.wasm.d.ts +55 -0
- package/dist/LICENSE +0 -21
- package/dist/README.md +0 -639
- package/dist/element.d.ts +0 -168
- package/dist/element.js +0 -730
- package/dist/engine/animationConfig.d.ts +0 -84
- package/dist/engine/animationConfig.js +0 -95
- package/dist/engine/animationDrift.d.ts +0 -11
- package/dist/engine/animationDrift.js +0 -43
- package/dist/engine/animationTiming.d.ts +0 -27
- package/dist/engine/animationTiming.js +0 -145
- package/dist/engine/autoScroll.d.ts +0 -24
- package/dist/engine/autoScroll.js +0 -103
- package/dist/engine/clock.d.ts +0 -29
- package/dist/engine/clock.js +0 -70
- package/dist/engine/contextSweep.d.ts +0 -10
- package/dist/engine/contextSweep.js +0 -102
- package/dist/engine/cssValues.d.ts +0 -13
- package/dist/engine/cssValues.js +0 -47
- package/dist/engine/culling.d.ts +0 -6
- package/dist/engine/culling.js +0 -76
- package/dist/engine/debugOverlay.d.ts +0 -14
- package/dist/engine/debugOverlay.js +0 -58
- package/dist/engine/decorationSlides.d.ts +0 -6
- package/dist/engine/decorationSlides.js +0 -53
- package/dist/engine/elementMotion.d.ts +0 -18
- package/dist/engine/elementMotion.js +0 -98
- package/dist/engine/glow.d.ts +0 -7
- package/dist/engine/glow.js +0 -59
- package/dist/engine/instance.d.ts +0 -105
- package/dist/engine/instance.js +0 -89
- package/dist/engine/instrumentalAnimations.d.ts +0 -7
- package/dist/engine/instrumentalAnimations.js +0 -59
- package/dist/engine/letterPlans.d.ts +0 -31
- package/dist/engine/letterPlans.js +0 -62
- package/dist/engine/lineAnimationState.d.ts +0 -10
- package/dist/engine/lineAnimationState.js +0 -82
- package/dist/engine/lineAnimations.d.ts +0 -5
- package/dist/engine/lineAnimations.js +0 -63
- package/dist/engine/lineLifecycle.d.ts +0 -12
- package/dist/engine/lineLifecycle.js +0 -92
- package/dist/engine/lineScroll.d.ts +0 -47
- package/dist/engine/lineScroll.js +0 -184
- package/dist/engine/lineScrollStyle.d.ts +0 -32
- package/dist/engine/lineScrollStyle.js +0 -133
- package/dist/engine/liveViews.d.ts +0 -3
- package/dist/engine/liveViews.js +0 -5
- package/dist/engine/motionCurves.d.ts +0 -19
- package/dist/engine/motionCurves.js +0 -85
- package/dist/engine/motionPreference.d.ts +0 -1
- package/dist/engine/motionPreference.js +0 -1
- package/dist/engine/passiveScroll.d.ts +0 -10
- package/dist/engine/passiveScroll.js +0 -93
- package/dist/engine/renderedLines.d.ts +0 -16
- package/dist/engine/renderedLines.js +0 -130
- package/dist/engine/scrollViewport.d.ts +0 -36
- package/dist/engine/scrollViewport.js +0 -104
- package/dist/engine/stageMotion.d.ts +0 -7
- package/dist/engine/stageMotion.js +0 -319
- package/dist/engine/sweepPlans.d.ts +0 -38
- package/dist/engine/sweepPlans.js +0 -66
- package/dist/engine/tick.d.ts +0 -16
- package/dist/engine/tick.js +0 -270
- package/dist/engine/timingLogs.d.ts +0 -7
- package/dist/engine/timingLogs.js +0 -81
- package/dist/engine/userScroll.d.ts +0 -12
- package/dist/engine/userScroll.js +0 -62
- package/dist/engine/wavePool.d.ts +0 -7
- package/dist/engine/wavePool.js +0 -22
- package/dist/engine/wordAnimations.d.ts +0 -5
- package/dist/engine/wordAnimations.js +0 -206
- package/dist/imageHighlights.d.ts +0 -4
- package/dist/imageHighlights.js +0 -34
- package/dist/index.d.ts +0 -17
- package/dist/index.js +0 -12
- package/dist/inject.d.ts +0 -98
- package/dist/inject.js +0 -591
- package/dist/instrumental.d.ts +0 -11
- package/dist/instrumental.js +0 -121
- package/dist/language.d.ts +0 -9
- package/dist/language.js +0 -47
- package/dist/letters.d.ts +0 -2
- package/dist/letters.js +0 -25
- package/dist/renderer.d.ts +0 -29
- package/dist/renderer.js +0 -412
- package/dist/seek.d.ts +0 -1
- package/dist/seek.js +0 -27
- package/dist/stage.d.ts +0 -58
- package/dist/stage.js +0 -121
- package/dist/types.d.ts +0 -236
- package/dist/types.js +0 -1
- package/dist/view.d.ts +0 -51
- package/dist/view.js +0 -398
package/README.md
CHANGED
|
@@ -1,639 +1,234 @@
|
|
|
1
|
-
# @braccato/core
|
|
1
|
+
# @braccato/core 2
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
The braccato lyrics renderer rewritten in Rust and drawn with WebGL2. From 2.0, this is
|
|
4
|
+
`@braccato/core`, in beta under the npm `beta` tag (`npm i @braccato/core@beta`) while `latest` stays
|
|
5
|
+
on 1.x. It shows the same lyrics the same way as the 1.x DOM renderer ([`packages/core`](../core),
|
|
6
|
+
kept in this repo as the private `@braccato/core-dom`): the same layout and wrapping, syllable sweep, glow, word
|
|
7
|
+
wobble, letter wave, line scale, autoscroll glide, background vocals, timed romanizations,
|
|
8
|
+
translations, instrumental note, credits and stage layout. It does all of that from one canvas, so a
|
|
9
|
+
playing song costs no style, layout or paint work per frame.
|
|
6
10
|
|
|
7
|
-
|
|
8
|
-
|
|
11
|
+
The glow is the one from 1.16.11, restored in PR #112: a single drop shadow of the lit word whose
|
|
12
|
+
radius shrinks from `--blyrics-highlight-glow-radius-from` to nothing.
|
|
9
13
|
|
|
10
|
-
|
|
11
|
-
scrolling and theming with lyrics from the Better Lyrics API. This README is still the full reference.
|
|
14
|
+
## Use
|
|
12
15
|
|
|
13
|
-
|
|
16
|
+
### As 1.x's renderer
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
```
|
|
18
|
+
The package's main export has 1.x's renderer interface, so a consumer written against 1.x upgrades
|
|
19
|
+
by loading the wasm first:
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
```js
|
|
22
|
+
import { createLyricsRenderer, injectTranslation, load } from "@braccato/core";
|
|
23
|
+
|
|
24
|
+
await load(); // or load(wasmUrl), or loadSync(bytes); the file is "@braccato/core/wasm"
|
|
25
|
+
const view = createLyricsRenderer({ document, window, host: { getScrollElement: () => scroller } });
|
|
26
|
+
view.setLyrics(lyrics, { mount, songKey: videoId });
|
|
27
|
+
view.tick(player.currentTime, { isPlaying: true, eventCreationTime: Date.now() });
|
|
28
|
+
injectTranslation(document, view.lines[3].lyricElement, "…", "en");
|
|
29
|
+
view.scheduleLyricPositionUpdate(() => true, () => {});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`createLyricsRenderer`, `injectTranslation`, `injectRomanization`, `removeTranslation`,
|
|
33
|
+
`removeRomanization`, `resetPlaybackClock` and `resumeAllAutoscroll` take what 1.x's do, and the
|
|
34
|
+
leaf modules `@braccato/core/constants`, `/text`, `/themeSettings` and `/util` and the stylesheets
|
|
35
|
+
under `@braccato/core/styles/` are 1.x's own. What is different underneath:
|
|
36
|
+
|
|
37
|
+
- The renderer draws on its own animation frames, so `tick` only hands it the player snapshot (time,
|
|
38
|
+
play state, and when it was read); frames in between carry the time forward. Ticking at 20 ms is
|
|
39
|
+
as smooth as ticking every frame.
|
|
40
|
+
- `lines` are records over the Rust song rather than core's render state: `lyricElement`, `parts`,
|
|
41
|
+
and `time` and `duration` in seconds, where writing `time` moves the line or word. The elements are
|
|
42
|
+
the invisible layout DOM's (below), so their `dataset` is the host's to use.
|
|
43
|
+
- Decorations are hung on a line with this module's `inject*`, not written into the element, and
|
|
44
|
+
are applied together on the next frame, sliding the lines they move.
|
|
45
|
+
- `setTheme` always returns false: the renderer keeps the lyrics and rebuilds them itself.
|
|
46
|
+
- Lyrics given with the `songKey` of the song on screen are swapped in: lines the two share keep their
|
|
47
|
+
elements, state and decorations, a changed line is rebuilt in its old element, the line in view
|
|
48
|
+
holds still and the others slide.
|
|
49
|
+
- A view can live in another window (Document Picture-in-Picture): it draws on that window's frames
|
|
50
|
+
and resolves fonts against its document.
|
|
51
|
+
|
|
52
|
+
### As a custom element
|
|
20
53
|
|
|
21
54
|
```html
|
|
22
|
-
<audio id="player" src="song.mp3"
|
|
55
|
+
<audio id="player" src="song.mp3"></audio>
|
|
23
56
|
<braccato-lyrics source="#player"></braccato-lyrics>
|
|
24
57
|
|
|
25
58
|
<script type="module">
|
|
26
|
-
import "@braccato/core/element";
|
|
27
|
-
import "@braccato/core/styles/variables.css";
|
|
28
|
-
import "@braccato/core/styles/lyrics.css";
|
|
29
|
-
import "@braccato/core/styles/instrumental.css";
|
|
30
|
-
|
|
31
|
-
document.querySelector("braccato-lyrics").lyrics = [
|
|
32
|
-
{ startTimeMs: 0, durationMs: 4200, words: "The first line" },
|
|
33
|
-
{ startTimeMs: 4200, durationMs: 3800, words: "The second" },
|
|
34
|
-
];
|
|
35
|
-
</script>
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
`source` takes a CSS selector or a media element. It is resolved when the element connects, so put
|
|
39
|
-
the `<audio>` before the tag, or write the property from script. Without a source, drive the view
|
|
40
|
-
yourself by writing `currentTime` and `playing`.
|
|
41
|
-
|
|
42
|
-
Two things catch everybody once. The element has no `display` of its own:
|
|
43
|
-
|
|
44
|
-
```css
|
|
45
|
-
braccato-lyrics {
|
|
46
|
-
display: block;
|
|
47
|
-
}
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
And autoscroll writes `scrollTop` on the nearest ancestor that scrolls, falling through to the
|
|
51
|
-
document when nothing does. If the element is not inside its own scroller, say which one it is:
|
|
52
|
-
|
|
53
|
-
```js
|
|
54
|
-
view.host = { getScrollElement: () => yourFrame };
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
## Lyrics
|
|
58
|
-
|
|
59
|
-
The array is the whole input, and nothing in this package produces one.
|
|
60
|
-
[`@braccato/parsers`](https://www.npmjs.com/package/@braccato/parsers) reads TTML, LRC, SRT, QRC and
|
|
61
|
-
plain text, and picks between them by looking at the file.
|
|
59
|
+
import { defineLyricsElement, load } from "@braccato/core/element";
|
|
62
60
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
view.lyrics = detectParser(text).parse(text, player.duration * 1000);
|
|
61
|
+
await load();
|
|
62
|
+
defineLyricsElement(); // or defineLyricsElement("my-lyrics")
|
|
63
|
+
document.querySelector("braccato-lyrics").lyrics = lyricsArray;
|
|
64
|
+
</script>
|
|
68
65
|
```
|
|
69
66
|
|
|
70
|
-
|
|
71
|
-
fields for syllable or word timing, and optional `translation`, `romanization` and
|
|
72
|
-
`timedRomanization` beside them.
|
|
67
|
+
The element takes the same `Lyric[]` as `@braccato/core` and keeps most of its surface:
|
|
73
68
|
|
|
74
|
-
|
|
75
|
-
|
|
69
|
+
| Property | Notes |
|
|
70
|
+
| --------------- | ----- |
|
|
71
|
+
| `lyrics` | `Lyric[]`. Writing it builds the view. |
|
|
72
|
+
| `lyricsOptions` | `{ noLyrics?, language?, songwriters? }`, read by the next `lyrics` write. |
|
|
73
|
+
| `source` | A media element or a selector for one. It drives the clock. |
|
|
74
|
+
| `currentTime`, `playing` | Drive the clock by hand when there is no `source`. |
|
|
75
|
+
| `tickOptions` | `{ lyricOffset?, globalLyricOffset?, playbackRate?, smoothScroll?, passiveScrollEnabled? }` |
|
|
76
|
+
| `theme` | A stylesheet: `--blyrics-*` custom properties, and `/* blyrics-key = value; */` settings in comments. |
|
|
77
|
+
| `host` | `{ getScrollElement? }`, the element autoscroll moves. |
|
|
78
|
+
| `layout` | `"scroll"` or `"stage"`. Also an attribute. |
|
|
79
|
+
| `decorations` | `{ translations, romanizations }`: whether to show the lyrics' own `translations` and `romanization` / `timedRomanization`. Both on by default. |
|
|
80
|
+
| `renderer` | The Rust `Renderer`, which has `resumeAutoscroll()`, `relayout()`, `setProfiling(on)`, `stats()` and `takeProfile()`. |
|
|
76
81
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
view.lyricsOptions = { songwriters: parser.metadata(text).songwriters };
|
|
80
|
-
view.lyrics = parser.parse(text, player.duration * 1000);
|
|
81
|
-
```
|
|
82
|
+
It dispatches the same events: `braccato:lyrics-loaded`, `braccato:line-click`,
|
|
83
|
+
`braccato:scroll-state`, `braccato:stage-layout` and `braccato:error`.
|
|
82
84
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
arriving for the song already playing, or an editor handing over every edit. Give each update the
|
|
87
|
-
same `songKey` (a `setLyrics` option, or a field of the element's `lyricsOptions`), and lyrics for the
|
|
88
|
-
song already on screen are swapped into it rather than built over it, so the only thing that changes
|
|
89
|
-
on screen is what changed in the lyrics. Lyrics with no `songKey`, or another song's, are built anew,
|
|
90
|
-
so a new song starts at the top with nothing of the last one's scroll.
|
|
91
|
-
|
|
92
|
-
- A line that builds the same as one already shown, by its words, timing, parts, singer, translation
|
|
93
|
-
and romanization, keeps that element and the animations running on it. The arrays do not need to be the same objects,
|
|
94
|
-
so a consumer that parses its source again on every change loses nothing.
|
|
95
|
-
- Lines that differ are built again in place. A line built where another one stood takes over that
|
|
96
|
-
line's place in the scroll, and on the stage its role and position, so a retimed line being sung
|
|
97
|
-
does not fade in again. A stage line replaced mid-glide finishes the glide it was in, unless the
|
|
98
|
-
new line is a different height, which moves it straight to its new place.
|
|
99
|
-
- The scroll stays where it was. When lines above the one being followed grow, shrink, arrive or go,
|
|
100
|
-
the scroll moves by however far that line moved, so it holds still on screen, and the lines that
|
|
101
|
-
moved relative to it slide there. A reader who scrolled away stays scrolled away.
|
|
102
|
-
- Translations and romanizations hung on a kept line stay, and so do those on a line rebuilt only for
|
|
103
|
-
its timing. Injecting the same one again does nothing, and injecting a different one replaces it. A
|
|
104
|
-
line whose `translation`, `translations`, `romanization` or `timedRomanization` changed is rebuilt
|
|
105
|
-
bare, ready to be decorated again. To drop one a consumer no longer wants, such as a translation
|
|
106
|
-
the listener turned off, call `removeTranslation` or `removeRomanization` (see Entry points).
|
|
107
|
-
- A swap that lands while the lines are still scrolling to the last line leaves the kept lines to
|
|
108
|
-
finish that scroll, and the new ones start at rest.
|
|
109
|
-
|
|
110
|
-
Some changes rebuild the whole view: another `songKey` or none, a different mount, a theme setting
|
|
111
|
-
that changes how lines are built, lyrics that gain or lose their timing, and anything written after
|
|
112
|
-
`clear()` or an empty array. A line whose times you changed after it was built, such as with an
|
|
113
|
-
offset you apply after every build, is built again from the lyrics rather than kept, so the offset is
|
|
114
|
-
not applied twice. A line built in place of another only takes over its place and sung state when
|
|
115
|
-
the two are sung over the same time.
|
|
116
|
-
|
|
117
|
-
## Properties
|
|
118
|
-
|
|
119
|
-
Every one of these may be written before the element is in a document. The renderer is built when it
|
|
120
|
-
connects, and everything it was handed by then is applied at once.
|
|
121
|
-
|
|
122
|
-
| Property | Attribute | Type | Default | Description |
|
|
123
|
-
| --------------- | -------------- | ----------------------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
|
|
124
|
-
| `lyrics` | | `Lyric[] \| null` | `null` | The song. Null means it was never given one, and an empty array clears the view, so there is a way to say both. |
|
|
125
|
-
| `lyricsOptions` | | `{ loaderVisible?, noLyrics?, language?, songwriters? }` | `{}` | How the lines are built. `noLyrics` marks a message as a placeholder rather than a song, which keeps passive scrolling from drifting it. `songwriters` closes the view with a credits line. |
|
|
126
|
-
| `source` | `source` | `string \| HTMLMediaElement \| null` | `null` | A selector or the media element itself. See Following a media element. |
|
|
127
|
-
| `mediaElement` | | `HTMLMediaElement \| null` (get) | `null` | What `source` resolved to. Null while disconnected, and null for a selector that missed. |
|
|
128
|
-
| `currentTime` | `current-time` | `number` | `0` | Playback position in **seconds**. Writing it renders the view again, so whoever holds the clock drives the lyrics by writing this. |
|
|
129
|
-
| `playing` | `playing` | `boolean` | `false` | A paused view animates differently from a playing one. |
|
|
130
|
-
| `tickOptions` | | `ElementTickOptions` | `{}` | The rest of a tick: four offsets taken off the clock before it is matched, whether passive scrolling is on, when the clock was sampled, and the rate the song is playing at. |
|
|
131
|
-
| `theme` | `theme` | `string` | `""` | A compiled stylesheet. See Theming. |
|
|
132
|
-
| `host` | | `Partial<LyricsRendererHost>` | `{}` | Overrides for what the renderer asks of its surroundings. Every member has a default. Writing it while connected rebuilds the view. |
|
|
133
|
-
| `layout` | `layout` | `"scroll" \| "stage"` | `"scroll"` | `stage` shows only the lines being sung. See Entry points. Writing it while connected rebuilds the view. |
|
|
134
|
-
| `renderer` | | `LyricsRenderer \| null` (get) | `null` | The renderer underneath, for the day the tag runs out. A different one after every reconnection. |
|
|
135
|
-
| `status` | | `ElementStatus` (get) | `"idle"` | `idle`, `rendering`, `theme-conflict`, `unsynced-on-stage` or `no-browsing-context`. |
|
|
136
|
-
|
|
137
|
-
`tickOptions` and `lyricsOptions` are stored on write and read by the next tick or the next build, so
|
|
138
|
-
writing options and the clock on the same frame renders once.
|
|
139
|
-
|
|
140
|
-
## Attributes
|
|
141
|
-
|
|
142
|
-
An attribute writes its property, and a property never writes back. Reflecting `current-time` would
|
|
143
|
-
put the playback clock into the DOM sixty times a second, and one attribute reflecting while the rest
|
|
144
|
-
do not is worse than none of them doing it.
|
|
145
|
-
|
|
146
|
-
| Attribute | Writes | Notes |
|
|
147
|
-
| -------------- | ------------- | -------------------------------------------------------------------------------------------------- |
|
|
148
|
-
| `source` | `source` | The selector form only. Another selector moves the binding, and removing it unbinds. |
|
|
149
|
-
| `theme` | `theme` | A whole stylesheet in an attribute value. It works, but nobody would ship a theme this way. |
|
|
150
|
-
| `layout` | `layout` | `stage` puts the view on a stage. Any other value, or no attribute, scrolls. |
|
|
151
|
-
| `current-time` | `currentTime` | Seconds. A value that does not parse as a number is ignored rather than read as zero. |
|
|
152
|
-
| `playing` | `playing` | An ordinary boolean attribute: its presence is what counts, so `playing="false"` is playing. |
|
|
153
|
-
|
|
154
|
-
## Events
|
|
155
|
-
|
|
156
|
-
All five bubble and are composed, so an element you put inside your own shadow root still reaches
|
|
157
|
-
your listener.
|
|
158
|
-
|
|
159
|
-
| Event | Detail | When |
|
|
160
|
-
| ------------------------ | ------------------------- | --------------------------------------------------------------------------------- |
|
|
161
|
-
| `braccato:lyrics-loaded` | `{ lineCount, syncType }` | Lyrics were applied, including an empty array. A theme change that rebuilds the lines reports itself the same way. |
|
|
162
|
-
| `braccato:line-click` | `{ timeS }` | A line was clicked. The seek has already reached the bound media element by the time you hear about it. |
|
|
163
|
-
| `braccato:scroll-state` | `{ userScrolling }` | Autoscroll stopped following the song, or started again. |
|
|
164
|
-
| `braccato:stage-layout` | `{ box }` | Stage layout only. The box around the sung lines in the container's coordinates, or null when nothing sung is on stage. |
|
|
165
|
-
| `braccato:error` | `{ phase, error }` | Connecting, resolving a source, or applying lyrics or a theme went wrong, or a stage cannot show what it was given. `phase` is `connect`, `conflict`, `layout`, `source`, `lyrics` or `theme`. |
|
|
166
|
-
|
|
167
|
-
Errors are dispatched a microtask after they happen rather than where they happen, which is what
|
|
168
|
-
makes them receivable at all: `connectedCallback` runs before any listener a page could have added.
|
|
169
|
-
A listener added later than that still misses them, so `status` answers the same question and needs
|
|
170
|
-
no listener. Nothing thrown by a tick lands here, because sixty error events a second would bury the
|
|
171
|
-
one that mattered.
|
|
172
|
-
|
|
173
|
-
There is no `braccato:word-click`. The renderer tells its host `seek(timeS)` and nothing else, so the
|
|
174
|
-
element cannot tell a word seek from a line seek without re-deriving the click branch off the DOM.
|
|
175
|
-
The DOM is light and the class names are published, so listen for `click` on the element and read
|
|
176
|
-
`.blyrics--word` yourself.
|
|
85
|
+
Anything the host puts in the container stays ordinary, visible DOM (a footer, buttons), and so does
|
|
86
|
+
anything it puts in the credits (a source card), which moves with the credits on the stage. The
|
|
87
|
+
lines' text is transparent rather than hidden, so it can be selected and copied.
|
|
177
88
|
|
|
178
89
|
## Theming
|
|
179
90
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
.blyrics-container {
|
|
190
|
-
--blyrics-font-size: 3.5rem;
|
|
191
|
-
--blyrics-lyric-active-color: white;
|
|
192
|
-
--blyrics-lyric-inactive-color: rgb(255 255 255 / 0.25);
|
|
193
|
-
}
|
|
194
|
-
`;
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
Settings are read from comments only. Everything else is CSS the browser is going to read, and a
|
|
198
|
-
stylesheet must not be able to configure the module by accident. An empty theme puts every setting
|
|
199
|
-
back to its default. The stylesheet itself goes into the document head under the
|
|
200
|
-
`blyrics-custom-style` id.
|
|
201
|
-
|
|
202
|
-
A comment setting holds one value for every view in the bundle. The target scroll position can
|
|
203
|
-
also be set per view: when `--blyrics-target-scroll-pos-ratio` resolves on `.blyrics-container`, it
|
|
204
|
-
wins over `blyrics-target-scroll-pos-ratio`, so an ordinary selector scopes it to one view. It takes
|
|
205
|
-
a unitless number, clamped to `0` to `1`; anything else falls back to the comment setting. Like the
|
|
206
|
-
other custom properties the module reads, it is read once per theme, so the selector has to match
|
|
207
|
-
before the theme is applied.
|
|
208
|
-
|
|
209
|
-
```css
|
|
210
|
-
/* blyrics-target-scroll-pos-ratio = 0.5; */
|
|
211
|
-
.pip-view .blyrics-container {
|
|
212
|
-
--blyrics-target-scroll-pos-ratio: 0.37;
|
|
213
|
-
}
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
A view whose edges are hidden, for example faded out by a mask, declares that with `scroll-padding`
|
|
217
|
-
on its scroll element. Active lines are then kept inside the band between the two insets instead of
|
|
218
|
-
the full viewport. When several active lines do not fit, the latest line still being sung keeps its
|
|
219
|
-
top in view, and an earlier line's tail or the next line's lookahead gives way. Give each inset as a
|
|
220
|
-
length or a percentage of the viewport height; a `calc()` that mixes the two is ignored, as are
|
|
221
|
-
insets that leave no band. The insets are measured on resize and relayout, not every tick.
|
|
222
|
-
|
|
223
|
-
```css
|
|
224
|
-
.pip-view .scroller {
|
|
225
|
-
scroll-padding-block: 12% 16%;
|
|
226
|
-
}
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
### Autoscroll grouping and animation
|
|
230
|
-
|
|
231
|
-
`blyrics-early-scroll-consider-s` is a comment setting with an independent default of `0.54`
|
|
232
|
-
seconds. When a lyric reaches its scroll time, the renderer includes nearby upcoming lines in
|
|
233
|
-
its target calculation. Entering the lookahead window alone does not scroll. A line already
|
|
234
|
-
included in a committed scroll does not scroll again when its own time arrives. Seeking,
|
|
235
|
-
resuming autoscroll and relayout can still reposition the view.
|
|
236
|
-
|
|
237
|
-
```css
|
|
238
|
-
/* blyrics-early-scroll-consider-s = 0.54; */
|
|
239
|
-
/* blyrics-line-scroll-duration = 750ms; */
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
New groups scroll immediately. A line still moving from an earlier scroll keeps that motion: what
|
|
243
|
-
is left of it and the new scroll are added together into one `translate` animation, so their
|
|
244
|
-
durations do not block the next scroll and the browser can run the motion off the main thread.
|
|
245
|
-
Decoration slides join the same animation. While a line scrolls or slides, that animation replaces
|
|
246
|
-
any `translate` a theme sets on the line itself. The line-scroll duration knobs control visual
|
|
247
|
-
motion. The legacy `--blyrics-lyric-scroll-duration` variable and its
|
|
248
|
-
`--blyrics-lyric-transition-duration` alias have been removed, along with the container transform
|
|
249
|
-
transition. `blyrics-queue-scroll-ms` is ignored, and no timing equation needs balancing.
|
|
250
|
-
Missing or invalid line durations fall back to an internal `750ms` duration; nonpositive resolved
|
|
251
|
-
durations also use that fallback. Themes should set the line-scroll duration knobs directly
|
|
252
|
-
instead of referencing the removed variables.
|
|
253
|
-
|
|
254
|
-
Themes that previously relied on duration-derived lookahead should set their preferred
|
|
255
|
-
`blyrics-early-scroll-consider-s` explicitly. The default remains close to the former default
|
|
256
|
-
of approximately `0.54s`, but a custom animation duration no longer changes grouping.
|
|
257
|
-
|
|
258
|
-
There is no `longWordThreshold`, `lineSyncedDelay` or `disableRichsync` property. Those are theme
|
|
259
|
-
settings (`blyrics-long-word-threshold`, `blyrics-line-synced-animation-delay`,
|
|
260
|
-
`blyrics-disable-richsync`), read from the stylesheet you already hand over. A theme that set one
|
|
261
|
-
while a property said otherwise would leave the module with two answers and no rule for picking.
|
|
262
|
-
|
|
263
|
-
`parseThemeConfig` is published on `@braccato/core/themeSettings` for reading the settings out of a
|
|
264
|
-
stylesheet somewhere no renderer is running.
|
|
265
|
-
|
|
266
|
-
### Custom properties
|
|
267
|
-
|
|
268
|
-
The ones a theme reaches for first. `variables.css` declares the rest.
|
|
269
|
-
|
|
270
|
-
```css
|
|
271
|
-
.blyrics-container {
|
|
272
|
-
--blyrics-font-family: system-ui, sans-serif;
|
|
273
|
-
--blyrics-font-size: 3rem;
|
|
274
|
-
--blyrics-line-height: 1.333;
|
|
275
|
-
--blyrics-padding: 2rem;
|
|
276
|
-
--blyrics-lyric-active-color: white;
|
|
277
|
-
--blyrics-lyric-inactive-color: rgb(255 255 255 / 0.3);
|
|
278
|
-
--blyrics-glow-color: rgb(255 255 255 / 0.5);
|
|
279
|
-
}
|
|
280
|
-
```
|
|
91
|
+
Themes are not compatible with `@braccato/core` themes, because there are no lines in the DOM to
|
|
92
|
+
style. What carries over is everything a theme says through custom properties: colours, fonts and
|
|
93
|
+
sizes, padding, line height, letter spacing, swipe stops, glow radius and timing, wobble and
|
|
94
|
+
letter-wave transforms, scale, fade and scroll timing, credits, and the `--blyrics-animate-*` switches.
|
|
95
|
+
The comment settings work as before, including `blyrics-image-highlights = true;`, which fills the
|
|
96
|
+
instrumental note with `--blyrics-highlight-image`. Selectors still scope these properties, so a rule like
|
|
97
|
+
`.blyrics-word-highlight:not([data-long-word]) { --blyrics-glow-color: transparent; }` still makes only
|
|
98
|
+
held words glow.
|
|
281
99
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
`data-long-word`, which the module sets on any part held past `blyrics-long-word-threshold`.
|
|
286
|
-
|
|
287
|
-
The sweep and the glow run off the main thread. Each word, or each letter of a word that wraps, holds
|
|
288
|
-
a fixed soft-edged mask over a copy of its text in `--blyrics-lyric-active-color`, and the two slide
|
|
289
|
-
against each other so only the edge moves. A word in a joining script, or one mixing directions, is
|
|
290
|
-
drawn from copies of its line placed over each box it breaks into. The swipe stops
|
|
291
|
-
(`--blyrics-highlight-swipe-*`) set where the edge starts and ends, and the soft edge is as wide as the
|
|
292
|
-
swipe's last start and end stops are apart. The glow is two copies of the word inside the same mask,
|
|
293
|
-
with a `text-shadow` of `--blyrics-highlight-glow-radius-from` and a tighter one in
|
|
294
|
-
`--blyrics-glow-color`, crossfaded as it fades; it starts where the edge reaches the word or letter,
|
|
295
|
-
and the `--blyrics-highlight-glow-filter-*` overrides are not read.
|
|
296
|
-
`--lyric-transition-amount-*` no longer animate, so a theme cannot draw its own sweep from them.
|
|
297
|
-
|
|
298
|
-
A word longer than twice `blyrics-long-word-wrap-threshold` (default `10` characters) gets
|
|
299
|
-
`blyrics-word-group-long` on its group. Its syllables become `display: inline` and take `<wbr>`
|
|
300
|
-
break points, so a word like `Oh-oh-oh-oh-oh-oh-oh-oh` wraps instead of overflowing. Browsers ignore
|
|
301
|
-
`transform` on inline boxes, so a per-syllable animation and the word wobble both stop on those
|
|
302
|
-
words. Raising `blyrics-long-word-wrap-threshold` (say to `1000`) brings both back, but then a word
|
|
303
|
-
wider than the view overflows instead of wrapping. To keep the wrap, set
|
|
304
|
-
`.blyrics-word-group-long .blyrics--word:not(.blyrics-bidi-sensitive *)` back to `display: inline-block`.
|
|
305
|
-
The syllables still wrap between each other and per-syllable animations return. The wobble stays off,
|
|
306
|
-
since it moves the group and the group is still inline.
|
|
307
|
-
|
|
308
|
-
The instrumental ripple is the one place a property carries geometry rather than a value.
|
|
309
|
-
`--blyrics-instrumental-wave-path-high` and `--blyrics-instrumental-wave-path-low` are the two shapes
|
|
310
|
-
it morphs between, each a `path()`, and a theme redrawing them is how the wave changes amplitude or
|
|
311
|
-
frequency. Both must use the same commands in the same order with the same number of arguments: a
|
|
312
|
-
browser only interpolates two paths smoothly when their command sequences match, and a mismatched
|
|
313
|
-
pair snaps at the halfway point instead of flowing.
|
|
314
|
-
|
|
315
|
-
### Credits
|
|
316
|
-
|
|
317
|
-
Given `songwriters`, the view ends with `A, B & C` after the last line. It sits dim through the song,
|
|
318
|
-
and once the last line has ended it brightens and takes the scroll focus, the way Apple Music closes a
|
|
319
|
-
song. The scroll never carries the last sung line out of view to do it, so a long list of writers
|
|
320
|
-
settles below that line rather than centred. Seeking back hands the focus to the lines again. Unsynced lyrics have no end, so their credits
|
|
321
|
-
show at full strength from the start.
|
|
322
|
-
|
|
323
|
-
| Custom property | Default | What it sets |
|
|
324
|
-
| ------------------------------------ | ---------------- | ---------------------------------------------------- |
|
|
325
|
-
| `--blyrics-credits-label` | `"Written by"` | The words before the names. Localise it here. |
|
|
326
|
-
| `--blyrics-credits-font-size` | `max(0.4em, 12px)` | The size of the whole line. |
|
|
327
|
-
| `--blyrics-credits-opacity` | `0.2` | While the song plays. |
|
|
328
|
-
| `--blyrics-credits-focused-opacity` | `0.85` | Once the song has ended. |
|
|
329
|
-
|
|
330
|
-
The credits are a `p`, not a `div`, so rules a theme writes for the lines as `.blyrics-container > div`
|
|
331
|
-
(hover scaling, per-line opacity and blur) never reach them. Style them through `.blyrics-credits`.
|
|
332
|
-
|
|
333
|
-
The container carries `data-credits-focused` while the credits hold the focus. To hide them, a
|
|
334
|
-
stylesheet can set `.blyrics-credits { display: none; }`, which the scroll then ignores, or a theme can
|
|
335
|
-
declare `/* blyrics-hide-credits = true; */` so they are never built.
|
|
336
|
-
|
|
337
|
-
### Letter wave (experimental)
|
|
338
|
-
|
|
339
|
-
On by default; a theme opts out with `/* blyrics-letter-wave = false; */`. It splits every word into
|
|
340
|
-
per-letter spans and, as the word is sung, floats each letter up and eases it most of the way back on
|
|
341
|
-
a small stagger, so a wave travels through the word. It layers on top of the word wobble rather than
|
|
342
|
-
replacing it: the word keeps whatever `--blyrics-word-wobble-*` does and the letters ride on top, so
|
|
343
|
-
it composes with the default `scaleX` pop and reduces to just the letters when a theme sets its wobble
|
|
344
|
-
to identity. It follows `--blyrics-animate-word-wobble`, so reduced motion turns it off with the rest.
|
|
345
|
-
|
|
346
|
-
A word held past `blyrics-long-word-threshold` (the same `data-long-word` the glow keys off) also
|
|
347
|
-
swells each letter with a transient scale at the crest.
|
|
100
|
+
Rules that style the lines directly (filters, per-line opacity, `display: none`, keyframes) have no
|
|
101
|
+
effect. Blurring and fading lines by how far they are from the one being sung, which themes like
|
|
102
|
+
Sustain do with chains of sibling selectors, is instead two lists:
|
|
348
103
|
|
|
349
104
|
```css
|
|
350
105
|
.blyrics-container {
|
|
351
|
-
|
|
352
|
-
--blyrics-
|
|
353
|
-
--blyrics-
|
|
354
|
-
--blyrics-letter-wave-duration: 0.9s;
|
|
355
|
-
--blyrics-letter-wave-rise-easing: ease-in-out;
|
|
356
|
-
--blyrics-letter-wave-fall-easing: ease-out;
|
|
106
|
+
/* opacity and blur for the sung line, then 1, 2, 3… lines away */
|
|
107
|
+
--blyrics-line-opacity-after: 1 0.88 0.72 0.52 0.28 0;
|
|
108
|
+
--blyrics-line-blur-after: 0 1.125px 2.5px 4.125px 6px;
|
|
357
109
|
}
|
|
358
110
|
```
|
|
359
111
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
112
|
+
`--blyrics-line-opacity` and `--blyrics-line-blur` apply to both sides, and `-before` / `-after`
|
|
113
|
+
replace one side: lines already sung, or still to come. Values between whole lines are interpolated,
|
|
114
|
+
the last value holds past the end, and blurs are the standard deviation, as CSS `blur()` takes it.
|
|
115
|
+
The distance follows the scroll smoothly, and the effects ease off while the reader scrolls.
|
|
116
|
+
|
|
117
|
+
## How it works
|
|
118
|
+
|
|
119
|
+
The lines are laid out by the browser in an invisible copy of `@braccato/core`'s markup, inside the
|
|
120
|
+
element. That gives the same wrapping, bidi reordering and font fallback, and it stays in the page
|
|
121
|
+
for scrolling, clicks and screen readers. It is measured once per layout.
|
|
122
|
+
|
|
123
|
+
Each line is then drawn once with the browser's own text engine (Canvas 2D), as coverage the shader
|
|
124
|
+
tints, and copied into the line pages: one array texture with mipmaps, each layer packed with lines
|
|
125
|
+
on shelves.
|
|
126
|
+
|
|
127
|
+
Every frame, Rust works out each visible line's state from the lyric clock: its scale, how far the
|
|
128
|
+
sweep has got, the glow's radius, the wobble and the letter wave. It writes one quad per letter or
|
|
129
|
+
word, placed on its line's spot in the pages, and draws them all with one instanced call. The
|
|
130
|
+
fragment shader mixes the inactive and lit colours across the sweep's edge.
|
|
131
|
+
|
|
132
|
+
Glows, the instrumental note's glow and blurred lines are real Gaussian blurs of what is lit (the
|
|
133
|
+
letters behind the sweep, the filled part of the note), made on the GPU each frame. What is to be
|
|
134
|
+
blurred is drawn with the same quads into an atlas, scaled down so its standard deviation spans 1.5
|
|
135
|
+
to 3 texels and reading the matching mip level, blurred across and then down with an instanced pass
|
|
136
|
+
each, and drawn back onto the canvas as one quad among the rest. Six glowing words cost about 0.1 ms
|
|
137
|
+
of GPU time. However many lines are on screen, a frame is four draws at most: the blur jobs, the two
|
|
138
|
+
blur passes, and the canvas.
|
|
139
|
+
|
|
140
|
+
The shader is written in WGSL. `build.rs` parses and validates it with naga, a build-dependency
|
|
141
|
+
only, and writes GLSL ES 3.00 for WebGL2, so a shader mistake fails `cargo build` with naga's report
|
|
142
|
+
rather than showing up as a compile log in the browser. naga renames the uniform buffer and the
|
|
143
|
+
textures, and the build script writes those names out as Rust constants for `gpu.rs` to bind by.
|
|
144
|
+
The two quad draws each have an instance buffer whose attributes are set up once, so a frame only
|
|
145
|
+
uploads quads and draws. Texture sizes come from `textureDimensions` in the shader, and the one
|
|
146
|
+
uniform buffer is written when the canvas resizes.
|
|
147
|
+
|
|
148
|
+
| File | What it holds |
|
|
149
|
+
| ---- | ------------- |
|
|
150
|
+
| `src/model.rs` | The `Lyric` input shapes |
|
|
151
|
+
| `src/song.rs` | Lyrics into lines, rows, word groups and timed parts (core's rules) |
|
|
152
|
+
| `src/layout.rs`, `src/style.rs` | The hidden DOM and its measurement into drawable slots |
|
|
153
|
+
| `src/raster.rs` | Drawing a line's slots onto a canvas, for the line pages |
|
|
154
|
+
| `src/paint.rs` | What every line looks like at a given moment, as quads |
|
|
155
|
+
| `src/scroll.rs` | Autoscroll targets and the per-line glide |
|
|
156
|
+
| `src/stage.rs` | The stage layout's roles and motion |
|
|
157
|
+
| `src/clock.rs` | Smoothing the media clock between reports |
|
|
158
|
+
| `src/shaders/*.wgsl` | The quad and blur shaders, in WGSL |
|
|
159
|
+
| `build.rs` | Validates the WGSL with naga and writes GLSL ES 3.00 and its resource names at build time |
|
|
160
|
+
| `src/gpu.rs` | WebGL2: the programs, instance buffers, the line pages and their allocator, the blur atlas |
|
|
161
|
+
| `src/renderer.rs` | The view: state, a frame, events |
|
|
162
|
+
| `src/profile.rs` | Per-frame timings by phase, for `takeProfile()` and `pnpm bench` |
|
|
163
|
+
| `src/lib.rs` | The `Renderer` exported to JavaScript, and its event wiring |
|
|
164
|
+
| `js/element.js` | The custom element |
|
|
165
|
+
|
|
166
|
+
## Cost
|
|
167
|
+
|
|
168
|
+
Measured with `pnpm bench` on an Intel Arc iGPU (Meteor Lake) in Chrome: a 1632 × 1000 canvas at
|
|
169
|
+
device pixel ratio 1.33, 240 Hz, the song playing. Medians of three 15 s runs:
|
|
170
|
+
|
|
171
|
+
| Demo song | Renderer CPU per frame, mean / p99 | GPU per frame |
|
|
172
|
+
| --------- | ---------------------------------- | ------------- |
|
|
173
|
+
| Kettle | 0.063 / 0.125 ms | 0.13 ms |
|
|
174
|
+
| Kettle, Sustain theme (lines blurred by distance) | 0.075 / 0.140 ms | 0.41 ms |
|
|
175
|
+
| The Steps (long glowing words) | 0.065 / 0.145 ms | 0.18 ms |
|
|
176
|
+
| Kettle, stage layout | 0.049 / 0.100 ms | 0.07 ms |
|
|
177
|
+
|
|
178
|
+
At 240 Hz that is about 1.5% of a core and 3 to 10% of the GPU. A line costs about 1 ms more on the
|
|
179
|
+
frame it is rasterized, as it scrolls into view. In Better Lyrics on YouTube Music, counting the whole
|
|
180
|
+
page with the video playing, the main thread was 13 to 14% busy against 40 to 45% for
|
|
181
|
+
`@braccato/core` 1.21.6, Chrome's renderer process used 32 to 34% of a core against 70 to 77%, and
|
|
182
|
+
its GPU process 38 to 39% against 51%.
|
|
183
|
+
|
|
184
|
+
### Benchmarks and profiles
|
|
185
|
+
|
|
186
|
+
`pnpm bench` (`tooling/bench.ts`) plays demo songs on the compare page in a real Chrome, one
|
|
187
|
+
renderer at a time, and reports for each scenario the median and range over several runs:
|
|
188
|
+
|
|
189
|
+
- each frame's CPU time in the renderer, as percentiles and split by phase (preparing, updating
|
|
190
|
+
clock and scroll, rasterizing, painting quads, moving riders, submitting to the GPU), and its GPU
|
|
191
|
+
time;
|
|
192
|
+
- the page's main thread (busy, script, layout, style) and Chrome's renderer and GPU processes, in
|
|
193
|
+
milliseconds per second, which compare this renderer with `@braccato/core` (`--renderers rs,dom`).
|
|
194
|
+
|
|
195
|
+
`--save before.json`, then `--baseline before.json` after a change, prints each number's change and
|
|
196
|
+
marks the ones within the runs' noise. `--profile` adds a run per scenario under Chrome's sampling
|
|
197
|
+
profiler, writes a `.cpuprofile` for DevTools' Performance panel to `.bench/`, and prints the
|
|
198
|
+
functions with the most time; build with `pnpm -C packages/core-rs build:profiling` first so Rust
|
|
199
|
+
functions have names. `pnpm bench --help` lists the options.
|
|
200
|
+
|
|
201
|
+
The numbers come from `renderer.setProfiling(true)`, after which `renderer.takeProfile()` returns
|
|
202
|
+
every frame's phase times and `renderer.stats()` running averages.
|
|
203
|
+
|
|
204
|
+
## Build and test
|
|
205
|
+
|
|
206
|
+
Needs a Rust toolchain with the `wasm32-unknown-unknown` target, clippy and rustfmt, and `wasm-pack`.
|
|
207
|
+
Without them every step prints a notice and skips, so `pnpm build`, `pnpm lint` and `pnpm test` still
|
|
208
|
+
pass for the rest of the workspace. CI and the release workflow install them and set
|
|
209
|
+
`BRACCATO_REQUIRE_RUST`, which makes a missing toolchain fail instead, so the package is never
|
|
210
|
+
published without its wasm.
|
|
211
|
+
|
|
212
|
+
The wasm is built with WebAssembly SIMD (`.cargo/config.toml`), which Chrome 91, Firefox 89 and
|
|
213
|
+
Safari 16.4 run.
|
|
363
214
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
layers of a word, the base `.blyrics--word` and its highlight overlay, so a theme can key either. A
|
|
370
|
-
word stays `active` for exactly as long as it is sung, so a transition on the flip is the whole of a
|
|
371
|
-
per-word karaoke effect, the one ramansg/Max-Performance built before the swept overlay.
|
|
372
|
-
|
|
373
|
-
```css
|
|
374
|
-
.blyrics--word {
|
|
375
|
-
color: var(--blyrics-lyric-inactive-color);
|
|
376
|
-
transition: color 180ms ease;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
.blyrics--word[data-word-state="active"],
|
|
380
|
-
.blyrics--word[data-word-state="past"] {
|
|
381
|
-
color: var(--blyrics-lyric-active-color);
|
|
382
|
-
}
|
|
383
|
-
```
|
|
384
|
-
|
|
385
|
-
It is written only when a word's state changes rather than every frame, so selecting on it costs a
|
|
386
|
-
theme nothing per tick. A line synced word, which has no duration of its own, is `active` from its
|
|
387
|
-
start until the next word begins.
|
|
388
|
-
|
|
389
|
-
### Browser engine
|
|
390
|
-
|
|
391
|
-
On Firefox the container carries `data-browser-engine="gecko"`, and on WebKit (Safari and the other
|
|
392
|
-
WebKit browsers) it carries `data-browser-engine="webkit"`. The renderer's engine-only CSS keys on it,
|
|
393
|
-
and a theme can do the same to target an engine without sniffing the user agent. Each is detected from
|
|
394
|
-
a feature only that engine ships, so forks carry it too. Script reads the same answers from `isGecko()`
|
|
395
|
-
and `isWebKit()` in `@braccato/core/util`; the renderer's Gecko-only script checks `isGecko()` once on
|
|
396
|
-
load.
|
|
397
|
-
|
|
398
|
-
```css
|
|
399
|
-
.blyrics-container[data-browser-engine="gecko"] .blyrics--line {
|
|
400
|
-
/* Firefox only */
|
|
401
|
-
}
|
|
402
|
-
```
|
|
403
|
-
|
|
404
|
-
WebKit puts the baseline of an inline-block that clips its overflow at the box's bottom edge, where
|
|
405
|
-
Blink and Gecko keep the baseline of its text. Each sweep cell clips its mask boxes, so on WebKit every
|
|
406
|
-
wrapped row of lit text sat lower than the row it covers. There the cells use paint containment instead,
|
|
407
|
-
which clips the same box and keeps the text's baseline.
|
|
408
|
-
|
|
409
|
-
The letter wave uses both. WebRender rounds text moved by a 2D translation to whole device
|
|
410
|
-
pixels, and it only redraws a cached tile once something on it has moved a quarter of a pixel, so the
|
|
411
|
-
wave stepped on Firefox. Lettered words on animating lines get a distant `perspective` there, and each
|
|
412
|
-
base letter in the wave runs an invisible additive opacity ripple that changes every frame and forces the
|
|
413
|
-
redraw. Chromium gets neither.
|
|
414
|
-
|
|
415
|
-
The playback clock checks `isGecko()`. Firefox advances a media element's `currentTime` about every 40ms, so
|
|
416
|
-
each read lags the true time by a different amount and never leads it. The renderer took each new lag
|
|
417
|
-
for drift and reseeked the line, so the highlight swipe jumped back mid-word. On Firefox the tick keeps
|
|
418
|
-
its own clock instead: a read ahead of it is taken, a read behind it pulls it back by a tenth per
|
|
419
|
-
second, and a read more than 60ms off, about one Firefox step plus margin, counts as a seek.
|
|
420
|
-
|
|
421
|
-
The instrumental fill uses the attribute. Gecko resolves a `fill-box` transform origin wrongly for a shape
|
|
422
|
-
inside a `clipPath`, so the flattening wave crest came loose from the rising fill. On Firefox the wave
|
|
423
|
-
scales from its floor in user units instead, which Chromium already renders identically.
|
|
424
|
-
|
|
425
|
-
### Class names
|
|
426
|
-
|
|
427
|
-
These are published API rather than implementation. Renaming one costs a migration rather than a
|
|
428
|
-
refactor. Import them from `@braccato/core/constants` instead of typing them out.
|
|
429
|
-
|
|
430
|
-
| Constant | Class | What it is |
|
|
431
|
-
| ------------------------- | --------------------------- | ----------------------------------------------------------- |
|
|
432
|
-
| `LYRICS_CLASS` | `blyrics-container` | The view. One per renderer. |
|
|
433
|
-
| `LINE_CLASS` | `blyrics--line` | One line, carrying its own `dir="auto"`. |
|
|
434
|
-
| `CURRENT_LYRICS_CLASS` | `blyrics--active` | The line the song is on right now. |
|
|
435
|
-
| `WORD_CLASS` | `blyrics--word` | One word, and the unit the sweep animates. |
|
|
436
|
-
| `BACKGROUND_LYRIC_CLASS` | `blyrics-background-lyric` | A background vocal, sung over the line it answers. |
|
|
437
|
-
| `USER_SCROLLING_CLASS` | `blyrics-user-scrolling` | Set while a reader has scrolled away and autoscroll waits. |
|
|
438
|
-
| `TRANSLATED_LYRICS_CLASS` | `blyrics--translated` | A translation hung off a line that was already built. |
|
|
439
|
-
| `LONG_WORD_GROUP_CLASS` | `blyrics-word-group-long` | A word longer than twice `blyrics-long-word-wrap-threshold`. Its syllables go inline so it can wrap, and browsers ignore `transform` on inline boxes. |
|
|
440
|
-
| `EXPLICIT_WORD_CLASS` | `blyrics-explicit` | A word the lyrics flag as explicit. Unstyled unless a theme styles it. |
|
|
441
|
-
| `CREDITS_CLASS` | `blyrics-credits` | The songwriter credits after the last line. |
|
|
442
|
-
| `CUSTOM_THEME_STYLE_ID` | `blyrics-custom-style` | The id of the `<style>` the theme lands in. |
|
|
443
|
-
|
|
444
|
-
## Stylesheets
|
|
445
|
-
|
|
446
|
-
Four sheets ship with the package, and loading them is yours, the way any package's CSS is. Leave
|
|
447
|
-
them out and you get lines that are in the document and unstyled, rather than lines that are missing.
|
|
448
|
-
|
|
449
|
-
| File | What it carries |
|
|
450
|
-
| ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
|
451
|
-
| `@braccato/core/styles/variables.css` | Every `--blyrics-*` default. It goes first, because the others read from it. |
|
|
452
|
-
| `@braccato/core/styles/lyrics.css` | The container, the lines, the words and the sweep. |
|
|
453
|
-
| `@braccato/core/styles/instrumental.css` | The waveform that fills a bar nobody sings over, and the animation that walks it. |
|
|
454
|
-
| `@braccato/core/styles/stage.css` | Placement for the stage layout, and nothing else. Only a renderer created with `layout: "stage"` needs it. |
|
|
455
|
-
|
|
456
|
-
One thing they do not do for you. The module measures the room the first and last lines need to reach
|
|
457
|
-
the view's target scroll position and writes it on the root as `--blyrics-padding-top` and
|
|
458
|
-
`--blyrics-padding-bottom`, but `lyrics.css` only spends the bottom one. Supply the top rule:
|
|
459
|
-
|
|
460
|
-
```css
|
|
461
|
-
.blyrics-container {
|
|
462
|
-
padding-top: var(--blyrics-padding-top, 2rem);
|
|
463
|
-
}
|
|
464
|
-
```
|
|
465
|
-
|
|
466
|
-
## Light DOM, not shadow DOM
|
|
467
|
-
|
|
468
|
-
The element builds into itself. That is what lets a stylesheet at document level select the lines,
|
|
469
|
-
and what lets the package's own `@property` registrations apply to them, which they would not inside
|
|
470
|
-
a shadow root. The theme is adopted into the element's document rather than encapsulated, and the
|
|
471
|
-
package's stylesheets are yours to load for the same reason.
|
|
472
|
-
|
|
473
|
-
## Entry points
|
|
474
|
-
|
|
475
|
-
`@braccato/core` is the facade and registers nothing. `createLyricsRenderer(options)` returns one
|
|
476
|
-
`LyricsRenderer`: give it lyrics, tick it, and it owns the DOM it builds and every re-measurement
|
|
477
|
-
that DOM needs. `resetPlaybackClock`, `resumeAllAutoscroll`, `injectRomanization`,
|
|
478
|
-
`injectTranslation`, `removeRomanization` and `removeTranslation` are published beside it, for what
|
|
479
|
-
one instance cannot answer for on its own. Hang one onto a built line and call
|
|
480
|
-
`renderer.scheduleLyricPositionUpdate` the way you already would to catch the layout up: the hung line
|
|
481
|
-
floats into place while the lines around it slide to make room, rather than the rest of the lyric
|
|
482
|
-
jumping down. `--blyrics-animate-decoration-entry` set to `0` drops both for an instant insert, and
|
|
483
|
-
reduced-motion does the same. Taking one off with `removeTranslation(lineElement)` or
|
|
484
|
-
`removeRomanization(lineElement, line)` is the same: call `scheduleLyricPositionUpdate` after, and the
|
|
485
|
-
lines below slide up into the space.
|
|
486
|
-
|
|
487
|
-
`createLyricsRenderer({ layout: "stage" })` builds a view for subtitles over a video: one line at a
|
|
488
|
-
time, and only while it is being sung. It fills its nearest positioned ancestor. There is no scroll
|
|
489
|
-
element to hand it, no scroll padding written and no line culling. Unsynced lyrics show nothing, because
|
|
490
|
-
a stage places lines by their time and unsynced lines have none.
|
|
491
|
-
|
|
492
|
-
The engine owns where a line is and whether you can see it. It moves each line with a `translate` Web
|
|
493
|
-
Animation and fades it through `--blyrics-stage-opacity`, blurring it slightly on the way in and out so
|
|
494
|
-
one line reads as turning into the next. Each line carries `data-stage-role` (`current`, `previous`,
|
|
495
|
-
`queued` or `gone`) and the container carries `data-layout="stage"`. `stage.css` sets a stage line's
|
|
496
|
-
`opacity` from that property with `!important` inside a cascade layer, and keeps the line
|
|
497
|
-
`visibility: hidden` until it is marked `data-stage-visible`. Layered important declarations outrank
|
|
498
|
-
unlayered ones whatever their specificity, so a theme that forces `opacity: 1 !important` on active
|
|
499
|
-
lines, which some do, can neither reveal a queued line nor hold one that is leaving. In a duet (any line sung by `v2` or `v3`) the
|
|
500
|
-
container also gets `data-stage-duet` and each singer keeps to their side, mirrored for right-to-left
|
|
501
|
-
lines; lines for everyone stay centred.
|
|
502
|
-
|
|
503
|
-
Everything else is the theme's. Lines are styled exactly as they would be in a scrolling view.
|
|
504
|
-
`host.onStageLayout(box)` reports the box around the sung lines in the container's coordinates, or null
|
|
505
|
-
when nothing sung is on stage, so you can draw a backdrop outside the container the theme styles.
|
|
506
|
-
|
|
507
|
-
The element takes the same layout as `layout="stage"`, and reports the same box as
|
|
508
|
-
`braccato:stage-layout`. Give the element or its parent a size and a position, and load `stage.css`.
|
|
509
|
-
A stage that cannot show what it was given says so rather than staying blank: unsynced lyrics set
|
|
510
|
-
`status` to `unsynced-on-stage`, and both they and a missing `stage.css` dispatch `braccato:error`
|
|
511
|
-
with `phase: "layout"`. Each song is reported once, so a theme that rebuilds the lines stays quiet.
|
|
512
|
-
The stylesheet is checked a frame after the song is built, and only the event reports it.
|
|
513
|
-
|
|
514
|
-
```html
|
|
515
|
-
<div style="position: relative; aspect-ratio: 16 / 9">
|
|
516
|
-
<video id="clip" src="clip.mp4"></video>
|
|
517
|
-
<braccato-lyrics source="#clip" layout="stage"></braccato-lyrics>
|
|
518
|
-
</div>
|
|
215
|
+
```bash
|
|
216
|
+
pnpm -C packages/core-rs build # wasm-pack into pkg/
|
|
217
|
+
pnpm -C packages/core-rs build:profiling # the same, with function names for CPU profiles
|
|
218
|
+
pnpm -C packages/core-rs lint # cargo fmt --check, clippy with warnings as errors
|
|
219
|
+
pnpm -C packages/core-rs test # cargo test: song building, timing, scroll, stage, clock, easing, line pages
|
|
519
220
|
```
|
|
520
221
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
- `@braccato/core/constants` for the class names and element ids above
|
|
527
|
-
- `@braccato/core/text` for script detection: `testRtl`, `containsNonLatin`, `detectNonLatinLanguage`
|
|
528
|
-
- `@braccato/core/themeSettings` for `parseThemeConfig`
|
|
529
|
-
- `@braccato/core/util` for pure helpers such as `clamp` and `toMs`, and `isGecko` and `isWebKit` for engine-specific fixes
|
|
530
|
-
|
|
531
|
-
Two notes on the element entry point. A browser extension's isolated world has no custom element
|
|
532
|
-
registry, so `window.customElements` is null there and importing this file throws where it registers.
|
|
533
|
-
An extension that wants the tag has to run in the page's own world; one that stays isolated calls
|
|
534
|
-
`createLyricsRenderer` directly. And registration is silent about a name already taken, so two copies
|
|
535
|
-
of this package on one page means the first to load takes both names and `instanceof` against the
|
|
536
|
-
second copy's class is false for every element on the page. Load one copy.
|
|
537
|
-
|
|
538
|
-
## Following a media element
|
|
539
|
-
|
|
540
|
-
While a `source` is bound, the element drives itself. It reads `currentTime` and `paused` off the
|
|
541
|
-
media element on a `requestAnimationFrame` loop that runs only while the song plays, and a click on a
|
|
542
|
-
lyric line sets `currentTime` back on it. So `currentTime` and `playing` become outputs: a write to
|
|
543
|
-
either is dropped and the getter keeps reporting what the binding last read. Dropped rather than
|
|
544
|
-
reported, because a consumer who bound a source and left their own frame loop running would otherwise
|
|
545
|
-
be told about it sixty times a second. Unbind and the clock goes back to whoever asked for it.
|
|
546
|
-
|
|
547
|
-
The rate is read off the media element too, and passed on as `tickOptions.playbackRate`, so a song at
|
|
548
|
-
half or double speed animates at half or double speed rather than sweeping at 1x and being corrected
|
|
549
|
-
on the next tick. A consumer driving the clock itself sets that option instead. Only the animations
|
|
550
|
-
that follow the song are scaled: a line's exit, a word's fade and the scroll between lines keep the
|
|
551
|
-
timing the theme asked for at every rate.
|
|
552
|
-
|
|
553
|
-
A reading the media element has not refreshed yet is carried forward at the playback rate it was
|
|
554
|
-
taken at, capped at 100ms of frame time. That cap is what covers a stall: the view runs at most 100ms
|
|
555
|
-
past the last real reading and then waits with it. What it costs is a step backwards when the clock
|
|
556
|
-
moves again, scaled by the rate. 100ms at 1x, 400ms at 4x.
|
|
557
|
-
|
|
558
|
-
`play`, `pause`, `seeking`, `seeked` and `ratechange` are listened to. The frame loop covers the rest
|
|
559
|
-
by asking the media element whether its clock is still going rather than trusting that something said
|
|
560
|
-
so. One gap is worth knowing: `emptied` while already paused leaves no loop running to notice, so
|
|
561
|
-
swapping `audio.src` between songs without playing goes on reporting the old position until the next
|
|
562
|
-
`play`.
|
|
563
|
-
|
|
564
|
-
## One renderer per document
|
|
565
|
-
|
|
566
|
-
Two renderers in one document write over each other, so the module supports one. It is a constraint
|
|
567
|
-
rather than a setting, and it is stated rather than enforced: none of the points where two of them
|
|
568
|
-
collide is a crash. A stage renderer is the exception. It writes no scroll padding, so it can sit in
|
|
569
|
-
the same document as a scrolling one, with three rules: give both the same theme, tick both with the
|
|
570
|
-
same time and wall time in the same frame (whichever ticks first is the one that sees a seek), and if
|
|
571
|
-
the stage renderer applied the theme first, destroy it first, because its `<style>` element goes with it.
|
|
572
|
-
|
|
573
|
-
Two things are written per document and belong to whichever renderer wrote them last: the theme's
|
|
574
|
-
`<style>` element, and the scroll padding on the root. Two more are per bundle, because a settings
|
|
575
|
-
registry and the playback clock both live at module scope: one theme means one set of values for
|
|
576
|
-
every view in that bundle, and whichever view ticked last is the one whose clock the others replay.
|
|
577
|
-
|
|
578
|
-
None of that is a limit on how many elements you may have. Two views handed the **same** theme share
|
|
579
|
-
only the settings both of them asked for, and the renderer adopts an existing theme element rather
|
|
580
|
-
than adding a rival under the same id. The line is drawn at the disagreement: when an element applies
|
|
581
|
-
a theme another element in its document was not given, both dispatch `braccato:error` with
|
|
582
|
-
`phase: "conflict"` and both read `status === "theme-conflict"`. Neither stops rendering, because a
|
|
583
|
-
blank view with a reason is worse than a themed one with a warning.
|
|
584
|
-
|
|
585
|
-
## Docs and demo
|
|
586
|
-
|
|
587
|
-
Full documentation is at [braccato.boidu.dev](https://braccato.boidu.dev).
|
|
588
|
-
|
|
589
|
-
The demo page lives at [`demo/`](https://github.com/better-lyrics/braccato/tree/master/demo) in the
|
|
590
|
-
repository and runs against the emitted package, with a control for most of what is above. Clone the
|
|
591
|
-
repository and run `pnpm -C demo dev`, then open `http://localhost:5173/`.
|
|
592
|
-
|
|
593
|
-
## Licence
|
|
594
|
-
|
|
595
|
-
MIT. See `LICENSE`.
|
|
596
|
-
|
|
597
|
-
### Content language and CJK fonts
|
|
598
|
-
|
|
599
|
-
Pass the source BCP 47 language to `renderer.setLyrics(lyrics, { language: "ja" })`
|
|
600
|
-
(or `element.lyricsOptions = { language: "ja" }` before assigning lyrics). If language detection
|
|
601
|
-
finishes later, `renderer.setLanguage("ja")` updates the existing lines and their measurements
|
|
602
|
-
without replacing their DOM or animations. Repeating the same language (including equivalent spellings
|
|
603
|
-
such as `ja-JP` and `ja_JP`) does not write to the DOM or measure again. Omitting the language on a
|
|
604
|
-
new song clears the old hint.
|
|
605
|
-
|
|
606
|
-
The renderer preserves Chinese script/region tags and uses kana or Hangul as a fallback when
|
|
607
|
-
metadata is missing or contradicts the text. Han characters alone cannot identify a language;
|
|
608
|
-
untagged Han-only lyrics remain unknown. Translation and romanization settings are not needed
|
|
609
|
-
for this local inference.
|
|
610
|
-
|
|
611
|
-
Use `injectTranslation(document, lineElement, text, "zh-Hant")` to label a translation separately
|
|
612
|
-
from its source. The language argument is optional; unknown translations get `lang=""` so they
|
|
613
|
-
do not inherit the original lyrics' language. Romanizations use the source language with `Latn`.
|
|
614
|
-
|
|
615
|
-
Default font stacks now resolve on each lyric and decoration instead of only at the root. Hosts
|
|
616
|
-
loading regional CJK web fonts should set `--noto-sans-universal` on these elements according to
|
|
617
|
-
`:lang()`: a Japanese line and its Chinese translation can then choose different font subsets.
|
|
618
|
-
`--blyrics-font-family` and `--blyrics-translated-font-family` remain explicit theme overrides.
|
|
619
|
-
They are unset by default; custom CSS that reads them directly should use
|
|
620
|
-
`var(--blyrics-font-family, var(--blyrics-default-font-family))` for the default stack.
|
|
621
|
-
|
|
622
|
-
### Theme-provided instrumental image
|
|
623
|
-
|
|
624
|
-
A theme can paint the instrumental note with an image. Enable it in a theme comment:
|
|
625
|
-
|
|
626
|
-
```css
|
|
627
|
-
/* blyrics-image-highlights = true; */
|
|
628
|
-
.blyrics-container {
|
|
629
|
-
--blyrics-highlight-image: url("my-image.jpg");
|
|
630
|
-
}
|
|
631
|
-
```
|
|
222
|
+
`demo/compare.html`, on the dev server only (`pnpm -C demo dev`), puts this renderer next to
|
|
223
|
+
`@braccato/core` on one clock. Its URL holds the
|
|
224
|
+
whole state (`?song=kettle&t=10.6&layout=stage&mode=split`), so a moment can be screenshotted
|
|
225
|
+
again and compared.
|
|
632
226
|
|
|
633
|
-
|
|
634
|
-
image may be an SDR texture or an HDR gain-map asset. The theme owns HDR media queries and
|
|
635
|
-
`dynamic-range-limit`; the core never opts into extra display brightness. The note keeps its original
|
|
636
|
-
color path until its SVG image loads, reuses the original animated wave clip and fade group, and
|
|
637
|
-
forced colors fall back to system colors. Words no longer take an image fill.
|
|
227
|
+
## Differences from `@braccato/core`
|
|
638
228
|
|
|
639
|
-
|
|
229
|
+
- Theme rules that style line elements directly have no effect, as above.
|
|
230
|
+
- Image highlights are drawn into the canvas, which is standard dynamic range: an HDR image shows
|
|
231
|
+
its SDR rendition.
|
|
232
|
+
- On Firefox, core centres the sung line about 3% of the view higher than it does on Chrome (its
|
|
233
|
+
line measurement differs there); this renderer centres it as core does on Chrome, in both.
|
|
234
|
+
- Checked in Chrome and Firefox; Safari has not been screenshotted.
|