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

magnet

TextAlso in3D and productBlocks and cards

@dioramajs/text/magnetSource

Letters become lit 3D slabs that react to the pointer: they push away, turn toward it, rise like a dock or tumble, and spring back.

View as Markdown
On this page

Warehouse Sessions · Friday 21 November · Doors 9 pm

Make some noise

Lua Preta, The Tram Lines, Okapi Sound System

Live preview needs WebGPU or WebGL 2. A concert poster headline whose red letters lift off the page as glossy slabs, push away from the pointer and cast soft shadows.

This example failed to draw.

Move the pointer over the headline, then switch the style.
Params of magnet
style

repel pushes letters off and turns their faces away from the pointer.

Usage

Put it on a heading, a link, a button label or a short line of text. Not img, video, canvas, svg or form fields. start() registers magnet, 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 magnet
<div class="mg-poster">
<p class="mg-kicker">Warehouse Sessions · Friday 21 November · Doors 9 pm</p>
<h2
class="mg-headline"
data-dio="magnet"
>Make some noise</h2>
<p class="mg-lineup">Lua Preta, The Tram Lines, Okapi Sound System</p>
</div>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('.mg-headline', 'magnet', {
style: 'repel',
radius: 2.5,
strength: 1,
rotate: 30,
stiffness: 180,
damping: 12,
})
import { start } from '@dioramajs/diorama'
import magnet from '@dioramajs/text/magnet'
const app = start()
app.attach('.mg-headline', magnet({
style: 'repel',
radius: 2.5,
strength: 1,
rotate: 30,
stiffness: 180,
damping: 12,
}))

Page transitions

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
stylerebuildsdata-dio-style'repel' | 'attract' | 'lift' | 'scatter''repel'
  • repel pushes letters off and turns their faces away from the pointer.
  • attract turns them to face it and draws them in.
  • lift raises the letters under it toward the viewer while neighbors make room.
  • scatter tosses them up and apart, each spinning its own way.
radiusdata-dio-radiusnumber0.5 to 8 em2.5Reach of the pointer in em of the text, so a link and a headline feel alike.
strengthdata-dio-strengthnumber0 to 31How far letters move, rise and turn; 0 leaves them in place.
rotatedata-dio-rotatenumber0 to 80°30Largest turn of a letter in 3D at the pointer; 0 keeps faces to the viewer.
stiffnessdata-dio-stiffnessnumber20 to 600180Pull of each letter toward where it goes; higher is snappier.
dampingdata-dio-dampingnumber2 to 6012Friction of the springs; lower wobbles longer, about 26 does not wobble.

Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.

Inputs and frames

It reads delta, presence, size, x and y . Draws only while something on it moves, then stops. Only while letters move toward the pointer or spring back; none at rest. How a frame is decided.

Reduced motion and touch

With reduced motion it is off and the element shows as plain HTML. Off: the text stays as it is, and the engine does not load for it.

Runs on every device. A finger pressed on the text moves the letters around it until it lifts or the page starts to scroll.

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

import { start } from '@dioramajs/diorama'
start({ effects: { magnet: { reducedMotion: 'static' } } })

Cost and quality

Download
10.9 KB min+gzip the first time a page uses it
GPU
Two instanced draws: five quads per letter for its face and sides, one for its shadow, two texture reads per pixel.
Memory
An atlas of the letters at twice their CSS size with room for the shadows, and a relief map of the same size, kept while the element lives.
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 letters are rastered at 1 device pixel per CSS pixel instead of 2.

Recipes

style="attract" turns each letter near the pointer to face it, so a link seems to look at the visitor as the pointer approaches. The radius is measured in em of the text, so the default reach of 2.5 em covers a word or two of a 16 px link and does not wake the whole menu. A hover color in your CSS shows on the moving letters too. Hover the links below.

Live preview needs WebGPU or WebGL 2. A restaurant navigation bar whose link letters turn toward the pointer and take the hover color.

This example failed to draw.

Params of magnet
<nav class="mg-nav" aria-label="Restaurant">
<a class="mg-brand" href="/top">Casa Lume</a>
<a
href="/menu"
data-dio="magnet"
data-dio-style="attract"
>Menu</a>
<a
href="/visit"
data-dio="magnet"
data-dio-style="attract"
>Visit</a>
<a
href="/events"
data-dio="magnet"
data-dio-style="attract"
>Events</a>
<a
href="/gift"
data-dio="magnet"
data-dio-style="attract"
>Gift cards</a>
</nav>

A word mark that magnifies like a dock

style="lift" raises the letters under the pointer toward the viewer, so they grow in the page’s perspective, and slides their neighbors along the line to make room. It suits a short, large word such as a studio name or a product name in a hero.

<p
class="mark"
data-dio="magnet"
data-dio-style="lift"
>studio</p>

Loose letters for a party poster

style="scatter" tosses the letters up and apart, each at its own angle and spin. A low damping keeps them wobbling for a while after the pointer leaves, and a lower stiffness makes them float back more slowly.

<h2
data-dio="magnet"
data-dio-style="scatter"
data-dio-damping="6"
data-dio-stiffness="120"
>Party starts at nine</h2>

Notes

Each letter is drawn on its own as a thin slab with beveled edges, lit from the upper left, so its face brightens or darkens as it turns, its sides show, and it casts a soft shadow shaped like the letter. A letter at rest is flat and square to the viewer in exactly your CSS color, so the hand-over to the real text is invisible. Shadows show best on a light background; under text smaller than about 40 px they fade out, so a small link stays crisp.

The element keeps its text as you wrote it, with no span per letter, so screen readers, search engines and find in page see the real words. While letters move a CSS filter hides the DOM text without removing it, and once they settle the element is real, selectable text again. Under reduced motion the effect is off.

Put the effect on the text itself: on a button with a background, wrap the label in a span and put data-dio="magnet" there, since the element’s own box is hidden while the letters move.

The canvas cannot draw background-clip: text, text-shadow, -webkit-text-stroke, underlines or vertical writing modes the way the browser does. An element with any of the first four keeps its text as it is, and a warning in development names the reason; an underline is hidden while the letters move. Keep it to headings, links and short lines: over 1500 letters the text stays as it is. style rebuilds the effect when it changes.