fix(perf): eliminate double-damping lag, layout thrashing, and optimize horizontal scroll speed
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import React, { useRef, useState, useEffect } from 'react';
|
import React, { useRef, useEffect, useState, useCallback } from 'react';
|
||||||
import Link from 'next/link';
|
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';
|
import { Cinematic3DComparison } from '@/components/ui/Cinematic3DComparison';
|
||||||
|
|
||||||
interface ShowcaseCard {
|
interface ShowcaseCard {
|
||||||
@@ -79,74 +79,116 @@ export function CinematicHorizontalScrollSection() {
|
|||||||
const bgTextRef = useRef<HTMLDivElement>(null);
|
const bgTextRef = useRef<HTMLDivElement>(null);
|
||||||
const progressBarRef = useRef<HTMLDivElement>(null);
|
const progressBarRef = useRef<HTMLDivElement>(null);
|
||||||
const progressTextRef = useRef<HTMLSpanElement>(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(() => {
|
useEffect(() => {
|
||||||
let rafId: number;
|
let ticking = false;
|
||||||
let targetX = 0;
|
let containerTop = 0;
|
||||||
let currentX = 0;
|
let scrollableDistance = 1;
|
||||||
let isTracking = true;
|
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 measure = () => {
|
||||||
const updateMotion = () => {
|
if (!containerRef.current || !trackRef.current) return;
|
||||||
if (!isTracking) return;
|
const rect = containerRef.current.getBoundingClientRect();
|
||||||
|
containerTop = rect.top + window.scrollY;
|
||||||
if (containerRef.current && trackRef.current) {
|
scrollableDistance = Math.max(containerRef.current.offsetHeight - window.innerHeight, 1);
|
||||||
const rect = containerRef.current.getBoundingClientRect();
|
maxTranslate = Math.max(trackRef.current.scrollWidth - window.innerWidth, 1);
|
||||||
const totalScrollable = containerRef.current.offsetHeight - window.innerHeight;
|
screenCenter = window.innerWidth / 2;
|
||||||
|
const firstCard = trackRef.current.children[0] as HTMLElement | undefined;
|
||||||
if (totalScrollable > 0) {
|
if (firstCard) {
|
||||||
const rawProgress = Math.min(Math.max(-rect.top / totalScrollable, 0), 1);
|
cardStep = firstCard.offsetWidth + 48; // card width + gap
|
||||||
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)`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 () => {
|
return () => {
|
||||||
isTracking = false;
|
window.removeEventListener('resize', measure);
|
||||||
cancelAnimationFrame(rafId);
|
window.removeEventListener('scroll', handleScroll);
|
||||||
|
window.removeEventListener('wheel', handleWheel);
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -154,14 +196,14 @@ export function CinematicHorizontalScrollSection() {
|
|||||||
<section
|
<section
|
||||||
id="flagship-models"
|
id="flagship-models"
|
||||||
ref={containerRef}
|
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 */}
|
{/* 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) */}
|
{/* Massive 3D Kinetic Background Typography Ribbon (Parallax Drift) */}
|
||||||
<div
|
<div
|
||||||
ref={bgTextRef}
|
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"
|
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">
|
<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>
|
</h2>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex flex-wrap items-center gap-2.5 sm:gap-3">
|
||||||
<span className="text-xs font-mono font-bold text-slate-500 uppercase tracking-widest hidden sm:inline">
|
{/* Quick Card Jump Pills */}
|
||||||
SCROLL DOWN TO PAN HORIZONTALLY
|
<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">
|
||||||
</span>
|
{showcaseCards.map((c, i) => (
|
||||||
<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">
|
<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 className="w-2 h-2 rounded-full bg-blue-600 animate-ping inline-block" />
|
||||||
<span ref={progressTextRef}>PROGRESS 0%</span>
|
<span ref={progressTextRef}>PROGRESS 0%</span>
|
||||||
</div>
|
</div>
|
||||||
@@ -200,7 +277,7 @@ export function CinematicHorizontalScrollSection() {
|
|||||||
<div className="relative w-full my-auto overflow-visible py-4">
|
<div className="relative w-full my-auto overflow-visible py-4">
|
||||||
<div
|
<div
|
||||||
ref={trackRef}
|
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) => (
|
{showcaseCards.map((card) => (
|
||||||
<div
|
<div
|
||||||
@@ -228,7 +305,7 @@ export function CinematicHorizontalScrollSection() {
|
|||||||
</div>
|
</div>
|
||||||
</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">
|
<div className="mb-6 relative z-10">
|
||||||
<Cinematic3DComparison
|
<Cinematic3DComparison
|
||||||
beforeUrl={card.beforeUrl}
|
beforeUrl={card.beforeUrl}
|
||||||
@@ -236,6 +313,7 @@ export function CinematicHorizontalScrollSection() {
|
|||||||
beforeLabel="RAW SOURCE"
|
beforeLabel="RAW SOURCE"
|
||||||
afterLabel={card.model}
|
afterLabel={card.model}
|
||||||
aspectRatio="aspect-[16/10]"
|
aspectRatio="aspect-[16/10]"
|
||||||
|
autoSlide={false}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -289,7 +367,7 @@ export function CinematicHorizontalScrollSection() {
|
|||||||
href={`/apps/${card.appSlug}`}
|
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"
|
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>
|
<span>{card.appName}</span>
|
||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
@@ -299,14 +377,24 @@ export function CinematicHorizontalScrollSection() {
|
|||||||
</div>
|
</div>
|
||||||
</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="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 className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 py-3 border-t border-slate-900/[0.06]">
|
||||||
<div
|
{/* Visual Progress Bar Track */}
|
||||||
ref={progressBarRef}
|
<div className="flex-1 max-w-md h-1.5 bg-slate-200/80 rounded-full overflow-hidden">
|
||||||
className="h-full bg-gradient-to-r from-blue-600 via-indigo-600 to-amber-500 rounded-full will-change-transform"
|
<div
|
||||||
style={{ width: '4%' }}
|
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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ export function Cinematic3DTextEngine() {
|
|||||||
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
|
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
|
||||||
|
|
||||||
let rafId: number;
|
let rafId: number;
|
||||||
let mouseX = 0;
|
|
||||||
let mouseY = 0;
|
|
||||||
let targetMouseX = 0;
|
let targetMouseX = 0;
|
||||||
let targetMouseY = 0;
|
let targetMouseY = 0;
|
||||||
let currentMouseX = 0;
|
let currentMouseX = 0;
|
||||||
@@ -32,14 +30,13 @@ export function Cinematic3DTextEngine() {
|
|||||||
const sy = window.scrollY || 0;
|
const sy = window.scrollY || 0;
|
||||||
const delta = sy - lastScrollY;
|
const delta = sy - lastScrollY;
|
||||||
lastScrollY = sy;
|
lastScrollY = sy;
|
||||||
// Scroll velocity pitch tilt (leans on scroll)
|
targetScrollPitch = Math.max(Math.min(delta * 0.18, 7), -7);
|
||||||
targetScrollPitch = Math.max(Math.min(delta * 0.2, 8), -8);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
window.addEventListener('mousemove', handleMouseMove, { passive: true });
|
window.addEventListener('mousemove', handleMouseMove, { passive: true });
|
||||||
window.addEventListener('scroll', handleScroll, { passive: true });
|
window.addEventListener('scroll', handleScroll, { passive: true });
|
||||||
|
|
||||||
// Selector targeting all headings, display titles, stats, badges, and card titles across the whole website
|
// Target headings, badges, card titles, and stats
|
||||||
const selector = [
|
const selector = [
|
||||||
'h1:not([data-no3d])',
|
'h1:not([data-no3d])',
|
||||||
'h2:not([data-no3d])',
|
'h2:not([data-no3d])',
|
||||||
@@ -51,58 +48,59 @@ export function Cinematic3DTextEngine() {
|
|||||||
'[data-3d-text]',
|
'[data-3d-text]',
|
||||||
].join(', ');
|
].join(', ');
|
||||||
|
|
||||||
|
// Use IntersectionObserver to track visible elements with ZERO per-frame DOM reads
|
||||||
|
const visibleElements = new Set<HTMLElement>();
|
||||||
|
|
||||||
|
const observer = new IntersectionObserver(
|
||||||
|
(entries) => {
|
||||||
|
entries.forEach((entry) => {
|
||||||
|
const el = entry.target as HTMLElement;
|
||||||
|
if (entry.isIntersecting) {
|
||||||
|
visibleElements.add(el);
|
||||||
|
// Ensure inline elements can be 3D transformed
|
||||||
|
if (window.getComputedStyle(el).display === 'inline') {
|
||||||
|
el.style.display = 'inline-block';
|
||||||
|
}
|
||||||
|
el.style.transformStyle = 'preserve-3d';
|
||||||
|
el.style.willChange = 'transform';
|
||||||
|
} else {
|
||||||
|
visibleElements.delete(el);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
{ rootMargin: '100px 0px 100px 0px', threshold: 0 }
|
||||||
|
);
|
||||||
|
|
||||||
|
const allElements = document.querySelectorAll<HTMLElement>(selector);
|
||||||
|
allElements.forEach((el) => {
|
||||||
|
if (!el.closest('canvas') && !el.closest('[data-no3d-container]')) {
|
||||||
|
observer.observe(el);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const tick = () => {
|
const tick = () => {
|
||||||
clock += 0.022;
|
clock += 0.024;
|
||||||
|
|
||||||
// Inertial smoothing lerps
|
// Fast, responsive lerps (0.12 factor eliminates input lag)
|
||||||
currentMouseX += (targetMouseX - currentMouseX) * 0.075;
|
currentMouseX += (targetMouseX - currentMouseX) * 0.12;
|
||||||
currentMouseY += (targetMouseY - currentMouseY) * 0.075;
|
currentMouseY += (targetMouseY - currentMouseY) * 0.12;
|
||||||
currentScrollPitch += (targetScrollPitch - currentScrollPitch) * 0.1;
|
currentScrollPitch += (targetScrollPitch - currentScrollPitch) * 0.15;
|
||||||
targetScrollPitch *= 0.88; // Damping decay
|
targetScrollPitch *= 0.85;
|
||||||
|
|
||||||
// Update root CSS variables for global CSS animation pairing
|
let idx = 0;
|
||||||
document.documentElement.style.setProperty('--3d-mouse-x', currentMouseX.toFixed(3));
|
visibleElements.forEach((el) => {
|
||||||
document.documentElement.style.setProperty('--3d-mouse-y', currentMouseY.toFixed(3));
|
idx++;
|
||||||
document.documentElement.style.setProperty('--3d-scroll-pitch', `${currentScrollPitch.toFixed(2)}deg`);
|
const phase = idx * 0.5;
|
||||||
|
const waveX = Math.sin(clock * 1.4 + phase) * 3.4;
|
||||||
|
const waveY = Math.cos(clock * 1.1 + phase) * 4.0;
|
||||||
|
const waveZ = Math.sin(clock * 1.8 + phase) * 6;
|
||||||
|
|
||||||
const elements = document.querySelectorAll<HTMLElement>(selector);
|
|
||||||
const vh = window.innerHeight;
|
|
||||||
const vw = window.innerWidth;
|
|
||||||
|
|
||||||
elements.forEach((el, index) => {
|
|
||||||
// Skip canvas or explicitly opted-out elements
|
|
||||||
if (el.closest('canvas') || el.closest('[data-no3d-container]')) return;
|
|
||||||
|
|
||||||
const rect = el.getBoundingClientRect();
|
|
||||||
// Skip elements far outside viewport for performance
|
|
||||||
if (rect.bottom < -200 || rect.top > vh + 200) return;
|
|
||||||
|
|
||||||
// Ensure element can receive 3D transform (inline elements cannot be transformed)
|
|
||||||
const computedDisplay = window.getComputedStyle(el).display;
|
|
||||||
if (computedDisplay === 'inline') {
|
|
||||||
el.style.display = 'inline-block';
|
|
||||||
}
|
|
||||||
|
|
||||||
const elCenterX = rect.left + rect.width / 2;
|
|
||||||
const elCenterY = rect.top + rect.height / 2;
|
|
||||||
|
|
||||||
// Parallax coordinates relative to viewport center
|
|
||||||
const dx = (elCenterX - vw / 2) / (vw / 2);
|
|
||||||
const dy = (elCenterY - vh / 2) / (vh / 2);
|
|
||||||
|
|
||||||
// Individual organic phase offset for each element
|
|
||||||
const phase = index * 0.45;
|
|
||||||
const waveX = Math.sin(clock * 1.3 + phase) * 3.2;
|
|
||||||
const waveY = Math.cos(clock * 1.0 + phase) * 3.8;
|
|
||||||
const waveZ = Math.sin(clock * 1.6 + phase) * 6;
|
|
||||||
|
|
||||||
// Hierarchical depth based on tag importance
|
|
||||||
const tagName = el.tagName.toLowerCase();
|
const tagName = el.tagName.toLowerCase();
|
||||||
let baseZ = 16;
|
let baseZ = 16;
|
||||||
let rotMultiplier = 1;
|
let rotMultiplier = 1;
|
||||||
|
|
||||||
if (tagName === 'h1') {
|
if (tagName === 'h1') {
|
||||||
baseZ = 30;
|
baseZ = 32;
|
||||||
rotMultiplier = 1.3;
|
rotMultiplier = 1.3;
|
||||||
} else if (tagName === 'h2') {
|
} else if (tagName === 'h2') {
|
||||||
baseZ = 24;
|
baseZ = 24;
|
||||||
@@ -111,21 +109,19 @@ export function Cinematic3DTextEngine() {
|
|||||||
baseZ = 18;
|
baseZ = 18;
|
||||||
rotMultiplier = 1.0;
|
rotMultiplier = 1.0;
|
||||||
} else if (el.classList.contains('stat-value')) {
|
} else if (el.classList.contains('stat-value')) {
|
||||||
baseZ = 26;
|
baseZ = 28;
|
||||||
rotMultiplier = 1.2;
|
rotMultiplier = 1.25;
|
||||||
} else if (tagName === 'h4') {
|
} else if (tagName === 'h4') {
|
||||||
baseZ = 14;
|
baseZ = 14;
|
||||||
rotMultiplier = 0.9;
|
rotMultiplier = 0.9;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate 3D perspective pitch, yaw, and translation
|
const rotX = (-currentMouseY * 6 + currentScrollPitch + waveX) * rotMultiplier;
|
||||||
const rotX = (-(currentMouseY * 6 + dy * 3.5) + currentScrollPitch + waveX) * rotMultiplier;
|
const rotY = (currentMouseX * 7 + waveY) * rotMultiplier;
|
||||||
const rotY = (currentMouseX * 7 + dx * 4 + waveY) * rotMultiplier;
|
const transZ = Math.max(baseZ + waveZ, 6);
|
||||||
const transZ = Math.max(baseZ + waveZ - Math.hypot(dx, dy) * 4, 6);
|
|
||||||
|
|
||||||
|
// Batch write directly to GPU transform matrix (ZERO DOM READS!)
|
||||||
el.style.transform = `perspective(950px) rotateX(${rotX.toFixed(2)}deg) rotateY(${rotY.toFixed(2)}deg) translateZ(${transZ.toFixed(1)}px)`;
|
el.style.transform = `perspective(950px) rotateX(${rotX.toFixed(2)}deg) rotateY(${rotY.toFixed(2)}deg) translateZ(${transZ.toFixed(1)}px)`;
|
||||||
el.style.transformStyle = 'preserve-3d';
|
|
||||||
el.style.willChange = 'transform';
|
|
||||||
});
|
});
|
||||||
|
|
||||||
rafId = requestAnimationFrame(tick);
|
rafId = requestAnimationFrame(tick);
|
||||||
@@ -135,6 +131,7 @@ export function Cinematic3DTextEngine() {
|
|||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
cancelAnimationFrame(rafId);
|
cancelAnimationFrame(rafId);
|
||||||
|
observer.disconnect();
|
||||||
window.removeEventListener('mousemove', handleMouseMove);
|
window.removeEventListener('mousemove', handleMouseMove);
|
||||||
window.removeEventListener('scroll', handleScroll);
|
window.removeEventListener('scroll', handleScroll);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,13 +9,13 @@ export function SmoothScroll() {
|
|||||||
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
|
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
|
||||||
|
|
||||||
const lenis = new Lenis({
|
const lenis = new Lenis({
|
||||||
duration: 1.15,
|
duration: 0.95,
|
||||||
easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
|
easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
|
||||||
orientation: 'vertical',
|
orientation: 'vertical',
|
||||||
gestureOrientation: 'vertical',
|
gestureOrientation: 'both', // Allows trackpad horizontal and vertical gestures
|
||||||
smoothWheel: true,
|
smoothWheel: true,
|
||||||
syncTouch: false, // Preserves hardware 120Hz momentum touch scrolling on phones & tablets
|
syncTouch: false, // Preserves hardware 120Hz momentum touch scrolling on phones & tablets
|
||||||
wheelMultiplier: 0.95,
|
wheelMultiplier: 1.0,
|
||||||
touchMultiplier: 1.0,
|
touchMultiplier: 1.0,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user