diff --git a/src/components/ui/Cinematic3DComparison.tsx b/src/components/ui/Cinematic3DComparison.tsx index b6b17a7..15b39fa 100644 --- a/src/components/ui/Cinematic3DComparison.tsx +++ b/src/components/ui/Cinematic3DComparison.tsx @@ -11,7 +11,6 @@ interface Cinematic3DComparisonProps { caption?: string; aspectRatio?: string; autoSlide?: boolean; - maxTilt?: number; className?: string; } @@ -23,35 +22,56 @@ export function Cinematic3DComparison({ caption, aspectRatio = 'aspect-[4/3] sm:aspect-[16/10]', autoSlide = false, - maxTilt = 6, className = '', }: Cinematic3DComparisonProps) { - const [sliderPosition, setSliderPosition] = useState(50); + const sliderPosition = useRef(50); const [isDragging, setIsDragging] = useState(false); const containerRef = useRef(null); - const glareRef = useRef(null); + const scanLabelRef = useRef(null); + + const updatePosition = useCallback((position: number) => { + sliderPosition.current = Math.max(0, Math.min(100, position)); + containerRef.current?.style.setProperty('--split', `${sliderPosition.current}%`); + if (scanLabelRef.current) scanLabelRef.current.textContent = `SCAN: ${Math.round(sliderPosition.current)}%`; + containerRef.current?.setAttribute('aria-valuenow', `${Math.round(sliderPosition.current)}`); + }, []); // Auto-slide smooth sine oscillation loop (only when explicitly requested) useEffect(() => { if (!autoSlide || isDragging) return; - let animId: number; - const start = Date.now(); - const animate = () => { - const elapsed = Date.now() - start; - // Gentle cinematic oscillation between 30% and 70% - const newPos = 50 + 20 * Math.sin(elapsed * 0.0014); - setSliderPosition(newPos); + let animId = 0; + let start = 0; + let visible = true; + let lastUpdate = 0; + const animate = (time: number) => { + if (!visible || document.hidden) { + animId = 0; + return; + } animId = requestAnimationFrame(animate); + if (time - lastUpdate < 50) return; + lastUpdate = time; + if (!start) start = time; + updatePosition(50 + 16 * Math.sin((time - start) * 0.001)); }; - + const observer = new IntersectionObserver(([entry]) => { + visible = entry.isIntersecting; + if (visible && !animId) animId = requestAnimationFrame(animate); + if (!visible && animId) { + cancelAnimationFrame(animId); + animId = 0; + } + }); + if (containerRef.current) observer.observe(containerRef.current); animId = requestAnimationFrame(animate); return () => { if (animId) cancelAnimationFrame(animId); + observer.disconnect(); }; - }, [autoSlide, isDragging]); + }, [autoSlide, isDragging, updatePosition]); // Handle position calculation const handleMove = useCallback((clientX: number) => { @@ -59,8 +79,8 @@ export function Cinematic3DComparison({ const rect = containerRef.current.getBoundingClientRect(); const x = Math.max(0, Math.min(clientX - rect.left, rect.width)); const percent = Math.max(0, Math.min(100, (x / rect.width) * 100)); - setSliderPosition(percent); - }, []); + updatePosition(percent); + }, [updatePosition]); const handleTouchMove = useCallback((e: TouchEvent) => { if (!isDragging) return; @@ -91,29 +111,17 @@ export function Cinematic3DComparison({ }; }, [isDragging, handleMouseMove, handleMouseUp, handleTouchMove]); - // 3D Spatial Tilt - Pure Direct DOM Manipulation (Zero React Re-renders on mousemove!) - const handleStageMouseMove = (e: React.MouseEvent) => { - if (!containerRef.current) return; - const rect = containerRef.current.getBoundingClientRect(); - const x = (e.clientX - rect.left) / rect.width; - const y = (e.clientY - rect.top) / rect.height; - - const rx = -(y - 0.5) * maxTilt * 2; - const ry = (x - 0.5) * maxTilt * 2; - - containerRef.current.style.transform = `rotateX(${rx.toFixed(2)}deg) rotateY(${ry.toFixed(2)}deg) scale3d(1.012, 1.012, 1.012)`; - if (glareRef.current) { - glareRef.current.style.opacity = '0.55'; - glareRef.current.style.background = `radial-gradient(circle 380px at ${(x * 100).toFixed(1)}% ${(y * 100).toFixed(1)}%, rgba(255, 255, 255, 0.35) 0%, rgba(255, 255, 255, 0.08) 50%, transparent 80%)`; - } - }; - - const handleMouseLeave = () => { - if (containerRef.current) { - containerRef.current.style.transform = 'rotateX(0deg) rotateY(0deg) scale3d(1, 1, 1)'; - } - if (glareRef.current) { - glareRef.current.style.opacity = '0'; + const handleKeyDown = (event: React.KeyboardEvent) => { + const step = event.shiftKey ? 10 : 2; + if (event.key === 'ArrowLeft' || event.key === 'ArrowDown') { + event.preventDefault(); + updatePosition(sliderPosition.current - step); + } else if (event.key === 'ArrowRight' || event.key === 'ArrowUp') { + event.preventDefault(); + updatePosition(sliderPosition.current + step); + } else if (event.key === 'Home' || event.key === 'End') { + event.preventDefault(); + updatePosition(event.key === 'Home' ? 0 : 100); } }; @@ -122,52 +130,47 @@ export function Cinematic3DComparison({ {/* 3D Kinetic Tilt Stage */}
setIsDragging(true)} onTouchStart={() => setIsDragging(true)} + onKeyDown={handleKeyDown} + role="slider" + aria-label="Before and after comparison" + aria-valuemin={0} + aria-valuemax={100} + aria-valuenow={50} + tabIndex={0} className={`relative w-full ${aspectRatio} rounded-2xl sm:rounded-3xl overflow-hidden cursor-ew-resize preserve-3d transition-transform duration-150 ease-out shadow-cinematic-3d hover:shadow-cinematic-hover border border-slate-900/[0.08] bg-slate-950`} - style={{ - transform: 'rotateX(0deg) rotateY(0deg) scale3d(1, 1, 1)', - touchAction: 'pan-y', - }} + style={{ '--split': '50%', touchAction: 'pan-y' } as React.CSSProperties} > {/* Layer 1: After Image (Full Reconstructed Neural Result) */} {/* eslint-disable-next-line @next/next/no-img-element */} AI Reconstructed result - - {/* Dynamic 3D Cursor Specular Light Glint */} -
{/* Layer 2: Before Image (Clipped Left Side with Polygon Split) */}
{/* eslint-disable-next-line @next/next/no-img-element */} Original input
{/* Layer 3: Illuminated Holographic Laser Split Line (Champagne Gold + Electric Sapphire) */}
{/* Subtle Vertical Caustic Glow Beam */}
@@ -228,7 +231,7 @@ export function Cinematic3DComparison({ className="absolute bottom-3.5 right-3.5 sm:bottom-4 sm:right-4 bg-white/80 backdrop-blur-md px-2.5 py-1 rounded-lg text-[9px] font-mono font-bold text-slate-700 border border-slate-900/[0.06] pointer-events-none z-20 preserve-3d" style={{ transform: 'translateZ(26px)' }} > - SCAN: {Math.round(sliderPosition)}% + SCAN: 50%