perf(ui): optimize comparison sliders and image loading
This commit is contained in:
@@ -11,7 +11,6 @@ interface Cinematic3DComparisonProps {
|
|||||||
caption?: string;
|
caption?: string;
|
||||||
aspectRatio?: string;
|
aspectRatio?: string;
|
||||||
autoSlide?: boolean;
|
autoSlide?: boolean;
|
||||||
maxTilt?: number;
|
|
||||||
className?: string;
|
className?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,35 +22,56 @@ export function Cinematic3DComparison({
|
|||||||
caption,
|
caption,
|
||||||
aspectRatio = 'aspect-[4/3] sm:aspect-[16/10]',
|
aspectRatio = 'aspect-[4/3] sm:aspect-[16/10]',
|
||||||
autoSlide = false,
|
autoSlide = false,
|
||||||
maxTilt = 6,
|
|
||||||
className = '',
|
className = '',
|
||||||
}: Cinematic3DComparisonProps) {
|
}: Cinematic3DComparisonProps) {
|
||||||
const [sliderPosition, setSliderPosition] = useState(50);
|
const sliderPosition = useRef(50);
|
||||||
const [isDragging, setIsDragging] = useState(false);
|
const [isDragging, setIsDragging] = useState(false);
|
||||||
|
|
||||||
const containerRef = useRef<HTMLDivElement>(null);
|
const containerRef = useRef<HTMLDivElement>(null);
|
||||||
const glareRef = useRef<HTMLDivElement>(null);
|
const scanLabelRef = useRef<HTMLSpanElement>(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)
|
// Auto-slide smooth sine oscillation loop (only when explicitly requested)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!autoSlide || isDragging) return;
|
if (!autoSlide || isDragging) return;
|
||||||
|
|
||||||
let animId: number;
|
let animId = 0;
|
||||||
const start = Date.now();
|
let start = 0;
|
||||||
const animate = () => {
|
let visible = true;
|
||||||
const elapsed = Date.now() - start;
|
let lastUpdate = 0;
|
||||||
// Gentle cinematic oscillation between 30% and 70%
|
const animate = (time: number) => {
|
||||||
const newPos = 50 + 20 * Math.sin(elapsed * 0.0014);
|
if (!visible || document.hidden) {
|
||||||
setSliderPosition(newPos);
|
animId = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
animId = requestAnimationFrame(animate);
|
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);
|
animId = requestAnimationFrame(animate);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
if (animId) cancelAnimationFrame(animId);
|
if (animId) cancelAnimationFrame(animId);
|
||||||
|
observer.disconnect();
|
||||||
};
|
};
|
||||||
}, [autoSlide, isDragging]);
|
}, [autoSlide, isDragging, updatePosition]);
|
||||||
|
|
||||||
// Handle position calculation
|
// Handle position calculation
|
||||||
const handleMove = useCallback((clientX: number) => {
|
const handleMove = useCallback((clientX: number) => {
|
||||||
@@ -59,8 +79,8 @@ export function Cinematic3DComparison({
|
|||||||
const rect = containerRef.current.getBoundingClientRect();
|
const rect = containerRef.current.getBoundingClientRect();
|
||||||
const x = Math.max(0, Math.min(clientX - rect.left, rect.width));
|
const x = Math.max(0, Math.min(clientX - rect.left, rect.width));
|
||||||
const percent = Math.max(0, Math.min(100, (x / rect.width) * 100));
|
const percent = Math.max(0, Math.min(100, (x / rect.width) * 100));
|
||||||
setSliderPosition(percent);
|
updatePosition(percent);
|
||||||
}, []);
|
}, [updatePosition]);
|
||||||
|
|
||||||
const handleTouchMove = useCallback((e: TouchEvent) => {
|
const handleTouchMove = useCallback((e: TouchEvent) => {
|
||||||
if (!isDragging) return;
|
if (!isDragging) return;
|
||||||
@@ -91,29 +111,17 @@ export function Cinematic3DComparison({
|
|||||||
};
|
};
|
||||||
}, [isDragging, handleMouseMove, handleMouseUp, handleTouchMove]);
|
}, [isDragging, handleMouseMove, handleMouseUp, handleTouchMove]);
|
||||||
|
|
||||||
// 3D Spatial Tilt - Pure Direct DOM Manipulation (Zero React Re-renders on mousemove!)
|
const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
|
||||||
const handleStageMouseMove = (e: React.MouseEvent<HTMLDivElement>) => {
|
const step = event.shiftKey ? 10 : 2;
|
||||||
if (!containerRef.current) return;
|
if (event.key === 'ArrowLeft' || event.key === 'ArrowDown') {
|
||||||
const rect = containerRef.current.getBoundingClientRect();
|
event.preventDefault();
|
||||||
const x = (e.clientX - rect.left) / rect.width;
|
updatePosition(sliderPosition.current - step);
|
||||||
const y = (e.clientY - rect.top) / rect.height;
|
} else if (event.key === 'ArrowRight' || event.key === 'ArrowUp') {
|
||||||
|
event.preventDefault();
|
||||||
const rx = -(y - 0.5) * maxTilt * 2;
|
updatePosition(sliderPosition.current + step);
|
||||||
const ry = (x - 0.5) * maxTilt * 2;
|
} else if (event.key === 'Home' || event.key === 'End') {
|
||||||
|
event.preventDefault();
|
||||||
containerRef.current.style.transform = `rotateX(${rx.toFixed(2)}deg) rotateY(${ry.toFixed(2)}deg) scale3d(1.012, 1.012, 1.012)`;
|
updatePosition(event.key === 'Home' ? 0 : 100);
|
||||||
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';
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -122,52 +130,47 @@ export function Cinematic3DComparison({
|
|||||||
{/* 3D Kinetic Tilt Stage */}
|
{/* 3D Kinetic Tilt Stage */}
|
||||||
<div
|
<div
|
||||||
ref={containerRef}
|
ref={containerRef}
|
||||||
onMouseMove={handleStageMouseMove}
|
|
||||||
onMouseLeave={handleMouseLeave}
|
|
||||||
onMouseDown={() => setIsDragging(true)}
|
onMouseDown={() => setIsDragging(true)}
|
||||||
onTouchStart={() => 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`}
|
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={{
|
style={{ '--split': '50%', touchAction: 'pan-y' } as React.CSSProperties}
|
||||||
transform: 'rotateX(0deg) rotateY(0deg) scale3d(1, 1, 1)',
|
|
||||||
touchAction: 'pan-y',
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{/* Layer 1: After Image (Full Reconstructed Neural Result) */}
|
{/* Layer 1: After Image (Full Reconstructed Neural Result) */}
|
||||||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||||
<img
|
<img
|
||||||
src={afterUrl}
|
src={afterUrl}
|
||||||
alt="AI Reconstructed result"
|
alt="AI Reconstructed result"
|
||||||
loading="eager"
|
loading="lazy"
|
||||||
className="absolute inset-0 w-full h-full object-cover pointer-events-none will-change-transform"
|
decoding="async"
|
||||||
/>
|
className="absolute inset-0 w-full h-full object-cover pointer-events-none"
|
||||||
|
|
||||||
{/* Dynamic 3D Cursor Specular Light Glint */}
|
|
||||||
<div
|
|
||||||
ref={glareRef}
|
|
||||||
className="absolute inset-0 pointer-events-none transition-opacity duration-300 z-10 opacity-0"
|
|
||||||
style={{
|
|
||||||
background: 'radial-gradient(circle 380px at 50% 50%, rgba(255, 255, 255, 0.35) 0%, rgba(255, 255, 255, 0.08) 50%, transparent 80%)',
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Layer 2: Before Image (Clipped Left Side with Polygon Split) */}
|
{/* Layer 2: Before Image (Clipped Left Side with Polygon Split) */}
|
||||||
<div
|
<div
|
||||||
className="absolute inset-0 overflow-hidden pointer-events-none will-change-transform"
|
className="absolute inset-0 overflow-hidden pointer-events-none"
|
||||||
style={{ clipPath: `polygon(0 0, ${sliderPosition}% 0, ${sliderPosition}% 100%, 0 100%)` }}
|
style={{ clipPath: 'polygon(0 0, var(--split) 0, var(--split) 100%, 0 100%)' }}
|
||||||
>
|
>
|
||||||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||||
<img
|
<img
|
||||||
src={beforeUrl}
|
src={beforeUrl}
|
||||||
alt="Original input"
|
alt="Original input"
|
||||||
loading="eager"
|
loading="lazy"
|
||||||
|
decoding="async"
|
||||||
className="absolute inset-0 w-full h-full object-cover"
|
className="absolute inset-0 w-full h-full object-cover"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Layer 3: Illuminated Holographic Laser Split Line (Champagne Gold + Electric Sapphire) */}
|
{/* Layer 3: Illuminated Holographic Laser Split Line (Champagne Gold + Electric Sapphire) */}
|
||||||
<div
|
<div
|
||||||
className="absolute top-0 bottom-0 w-[2px] bg-gradient-to-b from-blue-400 via-white to-amber-300 holographic-laser-split z-20 pointer-events-none will-change-transform"
|
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"
|
||||||
style={{ left: `${sliderPosition}%` }}
|
style={{ left: 'var(--split)' }}
|
||||||
>
|
>
|
||||||
{/* Subtle Vertical Caustic Glow Beam */}
|
{/* Subtle 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]" />
|
<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]" />
|
||||||
@@ -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"
|
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)' }}
|
style={{ transform: 'translateZ(26px)' }}
|
||||||
>
|
>
|
||||||
SCAN: {Math.round(sliderPosition)}%
|
<span ref={scanLabelRef}>SCAN: 50%</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user