Skip to content
Advertisement

Ramp in and out SVG turbulence filter

I’m working on a non-linear menu and I’d like it to have a nice animation when hovering over the options. I managed to put together a turbulence svg filter that I like. Now I’m wondering how to make it so that when one hovers over the option the turbulence ramps up progresively, and that the same happens in the opposite direction on mouse out, it slowly fades out.

What’s the most straightforward way of addresing this through js?

I’m not well acquaintanced with svg filters. Should I directly manipulate the baseFrequency’s values? I reckon that would affect all nodes and not just the one being hovered over. Is there any way css can be leveraged? Is there any way to pass arguments into a filter?

Here’s the hand-crafted svg filter code along with the -simplified- svg code exported from my design app. I’ve omitted the circle’s stroke data.

<defs>

  <filter filterUnits="userSpaceOnUse" id="turbulence" x="0" y="0" width="100%" height="100%">
    <feTurbulence id="base_turbulence" numOctaves="3" seed="2" baseFrequency="0.02 0.05"></feTurbulence>
    <feDisplacementMap scale="20" in="SourceGraphic"></feDisplacementMap>
  </filter>

  <animate xlink:href="#base_turbulence" attributeName="baseFrequency" dur="60s" keyTimes="0;0.5;1" values="0.01 0.02;0.02 0.03;0.01 0.03" repeatCount="indefinite"/>

</defs>


  <g id="ser" class="node" transform="matrix(1,0,0,1,84,88)">
      <g class="circle" transform="matrix(1,0,0,1,-12,13)" filter="url(#turbulence)">
          <g transform="matrix(1,0,0,1,-179,-8)">
              <circle cx="512.5" cy="229.5" r="53.5" style="fill:rgb(255,235,187);"/>
              <path d="(...)"/>
          </g>
          <g class="marker" transform="matrix(1.24299,0,0,1.24299,-303.533,-63.7664)">
              <path d="(...)"/>
          </g>
      </g>
      <g transform="matrix(1,0,0,1,0,-21.3664)">
          <text x="342px" y="229px" style="font-family:'LibreBaskerville-Regular', 'Libre Baskerville';font-size:47.751px;stroke:rgb(2,2,2);stroke-width:1px;">S<tspan x="379.962px 414.486px " y="229px 229px ">er</tspan></text>
      </g>
      <g transform="matrix(1,0,0,1,-18.1341,1.72278)">
          <text x="344.149px" y="246px" style="font-family:'LibreBaskerville-Regular', 'Libre Baskerville';font-size:25.202px;stroke:rgb(2,2,2);stroke-width:1px;">El Silencio Donde Escucho</text>
      </g>
  </g>

  <g id="propuestas" class="node" transform="matrix(1,0,0,1,137.028,83)">
      <g class="circle" transform="matrix(1,0,0,1,-126.851,580.865)">
          <g transform="matrix(1,0,0,1,-179,-8)">
              <circle cx="512.5" cy="229.5" r="53.5" style="fill:rgb(187,205,255);"/>
              <path d="(...)"/>
          </g>
          <g class="marker" transform="matrix(1.24299,0,0,1.24299,-303.533,-63.7664)">
              <path d="(...)"/>
          </g>
      </g>
      <g transform="matrix(1,0,0,1,-115,543.634)">
          <text x="342px" y="229px" style="font-family:'LibreBaskerville-Regular', 'Libre Baskerville';font-size:47.751px;stroke:rgb(2,2,2);stroke-width:1px;">P<tspan x="381.968px 411.383px 449.775px 489.36px 528.517px 563.041px 591.883px 617.095px 650.712px " y="229px 229px 229px 229px 229px 229px 229px 229px 229px ">ropuestas</tspan></text>
      </g>
      <g transform="matrix(1,0,0,1,-133.134,566.723)">
          <text x="344.149px" y="246px" style="font-family:'LibreBaskerville-Regular', 'Libre Baskerville';font-size:25.202px;stroke:rgb(2,2,2);stroke-width:1px;">Encuentr<tspan x="463.279px " y="246px ">o</tspan>s, taller<tspan x="571.371px " y="246px ">e</tspan>s, seminarios</text>
      </g>
  </g>

I’m targeting circles within nodes to have the effect.

And this is the result as is:

svg turbulence animation

Thanks!

Advertisement

Answer

The best I can come up with is to trigger animations of the filter with mouse events. As noted in the comments above, you need to define a separate filter for every target. Take care to always use unique IDs.

The “ramp up” effect is the result of “damping” the alpha channel of the turbulence before it is fed to the displacement map. Alpha values are multiplied with a value between 0 and 1. This value is interpolated to go from the current animated value to 1 after mouseover or to 0 after mouseout.

The trick is using additive="sum" in the mouseout animation. It adds to the value the previous animation currently is at. The two animations might run in parallel for some time if they are triggered in short sequence. During that times, the “ramping” stalls, because they are canceling each other out, but it does not jump.

To save on processing power needed, the turbulence animation is also only active while the mouse is hovering over the triggering element, on mouseout time plus 1s it is stopped.

The effect is not triggered by the mouse over the filtered element itself, but by an invisible element on top. Otherwise you would get some weird effects because the displacement changes the area where the mouse is considered over the element.

<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200">
  <filter id="turbulence" filterUnits="objectBoundingBox"
          x="-.5" y="-.5" width="2" height="2">
    <feTurbulence numOctaves="2" seed="3" baseFrequency="0.02 0.05" result="rand">
      <animate id="change" attributeName="baseFrequency"
               begin="dot.mouseover" end="dot.mouseout+1s" dur="60s"
               keyTimes="0;.125;.25;.375;.5;.625;.75;.875;1"
               values="0.013,0.013;0.01,0.02;0.013,0.027;0.02,0.03;0.027,0.027;0.03,0.02;0.027,0.013;0.02,0.01;0.013,0.013"
               repeatCount="indefinite"/>
    </feTurbulence>
    <feComponentTransfer>
      <feFuncA type="linear" slope="0">
        <animate attributeName="slope" begin="dot.mouseover" dur="1s"
                 keyTimes="0;1" values="0;1" fill="freeze" />
        <animate attributeName="slope" begin="dot.mouseout" dur="1s"
                 keyTimes="0;1" values="0;-1" fill="freeze"
                 additive="sum" />
      </feFuncA>
    </feComponentTransfer>
    <feDisplacementMap in="SourceGraphic" scale="20" />
    <feOffset dx="-10" dy="-10" />
  </filter>
  <g style="filter:url(#turbulence)">
      <circle r="60" cy="100" cx="100"
              style="fill:rgb(255, 235, 187);stroke:#999;stroke-width:16" />
      <circle r="60" cy="100" cx="100"
              style="fill:none;stroke:#fff;stroke-width:8" />
  </g>
  <circle id="dot" r="70" cy="100" cx="100" style="opacity:0" />
</svg>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement