// Common JavaScript Functions for TeaLab · 空间智能与自主设计 // Initialize Lucide Icons function initIcons() { if (window.lucide) { lucide.createIcons(); } else { console.warn("Lucide icons failed to load."); } } // Mobile Menu Toggle function initMobileMenu() { const menuBtn = document.getElementById('mobile-menu-btn'); const mobileMenu = document.getElementById('mobile-menu'); if (menuBtn && mobileMenu) { menuBtn.addEventListener('click', () => { mobileMenu.classList.toggle('hidden'); }); } } // Navbar scroll effect function initNavbarScroll() { window.addEventListener('scroll', () => { const nav = document.getElementById('navbar'); if (nav) { if (window.scrollY > 50) { nav.classList.add('shadow-sm'); if (nav.classList.contains('h-20')) { nav.classList.replace('h-20', 'h-16'); } } else { nav.classList.remove('shadow-sm'); if (nav.classList.contains('h-16')) { nav.classList.replace('h-16', 'h-20'); } } } }); } // Canvas Animation: Organic Connection Nodes function initCanvasAnimation() { const canvas = document.getElementById('heroCanvas'); if (!canvas) return; const ctx = canvas.getContext('2d'); let width, height; let particles = []; function resize() { width = window.innerWidth; height = window.innerHeight; canvas.width = width; canvas.height = height; } class Particle { constructor() { this.x = Math.random() * width; this.y = Math.random() * height; this.vx = (Math.random() - 0.5) * 0.5; this.vy = (Math.random() - 0.5) * 0.5; this.size = Math.random() * 2 + 1; } update() { this.x += this.vx; this.y += this.vy; if (this.x < 0 || this.x > width) this.vx *= -1; if (this.y < 0 || this.y > height) this.vy *= -1; } draw() { ctx.beginPath(); ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2); ctx.fillStyle = 'rgba(0, 0, 0, 0.1)'; ctx.fill(); } } function initParticles() { particles = []; const particleCount = Math.min(window.innerWidth / 10, 100); for (let i = 0; i < particleCount; i++) { particles.push(new Particle()); } } function animate() { ctx.clearRect(0, 0, width, height); for (let i = 0; i < particles.length; i++) { particles[i].update(); particles[i].draw(); for (let j = i + 1; j < particles.length; j++) { const dx = particles[i].x - particles[j].x; const dy = particles[i].y - particles[j].y; const dist = Math.sqrt(dx * dx + dy * dy); if (dist < 150) { ctx.beginPath(); ctx.strokeStyle = `rgba(0, 0, 0, ${0.05 - dist/3000})`; ctx.lineWidth = 0.5; ctx.moveTo(particles[i].x, particles[i].y); ctx.lineTo(particles[j].x, particles[j].y); ctx.stroke(); } } } requestAnimationFrame(animate); } window.addEventListener('resize', () => { resize(); initParticles(); }); resize(); initParticles(); animate(); } // Visit Counter function initVisitCounter() { const counterElement = document.getElementById('visit-counter'); if (!counterElement) return; let count = parseInt(localStorage.getItem('tealab_visit_count') || '0'); count++; localStorage.setItem('tealab_visit_count', count.toString()); counterElement.textContent = count.toLocaleString(); } // Initialize all common functions document.addEventListener('DOMContentLoaded', () => { initIcons(); initMobileMenu(); initNavbarScroll(); initCanvasAnimation(); initVisitCounter(); });