# glitch

A digital glitch in short bursts: blocks slide sideways, color channels split, scanlines flicker.

For a fraction of a second the picture breaks up: horizontal blocks jump sideways and wrap around, red and blue part ways, scanlines darken and the frame flickers, with flashes of static. The pattern jumps twenty times a second and the burst stutters, so it reads as a broken signal rather than an animation. Between bursts the copy is the image as it is. The same seed gives the same glitch on every visit.

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

A row of three new releases of a record label, each cover breaking up in a short digital glitch every couple of seconds.

```html title="index.html"
<ul class="gl-releases">
  <li>
    <img src="/media/festival/notes-01.webp" alt="Signal Notes 01, T-minus" width="480" height="480" data-dio="glitch" data-dio-trigger="interval" data-dio-interval="2" />
    <p>T-minus <span>Single, out now</span></p>
  </li>
  <li>
    <img src="/media/festival/notes-02.webp" alt="Signal Notes 02, The Eagle Has Landed" width="480" height="480" data-dio="glitch" data-dio-trigger="interval" data-dio-interval="2" data-dio-seed="2" />
    <p>The Eagle Has Landed <span>EP, 14 March</span></p>
  </li>
  <li>
    <img src="/media/festival/notes-03.webp" alt="Signal Notes 03, We've Had a Problem" width="480" height="480" data-dio="glitch" data-dio-trigger="interval" data-dio-interval="2" data-dio-seed="3" />
    <p>We've Had a Problem <span>Album, 2 May</span></p>
  </li>
</ul>
```

## Usage

Put it on: img, video.

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

```html
<img src="/cover.avif" alt="Album cover" data-dio="glitch" />
```

In page transitions: Leaving a page breaks the copy up and drops it block by block; entering brings it back.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'rgb' \| 'slice' \| 'vhs'` | `'rgb'` | rgb splits the colors in thin slices; slice shifts big blocks; vhs wobbles like worn tape. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'hover' \| 'interval' \| 'scroll' \| 'load'` | `'hover'` | hover plays on enter or tap; interval every few seconds; scroll with speed; load once. |
| `strength` | `data-dio-strength` | `number` 0 to 2 | `1` | How many blocks break and how far they jump; 0 keeps only split and lines. |
| `blocks` | `data-dio-blocks` | `number` 2 to 40 | `12` | About how many bands the picture breaks into. |
| `split` | `data-dio-split` | `number` 0 to 40 px | `8` | How far red and blue part, at the peak of a burst. |
| `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.45` | Length of one burst. |
| `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` splits the colors in thin slices.
  - `slice` shifts big blocks.
  - `vhs` wobbles like worn tape.
- `trigger`: `hover`, `interval`, `scroll`, `load`
  - `hover` plays on enter or tap.
  - `interval` every few seconds.
  - `scroll` with speed.
  - `load` once.

## Inputs and frames

Reads [`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. Nothing glitches: sudden flicker can trouble people sensitive to motion and flashing.

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

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

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

## Cost and quality

Loads 1.9 KB the first time a page uses it. GPU: Three texture reads and a few integer hashes per pixel. Memory: The shared texture of the image or video.

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

## Recipes

### A lineup that glitches under the pointer

On a festival, music or gaming site, each act or title gets a short burst as the pointer enters its card, and the card is calm the rest of the time. The `slice` style shifts whole blocks and smears a few into streaks. Give each card its own `seed` so no two glitch alike.

Three festival lineup cards on black: a cello, a flugelhorn and the night sky, each glitching as the pointer enters.

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

### A product film on worn tape

The effect runs on a playing `<video>` as well as on an image. The `vhs` style wobbles the frame, washes out the color and rolls a band of bad tracking down the picture. With `trigger="interval"` the burst comes back every few seconds, at slightly uneven times, while the video is on screen.

A short film of a mustard lounge chair in a sunlit room, breaking up like a worn VHS tape every few seconds.

```html title="index.html"
<video class="tape" poster="/media/film/lounge.webp" width="1280" height="720" autoplay muted loop playsinline aria-label="Product film, 12 seconds, no sound: a lounge chair in mustard velvet in a sunlit room" data-dio="glitch" data-dio-style="vhs" data-dio-trigger="interval" data-dio-interval="2.5" data-dio-duration="0.9">
  <source src="/media/film/lounge.webm" type="video/webm" />
  <source src="/media/film/lounge.mp4" type="video/mp4" />
</video>
```

### A hero that breaks up with scroll speed

With `trigger="scroll"` the glitch follows the scroll speed: a slow read leaves the image alone, a fast flick tears it apart for a moment. It suits a streetwear drop or a launch page where the hero should feel loud.

```html
<img
  src="/drop/hero.avif"
  data-dio="glitch"
  data-dio-trigger="scroll"
  data-dio-style="slice"
  data-dio-strength="1.5"
/>
```

## Notes

The glitch changes how the image looks for a fraction of a second; its `alt` stays the same. 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 unless the glitch is small.

Under reduced motion the effect does not run at all and the image shows as it is.

`style` and `trigger` rebuild the effect; `strength`, `blocks`, `split`, `interval`, `duration` and `seed` change live.

## Related

- [bend](/docs/effects/bend.md): Scroll speed bends the image like paper and splits its color channels.
- [compare](/docs/effects/compare.md): Two pictures in one box, before and after, split by a line you drag, hover or scroll.
- [flare](/docs/effects/flare.md): A lens flare or a glossy light sweep over a photo that follows the pointer or the scroll.
- [frost](/docs/effects/frost.md): Frosted glass over a photo, or over the whole page, that the pointer wipes clear and that slowly fogs back.
- [image](/docs/effects/image.md): Draws the image or video unchanged, as a GL copy that page flights and other effects build on.
- [liquid](/docs/effects/liquid.md): The pointer leaves a fluid trail on a photo that settles back like water.
- [parallax](/docs/effects/parallax.md): The image drifts inside its frame with the pointer and the scroll; with a depth map, in 2.5D.
- [pixelate](/docs/effects/pixelate.md): The image resolves from big pixels to sharp as it scrolls into view, or breaks into pixels on hover.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
