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

As the pointer comes near a heading or a link, the letters around it lift off the page as thin glossy slabs. By default they are pushed away and turn their faces from the pointer, the rest of the line making room so no letter runs over its neighbor, and their sides and beveled edges catch the light and each casts its own soft shadow on the page. Other styles turn the letters to face the pointer, raise the ones under it toward the viewer while their neighbors make room, like the macOS dock seen in perspective, or toss them apart, each spinning its own way. Every letter rides a spring, so when the pointer moves on they wobble back into place and the element is the real text again.

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

A concert poster headline whose red letters lift off the page as glossy slabs, push away from the pointer and cast soft shadows.

```html title="index.html"
<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>
```

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

```html
<h1 data-dio="magnet">Make some noise</h1>
```

In page transitions: Fades with the page.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-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. |
| `radius` | `data-dio-radius` | `number` 0.5 to 8 em | `2.5` | Reach of the pointer in em of the text, so a link and a headline feel alike. |
| `strength` | `data-dio-strength` | `number` 0 to 3 | `1` | How far letters move, rise and turn; 0 leaves them in place. |
| `rotate` | `data-dio-rotate` | `number` 0 to 80° | `30` | Largest turn of a letter in 3D at the pointer; 0 keeps faces to the viewer. |
| `stiffness` | `data-dio-stiffness` | `number` 20 to 600 | `180` | Pull of each letter toward where it goes; higher is snappier. |
| `damping` | `data-dio-damping` | `number` 2 to 60 | `12` | Friction of the springs; lower wobbles longer, about 26 does not wobble. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `style`: `repel`, `attract`, `lift`, `scatter`
  - `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.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`presence`](/docs/api/inputs#presence), [`size`](/docs/api/inputs#size), [`x`](/docs/api/inputs#input-x), [`y`](/docs/api/inputs#input-y). Draws only while something on it moves, then stops. Only while letters move toward the pointer or spring back; none at rest. See [how a frame is decided](/docs/concepts/frames).

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

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

start({ effects: { magnet: { reducedMotion: 'static' } } })
```

## Cost and quality

Loads 10.9 KB 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.

- low: The letters are rastered at 1 device pixel per CSS pixel instead of 2.

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

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

A restaurant navigation bar whose link letters turn toward the pointer and take the hover color.

```html title="index.html"
<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.

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

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

## 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)
