# lens

A glass reading lens follows the pointer over the text, magnifying it through real curved glass.

A thick biconvex lens in a thin steel ring hovers over the page under the pointer. Every pixel is traced through both curved faces of the glass: the text comes up sharp and magnified in the middle, bends and splits into colors toward the rim, and the glass reflects a softbox and a dim studio, strongest at its edge. On the page beside it lies its shadow with the bright spot of light it focuses, and the shadow of its handle. The lens leans into its motion and settles with a little sway.

Module `text`, category Text. Import `@dioramajs/text/lens`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/text/src/lens.ts)

A catalogue entry for an engraved sea chart on cream paper, with a heading, a paragraph and a line of fine print.

```html title="index.html"
<div class="ln-page">
  <article class="ln-entry" data-dio="lens" tabindex="0">
    <p class="ln-kicker">Plate XII · Admiralty chart 1871</p>
    <h3 class="ln-title">The harbour at low water</h3>
    <p class="ln-body">Engraved on copper by J. Walker and sons, the sheet records every sounding taken by the survey ship Rifleman over the winter of 1870. Depths are given in fathoms at low water of ordinary spring tides; the small figures with a bar beneath them dry at that state of the tide. The wreck of the brig Alma lies north of the outer buoy, marked with its mast above water.</p>
    <p class="ln-fine">Bearings are magnetic. Variation 19°40′ W in 1871, decreasing about 7′ annually. Heights of hills are in feet above high water. The anchorage shown in the inner road is not recommended in southerly gales.</p>
  </article>
</div>
```

## Usage

Put it on: a block of text: an article, a column of fine print, a spec sheet or a caption; the lens shows its text over its solid background. Not img, video, canvas, svg or form fields.

`start()` registers `lens`, so the attribute alone puts it on an element.

```html
<article data-dio="lens" tabindex="0"><h2>Terms of carriage</h2><p>Liability for checked baggage is limited to…</p></article>
```

In page transitions: The lens fades with the page.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `radius` | `data-dio-radius` | `number` 40 to 200 px | `90` | Radius of the lens on screen in px. |
| `zoom` | `data-dio-zoom` | `number` 1 to 4 | `2` | Magnification in the middle of the lens; 1 is plain glass. |
| `dispersion` | `data-dio-dispersion` | `number` 0 to 3 | `1` | How far the glass splits colors toward the rim; 0 has no color fringes. |
| `thickness` | `data-dio-thickness` | `number` 0 to 1 | `0.4` | How domed the glass is; thicker bends the rim more and hovers lower. |
| `tilt` | `data-dio-tilt` | `number` 0 to 30° | `12` | Largest lean into the motion in degrees; 0 keeps the lens level. |
| `ease` | `data-dio-ease` | `number` 0 to 0.5 s | `0.12` | Seconds the lens lags behind the pointer; 0 follows it exactly. |

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`opacity`](/docs/api/inputs#opacity), [`reducedMotion`](/docs/api/inputs#input-reducedMotion). Draws only while something on it moves, then stops. None at rest and none while the lens is hidden. Full rate while it moves, leans, settles or fades in and out. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it shows a still frame: motion inputs are zero and time stands still. The lens jumps with the pointer: no lag, no lean.

Runs on every device. The lens appears while a finger is pressed on the text, held a little above the finger so it stays visible, and leaves when the finger lifts or the page scrolls.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { lens: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 11.1 KB the first time a page uses it. GPU: One quad around the lens and its shadow while it shows; three rays through the glass per pixel, one per color. Memory: A canvas of the text at up to four device pixels per CSS px with mip levels, at most about six million pixels.

- low: The text is rastered at the screen resolution instead of up to four times it.

See [the quality plugin](/docs/effects/quality).

## Recipes

### Fine print a reader can inspect

On terms, spec sheets and catalogue entries, the lens lets a visitor read the small text closely. A `tabindex` makes it reachable from the keyboard: the arrow keys walk it along the lines.

```html
<article data-dio="lens" tabindex="0">
  <h2>Terms of carriage</h2>
  <p>Liability for checked baggage is limited to…</p>
</article>
```

### A stronger lens without color fringes

A higher `zoom` reads tiny print; `dispersion="0"` turns off the colored fringes at the rim for a cleaner, technical look.

```html
<div class="specs" data-dio="lens" data-dio-zoom="3" data-dio-dispersion="0">
  Sensor 24.6 MP…
</div>
```

## Notes

The lens is traced per pixel, not drawn as a picture of a lens. A ray from the camera meets the front face of a thick biconvex lens, bends through the glass to its back face, bends again and lands on the page; the lens hovers at the height that gives `zoom` in its middle, and leans toward the camera the way a hand holds a reading glass. Toward the rim the faces are steeper, so the text bends and the three colors, traced with slightly different refractive indices, drift apart; `dispersion` sets how far. The glass reflects a softbox and a dim studio, more strongly where you see it at a grazing angle, and on the page beside it lies its shadow, the bright spot of light it focuses and the shadow of its handle, all projected from the same light.

What the lens shows is the element’s own text, rastered from the DOM, over the element’s background color, or the color of the page behind it when it has none. The engine cannot read other markup: images, underlines, borders and background images inside the element are not seen through the glass, and a background image shows as a flat color there, with a warning in development. Put the effect on a block of text on a solid background, such as an article, a column of fine print or a spec sheet.

The text around the lens stays the real DOM text, so selection, find in page and screen readers work as always; the lens draws over it only while it shows. Nothing is drawn at rest. With a `tabindex` on the element the arrow keys walk the lens along the lines, Home and End jump to the ends of a line, and Escape puts it away. On a touch screen the lens appears while a finger is pressed on the text, a little above the finger.

A very long element is rastered at a lower resolution to stay within about six million pixels, which softens the magnified text; put the effect on each section of a long page instead.

## Related

- [assemble](/docs/effects/assemble.md): Thousands of lit particles fly out of the depth around the page and gather into a heading.
- [blinds](/docs/effects/blinds.md): A heading printed across real 3D slats that turn over in a wave, lit and casting shadows, to show it.
- [counter](/docs/effects/counter.md): Stats count up to their value on real 3D mechanics: odometer drums, slot machine reels or flip cards.
- [dust](/docs/effects/dust.md): A heading crumbles into fine sand that lifts off the page and drifts away in depth as it scrolls out, or on a click.
- [emboss](/docs/effects/emboss.md): Letters pressed into or raised out of paper, leather, brushed metal, stone or plaster, lit by a light that follows the pointer.
- [extrude](/docs/effects/extrude.md): A headline becomes a solid 3D block: beveled letters with lit side walls, seen in perspective, that turn toward the pointer.
- [fire](/docs/effects/fire.md): The letters burn: flames rise from their tops, sparks drift up in depth and the firelight flickers on the page.
- [hologram](/docs/effects/hologram.md): A heading becomes a 3D hologram: projected from a glowing emitter, built of slices in depth, crossed by scan lines.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
