# video-text

The letters of a headline become a window onto a video or a photo, while the page shows around them.

The headline keeps its font, size and layout, but its letters show a muted video playing inside them, framed over the text as a whole, and the page’s own background shows around and between them. Once the first frame is ready the letters turn from their CSS color into the video in about half a second. A photo works the same way and stands still. The text stays in the DOM for screen readers, search and selection.

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

A travel headline in heavy capitals on a dark page, with a film of the aurora playing inside the letters.

```html title="index.html"
<div class="vt-hero">
  <p class="vt-kicker">Tromsø, 69° north · January to March</p>
  <h2 class="vt-headline" data-dio="video-text" data-dio-src="/media/previews/aurora.webm /media/previews/aurora.mp4">Northern lights</h2>
  <p class="vt-line">Six nights under the aurora oval, with a guide who knows where the sky clears.</p>
</div>
```

## Usage

Put it on: a heading or short text in a large, heavy font. Not img, video, canvas, svg or form fields.

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

```html
<h1 class="hero" data-dio="video-text" data-dio-src="/media/coast.webm /media/coast.mp4">Go further</h1>
```

In page transitions: The text fades with the page.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `src` (rebuilds) | `data-dio-src` | `string` | `''` | URL of a video, or several by spaces such as WebM then MP4; an image URL shows a photo. |
| `fit` (rebuilds) | `data-dio-fit` | `'cover' \| 'contain' \| 'fill'` | `'cover'` | cover fills every letter; contain shows all of it, some letters empty; fill stretches it. |
| `fps` | `data-dio-fps` | `number` 0 to 60 | `30` | Most frames drawn a second while the video plays; 0 holds the first frame. At most low 24. |
| `loop` | `data-dio-loop` | `boolean` | `true` | Plays the video again from the start; off holds its last frame. |
| `dim` | `data-dio-dim` | `number` 0 to 1 | `0` | How far the picture inside the letters goes dark, for contrast on a light page; 0 keeps it as is. |

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

### Variants

- `fit`: `cover`, `contain`, `fill`
  - `cover` fills every letter.
  - `contain` shows all of it, some letters empty.
  - `fill` stretches it.

## Inputs and frames

Draws at a steady rate while it is on screen and stops when it leaves. Only while the video plays and the text is on screen, at most fps a second, and for half a second as the picture fades in; none for a photo or a paused video. 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. The video stands on its first frame and does not play; the letters still show it.

Runs on every device. The same as with a mouse: the video plays while the text is on screen.

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

start({ effects: { 'video-text': { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 7.4 KB the first time a page uses it. GPU: One quad over the text: a texture read of the text and one of the video per pixel. Memory: A canvas of the text at twice its CSS size with mip levels, and one video frame. A video of 1280 px wide or less is enough for a headline.

- low: At most 24 frames a second, and the text is rastered at 1 device pixel per CSS pixel.

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

## Recipes

### A photo inside the letters

An image URL in `src` puts a still picture in the letters instead of a film, and then nothing is drawn after the first frame. A product page can set the name of a color in the color itself. On a light page a little `dim` keeps the letters from washing out.

The word Ochre in huge capitals filled with a photo of an ochre lounge chair, on a cream page.

```html title="index.html"
<div class="vt-shop">
  <h2 class="vt-word" data-dio="video-text" data-dio-src="/media/previews/lounge.webp" data-dio-dim="0.15">Ochre</h2>
  <p>The lounge chair in ochre velvet, on oak legs. Ships in two weeks.</p>
</div>
```

### The whole picture inside the text

`fit` frames the picture over the text as a block, like `object-fit` over an element. `cover` fills every letter and crops the picture to the text’s proportions, which suits one wide line. `contain` keeps the whole picture visible, so letters past its edges stay empty: use it for a short, tall block of text.

```html
<h1
  class="poster"
  data-dio="video-text"
  data-dio-src="/media/stage.webm /media/stage.mp4"
  data-dio-fit="contain"
>
  Blue
  <br />
  Signal
</h1>
```

## Notes

The heading stays real text in the DOM: screen readers read it, search engines index it, and find in page works. While the effect draws, a CSS filter hides the DOM text without removing it. Give the text a color that reads on its own, since that is what shows before the first frame, without WebGL and under reduced motion off.

Use a large, heavy font: thin strokes show little of the picture. The film plays only while the text is on screen and the tab is visible, at most `fps` frames a second, and pauses otherwise. Under reduced motion it holds its first frame.

The canvas cannot draw some text the way the browser does: `background-clip: text`, `text-shadow`, `-webkit-text-stroke` and vertical writing modes. An element with any of them keeps its text as it is, and a warning in development names the reason. A background or border on the heading itself is hidden with it while the effect draws, so put them on a wrapper.

A video from another origin needs CORS headers: the effect loads it in CORS mode. `src` and `fit` rebuild the effect when they change.

## 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.
- [Performance and quality](/docs/guides/performance.md)
- [Reduced motion](/docs/guides/reduced-motion.md)
