# text-glitch

A heading glitches in depth: it breaks into slabs that jump at the viewer, then snaps back flat.

For about two thirds of a second the text breaks into horizontal slabs that jump sideways and towards or away from the viewer, turning in real perspective. Red and cyan layers sit at different depths behind and in front of the letters, so they part as the line tilts. The slice style turns struck slabs into solid blocks with shaded sides; vhs leans the line back with a hologram of scanlines trailing behind it and bends it like worn tape. The pattern jumps twenty times a second; a burst hits twice, with a moment of lost signal between the hits where the text dims, then the element is the real text again, selectable and crisp.

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

A live music night headline that breaks into slabs jumping at the viewer every few seconds, with red and cyan layers parting in depth.

```html title="index.html"
<section class="tg-hero">
  <p class="tg-kicker">Live from Studio 9 · Friday 23:00</p>
  <h2 class="tg-title" data-dio="text-glitch" data-dio-trigger="interval" data-dio-interval="2.5">Signal lost</h2>
  <p class="tg-sub">Four hours of broken beats, tape loops and analog noise.</p>
</section>
```

## Usage

Put it on: a heading, a label, a link or a button with text. Not img, video, canvas, svg or form fields.

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

```html
<h2 data-dio="text-glitch">Signal lost</h2>
```

In page transitions: Leaving a page the slabs fly at the viewer and drop out one by one; entering they fall back into place.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'rgb' \| 'slice' \| 'vhs'` | `'rgb'` | rgb tilts the line and parts its color layers; slice extrudes blocks; vhs is a hologram. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'hover' \| 'interval' \| 'scroll' \| 'load'` | `'hover'` | hover on pointer, focus or tap; interval every few seconds; scroll with speed; load once. |
| `strength` | `data-dio-strength` | `number` 0 to 2 | `1` | How many slabs break and how far they jump and turn; 0 keeps them in place. |
| `split` | `data-dio-split` | `number` 0 to 40 px | `6` | How far the red and cyan layers part in depth and sideways; 0 for none. |
| `slices` | `data-dio-slices` | `number` 1 to 16 | `5` | About how many slabs each line of text breaks into. |
| `interval` | `data-dio-interval` | `number` 0.5 to 10 s | `3` | Average time between bursts with trigger interval, varied by the seed. |
| `duration` | `data-dio-duration` | `number` 0.1 to 2 s | `0.7` | Length of one burst; from 0.4 s it hits twice with the signal lost between. |
| `seed` | `data-dio-seed` | `number` 1 to 100 | `1` | Which glitch plays: the same seed gives the same one. |

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

### Variants

- `style`: `rgb`, `slice`, `vhs`
  - `rgb` tilts the line and parts its color layers.
  - `slice` extrudes blocks.
  - `vhs` is a hologram.
- `trigger`: `hover`, `interval`, `scroll`, `load`
  - `hover` on pointer, focus or tap.
  - `interval` every few seconds.
  - `scroll` with speed.
  - `load` once.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`presence`](/docs/api/inputs#presence), [`size`](/docs/api/inputs#size). Draws only while something on it moves, then stops. Only during a burst, and with trigger scroll while the page scrolls fast; none at rest. A timer waits between interval bursts without drawing. 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 is shown as it is, and the engine does not load for it. Sudden flicker can trouble people sensitive to motion and flashing. With motion on, the text dims at most 3 times in any second, under the WCAG flash threshold.

Runs on every device. hover plays a burst on tap and on keyboard focus. The other triggers work the same as with a mouse.

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

start({ effects: { 'text-glitch': { reducedMotion: 'static' } } })
```

## Cost and quality

Loads 9.1 KB the first time a page uses it. GPU: One instanced draw of a quad per layer of a slab, one texture read per pixel; a block adds 14 layers to its slab, the hologram 7. Memory: A canvas of the text at twice its CSS size with mip levels, kept while the element lives.

- low: The text is rastered at 1 device pixel per CSS pixel instead of 2.

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

## Recipes

### A lineup whose names glitch under the pointer

On a festival, music or gaming site, each act or title glitches as the pointer enters its card, and stays calm the rest of the time. The effect listens on the link the heading sits in, so the whole card triggers it, and keyboard focus does too. The `slice` style turns the struck slabs into solid blocks that jump out of the page. Give each card its own `seed` so no two break alike.

Three festival lineup cards on black whose act names break into 3D blocks when a card is hovered.

```html title="index.html"
<ul class="tg-lineup">
  <li>
    <a href="/night-strings">
      <img src="/media/festival/cello.webp" alt="The body of a cello with its f-hole and bridge" width="960" height="1200" />
      <h3 data-dio="text-glitch" data-dio-style="slice">Night Strings</h3>
      <span>Fri 23:00</span>
    </a>
  </li>
  <li>
    <a href="/brass-signal">
      <img src="/media/festival/flugelhorn.webp" alt="A silver flugelhorn on black" width="960" height="1200" />
      <h3 data-dio="text-glitch" data-dio-style="slice" data-dio-seed="2">Brass Signal</h3>
      <span>Sat 21:30</span>
    </a>
  </li>
  <li>
    <a href="/orbit-sessions">
      <img src="/media/festival/milky-way.webp" alt="The Milky Way over the curve of the Earth" width="1400" height="933" />
      <h3 data-dio="text-glitch" data-dio-style="slice" data-dio-seed="3">Orbit Sessions</h3>
      <span>Sun 00:15</span>
    </a>
  </li>
</ul>
```

### A hologram title as it first shows

The `vhs` style leans the line back with a stack of scanline copies trailing behind it, like a hologram losing its signal, and bends it like worn tape. With `trigger="load"` it plays once, when half of the heading is in view.

```html
<h2 data-dio="text-glitch" data-dio-style="vhs" data-dio-trigger="load">Transmission 1994</h2>
```

### A headline that breaks up with scroll speed

With `trigger="scroll"` the glitch follows the scroll speed: a slow read leaves the heading alone, a fast flick tears it apart for a moment.

```html
<h1 data-dio="text-glitch" data-dio-trigger="scroll" data-dio-strength="1.5">Faster, louder</h1>
```

## Notes

The glitch is drawn in 3D through the page camera: slabs move towards and away from the viewer, so a heading far from the middle of the screen shows more parallax than one in the center. Between bursts the element is plain DOM text, selectable and read by screen readers as written.

Keep bursts short and rare on pages people read: three flashes or more per second can trigger seizures, so `duration` stays under a second and `interval` above two seconds. Under reduced motion the effect does not run at all.

The canvas cannot draw `background-clip: text`, `text-shadow`, `-webkit-text-stroke` or vertical writing modes the way the browser does; such text stays as it is, with a warning in development.

`style` and `trigger` rebuild the effect; `strength`, `split`, `slices`, `interval`, `duration` and `seed` 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)
