fix(ios): resolve iOS Safari WebKit SyntaxError: Unexpected token '{' via SWC package transpilation and dynamic loader
This commit is contained in:
@@ -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({
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// Interactive 3D WebGL Crystal Prism Background Canvas
|
||||
// (Optimized with IntersectionObserver: 0% GPU when offscreen)
|
||||
// -------------------------------------------------------------
|
||||
function Hero3DCanvas({ className }: { className?: string }) {
|
||||
const containerRef = useRef<HTMLDivElement>(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 <div ref={containerRef} className={className} />;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user