# stars

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

Hundreds of small stars over a flat sky color, most faint and a few bright, some warmer and some cooler. They twinkle, drift slowly sideways, nearer ones faster, and lean with the pointer and the scroll, so the sky has depth. A fast scroll stretches them into vertical streaks, and now and then a shooting star crosses the upper sky and fades. The warp motion flies through the field instead: stars rush out from the center and stretch into trails.

Module `backgrounds`, category Backgrounds. Import `@dioramajs/backgrounds/stars`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/backgrounds/src/stars.ts)

An event card for a public night at an observatory, with a twinkling starfield and the odd shooting star beside the date and times.

```html title="index.html"
<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>
```

## 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.

```html
<header class="hero"><div class="hero-sky" aria-hidden="true" data-dio="stars"></div><h1>Launch night</h1></header>
```

In page transitions: Fades with the page transition.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `motion` (rebuilds) | `data-dio-motion` | `'drift' \| 'warp' \| 'still'` | `'drift'` | drift twinkles and drifts slowly; warp flies through the stars; still keeps them in place. |
| `count` | `data-dio-count` | `number` 0 to 4000 | `800` | Stars over the whole element. At most low 300, medium 550. |
| `depth` | `data-dio-depth` | `number` 0 to 1 | `0.5` | How far the stars lean with the pointer and the scroll; 0 turns parallax off. |
| `twinkle` | `data-dio-twinkle` | `number` 0 to 1 | `0.5` | How deeply the stars twinkle; 0 keeps their brightness steady. |
| `speed` | `data-dio-speed` | `number` 0 to 2 | `0.3` | How fast the stars drift, or how fast the warp flies. |
| `warp` | `data-dio-warp` | `number` 0 to 1 | `0.5` | How far a fast scroll stretches the stars into streaks; 0 never stretches. |
| `scale` | `data-dio-scale` | `number` 0.3 to 4 | `1` | Size of the stars over a base of about 1 px. |
| `color` | `data-dio-color` | `color` | `'#ffffff'` | Color of the stars, any CSS color; each star tints it slightly. |
| `background` | `data-dio-background` | `color` | `'#04060f'` | Color of the sky behind the stars, any CSS color. |
| `shooting` | `data-dio-shooting` | `number` 0 to 30 | `4` | Shooting stars a minute on average; 0 turns them off. |
| `fps` | `data-dio-fps` | `number` 0 to 60 | `30` | Most frames a second the motion draws; 0 holds the stars still. At most low 24. |
| `seed` (rebuilds) | `data-dio-seed` | `number` 1 to 100 | `1` | Positions, sizes and colors of the stars. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `motion`: `drift`, `warp`, `still`
  - `drift` twinkles and drifts slowly.
  - `warp` flies through the stars.
  - `still` keeps them in place.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`hover`](/docs/api/inputs#hover), [`pointer`](/docs/api/inputs#pointer), [`presence`](/docs/api/inputs#presence), [`progress`](/docs/api/inputs#progress), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size), [`velocity`](/docs/api/inputs#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. See [how a frame is decided](/docs/concepts/frames).

## 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.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { stars: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 2.9 KB 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.

- low: At most 300 stars and 24 frames a second.
- medium: At most 550 stars.

See [the quality plugin](/docs/effects/quality).

## 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.

```html
<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>
```

```css
.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;
}
```

```ts title="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`.

A dark strip where stars rush out from the center and stretch into trails.

```html title="index.html"
<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.

```html
<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.

## Related

- [blobs](/docs/effects/blobs.md): Soft blobs of brand color drifting behind a hero: merging like a lava lamp, as blurred orbs or as glossy glass drops.
- [clouds](/docs/effects/clouds.md): A sky of soft layered clouds for the time of day, daylight, a sunset or a moonlit night, drifting in the wind and parting on scroll.
- [dots](/docs/effects/dots.md): A dot grid behind a hero or in a card that answers the pointer: dots near it grow, move aside or glow, then settle back.
- [gradient](/docs/effects/gradient.md): A living background of four soft color blobs or of aurora curtains, mixed in OKLab, without banding, with a light film grain.
- [grid](/docs/effects/grid.md): A grid floor in perspective running toward the viewer under a glowing horizon: neon synthwave, a blueprint or plain thin lines.
- [waves](/docs/effects/waves.md): Topographic contour lines or flowing wave lines across a hero or footer, slowly changing and shifted by scroll.
- [drops](/docs/effects/drops.md): Water drops on glass over a hero or the whole page: rain that beads and runs, or meltwater left by a thaw.
- [snow](/docs/effects/snow.md): Snow, cherry petals or autumn leaves drifting down over a section or the whole page, for a seasonal campaign.
- [Performance and quality](/docs/guides/performance.md)
- [Reduced motion](/docs/guides/reduced-motion.md)
