@braccato/core 1.21.2 → 1.21.4

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.
@@ -1,5 +1,5 @@
1
1
  import type { AnimationEngineInstance } from "./instance.js";
2
- import { type MotionCurve } from "./motionCurves.js";
2
+ import { type EasingLookup, type MotionCurve } from "./motionCurves.js";
3
3
  export type MotionKind = "scroll" | "slide";
4
4
  export interface ElementCurve extends MotionCurve {
5
5
  kind: MotionKind;
@@ -15,3 +15,4 @@ export declare function removeElementCurves(engine: AnimationEngineInstance, ele
15
15
  export declare function stopElementMotion(engine: AnimationEngineInstance, element: HTMLElement): void;
16
16
  export declare function stopAllElementMotion(engine: AnimationEngineInstance): void;
17
17
  export declare function elementMotionOffsetPx(engine: AnimationEngineInstance, element: HTMLElement): number;
18
+ export declare function easingLookup(engine: AnimationEngineInstance): EasingLookup;
@@ -61,7 +61,7 @@ function playCurves(engine, element, curves, nowMs) {
61
61
  animation.addEventListener("finish", settle, { once: true });
62
62
  animation.addEventListener("cancel", settle, { once: true });
63
63
  }
64
- function easingLookup(engine) {
64
+ export function easingLookup(engine) {
65
65
  return easing => {
66
66
  let table = engine.easingTables.get(easing);
67
67
  if (!table) {
@@ -24,6 +24,8 @@ export declare function cellFade(swipe: SwipeRamp, span: CellSpan): number;
24
24
  * the cell's halo would show behind the edge before any of its text is lit, so its glow waits.
25
25
  */
26
26
  export declare function cellReachedAt(swipe: SwipeRamp, span: CellSpan): number;
27
+ /** How far through the swipe, in eased progress, its start stop reaches the end of a cell. */
28
+ export declare function cellLitAt(swipe: SwipeRamp, span: CellSpan): number;
27
29
  /**
28
30
  * Plans one cell of a sweep. In the cell's own widths the start stop runs from
29
31
  * `(startFrom - span.start) / span.width` to `(startTo - span.start) / span.width`, and the cell only
@@ -25,8 +25,15 @@ export function cellFade(swipe, span) {
25
25
  * the cell's halo would show behind the edge before any of its text is lit, so its glow waits.
26
26
  */
27
27
  export function cellReachedAt(swipe, span) {
28
+ return startStopReaches(swipe, span.start);
29
+ }
30
+ /** How far through the swipe, in eased progress, its start stop reaches the end of a cell. */
31
+ export function cellLitAt(swipe, span) {
32
+ return startStopReaches(swipe, span.start + span.width);
33
+ }
34
+ function startStopReaches(swipe, position) {
28
35
  const stops = swipeStops(swipe);
29
- return stops ? clamp((span.start - stops.startFrom) / (stops.startTo - stops.startFrom), 0, 1) : 0;
36
+ return stops ? clamp((position - stops.startFrom) / (stops.startTo - stops.startFrom), 0, 1) : 0;
30
37
  }
31
38
  /**
32
39
  * Plans one cell of a sweep. In the cell's own widths the start stop runs from
@@ -1,9 +1,10 @@
1
1
  import { RTL_CLASS } from "../constants.js";
2
2
  import { registerThemeSetting } from "../themeSettings.js";
3
3
  import { correctedAnimationTimeMs, mirrorHighlightAnimations, followSongClock } from "./animationTiming.js";
4
+ import { easingLookup } from "./elementMotion.js";
4
5
  import { GECKO_REDRAW, GECKO_REDRAW_KEYFRAMES, GECKO_REDRAW_PERIOD_MS, LETTER_WAVE_CREST_OFFSET, planLetterWave, swipeRamp, } from "./letterPlans.js";
5
6
  import { resetPartAnimations } from "./lineAnimationState.js";
6
- import { cellReachedAt, planCellSweep } from "./sweepPlans.js";
7
+ import { cellLitAt, cellReachedAt, planCellSweep } from "./sweepPlans.js";
7
8
  import { acquireWaveAnimation } from "./wavePool.js";
8
9
  const SWIPE_LEAD_RATIO = registerThemeSetting("blyrics-swipe-lead-ratio", 0.1);
9
10
  const SWIPE_DURATION_RATIO = registerThemeSetting("blyrics-swipe-duration-ratio", 1.6);
@@ -13,6 +14,8 @@ const INSTANT_MS = 1;
13
14
  const SHOWN = [{ opacity: 1 }, { opacity: 1 }];
14
15
  const FADE_IN = [{ opacity: 0 }, { opacity: 1 }];
15
16
  const FADE_OUT = [{ opacity: 1 }, { opacity: 0 }];
17
+ // A line's exit leaves the started ones running so a glow fades out under the text instead of cutting off.
18
+ const glowDelaysMs = new WeakMap();
16
19
  export function startWordAnimations(engine, part, config, lyricTimeS, appliedTimingOffsetMs) {
17
20
  resetPartAnimations(part);
18
21
  const word = wordTimeline(engine, part, config, lyricTimeS, appliedTimingOffsetMs);
@@ -23,6 +26,8 @@ export function startWordAnimations(engine, part, config, lyricTimeS, appliedTim
23
26
  part.animations = [...highlight, ...mirrorHighlightAnimations(part, highlight), ...motion];
24
27
  }
25
28
  export function startWordExitAnimation(part, config) {
29
+ const glows = part.animations.filter(animation => glowStarted(animation));
30
+ part.animations = part.animations.filter(animation => !glows.includes(animation));
26
31
  resetPartAnimations(part);
27
32
  const fades = config.enabled.highlightFade;
28
33
  const durationMs = fades ? config.highlight.fadeOutDurationMs : INSTANT_MS;
@@ -31,9 +36,13 @@ export function startWordExitAnimation(part, config) {
31
36
  easing: fades ? config.highlight.fadeOutEasing : "linear",
32
37
  fill: "none",
33
38
  });
34
- part.animations = [fadeOut, ...mirrorHighlightAnimations(part, [fadeOut])];
39
+ part.animations = [fadeOut, ...mirrorHighlightAnimations(part, [fadeOut]), ...glows];
35
40
  fadeOut.addEventListener("finish", () => resetPartAnimations(part), { once: true });
36
41
  }
42
+ function glowStarted(animation) {
43
+ const delayMs = glowDelaysMs.get(animation);
44
+ return delayMs !== undefined && Number(animation.currentTime) >= delayMs;
45
+ }
37
46
  function wordTimeline(engine, part, config, lyricTimeS, appliedTimingOffsetMs) {
38
47
  const durationMs = Math.max(0, part.duration * 1000);
39
48
  return {
@@ -123,27 +132,27 @@ function glow(word, durationMs, followsSwipe) {
123
132
  }
124
133
  // Where the shrinking radius reaches the tight halo's, as a fraction of the glow's eased progress.
125
134
  const TIGHT_GLOW_AT = 0.6;
135
+ // The latest a halo may finish rising, so the crossfade after it keeps room to run.
136
+ const LATEST_GLOW_PEAK = TIGHT_GLOW_AT / 2;
126
137
  /**
127
138
  * Stands in for a glow whose radius shrinks to nothing: the wide halo crossfades into the tight one
128
139
  * while the radius would shrink between them, and the tight one then fades out. The keyframe offsets
129
140
  * are in eased progress, since the glow's easing applies to the whole effect. Under a swipe each
130
- * cell's glow starts when the edge reaches it, so a halo never shows ahead of its lit text. The wait
131
- * is exact for a linear swipe and taken as linear for any other.
141
+ * cell's glow starts when the edge reaches it and rises while the edge crosses it, the way a glow
142
+ * drawn from the lit text grows with it. Both are exact for a linear swipe and taken as linear for
143
+ * any other.
132
144
  */
133
145
  function sweepGlow(word, cells, durationMs, followsSwipe) {
134
- const { config, swipeLeadMs, swipeDurationMs } = word;
146
+ const { engine, config, swipeLeadMs, swipeDurationMs } = word;
135
147
  const fades = config.highlight.glowRestingInvisible;
136
- const wide = fades
137
- ? [{ opacity: 1 }, { opacity: 0, offset: TIGHT_GLOW_AT }, { opacity: 0 }]
138
- : [{ opacity: 1 }, { opacity: 1 }];
139
- const tight = fades
140
- ? [{ opacity: 0 }, { opacity: 1, offset: TIGHT_GLOW_AT }, { opacity: 0 }]
141
- : [{ opacity: 0 }, { opacity: 0 }];
148
+ const easeGlow = easingLookup(engine)(config.highlight.glowEasing);
149
+ const ramp = swipeRamp(config);
142
150
  return cells.flatMap((cell, cellIndex) => {
143
151
  const span = cell.span ?? { start: cellIndex / cells.length, width: 1 / cells.length };
144
- const delayMs = followsSwipe
145
- ? Math.max(0, cellReachedAt(swipeRamp(config), span) * swipeDurationMs - swipeLeadMs)
146
- : 0;
152
+ const delayMs = followsSwipe ? Math.max(0, cellReachedAt(ramp, span) * swipeDurationMs - swipeLeadMs) : 0;
153
+ const litMs = followsSwipe ? cellLitAt(ramp, span) * swipeDurationMs - swipeLeadMs : delayMs;
154
+ const riseMs = Math.max(0, litMs - delayMs);
155
+ const peak = durationMs > 0 ? Math.min(easeGlow(riseMs / durationMs), LATEST_GLOW_PEAK) : 0;
147
156
  const timing = {
148
157
  delay: delayMs,
149
158
  duration: durationMs,
@@ -151,12 +160,25 @@ function sweepGlow(word, cells, durationMs, followsSwipe) {
151
160
  fill: fades ? "none" : "forwards",
152
161
  };
153
162
  const currentTimeMs = scheduledTimeMs(word, word.elapsedMs, delayMs + durationMs);
163
+ const halos = haloKeyframes(fades, peak);
154
164
  return cell.glows.map((halo, index) => {
155
- const fade = halo.glow.animate(index === 0 ? wide : tight, timing);
165
+ const fade = halo.glow.animate(index === 0 ? halos.wide : halos.tight, timing);
166
+ glowDelaysMs.set(fade, delayMs);
156
167
  return followSong(word, fade, currentTimeMs, { skipDriftSample: true });
157
168
  });
158
169
  });
159
170
  }
171
+ function haloKeyframes(fades, peak) {
172
+ const rises = peak > 0;
173
+ const wideRise = rises ? [{ opacity: 0 }, { opacity: 1, offset: peak }] : [{ opacity: 1 }];
174
+ const tightWait = rises ? [{ opacity: 0 }, { opacity: 0, offset: peak }] : [{ opacity: 0 }];
175
+ if (!fades)
176
+ return { wide: [...wideRise, { opacity: 1 }], tight: [{ opacity: 0 }, { opacity: 0 }] };
177
+ return {
178
+ wide: [...wideRise, { opacity: 0, offset: TIGHT_GLOW_AT }, { opacity: 0 }],
179
+ tight: [...tightWait, { opacity: 1, offset: TIGHT_GLOW_AT }, { opacity: 0 }],
180
+ };
181
+ }
160
182
  function wobble(word) {
161
183
  const { wobbleFrom, wobblePeak, wobblePeakOffset, wobblePeakEasing, wobbleSettle, wobbleSettleOffset } = word.config.word;
162
184
  const { wobbleTo, wobbleEndEasing, wobbleDurationMs, wobbleEasing } = word.config.word;
package/dist/inject.js CHANGED
@@ -14,6 +14,7 @@
14
14
  import { releasePart } from "./engine/lineAnimationState.js";
15
15
  import { normalizeLanguage } from "./language.js";
16
16
  import { BACKGROUND_LINE_CLASS, BACKGROUND_LYRIC_CLASS, BIDI_RUN_CLASS, BIDI_SENSITIVE_CLASS, CONTENT_LINE_CLASS, EXPLICIT_WORD_CLASS, HIGHLIGHT_RUN_CLASS, LETTER_CLASS, LINE_MAIN_CLASS, LINE_SYNCED_WORD_CLASS, LONG_WORD_GROUP_CLASS, ROMANIZED_LYRICS_CLASS, RTL_CLASS, SWEEP_CELL_CLASS, SWEEP_GLOW_CLASS, SWEEP_MASK_CLASS, SWEEP_TEXT_CLASS, TRANSLATED_LYRICS_CLASS, WORD_CLASS, WORD_GROUP_CLASS, WORD_HIGHLIGHT_CLASS, WORD_HIGHLIGHT_CONTEXT_CLASS, WORD_HIGHLIGHT_LETTERED_CLASS, WORD_HIGHLIGHT_SWEEP_CLASS, WORD_STATE_ATTR, WORD_STATE_UPCOMING, ZERO_DURATION_ANIMATION_CLASS, } from "./constants.js";
17
+ import { letterClusters } from "./letters.js";
17
18
  import { getSeekTimeFromClick } from "./seek.js";
18
19
  import { isSameText, testJoiningScript, testRtl } from "./text.js";
19
20
  import { registerThemeSetting } from "./themeSettings.js";
@@ -23,7 +24,6 @@ let longWordThreshold = registerThemeSetting("blyrics-long-word-threshold", 1500
23
24
  let longWordWrapThreshold = registerThemeSetting("blyrics-long-word-wrap-threshold", 10, true);
24
25
  let letterWave = registerThemeSetting("blyrics-letter-wave", true, true);
25
26
  export const hideCredits = registerThemeSetting("blyrics-hide-credits", false, true);
26
- const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
27
27
  const LTR_SCRIPT_REGEX = /[\p{Script=Latin}\p{Script=Greek}\p{Script=Cyrillic}\p{Script=Han}\p{Script=Hangul}\p{Script=Hiragana}\p{Script=Katakana}]/u;
28
28
  const SPACE_REGEX = /^\s+$/u;
29
29
  export function findNearestAgent(lyrics, fromIndex) {
@@ -194,21 +194,20 @@ function groupTokensByWord(tokens, splitBackgroundGroups) {
194
194
  flush();
195
195
  return groups;
196
196
  }
197
- function appendLongWordBreaks(doc, span, text, threshold) {
198
- if (text.length <= threshold) {
199
- span.textContent = text;
197
+ function appendLongWordBreaks(doc, span, clusters, threshold) {
198
+ if (clusters.length <= threshold) {
199
+ span.textContent = clusters.join("");
200
200
  return false;
201
201
  }
202
- for (let i = 0; i < text.length; i += threshold) {
203
- span.appendChild(doc.createTextNode(text.slice(i, i + threshold)));
204
- if (i + threshold < text.length) {
202
+ for (let i = 0; i < clusters.length; i += threshold) {
203
+ span.appendChild(doc.createTextNode(clusters.slice(i, i + threshold).join("")));
204
+ if (i + threshold < clusters.length) {
205
205
  span.appendChild(doc.createElement("wbr"));
206
206
  }
207
207
  }
208
208
  return true;
209
209
  }
210
- function appendLetters(doc, wordElement, text) {
211
- const chars = [...graphemeSegmenter.segment(text)].map(segment => segment.segment);
210
+ function appendLetters(doc, wordElement, chars) {
212
211
  wordElement.style.setProperty("--letters", String(chars.length));
213
212
  return chars.map(char => {
214
213
  const letter = doc.createElement("span");
@@ -218,13 +217,13 @@ function appendLetters(doc, wordElement, text) {
218
217
  return letter;
219
218
  });
220
219
  }
221
- function fillWordText(doc, host, text, wrapThreshold, perLetter, preserveJoining) {
220
+ function fillWordText(doc, host, clusters, wrapThreshold, perLetter, preserveJoining) {
222
221
  if (perLetter)
223
- return appendLetters(doc, host, text);
222
+ return appendLetters(doc, host, clusters);
224
223
  if (preserveJoining)
225
- host.textContent = text;
224
+ host.textContent = clusters.join("");
226
225
  else
227
- appendLongWordBreaks(doc, host, text, wrapThreshold);
226
+ appendLongWordBreaks(doc, host, clusters, wrapThreshold);
228
227
  return undefined;
229
228
  }
230
229
  const SWEEP_GLOW_RADII = ["wide", "tight"];
@@ -264,6 +263,7 @@ function createTimedWordSpan(doc, part, wrapThreshold, perLetter, preserveJoinin
264
263
  let sweep;
265
264
  let letters;
266
265
  let highlightLetters;
266
+ const clusters = perLetter || (!preserveJoining && part.words.length > wrapThreshold) ? letterClusters(part.words) : [part.words];
267
267
  for (const wordElement of [span, highlight]) {
268
268
  wordElement.classList.add(WORD_CLASS);
269
269
  wordElement.dir = "auto";
@@ -285,12 +285,12 @@ function createTimedWordSpan(doc, part, wrapThreshold, perLetter, preserveJoinin
285
285
  wordElement.classList.add(WORD_HIGHLIGHT_LETTERED_CLASS);
286
286
  }
287
287
  if (wordElement === highlight && atomic) {
288
- const cell = createSweepCell(doc, highlight, into => fillWordText(doc, into, part.words, wrapThreshold, perLetter, preserveJoining));
288
+ const cell = createSweepCell(doc, highlight, into => fillWordText(doc, into, clusters, wrapThreshold, perLetter, preserveJoining));
289
289
  sweep = [cell];
290
290
  highlightLetters = cell.letters;
291
291
  }
292
292
  else {
293
- const collected = fillWordText(doc, wordElement, part.words, wrapThreshold, perLetter, preserveJoining);
293
+ const collected = fillWordText(doc, wordElement, clusters, wrapThreshold, perLetter, preserveJoining);
294
294
  if (wordElement === span)
295
295
  letters = collected;
296
296
  else
@@ -316,9 +316,9 @@ function createWordGroup(doc, group, lineData, wordsFlowInline) {
316
316
  const mixedDirection = testRtl(group.text) && [...group.text].some(char => /[\p{Letter}\p{Number}]/u.test(char) && !testRtl(char));
317
317
  const preserveJoining = testJoiningScript(group.text);
318
318
  const perLetter = letterWave.getBooleanValue() && !preserveJoining && !mixedDirection;
319
+ const isLongGroup = group.text.length > wrapThreshold * 2 && letterClusters(group.text).length > wrapThreshold * 2;
319
320
  const lyricGroup = doc.createElement("span");
320
321
  const highlightGroup = doc.createElement("span");
321
- const isLongGroup = group.text.length > wrapThreshold * 2;
322
322
  // A word that is its own inline-block box can hold the sweep's block layers. Long groups and
323
323
  // bidi-sensitive rows lay their words out inline, where a word can break across lines.
324
324
  const atomic = !wordsFlowInline && !isLongGroup;
@@ -0,0 +1,2 @@
1
+ export declare function letterClusters(text: string): string[];
2
+ export declare function joinLetterSegments(segments: Iterable<string>): string[];
@@ -0,0 +1,25 @@
1
+ const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
2
+ // Canonical_Combining_Class=Virama minus the ones that stay visible; Sinhala al-lakuna joins only beside a ZWJ.
3
+ const CONJUNCT_VIRAMA_REGEX = /(?:[\u{94D}\u{9CD}\u{A4D}\u{ACD}\u{B4D}\u{C4D}\u{CCD}\u{D4D}\u{1039}\u{17D2}\u{1A60}\u{1B44}\u{1BAB}\u{2D7F}\u{A9C0}\u{AAF6}\u{10A3F}\u{11046}\u{1107F}\u{110B9}\u{11133}\u{111C0}\u{11235}\u{1134D}\u{113D0}\u{11442}\u{114C2}\u{115BF}\u{1163F}\u{116B6}\u{11839}\u{1193E}\u{119E0}\u{11A47}\u{11A99}\u{11C3F}\u{11D45}\u{11D97}\u{11F42}]\u200D?|\u200D\u0DCA|\u0DCA\u200D)$/u;
4
+ const LETTER_START_REGEX = /^\p{L}/u;
5
+ const MARK_START_REGEX = /^\p{M}/u;
6
+ const joinsTamilLigature = (previous, next) => (previous.endsWith("க்") && next.startsWith("ஷ")) || (previous.endsWith("ஸ்") && next.startsWith("ரீ"));
7
+ export function letterClusters(text) {
8
+ return joinLetterSegments(Array.from(graphemeSegmenter.segment(text), ({ segment }) => segment));
9
+ }
10
+ export function joinLetterSegments(segments) {
11
+ const clusters = [];
12
+ let previous = "";
13
+ for (const segment of segments) {
14
+ const joins = clusters.length > 0 &&
15
+ (MARK_START_REGEX.test(segment) ||
16
+ (LETTER_START_REGEX.test(segment) &&
17
+ (CONJUNCT_VIRAMA_REGEX.test(previous) || joinsTamilLigature(previous, segment))));
18
+ if (joins)
19
+ clusters[clusters.length - 1] += segment;
20
+ else
21
+ clusters.push(segment);
22
+ previous = segment;
23
+ }
24
+ return clusters;
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@braccato/core",
3
- "version": "1.21.2",
3
+ "version": "1.21.4",
4
4
  "description": "Synchronized lyrics renderer with word-by-word animations",
5
5
  "type": "module",
6
6
  "license": "MIT",