Diorama

Made for a big screen.

This site is one 3D scene, built for a computer: a wide window and a mouse. Open dioramajs.dev on a desktop or a laptop to see it.This site is one 3D scene, built for a wide window. Make this one wider, or open dioramajs.dev on a larger screen.

GitHub

DioramaDocs

stars

Backgrounds

@dioramajs/backgrounds/starsSource

A night sky of twinkling stars in depth layers that lean with the pointer and the scroll, with shooting stars and a warp flight.

View as Markdown
On this page

Public night, Tien Shan Observatory

Perseids under a dark sky

Date
Aug 12
Gates
21:30
Peak
60 per hour

Telescopes on the terrace, a guided tour of the summer sky and hot tea until the last meteor.

Live preview needs WebGPU or WebGL 2. An event card for a public night at an observatory, with a twinkling starfield and the odd shooting star beside the date and times.

This example failed to draw.

Move the pointer over the sky to see the nearer stars lean, or switch motion to warp.
Params of stars
motion

drift twinkles and drifts slowly.

Usage

Put it on an empty decorative element behind the content, such as an aria-hidden div filling a hero; its CSS background is the look without WebGL. start() registers stars, 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.

Terminal window
npx @dioramajs/diorama add stars
<article class="st-event">
<div
class="st-sky"
aria-hidden="true"
data-dio="stars"
data-dio-background="#050816"
data-dio-shooting="8"
></div>
<div class="st-info">
<p class="st-kicker">Public night, Tien Shan Observatory</p>
<h2>Perseids under a dark sky</h2>
<dl>
<div><dt>Date</dt><dd>Aug 12</dd></div>
<div><dt>Gates</dt><dd>21:30</dd></div>
<div><dt>Peak</dt><dd>60 per hour</dd></div>
</dl>
<p>Telescopes on the terrace, a guided tour of the summer sky and hot tea until the last meteor.</p>
</div>
</article>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('.st-sky', 'stars', {
motion: 'drift',
count: 800,
depth: 0.5,
twinkle: 0.5,
speed: 0.3,
warp: 0.5,
scale: 1,
color: '#ffffff',
background: '#050816',
shooting: 8,
fps: 30,
})
import { start } from '@dioramajs/diorama'
import stars from '@dioramajs/backgrounds/stars'
const app = start()
app.attach('.st-sky', stars({
motion: 'drift',
count: 800,
depth: 0.5,
twinkle: 0.5,
speed: 0.3,
warp: 0.5,
scale: 1,
color: '#ffffff',
background: '#050816',
shooting: 8,
fps: 30,
}))

Page transitions

Fades with the page transition. How a flight flies.

Params

Every param is also an attribute, and both app.attach() and the handle take it by name.

ParamTypeDefaultDescription
motionrebuildsdata-dio-motion'drift' | 'warp' | 'still''drift'
  • drift twinkles and drifts slowly.
  • warp flies through the stars.
  • still keeps them in place.
countdata-dio-countnumber0 to 4000800Stars over the whole element.At most low 300, medium 550 by quality tier.
depthdata-dio-depthnumber0 to 10.5How far the stars lean with the pointer and the scroll; 0 turns parallax off.
twinkledata-dio-twinklenumber0 to 10.5How deeply the stars twinkle; 0 keeps their brightness steady.
speeddata-dio-speednumber0 to 20.3How fast the stars drift, or how fast the warp flies.
warpdata-dio-warpnumber0 to 10.5How far a fast scroll stretches the stars into streaks; 0 never stretches.
scaledata-dio-scalenumber0.3 to 41Size of the stars over a base of about 1 px.
colordata-dio-colorcolor'#ffffff'Color of the stars, any CSS color; each star tints it slightly.
backgrounddata-dio-backgroundcolor'#04060f'Color of the sky behind the stars, any CSS color.
shootingdata-dio-shootingnumber0 to 304Shooting stars a minute on average; 0 turns them off.
fpsdata-dio-fpsnumber0 to 6030Most frames a second the motion draws; 0 holds the stars still.At most low 24 by quality tier.
seedrebuildsdata-dio-seednumber1 to 1001Positions, sizes and colors of the stars.

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, hover, pointer, presence, progress, reducedMotion, size and velocity . Draws at a steady rate while it is on screen and stops when it leaves. Up to fps frames a second while on screen, none off screen, at fps 0 or with the still motion; pointer and scroll draw at full rate. 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. A still sky: no twinkle, drift, warp, shooting stars, parallax or streaks, and no frames.

Runs on every device. The stars lean with a finger while it is on the screen; scroll parallax and streaks work as usual.

To change what reduced motion does, patch it in start().

import { start } from '@dioramajs/diorama'
start({ effects: { stars: { reducedMotion: 'off' } } })

Cost and quality

Download
2.9 KB min+gzip the first time a page uses it
GPU
One pass for the sky and one instanced draw of small quads for all the stars.
Memory
Two instance buffers of 4003 entries, about 128 KB.
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.

TierWhat changes
lowAt most 300 stars and 24 frames a second.
mediumAt most 550 stars.

Recipes

A night sky behind a hero

Put the stars on an empty aria-hidden element that fills the hero and start the app with canvas: 'behind', so the heading sits over the sky. The element’s CSS background is what visitors see before the engine draws and without WebGL, so give it the same color as background and a few fixed dots.

<header class="hero">
<div
class="hero-sky"
aria-hidden="true"
data-dio="stars"
data-dio-background="#04060f"
></div>
<h1>Orbit, launch week</h1>
<a class="cta" href="/keynote">Watch the keynote</a>
</header>
.hero {
position: relative;
isolation: isolate;
min-height: 80vh;
}
.hero-sky {
position: absolute;
inset: 0;
z-index: -1;
background: radial-gradient(1px 1px at 30% 40%, #fff 50%, transparent), #04060f;
}
src/main.ts
import { start } from '@dioramajs/diorama'
export const app = start({ canvas: 'behind' })

A warp for a countdown or a launch

motion="warp" flies through the field: stars rush out of the center and stretch into trails. More stars and a higher speed make it feel faster; a fast scroll speeds it up further, by warp.

Live preview needs WebGPU or WebGL 2. A dark strip where stars rush out from the center and stretch into trails.

This example failed to draw.

Params of stars
<div
class="st-warp"
aria-hidden="true"
data-dio="stars"
data-dio-motion="warp"
data-dio-count="1400"
data-dio-speed="0.7"
data-dio-background="#03040b"
data-dio-color="#dce6ff"
></div>

A calm sky for a long page

For a background that sits behind reading, turn the shooting stars off, lower twinkle and keep depth small. With motion="still" the stars draw no frames at all and only lean as the page scrolls.

<div
class="night"
aria-hidden="true"
data-dio="stars"
data-dio-motion="still"
data-dio-shooting="0"
data-dio-depth="0.3"
data-dio-background="#0b1026"
></div>

Notes

The docs canvas sits above the page, so the examples here keep text beside the stars instead of over them. Put text over the sky with canvas: 'behind', as in the first recipe.

Every star is one quad of a single instanced draw, so count changes live without a rebuild. Nearer stars are larger, brighter, drift faster and lean further with the pointer, and they move less with the scroll than far ones, which gives the sky its depth.

It draws up to fps frames a second while it is on screen and none off screen; the pointer and scroll draw at full rate while they move the stars. On the low tier it draws at most 300 stars at 24 frames a second.

motion and seed rebuild the effect when they change. The colors, count, speeds and rates change live.