New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Twist The Reality with feDisplacementMap #16
Comments
Table of contentsPreface ␥Inspired by the famous album cover of Joy Division made by Peter Saville, I wanted to build an SVG filter that turns the source(image, text) into a bunch of 'contour' lines. That's where 𐄡
Super power of
|
<svg>
...
<feMerge>
<feMergeNode in="SourceGraphic" />
<feMergeNode in="TILE" />
</feMerge>
...
</svg>
<div><p>Twist The Reality !</p></div> |
3. Bend the lines ␥
Now using what we know about <feDiplacementMap>
, move the pixel's y
under the text up with yChannalSelector='A'
, x
stay the same since the R
channel never changed.
Remove the map
, we can see the shape of the contour. But the stripes just shift position, not curved like a contour. Let's make it better by smoothing the alpha.
<svg>
...
<feDisplacementMap
in2="SourceGraphic"
in="TILE"
scale="10"
xChannelSelector="R"
yChannelSelector="A"
result="OUTPUT"
/>
...
</svg>
|
4. Smooth the contour ␥
Apply a <feGaussianBlur>
to make the alpha spread around, which can create a natural linear gradient for the text. The contour will be curved after this step.
We can control the y-offset by increasing the scale
of <feDisplacementMap>
, and adjust the curve scope by increasing the stdDeviation
of blur.
<svg>
...
<filter
id="contour"
color-interpolation-filters="sRGB"
x="0"
y="0"
width="100%"
height="100%"
>
<feGaussianBlur
stdDeviation=".8"
result="BLUR"
/>
...
</svg>
|
Throw more vertical stripes, and a
linear-gradient
to show alpha change.
𐄡
Global Magnifier filter ␥
After the contour, I thought <feDisplacementMap>
also could be used to make a Magnifier filter. Check it out:
I'll not explain the details, but here are some keys to take away:
- Due to security reasons, a
<feImage>
can not be used asmap
directly, one way is inline SVG directly intohref
. - Apply the filter with
backdrop-filter
, so theSourceGraphic
will be anything under the target. In this way, the "Magnifier" can show everything on the web page. - Use
R
andB
channel to create an "identity" map, then add agrey
totransparent
radial-gradient circle in the center which twist the edge of the circle. The type of Magnifier can be controlled by the<stop>
position.
𐄡
Closing thoughts
There is so much fun playing with SVG filters, they're like built-in shaders that offer limited parameters, but with a little bit of innovation, they can be powerful and maybe it's the simplest way to tweak pixels on a web page. I'm considering writing a series to dig more for each of them. Thanks for reading, see you next time.
@9am 🕘
The text was updated successfully, but these errors were encountered: