Sitemap
codebro.io

codebro.io

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

SVG noise filter — Part I

Introduction

2 min readNov 17, 2019

--

What’s that noise?

Normally when we speak of “noise” we mean sound.

Today we will be speaking about “noise” in the context of graphics, and applying it to some SVG elements.

There is only one filter primitive available for applying noise to SVGs and that is

<feTurbulance/>

Under the hood it uses the popular Perlin noise algorithm developed by Ken Perlin.

This produces a natural-looking noise effect and is commonly used to create graphics such as land, clouds, fire and water kind of like Captain Planet’s — earth, air, fire, water, (and heart?).

Press enter or click to view image in full size
Captain Planet flying through Perlin noise
Captain Planet flying through our noise.

Base Frequency

feTurbulance requires a baseFrequency attribute.

<feTurbulance baseFrequency="<NUMBER> | <NUMBER NUMBER>"/>

To put it simply, the input represents a “multiplier” for the noise algorithm.

The higher the baseFrequency, the more “noisy” your noise gets. If you supply a single number, this number will be used for both the x and y frequency.

Get codebro.io’s stories in your inbox

Join Medium for free to get updates from this writer.

Alternatively you can provide two numbers separated by a space representing x and y respectively.

Let’s make some noise

  1. Make an SVG.
<svg>
<rect width="100" height="100"/>
</svg>

2. Add a filter element to it.

<svg>
<filter></filter>
<rect width="100" height="100"/>
</svg>

3. Nest your filter primitive inside this and apply a base frequency.

<svg>
<filter>
<feTurbulance baseFrequency="0.05"/>
</filter>
<rect width="100" height="100"/>
</svg>

4. Use an id to reference your filter primitive on whatever element you are trying to apply the noise to.

<svg>
<filter id="noise">
<feTurbulance baseFrequency="0.05"/>
</filter>
<rect filter="url(#noise)" width="100" height="100"/>
</svg>
Hey presto, a 100 x 100 square of delectable Perlin noise

Right now we have simply added the noise over the top of our shape, in the next article we will merge it into our shape.

--

codebro.io
codebro.io

Written by codebro.io

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