neon
A heading becomes a 3D neon sign: round glass tubes that stutter on, light the wall and cast a shadow.
Live preview needs WebGPU or WebGL 2. A basement bar's headline as a pink neon sign on a dark wall, its tubes stuttering on letter by letter and lighting the wall behind them.
This example failed to draw.
Usage
Put it on a heading, a label or a short line of text, best on a dark background. Not img, video, canvas, svg or form fields. start() registers neon, 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 neon<section class="nn-bar"> <p class="nn-kicker">Basement · 41 Canal Street</p> <h2 class="nn-sign" data-dio="neon">Open late</h2> <p class="nn-line">Natural wine, small plates and a record on the turntable until 2 a.m., Thursday to Sunday.</p></section>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.nn-sign', 'neon', { style: 'tube', trigger: 'view', glow: 0.5, strength: 1, depth: 0.12, shadow: 0.6, tilt: 5, flicker: 0.3, broken: 0,})import { start } from '@dioramajs/diorama'import neon from '@dioramajs/text/neon'
const app = start()app.attach('.nn-sign', neon({ style: 'tube', trigger: 'view', glow: 0.5, strength: 1, depth: 0.12, shadow: 0.6, tilt: 5, flicker: 0.3, broken: 0,}))Page transitions
Leaving a page the tubes go dark from the last letter back and the glass fades; entering they light up in reading order. 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 | 'tube' | 'outline' | 'tube' |
|
triggerrebuildsdata-dio-trigger | 'view' | 'hover' | 'always' | 'view' |
|
colorrebuildsdata-dio-color | string | '' | Color of the gas and its light, any CSS color; empty uses the text color. |
glowdata-dio-glow | number0 to 1.5 em | 0.5 | Radius of the light thrown on the wall, in ems of the font size; 0 for none. |
strengthdata-dio-strength | number0 to 3 | 1 | Brightness of the light on the wall and around the tubes; 0 leaves bare tubes. |
depthdata-dio-depth | number0 to 0.4 em | 0.12 | How far the tubes stand off the wall, in ems; 0 mounts them flat on it. |
shadowdata-dio-shadow | number0 to 1 | 0.6 | Darkness of the shadow the tubes cast on the wall; 0 for none. |
tiltdata-dio-tilt | number0 to 12° | 5 | Largest turn of the sign toward a mouse near it, degrees; 0 keeps it still. |
flickerdata-dio-flicker | number0 to 1 | 0.3 | How often and how deep the lit sign buzzes and dims; 0 holds it steady. |
brokendata-dio-broken | number0 to 1 | 0 | Share of the letters that keep stuttering and going dark; 0 for none. |
seeddata-dio-seed | number1 to 100 | 1 | Picks the flicker, the broken letters and the turn-on; the same seed repeats. |
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, presence, reducedMotion, size, width, x and y
. Draws at a steady rate while it is on screen and stops when it leaves. Full rate while tubes switch on or off and while the lamp or the sign follows the mouse, 30 a second while a tube flickers, none in between: a timer wakes the sign for its next flicker. With flicker and broken at 0 it draws nothing once lit, except on scroll. 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 sign is lit from the start: no turn-on, no flicker, no broken letters, the shadow does not follow the scroll or the mouse, and it draws no frames of its own.
Runs on every device. No mouse lamp and no turn: the light stays above the screen. With trigger hover a tap switches the sign on and off.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { neon: { reducedMotion: 'off' } } })Cost and quality
- Download
- 12.7 KB min+gzip the first time a page uses it
- GPU
- Three instanced draws of a quad per letter (wall light and shadow, bloom, tubes), at most three texture reads per pixel; nothing is blurred on the GPU.
- Memory
- A canvas of the text at twice its CSS size, a distance field of it with its direction, and a small atlas of each letter’s light blurred once on the CPU.
- 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 text is rastered at 1 device pixel per CSS pixel instead of 2, and the distance field at half that. |
Recipes
A menu that lights up on hover
With trigger="hover" each link stays dark glass until the pointer or keyboard focus reaches it, then its tubes catch in a quick stutter; on a phone a tap switches it on. Each link takes its color from its CSS color. flicker="0" keeps a lit link steady, so it draws nothing while the pointer rests on it.
Live preview needs WebGPU or WebGL 2. A club menu of three uppercase links in red, cyan and lime that light up as neon when hovered.
This example failed to draw.
<nav class="nn-menu" aria-label="Club"> <a class="nn-program" href="/program" data-dio="neon" data-dio-trigger="hover" data-dio-flicker="0" >Program</a> <a class="nn-tickets" href="/tickets" data-dio="neon" data-dio-trigger="hover" data-dio-flicker="0" >Tickets</a> <a class="nn-drinks" href="/bar" data-dio="neon" data-dio-trigger="hover" data-dio-flicker="0" >Bar</a></nav>A motel sign with a dying letter
style="outline" bends one thin tube along the edge of each letter, the classic roadside look. broken picks a share of the letters, at least one, that keep stuttering and go dark for a while now and then; seed decides which. Between two stutters the sign draws nothing.
Vacancy
Route 66 · Rooms from $59
Live preview needs WebGPU or WebGL 2. A red outline neon sign that reads Vacancy over a dark wall, one of its letters flickering out now and then.
This example failed to draw.
<div class="nn-motel"> <h2 data-dio="neon" data-dio-style="outline" data-dio-trigger="always" data-dio-broken="0.12" data-dio-seed="4" >Vacancy</h2> <p>Route 66 · Rooms from $59</p></div>On a light wall
In daylight the light of the tubes on the wall gives way to their shadow. The sign reads the background color behind it: on a light wall the glow is softer and the shadow plainer, so a sign on a pale shop front still stands off the wall. A larger depth moves the tubes and their shadow further apart.
<h2 data-dio="neon" data-dio-trigger="always" data-dio-flicker="0" data-dio-depth="0.2" data-dio-shadow="0.8">Fresh flowers</h2>Notes
The sign is built in 3D, not painted: the tubes have a round section whose normals come from a distance field of the letters, so a highlight runs along each tube and slides as the light moves. They hang depth ems in front of the page, the light they throw lands on the wall behind them, and so does their shadow. The lamp for that shadow sits above the screen, so the shadow shifts as the page scrolls; a mouse near the sign takes its place, the shadow swings away from it and the sign turns a little toward it. The light on the wall is blurred once on the CPU when the sign is built, never per frame.
The heading stays real text in the DOM: screen readers read it, search engines index it, and find in page works. While the sign is drawn the DOM text is hidden without being removed. Neon reads best in a medium or bold font and a saturated color on a dark background.
Frames run while tubes switch on or off and while the lamp or the sign follows the mouse; a flickering tube asks for 30 a second. Between flickers a timer wakes the sign, and with flicker and broken at 0 a lit sign draws nothing. Under reduced motion the sign is lit from the start, with no turn-on, no flicker and no broken letters, and the shadow and the sign stay still.
The canvas cannot draw some text the way the browser does: background-clip: text, -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 text-shadow is fine: the sign draws its own light. style, trigger and color rebuild the effect when they change; a new glow blurs the light again after a short pause, and the other params change live.