magnet
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.
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.
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.
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.
| Param | Type | Default | Description |
|---|---|---|---|
stylerebuildsdata-dio-style | 'repel' | 'attract' | 'lift' | 'scatter' | 'repel' |
|
radiusdata-dio-radius | number0.5 to 8 em | 2.5 | Reach of the pointer in em of the text, so a link and a headline feel alike. |
strengthdata-dio-strength | number0 to 3 | 1 | How far letters move, rise and turn; 0 leaves them in place. |
rotatedata-dio-rotate | number0 to 80° | 30 | Largest turn of a letter in 3D at the pointer; 0 keeps faces to the viewer. |
stiffnessdata-dio-stiffness | number20 to 600 | 180 | Pull of each letter toward where it goes; higher is snappier. |
dampingdata-dio-damping | number2 to 60 | 12 | Friction 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.
| Tier | What changes |
|---|---|
low | The letters are rastered at 1 device pixel per CSS pixel instead of 2. |
Recipes
Navigation links that turn to the pointer
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.
<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.