// SPDX-License-Identifier: AGPL-3.0-only // Ported from Folia (https://github.com/chthollyphile/folia-major, v0.8.2), src/components/visualizer/GlowWord.tsx, // glowWordTiming.ts, glowWordVariants.ts, wordGlow.ts and glowWordOverlap.ts (the width measure only). // // The word shared by 流光 (classic) and 云阶 (partita). A word is waiting, active or passed; only a change of // state starts motion. Three layers: the container (place, size, turn), the body (colour, blur) and a glow of // transparent letters with a halo whose opacity carries the sweep, one letter after another. // // Changed from Folia: React and framer-motion variants became a class over ./motion; the halo is always drawn // with text-shadow (Folia's Linux drop-shadow path is left out). import { resolveThemeFontWeight, type Line, type Theme, type Word } from './types'; import { buildWordGraphemeTimings, type GraphemeTiming } from './lyrics/graphemes'; import { getLineRenderEndTime, getLineRenderHints } from './lyrics/renderHints'; import { Motion, type Transitions } from './motion'; export type WordStatus = 'waiting' | 'active' | 'passed'; export interface GlowWordConfig { id: string; x: number; y: number; rotate: number; scale: number; passedRotate: number; } export interface GlowWordRenderProfile { renderHints: NonNullable | null; lineRenderEndTime: number; lineTransitionMode: 'normal' | 'fast' | 'none'; wordRevealMode: 'normal' | 'fast' | 'instant'; wordLookahead: number; } /** The line box of a word, in em. */ export const GLOW_WORD_LINE_HEIGHT = 1.22; /** An active word grows to its config scale times this. */ export const GLOW_WORD_ACTIVE_SCALE = 1.4; export const resolveGlowWordRenderProfile = (line: Line): GlowWordRenderProfile => { const renderHints = getLineRenderHints(line); const wordRevealMode = renderHints?.wordRevealMode ?? 'normal'; return { renderHints, lineRenderEndTime: getLineRenderEndTime(line), lineTransitionMode: renderHints?.lineTransitionMode ?? 'normal', wordRevealMode, wordLookahead: wordRevealMode === 'instant' ? 0.03 : wordRevealMode === 'fast' ? 0.08 : 0.15, }; }; export const getGlowWordActiveEndTime = (word: Word, renderProfile: GlowWordRenderProfile) => { if (renderProfile.wordRevealMode === 'instant') return renderProfile.lineRenderEndTime; if (renderProfile.wordRevealMode === 'fast') { return Math.min(renderProfile.lineRenderEndTime, Math.max(word.endTime, word.startTime + 0.12)); } return word.endTime; }; export const getGlowWordDisplayDuration = (word: Word, renderProfile: GlowWordRenderProfile) => { const activeEndTime = getGlowWordActiveEndTime(word, renderProfile); const minDuration = renderProfile.wordRevealMode === 'instant' ? 0.08 : renderProfile.wordRevealMode === 'fast' ? 0.12 : 0.1; return Math.max(activeEndTime - word.startTime, minDuration); }; export const statusAt = (time: number, startTime: number, activeEndTime: number, lookahead: number): WordStatus => { if (time >= startTime - lookahead && time <= activeEndTime) return 'active'; if (time > activeEndTime) return 'passed'; return 'waiting'; }; type LinePose = { opacity: number; scale: number; blur: number }; export interface LineContainerMotion { initial: LinePose; animate: LinePose; /** How the line comes in; left out, each value moves by framer's default. */ enter?: Transitions<'opacity' | 'scale' | 'blur'>; exit: LinePose; leave: Transitions<'opacity' | 'scale' | 'blur'>; } /** How a whole line comes in and goes out, by how long it lasts. */ export const getGlowWordLineContainerMotion = (renderProfile: GlowWordRenderProfile | null): LineContainerMotion => { if (renderProfile?.lineTransitionMode === 'none') { return { initial: { opacity: 1, scale: 1, blur: 0 }, animate: { opacity: 1, scale: 1, blur: 0 }, exit: { opacity: 0, scale: 1.02, blur: 6 }, leave: { duration: 0.12, ease: 'easeOut' }, }; } if (renderProfile?.lineTransitionMode === 'fast') { return { initial: { opacity: 0.35, scale: 0.96, blur: 4 }, animate: { opacity: 1, scale: 1, blur: 0 }, enter: { duration: 0.16, ease: 'easeOut' }, exit: { opacity: 0, scale: 1.04, blur: 10 }, leave: { duration: 0.16, ease: 'easeInOut' }, }; } return { initial: { opacity: 0, scale: 0.9, blur: 10 }, animate: { opacity: 1, scale: 1, blur: 0 }, exit: { opacity: 0, scale: 1.1, blur: 20 }, leave: { duration: 0.3 }, }; }; /** A line's box with its entrance and exit: opacity, scale and blur on one node. */ export class LineContainer { readonly el: HTMLDivElement; #motion: Motion<'opacity' | 'scale' | 'blur'>; #plan: LineContainerMotion; #owner: HTMLElement; /** `owner`: the node to take out of the page when the line has left, when that is not the animated box itself. */ constructor(el: HTMLDivElement, plan: LineContainerMotion, owner: HTMLElement = el) { this.el = el; this.#plan = plan; this.#owner = owner; let written = ''; this.#motion = new Motion(plan.initial, (values) => { const blur = values.blur as number; const next = `${values.opacity}|${values.scale}|${blur}`; if (next === written) return; written = next; el.style.opacity = String(values.opacity); el.style.transform = `scale(${values.scale})`; el.style.filter = blur > 0.05 ? `blur(${blur.toFixed(2)}px)` : 'none'; }); } enter() { return this.#motion.to(this.#plan.animate, this.#plan.enter); } /** Leaves and removes itself. `hidden`: the text was switched off rather than replaced, so it only fades. */ async leave(hidden = false) { if (hidden) await this.#motion.to({ opacity: 0 }, { duration: 0.2, ease: 'easeOut' }); else await this.#motion.to(this.#plan.exit, this.#plan.leave); this.destroy(); } destroy() { this.#motion.destroy(); this.#owner.remove(); } } let measureCanvas: HTMLCanvasElement | null = null; /** The width of a word in a font, from a 2D canvas. */ export const measureTextWidth = (text: string, pxSize: number, fontStack: string, fontWeight: number, italic = false): number => { measureCanvas ??= document.createElement('canvas'); const context = measureCanvas.getContext('2d'); if (!context) return text.length * pxSize * 0.65; context.font = `${italic ? 'italic ' : ''}${fontWeight} ${pxSize}px ${fontStack}`; return context.measureText(text).width; }; /** text-shadow halo radii per reveal mode: [inner, outer]. */ const glowRadii = (wordRevealMode: string): [number, number] => ( wordRevealMode === 'instant' ? [14, 24] : wordRevealMode === 'fast' ? [18, 32] : [20, 40] ); export interface GlowWordOptions { word: Word; config: GlowWordConfig; theme: Theme; baseColor: string; activeColor: string; renderProfile: GlowWordRenderProfile; isChorus?: boolean; /** A CSS length for the word, as the style wrote it (a clamp of rem and container units). */ fontSize: string; enableWordRotation?: boolean; /** Spacing the style gives the word in its row. */ layoutStyle?: Partial>; } export class GlowWord { readonly el: HTMLDivElement; #options: GlowWordOptions; #status: WordStatus = 'waiting'; #duration: number; #activeEndTime: number; #layout: Motion<'opacity' | 'scale' | 'x' | 'y' | 'rotate'>; #body: Motion<'color' | 'blur'>; #glows: { motion: Motion<'opacity'>; timing?: GraphemeTiming; index: number }[] = []; #total: number; #ripple?: { el: HTMLSpanElement; motion: Motion<'scale' | 'opacity'> }; #rippleScale = 1.5 + Math.random() * 2; constructor(options: GlowWordOptions) { this.#options = options; const { word, config, theme, baseColor, activeColor, renderProfile } = options; this.#duration = getGlowWordDisplayDuration(word, renderProfile); this.#activeEndTime = getGlowWordActiveEndTime(word, renderProfile); const el = this.el = document.createElement('div'); el.className = 'folia-word'; el.style.fontSize = options.fontSize; el.style.fontWeight = String(resolveThemeFontWeight(theme, 700)); el.style.lineHeight = String(GLOW_WORD_LINE_HEIGHT); if (options.layoutStyle?.marginInlineEnd) el.style.marginInlineEnd = options.layoutStyle.marginInlineEnd; if (options.layoutStyle?.alignSelf) el.style.alignSelf = options.layoutStyle.alignSelf; // Glow layer: transparent letters under the body, each with the halo already in place at full size and // hidden by opacity, so turning active animates opacity only. const glow = document.createElement('span'); glow.className = 'folia-word__glow'; glow.setAttribute('aria-hidden', 'true'); const [inner, outer] = glowRadii(renderProfile.wordRevealMode); const halo = `0 0 ${inner}px ${activeColor}, 0 0 ${outer}px ${activeColor}`; const timings = buildWordGraphemeTimings(word); const letters = timings.length > 1 ? timings : [undefined]; this.#total = letters.length; letters.forEach((timing, index) => { const span = document.createElement('span'); span.textContent = timing ? timing.char : word.text; span.style.textShadow = halo; glow.append(span); this.#glows.push({ timing, index, motion: new Motion({ opacity: 0 }, (values) => { span.style.opacity = String(values.opacity); }) }); }); const body = document.createElement('span'); body.className = 'folia-word__body'; body.textContent = word.text; el.append(glow, body); // Every word starts in its waiting pose, without motion. const rotation = options.enableWordRotation ?? true; this.#layout = new Motion({ opacity: 0, scale: 0.5, x: config.x + (Math.sin(config.y) * 100), y: config.y + (Math.cos(config.x) * 50), rotate: rotation ? config.rotate + 20 : 0, }, (values) => { el.style.opacity = String(values.opacity); el.style.transform = `translate(${(values.x as number).toFixed(2)}px, ${(values.y as number).toFixed(2)}px) scale(${(values.scale as number).toFixed(4)}) rotate(${(values.rotate as number).toFixed(3)}deg)`; }); this.#body = new Motion<'color' | 'blur'>({ color: baseColor, blur: 10 }, (values) => { const blur = values.blur as number; body.style.color = values.color as string; body.style.filter = blur > 0.05 ? `blur(${blur.toFixed(2)}px)` : 'none'; }); } get status() { return this.#status; } /** Follow the song's clock; motion starts only when the state changes. */ update(time: number) { const { word, renderProfile } = this.#options; const next = statusAt(time, word.startTime, this.#activeEndTime, renderProfile.wordLookahead); if (next === this.#status) return; this.#status = next; if (next === 'waiting') this.#toWaiting(); else if (next === 'active') this.#toActive(); else this.#toPassed(); } destroy() { this.#layout.destroy(); this.#body.destroy(); for (const glow of this.#glows) glow.motion.destroy(); this.#ripple?.motion.destroy(); } #toWaiting() { const { config, baseColor } = this.#options; const rotation = this.#options.enableWordRotation ?? true; void this.#layout.to({ opacity: 0, scale: 0.5, x: config.x + (Math.sin(config.y) * 100), y: config.y + (Math.cos(config.x) * 50), rotate: rotation ? config.rotate + 20 : 0, }, { duration: 0.4 }); void this.#body.to({ color: baseColor, blur: 10 }, { duration: 0.4 }); for (const glow of this.#glows) glow.motion.set({ opacity: 0 }); this.#dropRipple(); } #toActive() { const { config, activeColor, renderProfile, word } = this.#options; const mode = renderProfile.wordRevealMode; const duration = this.#duration; void this.#layout.to({ opacity: 1, scale: Number.isNaN(config.scale) ? 1.5 : config.scale * GLOW_WORD_ACTIVE_SCALE, x: config.x, y: config.y, rotate: config.rotate, }, { type: 'spring', stiffness: 200, damping: 20, opacity: { duration: 0.1 } }); void this.#body.to({ color: activeColor, blur: 0 }, { color: { duration: duration || 0.2, ease: 'linear' }, blur: { type: 'tween', duration: mode === 'instant' ? 0.08 : mode === 'fast' ? 0.12 : 0.2 }, }); for (const glow of this.#glows) { if (mode === 'instant') { void glow.motion.to({ opacity: [0, 1, 0] }, { duration: Math.min(duration || 0.08, 0.12), times: [0, 0.35, 1], ease: 'easeOut' }); } else if (mode === 'fast') { void glow.motion.to({ opacity: [0, 1, 0] }, { duration: Math.min(Math.max(duration || 0.12, 0.12), 0.2), times: [0, 0.4, 1], ease: 'easeInOut' }); } else if (this.#total > 1) { // Letter by letter: each letter's halo peaks early and fades over the next few letters. const single = duration / this.#total; const timing = glow.timing; const charDuration = timing ? Math.max(timing.endTime - timing.startTime, 0.001) : single; const charDelay = timing ? Math.max(0, timing.startTime - word.startTime) : single * glow.index; void glow.motion.to({ opacity: [0, 1, 0] }, { duration: charDuration * 6, times: [0, 0.3, 1], delay: charDelay, ease: 'easeInOut' }); } else { // A single letter (a CJK character): the halo holds for the length of the word. void glow.motion.to({ opacity: [0, 1, 1] }, { duration: duration || 0.1, times: [0, 0.9, 1], ease: 'easeInOut' }); } } if (this.#options.isChorus) this.#raiseRipple(); } #toPassed() { const { config, baseColor, renderProfile, theme } = this.#options; const mode = renderProfile.wordRevealMode; void this.#layout.to({ opacity: theme.animationIntensity === 'chaotic' ? 0.9 : 0.82, scale: config.scale || 1, x: config.x, y: config.y, rotate: config.rotate + config.passedRotate, }, { duration: 0.5, rotate: { duration: 5, ease: 'linear' } }); void this.#body.to({ color: baseColor, blur: 0 }, { color: { duration: mode === 'instant' ? 0.12 : mode === 'fast' ? 0.24 : 0.8, ease: 'easeInOut' }, blur: { duration: mode === 'instant' ? 0.12 : mode === 'fast' ? 0.2 : 0.5 }, }); const fade = { duration: mode === 'instant' ? 0.12 : mode === 'fast' ? 0.22 : 0.9, ease: 'easeOut' as const }; for (const glow of this.#glows) void glow.motion.to({ opacity: 0 }, fade); this.#dropRipple(); } /** A chorus word sends out one ring as it turns active. */ #raiseRipple() { this.#ripple?.motion.destroy(); this.#ripple?.el.remove(); const el = document.createElement('span'); el.className = 'folia-word__ripple'; el.style.borderColor = this.#options.activeColor; const motion = new Motion({ scale: 0.2, opacity: 0.8 }, (values) => { el.style.opacity = String(values.opacity); el.style.transform = `translate(-50%, -50%) scale(${values.scale})`; }); this.el.append(el); this.#ripple = { el, motion }; void motion.to({ scale: this.#rippleScale, opacity: 0 }, { duration: 0.5, ease: 'easeOut' }); } #dropRipple() { const ripple = this.#ripple; if (!ripple) return; this.#ripple = undefined; void ripple.motion.to({ opacity: 0 }).then(() => { ripple.motion.destroy(); ripple.el.remove(); }); } }