Sitemap

SVG noise filter — Part IV

Animating the noise

--

NB: This article is basically an explanation of Michael Mullany’s stackoverflow answer here https://stackoverflow.com/questions/29966256/how-to-animate-feturbulence-to-appear-continuous. A big thank you to Michael for providing such a clever solution.

Recap

Previously we looked at the effects of animating seed, baseFrequency and numOctaves. https://medium.com/@codebro/svg-noise-filter-f38a9327f66c

Although producing interesting results, they don’t create a smooth and continuous animation of our noise.

Let the smoke, smoke and the water, ripple

If you look closely at our raw noise you will see a lot of rainbow colors, this is the key to animating noise continuously.

What is the Color Matrix?

<feColorMatrix/>

This is a highly versatile filter primitive, useful for finely tuning and manipulating colors (especially useful for making sophisticated image filters).

Attributes

We have two main corresponding attributes for type and values.

Get codebro.io’s stories in your inbox

Join Medium for free to get updates from this writer.

For our animation we will use two different color matrix types.

Hue Rotate

This allows us to smoothly and continuously change our rainbow colors through a full spectrum of color.

Press enter or click to view image in full size
Photo by Sharon Pittaway on Unsplash

Color Matrix

While cycling through our color spectrum we will want to isolate a single color in order to access and apply our noise.

Let’s make some noise

  1. Nest our color matrix inside our filters under our turbulence.
<svg>
<filter id="noise">
<feTurbulence baseFrequency="0.05"/>
<feColorMatrix type="hueRotate" values="0"/>
<feDisplacementMap in="SourceGraphic" scale="10"/>
</filter>
<rect filter="url(#noise)" width="100" height="100"/>
</svg>

2. Nest our animation inside our color matrix. Setting repeatCount to indefinite will create a continuous loop. You will notice the rainbow colors changing.

<svg>
<filter id="noise">
<feTurbulence baseFrequency="0.05"/>
<feColorMatrix type="hueRotate" values="0">
<animate
attributeName="values"
from="0"
to="360"
dur="2s"
repeatCount="indefinite"
/>

</feColorMatrix>
<feDisplacementMap in="SourceGraphic" scale="10"/>
</filter>
<rect filter="url(#noise)" width="100" height="100"/>
</svg>

3. Add our second color matrix to isolate a color channel. We only need the alpha value of either the R, G or B channels to be present.

<svg>
<filter id="noise">
<feTurbulence baseFrequency="0.05"/>
<feColorMatrix type="hueRotate" values="0">
<animate
attributeName="values"
from="0"
to="360"
dur="2s"
repeatCount="indefinite"
/>
</feColorMatrix>
<feColorMatrix
type="matrix"
values="0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 0 0 0 0"
/>

<feDisplacementMap in="SourceGraphic" scale="10"/>
</filter>
<rect filter="url(#noise)" width="100" height="100"/>
</svg>

Keep the noise down

It is important to keep in mind that animating SVG filters are generally very expensive and should be used sparingly. On Google Chrome they look and perform ok, but on Safari, for example, they have the potential to bulldoze your webpage into the 1990s.

--

--

codebro.io
codebro.io

Written by codebro.io

♡ playing with web technologies. Especially anything graphics, game or education