@braccato/core 1.19.1 → 1.20.1

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.
Files changed (70) hide show
  1. package/dist/engine/animationConfig.d.ts +88 -0
  2. package/dist/engine/animationConfig.js +98 -0
  3. package/dist/engine/animationDrift.d.ts +11 -0
  4. package/dist/engine/animationDrift.js +43 -0
  5. package/dist/engine/animationTiming.d.ts +25 -0
  6. package/dist/engine/animationTiming.js +119 -0
  7. package/dist/engine/autoScroll.d.ts +24 -0
  8. package/dist/engine/autoScroll.js +103 -0
  9. package/dist/engine/clock.d.ts +29 -0
  10. package/dist/engine/clock.js +70 -0
  11. package/dist/engine/cssValues.d.ts +13 -0
  12. package/dist/engine/cssValues.js +47 -0
  13. package/dist/engine/culling.d.ts +6 -0
  14. package/dist/engine/culling.js +67 -0
  15. package/dist/engine/debugOverlay.d.ts +14 -0
  16. package/dist/engine/debugOverlay.js +58 -0
  17. package/dist/engine/decorationSlides.d.ts +6 -0
  18. package/dist/engine/decorationSlides.js +44 -0
  19. package/dist/engine/glow.d.ts +7 -0
  20. package/dist/engine/glow.js +59 -0
  21. package/dist/engine/highlightKeyframes.d.ts +8 -0
  22. package/dist/engine/highlightKeyframes.js +37 -0
  23. package/dist/engine/instance.d.ts +102 -0
  24. package/dist/engine/instance.js +87 -0
  25. package/dist/engine/instrumentalAnimations.d.ts +7 -0
  26. package/dist/engine/instrumentalAnimations.js +59 -0
  27. package/dist/engine/letterPlans.d.ts +42 -0
  28. package/dist/engine/letterPlans.js +103 -0
  29. package/dist/engine/lineAnimationState.d.ts +10 -0
  30. package/dist/engine/lineAnimationState.js +85 -0
  31. package/dist/engine/lineAnimations.d.ts +5 -0
  32. package/dist/engine/lineAnimations.js +82 -0
  33. package/dist/engine/lineLifecycle.d.ts +12 -0
  34. package/dist/engine/lineLifecycle.js +92 -0
  35. package/dist/engine/lineScroll.d.ts +48 -0
  36. package/dist/engine/lineScroll.js +174 -0
  37. package/dist/engine/lineScrollStyle.d.ts +27 -0
  38. package/dist/engine/lineScrollStyle.js +119 -0
  39. package/dist/engine/liveViews.d.ts +3 -0
  40. package/dist/engine/liveViews.js +5 -0
  41. package/dist/engine/motionPreference.d.ts +1 -0
  42. package/dist/engine/motionPreference.js +1 -0
  43. package/dist/engine/passiveScroll.d.ts +10 -0
  44. package/dist/engine/passiveScroll.js +93 -0
  45. package/dist/engine/renderedLines.d.ts +17 -0
  46. package/dist/engine/renderedLines.js +137 -0
  47. package/dist/engine/scrollViewport.d.ts +36 -0
  48. package/dist/engine/scrollViewport.js +104 -0
  49. package/dist/engine/stageMotion.d.ts +7 -0
  50. package/dist/engine/stageMotion.js +319 -0
  51. package/dist/engine/tick.d.ts +13 -0
  52. package/dist/engine/tick.js +246 -0
  53. package/dist/engine/timingLogs.d.ts +7 -0
  54. package/dist/engine/timingLogs.js +81 -0
  55. package/dist/engine/userScroll.d.ts +12 -0
  56. package/dist/engine/userScroll.js +62 -0
  57. package/dist/engine/wavePool.d.ts +8 -0
  58. package/dist/engine/wavePool.js +22 -0
  59. package/dist/engine/wordAnimations.d.ts +9 -0
  60. package/dist/engine/wordAnimations.js +216 -0
  61. package/dist/index.d.ts +3 -2
  62. package/dist/index.js +2 -1
  63. package/dist/inject.js +40 -22
  64. package/dist/renderer.js +8 -2
  65. package/dist/types.d.ts +1 -1
  66. package/dist/view.d.ts +2 -2
  67. package/dist/view.js +7 -3
  68. package/package.json +2 -2
  69. package/dist/engine.d.ts +0 -471
  70. package/dist/engine.js +0 -3133
@@ -0,0 +1,103 @@
1
+ import { clamp, isGecko } from "../util.js";
2
+ export const LETTER_WAVE_CREST_OFFSET = 0.4;
3
+ // WebRender redraws a cached tile only after a quarter device pixel of movement, so Gecko letters step.
4
+ // An invisible additive opacity ripple changes every frame and forces the redraw.
5
+ export const GECKO_REDRAW = isGecko();
6
+ export const GECKO_REDRAW_KEYFRAMES = [{ opacity: 0 }, { opacity: -0.002 }, { opacity: 0 }];
7
+ export const GECKO_REDRAW_PERIOD_MS = 200;
8
+ export function swipeRamp({ highlight }) {
9
+ return {
10
+ easing: highlight.swipeEasing,
11
+ startFrom: highlight.swipeStartFrom,
12
+ startTo: highlight.swipeStartTo,
13
+ endFrom: highlight.swipeEndFrom,
14
+ endTo: highlight.swipeEndTo,
15
+ };
16
+ }
17
+ export function linearForwardSwipeWindows(swipe, letterCount, swipeDurationMs) {
18
+ if (swipe.easing !== "linear" || swipeDurationMs <= 0 || letterCount <= 0)
19
+ return null;
20
+ const startFrom = Number.parseFloat(swipe.startFrom);
21
+ const startTo = Number.parseFloat(swipe.startTo);
22
+ const endFrom = Number.parseFloat(swipe.endFrom);
23
+ const endTo = Number.parseFloat(swipe.endTo);
24
+ const finite = [startFrom, startTo, endFrom, endTo].every(Number.isFinite);
25
+ if (!finite || startTo <= startFrom || endTo <= endFrom)
26
+ return null;
27
+ const startAt = (timeMs) => startFrom + ((startTo - startFrom) * timeMs) / swipeDurationMs;
28
+ const endAt = (timeMs) => endFrom + ((endTo - endFrom) * timeMs) / swipeDurationMs;
29
+ const timeWhenEndReaches = (value) => (swipeDurationMs * (value - endFrom)) / (endTo - endFrom);
30
+ const timeWhenStartReaches = (value) => (swipeDurationMs * (value - startFrom)) / (startTo - startFrom);
31
+ const windows = [];
32
+ for (let index = 0; index < letterCount; index++) {
33
+ const beginMs = clamp(timeWhenEndReaches(index / letterCount), 0, swipeDurationMs);
34
+ const finishMs = clamp(timeWhenStartReaches((index + 1) / letterCount), 0, swipeDurationMs);
35
+ windows.push({
36
+ delayMs: beginMs,
37
+ durationMs: Math.max(finishMs - beginMs, 1),
38
+ from: { start: startAt(beginMs), end: endAt(beginMs) },
39
+ to: { start: startAt(finishMs), end: endAt(finishMs) },
40
+ });
41
+ }
42
+ return windows;
43
+ }
44
+ export function planLetterMaskSweep(swipe, letterCount, swipeDurationMs, rtl) {
45
+ const windows = linearForwardSwipeWindows({ ...swipe, easing: "linear" }, letterCount, swipeDurationMs);
46
+ if (!windows)
47
+ return [];
48
+ const maskSpan = letterCount + 2;
49
+ const maskPositionAt = (start, index) => {
50
+ const position = (0.5 * maskSpan - (start * letterCount - index)) / (maskSpan - 1);
51
+ return `${(rtl ? 1 - position : position) * 100}% 0%`;
52
+ };
53
+ const durationMs = swipeDurationMs > 0 ? swipeDurationMs : 1;
54
+ return windows.map((window, index) => {
55
+ const from = maskPositionAt(window.from.start, index);
56
+ const to = maskPositionAt(window.to.start, index);
57
+ return swipe.easing === "linear"
58
+ ? windowedSweep(window, from, to)
59
+ : wholeSwipeSweep(window, from, to, durationMs, swipe.easing);
60
+ });
61
+ }
62
+ function windowedSweep(window, from, to) {
63
+ return {
64
+ keyframes: [
65
+ { offset: 0, maskPosition: from },
66
+ { offset: 1, maskPosition: to },
67
+ ],
68
+ delayMs: window.delayMs,
69
+ durationMs: window.durationMs,
70
+ easing: "linear",
71
+ };
72
+ }
73
+ function wholeSwipeSweep(window, from, to, swipeDurationMs, easing) {
74
+ const revealStart = clamp(window.delayMs / swipeDurationMs, 0, 1);
75
+ const revealEnd = clamp((window.delayMs + window.durationMs) / swipeDurationMs, 0, 1);
76
+ const keyframes = [{ offset: 0, maskPosition: from }];
77
+ if (revealStart > 0)
78
+ keyframes.push({ offset: revealStart, maskPosition: from });
79
+ if (revealEnd > revealStart)
80
+ keyframes.push({ offset: revealEnd, maskPosition: to });
81
+ if (revealEnd < 1)
82
+ keyframes.push({ offset: 1, maskPosition: to });
83
+ return { keyframes, delayMs: 0, durationMs: swipeDurationMs, easing };
84
+ }
85
+ export function planLetterWave(swipe, letterCount, wordDurationMs, swipeDurationMs, swipeLeadMs, floatDurationMs) {
86
+ const windows = linearForwardSwipeWindows({ ...swipe, easing: "linear" }, letterCount, swipeDurationMs);
87
+ if (!windows)
88
+ return evenlySpreadWaves(letterCount, wordDurationMs, floatDurationMs);
89
+ const riseMs = floatDurationMs * LETTER_WAVE_CREST_OFFSET;
90
+ return windows.map(window => {
91
+ const touchedMs = window.delayMs - swipeLeadMs;
92
+ const lightingMs = window.durationMs;
93
+ if (lightingMs > riseMs)
94
+ return { delayMs: touchedMs, durationMs: lightingMs / LETTER_WAVE_CREST_OFFSET };
95
+ return { delayMs: touchedMs + lightingMs - riseMs, durationMs: floatDurationMs };
96
+ });
97
+ }
98
+ function evenlySpreadWaves(letterCount, wordDurationMs, floatDurationMs) {
99
+ return Array.from({ length: letterCount }, (_, index) => ({
100
+ delayMs: (wordDurationMs * index) / letterCount,
101
+ durationMs: floatDurationMs,
102
+ }));
103
+ }
@@ -0,0 +1,10 @@
1
+ import type { AnimationData, LineData, PartData } from "../inject.js";
2
+ export declare function releasePart(part: PartData): void;
3
+ export declare function resetPartAnimations(part: AnimationData): void;
4
+ export declare function hasLineAnimations(lineData: LineData): boolean;
5
+ export declare function markLineAnimationsStopped(lineData: LineData): void;
6
+ export declare function resetLineAnimationState(lineData: LineData): void;
7
+ export declare function togglePartClass(part: LineData | PartData, className: string, force: boolean): void;
8
+ export declare function setAnimationsPlayState(lineData: LineData, isPlaying: boolean): void;
9
+ export declare function clearLineStateClasses(lineData: LineData): void;
10
+ export declare function updateWordStates(lineData: LineData, currentTime: number): void;
@@ -0,0 +1,85 @@
1
+ import { ANIMATING_CLASS, PAUSED_CLASS, WORD_STATE_ACTIVE, WORD_STATE_ATTR, WORD_STATE_PAST, WORD_STATE_UPCOMING, } from "../constants.js";
2
+ import { waveAnimationPools } from "./wavePool.js";
3
+ export function releasePart(part) {
4
+ resetPartAnimations(part);
5
+ togglePartClass(part, PAUSED_CLASS, false);
6
+ togglePartClass(part, ANIMATING_CLASS, false);
7
+ part.wordState = WORD_STATE_UPCOMING;
8
+ part.lyricElement.setAttribute(WORD_STATE_ATTR, WORD_STATE_UPCOMING);
9
+ part.highlightElement.setAttribute(WORD_STATE_ATTR, WORD_STATE_UPCOMING);
10
+ part.imageLayers?.highlight?.setAttribute(WORD_STATE_ATTR, WORD_STATE_UPCOMING);
11
+ }
12
+ export function resetPartAnimations(part) {
13
+ for (const animation of part.animations) {
14
+ animation.cancel();
15
+ const pool = waveAnimationPools.get(animation);
16
+ if (pool)
17
+ pool.push(animation);
18
+ }
19
+ part.animations = [];
20
+ }
21
+ function resetLineAnimations(lineData) {
22
+ resetPartAnimations(lineData);
23
+ for (const part of lineData.parts)
24
+ resetPartAnimations(part);
25
+ }
26
+ export function hasLineAnimations(lineData) {
27
+ return [lineData, ...lineData.parts].some(part => part.animations.length > 0);
28
+ }
29
+ export function markLineAnimationsStopped(lineData) {
30
+ lineData.isAnimating = false;
31
+ lineData.isAnimationPlayStatePlaying = false;
32
+ lineData.accumulatedOffsetMs = 0;
33
+ }
34
+ export function resetLineAnimationState(lineData) {
35
+ resetLineAnimations(lineData);
36
+ markLineAnimationsStopped(lineData);
37
+ }
38
+ export function togglePartClass(part, className, force) {
39
+ part.lyricElement.classList.toggle(className, force);
40
+ if ("highlightElement" in part) {
41
+ part.highlightElement.classList.toggle(className, force);
42
+ part.imageLayers?.highlight?.classList.toggle(className, force);
43
+ }
44
+ }
45
+ export function setAnimationsPlayState(lineData, isPlaying) {
46
+ const children = [lineData, ...lineData.parts];
47
+ for (const part of children) {
48
+ togglePartClass(part, PAUSED_CLASS, !isPlaying);
49
+ for (const animation of part.animations) {
50
+ if (isPlaying) {
51
+ animation.play();
52
+ }
53
+ else {
54
+ animation.pause();
55
+ }
56
+ }
57
+ }
58
+ }
59
+ export function clearLineStateClasses(lineData) {
60
+ lineData.lyricElement.classList.remove(ANIMATING_CLASS);
61
+ for (const part of [lineData, ...lineData.parts]) {
62
+ togglePartClass(part, PAUSED_CLASS, false);
63
+ }
64
+ }
65
+ export function updateWordStates(lineData, currentTime) {
66
+ const parts = lineData.parts;
67
+ const lineEndTime = lineData.time + lineData.duration;
68
+ for (let i = 0; i < parts.length; i++) {
69
+ const part = parts[i];
70
+ const endTime = part.duration > 0 ? part.time + part.duration : (parts[i + 1]?.time ?? lineEndTime);
71
+ let state = WORD_STATE_PAST;
72
+ if (currentTime < part.time) {
73
+ state = WORD_STATE_UPCOMING;
74
+ }
75
+ else if (currentTime < endTime) {
76
+ state = WORD_STATE_ACTIVE;
77
+ }
78
+ if (part.wordState === state)
79
+ continue;
80
+ part.wordState = state;
81
+ part.lyricElement.setAttribute(WORD_STATE_ATTR, state);
82
+ part.highlightElement.setAttribute(WORD_STATE_ATTR, state);
83
+ part.imageLayers?.highlight?.setAttribute(WORD_STATE_ATTR, state);
84
+ }
85
+ }
@@ -0,0 +1,5 @@
1
+ import type { LineData } from "../inject.js";
2
+ import type { AnimationConfig } from "./animationConfig.js";
3
+ import type { AnimationEngineInstance } from "./instance.js";
4
+ export declare function startLineAnimations(engine: AnimationEngineInstance, line: LineData, config: AnimationConfig, lyricTimeS: number): void;
5
+ export declare function startLineExitAnimations(engine: AnimationEngineInstance, line: LineData, config: AnimationConfig, lyricTimeS: number): void;
@@ -0,0 +1,82 @@
1
+ import { correctedAnimationTimeMs, followSongClock } from "./animationTiming.js";
2
+ import { suppressInvisibleWordGlows } from "./glow.js";
3
+ import { startInstrumentalAnimations, startInstrumentalExitAnimations } from "./instrumentalAnimations.js";
4
+ import { resetPartAnimations } from "./lineAnimationState.js";
5
+ import { startWordAnimations, startWordExitAnimation } from "./wordAnimations.js";
6
+ export function startLineAnimations(engine, line, config, lyricTimeS) {
7
+ const appliedTimingOffsetMs = engine.learnedAnimationTimingOffsetMs;
8
+ startLineEntrance(engine, line, config, lyricTimeS, appliedTimingOffsetMs);
9
+ if (isInstrumental(line)) {
10
+ startInstrumentalAnimations(engine, line, config, lyricTimeS, appliedTimingOffsetMs);
11
+ return;
12
+ }
13
+ suppressInvisibleWordGlows(engine, line, config);
14
+ for (const part of line.parts)
15
+ startWordAnimations(engine, part, config, lyricTimeS, appliedTimingOffsetMs);
16
+ }
17
+ export function startLineExitAnimations(engine, line, config, lyricTimeS) {
18
+ const heldPaint = readImagePaintBeforeWrites(engine, line, lyricTimeS);
19
+ startLineExit(line, config);
20
+ if (isInstrumental(line)) {
21
+ startInstrumentalExitAnimations(line, config, lyricTimeS);
22
+ return;
23
+ }
24
+ for (const part of line.parts) {
25
+ if (lyricTimeS >= part.time)
26
+ startWordExitAnimation(part, config, heldPaint.get(part));
27
+ else
28
+ resetPartAnimations(part);
29
+ }
30
+ }
31
+ function isInstrumental(line) {
32
+ return line.lyricElement.dataset.instrumental === "true";
33
+ }
34
+ function startLineEntrance(engine, line, config, lyricTimeS, appliedTimingOffsetMs) {
35
+ resetPartAnimations(line);
36
+ if (!config.enabled.lineScale)
37
+ return;
38
+ const { enterFrom, enterTo, enterEasing, durationMs } = config.line;
39
+ const entrance = line.lyricElement.animate([{ transform: enterFrom }, { transform: enterTo }], {
40
+ duration: durationMs,
41
+ easing: enterEasing,
42
+ fill: "forwards",
43
+ });
44
+ followSongClock(engine, entrance, { appliedTimingOffsetMs, offsetMs: 0 });
45
+ const elapsedMs = (lyricTimeS - line.time) * 1000;
46
+ entrance.currentTime = correctedAnimationTimeMs(elapsedMs, appliedTimingOffsetMs, durationMs);
47
+ line.animations = [entrance];
48
+ }
49
+ function startLineExit(line, config) {
50
+ resetPartAnimations(line);
51
+ if (!config.enabled.lineScale)
52
+ return;
53
+ const { exitFrom, exitTo, exitEasing, durationMs } = config.line;
54
+ const exit = line.lyricElement.animate([{ transform: exitFrom }, { transform: exitTo }], {
55
+ duration: durationMs,
56
+ easing: exitEasing,
57
+ fill: "none",
58
+ });
59
+ line.animations = [exit];
60
+ exit.addEventListener("finish", () => resetPartAnimations(line), { once: true });
61
+ }
62
+ function readImagePaintBeforeWrites(engine, line, lyricTimeS) {
63
+ const heldPaint = new Map();
64
+ const styleOf = (element) => engine.window.getComputedStyle(element);
65
+ for (const part of line.parts) {
66
+ if (!part.imageLayers || lyricTimeS < part.time)
67
+ continue;
68
+ const glowStyle = styleOf(part.imageLayers.glow);
69
+ const paint = [
70
+ { target: part.imageLayers.glow, frame: { opacity: glowStyle.opacity, filter: glowStyle.filter } },
71
+ ];
72
+ for (const target of part.letterElements ?? []) {
73
+ paint.push({ target, frame: { transform: styleOf(target).transform } });
74
+ }
75
+ for (const target of [...(part.highlightLetterElements ?? []), ...part.imageLayers.glowLetters]) {
76
+ const letterStyle = styleOf(target);
77
+ paint.push({ target, frame: { maskPosition: letterStyle.maskPosition, transform: letterStyle.transform } });
78
+ }
79
+ heldPaint.set(part, paint);
80
+ }
81
+ return heldPaint;
82
+ }
@@ -0,0 +1,12 @@
1
+ import type { AnimationConfig } from "./animationConfig.js";
2
+ import type { Playhead } from "./clock.js";
3
+ import type { AnimationEngineInstance } from "./instance.js";
4
+ interface LineTick {
5
+ engine: AnimationEngineInstance;
6
+ lyricTimeS: number;
7
+ playhead: Playhead;
8
+ config: AnimationConfig;
9
+ now: number;
10
+ }
11
+ export declare function driveLineAnimations(tick: LineTick): void;
12
+ export {};
@@ -0,0 +1,92 @@
1
+ import { ANIMATING_CLASS } from "../constants.js";
2
+ import { correctLineDrift } from "./animationDrift.js";
3
+ import { startLineAnimations, startLineExitAnimations } from "./lineAnimations.js";
4
+ import { clearLineStateClasses, hasLineAnimations, markLineAnimationsStopped, resetLineAnimationState, setAnimationsPlayState, updateWordStates, } from "./lineAnimationState.js";
5
+ import { logAnimationCleanup } from "./timingLogs.js";
6
+ const ANIMATION_PREPARE_AHEAD_S = 2;
7
+ const LINE_END_GRACE_S = 0.05;
8
+ const STALE_ANIMATION_GRACE_S = 0.05;
9
+ export function driveLineAnimations(tick) {
10
+ const { engine, lyricTimeS, playhead } = tick;
11
+ const lines = engine.lines;
12
+ const prepareAheadS = playhead.isPlaying ? ANIMATION_PREPARE_AHEAD_S : 0;
13
+ const linesToStart = [];
14
+ for (let index = 0; index < lines.length; index++) {
15
+ const line = lines[index];
16
+ const nextLineStartS = lines[index + 1]?.time ?? Infinity;
17
+ const animationEndS = Math.max(nextLineStartS, line.time + line.duration + LINE_END_GRACE_S);
18
+ if (lyricTimeS + prepareAheadS >= line.time && lyricTimeS < animationEndS) {
19
+ updateLineAnimationState(tick, line, index);
20
+ if (!line.isAnimating)
21
+ linesToStart.push(line);
22
+ }
23
+ else {
24
+ retireLine(tick, line, index, animationEndS);
25
+ }
26
+ }
27
+ for (const line of linesToStart)
28
+ startLine(tick, line);
29
+ }
30
+ function updateLineAnimationState(tick, line, index) {
31
+ const { lyricTimeS, playhead } = tick;
32
+ if (!line.isSelected) {
33
+ line.isSelected = true;
34
+ line.lyricElement.classList.add(ANIMATING_CLASS);
35
+ }
36
+ if (line.isAnimationPlayStatePlaying !== playhead.isPlaying) {
37
+ line.isAnimationPlayStatePlaying = playhead.isPlaying;
38
+ setAnimationsPlayState(line, playhead.isPlaying);
39
+ if (playhead.isPlaying)
40
+ line.isAnimating = false;
41
+ }
42
+ const playbackDiscontinuity = (playhead.jumped && playhead.isPlaying) || playhead.mediaClockRanBackwards || playhead.seekedWhilePaused;
43
+ if (playbackDiscontinuity && line.isAnimating)
44
+ resetLineAnimationState(line);
45
+ updateWordStates(line, lyricTimeS);
46
+ correctLineDrift({
47
+ engine: tick.engine,
48
+ line,
49
+ lineIndex: index,
50
+ lyricTimeS,
51
+ isPlaying: playhead.isPlaying,
52
+ now: tick.now,
53
+ });
54
+ }
55
+ function retireLine({ engine, lyricTimeS, playhead, config }, line, index, animationEndS) {
56
+ const staleAfterS = animationEndS + config.highlight.fadeOutDurationMs / 1000 + STALE_ANIMATION_GRACE_S;
57
+ const isStale = lyricTimeS > staleAfterS;
58
+ const wasSelected = line.isSelected;
59
+ if (wasSelected) {
60
+ if (playhead.isPlaying || playhead.jumped || playhead.seekedWhilePaused) {
61
+ if (isStale) {
62
+ logAnimationCleanup(engine, "selected-stale-reset", line, index, lyricTimeS, staleAfterS);
63
+ resetLineAnimationState(line);
64
+ }
65
+ else {
66
+ startLineExitAnimations(engine, line, config, lyricTimeS);
67
+ markLineAnimationsStopped(line);
68
+ }
69
+ }
70
+ else {
71
+ setAnimationsPlayState(line, false);
72
+ line.isAnimationPlayStatePlaying = false;
73
+ }
74
+ line.isSelected = false;
75
+ clearLineStateClasses(line);
76
+ }
77
+ else if (hasLineAnimations(line) && (playhead.jumped || isStale)) {
78
+ logAnimationCleanup(engine, playhead.jumped ? "time-jump-reset" : "stale-reset", line, index, lyricTimeS, staleAfterS);
79
+ resetLineAnimationState(line);
80
+ }
81
+ if (wasSelected || playhead.jumped)
82
+ updateWordStates(line, lyricTimeS);
83
+ }
84
+ function startLine({ engine, lyricTimeS, playhead, config, now }, line) {
85
+ startLineAnimations(engine, line, config, lyricTimeS);
86
+ line.isAnimating = true;
87
+ line.lastAnimSetupAt = now;
88
+ line.isAnimationPlayStatePlaying = playhead.isPlaying;
89
+ line.accumulatedOffsetMs = 0;
90
+ if (!playhead.isPlaying)
91
+ setAnimationsPlayState(line, false);
92
+ }
@@ -0,0 +1,48 @@
1
+ import type { LineData } from "../inject.js";
2
+ import type { AnimationConfig } from "./animationConfig.js";
3
+ import type { AnimationEngineInstance } from "./instance.js";
4
+ import { type StyledScrollLine } from "./lineScrollStyle.js";
5
+ export declare const SCROLL_PREPARE_LEAD_MS = 120;
6
+ export declare const LINE_SCROLL_WILL_CHANGE_VALUE = "transform, translate";
7
+ export interface LineScrollItem {
8
+ lyricElement: HTMLElement;
9
+ height: number;
10
+ position: number;
11
+ }
12
+ export interface LineScrollAnimationRecord {
13
+ animation: Animation;
14
+ lineElement: HTMLElement;
15
+ token: number;
16
+ }
17
+ export interface LineScrollRequest {
18
+ lines: LineScrollItem[];
19
+ activeLine: LineData;
20
+ fromScrollTop: number;
21
+ toScrollTop: number;
22
+ viewportHeight: number;
23
+ config: AnimationConfig;
24
+ continuesEngineScroll: boolean;
25
+ }
26
+ interface PlannedLineScroll extends StyledScrollLine {
27
+ token: number;
28
+ durationMs: number;
29
+ startEasing: string;
30
+ endEasing: string;
31
+ startTranslate: string;
32
+ endTranslate: string;
33
+ }
34
+ export interface PendingLineScroll {
35
+ plan: PlannedLineScroll[];
36
+ activeLineElement: HTMLElement;
37
+ fromScrollTop: number;
38
+ toScrollTop: number;
39
+ }
40
+ export declare function commitOrPrepareLineScroll(engine: AnimationEngineInstance, request: LineScrollRequest): void;
41
+ export declare function prepareUpcomingLineScroll(engine: AnimationEngineInstance, request: LineScrollRequest): void;
42
+ export declare function dropPendingLineScroll(engine: AnimationEngineInstance): void;
43
+ export declare function removeLineScrollAnimation(engine: AnimationEngineInstance, record: LineScrollAnimationRecord): void;
44
+ export declare function clearLineScrollAnimations(engine: AnimationEngineInstance): void;
45
+ export declare function isLineVisibleDuringScroll(line: LineScrollItem, fromScrollTop: number, toScrollTop: number, viewportHeight: number): boolean;
46
+ export declare function getLineScrollItems(engine: AnimationEngineInstance, lines: LineData[]): LineScrollItem[];
47
+ export declare function measureTrailingItem(lyricsElement: HTMLElement, className: string): LineScrollItem | null;
48
+ export {};
@@ -0,0 +1,174 @@
1
+ import { getRelativeLayoutBounds } from "../util.js";
2
+ import { clearScrollVariables, resolveScrollTimings, resolveThemedTranslates, themeOverridesScrollTranslate, writeScrollVariables, } from "./lineScrollStyle.js";
3
+ export const SCROLL_PREPARE_LEAD_MS = 120;
4
+ export const LINE_SCROLL_WILL_CHANGE_VALUE = "transform, translate";
5
+ const SAME_SCROLL_TOLERANCE_PX = 2;
6
+ export function commitOrPrepareLineScroll(engine, request) {
7
+ const pending = engine.pendingLineScroll;
8
+ if (pending && pendingScrollMatches(pending, request)) {
9
+ engine.pendingLineScroll = null;
10
+ startLineScroll(engine, pending.plan);
11
+ return;
12
+ }
13
+ dropPendingLineScroll(engine);
14
+ const plan = planLineScroll(engine, request);
15
+ if (plan)
16
+ startLineScroll(engine, plan);
17
+ }
18
+ export function prepareUpcomingLineScroll(engine, request) {
19
+ const pending = engine.pendingLineScroll;
20
+ if (pending && pendingScrollMatches(pending, request))
21
+ return;
22
+ dropPendingLineScroll(engine);
23
+ const plan = planLineScroll(engine, request);
24
+ if (!plan)
25
+ return;
26
+ const { activeLine, fromScrollTop, toScrollTop } = request;
27
+ engine.pendingLineScroll = { plan, activeLineElement: activeLine.lyricElement, fromScrollTop, toScrollTop };
28
+ }
29
+ export function dropPendingLineScroll(engine) {
30
+ const pending = engine.pendingLineScroll;
31
+ if (!pending)
32
+ return;
33
+ for (const line of pending.plan)
34
+ releaseLine(engine, line.lineElement, line.token);
35
+ engine.pendingLineScroll = null;
36
+ }
37
+ function pendingScrollMatches(pending, request) {
38
+ return (pending.activeLineElement === request.activeLine.lyricElement &&
39
+ Math.abs(pending.fromScrollTop - request.fromScrollTop) <= SAME_SCROLL_TOLERANCE_PX &&
40
+ Math.abs(pending.toScrollTop - request.toScrollTop) <= SAME_SCROLL_TOLERANCE_PX);
41
+ }
42
+ function planLineScroll(engine, request) {
43
+ const { lines, config } = request;
44
+ const activeIndex = lines.findIndex(line => line.lyricElement === request.activeLine.lyricElement);
45
+ if (!config.enabled.scroll || activeIndex < 0)
46
+ return null;
47
+ const deltaPx = request.toScrollTop - request.fromScrollTop;
48
+ const translateThemed = themeOverridesScrollTranslate();
49
+ const targets = [];
50
+ for (let index = 0; index < lines.length; index++) {
51
+ const line = lines[index];
52
+ if (!isLineVisibleDuringScroll(line, request.fromScrollTop, request.toScrollTop, request.viewportHeight))
53
+ continue;
54
+ const lineElement = line.lyricElement;
55
+ const relativeIndex = index - activeIndex;
56
+ const side = scrollSide(relativeIndex, deltaPx);
57
+ const timingKey = `${side}|${Math.abs(relativeIndex)}`;
58
+ const cachedTiming = engine.cachedLineScrollTiming.get(timingKey);
59
+ targets.push({ lineElement, side, token: claimLine(engine, lineElement), relativeIndex, timingKey, cachedTiming });
60
+ if (translateThemed || !cachedTiming) {
61
+ writeScrollVariables(lineElement, { index, activeIndex, relativeIndex, side, deltaPx });
62
+ }
63
+ }
64
+ const timings = getOrResolveScrollTimings(engine, targets, config);
65
+ const translates = translateThemed ? resolveThemedTranslates(engine, targets, config) : null;
66
+ const staggerScale = staggerScaleKeepingLinesApart(lines, activeIndex, Math.abs(deltaPx), request.continuesEngineScroll);
67
+ const activeDurationMs = timings[targets.findIndex(target => target.relativeIndex === 0)]?.durationMs;
68
+ return targets.map(({ lineElement, side, token }, index) => {
69
+ const timing = timings[index];
70
+ return {
71
+ lineElement,
72
+ side,
73
+ token,
74
+ durationMs: activeDurationMs === undefined
75
+ ? timing.durationMs
76
+ : activeDurationMs + (timing.durationMs - activeDurationMs) * staggerScale,
77
+ startEasing: timing.startEasing,
78
+ endEasing: timing.endEasing,
79
+ startTranslate: translates?.start[index] ?? `0px ${deltaPx}px`,
80
+ endTranslate: translates?.end[index] ?? "0px 0px",
81
+ };
82
+ });
83
+ }
84
+ function scrollSide(relativeIndex, deltaPx) {
85
+ const scrollsUp = deltaPx < 0;
86
+ if (relativeIndex < 0)
87
+ return scrollsUp ? "below" : "above";
88
+ if (relativeIndex > 0)
89
+ return scrollsUp ? "above" : "below";
90
+ return "active";
91
+ }
92
+ function getOrResolveScrollTimings(engine, targets, config) {
93
+ const misses = targets.filter(target => target.cachedTiming === undefined);
94
+ const resolved = new Map();
95
+ if (misses.length > 0) {
96
+ resolveScrollTimings(engine, misses, config).forEach((timing, index) => {
97
+ engine.cachedLineScrollTiming.set(misses[index].timingKey, timing);
98
+ resolved.set(misses[index], timing);
99
+ });
100
+ }
101
+ return targets.map(target => target.cachedTiming ?? resolved.get(target));
102
+ }
103
+ function staggerScaleKeepingLinesApart(lines, activeIndex, distancePx, continuesEngineScroll) {
104
+ if (continuesEngineScroll)
105
+ return 1;
106
+ const active = lines[activeIndex];
107
+ const neighbourSteps = [lines[activeIndex - 1], lines[activeIndex + 1]]
108
+ .filter(neighbour => neighbour !== undefined)
109
+ .map(neighbour => Math.abs(active.position - neighbour.position));
110
+ const stepPx = neighbourSteps.length > 0 ? Math.min(...neighbourSteps) : active.height;
111
+ return Math.min(1, stepPx / distancePx);
112
+ }
113
+ function startLineScroll(engine, plan) {
114
+ for (const line of plan) {
115
+ if (!line.lineElement.isConnected || !ownsLine(engine, line.lineElement, line.token))
116
+ continue;
117
+ const animation = line.lineElement.animate([
118
+ { translate: line.startTranslate, easing: line.startEasing },
119
+ { translate: line.endTranslate, easing: line.endEasing },
120
+ ], { composite: "add", duration: line.durationMs, easing: "linear", fill: "none" });
121
+ trackLineScrollAnimation(engine, animation, line.lineElement, line.token);
122
+ }
123
+ }
124
+ function claimLine(engine, lineElement) {
125
+ const token = ++engine.lineScrollAnimationToken;
126
+ engine.lineScrollElementTokens.set(lineElement, token);
127
+ return token;
128
+ }
129
+ function ownsLine(engine, lineElement, token) {
130
+ return engine.lineScrollElementTokens.get(lineElement) === token;
131
+ }
132
+ function releaseLine(engine, lineElement, token) {
133
+ if (!ownsLine(engine, lineElement, token))
134
+ return;
135
+ clearScrollVariables(lineElement);
136
+ engine.lineScrollElementTokens.delete(lineElement);
137
+ }
138
+ function trackLineScrollAnimation(engine, animation, lineElement, token) {
139
+ const record = { animation, lineElement, token };
140
+ engine.lineScrollAnimations.push(record);
141
+ animation.addEventListener("finish", () => removeLineScrollAnimation(engine, record), { once: true });
142
+ animation.addEventListener("cancel", () => removeLineScrollAnimation(engine, record), { once: true });
143
+ }
144
+ export function removeLineScrollAnimation(engine, record) {
145
+ const index = engine.lineScrollAnimations.indexOf(record);
146
+ if (index !== -1)
147
+ engine.lineScrollAnimations.splice(index, 1);
148
+ releaseLine(engine, record.lineElement, record.token);
149
+ }
150
+ export function clearLineScrollAnimations(engine) {
151
+ const records = engine.lineScrollAnimations;
152
+ engine.lineScrollAnimations = [];
153
+ for (const record of records) {
154
+ record.animation.cancel();
155
+ releaseLine(engine, record.lineElement, record.token);
156
+ }
157
+ }
158
+ export function isLineVisibleDuringScroll(line, fromScrollTop, toScrollTop, viewportHeight) {
159
+ const visibleTop = Math.min(fromScrollTop, toScrollTop);
160
+ const visibleBottom = Math.max(fromScrollTop, toScrollTop) + viewportHeight;
161
+ return line.position + line.height >= visibleTop && line.position <= visibleBottom;
162
+ }
163
+ // Use relayout-time bounds; measuring here can force synchronous layout after tick style writes.
164
+ export function getLineScrollItems(engine, lines) {
165
+ const trailing = [engine.cachedCreditsItem, engine.cachedFooterItem].filter(item => item !== null);
166
+ return trailing.length > 0 ? [...lines, ...trailing] : lines;
167
+ }
168
+ export function measureTrailingItem(lyricsElement, className) {
169
+ const element = lyricsElement.querySelector(`:scope > .${className}`);
170
+ if (!element)
171
+ return null;
172
+ const bounds = getRelativeLayoutBounds(lyricsElement, element);
173
+ return { lyricElement: element, position: bounds.y, height: bounds.height };
174
+ }
@@ -0,0 +1,27 @@
1
+ import type { AnimationConfig } from "./animationConfig.js";
2
+ import type { AnimationEngineInstance } from "./instance.js";
3
+ export type LineScrollSide = "above" | "active" | "below";
4
+ export interface LineScrollTiming {
5
+ durationMs: number;
6
+ startEasing: string;
7
+ endEasing: string;
8
+ }
9
+ export interface StyledScrollLine {
10
+ lineElement: HTMLElement;
11
+ side: LineScrollSide;
12
+ }
13
+ export interface ScrollVariables {
14
+ index: number;
15
+ activeIndex: number;
16
+ relativeIndex: number;
17
+ side: LineScrollSide;
18
+ deltaPx: number;
19
+ }
20
+ export declare function themeOverridesScrollTranslate(): boolean;
21
+ export declare function writeScrollVariables(lineElement: HTMLElement, variables: ScrollVariables): void;
22
+ export declare function clearScrollVariables(lineElement: HTMLElement): void;
23
+ export declare function resolveScrollTimings(engine: AnimationEngineInstance, lines: StyledScrollLine[], { lineScroll }: AnimationConfig): LineScrollTiming[];
24
+ export declare function resolveThemedTranslates(engine: AnimationEngineInstance, lines: StyledScrollLine[], { lineScroll }: AnimationConfig): {
25
+ start: string[];
26
+ end: string[];
27
+ };