弹性指针网格
Elastic Pointer Lattice
邻近交点被指针拉动,连线网格随之变形并弹回原位。
适用场景
适合交互首屏或视觉实验中可拨动的背景。 网格适合单独占一块有余量的画面,变形时仍保留边界。按钮和说明文字放在比较安静的位置。
应用示例
纺织设计展的小站首页有一片线网。观众用鼠标拨动时,附近的交点牵动相连的线,移开后整片网格慢慢弹回。
AI 提示词
给【目标位置】添加弹性指针网格。把矩形网格表示成带静止坐标的采样节点,向指针吸引附近节点,通过阻尼弹簧回位,并在运动结束后停止重绘。保留键盘操作,并提供减少动态效果时的状态。
示例代码
保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。
查看 HTML
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>弹性指针网格</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.pointer-stage {
--x: 50%; --y: 50%; --dx: 0; --dy: 0;
display: grid; place-items: center; position: relative;
width: min(100%, 280px); height: clamp(106px, 68vh, 174px);
overflow: hidden; isolation: isolate;
border: 1px solid #b8a8ff70; border-radius: 13px;
background: #23213e; color: #fcf9ff; cursor: pointer;
touch-action: manipulation;
}
.pointer-stage:focus-visible { outline-offset: 3px; }
.pointer-stage .tag {
position: absolute; z-index: 4; bottom: 7px; left: 8px; color: #fff;
background: #17152fe8; border-radius: 5px; padding: 3px 7px;
font-size: 10px; line-height: 1.4;
}
.pointer-stage { background: radial-gradient(ellipse at 58% 33%, #263354, #141a33 70%); padding: 0; }
.pointer-stage canvas { width: 100%; height: 100%; display: block; }
@media (prefers-reduced-motion: reduce) {
.lab, .lab *, .lab::before, .lab::after, .lab *::before, .lab *::after {
animation: none !important;
transition: none !important;
scroll-behavior: auto !important;
}
}
</style>
</head>
<body>
<main class="demo">
<button type="button" aria-label="拨动弹簧网格" aria-pressed="false" class="lab pointer-stage"><canvas width="280" height="155" aria-hidden="true"></canvas><span class="tag" aria-hidden="true">弹簧网格</span></button>
</main>
<script>
const stage = document.querySelector('.pointer-stage');
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
function setPosition(x, y) {
stage.style.setProperty('--x', (x * 100) + '%');
stage.style.setProperty('--y', (y * 100) + '%');
stage.style.setProperty('--dx', 2 * x - 1);
stage.style.setProperty('--dy', 2 * y - 1);
}
function position(event) {
const bounds = stage.getBoundingClientRect();
return [clamp((event.clientX - bounds.left) / bounds.width, 0, 1), clamp((event.clientY - bounds.top) / bounds.height, 0, 1)];
}
stage.addEventListener('pointermove', (event) => {
if (event.pointerType !== 'touch' && !reduced.matches) setPosition(...position(event));
});
stage.addEventListener('pointerleave', () => {
if (stage.getAttribute('aria-pressed') !== 'true') setPosition(.5, .5);
});
stage.addEventListener('click', (event) => {
const active = stage.getAttribute('aria-pressed') !== 'true';
stage.setAttribute('aria-pressed', String(active));
setPosition(...(active && event.detail ? position(event) : active ? [.68, .38] : [.5, .5]));
});
const canvas = stage.querySelector('canvas');
const context = canvas.getContext('2d');
const columns = 12;
const rows = 8;
const points = Array.from({length: columns * rows}, (_, index) => {
const col = index % columns, row = Math.floor(index / columns);
const x = 15 + col / (columns - 1) * 250;
const y = 12 + row / (rows - 1) * 128;
return {x, y, homeX:x, homeY:y, vx:0, vy:0};
});
let cursor = null;
let frame = 0;
function paint() {
if (!context) return;
context.clearRect(0, 0, 280, 155);
context.strokeStyle = '#9eecea';
context.lineWidth = 1;
context.beginPath();
points.forEach((point, index) => {
context.moveTo(point.x, point.y);
if (index % columns < columns - 1) context.lineTo(points[index + 1].x, points[index + 1].y);
context.moveTo(point.x, point.y);
if (index + columns < points.length) context.lineTo(points[index + columns].x, points[index + columns].y);
});
context.stroke();
}
function render() {
frame = 0;
let energy = 0;
for (const point of points) {
const dx = point.homeX - (cursor?.[0] ?? -300);
const dy = point.homeY - (cursor?.[1] ?? -300);
const distance = Math.max(1, Math.hypot(dx,dy));
const force = Math.max(0, 57 - distance) * -.44;
const targetX = point.homeX + dx / distance * force;
const targetY = point.homeY + dy / distance * force;
if (reduced.matches) {
point.x = targetX;
point.y = targetY;
point.vx = 0;
point.vy = 0;
} else {
point.vx = (point.vx + (targetX - point.x) * .12) * .73;
point.vy = (point.vy + (targetY - point.y) * .12) * .73;
point.x += point.vx;
point.y += point.vy;
energy += Math.abs(point.vx) + Math.abs(point.vy);
}
}
paint();
if (energy > .12) frame = requestAnimationFrame(render);
}
function move(x, y) {
cursor = x === null ? null : [x * 280, y * 155];
if (!frame) frame = requestAnimationFrame(render);
}
stage.addEventListener('pointermove', (event) => {
if (event.pointerType !== 'touch' && !reduced.matches) move(...position(event));
});
stage.addEventListener('pointerleave', () => {
if (stage.getAttribute('aria-pressed') !== 'true') move(null);
});
stage.addEventListener('click', (event) => {
if (stage.getAttribute('aria-pressed') === 'true') move(...(event.detail ? position(event) : [.68,.39]));
else move(null);
});
paint();
</script>
</body>
</html>UI Skills 原创示例