perf(ui): optimize comparison sliders and image loading
This commit is contained in:
@@ -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<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)
|
||||
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<HTMLDivElement>) => {
|
||||
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<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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -122,52 +130,47 @@ export function Cinematic3DComparison({
|
||||
{/* 3D Kinetic Tilt Stage */}
|
||||
<div
|
||||
ref={containerRef}
|
||||
onMouseMove={handleStageMouseMove}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
onMouseDown={() => 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 */}
|
||||
<img
|
||||
src={afterUrl}
|
||||
alt="AI Reconstructed result"
|
||||
loading="eager"
|
||||
className="absolute inset-0 w-full h-full object-cover pointer-events-none will-change-transform"
|
||||
/>
|
||||
|
||||
{/* 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%)',
|
||||
}}
|
||||
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 will-change-transform"
|
||||
style={{ clipPath: `polygon(0 0, ${sliderPosition}% 0, ${sliderPosition}% 100%, 0 100%)` }}
|
||||
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="eager"
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
className="absolute inset-0 w-full h-full object-cover"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Layer 3: Illuminated Holographic Laser Split Line (Champagne Gold + Electric Sapphire) */}
|
||||
<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"
|
||||
style={{ left: `${sliderPosition}%` }}
|
||||
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: 'var(--split)' }}
|
||||
>
|
||||
{/* 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]" />
|
||||
@@ -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)}%
|
||||
<span ref={scanLabelRef}>SCAN: 50%</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user