Files
stemengine_old/frontend/src/components/visualizers/MuonVisualizerCanvas.tsx
T
2026-04-09 08:30:51 +09:00

145 lines
5.5 KiB
TypeScript

import { useEffect, useRef } from 'react'
import type { MutableRefObject } from 'react'
import type { AudioFeatures } from '../../hooks/useAudioFeatures'
import type { VisualizerPalette } from '../../hooks/useVisualizerPalette'
interface MuonVisualizerCanvasProps {
featuresRef: MutableRefObject<AudioFeatures>
paletteRef: MutableRefObject<VisualizerPalette>
width: number
height: number
fpsCap?: 30 | 60 | 120
quality?: 'low' | 'balanced' | 'high'
settings?: {
radius_multiplier?: number
aperture?: number
spacing?: number
}
}
type Particle = { angle: number; radius: number; speed: number; lane: number }
const PARTICLE_COUNT = 220
function clamp01(v: number): number {
return Math.max(0, Math.min(1, v))
}
export default function MuonVisualizerCanvas({ featuresRef, paletteRef, width, height, fpsCap = 60, quality = 'balanced', settings }: MuonVisualizerCanvasProps) {
const canvasRef = useRef<HTMLCanvasElement>(null)
useEffect(() => {
const canvas = canvasRef.current
if (!canvas) return
const ctx = canvas.getContext('2d')
if (!ctx) return
const particles: Particle[] = Array.from({ length: PARTICLE_COUNT }, (_, i) => ({
angle: (i / PARTICLE_COUNT) * Math.PI * 2,
radius: 0.2 + (i % 12) * 0.08,
speed: 0.002 + (i % 9) * 0.00045,
lane: i % 8,
}))
let raf = 0
// Correction B fix: radiusMultiplier cycles through presets on beats rather than
// being mapped directly from drum intensity each frame. We lerp toward a preset
// target, snapping to the next preset on each detected beat — equivalent to
// wavePresetController triggering a GSAP transition in the full Three.js port.
const RADIUS_PRESETS = [0.90, 0.95, 1.00, 1.05, 1.10, 0.85, 1.15, 1.02]
let radiusTarget = 1.0
let radiusCurrent = 1.0
let lastPresetAt = 0
let presetIdx = 0
let lastFrameAt = 0
const cap = fpsCap === 30 || fpsCap === 120 ? fpsCap : 60
const minFrameMs = 1000 / cap
const render = (ts: number) => {
raf = window.requestAnimationFrame(render)
if (lastFrameAt && ts - lastFrameAt < minFrameMs) return
lastFrameAt = ts
const w = Math.max(1, Math.round(canvas.clientWidth || 0), Math.round(width || 0))
const h = Math.max(1, Math.round(canvas.clientHeight || 0), Math.round(height || 0))
if (canvas.width !== w || canvas.height !== h) {
canvas.width = w
canvas.height = h
}
const f = featuresRef.current
const p = paletteRef.current
const drum = clamp01(f.drumIntensity)
const treble = clamp01(f.treble)
const now = ts
const idle = 0.5 + 0.5 * Math.sin(now * 0.0012)
const radiusScale = Math.max(0.2, Number(settings?.radius_multiplier ?? 1))
const apertureScale = Math.max(0.2, Number(settings?.aperture ?? 1))
const spacingScale = Math.max(0.2, Number(settings?.spacing ?? 1))
// Bug 1 fix: hue animates via drum intensity when audio is playing, and falls
// back to a slow time-based drift when silent so the visual never freezes on a
// single colour (original hueControl bug: hue was always 0 during playback).
// Correction A fix: treble drift is capped at ±15° to stay within the manifest
// palette colour family (original Integration Plan used treble * 40 / 120 which
// rotated far outside the intended hue family).
const hue = drum > 0.01
? ((p.seedHue + drum * 20 + treble * 15) + 360) % 360
: ((p.seedHue + Math.sin(now * 0.00018) * 15) + 360) % 360
// Correction B fix: advance the preset index on each beat and lerp toward the
// new target — mirrors wavePresetController / GSAP transition in the Three.js port.
if (f.beatDetected && now - lastPresetAt > 200) {
presetIdx = (presetIdx + 1) % RADIUS_PRESETS.length
radiusTarget = RADIUS_PRESETS[presetIdx] + drum * 0.25
lastPresetAt = now
}
radiusCurrent += (radiusTarget - radiusCurrent) * 0.07
const radiusMultiplier = (radiusCurrent + idle * 0.12) * radiusScale
const aperture = (1.55 + drum * 1.9 + idle * 0.24) * apertureScale
const spacing = (0.015 + (1 - drum) * 0.014) * spacingScale
ctx.clearRect(0, 0, w, h)
ctx.fillStyle = p.backgroundColor
ctx.fillRect(0, 0, w, h)
const cx = w / 2
const cy = h / 2
const base = Math.min(w, h) * 0.2
const glow = 14 + drum * 28
const particleStride = quality === 'low' ? 2 : 1
ctx.globalCompositeOperation = 'lighter'
for (let i = 0; i < particles.length; i += particleStride) {
const pt = particles[i]
pt.angle += pt.speed + drum * 0.02
const ring = base + pt.lane * (spacing * Math.min(w, h) * 5)
const r = ring * radiusMultiplier * (1 + Math.sin(pt.angle * aperture) * 0.16)
const x = cx + Math.cos(pt.angle) * r
const y = cy + Math.sin(pt.angle * aperture) * (r * 0.62)
const size = 1.75 + drum * 3.0 + (i % 4) * 0.35
const alpha = 0.34 + drum * 0.42
ctx.beginPath()
ctx.fillStyle = `hsla(${hue}, 92%, 64%, ${alpha})`
ctx.shadowColor = p.highlightColor
ctx.shadowBlur = glow
ctx.arc(x, y, size, 0, Math.PI * 2)
ctx.fill()
}
ctx.globalCompositeOperation = 'source-over'
ctx.shadowBlur = 0
}
raf = window.requestAnimationFrame(render)
return () => {
window.cancelAnimationFrame(raf)
}
}, [featuresRef, paletteRef, width, height, fpsCap, quality, settings])
return <canvas ref={canvasRef} className="h-full w-full" aria-hidden="true" />
}