298 lines
12 KiB
TypeScript
298 lines
12 KiB
TypeScript
'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<HTMLDivElement>(null);
|
|
const scanLabelRef = useRef<HTMLSpanElement>(null);
|
|
const isPointerDownRef = useRef(false);
|
|
const cachedRectRef = useRef<{ left: number; width: number } | null>(null);
|
|
const rafIdRef = useRef<number | null>(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<HTMLDivElement>) => {
|
|
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<HTMLDivElement>) => {
|
|
// 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<HTMLDivElement>) => {
|
|
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<HTMLDivElement>) => {
|
|
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<HTMLDivElement>) => {
|
|
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 (
|
|
<div className={`w-full perspective-1400 select-none ${className}`}>
|
|
{/* 3D Kinetic Tilt Stage - Allows smooth page scrolling when touching anywhere except the arrows handle */}
|
|
<div
|
|
ref={containerRef}
|
|
onPointerDown={handleContainerPointerDown}
|
|
className={`relative w-full ${aspectRatio} rounded-2xl sm:rounded-3xl overflow-hidden 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={{
|
|
'--split': '50%',
|
|
touchAction: 'pan-y', // Allows mobile vertical scrolling across the image and line
|
|
} as React.CSSProperties}
|
|
>
|
|
{/* Layer 1: After Image (Full Reconstructed Neural Result) */}
|
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
<img
|
|
src={afterUrl}
|
|
alt="AI Reconstructed result"
|
|
loading="lazy"
|
|
decoding="async"
|
|
className="absolute inset-0 w-full h-full object-cover pointer-events-none"
|
|
/>
|
|
|
|
{/* Layer 2: Before Image (Clipped Left Side with Polygon Split) */}
|
|
<div
|
|
className="absolute inset-0 overflow-hidden pointer-events-none"
|
|
style={{ clipPath: 'polygon(0 0, var(--split) 0, var(--split) 100%, 0 100%)' }}
|
|
>
|
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
<img
|
|
src={beforeUrl}
|
|
alt="Original input"
|
|
loading="lazy"
|
|
decoding="async"
|
|
className="absolute inset-0 w-full h-full object-cover"
|
|
/>
|
|
</div>
|
|
|
|
{/* Layer 3: Illuminated Holographic Laser Split Line (pointer-events-none so it doesn't block scrolling) */}
|
|
<div
|
|
className="absolute top-0 bottom-0 w-[2px] bg-gradient-to-b from-blue-400 via-white to-amber-300 z-20 pointer-events-none -translate-x-1/2"
|
|
style={{ left: 'var(--split)' }}
|
|
>
|
|
{/* Vertical Caustic Glow Beam */}
|
|
<div className="absolute -top-4 -bottom-4 -left-1.5 w-3.5 bg-gradient-to-r from-blue-500/20 via-white/40 to-amber-400/20 blur-[3px]" />
|
|
|
|
{/* 3D Tactile Center Control Handle with Arrows - Dedicated Drag Trigger with Pointer Capture */}
|
|
<div
|
|
onPointerDown={handlePointerDown}
|
|
onPointerMove={handlePointerMove}
|
|
onPointerUp={handlePointerUp}
|
|
onPointerCancel={handlePointerUp}
|
|
onKeyDown={handleKeyDown}
|
|
role="slider"
|
|
aria-label="Before and after comparison slider"
|
|
aria-valuemin={0}
|
|
aria-valuemax={100}
|
|
aria-valuenow={50}
|
|
tabIndex={0}
|
|
className="absolute top-1/2 -translate-y-1/2 -translate-x-1/2 w-10 h-10 sm:w-11 sm:h-11 rounded-full bg-white/95 backdrop-blur-md border border-slate-900/10 shadow-2xl flex items-center justify-center cursor-ew-resize hover:scale-110 active:scale-95 transition-transform duration-150 preserve-3d before:absolute before:-inset-3 before:content-[''] pointer-events-auto touch-none"
|
|
style={{
|
|
transform: 'translateZ(28px)',
|
|
touchAction: 'none', // Strictly locks drag on the arrow handle only
|
|
}}
|
|
>
|
|
{/* Outer Metallic Ring */}
|
|
<div className="absolute inset-0 rounded-full border border-blue-600/30 shadow-[0_0_12px_rgba(37,99,235,0.3)]" />
|
|
|
|
{/* Inner Arrows SVG */}
|
|
<svg
|
|
className="w-4 h-4 text-blue-700"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
viewBox="0 0 24 24"
|
|
>
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2.5} d="M8 9l-3 3 3 3m8-6l3 3-3 3" />
|
|
</svg>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Layer 4: Floating 3D Telemetry Badges */}
|
|
<div
|
|
className="absolute top-3.5 left-3.5 sm:top-5 sm:left-5 bg-white/85 backdrop-blur-md px-3 py-1.5 rounded-xl text-[10px] sm:text-[11px] font-mono font-bold text-slate-800 border border-slate-900/[0.08] pointer-events-none z-20 flex items-center gap-1.5 shadow-sm preserve-3d"
|
|
style={{ transform: 'translateZ(30px)' }}
|
|
>
|
|
<Eye className="w-3.5 h-3.5 text-slate-600" />
|
|
<span>{beforeLabel}</span>
|
|
</div>
|
|
|
|
<div
|
|
className="absolute top-3.5 right-3.5 sm:top-5 sm:right-5 bg-gradient-to-r from-blue-600 via-indigo-600 to-blue-700 text-white backdrop-blur-md px-3.5 py-1.5 rounded-xl text-[10px] sm:text-[11px] font-mono font-bold shadow-md border border-white/20 pointer-events-none z-20 flex items-center gap-1.5 preserve-3d"
|
|
style={{ transform: 'translateZ(30px)' }}
|
|
>
|
|
<Sparkles className="w-3.5 h-3.5 text-white" />
|
|
<span>{afterLabel}</span>
|
|
</div>
|
|
|
|
{/* Lower Spatial Telemetry Chips */}
|
|
<div
|
|
className="absolute bottom-3.5 left-3.5 sm:bottom-4 sm:left-4 hidden sm:flex items-center gap-2 pointer-events-none z-20 preserve-3d"
|
|
style={{ transform: 'translateZ(26px)' }}
|
|
>
|
|
<div className="bg-slate-950/60 backdrop-blur-md px-2.5 py-1 rounded-lg text-[9px] font-mono font-semibold text-white/90 border border-white/10 flex items-center gap-1.5">
|
|
<Activity className="w-3 h-3 text-emerald-400 animate-pulse" />
|
|
<span>DELTA DEPTH MAP</span>
|
|
</div>
|
|
<div className="bg-slate-950/60 backdrop-blur-md px-2.5 py-1 rounded-lg text-[9px] font-mono font-semibold text-white/90 border border-white/10 flex items-center gap-1.5">
|
|
<Scan className="w-3 h-3 text-cyan-400" />
|
|
<span>LOSSLESS 4K</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div
|
|
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)' }}
|
|
>
|
|
<span ref={scanLabelRef}>SCAN: 50%</span>
|
|
</div>
|
|
</div>
|
|
|
|
{caption && (
|
|
<p className="mt-3 text-center text-xs font-mono text-slate-500 tracking-wide uppercase">
|
|
{caption}
|
|
</p>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|