Proximity dot matrix
点阵近邻高亮
Illuminate and scale dots in a precision matrix based on pointer distance.
Also searched as: 点阵矩阵随鼠标变大, 近邻感应圆点网格, Linear 点阵光波, Dot Matrix Hover
- Placement
- Background, Card list
- Mood
- Technical, Crisp
- Trigger
- 悬停
- Site types
- 品牌官网, 工具和应用, 个人网站
How it works & accessibility
Computes Euclidean distance from the pointer to each dot center on pointermove, smoothly scaling dots up to maxScale and raising opacity within radius px while updating only compositor properties.
Best for: Background, Card list (悬停).
Reduced motion: 关闭随指针扩散放大计算,保持静态均布细点阵底纹
Mobile & touch: 移动端触按点附近局部点亮,松手后恢复均匀网格,不影响纵向滑动
Key parameters
| Parameter | Key | Default | Range | Notes |
|---|---|---|---|---|
| 感应半径 | radius | 88px | — | — |
| 最大倍率 | maxScale | 2.2倍 | — | — |
Ready-to-paste AI prompt
Add “Proximity dot matrix” to the background in my project: Computes Euclidean distance from the pointer to each dot center on pointermove, smoothly scaling dots up to maxScale and raising opacity within radius px while updating only compositor properties.
Parameters: radius: 88px, maxScale: 2.2×.
Preserve existing content and layout. Respect prefers-reduced-motion by showing the final state. Content must remain visible if JavaScript fails or is disabled.
Reference: https://donglang.linter.top/effects/proximity-grid.md (canonical specification and reference code).Dependency-free reference code
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>