video-text
The letters of a headline become a window onto a video or a photo, while the page shows around them.
Tromsø, 69° north · January to March
Northern lights
Six nights under the aurora oval, with a guide who knows where the sky clears.
Live preview needs WebGPU or WebGL 2. A travel headline in heavy capitals on a dark page, with a film of the aurora playing inside the letters.
This example failed to draw.
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. The tabs below are the example above, with the values you set.
To change its code, copy the source into your effects folder; the copy replaces the built-in.
npx @dioramajs/diorama add video-text<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>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.vt-headline', 'video-text', { fit: 'cover', fps: 30, dim: 0,})import { start } from '@dioramajs/diorama'import videoText from '@dioramajs/text/video-text'
const app = start()app.attach('.vt-headline', videoText({ fit: 'cover', fps: 30, dim: 0,}))Page transitions
The text fades with the page. How a flight flies.
Params
Every param is also an attribute, and both app.attach() and the handle take it by name.
| Param | Type | Default | Description |
|---|---|---|---|
srcrebuildsdata-dio-src | string | '' | URL of a video, or several by spaces such as WebM then MP4; an image URL shows a photo. |
fitrebuildsdata-dio-fit | 'cover' | 'contain' | 'fill' | 'cover' |
|
fpsdata-dio-fps | number0 to 60 | 30 | Most frames drawn a second while the video plays; 0 holds the first frame.At most low 24 by quality tier. |
loopdata-dio-loop | boolean | true | Plays the video again from the start; off holds its last frame. |
dimdata-dio-dim | number0 to 1 | 0 | How far the picture inside the letters goes dark, for contrast on a light page; 0 keeps it as is. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
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. How a frame is decided.
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.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { 'video-text': { reducedMotion: 'off' } } })Cost and quality
- Download
- 7.4 KB min+gzip 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.
- Frames
- Draws while on screen
It runs from the low tier up. Each tier changes this: The quality plugin picks the tier for each device.
| Tier | What changes |
|---|---|
low | At most 24 frames a second, and the text is rastered at 1 device pixel per CSS pixel. |
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.
Ochre
The lounge chair in ochre velvet, on oak legs. Ships in two weeks.
Live preview needs WebGPU or WebGL 2. The word Ochre in huge capitals filled with a photo of an ochre lounge chair, on a cream page.
This example failed to draw.
<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.
<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.