How it works
A mosaic from five filter primitives
SVG filters have no 'pixelate' primitive, and no way to read one pixel and paint it over a whole square. But they can keep a grid of single pixels and grow each one until it fills its cell, which is exactly what a mosaic is.
1. A grid of dots: feFlood, feComposite, feTile
<feFlood> paints a solid color over its primitive subregion,
which is set with x, y, width and height. Shrunk to a single pixel, it is a dot. The next <feComposite> doesn't change any pixels; its only job is its subregion,
exactly one block (width = height = size), with the dot inside. <feTile> repeats its input's subregion across the whole filter region, so the
result is one white dot per block.
2. Sample: feComposite operator="in"
operator="in" keeps the first input only where the second one is opaque. With
the source as the first input and the dots as the second, every block now holds one pixel of
the content, the one at its center, and nothing else. This is the sample half of sample-and-hold:
the colors are not averaged, they are picked, which is why thin lines can vanish or flicker in
and out as the content moves.
3. Hold: feMorphology dilate
<feMorphology operator="dilate"> replaces each pixel with the maximum of
its neighborhood, a square of 2 × radius + 1 pixels. Around a lone sample that
maximum is the sample itself, so it grows into a square of its own color. The radius has to
be chosen so the squares fill the blocks exactly: a d×d dot dilated by r becomes d + 2r wide. A one-pixel dot only reaches odd sizes, so even blocks sample a 2×2
dot instead. If the squares overlapped, dilate would keep the larger value of each channel where
they meet and a bright neighbor would bleed in.
<svg width="0" height="0" aria-hidden="true">
<filter id="pixelate" x="0" y="0" width="100%" height="100%"
color-interpolation-filters="sRGB">
<!-- one 2×2 dot, 5px in from the corner of a 12px cell -->
<feFlood x="5" y="5" width="2" height="2" flood-color="#fff" result="dot" />
<!-- crop to exactly one cell, so feTile repeats it every 12px -->
<feComposite in="dot" in2="dot" x="0" y="0" width="12" height="12" result="cell" />
<feTile in="cell" result="dots" />
<!-- keep the source only under the dots: one sample per block -->
<feComposite in="SourceGraphic" in2="dots" operator="in" result="samples" />
<!-- grow every sample into its block: 2 + 2 × 5 = 12 -->
<feMorphology in="samples" operator="dilate" radius="5" result="blocks" />
</filter>
</svg> Like every SVG filter, it applies to HTML with one declaration:
.censored {
filter: url(#pixelate);
} 4. Tiles and grout: a tiled mask
With a Gap or Roundness the blocks get a shape. One cell of a mask is
drawn as a tiny SVG, a white (rounded) square on transparent, loaded with <feImage> and repeated with another <feTile>. A second feComposite in cuts the blocks to that shape, and a flood of the grout color merged
underneath fills the gaps. Without grout the gaps stay transparent. Roundness 1 gives circles,
which reads as an LED wall or a bead mosaic.
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12">
<rect x="0" y="0" width="12" height="12" rx="0" fill="white" />
</svg> 5. Censor and reveal
Filter attributes can't be transitioned with CSS, so the block size is animated from script:
each frame sets a new radius and cell size, and the browser reruns the filter.
The sizes are rounded to whole pixels, which gives the stepped "resolve" look of old TV
censoring rather than a smooth blur. At size 1 the filter is removed altogether. reveal="hover" does this on pointer hover and keyboard focus inside the
element; Play resolve above drives size with a Tween instead. Under reduced motion both jump straight to the end.
<script>
import { Tween } from 'svelte/motion';
import { Pixelate } from '@sunsided/web-visual-fx/svelte';
const size = new Tween(48, { duration: 1600 });
</script>
<button onclick={() => size.set(1)}>Reveal</button>
<Pixelate size={size.current}>…</Pixelate> Pixelation is not redaction. The original stays in the DOM, in the accessibility tree and in the page's source, and pixelated text can often be read back by trying candidate texts until one pixelates the same way. Use it for spoilers and playful reveals; remove real secrets before they reach the browser.
Browser support and performance
All primitives used here are part of SVG filters, which are Baseline widely available since
July 2015, feMorphology and feTile included. Checked in Chromium
only (current block size: 12px); Firefox and Safari are untested.
- The chain is cheap: a flood, a tile, a composite and a dilate. Dilation cost grows with the radius, but block sizes of a few dozen pixels are fine. On static content the result is cached; animated content (try Animate source) or a running reveal reruns it on every frame.
- Filters work in CSS pixels and render at device resolution, so a 12px block is 24 device pixels on a 2× screen. At fractional zoom levels the grid may not land on whole device pixels and block edges can look slightly soft (unverified).
- For the "8-bit" look, combine it with a palette: put the dither filter inside the mosaic so each block gets one posterized color.