全部效果悬停与跟随

液态图像扭曲

Liquid Image Distortion

指针附近的像素像水中的倒影一样起伏,移开后逐渐恢复原位。

移动指针观察画面,或点按播放一道涟漪

适用场景

适合图片主导的首屏或作品封面,在指针经过的局部展示材质变化。 重要文字应放在独立的图层,波纹只作用在指针附近,画面主体始终容易辨认。

应用示例

旅行杂志的城市游记可以用一张运河照片做封面。指针从水面划过,附近的像素像水纹一样轻微扭动,移开后画面恢复。

AI 提示词

为【目标图片】添加 Liquid Image Distortion。保留原图作为采样源,在指针周围的小范围内按半径偏移像素采样坐标,使扭曲强度向边缘衰减。点按或键盘激活时播放一次涟漪,静止时停止绘制,减少动态效果时显示原图。

示例代码

保存为 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;
    }

    .liquid-panel {
      display: block; position: relative; width: min(100%, 420px); padding: 0;
      max-width: calc((100vh - 30px) * 2.23);
      overflow: hidden; border: 1px solid #c3d3ff5c; border-radius: 12px;
      background: linear-gradient(135deg, #153a56, #295a69 60%, #885bae);
      color: #eaf8ff; cursor: pointer;
    }
    .liquid-panel canvas { display: block; width: 100%; height: auto; }
    .liquid-panel span {
      position: absolute; right: 7px; bottom: 7px; padding: 3px 7px;
      border: 1px solid #bdd9ff54; border-radius: 5px; background: #112838d9;
      font: 640 10px/1.45 system-ui, sans-serif; pointer-events: none;
    }
    @media (prefers-reduced-motion: reduce) {
      .liquid-panel { cursor: default; }
      .liquid-panel span { display: none; }
    }
  </style>
</head>
<body>
  <main class="demo">
    <button type="button" class="liquid-panel" aria-label="播放图像水纹">
      <canvas width="420" height="188" aria-hidden="true"></canvas>
      <span>点按播放涟漪</span>
    </button>
  </main>
  <script>
    const panel = document.querySelector('.liquid-panel');
    const canvas = panel.querySelector('canvas');
    const context = canvas.getContext('2d', { willReadFrequently: true });
    const motionPreference = matchMedia('(prefers-reduced-motion: reduce)');
    const { width, height } = canvas;

    function paintOriginal() {
      const base = context.createLinearGradient(0, 0, width, height);
      base.addColorStop(0, '#0d2a46');
      base.addColorStop(.55, '#215468');
      base.addColorStop(1, '#392c64');
      context.fillStyle = base;
      context.fillRect(0, 0, width, height);

      const glow = context.createRadialGradient(302, 74, 2, 302, 74, 120);
      glow.addColorStop(0, '#ffe57ab0');
      glow.addColorStop(.42, '#ee91ab55');
      glow.addColorStop(1, '#ffb8ea00');
      context.fillStyle = glow;
      context.fillRect(0, 0, width, height);

      context.save();
      context.beginPath();
      context.arc(302, 70, 39, 0, Math.PI * 2);
      context.clip();
      const sun = context.createLinearGradient(0, 32, 0, 109);
      sun.addColorStop(0, '#fff3bc');
      sun.addColorStop(1, '#fba6a7');
      context.fillStyle = sun;
      for (let y = 30; y < 111; y += 6) context.fillRect(261, y, 82, 4);
      context.restore();

      for (let row = 0; row < 19; row += 1) {
        const shade = context.createLinearGradient(0, 0, width, 0);
        shade.addColorStop(0, '#9cf5dd');
        shade.addColorStop(.46, '#8fd6ff');
        shade.addColorStop(1, '#dab8fa');
        context.strokeStyle = shade;
        context.globalAlpha = row % 4 === 0 ? .86 : .45;
        context.lineWidth = row % 4 === 0 ? 1.55 : .85;
        context.beginPath();
        for (let x = 0; x <= width; x += 3) {
          const y = 39 + row * 8 + Math.sin(x / 47 + row * .19) * 23
            + Math.sin(x / 72 - row * .08) * 19 - x * .058;
          if (x === 0) context.moveTo(x, y);
          else context.lineTo(x, y);
        }
        context.stroke();
      }
      context.globalAlpha = 1;
    }

    paintOriginal();
    const original = context.getImageData(0, 0, width, height);
    const distorted = context.createImageData(width, height);
    const point = { x: width / 2, y: height / 2 };
    let hovering = false;
    let pulseStart = -Infinity;
    let animation = 0;

    function render(time) {
      animation = 0;
      const elapsed = time - pulseStart;
      const pulse = Math.max(0, 1 - elapsed / 950);
      const strength = Math.max(hovering ? 1 : 0, pulse);
      if (motionPreference.matches || strength === 0) {
        context.putImageData(original, 0, 0);
        return;
      }

      const radius = 54 + (hovering ? 0 : 30 * (1 - pulse));
      const left = Math.max(0, Math.floor(point.x - radius));
      const right = Math.min(width, Math.ceil(point.x + radius));
      const top = Math.max(0, Math.floor(point.y - radius));
      const bottom = Math.min(height, Math.ceil(point.y + radius));
      distorted.data.set(original.data);
      for (let y = top; y < bottom; y += 1) {
        for (let x = left; x < right; x += 1) {
          const dx = x - point.x;
          const dy = y - point.y;
          const distance = Math.hypot(dx, dy);
          if (distance >= radius || distance === 0) continue;
          const falloff = (1 - distance / radius) ** 1.6;
          const displacement = Math.sin(distance * .185 - time * .019) * 19 * falloff * strength;
          const sourceX = Math.max(0, Math.min(width - 1, Math.round(x + dx / distance * displacement)));
          const sourceY = Math.max(0, Math.min(height - 1, Math.round(y + dy / distance * displacement)));
          const sourceOffset = (sourceY * width + sourceX) * 4;
          const destinationOffset = (y * width + x) * 4;
          distorted.data[destinationOffset] = original.data[sourceOffset];
          distorted.data[destinationOffset + 1] = original.data[sourceOffset + 1];
          distorted.data[destinationOffset + 2] = original.data[sourceOffset + 2];
        }
      }
      context.putImageData(distorted, 0, 0);
      animation = requestAnimationFrame(render);
    }

    function schedule() {
      if (!animation) animation = requestAnimationFrame(render);
    }

    function locate(event) {
      const bounds = canvas.getBoundingClientRect();
      point.x = (event.clientX - bounds.left) / bounds.width * width;
      point.y = (event.clientY - bounds.top) / bounds.height * height;
    }

    panel.addEventListener('pointermove', (event) => {
      if (event.pointerType === 'touch' || motionPreference.matches) return;
      locate(event);
      hovering = true;
      schedule();
    });
    panel.addEventListener('pointerleave', () => {
      if (!hovering) return;
      hovering = false;
      pulseStart = performance.now() - 610;
      schedule();
    });
    panel.addEventListener('click', (event) => {
      if (motionPreference.matches) return;
      if (event.detail === 0) {
        point.x = width / 2;
        point.y = height / 2;
      } else {
        locate(event);
      }
      pulseStart = performance.now();
      schedule();
    });
    motionPreference.addEventListener('change', () => {
      panel.disabled = motionPreference.matches;
      hovering = false;
      pulseStart = -Infinity;
      if (animation) cancelAnimationFrame(animation);
      animation = 0;
      context.putImageData(original, 0, 0);
    });
    panel.disabled = motionPreference.matches;
  </script>
</body>
</html>

UI Skills 原创示例

相关参考