prism
Letters become crystal: their edges split a light into a crisp spectrum that slides around them as the light moves.
Atelier Lumen · hand-cut lead-free crystal
Lumen
Each piece is cut, polished and signed in our Bohemian workshop, and throws a little sunlight across your table.
Live preview needs WebGPU or WebGL 2. A crystal workshop's page on warm white paper whose name is glass in its own color, its edges splitting the light into crisp spectra that follow the pointer.
This example failed to draw.
Usage
Put it on a large heading or short display line in a bold font, on a plain background. Not img, video, canvas, svg or form fields. start() registers prism, 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 prism<div class="pr-hero"> <p class="pr-kicker">Atelier Lumen · hand-cut lead-free crystal</p> <h2 class="pr-headline" data-dio="prism">Lumen</h2> <p class="pr-line">Each piece is cut, polished and signed in our Bohemian workshop, and throws a little sunlight across your table.</p></div>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.pr-headline', 'prism', { style: 'prism', trigger: 'pointer', dispersion: 1, spread: 1, strength: 1, bevel: 0.07, light: 300, follow: 1,})import { start } from '@dioramajs/diorama'import prism from '@dioramajs/text/prism'
const app = start()app.attach('.pr-headline', prism({ style: 'prism', trigger: 'pointer', dispersion: 1, spread: 1, strength: 1, bevel: 0.07, light: 300, follow: 1,}))Page transitions
The glass and its light fade 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 |
|---|---|---|---|
stylerebuildsdata-dio-style | 'prism' | 'crystal' | 'diamond' | 'prism' |
|
triggerrebuildsdata-dio-trigger | 'pointer' | 'scroll' | 'loop' | 'view' | 'pointer' |
|
dispersiondata-dio-dispersion | number0 to 2 | 1 | How far the colors pull apart; 0 keeps the light white, with no rainbow. |
spreaddata-dio-spread | number0 to 2 | 1 | How far past the letters the thrown spectrum lands; 0 turns it off. |
strengthdata-dio-strength | number0 to 2 | 1 | Brightness of the rainbows, the highlights and the sparkle. |
beveldata-dio-bevel | number0.01 to 0.15 em | 0.07 | Width of the bevel along every edge in ems; larger is rounder glass. |
lightdata-dio-light | number0 to 360° | 300 | Where the light comes from at rest as a CSS angle, 0 above and 90 right. |
followdata-dio-follow | number0 to 1 | 1 | How far the light moves toward the pointer or with the scroll; 0 holds it. |
fpsdata-dio-fps | number0 to 60 | 30 | Frame rate of the loop; 0 holds the light still.At most low 24 by quality tier. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads delta, height, reducedMotion, size, width, x and y
. Draws at a steady rate while it is on screen and stops when it leaves. None at rest: the glass and its spectra stay drawn. Full rate while the light settles after the pointer moves, one frame per scroll with trigger scroll, `fps` frames a second on a loop while in view, and a short sweep on view. 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 light stays at its angle and the spectra hold still.
Runs on every device. A finger does not move the light, so a tap or a swipe never flickers it; the scroll and loop triggers work as on desktop.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { prism: { reducedMotion: 'off' } } })Cost and quality
- Download
- 10.9 KB min+gzip the first time a page uses it
- GPU
- One quad over the text and a margin around it; about 25 texture reads per pixel, three per color of the split light.
- Memory
- A half-float relief of the text built once on the CPU from a distance field, and a canvas of the text.
- 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 | The glass is built at 1 px per CSS px instead of 2, the light is split into 5 colors instead of 7, and the loop runs at most 24 frames a second. |
Recipes
Cut crystal on a dark page
style="crystal" cuts each stroke into flat facets that meet in a sharp ridge, the way a chiselled glass letter is cut, with small glints that flash as the light moves. Each facet that faces the light at the right angle lights up in one color of the spectrum, the way a cut stone flashes. On a dark page the spectra glow.
<h2 data-dio="prism" data-dio-style="crystal" style="color: #c9d4e6">Brilliant cut</h2>A diamond line whose light orbits
style="diamond" cuts finer facets, pulls the colors apart the most and sparkles with fire as the light passes. With trigger="loop" the light orbits slowly at fps frames a second while the text is on screen, and stops when it scrolls away.
<p class="display" data-dio="prism" data-dio-style="diamond" data-dio-trigger="loop">Forever</p>Notes
The glass is built from the shape of the letters: a distance field of the text gives every pixel its distance to the nearest edge, and a rounded profile over bevel turns that into the slope of the glass, so the edges catch the light the way a real bevel would. The face of each letter shows its ink as it is, crisp. Across a bevel the glass bends the view outward by its slope, and the light is split into seven colors, each bent a little more than the last: the edge of the ink comes apart into a thin spectrum inside the letter, and where a bevel faces away from the lamp at the right angle, the light leaves it in a saturated line of color that runs along the contour. Both gather near the lamp and slide around the curves as it moves, never as a blurred copy of the word. A fainter spectrum lands on the page right past the far edges; spread is how far, and 0 turns it off. It stays within a few hundredths of an em of the letters, so the element needs no extra room around it.
The light sits about the size of the element away, at light when nothing moves it. With trigger="pointer" it follows a mouse or a pen anywhere on the page and drifts as the element scrolls; after it settles nothing more is drawn. Set follow to 0 for a fixed light. Under reduced motion the light stays at its angle.
The heading stays real text in the DOM: screen readers read it, search engines index it, and find in page works. While the glass is drawn the element is hidden without being removed, so give the text a color that reads on its own, since that is what shows without WebGL. Use a bold, large font on a plain background: thin strokes have little glass to bend the light. The canvas cannot draw background-clip: text, -webkit-text-stroke or vertical writing modes the way the browser does, and an element with any of them keeps its text as it is, with a warning in development.
style and trigger rebuild the effect when they change. bevel builds the glass again a moment after it changes; the other params change live.