/* * qq-fx.js —— 首页 / 登录页的「炫酷」动效层(纯前端,无依赖) * --------------------------------------------------------------------------- * 只在两处生效:.page-content.home(首页)、.page-content.user.signin(登录页)。 * 其余页面直接 return,一点开销都不加。 * * 做三件事: * 1. 背景粒子星座(canvas,40 个慢速漂移的点 + 近邻连线,颜色取 --color-primary) * 2. 首页大标题下的口号打字机(data-qq-type),打完留着光标闪烁 * 3. 首页 hero 跟随鼠标的柔和光斑(只改两个 CSS 变量,不做重排) * * 三处护栏: * - prefers-reduced-motion: reduce → 全部动画关闭,只留静态样式 * - 标签页切到后台 → 停掉 rAF,回来再继续 * - 设备像素比封顶 2,避免 4K 屏把 canvas 撑爆 */ (function () { 'use strict'; var page = document.querySelector('.page-content.home, .page-content.user.signin'); if (!page) return; var isHome = page.classList.contains('home'); var body = document.body; body.classList.add('qq-fx-active', isHome ? 'qq-fx-home' : 'qq-fx-login'); var reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; /* ---------------- 1. 背景粒子星座 ---------------- */ function initParticles() { var canvas = document.createElement('canvas'); canvas.id = 'qq-fx-canvas'; canvas.setAttribute('aria-hidden', 'true'); body.appendChild(canvas); var ctx = canvas.getContext('2d'); var dpr = Math.min(window.devicePixelRatio || 1, 2); var w = 0, h = 0, dots = [], raf = 0, running = true; function accent() { var v = getComputedStyle(body).getPropertyValue('--color-primary').trim(); return v || '#3B82F6'; } var color = accent(); var LINK_DIST = 132; function resize() { w = canvas.clientWidth; h = canvas.clientHeight; canvas.width = Math.round(w * dpr); canvas.height = Math.round(h * dpr); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); // 点数量跟面积走,手机少一点 var count = Math.max(18, Math.min(44, Math.round((w * h) / 42000))); dots = []; for (var i = 0; i < count; i++) { dots.push({ x: Math.random() * w, y: Math.random() * h, vx: (Math.random() - 0.5) * 0.22, vy: (Math.random() - 0.5) * 0.22, r: 1 + Math.random() * 1.6 }); } } function frame() { ctx.clearRect(0, 0, w, h); for (var i = 0; i < dots.length; i++) { var d = dots[i]; d.x += d.vx; d.y += d.vy; if (d.x < -20) d.x = w + 20; if (d.x > w + 20) d.x = -20; if (d.y < -20) d.y = h + 20; if (d.y > h + 20) d.y = -20; } // 近邻连线 ctx.lineWidth = 1; for (var a = 0; a < dots.length; a++) { for (var b = a + 1; b < dots.length; b++) { var dx = dots[a].x - dots[b].x; var dy = dots[a].y - dots[b].y; var dist = Math.sqrt(dx * dx + dy * dy); if (dist > LINK_DIST) continue; ctx.globalAlpha = (1 - dist / LINK_DIST) * 0.18; ctx.strokeStyle = color; ctx.beginPath(); ctx.moveTo(dots[a].x, dots[a].y); ctx.lineTo(dots[b].x, dots[b].y); ctx.stroke(); } } // 点 ctx.globalAlpha = 0.55; ctx.fillStyle = color; for (var k = 0; k < dots.length; k++) { ctx.beginPath(); ctx.arc(dots[k].x, dots[k].y, dots[k].r, 0, Math.PI * 2); ctx.fill(); } ctx.globalAlpha = 1; if (running) raf = requestAnimationFrame(frame); } resize(); window.addEventListener('resize', function () { cancelAnimationFrame(raf); resize(); if (running) raf = requestAnimationFrame(frame); }); // 深浅色切换后换一版粒子颜色 var mq = window.matchMedia('(prefers-color-scheme: dark)'); if (mq.addEventListener) { mq.addEventListener('change', function () { setTimeout(function () { color = accent(); }, 50); }); } document.addEventListener('visibilitychange', function () { if (document.hidden) { running = false; cancelAnimationFrame(raf); } else if (!reduceMotion) { running = true; raf = requestAnimationFrame(frame); } }); if (reduceMotion) { // 关掉动画:只画一帧静态星座 running = false; frame(); } else { raf = requestAnimationFrame(frame); } } /* ---------------- 2. 口号打字机 ---------------- */ function initTyping() { var el = document.querySelector('[data-qq-type]'); if (!el) return; var text = (el.getAttribute('data-qq-type') || el.textContent || '').trim(); if (!text) return; if (reduceMotion) { el.textContent = text; el.classList.add('qq-typed'); return; } el.textContent = ''; el.classList.add('qq-typing'); var i = 0; var timer = setInterval(function () { el.textContent = text.slice(0, ++i); if (i >= text.length) { clearInterval(timer); el.classList.remove('qq-typing'); el.classList.add('qq-typed'); } }, 95); } /* ---------------- 3. hero 跟随鼠标的光斑 ---------------- */ function initSpotlight() { var hero = document.querySelector('.qq-hero'); if (!hero || reduceMotion) return; hero.addEventListener('mousemove', function (e) { var r = hero.getBoundingClientRect(); hero.style.setProperty('--qq-mx', (e.clientX - r.left) + 'px'); hero.style.setProperty('--qq-my', (e.clientY - r.top) + 'px'); }); hero.addEventListener('mouseleave', function () { hero.style.setProperty('--qq-mx', '50%'); hero.style.setProperty('--qq-my', '40%'); }); } initParticles(); initTyping(); initSpotlight(); })();