fix(perf): eliminate double-damping lag, layout thrashing, and optimize horizontal scroll speed

This commit is contained in:
2026-11-03 17:17:02 +05:30
parent 6cba2256b4
commit 3bdd5b6d8a
3 changed files with 224 additions and 139 deletions
@@ -1,8 +1,8 @@
'use client';
import React, { useRef, useState, useEffect } from 'react';
import React, { useRef, useEffect, useState, useCallback } from 'react';
import Link from 'next/link';
import { Sparkles, ArrowRight, Smartphone } from 'lucide-react';
import { Sparkles, ArrowRight, Smartphone, ChevronLeft, ChevronRight } from 'lucide-react';
import { Cinematic3DComparison } from '@/components/ui/Cinematic3DComparison';
interface ShowcaseCard {
@@ -79,74 +79,116 @@ export function CinematicHorizontalScrollSection() {
const bgTextRef = useRef<HTMLDivElement>(null);
const progressBarRef = useRef<HTMLDivElement>(null);
const progressTextRef = useRef<HTMLSpanElement>(null);
const [activeCardIndex, setActiveCardIndex] = useState(0);
const scrollToCard = useCallback((index: number) => {
if (!containerRef.current) return;
const rect = containerRef.current.getBoundingClientRect();
const containerTop = rect.top + window.scrollY;
const scrollableDistance = Math.max(containerRef.current.offsetHeight - window.innerHeight, 1);
const targetY = containerTop + (index / 2) * scrollableDistance;
window.scrollTo({ top: targetY, behavior: 'smooth' });
}, []);
const navigateStep = useCallback((delta: number) => {
const nextIdx = Math.max(0, Math.min(2, activeCardIndex + delta));
scrollToCard(nextIdx);
}, [activeCardIndex, scrollToCard]);
useEffect(() => {
let rafId: number;
let targetX = 0;
let currentX = 0;
let isTracking = true;
let ticking = false;
let containerTop = 0;
let scrollableDistance = 1;
let maxTranslate = 1;
let cardStep = 780;
let screenCenter = window.innerWidth / 2;
// High-precision lerp animation loop (eliminates CSS transition conflicts for butter-smooth 120 FPS glide)
const updateMotion = () => {
if (!isTracking) return;
if (containerRef.current && trackRef.current) {
const rect = containerRef.current.getBoundingClientRect();
const totalScrollable = containerRef.current.offsetHeight - window.innerHeight;
if (totalScrollable > 0) {
const rawProgress = Math.min(Math.max(-rect.top / totalScrollable, 0), 1);
const maxTranslate = trackRef.current.scrollWidth - window.innerWidth;
if (maxTranslate > 0) {
targetX = rawProgress * maxTranslate;
}
// Smooth exponential damping (lerp factor 0.085)
currentX += (targetX - currentX) * 0.085;
// Apply 100% GPU compositor transform without CSS transition lag
trackRef.current.style.transform = `translate3d(-${currentX.toFixed(2)}px, 0, 0)`;
// Parallax 3D background typography ribbon drifting in opposite perspective
if (bgTextRef.current) {
bgTextRef.current.style.transform = `translate3d(-${(currentX * 0.4).toFixed(2)}px, 0, 0) rotateX(10deg)`;
}
// Update Progress Indicator directly in DOM (zero React re-renders)
const liveProgress = Math.min(Math.max(currentX / (maxTranslate || 1), 0), 1);
if (progressBarRef.current) {
progressBarRef.current.style.width = `${Math.max(liveProgress * 100, 4).toFixed(1)}%`;
}
if (progressTextRef.current) {
progressTextRef.current.textContent = `PROGRESS ${Math.round(liveProgress * 100)}%`;
}
// 3D Perspective rotational offset on individual cards
const cards = trackRef.current.children;
const screenCenter = window.innerWidth / 2;
for (let i = 0; i < cards.length; i++) {
const card = cards[i] as HTMLElement;
const cardRect = card.getBoundingClientRect();
const cardCenter = cardRect.left + cardRect.width / 2;
const dist = (cardCenter - screenCenter) / (window.innerWidth * 0.6);
const rotY = Math.max(Math.min(dist * -8, 14), -14);
const scale = Math.max(1 - Math.abs(dist) * 0.04, 0.94);
card.style.transform = `perspective(1200px) rotateY(${rotY.toFixed(2)}deg) scale3d(${scale.toFixed(3)}, ${scale.toFixed(3)}, 1)`;
}
}
const measure = () => {
if (!containerRef.current || !trackRef.current) return;
const rect = containerRef.current.getBoundingClientRect();
containerTop = rect.top + window.scrollY;
scrollableDistance = Math.max(containerRef.current.offsetHeight - window.innerHeight, 1);
maxTranslate = Math.max(trackRef.current.scrollWidth - window.innerWidth, 1);
screenCenter = window.innerWidth / 2;
const firstCard = trackRef.current.children[0] as HTMLElement | undefined;
if (firstCard) {
cardStep = firstCard.offsetWidth + 48; // card width + gap
}
rafId = requestAnimationFrame(updateMotion);
};
rafId = requestAnimationFrame(updateMotion);
measure();
window.addEventListener('resize', measure, { passive: true });
// Ultra-fast direct 1:1 synchronization with smooth scroll (Zero double-damping lag)
const updatePosition = () => {
const sy = window.scrollY;
const offset = sy - containerTop;
const progress = Math.min(Math.max(offset / scrollableDistance, 0), 1);
const currentX = progress * maxTranslate;
if (trackRef.current) {
// Direct GPU compositor translation (100% synchronized with Lenis)
trackRef.current.style.transform = `translate3d(-${currentX.toFixed(1)}px, 0, 0)`;
if (bgTextRef.current) {
bgTextRef.current.style.transform = `translate3d(-${(currentX * 0.35).toFixed(1)}px, 0, 0) rotateX(8deg)`;
}
if (progressBarRef.current) {
progressBarRef.current.style.width = `${Math.max(progress * 100, 4).toFixed(1)}%`;
}
if (progressTextRef.current) {
progressTextRef.current.textContent = `PROGRESS ${Math.round(progress * 100)}%`;
}
const currentActive = Math.min(Math.round(progress * 2), 2);
setActiveCardIndex(currentActive);
// Arithmetic 3D perspective rotation (zero forced reflows)
const cards = trackRef.current.children;
const padLeft = window.innerWidth > 640 ? 48 : 24;
for (let i = 0; i < cards.length; i++) {
const card = cards[i] as HTMLElement;
const cardCenter = padLeft + i * cardStep + (cardStep - 48) / 2 - currentX;
const dist = (cardCenter - screenCenter) / (screenCenter * 1.2);
const rotY = Math.max(Math.min(dist * -6, 9), -9);
const scale = Math.max(1 - Math.abs(dist) * 0.03, 0.96);
card.style.transform = `perspective(1100px) rotateY(${rotY.toFixed(2)}deg) scale3d(${scale.toFixed(3)}, ${scale.toFixed(3)}, 1)`;
}
}
};
const handleScroll = () => {
if (!ticking) {
ticking = true;
requestAnimationFrame(() => {
updatePosition();
ticking = false;
});
}
};
// Trackpad horizontal swipe listener (converts two-finger deltaX directly into scroll)
const handleWheel = (e: WheelEvent) => {
if (!containerRef.current) return;
const rect = containerRef.current.getBoundingClientRect();
if (rect.top <= 20 && rect.bottom >= window.innerHeight - 20) {
if (Math.abs(e.deltaX) > Math.abs(e.deltaY) && Math.abs(e.deltaX) > 2) {
window.scrollBy({ top: e.deltaX * 0.9, behavior: 'instant' });
}
}
};
window.addEventListener('scroll', handleScroll, { passive: true });
window.addEventListener('wheel', handleWheel, { passive: true });
// Initial sync
updatePosition();
return () => {
isTracking = false;
cancelAnimationFrame(rafId);
window.removeEventListener('resize', measure);
window.removeEventListener('scroll', handleScroll);
window.removeEventListener('wheel', handleWheel);
};
}, []);
@@ -154,14 +196,14 @@ export function CinematicHorizontalScrollSection() {
<section
id="flagship-models"
ref={containerRef}
className="relative h-[290vh] sm:h-[350vh] bg-[#FAF8F5] border-t border-slate-900/[0.08] overflow-visible"
className="relative h-[150vh] sm:h-[170vh] bg-[#FAF8F5] border-t border-slate-900/[0.08] overflow-visible"
>
{/* Sticky Fullscreen Camera Viewport */}
<div className="sticky top-0 h-screen w-full overflow-hidden flex flex-col justify-between py-10 sm:py-14 select-none">
<div className="sticky top-0 h-screen w-full overflow-hidden flex flex-col justify-between py-8 sm:py-12 select-none">
{/* Massive 3D Kinetic Background Typography Ribbon (Parallax Drift) */}
<div
ref={bgTextRef}
className="absolute top-1/2 -translate-y-1/2 left-0 w-max whitespace-nowrap pointer-events-none -z-10 opacity-30 select-none preserve-3d"
className="absolute top-1/2 -translate-y-1/2 left-0 w-max whitespace-nowrap pointer-events-none -z-10 opacity-25 select-none preserve-3d"
aria-hidden="true"
>
<span className="font-display font-black text-[13vw] uppercase tracking-tighter text-transparent bg-clip-text bg-gradient-to-r from-slate-900/10 via-blue-900/15 to-amber-900/10 block leading-none">
@@ -185,11 +227,46 @@ export function CinematicHorizontalScrollSection() {
</h2>
</div>
<div className="flex items-center gap-3">
<span className="text-xs font-mono font-bold text-slate-500 uppercase tracking-widest hidden sm:inline">
SCROLL DOWN TO PAN HORIZONTALLY
</span>
<div className="flex items-center gap-1.5 px-3 py-1 rounded-lg bg-white/80 backdrop-blur-sm border border-slate-900/[0.06] text-xs font-mono font-bold text-slate-700 shadow-sm">
<div className="flex flex-wrap items-center gap-2.5 sm:gap-3">
{/* Quick Card Jump Pills */}
<div className="hidden sm:flex items-center gap-1.5 bg-slate-100/80 backdrop-blur-sm p-1 rounded-full border border-slate-200/80">
{showcaseCards.map((c, i) => (
<button
key={c.id}
onClick={() => scrollToCard(i)}
className={`px-3 py-1 rounded-full text-xs font-mono font-bold transition-all duration-200 ${
activeCardIndex === i
? 'bg-blue-600 text-white shadow-sm'
: 'text-slate-600 hover:text-slate-900'
}`}
>
0{i + 1}
</button>
))}
</div>
{/* Quick Step Controls */}
<div className="flex items-center gap-1">
<button
onClick={() => navigateStep(-1)}
disabled={activeCardIndex === 0}
aria-label="Previous card"
className="w-8 h-8 rounded-full bg-white border border-slate-200 shadow-sm flex items-center justify-center text-slate-700 hover:text-blue-600 hover:border-blue-300 disabled:opacity-40 disabled:cursor-not-allowed transition-all"
>
<ChevronLeft className="w-4 h-4" />
</button>
<button
onClick={() => navigateStep(1)}
disabled={activeCardIndex === 2}
aria-label="Next card"
className="w-8 h-8 rounded-full bg-white border border-slate-200 shadow-sm flex items-center justify-center text-slate-700 hover:text-blue-600 hover:border-blue-300 disabled:opacity-40 disabled:cursor-not-allowed transition-all"
>
<ChevronRight className="w-4 h-4" />
</button>
</div>
{/* Progress Badge */}
<div className="flex items-center gap-1.5 px-3 py-1 rounded-full bg-white/90 backdrop-blur-sm border border-slate-900/[0.08] text-xs font-mono font-bold text-slate-800 shadow-sm">
<span className="w-2 h-2 rounded-full bg-blue-600 animate-ping inline-block" />
<span ref={progressTextRef}>PROGRESS 0%</span>
</div>
@@ -200,7 +277,7 @@ export function CinematicHorizontalScrollSection() {
<div className="relative w-full my-auto overflow-visible py-4">
<div
ref={trackRef}
className="flex items-center gap-8 sm:gap-14 px-6 sm:px-12 lg:px-20 w-max will-change-transform preserve-3d"
className="flex items-center gap-8 sm:gap-12 px-6 sm:px-12 w-max will-change-transform preserve-3d"
>
{showcaseCards.map((card) => (
<div
@@ -228,7 +305,7 @@ export function CinematicHorizontalScrollSection() {
</div>
</div>
{/* 3D Cinematic Comparison Viewport */}
{/* 3D Cinematic Comparison Viewport (autoSlide false to save CPU/GPU cycles during horizontal pan) */}
<div className="mb-6 relative z-10">
<Cinematic3DComparison
beforeUrl={card.beforeUrl}
@@ -236,6 +313,7 @@ export function CinematicHorizontalScrollSection() {
beforeLabel="RAW SOURCE"
afterLabel={card.model}
aspectRatio="aspect-[16/10]"
autoSlide={false}
/>
</div>
@@ -289,7 +367,7 @@ export function CinematicHorizontalScrollSection() {
href={`/apps/${card.appSlug}`}
className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-full bg-slate-900 text-white text-xs font-bold hover:bg-blue-700 transition-colors shadow-sm"
>
<Smartphone className="w-3.5 h-3.5" />
<Smartphone className="w-3 h-3 text-blue-400" />
<span>{card.appName}</span>
</Link>
</div>
@@ -299,14 +377,24 @@ export function CinematicHorizontalScrollSection() {
</div>
</div>
{/* Section Bottom Progress Indicator Bar */}
{/* Bottom Cinematic Control & Progress Rail */}
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 w-full z-10">
<div className="w-full bg-slate-200/80 h-1.5 rounded-full overflow-hidden shadow-inner">
<div
ref={progressBarRef}
className="h-full bg-gradient-to-r from-blue-600 via-indigo-600 to-amber-500 rounded-full will-change-transform"
style={{ width: '4%' }}
/>
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 py-3 border-t border-slate-900/[0.06]">
{/* Visual Progress Bar Track */}
<div className="flex-1 max-w-md h-1.5 bg-slate-200/80 rounded-full overflow-hidden">
<div
ref={progressBarRef}
className="h-full bg-gradient-to-r from-blue-600 via-indigo-600 to-amber-500 rounded-full will-change-transform"
style={{ width: '4%' }}
/>
</div>
{/* Model Metadata Tags */}
<div className="flex items-center gap-4 text-xs font-mono text-slate-500">
<span>ARCHIVE: VOL. 26</span>
<span>•</span>
<span>NEURAL VISION ACCELERATOR</span>
</div>
</div>
</div>
</div>