// SPDX-License-Identifier: AGPL-3.0-only // Ported from Folia (https://github.com/chthollyphile/folia-major, v0.8.2), // src/components/visualizer/classic/Visualizer.tsx: 流光, the baseline style the others borrow their timing from. // One line at a time; every word gets a loose place of its own (a small offset, turn and size seeded by the // line's start time, so the same lyric never reshuffles) and animates by itself through waiting, active, passed. // // Changed from Folia: React became a plain renderer; the stage's box stands in for the window; the settings are // Folia's (word rotation, breathing float, word spacing), without the legacy layout and the optional overlap fit. import { resolveThemeFontWeight, type Line } from '../types'; import { resolveWordColor } from '../color'; import { GlowWord, LineContainer, type GlowWordConfig, getGlowWordLineContainerMotion, measureTextWidth, resolveGlowWordRenderProfile } from '../glowWord'; import { resolveLyricTextDirection } from '../lyrics/graphemes'; import { buildDisplayWordsFromLayoutUnits, buildPostLyricLayoutUnits } from '../lyrics/layoutUnits'; import { isInterludeLine } from '../lyrics/timeline'; import type { StyleContext, StyleRenderer } from './registry'; interface WordLayoutConfig extends GlowWordConfig { marginRight: string; alignSelf: string; } interface ShownLine { container: LineContainer; words: GlowWord[]; } const JUSTIFY = { start: 'flex-start', center: 'center', end: 'flex-end', around: 'space-around', between: 'space-between' } as const; const ALIGN = { start: 'flex-start', center: 'center', end: 'flex-end' } as const; export const createClassic = (context: StyleContext): StyleRenderer => { const { layer, theme, width, rem } = context; const tuning = context.tunings.classic; const WORD_SPACING = Math.min(2, Math.max(0, tuning.wordSpacing)); const FONT_SCALE = context.fontScale; const floatAmount = Math.min(2, Math.max(0, tuning.breathingFloatMultiplier)); const intensity = theme.animationIntensity; const isChaotic = intensity === 'chaotic'; const isCalm = intensity === 'calm'; // The lyric area, with a small breathing float so the screen is never fully still between words. const area = document.createElement('div'); area.className = floatAmount > 0 ? 'folia-area folia-area--float' : 'folia-area'; const float = { calm: { distance: 10, duration: 8.5 }, normal: { distance: 14, duration: 7 }, chaotic: { distance: 18, duration: 5.8 } }[intensity]; area.style.setProperty('--folia-float', `${float.distance * floatAmount}px`); area.style.animationDuration = `${float.duration}s`; layer.append(area); const mainFontSize = `clamp(${(2.25 * FONT_SCALE).toFixed(3)}rem, ${(6 * FONT_SCALE).toFixed(3)}cqw, ${(4.5 * FONT_SCALE).toFixed(3)}rem)`; const pxFontSize = Math.max(2.25 * FONT_SCALE * rem, Math.min((6 * FONT_SCALE * width) / 100, 4.5 * FONT_SCALE * rem)); const fontWeight = resolveThemeFontWeight(theme, 700); let shown: ShownLine | null = null; const leaving = new Set(); const build = (line: Line): ShownLine => { const displayWords = buildDisplayWordsFromLayoutUnits(buildPostLyricLayoutUnits(line, { semantic: true, sticky: true })); const renderProfile = resolveGlowWordRenderProfile(line); const seed = line.startTime; const isInterlude = isInterludeLine(line); // How the whole line sits in the field before the words' own offsets. const justifyOptions = isCalm ? (['center'] as const) : (['start', 'center', 'end', 'around', 'between'] as const); const alignOptions = isCalm ? (['center'] as const) : (['start', 'center', 'end'] as const); const justify = isInterlude ? 'center' : justifyOptions[Math.floor(seed % justifyOptions.length)]; const align = alignOptions[Math.floor((seed * 2) % alignOptions.length)]; const perspective = isChaotic ? 500 + (seed % 500) : 1000; const wordWidths = displayWords.map((word) => measureTextWidth(word.text, pxFontSize, theme.fontStack, fontWeight)); const baseSpread = isChaotic ? 60 : isCalm ? 0 : 20; const baseRotate = isChaotic ? 30 : isCalm ? 0 : 5; // In an RTL line the next word sits to the left, so its x offset closes the gap the other way. const rtl = resolveLyricTextDirection(line.fullText) === 'rtl'; // A tiny deterministic generator, so every word gets its own reproducible offsets. const randomOf = (index: number) => (offset: number) => { const x = Math.sin(seed + index + offset) * 10000; return x - Math.floor(x); }; const placementOf = (index: number) => { const random = randomOf(index); return { width: wordWidths[index] ?? 0, scale: isChaotic ? 0.8 + random(4) * 0.6 : 1.1 + random(4) * 0.2, rotate: tuning.enableWordRotation ? (random(3) - 0.5) * baseRotate * 2 : 0, passedRotate: tuning.enableWordRotation ? (random(8) - 0.5) * 45 : 0, x: (random(1) - 0.5) * baseSpread * 2, y: (random(2) - 0.5) * baseSpread * 2, }; }; const configs: WordLayoutConfig[] = displayWords.map((word, index) => { const random = randomOf(index); const id = `${word.text}-${index}-${seed}`; if (isInterlude) { return { id, x: 0, y: (random(2) - 0.5) * 15, rotate: 0, scale: 1.5, marginRight: '3rem', alignSelf: 'center', passedRotate: 0 }; } const placement = placementOf(index); // The margin keeps neighbours from overlapping while one of them is scaled up and moved. const activeScale = placement.scale * 1.4; let nextWidth = 0; let nextScale = 1; let nextX = 0; if (index + 1 < displayWords.length) { const next = placementOf(index + 1); nextScale = next.scale * 1.4; nextX = next.x; nextWidth = next.width; } const gap = 0.05 * pxFontSize; const halfOverflow = placement.width * (activeScale - 1) / 2; const halfOverflowNext = nextWidth * (nextScale - 1) / 2; const xOffsetDiff = rtl ? nextX - placement.x : placement.x - nextX; const calculatedMargin = (halfOverflow + halfOverflowNext + xOffsetDiff + gap) * WORD_SPACING; const minMargin = (isChaotic ? 0.08 * pxFontSize : 0.12 * pxFontSize) * WORD_SPACING; return { id, x: placement.x, y: placement.y, rotate: placement.rotate, scale: placement.scale, marginRight: `${Math.max(minMargin, calculatedMargin).toFixed(1)}px`, alignSelf: isChaotic && random(6) > 0.7 ? (random(7) > 0.5 ? 'flex-start' : 'flex-end') : 'auto', passedRotate: placement.passedRotate, }; }); const slot = document.createElement('div'); slot.className = 'folia-slot'; const box = document.createElement('div'); box.className = 'folia-classic__line'; box.dir = resolveLyricTextDirection(line.fullText); box.style.justifyContent = JUSTIFY[justify]; box.style.alignItems = ALIGN[align]; box.style.perspective = `${perspective}px`; slot.append(box); const words = displayWords.map((word, index) => { const config = configs[index]; const glowWord = new GlowWord({ word, config, theme, baseColor: theme.primaryColor, activeColor: resolveWordColor(word.text, theme.wordColors, theme.accentColor), renderProfile, isChorus: line.isChorus, fontSize: mainFontSize, enableWordRotation: tuning.enableWordRotation, layoutStyle: { marginInlineEnd: config.marginRight, alignSelf: config.alignSelf }, }); box.append(glowWord.el); return glowWord; }); const container = new LineContainer(box, getGlowWordLineContainerMotion(renderProfile), slot); area.append(slot); return { container, words }; }; const retire = (line: ShownLine) => { leaving.add(line); void line.container.leave().then(() => { leaving.delete(line); for (const word of line.words) word.destroy(); }); }; return { setLine(line, _index, time) { if (shown) retire(shown); shown = null; if (!line) return; shown = build(line); for (const word of shown.words) word.update(time); void shown.container.enter(); }, frame(time) { if (shown) for (const word of shown.words) word.update(time); for (const line of leaving) for (const word of line.words) word.update(time); }, destroy() { for (const line of [...leaving, ...(shown ? [shown] : [])]) { for (const word of line.words) word.destroy(); line.container.destroy(); } leaving.clear(); shown = null; area.remove(); }, }; };