Diorama

Made for a big screen.

This site is one 3D scene, built for a computer: a wide window and a mouse. Open dioramajs.dev on a desktop or a laptop to see it.This site is one 3D scene, built for a wide window. Make this one wider, or open dioramajs.dev on a larger screen.

GitHub

DioramaDocs

lens

TextAlso in3D and product

@dioramajs/text/lensSource

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

View as Markdown
On this page

Plate XII · Admiralty chart 1871

The harbour at low water

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.

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.

Live preview needs WebGPU or WebGL 2. A catalogue entry for an engraved sea chart on cream paper, with a heading, a paragraph and a line of fine print.

This example failed to draw.

Move the pointer over the text, or focus it and use the arrow keys. Change the zoom, the radius and the dispersion of the glass.
Params of lens

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. The tabs below are the example above, with the values you set.

To change its code, copy the source into your effects folder; the copy replaces the built-in.

Terminal window
npx @dioramajs/diorama add lens
<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>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('.ln-entry', 'lens', {
radius: 90,
zoom: 2,
dispersion: 1,
thickness: 0.4,
tilt: 12,
ease: 0.12,
})
import { start } from '@dioramajs/diorama'
import lens from '@dioramajs/text/lens'
const app = start()
app.attach('.ln-entry', lens({
radius: 90,
zoom: 2,
dispersion: 1,
thickness: 0.4,
tilt: 12,
ease: 0.12,
}))

Page transitions

The lens fades with the page. How a flight flies.

Params

Every param is also an attribute, and both app.attach() and the handle take it by name.

ParamTypeDefaultDescription
radiusdata-dio-radiusnumber40 to 200 px90Radius of the lens on screen in px.
zoomdata-dio-zoomnumber1 to 42Magnification in the middle of the lens; 1 is plain glass.
dispersiondata-dio-dispersionnumber0 to 31How far the glass splits colors toward the rim; 0 has no color fringes.
thicknessdata-dio-thicknessnumber0 to 10.4How domed the glass is; thicker bends the rim more and hovers lower.
tiltdata-dio-tiltnumber0 to 30°12Largest lean into the motion in degrees; 0 keeps the lens level.
easedata-dio-easenumber0 to 0.5 s0.12Seconds the lens lags behind the pointer; 0 follows it exactly.

Inputs and frames

It reads delta, opacity and 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. How a frame is decided.

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.

To change what reduced motion does, patch it in start().

import { start } from '@dioramajs/diorama'
start({ effects: { lens: { reducedMotion: 'off' } } })

Cost and quality

Download
11.1 KB min+gzip 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.
Frames
Idle at rest

It runs from the low tier up. Each tier changes this: The quality plugin picks the tier for each device.

TierWhat changes
lowThe text is rastered at the screen resolution instead of up to four times it.

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.

<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.

<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.