fix(perf): eliminate double-damping lag, layout thrashing, and optimize horizontal scroll speed
This commit is contained in:
@@ -8,8 +8,6 @@ export function Cinematic3DTextEngine() {
|
||||
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
|
||||
|
||||
let rafId: number;
|
||||
let mouseX = 0;
|
||||
let mouseY = 0;
|
||||
let targetMouseX = 0;
|
||||
let targetMouseY = 0;
|
||||
let currentMouseX = 0;
|
||||
@@ -32,14 +30,13 @@ export function Cinematic3DTextEngine() {
|
||||
const sy = window.scrollY || 0;
|
||||
const delta = sy - lastScrollY;
|
||||
lastScrollY = sy;
|
||||
// Scroll velocity pitch tilt (leans on scroll)
|
||||
targetScrollPitch = Math.max(Math.min(delta * 0.2, 8), -8);
|
||||
targetScrollPitch = Math.max(Math.min(delta * 0.18, 7), -7);
|
||||
};
|
||||
|
||||
window.addEventListener('mousemove', handleMouseMove, { 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 = [
|
||||
'h1:not([data-no3d])',
|
||||
'h2:not([data-no3d])',
|
||||
@@ -51,58 +48,59 @@ export function Cinematic3DTextEngine() {
|
||||
'[data-3d-text]',
|
||||
].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 = () => {
|
||||
clock += 0.022;
|
||||
clock += 0.024;
|
||||
|
||||
// Inertial smoothing lerps
|
||||
currentMouseX += (targetMouseX - currentMouseX) * 0.075;
|
||||
currentMouseY += (targetMouseY - currentMouseY) * 0.075;
|
||||
currentScrollPitch += (targetScrollPitch - currentScrollPitch) * 0.1;
|
||||
targetScrollPitch *= 0.88; // Damping decay
|
||||
// Fast, responsive lerps (0.12 factor eliminates input lag)
|
||||
currentMouseX += (targetMouseX - currentMouseX) * 0.12;
|
||||
currentMouseY += (targetMouseY - currentMouseY) * 0.12;
|
||||
currentScrollPitch += (targetScrollPitch - currentScrollPitch) * 0.15;
|
||||
targetScrollPitch *= 0.85;
|
||||
|
||||
// Update root CSS variables for global CSS animation pairing
|
||||
document.documentElement.style.setProperty('--3d-mouse-x', currentMouseX.toFixed(3));
|
||||
document.documentElement.style.setProperty('--3d-mouse-y', currentMouseY.toFixed(3));
|
||||
document.documentElement.style.setProperty('--3d-scroll-pitch', `${currentScrollPitch.toFixed(2)}deg`);
|
||||
let idx = 0;
|
||||
visibleElements.forEach((el) => {
|
||||
idx++;
|
||||
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();
|
||||
let baseZ = 16;
|
||||
let rotMultiplier = 1;
|
||||
|
||||
if (tagName === 'h1') {
|
||||
baseZ = 30;
|
||||
baseZ = 32;
|
||||
rotMultiplier = 1.3;
|
||||
} else if (tagName === 'h2') {
|
||||
baseZ = 24;
|
||||
@@ -111,21 +109,19 @@ export function Cinematic3DTextEngine() {
|
||||
baseZ = 18;
|
||||
rotMultiplier = 1.0;
|
||||
} else if (el.classList.contains('stat-value')) {
|
||||
baseZ = 26;
|
||||
rotMultiplier = 1.2;
|
||||
baseZ = 28;
|
||||
rotMultiplier = 1.25;
|
||||
} else if (tagName === 'h4') {
|
||||
baseZ = 14;
|
||||
rotMultiplier = 0.9;
|
||||
}
|
||||
|
||||
// Calculate 3D perspective pitch, yaw, and translation
|
||||
const rotX = (-(currentMouseY * 6 + dy * 3.5) + currentScrollPitch + waveX) * rotMultiplier;
|
||||
const rotY = (currentMouseX * 7 + dx * 4 + waveY) * rotMultiplier;
|
||||
const transZ = Math.max(baseZ + waveZ - Math.hypot(dx, dy) * 4, 6);
|
||||
const rotX = (-currentMouseY * 6 + currentScrollPitch + waveX) * rotMultiplier;
|
||||
const rotY = (currentMouseX * 7 + waveY) * rotMultiplier;
|
||||
const transZ = Math.max(baseZ + waveZ, 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.transformStyle = 'preserve-3d';
|
||||
el.style.willChange = 'transform';
|
||||
});
|
||||
|
||||
rafId = requestAnimationFrame(tick);
|
||||
@@ -135,6 +131,7 @@ export function Cinematic3DTextEngine() {
|
||||
|
||||
return () => {
|
||||
cancelAnimationFrame(rafId);
|
||||
observer.disconnect();
|
||||
window.removeEventListener('mousemove', handleMouseMove);
|
||||
window.removeEventListener('scroll', handleScroll);
|
||||
};
|
||||
|
||||
@@ -9,13 +9,13 @@ export function SmoothScroll() {
|
||||
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
|
||||
|
||||
const lenis = new Lenis({
|
||||
duration: 1.15,
|
||||
duration: 0.95,
|
||||
easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
|
||||
orientation: 'vertical',
|
||||
gestureOrientation: 'vertical',
|
||||
gestureOrientation: 'both', // Allows trackpad horizontal and vertical gestures
|
||||
smoothWheel: true,
|
||||
syncTouch: false, // Preserves hardware 120Hz momentum touch scrolling on phones & tablets
|
||||
wheelMultiplier: 0.95,
|
||||
wheelMultiplier: 1.0,
|
||||
touchMultiplier: 1.0,
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user