@braccato/core 1.20.0 → 1.21.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.
Files changed (85) hide show
  1. package/README.md +40 -37
  2. package/dist/README.md +40 -37
  3. package/dist/constants.d.ts +10 -0
  4. package/dist/constants.js +10 -0
  5. package/dist/engine/animationConfig.d.ts +83 -0
  6. package/dist/engine/animationConfig.js +93 -0
  7. package/dist/engine/animationDrift.d.ts +11 -0
  8. package/dist/engine/animationDrift.js +43 -0
  9. package/dist/engine/animationTiming.d.ts +26 -0
  10. package/dist/engine/animationTiming.js +120 -0
  11. package/dist/engine/autoScroll.d.ts +24 -0
  12. package/dist/engine/autoScroll.js +103 -0
  13. package/dist/engine/clock.d.ts +29 -0
  14. package/dist/engine/clock.js +70 -0
  15. package/dist/engine/contextSweep.d.ts +10 -0
  16. package/dist/engine/contextSweep.js +102 -0
  17. package/dist/engine/cssValues.d.ts +13 -0
  18. package/dist/engine/cssValues.js +47 -0
  19. package/dist/engine/culling.d.ts +6 -0
  20. package/dist/engine/culling.js +76 -0
  21. package/dist/engine/debugOverlay.d.ts +14 -0
  22. package/dist/engine/debugOverlay.js +58 -0
  23. package/dist/engine/decorationSlides.d.ts +6 -0
  24. package/dist/engine/decorationSlides.js +53 -0
  25. package/dist/engine/elementMotion.d.ts +17 -0
  26. package/dist/engine/elementMotion.js +98 -0
  27. package/dist/engine/glow.d.ts +7 -0
  28. package/dist/engine/glow.js +59 -0
  29. package/dist/engine/instance.d.ts +105 -0
  30. package/dist/engine/instance.js +89 -0
  31. package/dist/engine/instrumentalAnimations.d.ts +7 -0
  32. package/dist/engine/instrumentalAnimations.js +59 -0
  33. package/dist/engine/letterPlans.d.ts +31 -0
  34. package/dist/engine/letterPlans.js +62 -0
  35. package/dist/engine/lineAnimationState.d.ts +10 -0
  36. package/dist/engine/lineAnimationState.js +82 -0
  37. package/dist/engine/lineAnimations.d.ts +5 -0
  38. package/dist/engine/lineAnimations.js +63 -0
  39. package/dist/engine/lineLifecycle.d.ts +12 -0
  40. package/dist/engine/lineLifecycle.js +92 -0
  41. package/dist/engine/lineScroll.d.ts +47 -0
  42. package/dist/engine/lineScroll.js +184 -0
  43. package/dist/engine/lineScrollStyle.d.ts +32 -0
  44. package/dist/engine/lineScrollStyle.js +133 -0
  45. package/dist/engine/liveViews.d.ts +3 -0
  46. package/dist/engine/liveViews.js +5 -0
  47. package/dist/engine/motionCurves.d.ts +19 -0
  48. package/dist/engine/motionCurves.js +85 -0
  49. package/dist/engine/motionPreference.d.ts +1 -0
  50. package/dist/engine/motionPreference.js +1 -0
  51. package/dist/engine/passiveScroll.d.ts +10 -0
  52. package/dist/engine/passiveScroll.js +93 -0
  53. package/dist/engine/renderedLines.d.ts +16 -0
  54. package/dist/engine/renderedLines.js +130 -0
  55. package/dist/engine/scrollViewport.d.ts +36 -0
  56. package/dist/engine/scrollViewport.js +104 -0
  57. package/dist/engine/stageMotion.d.ts +7 -0
  58. package/dist/engine/stageMotion.js +319 -0
  59. package/dist/engine/sweepPlans.d.ts +36 -0
  60. package/dist/engine/sweepPlans.js +59 -0
  61. package/dist/engine/tick.d.ts +16 -0
  62. package/dist/engine/tick.js +270 -0
  63. package/dist/engine/timingLogs.d.ts +7 -0
  64. package/dist/engine/timingLogs.js +81 -0
  65. package/dist/engine/userScroll.d.ts +12 -0
  66. package/dist/engine/userScroll.js +62 -0
  67. package/dist/engine/wavePool.d.ts +7 -0
  68. package/dist/engine/wavePool.js +22 -0
  69. package/dist/engine/wordAnimations.d.ts +5 -0
  70. package/dist/engine/wordAnimations.js +238 -0
  71. package/dist/imageHighlights.d.ts +0 -11
  72. package/dist/imageHighlights.js +2 -48
  73. package/dist/index.d.ts +3 -2
  74. package/dist/index.js +2 -1
  75. package/dist/inject.d.ts +41 -2
  76. package/dist/inject.js +73 -28
  77. package/dist/renderer.js +10 -3
  78. package/dist/styles/image-highlights.css +1 -59
  79. package/dist/styles/lyrics.css +128 -57
  80. package/dist/types.d.ts +1 -1
  81. package/dist/view.d.ts +2 -2
  82. package/dist/view.js +7 -3
  83. package/package.json +1 -1
  84. package/dist/engine.d.ts +0 -471
  85. package/dist/engine.js +0 -3133
@@ -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,82 @@
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
+ }
11
+ export function resetPartAnimations(part) {
12
+ for (const animation of part.animations) {
13
+ animation.cancel();
14
+ const pool = waveAnimationPools.get(animation);
15
+ if (pool)
16
+ pool.push(animation);
17
+ }
18
+ part.animations = [];
19
+ }
20
+ function resetLineAnimations(lineData) {
21
+ resetPartAnimations(lineData);
22
+ for (const part of lineData.parts)
23
+ resetPartAnimations(part);
24
+ }
25
+ export function hasLineAnimations(lineData) {
26
+ return [lineData, ...lineData.parts].some(part => part.animations.length > 0);
27
+ }
28
+ export function markLineAnimationsStopped(lineData) {
29
+ lineData.isAnimating = false;
30
+ lineData.isAnimationPlayStatePlaying = false;
31
+ lineData.accumulatedOffsetMs = 0;
32
+ }
33
+ export function resetLineAnimationState(lineData) {
34
+ resetLineAnimations(lineData);
35
+ markLineAnimationsStopped(lineData);
36
+ }
37
+ export function togglePartClass(part, className, force) {
38
+ part.lyricElement.classList.toggle(className, force);
39
+ if ("highlightElement" in part) {
40
+ part.highlightElement.classList.toggle(className, force);
41
+ }
42
+ }
43
+ export function setAnimationsPlayState(lineData, isPlaying) {
44
+ const children = [lineData, ...lineData.parts];
45
+ for (const part of children) {
46
+ togglePartClass(part, PAUSED_CLASS, !isPlaying);
47
+ for (const animation of part.animations) {
48
+ if (isPlaying) {
49
+ animation.play();
50
+ }
51
+ else {
52
+ animation.pause();
53
+ }
54
+ }
55
+ }
56
+ }
57
+ export function clearLineStateClasses(lineData) {
58
+ lineData.lyricElement.classList.remove(ANIMATING_CLASS);
59
+ for (const part of [lineData, ...lineData.parts]) {
60
+ togglePartClass(part, PAUSED_CLASS, false);
61
+ }
62
+ }
63
+ export function updateWordStates(lineData, currentTime) {
64
+ const parts = lineData.parts;
65
+ const lineEndTime = lineData.time + lineData.duration;
66
+ for (let i = 0; i < parts.length; i++) {
67
+ const part = parts[i];
68
+ const endTime = part.duration > 0 ? part.time + part.duration : (parts[i + 1]?.time ?? lineEndTime);
69
+ let state = WORD_STATE_PAST;
70
+ if (currentTime < part.time) {
71
+ state = WORD_STATE_UPCOMING;
72
+ }
73
+ else if (currentTime < endTime) {
74
+ state = WORD_STATE_ACTIVE;
75
+ }
76
+ if (part.wordState === state)
77
+ continue;
78
+ part.wordState = state;
79
+ part.lyricElement.setAttribute(WORD_STATE_ATTR, state);
80
+ part.highlightElement.setAttribute(WORD_STATE_ATTR, state);
81
+ }
82
+ }
@@ -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(line: LineData, config: AnimationConfig, lyricTimeS: number): void;
@@ -0,0 +1,63 @@
1
+ import { correctedAnimationTimeMs, followSongClock } from "./animationTiming.js";
2
+ import { placeContextSweeps } from "./contextSweep.js";
3
+ import { suppressInvisibleWordGlows } from "./glow.js";
4
+ import { startInstrumentalAnimations, startInstrumentalExitAnimations } from "./instrumentalAnimations.js";
5
+ import { resetPartAnimations } from "./lineAnimationState.js";
6
+ import { startWordAnimations, startWordExitAnimation } from "./wordAnimations.js";
7
+ export function startLineAnimations(engine, line, config, lyricTimeS) {
8
+ const appliedTimingOffsetMs = engine.learnedAnimationTimingOffsetMs;
9
+ if (!isInstrumental(line))
10
+ placeContextSweeps(line, config);
11
+ startLineEntrance(engine, line, config, lyricTimeS, appliedTimingOffsetMs);
12
+ if (isInstrumental(line)) {
13
+ startInstrumentalAnimations(engine, line, config, lyricTimeS, appliedTimingOffsetMs);
14
+ return;
15
+ }
16
+ suppressInvisibleWordGlows(engine, line, config);
17
+ for (const part of line.parts)
18
+ startWordAnimations(engine, part, config, lyricTimeS, appliedTimingOffsetMs);
19
+ }
20
+ export function startLineExitAnimations(line, config, lyricTimeS) {
21
+ startLineExit(line, config);
22
+ if (isInstrumental(line)) {
23
+ startInstrumentalExitAnimations(line, config, lyricTimeS);
24
+ return;
25
+ }
26
+ for (const part of line.parts) {
27
+ if (lyricTimeS >= part.time)
28
+ startWordExitAnimation(part, config);
29
+ else
30
+ resetPartAnimations(part);
31
+ }
32
+ }
33
+ function isInstrumental(line) {
34
+ return line.lyricElement.dataset.instrumental === "true";
35
+ }
36
+ function startLineEntrance(engine, line, config, lyricTimeS, appliedTimingOffsetMs) {
37
+ resetPartAnimations(line);
38
+ if (!config.enabled.lineScale)
39
+ return;
40
+ const { enterFrom, enterTo, enterEasing, durationMs } = config.line;
41
+ const entrance = line.lyricElement.animate([{ transform: enterFrom }, { transform: enterTo }], {
42
+ duration: durationMs,
43
+ easing: enterEasing,
44
+ fill: "forwards",
45
+ });
46
+ followSongClock(engine, entrance, { appliedTimingOffsetMs, offsetMs: 0 });
47
+ const elapsedMs = (lyricTimeS - line.time) * 1000;
48
+ entrance.currentTime = correctedAnimationTimeMs(elapsedMs, appliedTimingOffsetMs, durationMs);
49
+ line.animations = [entrance];
50
+ }
51
+ function startLineExit(line, config) {
52
+ resetPartAnimations(line);
53
+ if (!config.enabled.lineScale)
54
+ return;
55
+ const { exitFrom, exitTo, exitEasing, durationMs } = config.line;
56
+ const exit = line.lyricElement.animate([{ transform: exitFrom }, { transform: exitTo }], {
57
+ duration: durationMs,
58
+ easing: exitEasing,
59
+ fill: "none",
60
+ });
61
+ line.animations = [exit];
62
+ exit.addEventListener("finish", () => resetPartAnimations(line), { once: true });
63
+ }
@@ -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(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,47 @@
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 RunningLineScroll {
13
+ lineElement: HTMLElement;
14
+ token: number;
15
+ durationMs: 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
+ startPx: number;
32
+ endPx: number;
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 clearLineScrolls(engine: AnimationEngineInstance): void;
44
+ export declare function isLineVisibleDuringScroll(line: LineScrollItem, fromScrollTop: number, toScrollTop: number, viewportHeight: number): boolean;
45
+ export declare function getLineScrollItems(engine: AnimationEngineInstance, lines: LineData[]): LineScrollItem[];
46
+ export declare function measureTrailingItem(lyricsElement: HTMLElement, className: string): LineScrollItem | null;
47
+ export {};
@@ -0,0 +1,184 @@
1
+ import { getRelativeLayoutBounds } from "../util.js";
2
+ import { addElementCurve, removeElementCurves, timelineNow } from "./elementMotion.js";
3
+ import { clearScrollVariables, resolveScrollTimings, resolveThemedTranslates, themeOverridesScrollTranslate, writeScrollVariables, } from "./lineScrollStyle.js";
4
+ export const SCROLL_PREPARE_LEAD_MS = 120;
5
+ export const LINE_SCROLL_WILL_CHANGE_VALUE = "transform, translate";
6
+ const SAME_SCROLL_TOLERANCE_PX = 2;
7
+ export function commitOrPrepareLineScroll(engine, request) {
8
+ const pending = engine.pendingLineScroll;
9
+ if (pending && pendingScrollMatches(pending, request)) {
10
+ engine.pendingLineScroll = null;
11
+ startLineScroll(engine, pending.plan);
12
+ return;
13
+ }
14
+ dropPendingLineScroll(engine);
15
+ const plan = planLineScroll(engine, request);
16
+ if (plan)
17
+ startLineScroll(engine, plan);
18
+ }
19
+ export function prepareUpcomingLineScroll(engine, request) {
20
+ const pending = engine.pendingLineScroll;
21
+ if (pending && pendingScrollMatches(pending, request))
22
+ return;
23
+ dropPendingLineScroll(engine);
24
+ const plan = planLineScroll(engine, request);
25
+ if (!plan)
26
+ return;
27
+ const { activeLine, fromScrollTop, toScrollTop } = request;
28
+ engine.pendingLineScroll = { plan, activeLineElement: activeLine.lyricElement, fromScrollTop, toScrollTop };
29
+ }
30
+ export function dropPendingLineScroll(engine) {
31
+ const pending = engine.pendingLineScroll;
32
+ if (!pending)
33
+ return;
34
+ for (const line of pending.plan)
35
+ releaseLine(engine, line.lineElement, line.token);
36
+ engine.pendingLineScroll = null;
37
+ }
38
+ function pendingScrollMatches(pending, request) {
39
+ return (pending.activeLineElement === request.activeLine.lyricElement &&
40
+ Math.abs(pending.fromScrollTop - request.fromScrollTop) <= SAME_SCROLL_TOLERANCE_PX &&
41
+ Math.abs(pending.toScrollTop - request.toScrollTop) <= SAME_SCROLL_TOLERANCE_PX);
42
+ }
43
+ function planLineScroll(engine, request) {
44
+ const { lines, config } = request;
45
+ const activeIndex = lines.findIndex(line => line.lyricElement === request.activeLine.lyricElement);
46
+ if (!config.enabled.scroll || activeIndex < 0)
47
+ return null;
48
+ const deltaPx = request.toScrollTop - request.fromScrollTop;
49
+ const translateThemed = themeOverridesScrollTranslate();
50
+ const targets = [];
51
+ for (let index = 0; index < lines.length; index++) {
52
+ const line = lines[index];
53
+ if (!isLineVisibleDuringScroll(line, request.fromScrollTop, request.toScrollTop, request.viewportHeight))
54
+ continue;
55
+ const lineElement = line.lyricElement;
56
+ const relativeIndex = index - activeIndex;
57
+ const side = scrollSide(relativeIndex, deltaPx);
58
+ const timingKey = `${side}|${Math.abs(relativeIndex)}`;
59
+ const cachedTiming = engine.cachedLineScrollTiming.get(timingKey);
60
+ targets.push({
61
+ lineElement,
62
+ side,
63
+ heightPx: line.height,
64
+ token: claimLine(engine, lineElement),
65
+ relativeIndex,
66
+ timingKey,
67
+ cachedTiming,
68
+ });
69
+ if (translateThemed || !cachedTiming) {
70
+ writeScrollVariables(lineElement, { index, activeIndex, relativeIndex, side, deltaPx });
71
+ }
72
+ }
73
+ const timings = getOrResolveScrollTimings(engine, targets, config);
74
+ const translates = translateThemed ? resolveThemedTranslates(engine, targets, config) : null;
75
+ const staggerScale = staggerScaleKeepingLinesApart(lines, activeIndex, Math.abs(deltaPx), request.continuesEngineScroll);
76
+ const activeDurationMs = timings[targets.findIndex(target => target.relativeIndex === 0)]?.durationMs;
77
+ return targets.map(({ lineElement, side, token }, index) => {
78
+ const timing = timings[index];
79
+ return {
80
+ lineElement,
81
+ side,
82
+ token,
83
+ durationMs: activeDurationMs === undefined
84
+ ? timing.durationMs
85
+ : activeDurationMs + (timing.durationMs - activeDurationMs) * staggerScale,
86
+ startEasing: timing.startEasing,
87
+ endEasing: timing.endEasing,
88
+ startPx: translates?.start[index] ?? deltaPx,
89
+ endPx: translates?.end[index] ?? 0,
90
+ };
91
+ });
92
+ }
93
+ function scrollSide(relativeIndex, deltaPx) {
94
+ const scrollsUp = deltaPx < 0;
95
+ if (relativeIndex < 0)
96
+ return scrollsUp ? "below" : "above";
97
+ if (relativeIndex > 0)
98
+ return scrollsUp ? "above" : "below";
99
+ return "active";
100
+ }
101
+ function getOrResolveScrollTimings(engine, targets, config) {
102
+ const misses = targets.filter(target => target.cachedTiming === undefined);
103
+ const resolved = new Map();
104
+ if (misses.length > 0) {
105
+ resolveScrollTimings(engine, misses, config).forEach((timing, index) => {
106
+ engine.cachedLineScrollTiming.set(misses[index].timingKey, timing);
107
+ resolved.set(misses[index], timing);
108
+ });
109
+ }
110
+ return targets.map(target => target.cachedTiming ?? resolved.get(target));
111
+ }
112
+ function staggerScaleKeepingLinesApart(lines, activeIndex, distancePx, continuesEngineScroll) {
113
+ if (continuesEngineScroll)
114
+ return 1;
115
+ const active = lines[activeIndex];
116
+ const neighbourSteps = [lines[activeIndex - 1], lines[activeIndex + 1]]
117
+ .filter(neighbour => neighbour !== undefined)
118
+ .map(neighbour => Math.abs(active.position - neighbour.position));
119
+ const stepPx = neighbourSteps.length > 0 ? Math.min(...neighbourSteps) : active.height;
120
+ return Math.min(1, stepPx / distancePx);
121
+ }
122
+ function startLineScroll(engine, plan) {
123
+ const startMs = timelineNow(engine);
124
+ for (const line of plan) {
125
+ if (!line.lineElement.isConnected || !ownsLine(engine, line.lineElement, line.token))
126
+ continue;
127
+ const scroll = { lineElement: line.lineElement, token: line.token, durationMs: line.durationMs };
128
+ engine.runningLineScrolls.push(scroll);
129
+ addElementCurve(engine, line.lineElement, {
130
+ kind: "scroll",
131
+ startMs,
132
+ durationMs: line.durationMs,
133
+ fromPx: line.startPx,
134
+ toPx: line.endPx,
135
+ easing: line.startEasing,
136
+ endEasing: line.endEasing,
137
+ onEnd: () => endLineScroll(engine, scroll),
138
+ });
139
+ }
140
+ }
141
+ function claimLine(engine, lineElement) {
142
+ const token = ++engine.lineScrollAnimationToken;
143
+ engine.lineScrollElementTokens.set(lineElement, token);
144
+ return token;
145
+ }
146
+ function ownsLine(engine, lineElement, token) {
147
+ return engine.lineScrollElementTokens.get(lineElement) === token;
148
+ }
149
+ function releaseLine(engine, lineElement, token) {
150
+ if (!ownsLine(engine, lineElement, token))
151
+ return;
152
+ clearScrollVariables(lineElement);
153
+ engine.lineScrollElementTokens.delete(lineElement);
154
+ }
155
+ function endLineScroll(engine, scroll) {
156
+ const index = engine.runningLineScrolls.indexOf(scroll);
157
+ if (index !== -1)
158
+ engine.runningLineScrolls.splice(index, 1);
159
+ releaseLine(engine, scroll.lineElement, scroll.token);
160
+ }
161
+ export function clearLineScrolls(engine) {
162
+ const scrolledElements = new Set(engine.runningLineScrolls.map(scroll => scroll.lineElement));
163
+ for (const element of scrolledElements)
164
+ removeElementCurves(engine, element, "scroll");
165
+ for (const scroll of [...engine.runningLineScrolls])
166
+ endLineScroll(engine, scroll);
167
+ }
168
+ export function isLineVisibleDuringScroll(line, fromScrollTop, toScrollTop, viewportHeight) {
169
+ const visibleTop = Math.min(fromScrollTop, toScrollTop);
170
+ const visibleBottom = Math.max(fromScrollTop, toScrollTop) + viewportHeight;
171
+ return line.position + line.height >= visibleTop && line.position <= visibleBottom;
172
+ }
173
+ // Use relayout-time bounds; measuring here can force synchronous layout after tick style writes.
174
+ export function getLineScrollItems(engine, lines) {
175
+ const trailing = [engine.cachedCreditsItem, engine.cachedFooterItem].filter(item => item !== null);
176
+ return trailing.length > 0 ? [...lines, ...trailing] : lines;
177
+ }
178
+ export function measureTrailingItem(lyricsElement, className) {
179
+ const element = lyricsElement.querySelector(`:scope > .${className}`);
180
+ if (!element)
181
+ return null;
182
+ const bounds = getRelativeLayoutBounds(lyricsElement, element);
183
+ return { lyricElement: element, position: bounds.y, height: bounds.height };
184
+ }
@@ -0,0 +1,32 @@
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
+ interface SizedScrollLine extends StyledScrollLine {
14
+ heightPx: number;
15
+ }
16
+ export interface ScrollVariables {
17
+ index: number;
18
+ activeIndex: number;
19
+ relativeIndex: number;
20
+ side: LineScrollSide;
21
+ deltaPx: number;
22
+ }
23
+ export declare function themeOverridesScrollTranslate(): boolean;
24
+ export declare function writeScrollVariables(lineElement: HTMLElement, variables: ScrollVariables): void;
25
+ export declare function clearScrollVariables(lineElement: HTMLElement): void;
26
+ export declare function resolveScrollTimings(engine: AnimationEngineInstance, lines: StyledScrollLine[], { lineScroll }: AnimationConfig): LineScrollTiming[];
27
+ export declare function resolveThemedTranslates(engine: AnimationEngineInstance, lines: SizedScrollLine[], { lineScroll }: AnimationConfig): {
28
+ start: (number | null)[];
29
+ end: (number | null)[];
30
+ };
31
+ export declare function translateYPx(translate: string, heightPx: number): number | null;
32
+ export {};
@@ -0,0 +1,133 @@
1
+ import { registerThemeSetting } from "../themeSettings.js";
2
+ import { toMs } from "../util.js";
3
+ const FALLBACK_DURATION_MS = 750;
4
+ const DEFAULT_TRAILING_DURATION = "calc(750ms + log(var(--blyrics-line-scroll-abs-relative-index) + 2, 2.71828) * 80ms + (var(--blyrics-line-scroll-abs-relative-index) + 1) * 20ms)";
5
+ function themeSetting(property, defaultValue) {
6
+ return [property, registerThemeSetting(property.slice(2), defaultValue)];
7
+ }
8
+ const THEME_SETTINGS = [
9
+ themeSetting("--blyrics-line-scroll-duration", DEFAULT_TRAILING_DURATION),
10
+ themeSetting("--blyrics-line-scroll-above-duration", "calc(750ms + min(var(--blyrics-line-scroll-abs-relative-index), 6) * 20ms)"),
11
+ themeSetting("--blyrics-line-scroll-active-duration", ""),
12
+ themeSetting("--blyrics-line-scroll-below-duration", DEFAULT_TRAILING_DURATION),
13
+ themeSetting("--blyrics-line-scroll-timing-function", "var(--blyrics-lyric-scroll-timing-function)"),
14
+ themeSetting("--blyrics-line-scroll-start-easing", "var(--blyrics-line-scroll-timing-function)"),
15
+ themeSetting("--blyrics-line-scroll-end-easing", "linear"),
16
+ themeSetting("--blyrics-line-scroll-above-start-easing", ""),
17
+ themeSetting("--blyrics-line-scroll-active-start-easing", ""),
18
+ themeSetting("--blyrics-line-scroll-below-start-easing", ""),
19
+ themeSetting("--blyrics-line-scroll-above-end-easing", ""),
20
+ themeSetting("--blyrics-line-scroll-active-end-easing", ""),
21
+ themeSetting("--blyrics-line-scroll-below-end-easing", ""),
22
+ themeSetting("--blyrics-line-scroll-translate-y-start", "var(--blyrics-line-scroll-delta-px)"),
23
+ themeSetting("--blyrics-line-scroll-translate-y-end", "0px"),
24
+ themeSetting("--blyrics-line-scroll-above-translate-y-start", ""),
25
+ themeSetting("--blyrics-line-scroll-active-translate-y-start", ""),
26
+ themeSetting("--blyrics-line-scroll-below-translate-y-start", ""),
27
+ themeSetting("--blyrics-line-scroll-above-translate-y-end", ""),
28
+ themeSetting("--blyrics-line-scroll-active-translate-y-end", ""),
29
+ themeSetting("--blyrics-line-scroll-below-translate-y-end", ""),
30
+ ];
31
+ const TRANSLATE_SETTINGS = THEME_SETTINGS.filter(([property]) => property.includes("translate-y")).map(([, setting]) => setting);
32
+ const INDEX_PROPERTY = "--blyrics-line-scroll-index";
33
+ const ACTIVE_INDEX_PROPERTY = "--blyrics-line-scroll-active-index";
34
+ const RELATIVE_INDEX_PROPERTY = "--blyrics-line-scroll-relative-index";
35
+ const ABS_RELATIVE_INDEX_PROPERTY = "--blyrics-line-scroll-abs-relative-index";
36
+ const SIDE_PROPERTY = "--blyrics-line-scroll-side";
37
+ const DELTA_PROPERTY = "--blyrics-line-scroll-delta-px";
38
+ const DISTANCE_PROPERTY = "--blyrics-line-scroll-distance-px";
39
+ const INLINE_PROPERTIES = [
40
+ INDEX_PROPERTY,
41
+ ACTIVE_INDEX_PROPERTY,
42
+ RELATIVE_INDEX_PROPERTY,
43
+ ABS_RELATIVE_INDEX_PROPERTY,
44
+ SIDE_PROPERTY,
45
+ DELTA_PROPERTY,
46
+ DISTANCE_PROPERTY,
47
+ ...THEME_SETTINGS.map(([property]) => property),
48
+ ];
49
+ export function themeOverridesScrollTranslate() {
50
+ return TRANSLATE_SETTINGS.some(setting => setting.isManuallySet());
51
+ }
52
+ export function writeScrollVariables(lineElement, variables) {
53
+ const style = lineElement.style;
54
+ style.setProperty(INDEX_PROPERTY, String(variables.index));
55
+ style.setProperty(ACTIVE_INDEX_PROPERTY, String(variables.activeIndex));
56
+ style.setProperty(RELATIVE_INDEX_PROPERTY, String(variables.relativeIndex));
57
+ style.setProperty(ABS_RELATIVE_INDEX_PROPERTY, String(Math.abs(variables.relativeIndex)));
58
+ style.setProperty(SIDE_PROPERTY, variables.side);
59
+ style.setProperty(DELTA_PROPERTY, `${variables.deltaPx}px`);
60
+ style.setProperty(DISTANCE_PROPERTY, `${Math.abs(variables.deltaPx)}px`);
61
+ for (const [property, setting] of THEME_SETTINGS) {
62
+ const value = setting.getStringValue().trim();
63
+ if (value)
64
+ style.setProperty(property, value);
65
+ else
66
+ style.removeProperty(property);
67
+ }
68
+ }
69
+ export function clearScrollVariables(lineElement) {
70
+ for (const property of INLINE_PROPERTIES)
71
+ lineElement.style.removeProperty(property);
72
+ }
73
+ function scrollVariable(name, side, differential, fallback) {
74
+ const shared = `var(--blyrics-line-scroll-${name}, ${fallback})`;
75
+ return differential ? `var(--blyrics-line-scroll-${side}-${name}, ${shared})` : shared;
76
+ }
77
+ export function resolveScrollTimings(engine, lines, { lineScroll }) {
78
+ const differential = lineScroll.differentialEffects;
79
+ const durations = batchProbeComputedStyle(engine, lines, "transition-duration", line => scrollVariable("duration", line.side, differential, `${FALLBACK_DURATION_MS}ms`), style => {
80
+ const durationMs = toMs(style.transitionDuration.split(",")[0].trim());
81
+ return durationMs > 0 ? durationMs : FALLBACK_DURATION_MS;
82
+ });
83
+ const easingsAt = (keyframe) => batchProbeComputedStyle(engine, lines, "transition-timing-function", line => scrollVariable(`${keyframe}-easing`, line.side, differential, `var(--blyrics-line-scroll-timing-function, ${lineScroll.easing})`), style => style.transitionTimingFunction.trim() || lineScroll.easing);
84
+ const startEasings = easingsAt("start");
85
+ const endEasings = easingsAt("end");
86
+ return lines.map((_, index) => ({
87
+ durationMs: durations[index],
88
+ startEasing: startEasings[index],
89
+ endEasing: endEasings[index],
90
+ }));
91
+ }
92
+ export function resolveThemedTranslates(engine, lines, { lineScroll }) {
93
+ const translatesAt = (keyframe) => batchProbeComputedStyle(engine, lines, "translate", line => {
94
+ const fallback = keyframe === "start" ? "var(--blyrics-line-scroll-delta-px, 0px)" : "0px";
95
+ return `0 ${scrollVariable(`translate-y-${keyframe}`, line.side, lineScroll.differentialEffects, fallback)}`;
96
+ }, style => style.translate).map((translate, index) => translateYPx(translate, lines[index].heightPx));
97
+ return { start: translatesAt("start"), end: translatesAt("end") };
98
+ }
99
+ export function translateYPx(translate, heightPx) {
100
+ const value = translate.trim();
101
+ if (value === "" || value === "none")
102
+ return 0;
103
+ const [, y = "0px"] = value.match(/calc\([^)]*\)|\S+/g) ?? [];
104
+ const terms = y
105
+ .replace(/^calc\((.*)\)$/, "$1")
106
+ .replace(/\s+-\s+/g, " + -")
107
+ .split(/\s+\+\s+/);
108
+ let px = 0;
109
+ for (const term of terms) {
110
+ const length = term.trim().match(/^(-?[\d.]+(?:e-?\d+)?)(px|%)?$/);
111
+ if (!length || (length[2] === undefined && Number(length[1]) !== 0))
112
+ return null;
113
+ px += length[2] === "%" ? (Number(length[1]) * heightPx) / 100 : Number(length[1]);
114
+ }
115
+ return px;
116
+ }
117
+ function batchProbeComputedStyle(engine, lines, property, probeValue, readValue) {
118
+ const previous = lines.map(({ lineElement }) => ({
119
+ value: lineElement.style.getPropertyValue(property),
120
+ priority: lineElement.style.getPropertyPriority(property),
121
+ }));
122
+ for (const line of lines)
123
+ line.lineElement.style.setProperty(property, probeValue(line), "important");
124
+ const values = lines.map(line => readValue(engine.window.getComputedStyle(line.lineElement)));
125
+ lines.forEach(({ lineElement }, index) => {
126
+ const { value, priority } = previous[index];
127
+ if (value)
128
+ lineElement.style.setProperty(property, value, priority);
129
+ else
130
+ lineElement.style.removeProperty(property);
131
+ });
132
+ return values;
133
+ }
@@ -0,0 +1,3 @@
1
+ import type { AnimationEngineInstance } from "./instance.js";
2
+ export declare const liveEngines: Set<AnimationEngineInstance>;
3
+ export declare function forEveryLiveView(runOperation: (engine: AnimationEngineInstance) => void): void;