'use client'; import React, { useState, useRef, useCallback, useEffect } from 'react'; import { Eye, Sparkles, Scan, Activity } from 'lucide-react'; interface Cinematic3DComparisonProps { beforeUrl: string; afterUrl: string; beforeLabel?: string; afterLabel?: string; caption?: string; aspectRatio?: string; autoSlide?: boolean; className?: string; } export function Cinematic3DComparison({ beforeUrl, afterUrl, beforeLabel = 'RAW SOURCE', afterLabel = 'AI RECONSTRUCTED', caption, aspectRatio = 'aspect-[4/3] sm:aspect-[16/10]', autoSlide = false, className = '', }: Cinematic3DComparisonProps) { const sliderPosition = useRef(50); const [isDragging, setIsDragging] = useState(false); const containerRef = useRef(null); const scanLabelRef = useRef(null); const isPointerDownRef = useRef(false); const cachedRectRef = useRef<{ left: number; width: number } | null>(null); const rafIdRef = useRef(null); const updatePosition = useCallback((position: number) => { sliderPosition.current = Math.max(0, Math.min(100, position)); if (containerRef.current) { 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 and not dragging) useEffect(() => { if (!autoSlide || isDragging) return; let animId = 0; let start = 0; let visible = true; let lastUpdate = 0; const animate = (time: number) => { if (!visible || document.hidden || isPointerDownRef.current) { animId = 0; return; } animId = requestAnimationFrame(animate); if (time - lastUpdate < 30) return; lastUpdate = time; if (!start) start = time; updatePosition(50 + 18 * Math.sin((time - start) * 0.0012)); }; const observer = new IntersectionObserver(([entry]) => { visible = entry.isIntersecting; if (visible && !animId && !isPointerDownRef.current) 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, updatePosition]); // Unified Pointer Handlers const handlePointerDown = (e: React.PointerEvent) => { if (e.button !== 0 && e.pointerType === 'mouse') return; e.stopPropagation(); isPointerDownRef.current = true; setIsDragging(true); try { e.currentTarget.setPointerCapture(e.pointerId); } catch { // Fallback } if (containerRef.current) { const rect = containerRef.current.getBoundingClientRect(); cachedRectRef.current = { left: rect.left, width: rect.width }; const x = Math.max(0, Math.min(e.clientX - rect.left, rect.width)); const percent = (x / rect.width) * 100; updatePosition(percent); } }; const handleContainerPointerDown = (e: React.PointerEvent) => { // On desktop with mouse, clicking anywhere on the stage moves the slider if (e.pointerType !== 'mouse' || e.button !== 0) return; handlePointerDown(e); }; const handlePointerMove = (e: React.PointerEvent) => { if (!isPointerDownRef.current) return; e.stopPropagation(); const clientX = e.clientX; if (rafIdRef.current) { cancelAnimationFrame(rafIdRef.current); } rafIdRef.current = requestAnimationFrame(() => { let rect = cachedRectRef.current; if (!rect && containerRef.current) { const r = containerRef.current.getBoundingClientRect(); rect = { left: r.left, width: r.width }; cachedRectRef.current = rect; } if (!rect || rect.width <= 0) return; const x = Math.max(0, Math.min(clientX - rect.left, rect.width)); const percent = (x / rect.width) * 100; updatePosition(percent); }); }; const handlePointerUp = (e: React.PointerEvent) => { if (!isPointerDownRef.current) return; e.stopPropagation(); isPointerDownRef.current = false; cachedRectRef.current = null; setIsDragging(false); if (rafIdRef.current) { cancelAnimationFrame(rafIdRef.current); rafIdRef.current = null; } try { e.currentTarget.releasePointerCapture(e.pointerId); } catch { // Fallback } }; 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); } }; return (
{/* 3D Kinetic Tilt Stage - Allows smooth page scrolling when touching anywhere except the arrows handle */}
{/* Layer 1: After Image (Full Reconstructed Neural Result) */} {/* eslint-disable-next-line @next/next/no-img-element */} AI Reconstructed result {/* 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 (pointer-events-none so it doesn't block scrolling) */}
{/* Vertical Caustic Glow Beam */}
{/* 3D Tactile Center Control Handle with Arrows - Dedicated Drag Trigger with Pointer Capture */}
{/* Outer Metallic Ring */}
{/* Inner Arrows SVG */}
{/* Layer 4: Floating 3D Telemetry Badges */}
{beforeLabel}
{afterLabel}
{/* Lower Spatial Telemetry Chips */}
DELTA DEPTH MAP
LOSSLESS 4K
SCAN: 50%
{caption && (

{caption}

)}
); }