How SVG feDisplacementMap Creates Distortion Effects

The <feDisplacementMap> filter creates distortion effects in Scalable Vector Graphics (SVG) by using the color values of one image to spatially shift the pixel coordinates of another. By mapping specific color channels—such as Red, Green, Blue, or Alpha—to horizontal and vertical axes, the filter alters the geometry of the target element. This article explains the underlying mechanism, the mathematical formula behind the transformation, key attributes, and practical implementations like generating organic or glitch textures.

The Core Mechanism of Displacement

The <feDisplacementMap> filter requires two inputs: 1. Source Graphic (in): The primary graphic, shape, or text that you want to distort. 2. Displacement Map (in2): A secondary graphic whose color values determine where and how far the pixels of the source graphic move.

Rather than altering the colors of the target graphic, the filter changes where each pixel is drawn. It samples the color values from the displacement map at a given coordinate \((x, y)\) and calculates a new coordinate \((x', y')\) from which to fetch the pixel from the source graphic.

The Mathematical Formula

The shift in position is determined by the following SVG specification formula for calculating the displaced pixel location \(P(x,y)\):

\[P(x,y) \leftarrow I(x + \text{scale} \times (XC(x,y) - 0.5), y + \text{scale} \times (YC(x,y) - 0.5))\]

Key Attributes of feDisplacementMap

To control the distortion, <feDisplacementMap> provides several configuration attributes:

Common Pairing: feTurbulence and feDisplacementMap

While any graphic can serve as a displacement map, <feDisplacementMap> is most frequently paired with the <feTurbulence> primitive.

The <feTurbulence> element algorithmically generates Perlin noise, creating a continuous spectrum of light and dark values with organic, natural transitions. When fed into <feDisplacementMap> as in2, this noise creates realistic effects such as water ripples, heat haze, melting shapes, smoke, and textured glass. Combining simple geometry or text with noise-driven displacement allows vector elements to achieve complex, organic physical behaviors natively in the browser.