diff --git a/next.config.mjs b/next.config.mjs index b335f9b..2035855 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -40,6 +40,7 @@ function getPermittedDevOrigins() { /** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true, + transpilePackages: ['lenis', 'three', 'framer-motion'], allowedDevOrigins: getPermittedDevOrigins(), images: { unoptimized: true, diff --git a/src/components/3d/HeroCrystalCanvas.tsx b/src/components/3d/HeroCrystalCanvas.tsx new file mode 100644 index 0000000..d42af21 --- /dev/null +++ b/src/components/3d/HeroCrystalCanvas.tsx @@ -0,0 +1,209 @@ +'use client'; + +import React, { useEffect, useRef } from 'react'; +import * as THREE from 'three'; + +interface HeroCrystalCanvasProps { + className?: string; +} + +/** + * Interactive 3D WebGL Crystal Prism Background Canvas + * - Features: Icosahedron optical crystal, dual orbital gimbal rings, floating starlight particles + * - IntersectionObserver pauses RAF loop when scrolled out of view (0% CPU/GPU offscreen) + * - Safe WebGL context loss handling and automatic resource disposal + */ +export function HeroCrystalCanvas({ className }: HeroCrystalCanvasProps) { + const containerRef = useRef(null); + + useEffect(() => { + const container = containerRef.current; + if (!container) return; + + let width = container.clientWidth || 800; + let height = container.clientHeight || 800; + + const scene = new THREE.Scene(); + const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100); + camera.position.z = 4.8; + + let renderer: THREE.WebGLRenderer | null = null; + try { + renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true, powerPreference: 'high-performance' }); + renderer.setSize(width, height); + renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2)); + container.appendChild(renderer.domElement); + } catch { + return; + } + + // Master Group for smooth coordinated rotation + const masterGroup = new THREE.Group(); + scene.add(masterGroup); + + // 1. Central Optical Translucent Crystal (Icosahedron, mobile GPU safe) + const crystalGeo = new THREE.IcosahedronGeometry(1.25, 0); + const crystalMat = new THREE.MeshStandardMaterial({ + color: 0x3b82f6, + emissive: 0x1d4ed8, + emissiveIntensity: 0.12, + roughness: 0.18, + metalness: 0.1, + transparent: true, + opacity: 0.14, + }); + const crystal = new THREE.Mesh(crystalGeo, crystalMat); + masterGroup.add(crystal); + + // Wireframe Fine Lines for the Crystal + const wireGeo = new THREE.WireframeGeometry(crystalGeo); + const wireMat = new THREE.LineBasicMaterial({ + color: 0x60a5fa, + transparent: true, + opacity: 0.22, + }); + const wireframe = new THREE.LineSegments(wireGeo, wireMat); + crystal.add(wireframe); + + // 2. Dual Gyro Gimbal Orbital Rings + // Ring 1 (Cyan orbital ring) + const ring1Geo = new THREE.TorusGeometry(2.15, 0.007, 16, 120); + const ring1Mat = new THREE.MeshBasicMaterial({ + color: 0x38bdf8, + transparent: true, + opacity: 0.20, + }); + const ring1 = new THREE.Mesh(ring1Geo, ring1Mat); + ring1.rotation.x = Math.PI / 3.2; + masterGroup.add(ring1); + + // Ring 2 (Indigo tilted orbital ring) + const ring2Geo = new THREE.TorusGeometry(1.8, 0.007, 16, 100); + const ring2Mat = new THREE.MeshBasicMaterial({ + color: 0x818cf8, + transparent: true, + opacity: 0.16, + }); + const ring2 = new THREE.Mesh(ring2Geo, ring2Mat); + ring2.rotation.y = Math.PI / 4; + ring2.rotation.z = Math.PI / 6; + masterGroup.add(ring2); + + // 3. Ambient Starlight Particles + const particleCount = 40; + const particleGeo = new THREE.BufferGeometry(); + const posArray = new Float32Array(particleCount * 3); + for (let i = 0; i < particleCount * 3; i++) { + posArray[i] = (Math.random() - 0.5) * 7.5; + } + particleGeo.setAttribute('position', new THREE.BufferAttribute(posArray, 3)); + const particleMat = new THREE.PointsMaterial({ + size: 0.03, + color: 0x93c5fa, + transparent: true, + opacity: 0.35, + }); + const particles = new THREE.Points(particleGeo, particleMat); + masterGroup.add(particles); + + // 4. Studio Lighting + const ambientLight = new THREE.AmbientLight(0xffffff, 1.2); + scene.add(ambientLight); + + const cyanLight = new THREE.PointLight(0x06b6d4, 2.2, 12); + cyanLight.position.set(3.5, 3, 3); + scene.add(cyanLight); + + const indigoLight = new THREE.PointLight(0x6366f1, 1.8, 12); + indigoLight.position.set(-3.5, -2, 2.5); + scene.add(indigoLight); + + let animId = 0; + const clock = new THREE.Clock(); + let isVisible = true; + + // IntersectionObserver to pause loop when scrolled out of view (0% CPU/GPU offscreen) + const observer = new IntersectionObserver( + (entries) => { + entries.forEach((entry) => { + isVisible = entry.isIntersecting; + }); + }, + { threshold: 0.05 } + ); + observer.observe(container); + + const animate = () => { + animId = requestAnimationFrame(animate); + if (!isVisible) return; + + const elapsed = clock.getElapsedTime(); + + // Fluid crystal rotation + crystal.rotation.x = elapsed * 0.12; + crystal.rotation.y = elapsed * 0.16; + + // Gimbal ring orbital movements + ring1.rotation.z = elapsed * 0.14; + ring1.rotation.x = Math.PI / 3.2 + Math.sin(elapsed * 0.25) * 0.08; + + ring2.rotation.y = -elapsed * 0.11; + ring2.rotation.z = Math.PI / 6 + Math.cos(elapsed * 0.2) * 0.1; + + // Particle constellation subtle drift + particles.rotation.y = elapsed * 0.03; + particles.rotation.x = elapsed * 0.02; + + if (renderer) { + try { + renderer.render(scene, camera); + } catch { + cancelAnimationFrame(animId); + } + } + }; + + animate(); + + const canvas = renderer.domElement; + const handleContextLost = (e: Event) => { + e.preventDefault(); + cancelAnimationFrame(animId); + }; + canvas.addEventListener('webglcontextlost', handleContextLost, false); + + const handleResize = () => { + if (!container || !renderer) return; + const w = container.clientWidth; + const h = container.clientHeight; + camera.aspect = w / h; + camera.updateProjectionMatrix(); + renderer.setSize(w, h); + }; + + window.addEventListener('resize', handleResize, { passive: true }); + + return () => { + cancelAnimationFrame(animId); + observer.disconnect(); + canvas.removeEventListener('webglcontextlost', handleContextLost); + window.removeEventListener('resize', handleResize); + if (renderer && renderer.domElement && container.contains(renderer.domElement)) { + container.removeChild(renderer.domElement); + } + crystalGeo.dispose(); + crystalMat.dispose(); + wireGeo.dispose(); + wireMat.dispose(); + ring1Geo.dispose(); + ring1Mat.dispose(); + ring2Geo.dispose(); + ring2Mat.dispose(); + particleGeo.dispose(); + particleMat.dispose(); + renderer?.dispose(); + }; + }, []); + + return
; +} diff --git a/src/components/sections/HeroSection.tsx b/src/components/sections/HeroSection.tsx index c3c62fe..53f6611 100644 --- a/src/components/sections/HeroSection.tsx +++ b/src/components/sections/HeroSection.tsx @@ -2,7 +2,12 @@ import React, { useState, useEffect, useRef } from 'react'; import Link from 'next/link'; -import * as THREE from 'three'; +import dynamic from 'next/dynamic'; + +const Hero3DCanvas = dynamic( + () => import('@/components/3d/HeroCrystalCanvas').then((mod) => mod.HeroCrystalCanvas), + { ssr: false, loading: () => null } +); import { Sparkles, ArrowRight, @@ -420,202 +425,3 @@ export function HeroSection({ ); } - -// ------------------------------------------------------------- -// Interactive 3D WebGL Crystal Prism Background Canvas -// (Optimized with IntersectionObserver: 0% GPU when offscreen) -// ------------------------------------------------------------- -function Hero3DCanvas({ className }: { className?: string }) { - const containerRef = useRef(null); - - useEffect(() => { - const container = containerRef.current; - if (!container) return; - - let width = container.clientWidth || 800; - let height = container.clientHeight || 800; - - const scene = new THREE.Scene(); - const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100); - camera.position.z = 4.8; - - let renderer: THREE.WebGLRenderer | null = null; - try { - renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true, powerPreference: 'high-performance' }); - renderer.setSize(width, height); - renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2)); - container.appendChild(renderer.domElement); - } catch (e) { - return; - } - - // Master Group for smooth coordinated rotation - const masterGroup = new THREE.Group(); - scene.add(masterGroup); - - // 1. Central Optical Translucent Crystal (Icosahedron, mobile GPU safe) - const crystalGeo = new THREE.IcosahedronGeometry(1.25, 0); - const crystalMat = new THREE.MeshStandardMaterial({ - color: 0x3b82f6, - emissive: 0x1d4ed8, - emissiveIntensity: 0.12, - roughness: 0.18, - metalness: 0.1, - transparent: true, - opacity: 0.14, - }); - const crystal = new THREE.Mesh(crystalGeo, crystalMat); - masterGroup.add(crystal); - - // Wireframe Fine Lines for the Crystal - const wireGeo = new THREE.WireframeGeometry(crystalGeo); - const wireMat = new THREE.LineBasicMaterial({ - color: 0x60a5fa, - transparent: true, - opacity: 0.22, - }); - const wireframe = new THREE.LineSegments(wireGeo, wireMat); - crystal.add(wireframe); - - // 2. Dual Gyro Gimbal Orbital Rings (Sleek Apple/Quantum Aesthetic) - // Ring 1 (Cyan orbital ring) - const ring1Geo = new THREE.TorusGeometry(2.15, 0.007, 16, 120); - const ring1Mat = new THREE.MeshBasicMaterial({ - color: 0x38bdf8, - transparent: true, - opacity: 0.20, - }); - const ring1 = new THREE.Mesh(ring1Geo, ring1Mat); - ring1.rotation.x = Math.PI / 3.2; - masterGroup.add(ring1); - - // Ring 2 (Indigo tilted orbital ring) - const ring2Geo = new THREE.TorusGeometry(1.8, 0.007, 16, 100); - const ring2Mat = new THREE.MeshBasicMaterial({ - color: 0x818cf8, - transparent: true, - opacity: 0.16, - }); - const ring2 = new THREE.Mesh(ring2Geo, ring2Mat); - ring2.rotation.y = Math.PI / 4; - ring2.rotation.z = Math.PI / 6; - masterGroup.add(ring2); - - // 3. Subtle Constellation of Ambient Starlight Particles - const particleCount = 40; - const particleGeo = new THREE.BufferGeometry(); - const posArray = new Float32Array(particleCount * 3); - for (let i = 0; i < particleCount * 3; i++) { - posArray[i] = (Math.random() - 0.5) * 7.5; - } - particleGeo.setAttribute('position', new THREE.BufferAttribute(posArray, 3)); - const particleMat = new THREE.PointsMaterial({ - size: 0.03, - color: 0x93c5fa, - transparent: true, - opacity: 0.35, - }); - const particles = new THREE.Points(particleGeo, particleMat); - masterGroup.add(particles); - - // 4. Studio Lighting - const ambientLight = new THREE.AmbientLight(0xffffff, 1.2); - scene.add(ambientLight); - - const cyanLight = new THREE.PointLight(0x06b6d4, 2.2, 12); - cyanLight.position.set(3.5, 3, 3); - scene.add(cyanLight); - - const indigoLight = new THREE.PointLight(0x6366f1, 1.8, 12); - indigoLight.position.set(-3.5, -2, 2.5); - scene.add(indigoLight); - - let animId = 0; - let clock = new THREE.Clock(); - let isVisible = true; - - // IntersectionObserver to pause loop when scrolled out of view (0% CPU/GPU offscreen) - const observer = new IntersectionObserver( - (entries) => { - entries.forEach((entry) => { - isVisible = entry.isIntersecting; - }); - }, - { threshold: 0.05 } - ); - observer.observe(container); - - const animate = () => { - animId = requestAnimationFrame(animate); - if (!isVisible) return; - - const elapsed = clock.getElapsedTime(); - - // Fluid crystal rotation - crystal.rotation.x = elapsed * 0.12; - crystal.rotation.y = elapsed * 0.16; - - // Gimbal ring orbital movements - ring1.rotation.z = elapsed * 0.14; - ring1.rotation.x = Math.PI / 3.2 + Math.sin(elapsed * 0.25) * 0.08; - - ring2.rotation.y = -elapsed * 0.11; - ring2.rotation.z = Math.PI / 6 + Math.cos(elapsed * 0.2) * 0.1; - - // Particle constellation subtle drift - particles.rotation.y = elapsed * 0.03; - particles.rotation.x = elapsed * 0.02; - - if (renderer) { - try { - renderer.render(scene, camera); - } catch { - cancelAnimationFrame(animId); - } - } - }; - - animate(); - - const canvas = renderer.domElement; - const handleContextLost = (e: Event) => { - e.preventDefault(); - cancelAnimationFrame(animId); - }; - canvas.addEventListener('webglcontextlost', handleContextLost, false); - - const handleResize = () => { - if (!container || !renderer) return; - const w = container.clientWidth; - const h = container.clientHeight; - camera.aspect = w / h; - camera.updateProjectionMatrix(); - renderer.setSize(w, h); - }; - - window.addEventListener('resize', handleResize, { passive: true }); - - return () => { - cancelAnimationFrame(animId); - observer.disconnect(); - canvas.removeEventListener('webglcontextlost', handleContextLost); - window.removeEventListener('resize', handleResize); - if (renderer && renderer.domElement && container.contains(renderer.domElement)) { - container.removeChild(renderer.domElement); - } - crystalGeo.dispose(); - crystalMat.dispose(); - wireGeo.dispose(); - wireMat.dispose(); - ring1Geo.dispose(); - ring1Mat.dispose(); - ring2Geo.dispose(); - ring2Mat.dispose(); - particleGeo.dispose(); - particleMat.dispose(); - renderer?.dispose(); - }; - }, []); - - return
; -} diff --git a/src/components/ui/SmoothScrollProvider.tsx b/src/components/ui/SmoothScrollProvider.tsx index ba4afa2..6b26651 100644 --- a/src/components/ui/SmoothScrollProvider.tsx +++ b/src/components/ui/SmoothScrollProvider.tsx @@ -1,61 +1,72 @@ 'use client'; import React, { useEffect } from 'react'; -import Lenis from 'lenis'; interface SmoothScrollProviderProps { children: React.ReactNode; } /** - * High-performance, lightweight Lenis Smooth Scrolling Provider. - * Synchronizes with display refresh rate (60-120fps) via requestAnimationFrame. - * Features: - * - Fluid exponential easing (Apple/Linear quality) - * - Native momentum & touch compatibility - * - Smooth scroll interception for internal anchor links (#our-tools, #recommended-apps, etc.) - * - Automatic offset handling for sticky floating navigation - * - Respects prefers-reduced-motion + * Universal Smooth Scrolling Provider. + * - On iOS / Android / Mobile touch devices: Keeps native hardware-accelerated 120Hz momentum + * scrolling intact, completely avoiding WebKit JS scroll conflicts and parser overhead. + * - On Desktop & Trackpads: Dynamically loads high-performance exponential easing Lenis engine. + * - Handles anchor link smooth scrolling safely across all devices. */ export function SmoothScrollProvider({ children }: SmoothScrollProviderProps) { useEffect(() => { - // Only run on client-side and non-reduced-motion environments + // Only run in client browser if (typeof window === 'undefined') return; - const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; - if (prefersReducedMotion) return; - - let lenis: Lenis | null = null; + let lenis: any = null; let animId = 0; + let isDisposed = false; - try { - // Initialize ultra-lightweight Lenis instance - lenis = new Lenis({ - duration: 1.15, - easing: (t: number) => Math.min(1, 1.001 - Math.pow(2, -10 * t)), // Silky exponential ease-out - orientation: 'vertical', - gestureOrientation: 'vertical', - smoothWheel: true, - syncTouch: false, // Preserves native 120Hz iOS/Android touch momentum without interference - wheelMultiplier: 0.95, - touchMultiplier: 1.0, - infinite: false, - }); + // Detect mobile / touch devices: iOS (iPhone, iPad, iPod) and touch screens + const isTouchDevice = + 'ontouchstart' in window || + navigator.maxTouchPoints > 0 || + (window.matchMedia && window.matchMedia('(pointer: coarse)').matches && !window.matchMedia('(pointer: fine)').matches); - // Expose lenis instance globally for programmatic scrolling - (window as any).__lenis = lenis; + const prefersReducedMotion = + window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches; - // Locked high-frequency RAF loop - const raf = (time: number) => { - lenis?.raf(time); - animId = requestAnimationFrame(raf); - }; - animId = requestAnimationFrame(raf); - } catch { - // Fallback silently to native smooth scrolling + // Only load Lenis for non-touch desktop environments that haven't requested reduced motion + if (!isTouchDevice && !prefersReducedMotion) { + import('lenis') + .then(({ default: Lenis }) => { + if (isDisposed) return; + try { + lenis = new Lenis({ + duration: 1.15, + easing: (t: number) => Math.min(1, 1.001 - Math.pow(2, -10 * t)), + orientation: 'vertical', + gestureOrientation: 'vertical', + smoothWheel: true, + syncTouch: false, + wheelMultiplier: 0.95, + touchMultiplier: 1.0, + infinite: false, + }); + + (window as any).__lenis = lenis; + + const raf = (time: number) => { + if (isDisposed) return; + lenis?.raf(time); + animId = requestAnimationFrame(raf); + }; + animId = requestAnimationFrame(raf); + } catch { + // Graceful fallback to native scrolling + } + }) + .catch(() => { + // Graceful fallback if dynamic import fails + }); } - // Intercept in-page anchor links for seamless animated scrolling (safe on text nodes on mobile) + // Universal anchor link handler (safe on text nodes and SVG elements across mobile & desktop) const handleAnchorClick = (e: MouseEvent) => { try { const target = e.target as Node | null; @@ -73,9 +84,9 @@ export function SmoothScrollProvider({ children }: SmoothScrollProviderProps) { const targetElement = document.querySelector(href); if (targetElement) { e.preventDefault(); - if (lenis) { + if (lenis && typeof lenis.scrollTo === 'function') { lenis.scrollTo(targetElement as HTMLElement, { - offset: -80, // Offset for floating glass navbar + offset: -80, duration: 1.25, easing: (t: number) => Math.min(1, 1.001 - Math.pow(2, -10 * t)), }); @@ -84,16 +95,19 @@ export function SmoothScrollProvider({ children }: SmoothScrollProviderProps) { } } } catch { - // Fallback gracefully to default navigation if querySelector fails + // Fallback gracefully } }; document.addEventListener('click', handleAnchorClick, { capture: true }); return () => { - cancelAnimationFrame(animId); + isDisposed = true; + if (animId) cancelAnimationFrame(animId); document.removeEventListener('click', handleAnchorClick, { capture: true }); - lenis?.destroy(); + if (lenis && typeof lenis.destroy === 'function') { + lenis.destroy(); + } delete (window as any).__lenis; }; }, []);