# Tractor Beam

https://docs.kleaver.ie/sfx/beam/

## Markup

Three things carry the wiring: data-beam on the section, a .beam layer inside it, and data-beam-target on the button the beam passes through. data-beam-surface is optional and says where it lands.

```
<section class="hero hero--center" data-beam data-beam-position="0.45">
  <div class="beam" aria-hidden="true"></div>
  <div class="container hero__content">
    …
    <a class="btn btn--primary" data-beam-target href="…">Request an invite</a>
    <figure data-beam-surface>…</figure>
  </div>
</section>

<script defer src="scripts/beam.js"></script>   <!-- only on pages that use it -->
```

## Attributes

Behaviour lives in attributes on the host.

| Attribute | Values | Default |
| --- | --- | --- |
| data-beam-position | 0 (left) … 1 (right), or random | 0.5 |
| data-beam-delay | milliseconds after the page has loaded | 2000 |

random is rolled once per page load and kept off the extreme edges so the beam stays in view. A value that isn't a number or random falls back to the centre.

## Tokens

The look is a token, set in the client's theme.css.

| Token | Default | What it does |
| --- | --- | --- |
| --beam-color | var(--color-brand) | Any CSS colour |
| --beam-intensity | 0.5 | 0 off · 1 bright · 2 blazing. Keep it low behind text. |
| --beam-width | 0.2 | 0.2 a thin shaft … 1.6 a flood |
| --beam-smoke | 0.7 | 0 clean light … 1 thick fog |
| --beam-sparks | 0.6 | 0 none … 1.5 a lot |
| --beam-pace | 1 | Speed of the drift toward the source; 0 freezes it |
| --beam-fade | 1400ms | How long it takes to switch on |

## Behaviour and limits

- **Opt-in.** A site whose pages have no data-beam gets none of its CSS or JavaScript. The script is not part of the shared bundle.
- **Reduced motion.** With prefers-reduced-motion the beam shows at once as a single still frame: no delay, no fade, no animation.
- **No WebGL2 or no JavaScript.** A static cone of light is drawn by the CSS instead.
- **Pauses** when it is off screen or the tab is hidden, and renders at 60% resolution, which is fine for soft light.
- **Dark themes.** It adds light, so on a light background it will barely show.
- **One per page.** Only the first [data-beam] is drawn.
- **Text.** The beam sits behind the copy. If it crosses a paragraph, lower --beam-intensity and check the contrast.

## Catalog

Tractor Beam (in `components.css`)  ·  SFX #1  ·  see **`beam.html`**

| Component | Markup | Use for |
|---|---|---|
| Tractor Beam | `[data-beam]` host (usually a hero) > `.beam` layer (`aria-hidden`), a `[data-beam-target]` button, optional `[data-beam-surface]` | A beam of light from a point on the top edge, through the primary button, onto a surface. Switches on `data-beam-delay` ms (default 2000) after load. |

- **Attributes** (on the host): `data-beam-position` = `0`…`1` (left to right) or `random` (once per page load; default `0.5`); `data-beam-delay` = ms.
- **Tokens** (in `theme.css`): `--beam-color`, `--beam-intensity`, `--beam-width`, `--beam-smoke`, `--beam-sparks`, `--beam-pace`, `--beam-fade` (see `THEMING.md`).
- **Script:** `<script defer src="scripts/beam.js"></script>` on the page. WebGL2, one fullscreen triangle; pauses off screen; one still frame with reduced motion (no delay, no fade); a static cone without WebGL2 or JS.
- **Limits:** made for dark themes (it adds light); one beam per page; it sits behind the copy, so check contrast if it crosses text and lower `--beam-intensity`.
- **Tuning:** `beam.html` has a live panel; **Copy tokens** gives the CSS for `theme.css`.
