# prism

Letters become crystal: their edges split a light into a crisp spectrum that slides around them as the light moves.

The text turns into bevelled glass in its own color, standing a little off the page. The face stays crisp ink; across each bevel the glass bends the view outward, and every color by its own amount, so the edge of the ink splits into a clean spectrum, as through a prism. Sharp highlights and thin lines of light caught inside the edges follow a lamp, and where a bevel faces away from it the light leaves in a thin saturated spectrum along the contour, with a fainter one landing on the page just past the letter. The rainbows gather near the lamp and slide around the curves as it moves: it follows the pointer, sweeps with the scroll, orbits slowly or passes once as the text comes into view.

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

A crystal workshop's page on warm white paper whose name is glass in its own color, its edges splitting the light into crisp spectra that follow the pointer.

```html title="index.html"
<div class="pr-hero">
  <p class="pr-kicker">Atelier Lumen · hand-cut lead-free crystal</p>
  <h2 class="pr-headline" data-dio="prism">Lumen</h2>
  <p class="pr-line">Each piece is cut, polished and signed in our Bohemian workshop, and throws a little sunlight across your table.</p>
</div>
```

## Usage

Put it on: a large heading or short display line in a bold font, on a plain background. Not img, video, canvas, svg or form fields.

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

```html
<h1 data-dio="prism">Clarity, refined.</h1>
```

In page transitions: The glass and its light fade with the page.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'prism' \| 'crystal' \| 'diamond'` | `'prism'` | prism round glass with smooth spectra; crystal cut facets that flash colors; diamond more facets and fire. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'pointer' \| 'scroll' \| 'loop' \| 'view'` | `'pointer'` | pointer follows the pointer; scroll sweeps with the scroll; loop orbits; view passes once. |
| `dispersion` | `data-dio-dispersion` | `number` 0 to 2 | `1` | How far the colors pull apart; 0 keeps the light white, with no rainbow. |
| `spread` | `data-dio-spread` | `number` 0 to 2 | `1` | How far past the letters the thrown spectrum lands; 0 turns it off. |
| `strength` | `data-dio-strength` | `number` 0 to 2 | `1` | Brightness of the rainbows, the highlights and the sparkle. |
| `bevel` | `data-dio-bevel` | `number` 0.01 to 0.15 em | `0.07` | Width of the bevel along every edge in ems; larger is rounder glass. |
| `light` | `data-dio-light` | `number` 0 to 360° | `300` | Where the light comes from at rest as a CSS angle, 0 above and 90 right. |
| `follow` | `data-dio-follow` | `number` 0 to 1 | `1` | How far the light moves toward the pointer or with the scroll; 0 holds it. |
| `fps` | `data-dio-fps` | `number` 0 to 60 | `30` | Frame rate of the loop; 0 holds the light still. At most low 24. |

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

### Variants

- `style`: `prism`, `crystal`, `diamond`
  - `prism` round glass with smooth spectra.
  - `crystal` cut facets that flash colors.
  - `diamond` more facets and fire.
- `trigger`: `pointer`, `scroll`, `loop`, `view`
  - `pointer` follows the pointer.
  - `scroll` sweeps with the scroll.
  - `loop` orbits.
  - `view` passes once.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size), [`width`](/docs/api/inputs#input-width), [`x`](/docs/api/inputs#input-x), [`y`](/docs/api/inputs#input-y). Draws at a steady rate while it is on screen and stops when it leaves. None at rest: the glass and its spectra stay drawn. Full rate while the light settles after the pointer moves, one frame per scroll with trigger scroll, `fps` frames a second on a loop while in view, and a short sweep on view. 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 light stays at its angle and the spectra hold still.

Runs on every device. A finger does not move the light, so a tap or a swipe never flickers it; the scroll and loop triggers work as on desktop.

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

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

## Cost and quality

Loads 10.9 KB the first time a page uses it. GPU: One quad over the text and a margin around it; about 25 texture reads per pixel, three per color of the split light. Memory: A half-float relief of the text built once on the CPU from a distance field, and a canvas of the text.

- low: The glass is built at 1 px per CSS px instead of 2, the light is split into 5 colors instead of 7, and the loop runs at most 24 frames a second.

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

## Recipes

### Cut crystal on a dark page

`style="crystal"` cuts each stroke into flat facets that meet in a sharp ridge, the way a chiselled glass letter is cut, with small glints that flash as the light moves. Each facet that faces the light at the right angle lights up in one color of the spectrum, the way a cut stone flashes. On a dark page the spectra glow.

```html
<h2 data-dio="prism" data-dio-style="crystal" style="color: #c9d4e6">Brilliant cut</h2>
```

### A diamond line whose light orbits

`style="diamond"` cuts finer facets, pulls the colors apart the most and sparkles with fire as the light passes. With `trigger="loop"` the light orbits slowly at `fps` frames a second while the text is on screen, and stops when it scrolls away.

```html
<p class="display" data-dio="prism" data-dio-style="diamond" data-dio-trigger="loop">Forever</p>
```

## Notes

The glass is built from the shape of the letters: a distance field of the text gives every pixel its distance to the nearest edge, and a rounded profile over `bevel` turns that into the slope of the glass, so the edges catch the light the way a real bevel would. The face of each letter shows its ink as it is, crisp. Across a bevel the glass bends the view outward by its slope, and the light is split into seven colors, each bent a little more than the last: the edge of the ink comes apart into a thin spectrum inside the letter, and where a bevel faces away from the lamp at the right angle, the light leaves it in a saturated line of color that runs along the contour. Both gather near the lamp and slide around the curves as it moves, never as a blurred copy of the word. A fainter spectrum lands on the page right past the far edges; `spread` is how far, and 0 turns it off. It stays within a few hundredths of an em of the letters, so the element needs no extra room around it.

The light sits about the size of the element away, at `light` when nothing moves it. With `trigger="pointer"` it follows a mouse or a pen anywhere on the page and drifts as the element scrolls; after it settles nothing more is drawn. Set `follow` to 0 for a fixed light. Under reduced motion the light stays at its angle.

The heading stays real text in the DOM: screen readers read it, search engines index it, and find in page works. While the glass is drawn the element is hidden without being removed, so give the text a color that reads on its own, since that is what shows without WebGL. Use a bold, large font on a plain background: thin strokes have little glass to bend the light. The canvas cannot draw `background-clip: text`, `-webkit-text-stroke` or vertical writing modes the way the browser does, and an element with any of them keeps its text as it is, with a warning in development.

`style` and `trigger` rebuild the effect when they change. `bevel` builds the glass again a moment after it changes; the other params change live.

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