feat: 优化歌词视觉效果

This commit is contained in:
yhlhoh committed 2026-11-11 21:40:57 +08:00
1 parent 82b1fa0d47
commit 60fbbced7d
6 files changed
+532 -47

No files matched your search

+177 -20
View File
@@ -298,8 +298,13 @@
shownIndex: -2,
tickTimer: null,
spans: null,
cue: null,
phase: null,
phaseTimer: null,
};
const LRC_OUTROS = ['none', 'fade', 'blur', 'slideDown', 'slideUp', 'zoom', 'custom'];
let lrcCustomStyleEl = null;
function applyLrcCustomCss(css) {
@@ -331,12 +336,60 @@
return Date.now() + clockSkewMs;
}
function getLrcTiming(style) {
const s = style || {};
const clamp = (value, min, max, fallback) => {
const n = Number(value);
return Number.isFinite(n) ? Math.min(max, Math.max(min, n)) : fallback;
};
return {
inMs: clamp(s.enterDurationMs, 0, 10000, 300),
holdMs: clamp(s.holdDurationMs, 0, 10000, 0),
outMs: clamp(s.outDurationMs, 0, 10000, 260),
shrinkMs: clamp(s.shrinkDurationMs, 0, 20000, 0),
shrinkTo: clamp(s.holdShrinkTo, 0.3, 1, 1),
blurPx: clamp(s.blurPx, 0, 60, 10),
outro: LRC_OUTROS.includes(s.outro) ? s.outro : 'fade',
};
}
/**
* 每句字幕的可用时长 = 到下一句的距离(最后一句用 tailMs)。
* 入场和回缩都必须在下一句出现前完成,否则观众看到的永远是「没缩完就跳走」。
*/
function cueWindowMs(index) {
const cue = lrc.cues[index];
if (!cue) return 0;
const next = lrc.cues[index + 1];
const end = next ? next.t : cue.t + (Number(lrc.tailMs) || 0);
return Math.max(0, end - cue.t);
}
function fitTimingToWindow(timing, windowMs) {
// 至少给回缩留 40% 的时间,否则回缩没有观感
const available = Math.max(120, windowMs || 0);
let inMs = timing.inMs;
let shrinkMs = timing.shrinkMs;
const total = inMs + shrinkMs;
if (total <= available) return { inMs, shrinkMs, fitted: false };
const inFloor = Math.min(inMs, available * 0.45);
inMs = Math.round(inFloor);
shrinkMs = Math.round(Math.max(120, Math.min(shrinkMs, available - inMs)));
return { inMs, shrinkMs, fitted: true };
}
function applyLrcStyle(style) {
const s = style || {};
lrcLayer.className = 'lrc-layer';
lrcLayer.classList.add('pos-' + (s.position || 'bottom'));
lrcLayer.classList.add('lrc-align-' + (s.align || 'center'));
lrcLayer.classList.add('lrc-anim-' + (s.animation || 'fade'));
const timing = getLrcTiming(s);
lrcLayer.classList.add('lrc-out-' + timing.outro);
lrcLayer.style.setProperty('--lrc-in-dur', timing.inMs + 'ms');
lrcLayer.style.setProperty('--lrc-out-dur', timing.outMs + 'ms');
lrcLayer.style.setProperty('--lrc-shrink-dur', timing.shrinkMs + 'ms');
lrcLayer.style.setProperty('--lrc-shrink-to', String(timing.shrinkTo)); lrcLayer.style.setProperty('--lrc-blur-px', timing.blurPx + 'px');
lrcLayer.style.paddingBottom = '0px';
lrcLayer.style.paddingTop = '0px';
@@ -373,6 +426,9 @@
lrcNext.style.textShadow = shadow;
lrcNext.style.display = s.showNextLine === false ? 'none' : '';
// 下一句和当前句同属 .lrc-text 的兄弟节点,回缩时一起缩放更自然
lrcNext.style.transformOrigin = 'center';
applyLrcCustomCss(s.customCss || '');
}
@@ -403,6 +459,90 @@
lrcLayer.classList.add('visible');
}
// ── 入场 / 停留回缩 / 出场 状态机 ──────────────────────────
function setPhase(phase) {
if (lrc.phase === phase) return;
lrc.phase = phase;
lrcLayer.classList.remove('lrc-state-entering', 'lrc-state-hold', 'lrc-state-leaving');
if (phase) lrcLayer.classList.add('lrc-state-' + phase);
}
function clearPhaseTimer() {
if (lrc.phaseTimer) {
clearTimeout(lrc.phaseTimer);
lrc.phaseTimer = null;
}
}
function restartEntrance(cue, index) {
clearPhaseTimer();
// 关键:先把上一句残留的 hold 状态去掉并让浏览器结算掉旧的回缩过渡,
// 否则新一句会从上一次的缩小值继续,看起来像「回缩瞬间完成」或「根本不动」。
lrcLayer.classList.remove('lrc-state-entering', 'lrc-state-hold', 'lrc-state-leaving');
lrc.phase = null;
lrcText.style.transition = 'none';
lrcText.style.transform = 'scale(1)';
void lrcText.offsetWidth; // 强制重排,让上面的重置立即生效
lrcText.style.transition = '';
lrcText.style.transform = '';
// 入场 + 回缩必须塞进「到下一句的时间」里,否则回缩永远播不完
const timing = getLrcTiming(lrc.style);
const fitted = fitTimingToWindow(timing, cueWindowMs(index));
lrcLayer.style.setProperty('--lrc-in-dur', fitted.inMs + 'ms');
lrcLayer.style.setProperty('--lrc-shrink-dur', fitted.shrinkMs + 'ms');
setPhase('entering');
showLrc();
const current = cue;
lrc.phaseTimer = setTimeout(() => {
lrc.phaseTimer = null;
if (!lrc.active || lrc.cue !== current) return;
// 入场结束 -> 停留阶段(从 1 开始缓慢回缩)
setPhase('hold');
}, fitted.inMs + timing.holdMs + 20);
return fitted;
}
function beginOutro(onDone) {
clearPhaseTimer();
const timing = getLrcTiming(lrc.style);
lrc.cue = null;
if (timing.outro === 'none' || timing.outMs <= 0) {
lrcLayer.classList.remove('lrc-state-entering', 'lrc-state-hold', 'lrc-state-leaving');
lrc.phase = null;
hideLrc();
onDone();
return;
}
// 出场动画要作用在「当前可见的尺寸」上,先把回缩的 transform 固定住再切到 leaving
const computed = getComputedStyle(lrcText).transform;
lrcLayer.classList.remove('lrc-state-entering', 'lrc-state-hold', 'lrc-state-leaving');
lrc.phase = null;
lrcText.style.transition = 'none';
if (computed && computed !== 'none') lrcText.style.transform = computed;
void lrcText.offsetWidth;
lrcText.style.transition = '';
lrcText.style.transform = '';
setPhase('leaving');
lrc.phaseTimer = setTimeout(() => {
lrc.phaseTimer = null;
setPhase(null);
hideLrc();
if (onDone) onDone();
}, timing.outMs + 20);
}
function clearLrcContent() {
lrc.cue = null;
lrc.spans = null;
lrcText.textContent = '';
lrcNext.textContent = '';
}
function cueUsesKaraoke(cue) {
return !!(lrc.style && lrc.style.karaoke !== false && cue
&& Array.isArray(cue.segments) && cue.segments.length >= 2);
@@ -439,16 +579,28 @@
function renderCue(index) {
if (index === lrc.shownIndex) return;
lrc.shownIndex = index;
if (index < 0) {
// 还没到第一句:只有本次播放尚未入场过时才清空,避免打断正在进行的出场动画
const neverShown = lrc.shownIndex === -2;
lrc.shownIndex = -2;
if (neverShown) {
if (lrc.spans) {
lrc.spans = null;
lrcText.textContent = '';
lrcNext.textContent = '';
}
} else if (lrc.cue) {
lrc.cue = null;
lrc.spans = null;
lrcText.textContent = '';
lrcNext.textContent = '';
}
hideLrc();
lrc.spans = null;
lrcText.textContent = '';
lrcNext.textContent = '';
return;
}
lrc.shownIndex = index;
const cue = lrc.cues[index];
if (!cue) {
hideLrc();
@@ -466,11 +618,9 @@
// 逐字模式下下一句也可能带增强标签,这里统一只显示纯文本
lrcNext.textContent = next ? next.text : '';
// 重启入场动画
lrcText.style.animation = 'none';
void lrcText.offsetWidth;
lrcText.style.animation = '';
showLrc();
lrc.cue = cue;
lrc.cueIndex = index;
restartEntrance(cue, index);
}
function currentElapsed() {
@@ -483,7 +633,8 @@
const elapsed = currentElapsed();
const last = lrc.cues[lrc.cues.length - 1];
if (last && elapsed > last.t + lrc.tailMs) {
hideLrc();
// 整段结束:播放淡出/出场动画
if (lrc.cue) beginOutro();
return;
}
renderCue(findCueIndex(elapsed));
@@ -505,19 +656,25 @@
function applyLrcState(state) {
if (!state || !state.playing) {
const reason = state && state.reason;
const wasActive = lrc.active;
lrc.active = false;
stopTicker();
hideLrc();
lrc.shownIndex = -2;
if (reason === 'stopped' || reason === 'ended' || reason === 'cleared' || reason === 'preset-deleted') {
setTimeout(() => {
if (!lrc.active) {
lrc.spans = null;
lrcText.textContent = '';
lrcNext.textContent = '';
}
}, 240);
if (!wasActive) {
// 其他客户端已经播完并淡出,这里直接收尾
clearPhaseTimer();
setPhase(null);
hideLrc();
clearLrcContent();
lrc.shownIndex = -2;
return;
}
if (lrc.cue) {
// 播放被停止/清空:走一遍出场动画再清空
beginOutro(clearLrcContent);
} else {
clearLrcContent();
}
lrc.shownIndex = -2;
return;
}