# 点阵近邻高亮（Proximity dot matrix）

- 俗称：点阵矩阵随鼠标变大；近邻感应圆点网格；Linear 点阵光波；Dot Matrix Hover
- 用在：背景、卡片列表（品牌官网、工具和应用、个人网站）
- 触发：悬停
- 感觉：科技感、利落
- 版本：1，更新于 2026-12-02

## 原理

按行列摆好等距圆点网格；监听指针移动并计算每颗圆点中心到指针坐标的欧氏距离 dist，按半径 radius 算出衰减系数 k = max(0, 1 - dist / radius)，同步把每颗圆点的 scale 映射到 1 至 maxScale、opacity 映射到 0.24 至 1；只改合成层属性 scale 与 opacity，离场或触屏松手后复位，减少动态时保持静谧网格。

适合：暗色科技官网背景、特性介绍区底座纹理、交互式工程感装饰墙。

## 参数

| 参数名 | 含义 | 默认值 | 建议范围 |
|---|---|---|---|
| `radius` | 感应半径 | 88px | 40–180px |
| `maxScale` | 最大倍率 | 2.2倍 | 1.3–3.5倍 |

## 质量要求

- 开启"减少动态"时：关闭随指针扩散放大计算，保持静态均布细点阵底纹。
- 手机上：移动端触按点附近局部点亮，松手后恢复均匀网格，不影响纵向滑动。
- 动画只在播放时改变元素；脚本出错或没有执行时，内容照样可见（不要先用 CSS 把内容藏起来再等脚本显示）。

## 提示词（可以直接交给 AI）

```text
请在我的项目里给背景加上「点阵近邻高亮」（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（原理、参数和参考代码）
```

## 参考代码

HTML 结构：

```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>
```

CSS：

```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;
}
```

JS（原生，不依赖任何库）：

```js
// 点阵近邻高亮（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');
      }
    },
  };
}
```

## 出处

- 本站原创实现（参考暗夜精密点阵近邻高亮交互原理）
