DOT MATRIX
点阵近邻高亮
Proximity dot matrix
暗夜背景中的圆点矩阵随鼠标靠近逐颗亮起并放大,离开后回落成静谧网格
常见大白话搜法:点阵矩阵随鼠标变大、近邻感应圆点网格、Linear 点阵光波、Dot Matrix Hover
- 适用位置
- 背景、卡片列表
- 气质感觉
- 科技感、利落
- 触发方式
- 悬停
- 适合网站
- 品牌官网、工具和应用、个人网站
实现原理与无障碍底线
按行列摆好等距圆点网格;监听指针移动并计算每颗圆点中心到指针坐标的欧氏距离 dist,按半径 radius 算出衰减系数 k = max(0, 1 - dist / radius),同步把每颗圆点的 scale 映射到 1 至 maxScale、opacity 映射到 0.24 至 1;只改合成层属性 scale 与 opacity,离场或触屏松手后复位,减少动态时保持静谧网格。
适用场景:暗色科技官网背景、特性介绍区底座纹理、交互式工程感装饰墙
减少动态(prefers-reduced-motion):关闭随指针扩散放大计算,保持静态均布细点阵底纹
移动端与触屏:移动端触按点附近局部点亮,松手后恢复均匀网格,不影响纵向滑动
关键可调参数
| 参数 | 键名 | 默认值 | 建议范围 | 说明 |
|---|---|---|---|---|
| 感应半径 | radius | 88px | — | — |
| 最大倍率 | maxScale | 2.2倍 | — | — |
可直接复制给 AI(Cursor / Claude / v0)的提示词
请在我的项目里给背景加上「点阵近邻高亮」(Proximity dot matrix)效果:按行列摆好等距圆点网格;监听指针移动并计算每颗圆点中心到指针坐标的欧氏距离 dist,按半径 radius 算出衰减系数 k = max(0, 1 - dist / radius),同步把每颗圆点的 scale 映射到 1 至 maxScale、opacity 映射到 0.24 至 1;只改合成层属性 scale 与 opacity,离场或触屏松手后复位,减少动态时保持静谧网格。
参数:感应半径 88px,最大倍率 2.2倍。
要求:保留原有的内容和布局;系统开启"减少动态"时直接显示最终状态;脚本出错或没有执行时内容仍然可见。
参考:https://donglang.linter.top/effects/proximity-grid.md(原理、参数和参考代码)原生无依赖参考代码
CSS
.pg-grid {
position: relative;
display: grid;
grid-template-columns: repeat(7, 1fr);
place-items: center;
gap: 10px;
padding: 18px;
border-radius: 14px;
}
.pg-dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
opacity: 0.24;
transform-origin: 50% 50%;
will-change: scale, opacity;
}JavaScript
// 点阵近邻高亮(Proximity dot matrix)· 原生 JS,不依赖任何库。
// 用法:const ctl = proximityGrid(root, { radius: 88, maxScale: 2.2 });
// 返回 { update(x, y), reset(), destroy() },坐标相对 root 左上角像素。
export function proximityGrid(root, {
radius = 88,
maxScale = 2.2,
ignoreReducedMotion = false,
} = {}) {
if (!root) return null;
const dots = [...root.querySelectorAll('.pg-dot')];
if (!dots.length) return null;
const reduce = !ignoreReducedMotion && matchMedia('(prefers-reduced-motion: reduce)').matches;
const baseOpacity = 0.24;
const reset = () => {
for (const dot of dots) {
dot.style.scale = '1';
dot.style.opacity = String(baseOpacity);
}
};
const update = (x, y) => {
if (reduce) return;
const area = root.getBoundingClientRect();
const rLimit = Math.max(1, radius);
const sx = area.width ? root.offsetWidth / area.width : 1;
const sy = area.height ? root.offsetHeight / area.height : 1;
for (const dot of dots) {
const box = dot.getBoundingClientRect();
const cx = (box.left - area.left + box.width / 2) * sx;
const cy = (box.top - area.top + box.height / 2) * sy;
const dist = Math.hypot(cx - x, cy - y);
const k = Math.max(0, 1 - dist / rLimit);
const s = 1 + (maxScale - 1) * k;
const op = baseOpacity + (1 - baseOpacity) * k;
dot.style.scale = s.toFixed(4);
dot.style.opacity = op.toFixed(4);
}
};
const local = (e) => {
const r = root.getBoundingClientRect();
const kx = r.width ? root.offsetWidth / r.width : 1;
const ky = r.height ? root.offsetHeight / r.height : 1;
return [(e.clientX - r.left) * kx - root.clientLeft, (e.clientY - r.top) * ky - root.clientTop];
};
const listeners = [
['pointerenter', (e) => { if (e.pointerType !== 'touch') update(...local(e)); }],
['pointermove', (e) => { if (e.pointerType !== 'touch') update(...local(e)); }],
['pointerleave', (e) => { if (e.pointerType !== 'touch') reset(); }],
['pointerdown', (e) => { if (e.pointerType === 'touch') update(...local(e)); }],
['pointerup', (e) => { if (e.pointerType === 'touch') reset(); }],
['pointercancel', (e) => { if (e.pointerType === 'touch') reset(); }],
];
listeners.forEach(([type, fn]) => root.addEventListener(type, fn));
reset();
return {
update,
reset,
destroy() {
listeners.forEach(([type, fn]) => root.removeEventListener(type, fn));
for (const dot of dots) {
dot.style.removeProperty('scale');
dot.style.removeProperty('opacity');
}
},
};
}HTML
<div class="pg-grid" data-matrix>
<i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i>
<i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i>
<i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i><i class="pg-dot"></i>
</div>