Ripple

Click a picture and a water ripple spreads from that point, driven by an animated displacement map.

What you are seeing

A close view of a broad green leaf with fine veins and three water drops against dark blurred foliage. Clicking makes a circular ring of distortion expand from the clicked point, bending the veins and drops as it passes, then fade after three seconds.

How it works

The displacement map stores a direction in its red and green channels around a ring. Neutral grey means no movement, so only the ring bends the picture.

On a click, SMIL grows the map from the click point to 400 units across while a circular mask grows and fades, so the ring travels outward and dies away.

The original drew the map through feImage pointing at an element. Firefox does not render that form, so the map is now a file and the animation changes the feImage subregion instead.

History

Published on October 4, 2009.

Source

/filterEffects/ripple.svg
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 800 800" id="root" style="background:black" >
  <title>Ripple</title>
  <desc>A drawn green leaf with water drops. Clicking the picture sends a circular ripple outward from that point: the image bends inside an expanding ring and settles back. Press Enter or Space for a ripple at the centre.</desc>
  <defs>
    <desc>
      /* ============================================================================
       * Author: Erik Dahlström
       * License terms: see svg-wow.org
       * CC0 http://creativecommons.org/publicdomain/zero/1.0/
       * Restoration: photo replaced by original artwork, displacement map
       * regenerated. feImage now loads the map as a file and animates its
       * subregion, and the edge fade is a mask, because Firefox does not render
       * feImage references to elements.
       * =========================================================================== */
    </desc>
    <radialGradient id="fade">
      <stop stop-color="white" stop-opacity="1" offset="0.7">
        <animate attributeName="stop-opacity" to="0" dur="3s" begin="animripple.begin"/>
      </stop>
      <stop stop-color="white" stop-opacity="0" offset="1"/>
    </radialGradient>
    <filter id="ripple" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse">
      <!-- neutral grey means no displacement outside the growing map -->
      <feFlood flood-color="rgb(128,128,128)" result="neutral"/>
      <!-- generate ripple displacement map -->
      <feImage id="dispmap" xlink:href="../res/1.0/images/displacementMap.jpg" preserveAspectRatio="none" x="0" y="0" width="1" height="1" result="ring">
        <animate id="at1" attributeName="x" from="0" to="0" dur="3s" begin="animripple.begin" fill="freeze"/>
        <animate id="at2" attributeName="y" from="0" to="0" dur="3s" begin="animripple.begin" fill="freeze"/>
        <animate id="animripple" attributeName="width" from="1" to="400" dur="3s" begin="indefinite" fill="freeze"/>
        <animate attributeName="height" from="1" to="400" dur="3s" begin="animripple.begin" fill="freeze"/>
      </feImage>
      <feMerge result="dmap">
        <feMergeNode in="neutral"/>
        <feMergeNode in="ring"/>
      </feMerge>
      <!-- displace input image -->
      <feDisplacementMap in="SourceGraphic" in2="dmap" scale="32" xChannelSelector="R" yChannelSelector="G"/>
    </filter>
    <!-- mask rect edges -->
    <mask id="ripplemask" maskUnits="userSpaceOnUse" x="0" y="0" width="800" height="800">
      <circle id="mask" r="200" fill="url(#fade)">
        <animate attributeName="r" values="0;200" dur="3s" begin="animripple.begin" onend="running=false" fill="freeze"/>
      </circle>
    </mask>

    <script><![CDATA[
      var running = false;
      function $(id) { return document.getElementById(id); }
      
      function clientToUserSpace(p)
      {
        var sctm = document.documentElement.getScreenCTM();
        sctm = sctm.inverse();
        return p.matrixTransform(sctm);
      }

      function startRipple(x, y)
      {
        if(running)
          return;

        var radius = 200;

        var ripple = $('ripple');
        var mask = $('mask');
        var animripple = $('animripple');

        ripple.x.baseVal.value = x-radius;
        ripple.y.baseVal.value = y-radius;
        ripple.width.baseVal.value = radius*2;
        ripple.height.baseVal.value = radius*2;

        mask.setAttribute("cx", x);
        mask.setAttribute("cy", y);

        // the map grows from the click point to the full ripple size
        $('at1').setAttribute("from", x);
        $('at1').setAttribute("to", x-radius);
        $('at2').setAttribute("from", y);
        $('at2').setAttribute("to", y-radius);

        running = true;
        animripple.beginElement();
      }

      function doRipple(evt)
      {
        var m = document.documentElement.createSVGPoint();
        m.x = evt.clientX;
        m.y = evt.clientY;
        m = clientToUserSpace(m);
        startRipple(m.x, m.y);
      }

      document.addEventListener("keydown", function(evt) {
        if(evt.key === "Enter" || evt.key === " ")
        {
          evt.preventDefault();
          startRipple(400, 400);
        }
      });
    ]]></script>
  </defs>
  <image xlink:href="../res/1.0/images/ripple-leaf.jpg" width="100%" height="100%" onclick="doRipple(evt)" style="cursor:pointer"/>
  <g mask="url(#ripplemask)">
    <image id="overlay" xlink:href="../res/1.0/images/ripple-leaf.jpg" width="100%" height="100%" pointer-events="none" filter="url(#ripple)" display="none">
      <set attributeName="display" begin="animripple.begin" end="animripple.end" to="inline"/>
    </image>
  </g>
  <text x="800" y="716" style="font:11px sans-serif; fill:#bbb; text-anchor:end">Leaf artwork drawn for this restoration</text>
</svg>
/filterEffects/ripple.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ripple | SVG Wow!</title>
<meta name="description" content="Click a picture to send a water ripple through it, made with an animated SVG displacement map.">
<style>html,body{margin:0;height:100%;background:#111;color:#ddd;font:15px/1.45 system-ui,-apple-system,"Segoe UI",sans-serif}body{display:flex;flex-direction:column;min-height:100%}header{display:flex;flex-wrap:wrap;gap:4px 16px;align-items:baseline;padding:10px 16px}h1{font-size:18px;margin:0;color:#fff}p{margin:0;color:#aaa}a{color:#ffc14d}iframe{flex:1;width:100%;min-height:320px;border:0;background:#000;display:block}</style>
</head>
<body>
<header>
<h1>Ripple</h1>
<p>Erik Dahlström, 2009. Click the picture to start a ripple.</p>
<p><a href="/gallery/ripple/">Gallery page</a> · <a href="ripple.svg">Open the SVG</a></p>
</header>
<iframe src="ripple.svg" title="Ripple demo"></iframe>
</body>
</html>