keycaps
Each letter sits on a 3D mechanical keycap that presses down as if the word were typed, with a click under the pointer.
Kestrel 75 · gasket-mount mechanical keyboard
Type less
Lubed linear switches, double-shot PBT caps and a milled aluminum case. Ships in March, preorders open today.
Live preview needs WebGPU or WebGL 2. A keyboard maker's launch hero with the words Type less on white 3D keycaps that press down one after another as if typed.
This example failed to draw.
Usage
Put it on a short heading, a word mark or a button label. Not img, video, canvas, svg or form fields. start() registers keycaps, 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 keycaps<section class="kc-launch"> <p class="kc-kicker">Kestrel 75 · gasket-mount mechanical keyboard</p> <h2 class="kc-title" data-dio="keycaps">Type less</h2> <p class="kc-line">Lubed linear switches, double-shot PBT caps and a milled aluminum case. Ships in March, preorders open today.</p></section>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.kc-title', 'keycaps', { style: 'keycap', trigger: 'view', depth: 0.22, gap: 0.07, radius: 0.16, speed: 9,})import { start } from '@dioramajs/diorama'import keycaps from '@dioramajs/text/keycaps'
const app = start()app.attach('.kc-title', keycaps({ style: 'keycap', trigger: 'view', depth: 0.22, gap: 0.07, radius: 0.16, speed: 9,}))Page transitions
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 |
|---|---|---|---|
stylerebuildsdata-dio-style | 'keycap' | 'retro' | 'glow' | 'keycap' |
|
triggerrebuildsdata-dio-trigger | 'view' | 'hover' | 'loop' | 'view' |
|
depthdata-dio-depth | number0 to 0.5 | 0.22 | Key travel as a share of the key size; 0 keeps the keys still. |
gapdata-dio-gap | number0 to 0.3 | 0.07 | Space between neighboring keys as a share of the key size. |
radiusdata-dio-radius | number0 to 0.5 | 0.16 | Rounding of the cap corners as a share of the key; 0 is square, 0.5 round. |
colorrebuildsdata-dio-color | string | '' | Cap color; empty picks the style color that contrasts with the text. |
accentrebuildsdata-dio-accent | string | '' | Color of the first key; empty uses the style accent, none matches the others. |
speeddata-dio-speed | number2 to 20 | 9 | Typing speed in keys per second. |
sounddata-dio-sound | boolean | false | Plays a short synthesized click per key press, only after the visitor touched the page. |
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 only while something on it moves, then stops. Only while keys move or flash; none at rest, when the keyboard stays drawn. 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 keys are drawn but never press, flash or type.
Runs on every device. A tap presses the key under the finger hard; the hover trigger types the word on a tap.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { keycaps: { reducedMotion: 'off' } } })Cost and quality
- Download
- 14.6 KB min+gzip the first time a page uses it
- GPU
- Three instanced draws: a shell of about 600 triangles per cap, switch and the case, and a soft quad per key for its shadow and its light.
- Memory
- An atlas of the letters at twice their CSS size, kept while the element lives.
- Frames
- Idle at rest
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 legends are rastered at 1 device pixel per CSS pixel instead of 2. |
Recipes
RGB caps for a game launch
style="glow" draws dark translucent caps with an RGB light under each one. The light bleeds round the edges of the caps onto the plate and shines through the legends, and its colors run along the row like a rainbow wave while keys are typed or the pointer moves over them. trigger="loop" types the word again every few seconds while it is on screen, so a teaser keeps its rhythm.
<h2 data-dio="keycaps" data-dio-style="glow" data-dio-trigger="loop">Press start</h2>Retro caps on a card
style="retro" draws thick beige caps of the 1980s, each yellowed a little differently, with a gray accent key. When the element has a background of its own, the keyboard case takes its place and its color, with its rounded corners; otherwise the case fits round the keys.
<p class="mark" style="background: #3a362f; padding: 0.3em 0.5em; border-radius: 16px" data-dio="keycaps" data-dio-style="retro">1984</p>A shortcut hint that clicks
trigger="hover" types the word only when the pointer comes over the keys, and sound="true" plays a short synthesized click for each press. The sound only starts after the visitor has clicked, tapped or typed on the page, as browsers require, and it is off by default.
<p data-dio="keycaps" data-dio-trigger="hover" data-dio-sound="true">Ctrl K</p>Notes
Each key is a real 3D cap in the page’s perspective: rounded, tapered toward a slightly dished top, lit as plastic with a soft sheen, on a switch, with a soft shadow on a darker plate. The keyboard leans back a little, so the fronts of the caps show and a press reads as travel. The letter is printed on the top in the text color, and a space between words becomes a wide space bar. With style="keycap" the caps contrast with the text: dark text sits on white caps and light text on graphite ones, the first letter on an accent key.
Keys are about 1.35 em wide, wider than the letters, so give the element room: the case grows past a narrow box, aligned the way the text is, and the keys shrink when the container is too narrow. Keep it to a short word or phrase; over 240 letters the text stays as it is.
The element keeps its text as you wrote it, so screen readers, search and find in page see the real words, while the keyboard stays drawn in their place. Frames run only while keys move or flash; at rest the keyboard draws nothing new. Under reduced motion the keys are drawn but never press. Text with text-shadow, -webkit-text-stroke, background-clip: text or a vertical writing mode stays as it is, with a warning in development. style, trigger, color and accent rebuild the effect when they change.