import React, { useEffect, useRef } from 'react'; import './CinematicTokenFactory.css'; const mulberry32 = (seed) => { let value = seed >>> 0; return () => { value += 0x6d2b79f5; let result = value; result = Math.imul(result ^ (result >>> 15), result | 1); result ^= result + Math.imul(result ^ (result >>> 7), result | 61); return ((result ^ (result >>> 14)) >>> 0) / 4294967296; }; }; const CinematicTokenFactory = () => { const sceneRef = useRef(null); const canvasRef = useRef(null); useEffect(() => { const scene = sceneRef.current; const canvas = canvasRef.current; if (!scene || !canvas) return undefined; const context = canvas.getContext('2d'); const motionQuery = window.matchMedia('(prefers-reduced-motion: reduce)'); let reducedMotion = motionQuery.matches; const random = mulberry32(0x746f6b65); const particles = Array.from({ length: 48 }, (_, index) => ({ x: random(), y: random(), depth: 0.35 + random() * 0.9, radius: index % 8 === 0 ? 3 + random() * 2.4 : 0.6 + random() * 1.5, speed: 0.000018 + random() * 0.000035, phase: random() * Math.PI * 2, token: index % 8 === 0, })); let width = 0; let height = 0; let dpr = 1; let frameId = 0; let visible = !document.hidden; let pointerX = 0; let pointerY = 0; let smoothX = 0; let smoothY = 0; let lastFrameTime = 0; const resize = () => { width = window.innerWidth; height = window.innerHeight; dpr = Math.min(window.devicePixelRatio || 1, 1.5); canvas.width = Math.round(width * dpr); canvas.height = Math.round(height * dpr); canvas.style.width = `${width}px`; canvas.style.height = `${height}px`; context.setTransform(dpr, 0, 0, dpr, 0, 0); }; const onPointerMove = (event) => { pointerX = event.clientX / Math.max(width, 1) - 0.5; pointerY = event.clientY / Math.max(height, 1) - 0.5; }; const drawEnergyStream = (time, y, color, speed, amplitude) => { context.save(); context.globalCompositeOperation = 'screen'; context.lineWidth = 1.15; context.strokeStyle = color; context.shadowColor = color; context.shadowBlur = 10; context.beginPath(); for (let x = -40; x <= width + 40; x += 18) { const progress = x / Math.max(width, 1); const wave = Math.sin(progress * 8 + time * speed) * amplitude; const perspective = (progress - 0.52) ** 2 * height * 0.08; const py = height * y + wave + perspective; if (x === -40) context.moveTo(x, py); else context.lineTo(x, py); } context.stroke(); for (let i = 0; i < 6; i += 1) { const progress = (time * speed * 0.13 + i / 6) % 1; const x = progress * width; const wave = Math.sin(progress * 8 + time * speed) * amplitude; const py = height * y + wave + (progress - 0.52) ** 2 * height * 0.08; const glow = context.createRadialGradient(x, py, 0, x, py, 13); glow.addColorStop(0, 'rgba(255,255,255,.95)'); glow.addColorStop(0.25, color); glow.addColorStop(1, 'rgba(0,0,0,0)'); context.fillStyle = glow; context.beginPath(); context.arc(x, py, 13, 0, Math.PI * 2); context.fill(); } context.restore(); }; const drawParticle = (particle, time, deltaMs) => { if (!reducedMotion) { particle.x += particle.speed * particle.depth * deltaMs; particle.y += Math.sin(time * 0.00045 + particle.phase) * 0.000025 * (deltaMs / 16.67); if (particle.x > 1.08) particle.x = -0.08; } const x = particle.x * width + smoothX * 24 * particle.depth; const y = particle.y * height + smoothY * 14 * particle.depth; const pulse = 0.65 + Math.sin(time * 0.0012 + particle.phase) * 0.35; context.save(); context.globalCompositeOperation = 'screen'; if (particle.token) { const radius = particle.radius * (0.85 + pulse * 0.2); context.translate(x, y); context.rotate(Math.sin(time * 0.00035 + particle.phase) * 0.35); context.shadowColor = '#ffc861'; context.shadowBlur = 14; context.strokeStyle = `rgba(255, 202, 98, ${0.45 + pulse * 0.4})`; context.lineWidth = 1.1; context.beginPath(); context.arc(0, 0, radius * 1.65, 0, Math.PI * 2); context.stroke(); context.beginPath(); context.moveTo(-radius * 0.65, -radius * 0.15); context.lineTo(radius * 0.65, -radius * 0.15); context.moveTo(-radius * 0.65, radius * 0.35); context.lineTo(radius * 0.65, radius * 0.35); context.stroke(); } else { context.shadowColor = '#72e8ff'; context.shadowBlur = 9; context.fillStyle = `rgba(125, 231, 255, ${0.18 + pulse * 0.5})`; context.beginPath(); context.arc(x, y, particle.radius * pulse, 0, Math.PI * 2); context.fill(); } context.restore(); }; const render = (time = 0) => { const deltaMs = lastFrameTime ? Math.min(Math.max(time - lastFrameTime, 0), 50) : 16.67; lastFrameTime = time; context.clearRect(0, 0, width, height); smoothX += (pointerX - smoothX) * 0.035; smoothY += (pointerY - smoothY) * 0.035; scene.style.setProperty('--factory-parallax-x', `${smoothX * -18}px`); scene.style.setProperty('--factory-parallax-y', `${smoothY * -10}px`); drawEnergyStream(time * 0.001, 0.68, 'rgba(255, 181, 73, .58)', 1.2, 4); drawEnergyStream(time * 0.001, 0.73, 'rgba(47, 211, 255, .62)', 1.65, 6); particles.forEach((particle) => drawParticle(particle, time, deltaMs)); if (!reducedMotion && visible) frameId = requestAnimationFrame(render); }; const onVisibilityChange = () => { visible = !document.hidden; if (visible && !reducedMotion) { cancelAnimationFrame(frameId); lastFrameTime = 0; frameId = requestAnimationFrame(render); } }; const onResize = () => { resize(); if (reducedMotion) { lastFrameTime = 0; render(performance.now()); } }; const onMotionPreferenceChange = (event) => { reducedMotion = event.matches; cancelAnimationFrame(frameId); lastFrameTime = 0; render(performance.now()); }; resize(); render(); window.addEventListener('resize', onResize); window.addEventListener('pointermove', onPointerMove, { passive: true }); document.addEventListener('visibilitychange', onVisibilityChange); motionQuery.addEventListener('change', onMotionPreferenceChange); return () => { cancelAnimationFrame(frameId); window.removeEventListener('resize', onResize); window.removeEventListener('pointermove', onPointerMove); document.removeEventListener('visibilitychange', onVisibilityChange); motionQuery.removeEventListener('change', onMotionPreferenceChange); }; }, []); return (