全部效果悬停与跟随

速度拉伸光标

Velocity Stretch Cursor

快速移动时装饰指示点沿运动方向被拉长,减速后恢复圆形。

移动指针、点按或使用键盘

适用场景

适合绘图工具或首屏视觉中的空间运动反馈。 把装饰标记放在独立的互动区,手势变慢后让它尽快恢复圆形。操作按钮的地方继续使用系统光标。

应用示例

海报工具的动效练习画布上,用户用指针快速横扫时,小圆点沿运动方向拉长。动作慢下来后,小圆点恢复圆形,快慢变化一眼可见。

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(circle,#b3cdff6e 1px,transparent 1.5px) 0 0 / 13px 13px, linear-gradient(130deg,#202652,#182a37); }
    .velocity-cursor { position:absolute; width:22px; height:22px; border-radius:50%; top:var(--y); left:var(--x); background:#bdffda; box-shadow:0 0 15px #83e1bd94; transform:translate(-50%,-50%) rotate(var(--angle,0deg)) scaleX(var(--stretch,1)); }
    .pointer-stage:is(:hover, :focus-visible, [aria-pressed="true"]) .velocity-cursor { background:#f4ffd5; }

    @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"><span class="velocity-cursor" aria-hidden="true"></span><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]));
    });

    let previous = null;
    let strength = 1;
    let timestamp = 0;
    let frame = 0;
    function decay(now) {
      const time = now - timestamp;
      timestamp = now;
      strength = 1 + (strength - 1) * Math.exp(-time / 90);
      stage.style.setProperty('--stretch', strength.toFixed(3));
      frame = strength > 1.01 ? requestAnimationFrame(decay) : 0;
    }
    function stretch(amount) {
      strength = amount;
      timestamp = performance.now();
      stage.style.setProperty('--stretch', strength);
      if (!frame) frame = requestAnimationFrame(decay);
    }
    stage.addEventListener('pointermove', (event) => {
      if (event.pointerType === 'touch' || reduced.matches) return;
      if (previous) {
        const dx = event.clientX - previous.x, dy = event.clientY - previous.y;
        const speed = Math.hypot(dx,dy) / Math.max(12,event.timeStamp - previous.time);
        if (speed > .18) {
          stage.style.setProperty('--angle', Math.atan2(dy,dx) + 'rad');
          stretch(1 + Math.min(2.5,speed * .55));
        }
      }
      previous = {x:event.clientX,y:event.clientY,time:event.timeStamp};
    });
    stage.addEventListener('pointerleave', () => { previous = null; });
    stage.addEventListener('click', () => { if (!reduced.matches) stretch(2.6); });
    reduced.addEventListener('change', () => {
      cancelAnimationFrame(frame);
      frame = 0;
      previous = null;
      stage.style.removeProperty('--stretch');
    });
  </script>
</body>
</html>

UI Skills 原创示例

相关参考