# 发光网格 Grid Glow > 深色背景上一层细方格,鼠标经过的格子亮起来再慢慢淡回去,留下一条发光的轨迹。 分类:鼠标特效 · 角色:主角(占一整块、抢全部注意力,一页最多一个) [全部组件和流程](https://www.dachuiai.com/llms.txt) · [鼠标特效选型规则](https://www.dachuiai.com/guides/cursor.md) ## 给 AI:用户把这个组件发给了你 1. 先看用户的项目:框架、页面类型、现在的风格、手机还是电脑为主。 2. 对照下面的「适合 / 不适合」判断合不合适。不合适就直接说,并从[鼠标特效选型规则](https://www.dachuiai.com/guides/cursor.md)或[全部组件](https://www.dachuiai.com/llms.txt)里另挑一个。 3. 合适就按「装进项目」装好,示例里的文字和图片换成用户自己的。 4. 告诉用户用了什么、放在哪个文件、以后想改快慢、颜色、大小改哪个参数。 ## 什么时候用 - 适合:开发者工具、AI、科技类 SaaS 的深色首屏背景。 - 不适合:浅色网站(亮格子在白底上看不出来);已经有图片、视频或 WebGL 背景;后台。 - 适合的网站:SaaS、官网、活动页 - 放在:首屏、背景 - 手机上:没效果:手机上只有静态的格子。 - 性能代价:低:Canvas 2D,没东西要画就停。 - 常搭配:[乱码解码 Scramble Text](https://www.dachuiai.com/c/scramble-text.md)、[磁吸 Magnetic](https://www.dachuiai.com/c/magnetic.md) ## 装进项目 - 写法:React 18 以上 + TypeScript + Tailwind CSS v4,一个文件就能用,不引用别的组件。 - 依赖:只用 react,不用另外安装 - 文件:原样存成 `grid-glow.tsx`(比如 `src/components/grid-glow.tsx`)。能跑命令就下载[原文](https://www.dachuiai.com/code/grid-glow.tsx),比手抄准:`curl -fsSL -o src/components/grid-glow.tsx https://www.dachuiai.com/code/grid-glow.tsx`(Windows 写 `curl.exe`)。下载完看一眼第一行是 `import`,不是网页。 - 导出:`GridGlow` 是可以直接用的部分;最后的 `export default` 是演示示例,只作参考。 - 注意:格子画在内容下面的 canvas 上;from、to 是左右两头的颜色,要写成 #RRGGBB。 - 保留代码里 `prefers-reduced-motion` 的处理:用户开了「减弱动态效果」时内容照样完整显示。 - Next.js(有 app 文件夹):文件第一行加 `'use client';`。 - 用 JavaScript 不用 TypeScript:Vite 和 Next.js 都能直接用 .tsx 文件;也可以删掉类型标注,改名 .jsx。 - 没有 Tailwind:先装 Tailwind CSS v4(Vite 项目 `npm i tailwindcss @tailwindcss/vite`,vite 配置的 plugins 里加 `tailwindcss()`,入口引入的那个 CSS 文件(一般是 `src/index.css`)第一行写 `@import "tailwindcss";`),或者把类名改写成项目用的样式。 - 已经装了 Tailwind v3:别为了一个组件升级。类名大多通用,少数 v4 写法要换,比如焦点框 `outline-2` 在 v3 里要写成 `outline outline-2`。 - 不是 React(Vue、Svelte、小程序、纯 HTML):照着代码的做法改写,时长、曲线、参数保持一样。Wix、有赞这类建站平台装不进代码,告诉用户去平台自带的动效设置里找。 ## 参数 | 参数 | 默认值 | 作用 | | --- | --- | --- | | `children` | | 包在里面的内容 | | `cell` | `40` | 格子边长(px) | | `fade` | `0.8` | 亮起的格子几秒淡回去 | | `from` | `'#7CFFB2'` | 左边的颜色 | | `to` | `'#38E1FF'` | 右边的颜色 | | `className` | `''` | 加在最外层的类名,用来调大小、位置、间距 | ## 你可以说 > 深色背景铺一层 40 像素的方格:鼠标经过的格子亮起主色,0.8 秒内淡回去,留下一条渐暗的轨迹。 ## 完整代码 ```tsx import {useEffect, useRef, type ReactNode} from 'react'; /* --------------------------------------------------------------- * GridGlow 发光网格 * 底下铺一层细方格:鼠标经过的格子亮起主色,再慢慢淡回去,留下一条渐暗的轨迹; * 四周相邻的格子带一点柔光,鼠标附近的格线也微微亮起。颜色从左到右在两种颜色之间过渡。 * 鼠标走得快也不会跳格:两次位置之间经过的格子都会点亮。没东西要画时就停下,不占动画。 * 只在有鼠标的设备上发光;手机、平板上只有静态的格子。开了「减弱动态效果」只亮鼠标下面那一格,不留轨迹。 * cell:格子边长(px) * fade:亮起的格子几秒淡回去 * from:左边的颜色 * to:右边的颜色 * ------------------------------------------------------------- */ type Props = {children?: ReactNode; cell?: number; fade?: number; from?: string; to?: string; className?: string}; const rgb = (hex: string) => [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16)); export function GridGlow({children, cell = 40, fade = 0.8, from = '#7CFFB2', to = '#38E1FF', className = ''}: Props) { const root = useRef(null); const canvas = useRef(null); useEffect(() => { const el = root.current!, cv = canvas.current!, ctx = cv.getContext('2d')!; const live = matchMedia('(pointer: fine)').matches, reduce = matchMedia('(prefers-reduced-motion: reduce)').matches; const [a, b] = [rgb(from), rgb(to)]; let W = 0, H = 0, cols = 0, rows = 0, ox = 0, oy = 0, energy = new Float32Array(0); let raf = 0, prev = 0, last: {x: number; y: number} | null = null; const mouse = {x: 0, y: 0, in: false}; const draw = (dt: number) => { ctx.clearRect(0, 0, W, H); ctx.lineWidth = 1; ctx.strokeStyle = 'rgba(255,255,255,.055)'; ctx.beginPath(); for (let c = 0; c <= cols; c++) { const x = Math.round(ox + c * cell) + 0.5; ctx.moveTo(x, 0); ctx.lineTo(x, H); } for (let r = 0; r <= rows; r++) { const y = Math.round(oy + r * cell) + 0.5; ctx.moveTo(0, y); ctx.lineTo(W, y); } ctx.stroke(); // 鼠标附近的格线亮一圈 if (mouse.in) { const g = ctx.createRadialGradient(mouse.x, mouse.y, 0, mouse.x, mouse.y, cell * 6); g.addColorStop(0, `rgba(${a},.3)`); g.addColorStop(1, `rgba(${a},0)`); ctx.strokeStyle = g; ctx.stroke(); } let busy = false; for (let r = 0; r < rows; r++) for (let c = 0; c < cols; c++) { const i = r * cols + c, e = energy[i]; if (e <= 0) continue; if (!reduce) energy[i] = Math.max(0, e - dt / fade); busy = busy || energy[i] > 0; const k = e * e * (3 - 2 * e); // 先亮着,再放手 const t = c / cols, col = a.map((v, j) => Math.round(v + (b[j] - v) * t)).join(','); const x = ox + c * cell, y = oy + r * cell; ctx.shadowColor = `rgba(${col},${0.9 * k})`; ctx.shadowBlur = 24 * k; ctx.fillStyle = `rgba(${col},${0.78 * k})`; ctx.fillRect(x + 1.5, y + 1.5, cell - 2, cell - 2); ctx.shadowBlur = 0; ctx.strokeStyle = `rgba(235,255,250,${0.75 * k})`; ctx.lineWidth = 1.5; ctx.strokeRect(x + 1.5, y + 1.5, cell - 2, cell - 2); } return busy; }; const frame = (now: number) => { const busy = draw(Math.min(0.05, Math.max(0, now - prev) / 1000)); prev = now; raf = busy && !reduce ? requestAnimationFrame(frame) : 0; }; const kick = () => { if (!raf) { prev = performance.now(); raf = requestAnimationFrame(frame); } }; const fit = () => { const d = Math.min(2, devicePixelRatio || 1); W = el.clientWidth; H = el.clientHeight; cv.width = Math.round(W * d); cv.height = Math.round(H * d); ctx.setTransform(d, 0, 0, d, 0, 0); cols = Math.ceil(W / cell) + 1; rows = Math.ceil(H / cell) + 1; ox = (W - cols * cell) / 2; oy = (H - rows * cell) / 2; // 居中,左右两边切到的格子一样宽 energy = new Float32Array(cols * rows); draw(0); }; const put = (c: number, r: number, e: number) => { if (c >= 0 && r >= 0 && c < cols && r < rows && energy[r * cols + c] < e) energy[r * cols + c] = e; }; const light = (x: number, y: number) => { const c = Math.floor((x - ox) / cell), r = Math.floor((y - oy) / cell); put(c, r, 1); put(c + 1, r, 0.34); put(c - 1, r, 0.34); put(c, r + 1, 0.34); put(c, r - 1, 0.34); }; const move = (e: PointerEvent) => { if (e.pointerType === 'touch') return; const box = el.getBoundingClientRect(), x = e.clientX - box.left, y = e.clientY - box.top; Object.assign(mouse, {x, y, in: true}); if (reduce) energy.fill(0); // 两次位置之间经过的格子都点亮,走得快也不跳格 if (last && !reduce) { const n = Math.max(1, Math.ceil(Math.hypot(x - last.x, y - last.y) / (cell / 3))); for (let i = 1; i <= n; i++) light(last.x + ((x - last.x) * i) / n, last.y + ((y - last.y) * i) / n); } else light(x, y); last = {x, y}; if (reduce) draw(0); else kick(); }; const leave = () => { mouse.in = false; last = null; if (reduce) { energy.fill(0); draw(0); } else kick(); }; fit(); const ro = new ResizeObserver(fit); ro.observe(el); if (live) { el.addEventListener('pointermove', move); el.addEventListener('pointerleave', leave); } return () => { cancelAnimationFrame(raf); ro.disconnect(); el.removeEventListener('pointermove', move); el.removeEventListener('pointerleave', leave); }; }, [cell, fade, from, to]); return (
{children}
); } /* ---------------- 示例:开发者工具的首页 ---------------- */ const MONO = '"JetBrains Mono", "SF Mono", ui-monospace, Menlo, monospace'; const stay = (e: {preventDefault: () => void}) => e.preventDefault(); export default function GridGlowDemo() { return (
2.0 新版本上线

把想法
变成产品

给开发者的一整套工具,从第一行代码到上线。

$npm create my-app
); } ```