Displacement lab

Bend a grid with noise using feTurbulence and feDisplacementMap, and drag to tune scale and frequency.

What you are seeing

A grid of charcoal lines with two blue rings, a blue dot and a blue square outline, warped into soft wavy shapes by noise. Straight edges bend and ripple more as scale rises and finer as frequency rises.

How it works

feTurbulence generates a noise image. feDisplacementMap then moves each pixel of the source by an amount read from one colour channel of that noise, with the red channel pushing sideways and the green channel pushing up and down.

The scale attribute sets how far pixels can move. The baseFrequency attribute sets how fine the noise is, so low values give slow bends and high values give ripples. More octaves add finer detail.

Dragging on the picture maps left and right to scale and up and down to frequency. The animate option gently varies the frequency over time.

Source

/lab/displacement-lab/index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Displacement lab</title>
<meta name="description" content="Warp a grid with feTurbulence and feDisplacementMap. Drag on the picture to change scale and frequency.">
<link rel="stylesheet" href="../lab.css">
<style>
.g{stroke:#24272c;stroke-width:2;fill:none}
.ring{fill:none;stroke:#1f3fe0;stroke-width:10}
.dot{fill:#1f3fe0}
.padhint{fill:none;stroke:#8a909b;stroke-dasharray:4 4}
</style>
</head>
<body>
<main class="demo">
  <div class="stage">
    <svg id="svg" class="drag-surface" viewBox="0 0 400 300" role="img" aria-labelledby="t d" tabindex="0">
      <title id="t">Displacement lab</title>
      <desc id="d">A grid of charcoal lines with blue rings and a blue dot, bent into wavy shapes by a noise displacement map. Dragging on the picture changes the displacement scale across and the noise frequency up and down. The sliders below control the same values, and the arrow keys adjust them when the picture has focus.</desc>
      <defs>
        <filter id="dm" filterUnits="userSpaceOnUse" x="-40" y="-40" width="480" height="380" color-interpolation-filters="sRGB">
          <feTurbulence id="turb" type="fractalNoise" baseFrequency="0.015 0.015" numOctaves="2" seed="3" result="noise"/>
          <feDisplacementMap id="map" in="SourceGraphic" in2="noise" scale="30" xChannelSelector="R" yChannelSelector="G"/>
        </filter>
      </defs>
      <g filter="url(#dm)">
        <g id="lines" class="g"></g>
        <circle class="ring" cx="130" cy="150" r="60"/><circle class="ring" cx="130" cy="150" r="28"/>
        <circle class="dot" cx="290" cy="110" r="34"/>
        <rect x="250" y="170" width="90" height="70" fill="none" stroke="#1f3fe0" stroke-width="10"/>
      </g>
    </svg>
  </div>
  <div class="bar">
    <button id="anim" type="button">Pause</button>
    <label><span>Scale</span><input id="scale" type="range" min="0" max="100" value="30"><output id="so">30</output></label>
    <label><span>Frequency</span><input id="freq" type="range" min="1" max="120" value="15"><output id="fo">0.015</output></label>
    <label><span>Octaves</span><input id="oct" type="range" min="1" max="5" value="2"></label>
    <label><span>Noise</span><select id="type"><option value="fractalNoise">Fractal noise</option><option value="turbulence">Turbulence</option></select></label>
    <label><span>X from</span><select id="xc"><option>R</option><option>G</option><option>B</option><option>A</option></select></label>
    <button id="seed" type="button">New seed</button>
  </div>
</main>
<script src="../lab.js"></script>
<script src="demo.js"></script>
</body>
</html>
/lab/displacement-lab/demo.js
(function () {
  'use strict';
  var $ = Lab.$, svg = $('#svg'), turb = $('#turb'), map = $('#map'), lines = $('#lines');
  for (var i = 0; i <= 400; i += 25) Lab.make('path', { d: 'M' + i + ' 0V300' }, lines);
  for (var j = 0; j <= 300; j += 25) Lab.make('path', { d: 'M0 ' + j + 'H400' }, lines);
  var scale = $('#scale'), freq = $('#freq'), tPhase = 0, seed = 3, animating = false;
  function fv() { return (+freq.value / 1000); }
  function apply(wob) {
    var f = fv() * (1 + (wob || 0));
    turb.setAttribute('baseFrequency', f.toFixed(5) + ' ' + (f * 1.0).toFixed(5));
    map.setAttribute('scale', scale.value);
    $('#so').textContent = scale.value; $('#fo').textContent = fv().toFixed(3);
  }
  var loop = Lab.loop(function (dt) { tPhase += dt * 0.6; apply(Math.sin(tPhase) * 0.12); });
  function setAnim(on) { animating = on; $('#anim').textContent = on ? 'Pause' : 'Animate'; if (on) loop.start(); else { loop.stop(); apply(0); } }
  $('#anim').addEventListener('click', function () { setAnim(!animating); });
  scale.addEventListener('input', function () { apply(0); });
  freq.addEventListener('input', function () { apply(0); });
  $('#oct').addEventListener('input', function (e) { turb.setAttribute('numOctaves', e.target.value); });
  $('#type').addEventListener('change', function (e) { turb.setAttribute('type', e.target.value); });
  $('#xc').addEventListener('change', function (e) { map.setAttribute('xChannelSelector', e.target.value); });
  $('#seed').addEventListener('click', function () { seed = seed % 40 + 1; turb.setAttribute('seed', seed); });
  Lab.drag(svg, { space: svg, down: function (p) { setAnim(false); setFrom(p); }, move: setFrom });
  function setFrom(p) {
    scale.value = Math.round(Lab.clamp(p.x / 400, 0, 1) * 100);
    freq.value = Math.round(1 + Lab.clamp(1 - p.y / 300, 0, 1) * 119);
    apply(0);
  }
  svg.addEventListener('keydown', function (e) {
    var d = { ArrowRight: [scale, 5], ArrowLeft: [scale, -5], ArrowUp: [freq, 5], ArrowDown: [freq, -5] }[e.key];
    if (!d) return; d[0].value = +d[0].value + d[1]; setAnim(false); apply(0); e.preventDefault();
  });
  apply(0);
  if (Lab.reduced) setAnim(false); else setAnim(true);
})();
/lab/lab.js
/* Small shared helpers for the SVG Wow! lab demos. No dependencies. */
(function () {
  'use strict';
  var NS = 'http://www.w3.org/2000/svg';
  var mq = window.matchMedia('(prefers-reduced-motion: reduce)');
  var Lab = (window.Lab = { NS: NS });
  Object.defineProperty(Lab, 'reduced', { get: function () { return mq.matches; } });
  Lab.$ = function (s, r) { return (r || document).querySelector(s); };
  Lab.$$ = function (s, r) { return Array.prototype.slice.call((r || document).querySelectorAll(s)); };
  Lab.clamp = function (v, a, b) { return Math.max(a, Math.min(b, v)); };
  Lab.lerp = function (a, b, t) { return a + (b - a) * t; };
  Lab.ease = function (t) { return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2; };
  Lab.make = function (tag, attrs, parent) {
    var e = document.createElementNS(NS, tag);
    if (attrs) for (var k in attrs) e.setAttribute(k, attrs[k]);
    if (parent) parent.appendChild(e);
    return e;
  };
  /* Client point to the user space of `el` (any SVG graphics element). */
  Lab.pt = function (el, evt) {
    var m = el.getScreenCTM();
    if (!m) return { x: 0, y: 0 };
    var p = new DOMPoint(evt.clientX, evt.clientY).matrixTransform(m.inverse());
    return { x: p.x, y: p.y };
  };
  /* requestAnimationFrame loop that pauses while the document is hidden. */
  Lab.loop = function (fn) {
    var raf = 0, last = 0, running = false, wanted = false, self = {};
    function tick(t) {
      if (!running) return;
      raf = requestAnimationFrame(tick);
      var dt = last ? Math.min((t - last) / 1000, 0.1) : 0;
      last = t;
      fn(dt, t / 1000);
    }
    function go() { if (running || document.hidden || !wanted) return; running = true; last = 0; raf = requestAnimationFrame(tick); }
    function halt() { running = false; cancelAnimationFrame(raf); }
    self.start = function () { wanted = true; go(); };
    self.stop = function () { wanted = false; halt(); };
    self.running = function () { return wanted; };
    document.addEventListener('visibilitychange', function () { if (document.hidden) halt(); else go(); });
    return self;
  };
  /* Pointer drag with capture. space: element whose user space the points use. */
  Lab.drag = function (el, o) {
    var active = false, space = o.space || el.ownerSVGElement || el;
    el.addEventListener('pointerdown', function (e) {
      if (e.button > 0) return;
      active = true;
      try { el.setPointerCapture(e.pointerId); } catch (_) {}
      if (o.down) o.down(Lab.pt(space, e), e);
      e.preventDefault();
    });
    el.addEventListener('pointermove', function (e) { if (active) o.move(Lab.pt(space, e), e); });
    function end(e) {
      if (!active) return;
      active = false;
      try { el.releasePointerCapture(e.pointerId); } catch (_) {}
      if (o.up) o.up(e);
    }
    el.addEventListener('pointerup', end);
    el.addEventListener('pointercancel', end);
  };
  Lab.say = function (msg) {
    var l = document.getElementById('live');
    if (l) l.textContent = msg;
  };
})();
/lab/lab.css
:root{--bg:#f1f2f4;--ink:#24272c;--blue:#1f3fe0;--mag:#d4006a;--line:#c9ccd3;--panel:#fff;--muted:#555b66;font-family:system-ui,-apple-system,"Segoe UI",Roboto,Helvetica,Arial,sans-serif}
*{box-sizing:border-box}
html,body{margin:0;height:100%;background:var(--bg);color:var(--ink);overflow-x:hidden}
body{font-size:13px;line-height:1.3}
.demo{display:flex;flex-direction:column;height:100%;min-height:0}
.stage{flex:1 1 auto;min-height:120px;position:relative;overflow:hidden;background:var(--bg)}
.stage>svg{position:absolute;left:0;top:0;width:100%;height:100%;display:block}
.bar{flex:0 0 auto;display:flex;flex-wrap:wrap;gap:6px 14px;align-items:center;padding:6px 10px;background:var(--panel);border-top:1px solid var(--line);max-height:46%;overflow:auto}
.bar .grp{display:flex;flex-wrap:wrap;align-items:center;gap:6px 10px}
label{display:inline-flex;align-items:center;gap:6px;white-space:nowrap}
label>span,.dim{color:var(--muted)}
output{font-variant-numeric:tabular-nums;min-width:3.2ch;display:inline-block}
button,select,input[type=number],input[type=text]{font:inherit;color:var(--ink);background:#fff;border:1px solid #8a909b;border-radius:6px;padding:3px 8px;min-height:28px}
button{border-color:var(--ink);cursor:pointer}
button:hover{background:#e7e9ef}
button[aria-pressed=true]{background:var(--ink);color:#fff}
input[type=range]{width:110px;accent-color:var(--blue);margin:0}
input[type=color]{width:34px;height:28px;padding:1px;border:1px solid #8a909b;border-radius:6px;background:#fff}
input[type=checkbox]{accent-color:var(--blue);width:16px;height:16px}
:focus-visible{outline:3px solid var(--blue);outline-offset:2px}
.grab{cursor:grab;touch-action:none}
.grab:active{cursor:grabbing}
.grab:focus-visible{outline:none;stroke:var(--blue);stroke-width:4px}
.drag-surface{touch-action:none}
.ro{font-size:11px;color:var(--muted)}
.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
svg text{font-family:inherit}
pre,textarea{font:11px/1.35 ui-monospace,SFMono-Regular,Menlo,Consolas,monospace}
@media (pointer:coarse){button,select,input[type=number]{min-height:38px}input[type=range]{height:30px}}
@media (max-width:420px){.bar{gap:6px 10px;padding:6px 8px}input[type=range]{width:96px}}
@media (min-width:700px){.demo.split{flex-direction:row}.demo.split .stage{flex:1 1 auto}.demo.split .bar{width:320px;max-height:none;flex:0 0 320px;border-top:0;border-left:1px solid var(--line);align-content:flex-start}}