# pixelate

The image resolves from big pixels to sharp as it scrolls into view, or breaks into pixels on hover.

When enough of the image has entered the viewport it shows as large blocks of color that halve in size step by step, 32, 16, 8, 4, 2 px, until the image itself is there, like a picture loading on an old modem. Each block takes the average color of the part it covers. Blocks can be squares, round dots on the page behind or hexagons, and can shrink smoothly instead of in steps. With trigger hover the image is sharp and breaks into blocks under the pointer.

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

A photo of a brass astrolabe that resolves from large square pixels to the sharp image.

```html title="index.html"
<img class="astrolabe" src="/media/portfolio/astrolabe.webp" alt="A brass astrolabe, its rete of star pointers over a plate engraved with the signs of the zodiac" width="1400" height="933" data-dio="pixelate" />
```

## Usage

Put it on: img, video.

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

```html
<img src="/piece.avif" alt="Artwork" data-dio="pixelate" />
```

In page transitions: Leaving a page breaks the copy into blocks and fades it, entering resolves it. A flight lands on the sharp image.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'square' \| 'round' \| 'hex'` | `'square'` | square draws square blocks; round draws dots, the page between; hex draws hexagons. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'view' \| 'hover' \| 'scroll'` | `'view'` | view resolves it once in view; hover pixelates under the pointer; scroll scrubs it. |
| `motion` (rebuilds) | `data-dio-motion` | `'steps' \| 'smooth'` | `'steps'` | steps halves the blocks one size at a time; smooth shrinks them continuously. |
| `cell` | `data-dio-cell` | `number` 4 to 128 px | `32` | Side of the biggest blocks, where the reveal starts and hover ends. |
| `duration` | `data-dio-duration` | `number` 0.2 to 4 s | `1.2` | Length of the reveal, or of the change on hover. |
| `delay` | `data-dio-delay` | `number` 0 to 2 s | `0` | Wait after the image comes into view, for a cascade across a grid. |

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

### Variants

- `style`: `square`, `round`, `hex`
  - `square` draws square blocks.
  - `round` draws dots, the page between.
  - `hex` draws hexagons.
- `motion`: `steps`, `smooth`
  - `steps` halves the blocks one size at a time.
  - `smooth` shrinks them continuously.
- `trigger`: `view`, `hover`, `scroll`
  - `view` resolves it once in view.
  - `hover` pixelates under the pointer.
  - `scroll` scrubs it.

## Inputs and frames

Reads [`presence`](/docs/api/inputs#presence), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size). Draws only while something on it moves, then stops. Only while the blocks change: during the reveal and the hover change, and with trigger scroll while scrolling. None at rest. 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. With view and scroll the image is shown as it is, without blocks. With hover the blocks fade in at full size in 0.15 s instead of growing.

Runs on every device. view and scroll work the same as with a mouse; with hover the image stays sharp.

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

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

## Cost and quality

Loads 1.6 KB the first time a page uses it. GPU: Four texture reads per pixel while blocks show, one on the low tier. Memory: The shared texture of the image or video.

- low: Each block takes the color at its center instead of the average of four reads.

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

## Recipes

### Pieces that turn to pixels under the pointer

With `trigger="hover"` the image is sharp and breaks into blocks while the pointer is over it, which suits a retro or gaming portfolio and a gallery of digital art. A small `cell` and a short `duration` keep it playful rather than hiding the work.

Three artworks on black, a cyanotype, a tea bowl and an aurora, each breaking into pixels under the pointer.

```html title="index.html"
<ul class="pieces">
  <li><img src="/media/work/cyanotype.webp" alt="A cyanotype of a branching alga, white on deep blue" width="480" height="384" data-dio="pixelate" data-dio-trigger="hover" data-dio-cell="16" data-dio-duration="0.45" /></li>
  <li><img src="/media/work/hares-fur.webp" alt="A dark tea bowl with a streaked glaze" width="480" height="384" data-dio="pixelate" data-dio-trigger="hover" data-dio-cell="16" data-dio-duration="0.45" /></li>
  <li><img src="/media/work/polar-night.webp" alt="Green aurora over the night side of the Earth, seen from orbit" width="480" height="384" data-dio="pixelate" data-dio-trigger="hover" data-dio-cell="16" data-dio-duration="0.45" /></li>
</ul>
```

### Cascade across a grid

Give each image a slightly longer `delay` than the one before it, so a row resolves one tile after another as it scrolls into view.

```html
<div class="grid">
  <img src="/work/one.avif" data-dio="pixelate" />
  <img
    src="/work/two.avif"
    data-dio="pixelate"
    data-dio-delay="0.15"
  />
  <img
    src="/work/three.avif"
    data-dio="pixelate"
    data-dio-delay="0.3"
  />
</div>
```

### Hexagons tied to the scroll

With `trigger="scroll"` the blocks shrink as the image rises to the middle of the viewport and grow again when the reader scrolls back. `style="hex"` with `motion="smooth"` gives a softer, honeycomb look for an art or science page.

```html
<img
  src="/plates/algae.avif"
  data-dio="pixelate"
  data-dio-trigger="scroll"
  data-dio-style="hex"
  data-dio-motion="smooth"
/>
```

## Notes

A reveal on the first screen plays as the page opens: the image is hidden until the effect draws its first frame, at most three seconds. An image the reader already saw as plain HTML stays sharp and never breaks up again. Under reduced motion the reveal is skipped, and hover fades the blocks in without growing them.

In `round` the page shows between the dots, so the look depends on the background behind the image.

List `pixelate` before color effects such as `tone` in `data-dio`, so they color the blocks as well.

`style`, `trigger` and `motion` rebuild the effect; `cell`, `duration` and `delay` 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.
- [glitch](/docs/effects/glitch.md): A digital glitch in short bursts: blocks slide sideways, color channels split, scanlines flicker.
- [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.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
