// SPDX-License-Identifier: AGPL-3.0-only // Ported from Folia (https://github.com/chthollyphile/folia-major, v0.8.2), // src/components/visualizer/backgrounds/common/GeometricBackground.tsx (+ .css) and the base colour layer of // backgrounds/common/entry.tsx: Folia's default background. Fifteen shapes drift and turn on loops of their // own, each kind swelling with one band of the music; twenty specks rise; a vignette closes the corners. // // Changed from Folia: no cover-colour layer (the site's tracks have no covers) and no theme icons (they come // from Folia's AI themes); the band springs are stepped here instead of by framer-motion. import type { AudioLevels, Theme } from './types'; type ShapeType = 'circle' | 'square' | 'triangle' | 'cross'; type ScaleKey = 'bass' | 'lowMid' | 'mid' | 'treble'; const SCALE_KEY: Record = { circle: 'bass', square: 'lowMid', triangle: 'mid', cross: 'treble' }; const CLIP_PATH: Record = { circle: 'none', square: 'none', triangle: 'polygon(50% 0%, 0% 100%, 100% 100%)', cross: 'polygon(20% 0%, 0% 20%, 30% 50%, 0% 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0%, 50% 30%)', }; interface Shape { type: ShapeType; face: HTMLDivElement; filled: boolean; opacity: number; } /** A value following another like framer's useSpring: stiffness 300, damping 30. */ class BandSpring { value = 0; #velocity = 0; step(target: number, delta: number) { let left = Math.min(delta, 0.1); while (left > 0) { const slice = Math.min(left, 1 / 240); this.#velocity += (300 * (target - this.value) - 30 * this.#velocity) * slice; this.value += this.#velocity * slice; left -= slice; } return this.value; } } /** Folia maps a band level of 10..200 (of 255) onto a scale of 0.95..1.45, clamped. */ const bandScale = (level: number) => 0.95 + Math.min(1, Math.max(0, (level - 10) / 190)) * 0.5; export class GeometricBackground { readonly el: HTMLDivElement; #base: HTMLDivElement; #scene: HTMLDivElement | null = null; #shapes: Shape[] = []; #particles: HTMLDivElement[] = []; #theme: Theme; #springs: Record = { bass: new BandSpring(), lowMid: new BandSpring(), mid: new BandSpring(), treble: new BandSpring() }; #written: Record = { bass: 0, lowMid: 0, mid: 0, treble: 0 }; #paused = false; #hideShapes = false; constructor(theme: Theme) { this.#theme = theme; this.el = document.createElement('div'); this.el.className = 'folia-backdrop'; this.el.setAttribute('aria-hidden', 'true'); this.#base = document.createElement('div'); this.#base.className = 'folia-backdrop__base'; this.#base.style.backgroundColor = theme.backgroundColor; this.el.append(this.#base); } /** A new song lays the shapes out afresh; the old ones fade out under the new ones. */ build() { const previous = this.#scene; if (previous) { previous.style.opacity = '0'; window.setTimeout(() => previous.remove(), 650); } const scene = this.#scene = document.createElement('div'); scene.className = 'folia-geo'; scene.classList.toggle('folia-geo--paused', this.#paused); scene.classList.toggle('folia-geo--bare', this.#hideShapes); const shapeTypes: ShapeType[] = ['circle', 'square', 'triangle', 'cross']; const field = document.createElement('div'); field.className = 'folia-geo__field'; this.#shapes = Array.from({ length: 15 }, () => { const type = shapeTypes[Math.floor(Math.random() * shapeTypes.length)]; // Folia draws six of the fifteen as theme icons, on shorter loops; without icons they stay shapes // and keep those loops. const quick = Math.random() > 0.7; const size = 40 + Math.random() * 100; const drift = document.createElement('div'); drift.className = 'folia-geo__drift'; drift.style.left = `${Math.random() * 100}%`; drift.style.top = `${Math.random() * 100}%`; drift.style.width = drift.style.height = `${size}px`; drift.style.animationDuration = `${quick ? 20 + Math.random() * 20 : 30 + Math.random() * 30}s`; drift.style.animationDelay = `${Math.random() * 5}s`; drift.style.setProperty('--geometric-dir', Math.random() > 0.5 ? '-1' : '1'); drift.style.setProperty('--geometric-rotate', `${Math.random() * 360}deg`); const face = document.createElement('div'); face.className = 'folia-geo__face'; const shape: Shape = { type, face, filled: Math.random() < 0.3, opacity: 0.11 + Math.random() * 0.08 }; face.style.opacity = String(shape.opacity); face.style.borderRadius = type === 'circle' ? '50%' : '0%'; face.style.clipPath = CLIP_PATH[type]; drift.append(face); field.append(drift); return shape; }); this.#particles = Array.from({ length: 20 }, () => { const size = Math.random() * 4 + 1; const particle = document.createElement('div'); particle.className = 'folia-geo__rise'; particle.style.width = particle.style.height = `${size}px`; particle.style.left = `${Math.random() * 100}%`; particle.style.top = `${Math.random() * 100}%`; particle.style.animationDuration = `${15 + Math.random() * 20}s`; particle.style.animationDelay = `${Math.random() * 10}s`; particle.style.setProperty('--geometric-peak', String(Math.random() * 0.3)); field.append(particle); return particle; }); const vignette = document.createElement('div'); vignette.className = 'folia-geo__vignette'; scene.append(field, vignette); this.#paint(); this.#written = { bass: 0, lowMid: 0, mid: 0, treble: 0 }; this.el.append(scene); } setTheme(theme: Theme) { this.#theme = theme; this.#base.style.backgroundColor = theme.backgroundColor; this.#paint(); } /** A pause holds every loop where it is. */ setPaused(paused: boolean) { this.#paused = paused; this.#scene?.classList.toggle('folia-geo--paused', paused); } /** A style may ask for the bare colour and vignette (浮名 does). */ setHideShapes(hide: boolean) { this.#hideShapes = hide; this.#scene?.classList.toggle('folia-geo--bare', hide); } /** Each kind of shape follows its band: circles the bass, squares the low mids, triangles the mids, crosses the treble. */ frame(audio: AudioLevels, delta: number) { if (this.#hideShapes) return; for (const key of ['bass', 'lowMid', 'mid', 'treble'] as const) { const scale = bandScale(this.#springs[key].step(audio[key], delta)); if (Math.abs(scale - this.#written[key]) < 0.002) continue; this.#written[key] = scale; const transform = `scale(${scale.toFixed(3)})`; for (const shape of this.#shapes) if (SCALE_KEY[shape.type] === key) shape.face.style.transform = transform; } } destroy() { this.el.remove(); } #paint() { const theme = this.#theme; for (const shape of this.#shapes) { const stroke = (shape.type === 'circle' || shape.type === 'square') && !shape.filled; shape.face.style.border = stroke ? `1px solid ${theme.secondaryColor}` : 'none'; shape.face.style.backgroundColor = stroke ? 'transparent' : theme.secondaryColor; } for (const particle of this.#particles) particle.style.backgroundColor = theme.accentColor; } }