Plate IV · Lake at dusk

Field notes

Move the mouse over the sheet, or focus the lens with Tab and use the arrow keys. The type under the lens is still real text: select it, copy it, or read it with a screen reader. Only its pixels are bent by an SVG displacement map.

Ridges, far to near: 1,240 m, 980 m, 610 m. Shoreline pines, eight. Birds in flight, three. Water temperature at the jetty, 14 °C. Wind from the west, light.

Arrow keys move the lens, with Shift in bigger steps. Home centers it.

How it works

A loupe made of one displacement map

A magnifying glass shows the content from closer to its center than where you see it. That is a displacement: every pixel under the lens is fetched from a point nearer the middle. An SVG filter can do this to any HTML with a single generated map.

The pipeline

  1. Generate the map once per lens shape: a PNG of the lens's square, whose red and green channels hold, per pixel, how far to look towards the center.
  2. Place it under the pointer with feImage's x and y.
  3. Displace the content with feDisplacementMap, limited to the lens square, and lay the result over the untouched content with feMerge.

Moving the lens only changes four attributes; the map is rebuilt only when the radius, zoom, profile or rim change.

1. The zoom profile

For a pixel at distance t from the center (in units of the radius), a sampling factor f(t) says where to look: f = 1/zoom magnifies, f = 1 shows the content where it is. The two profiles differ in how they get from one to the other:

t = distance from the center / radius        // 0 … 1
k = 1 − 1 / zoom

loupe:   f(t) = 1 − k · (1 − smoothstep((t − (1 − rim)) / rim))
fisheye: f(t) = 1 − k · (1 − t²)²

a pixel at p shows the content at  c + (p − c) · f(t)
so the offset stored in the map is (p − c) · (f(t) − 1)

A loupe magnifies evenly and squeezes everything that the magnified center covers into a narrow rim, like a real magnifying glass. A fisheye is strongest in the center and eases off all the way to the edge. Both reach f(1) = 1, so the lens meets its surroundings without a seam, and t·f(t) only ever grows, so the picture never folds over; a unit test checks that for every profile and setting.

This is what sets it apart from the liquid glass lens, which uses the same delivery path (a generated map in feImage) but refracts only at its bezel and leaves the flat center alone. A loupe does the opposite: the interior is the point.

2. Encoding the map

feDisplacementMap fetches each pixel from scale × (R − ½, G − ½) away. The offset (p − c)·(f − 1) is divided by the largest offset in the lens, currently 0.355 × the radius, and stored around 128 in red and green, so the strongest pull reaches 1 or 255 and uses all 8 bits. The scale turns that back into px: 68.52 for your settings. Outside the circle the map is 128, which moves nothing. The PNG is encoded in plain TypeScript, with the same encoder as the CRT's barrel map, so it has no canvas color management in the way.

<svg width="0" height="0" aria-hidden="true">
  <!-- W × H: the element's size; X, Y: top-left of the lens square, in px -->
  <filter id="loupe" x="0" y="0" width="W" height="H"
          filterUnits="userSpaceOnUse" primitiveUnits="userSpaceOnUse"
          color-interpolation-filters="sRGB">
    <!-- 192 × 192 px PNG: red/green = offset towards the center, around 128 -->
    <feImage href="data:image/png;base64,…" x="X" y="Y"
             width="192" height="192" preserveAspectRatio="none" result="map" />
    <feDisplacementMap in="SourceGraphic" in2="map" scale="68.52"
                       xChannelSelector="R" yChannelSelector="G"
                       x="X" y="Y" width="192" height="192" result="lens" />
    <feMerge>
      <feMergeNode in="SourceGraphic" />
      <feMergeNode in="lens" />
    </feMerge>
  </filter>
</svg>

All coordinates are user-space px (filterUnits and primitiveUnits="userSpaceOnUse"): for an HTML element they start at the top-left corner of its border box. Without them, the default filter region is the bounding box plus 10%, and percentages would have to be recomputed on every resize.

3. Following the pointer, and the keyboard

A mouse anywhere over the element moves the lens; the lens follows with frame-rate independent smoothing (the same helper as the holo card's tilt), which is skipped when the user prefers reduced motion. The lens itself ignores mouse clicks, because the pointer always sits in its middle and would otherwise block every link underneath.

The lens is focusable and exposed as a two-dimensional slider, the pattern common color pickers use: the arrow keys move it by 2% of the element (Shift: 10%), Home centers it, and the value text reads both coordinates, for example "42% across, 38% down". On touch screens, drag the lens itself; it sets touch-action: none only on itself, so the page still scrolls everywhere else.

Readability

A filter magnifies the rendered pixels, it doesn't re-render the content larger. At 2× the type under the lens is as sharp as a 2× bitmap zoom: fine on a high-DPI screen, visibly soft on a 1× screen. It is a visual effect, not an accessibility zoom; for that the browser's own zoom re-renders text properly. The loupe's rim does the opposite and squeezes content into a narrow ring; displacement picks single pixels without averaging, so a very narrow rim looks grainy. A wider rim or the fisheye profile keeps it smooth. The text never leaves the DOM, so selection, find in page and screen readers work as usual.

Backdrop mode

By default the filter runs on the content, and every move of the lens reruns it over the whole element. In Backdrop mode the same filter is applied as backdrop-filter: url(#id) on the lens instead, with the map at the lens's own origin, so only the lens area is processed and anything behind the lens is magnified, not just the component's children. Only Chromium renders SVG filters in backdrop-filter: Firefox drops the whole backdrop filter when it contains url(), and feature detection can't tell, since CSS.supports accepts the syntax everywhere. The component therefore checks for a Chromium engine and otherwise falls back to the content filter, which looks the same.

Browser support and performance

Content mode uses SVG filters on HTML, feImage with a data URL and feDisplacementMap, all Baseline widely available (web-features 3.44.0). Backdrop mode is Chromium only, as above. Checked in Chromium; other engines are untested.

  • In content mode every lens move reruns the filter over the element. Only the lens square is displaced, but the merge still covers the whole element, so keep the magnified area no larger than needed. Backdrop mode only processes the lens.
  • Building a map is a loop over (2 × radius)² pixels plus an uncompressed PNG of the same size, about 110 KB for the default radius. It happens when the lens shape changes, not when it moves.

Back to the demo ↑