# tunnel

The phrases of a pinned section stand one behind another in real depth, and the scroll flies the camera through them.

The first phrase sits where the page has it, and the next ones wait further back in the dark, smaller and fainter. Scrolling moves the camera forward: the phrase in front grows in perspective, blurs and passes the viewer, and the next one comes out of the distance and slows down to be read. In a tunnel the phrases wait on the floor and ceiling of a corridor drawn with faint lines and swing to face the reader as they arrive, and while one holds in focus nothing behind it crosses it; zoom flies straight through them; in a spiral they wait along a turning helix. A fast scroll streaks the letters toward the edges. At the end of the section the last phrase stays as plain text.

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

A product launch story in five lines that wait one behind another down a dark corridor, flown through as you scroll.

```html title="index.html"
<div class="tn-scene">
  <div class="tn-story" data-dio="tunnel">
    <p>Every launch starts in the dark.</p>
    <p>A sketch on a napkin.</p>
    <p>A thousand late nights.</p>
    <p>One stubborn idea.</p>
    <p>Now it ships.</p>
  </div>
</div>
```

## Usage

Put it on: a block inside a pinned section, usually filling its sticky stage, whose children are the phrases, one element each, such as paragraphs. Not img, video, canvas, svg or form fields.

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

```html
<section style="height: 500vh"><div style="position: sticky; top: 0; height: 100vh"><div data-dio="tunnel" style="height: 100%"><p>Every launch starts in the dark.</p><p>A sketch on a napkin.</p><p>A thousand late nights.</p><p>Now it ships.</p></div></div></section>
```

- `:scope > *` inside: Each child element with text is one phrase, in reading order; the last one stays as text at the end.

In page transitions: The phrases fade with the copy as the page leaves or enters.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'tunnel' \| 'zoom' \| 'spiral'` | `'tunnel'` | tunnel lines the floor and ceiling of a corridor; zoom flies straight on; spiral turns along a helix. |
| `depth` | `data-dio-depth` | `number` 0.3 to 3 | `1` | Distance between two phrases, in heights of the element. |
| `perspective` | `data-dio-perspective` | `number` 0.3 to 3 | `1` | Strength of the perspective: higher puts the eye closer, so phrases grow faster. |
| `fade` | `data-dio-fade` | `number` 0 to 2 | `0.6` | Fog toward the distance; 0 shows every phrase behind at full strength. |
| `blur` | `data-dio-blur` | `number` 0 to 40 px | `10` | Blur of a phrase as it passes the viewer, at its nearest; 0 keeps it sharp. At most low 0. |
| `streak` | `data-dio-streak` | `number` 0 to 3 | `1` | Motion streaks with the scroll speed, toward the edges; 0 turns them off. At most low 0. |
| `twist` | `data-dio-twist` | `number` -180 to 180° | `50` | Turn of the helix from one phrase to the next, with style spiral. |
| `lines` | `data-dio-lines` | `boolean` | `true` | Draws the guide lines of the tunnel or the helix; zoom has none. |
| `color` | `data-dio-color` | `color` | `'currentColor'` | Color of the phrases and lines; currentColor keeps the text color. |

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

### Variants

- `style`: `tunnel`, `zoom`, `spiral`
  - `tunnel` lines the floor and ceiling of a corridor.
  - `zoom` flies straight on.
  - `spiral` turns along a helix.

## Inputs and frames

Reads [`height`](/docs/api/inputs#input-height), [`size`](/docs/api/inputs#size), [`velocity`](/docs/api/inputs#velocity), [`width`](/docs/api/inputs#input-width), [`x`](/docs/api/inputs#input-x), [`y`](/docs/api/inputs#input-y). Draws only when you scroll, move the pointer over it or change a param. Only while the page scrolls, and a few more while the scroll speed settles with streaks on; none at rest. 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. The effect does not run: the phrases stay as the page lays them out, one under another.

Runs on every device. The same as with a mouse: the scroll drives it.

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

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

## Cost and quality

Loads 10.8 KB the first time a page uses it. GPU: One instanced draw of up to seven phrase cards with a 12-tap blur, and one draw of a few hundred thin line quads. Memory: One atlas of every phrase at twice its CSS size, at most 4096 px a side.

- low: The phrases are rastered at 1 device pixel per CSS pixel instead of 2, without blur or streaks.

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

## Recipes

### A launch story told on a pinned section

Give the section one screen of height per phrase: five phrases take `500vh`, so the stage stays pinned for four screens of scroll and each phrase gets a screen to arrive, hold and pass. Put the effect on a block that fills the sticky stage, with the phrases as its children, and keep the background on the stage: while it draws, the effect’s own element is hidden, background included.

```html
<section class="launch" data-dio-section style="height: 500vh">
  <div class="stage" style="position: sticky; top: 0; height: 100vh; background: #08090d">
    <div class="story" data-dio="tunnel" style="height: 100%">
      <p>Every launch starts in the dark.</p>
      <p>A sketch on a napkin.</p>
      <p>A thousand late nights.</p>
      <p>One stubborn idea.</p>
      <p>Now it ships.</p>
    </div>
  </div>
</section>
```

Lay the phrases out as a plain list, one under another, centered in the stage. That is what visitors see under reduced motion and without WebGL, and what screen readers and search read in every case. With the effect running they stack in one spot: the camera slows down on each phrase so it can be read, and at the end of the section the last one stays as plain text.

### A calm zoom for a hotel or wellness intro

`style="zoom"` drops the corridor and flies straight through the phrases, each centered behind the one before. A light, thin typeface and a bigger `blur` make the passing line melt away before the next one settles.

Four short lines on warm paper, Slow down, Breathe in, Look closer, Stay a while, flown through one after another as you scroll.

```html title="index.html"
<div class="tz-scene">
  <div class="tz-story" data-dio="tunnel" data-dio-style="zoom" data-dio-blur="16">
    <p>Slow down.</p>
    <p>Breathe in.</p>
    <p>Look closer.</p>
    <p>Stay a while.</p>
  </div>
</div>
```

### Event details on a spiral

`style="spiral"` strings the phrases along a helix that turns as the camera travels, so each line swings round the axis and rights itself as it arrives. `twist` sets how far the helix turns from one phrase to the next. Short phrases, such as a date, a time and a place, read best this way.

```html
<div class="details" data-dio="tunnel" data-dio-style="spiral" data-dio-twist="70">
  <p>Friday</p>
  <p>23:00</p>
  <p>Warehouse 9</p>
  <p>Bring friends</p>
</div>
```

## Notes

The phrases are flat cards in real depth, seen through a camera an element’s height in front of them, divided by `perspective`. They take their colors from the text, one color per phrase if the page sets them, and the guide lines take the element’s color; `color` paints both. Under reduced motion the effect does not run at all.

A fast scroll streaks the letters away from the middle, the way the camera’s speed would blur them. The streaks fade with the scroll speed, and the page draws no frames at rest.

Without a pinned section around it, the scroll through the viewport drives the camera: it starts as the element’s middle passes 85% of the viewport height and reaches the last phrase at 25%.

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

`style` rebuilds the effect; `depth`, `perspective`, `fade`, `blur`, `streak`, `twist`, `lines` and `color` 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.
- [How a layer follows scroll](/docs/concepts/scroll.md)
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
