# coverflow

A row of covers as the classic cover flow: the middle one faces you, its neighbours turn away in perspective.

The cover in the middle faces the viewer at the size of its image; the covers beside it turn towards it and step back, darker the farther they are, with an optional reflection on the floor. A drag, a sideways wheel or trackpad swipe, the arrow keys or a click on a side cover glide the next one to the middle; a click on the middle cover follows its link. The list stays in the page as written, a row of links that scrolls sideways without WebGL.

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

Eight project covers in a row; the middle one faces you, its neighbors turn away, mirrored on the floor; drag, swipe or use the arrow keys.

```html title="index.html"
<ul class="work" data-dio="coverflow" data-dio-index="3" data-dio-reflection="0.25" aria-label="Eight projects. Drag, swipe or use the arrow keys; the cover in the middle opens its project." style="touch-action: pan-y">
  <li><a href="/cyanotype"><img src="/media/work/cyanotype.webp" alt="Cyanotype Impressions: white fronds of seaweed on deep blue" width="1200" height="800" /></a></li>
  <li><a href="/atlas-of-ice"><img src="/media/work/atlas-of-ice.webp" alt="Atlas of Ice: snow-covered ridges under a pale sky" width="1200" height="800" /></a></li>
  <li><a href="/hares-fur"><img src="/media/work/hares-fur.webp" alt="Hare’s Fur: a dark tea bowl with a streaked glaze" width="1200" height="800" /></a></li>
  <li><a href="/measure-of-the-sky"><img src="/media/work/measure-of-the-sky.webp" alt="The Measure of the Sky: the brass face of an astrolabe" width="1200" height="800" /></a></li>
  <li><a href="/polar-night"><img src="/media/work/polar-night.webp" alt="Polar Night: green aurora over the night side of the Earth" width="1200" height="800" /></a></li>
  <li><a href="/outer-coast"><img src="/media/work/outer-coast.webp" alt="Outer Coast: dark cliffs above a grey sea under clouds" width="1200" height="800" /></a></li>
  <li><a href="/sea-steps"><img src="/media/work/sea-steps.webp" alt="Sea Steps: concrete steps into a grey sea" width="1200" height="800" /></a></li>
  <li><a href="/tranquility"><img src="/media/work/tranquility.webp" alt="Tranquility: boulders on grey lunar ground" width="1200" height="800" /></a></li>
</ul>
```

## Usage

Put it on: a list of linked images, such as albums, books, products or case studies.

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

```html
<ul class="albums" data-dio="coverflow" data-dio-reflection="0.3" aria-label="Albums" style="touch-action: pan-y"><li><a href="/albums/tides"><img src="/covers/tides.avif" alt="Tides" /></a></li><li><a href="/albums/salt"><img src="/covers/salt.avif" alt="Salt" /></a></li><li><a href="/albums/north"><img src="/covers/north.avif" alt="North" /></a></li></ul>
```

In page transitions: Fades with the page transition.

- Event `dio:index`, detail `{ index: number }`: A cover reached the middle after a move; index is its place among the images, from 0. Bubbles.

- Event `dio:open`, detail `{ index: number }`: The middle cover was clicked and its image is in no link or button; index is its place. Bubbles.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `index` | `data-dio-index` | `number` 0 to 10 | `0` | Cover in the middle from 0; a fraction holds the row between two covers, for a tween. |
| `style` (rebuilds) | `data-dio-style` | `'classic' \| 'arc' \| 'cards'` | `'classic'` | classic turns the side covers towards the middle; arc lays the row on a wheel turning about a point below it; cards stacks them behind the middle one, smaller and slightly turned. |
| `angle` | `data-dio-angle` | `number` 0 to 80° | `60` | Turn of the side covers in classic, and of the cards behind; 0 lays them flat. |
| `spacing` | `data-dio-spacing` | `number` 0.05 to 1 | `0.3` | Gap between side covers, in widths of a cover. |
| `depth` | `data-dio-depth` | `number` 0 to 2 | `0.6` | How far the side covers step back, in widths of a cover; 0 keeps them level. |
| `shade` | `data-dio-shade` | `number` 0 to 1 | `0.4` | Darkening of the side covers, 0 for none. |
| `reflection` | `data-dio-reflection` | `number` 0 to 1 | `0` | Opacity of the covers mirrored on the floor below them, 0 for none. At most low 0. |
| `perspective` | `data-dio-perspective` | `number` 300 to 3000 px | `900` | Distance of the eye from the row, like CSS perspective; lower is stronger. |
| `duration` | `data-dio-duration` | `number` 0.15 to 2 s | `0.5` | Length of a move by one cover. |
| `loop` | `data-dio-loop` | `boolean` | `false` | Goes round: after the last cover comes the first. |
| `drag` | `data-dio-drag` | `boolean` | `true` | A drag or a swipe carries the row. |
| `wheel` | `data-dio-wheel` | `boolean` | `true` | A sideways wheel or trackpad swipe moves the row. |
| `click` | `data-dio-click` | `boolean` | `true` | A click on a side cover brings it to the middle. |
| `items` (rebuilds) | `data-dio-items` | `string` | `'img'` | Selector of the images inside the list. |

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

### Variants

- `style`: `classic`, `arc`, `cards`
  - `classic` turns the side covers towards the middle.
  - `arc` lays the row on a wheel turning about a point below it.
  - `cards` stacks them behind the middle one, smaller and slightly turned.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`opacity`](/docs/api/inputs#opacity), [`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 only while something on it moves, then stops. Only while the row moves; 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. Covers change at once: no glide, and a drag lands on its cover without coasting. Keys, clicks and swipes still move the row.

Runs on every device. A swipe moves the row. With touch-action: pan-y on the list the row follows the finger; without it the browser scrolls the list sideways and the row follows that scroll. A tap on a side cover brings it to the middle, on the middle one follows its link.

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

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

## Cost and quality

Loads 7.2 KB the first time a page uses it. GPU: A quad per cover in reach, up to fifteen, one texture read per pixel; the reflection doubles the pixels of each cover. Memory: The pictures of the covers in reach and one more on each side.

- low: No reflection.

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

## Recipes

### Books stacked as cards

The `cards` style puts the covers behind the middle one, a little to the side and smaller, like a hand of cards: it fits a short shelf of books or products in a narrow box. With `loop` the row goes round, so the last book leads back to the first.

Three book covers stacked as cards, the title of the one in the middle under them.

```html title="index.html"
<ul class="shelf" data-dio="coverflow" data-dio-style="cards" data-dio-loop="true" data-dio-index="1" aria-label="Three books. Click, swipe or use the arrow keys for the next one." style="touch-action: pan-y">
  <li><a href="/blue-shore"><img src="/media/books/blue-shore.webp" alt="The Blue Shore by Mara Ellison, a year of cyanotypes" width="900" height="1350" /></a></li>
  <li><a href="/low-water"><img src="/media/books/low-water.webp" alt="Low Water by Ines Calder, notes from the tide line" width="900" height="1350" /></a></li>
  <li><a href="/salt-garden"><img src="/media/books/salt-garden.webp" alt="Salt Garden" width="900" height="1350" /></a></li>
</ul>
<p class="shelf-title" aria-live="polite">Low Water</p>
```

```ts title="script.ts"
// The title of the book in the middle, from the part of its alt text before " by".
export default function mount(example: HTMLElement): () => void {
  const shelf = example.querySelector('.shelf')
  const title = example.querySelector('.shelf-title')
  if (!shelf || !title) return () => {}
  const names = [...shelf.querySelectorAll('img')].map(
    (img) => img.alt.split(' by ')[0] ?? '',
  )
  const onIndex = (e: Event) => {
    title.textContent =
      names[(e as CustomEvent<{ index: number }>).detail.index] ?? ''
  }
  shelf.addEventListener('dio:index', onIndex)
  return () => shelf.removeEventListener('dio:index', onIndex)
}
```

### Name the cover in the middle

Nothing tells a screen reader that another cover reached the middle. Put a caption with `aria-live="polite"` next to the row and fill it from `dio:index`, whose `index` is the place of the cover among the images, from 0. The books above name their title this way.

```ts
const shelf = document.querySelector('.shelf')
const title = document.querySelector('.shelf-title')
const names = [...shelf.querySelectorAll('img')].map(
  (img) => img.alt.split(' by ')[0],
)

shelf.addEventListener('dio:index', (e) => {
  title.textContent = names[e.detail.index]
})
```

### Buttons for the next and the previous cover

Set `index` on the instance; the row glides there in `duration` per cover.

```ts
const row = document.querySelector('.work')

document.querySelector('.next').addEventListener('click', () => {
  const flow = app.effects.get(row, 'coverflow')
  flow?.set({ index: Math.round(flow.params.index) + 1 })
})
```

## Notes

Write the row as a list of links around images, styled as the row you want without WebGL: a strip that scrolls sideways. Each cover takes the size of its image in that strip and stands at its height, so books of different heights keep their line, and its picture is cut like the image, with the image’s own `object-fit` and `object-position`. The corners follow the `border-radius` of the first image. The covers to the sides and the reflection reach past the box of the list: leave room under it for the reflection, about half the height of a cover.

The copy hides the list, its background included, so put a background on a parent. A click on a drawn cover is caught by the effect, since the link under the pointer in the hidden list is another one: a click on a side cover brings it to the middle, a click on the middle one follows its link, with Ctrl or Cmd in a new tab. An image without a link fires `dio:open` instead, for a page that opens its own viewer.

The links stay in the page in order, each with its image’s alt text. Tab moves through them and the row follows the focused one; the arrow keys, PageUp, PageDown, Home and End move the row and the focus with it, and Enter opens the cover in the middle. Without links give the list `tabindex="0"` and an `aria-label` that says how to move on.

A drag with a mouse always carries the row. On touch screens set `touch-action: pan-y` on the list so the row follows the finger while the page still scrolls up and down; without it the browser scrolls the hidden list itself and the row follows that scroll. A wheel turned up and down always scrolls the page; only a sideways wheel or a two-finger swipe on a trackpad moves the row.

`style` and `items` rebuild the effect when they change. Set them in the markup rather than switching them while the row is on screen.

## Related

- [carousel](/docs/effects/carousel.md): Pictures on a 3D ring that turns with a drag, the keys, the page scroll or by itself, the front one enlarged.
- [flipbook](/docs/effects/flipbook.md): The page images of a container become a book whose pages bend as they turn.
- [gallery](/docs/effects/gallery.md): A container of images shows one at a time and changes them with a displacement, a slide, a crossfade or a zoom.
- [hover-preview](/docs/effects/hover-preview.md): Over a list of projects, the picture of the item under the pointer follows the pointer, stands beside the item or holds a fixed place.
- [lightbox](/docs/effects/lightbox.md): A click opens an image full screen: its copy flies out of the grid, pages and zooms.
- [polaroids](/docs/effects/polaroids.md): Photos scattered like prints on a table: drag one to the top, click it to hold it up large.
- [stack](/docs/effects/stack.md): The images of a container become a pile of cards; a swipe, a click or a key throws the top one off.
- [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.
- [Reduced motion](/docs/guides/reduced-motion.md)
