# radial-slowmo.js (C-13 module, v1.0)

One plain script, no dependencies, no `import`/`export`. It defines one global, `GameDNA_RadialSlowmo`. Paste it into a `<script>` tag or load it with `<script src>`, without changing it.

**Hold a key (default Q), the right mouse button or the round touch button (or long-press the canvas) and a ring of 4-8 slots opens around the hero.** While the ring is open the world runs in slow motion (0.1-0.3x), the scene dims and greys out around the hero, and the ring springs open. The player points at a slot with the arrow keys / WASD / number keys, a gamepad stick, the mouse or a finger, and **releases to use it**. A quick tap (shorter than `holdMs`) never opens the ring (`onTap`); Escape, a click outside the ring or a second finger cancels.

The module is a **gate** at the top of your fixed step: `if (!radial.step(STEP_MS)) return;`. It returns `false` on the steps the world must skip while slowed (at 0.2x the world runs one step in five) and `true` on every other step. While nobody holds the key it always returns `true`, so autoplay, `?t=` fast-forward and every ending are unchanged. All animation and selection advance inside `step()`; `draw()` only paints, so `?t=<ms>&freeze=1` matches play.

## API

`GameDNA_RadialSlowmo.create(options)` returns an instance.

| option | meaning |
|---|---|
| `anchor()` | **Required.** The hero's centre in logical canvas px **on screen** (subtract the camera). Read every step; the last good value is cached; without one the ring refuses to open (`stats().noAnchor`). |
| `slots` | Array (or function returning one) of up to 8 `{id, label, icon, count, disabled}`. `icon`: a built-in key (`sword bow bomb potion fire frost shield boomerang bolt lantern`, aliases such as `arrow`, `heal`, `spell`, `ice`, `dash`, `guard`), an Image / canvas, or `(ctx, x, y, size, slot) => {...}` drawing into the size x size box at (x, y). `count: 0` or `disabled: true` = shown greyed, cannot be chosen. |
| `view` | `{w, h}`: your logical canvas size (what your draw code uses). Default: the canvas size. |
| `canvas` | Your canvas element: needed for mouse and touch. |
| `uiRects()` | HUD and open dialog rects, logical px. The ring and its label stay off them; the dim never covers them. |
| `canOpen()` | `false` on title, pause, menu, dialogue, battle-menu and end screens: a press is ignored there. |
| `onSelect(slot, {index, id, method})` | The player released on a usable slot. Do the action here (equip, throw, cast, drink). `method` = `key` / `mouse` / `touch` / `pad` / `api`. |
| `onCancel({why, method})`, `onTap(slot, {index})`, `onOpen()`, `onClose()` | Optional. `onTap` = short press; use it for "use the equipped item" or ignore it. |
| `equipped` | Index highlighted as equipped at start (default 0); it follows each selection. |
| `params` | See below. |
| `skin` | Optional GameDNA skin id (accent colour). |
| `keyboard: false`, `pointer: false`, `gamepad: false` | Turn off the module's own listeners (drive it with `press` / `release` / `aim`). |
| `transform: 'keep'` | Draw with your current transform instead of the module's own (canvas size / `view`). |

| param | default | |
|---|---|---|
| `slowmo` | 0.2 | World speed while open (0.1-0.3 recommended). |
| `holdMs` | 180 | Hold this long before the ring opens; a shorter press is a tap. |
| `key` | `'q'` | Any letter or digit, `'shift'`, `'tab'`, `'space'`. Pick one your game does not use. |
| `mouse` | `'right'` | Hold the right mouse button on the canvas; `'off'`. |
| `touch` | `'auto'` | Round button bottom-right: `'auto'` (coarse pointer or after the first touch), `'on'`, `'off'`. |
| `longpress` | `'on'` | Long-press (`longpressMs` 420) anywhere on the canvas opens it too. |
| `dim`, `desat` | 0.5, 0.85 | Backdrop strength (a lit circle stays around the hero). |
| `radius` | `'auto'` | Ring radius in px (auto: about a fifth of the shorter side, grows to fit 8 slots). |
| `style` | `'auto'` | `'pixel'` when the view is 270 px tall or less (whole pixels, hand-pixelled icons, 5x7 font), else `'smooth'` (vector icons, system font). |
| `spring` | `'springy'` | `'snappy'` for less overshoot. |
| `labels` | `'on'` | Name plate of the highlighted slot. |
| `reduced` | `'system'` | `'on'`: no spring, instant slow-down. |
| `slots` | 6 | Only for the built-in demo slot list. |

| method | |
|---|---|
| `step(dtMs)` -> bool | **First thing in the fixed step, after input is read.** `false` = skip the world update (and autopilot) this step. Also call it on menus (returns `true` there). |
| `capturing()` | `true` while the ring is open: give the world **neutral input** (no movement, no attack, no confirm). Keys pressed now never reach your handlers, but keys held before the ring opened are still in your held-key set. |
| `draw(ctx)` | **Last call in render, after the HUD.** Saves / restores the context. |
| `scale()` | Current world speed 0..1 (for dt-based games: `dt *= radial.scale()` instead of skipping). |
| `clear()` | New game, scene change, respawn: closes the ring at once, no callbacks. |
| `press(m)`, `release(m)`, `aim(x, y)`, `pointer(x, y, 'mouse' / 'touch')`, `pick(i)`, `cancel()` | Drive it from your own input or a script. |
| `isOpen()`, `phase()`, `equipped()`, `setEquipped(i)`, `setSlots(s)`, `slots()`, `state()`, `stats()`, `lastDrawn()`, `setParams(p)`, `params()`, `destroy()` | `stats()`: `{opens, selects, cancels, taps, openSteps, runOpen, skipped, swallowed, clamped, noAnchor, errors, byMethod, ...}`; `lastDrawn()`: `[{kind: 'ring' / 'slot' / 'label' / 'button', x, y, w, h}]`. |

## Integration example (generic)

```js
const radial = GameDNA_RadialSlowmo.create({
  canvas, view: { w: VIEW_W, h: VIEW_H },
  anchor: () => ({ x: hero.x + hero.w / 2 - cam.x, y: hero.y + hero.h / 2 - cam.y }),
  slots: () => [{ id: 'sword', label: 'Sword', icon: 'sword' }, { id: 'potion', label: 'Potion', icon: 'potion', count: potions },
                { id: 'bomb', label: 'Bomb', icon: 'bomb', count: bombs }, { id: 'dash', label: 'Dash', icon: 'bolt' }],
  uiRects: () => [hudRect].concat(dialogOpen ? [dialogRect] : []),
  canOpen: () => scene === 'play' && !dialogOpen && !paused,
  onSelect: slot => useItem(slot.id),
  params: { slowmo: 0.2, key: 'q' }
});
function fixedStep() {
  const input = readInput();                       // consume taps as before
  if (!radial.step(STEP_MS)) return;               // slowed: the world waits this step
  updateWorld(radial.capturing() ? NEUTRAL_INPUT : (autoplay ? autopilot() : input));
}
function render() { drawWorld(); drawHud(); radial.draw(ctx); }
function newGame() { /* ...existing... */ radial.clear(); }
```

## Rules

- **The gate skips everything that changes the world**: movement, enemies, timers, spawns, RNG, animation counters, the step counter the game shows. Title / pause / menu code may stay above the gate. Never use wall-clock time for game logic.
- **Neutral input while `capturing()`**: the hero stands still (the world itself keeps moving, slowly). Do not let a key that was held before the ring opened keep walking the hero.
- **`onSelect` acts on the next world step** or at once; never open a second menu from it.
- The anchor is in screen px (after the camera). If the hero can leave the screen, the ring is clamped inside the view anyway.
- Find every caller of what you touch: autoplay (`?auto=1`), `?t=` fast-forward (calls the same step), restart, title, pause, keyboard shortcuts. Hook, don't replace. The hold key must not be one the game uses.

## Self-check without a browser

- The module file is byte-identical to the one you were given and loads before `create`.
- `radial.step(...)` sits in the fixed step after input reading and before the autopilot and world update and returns early on `false`; `?t=` fast-forward goes through the same step.
- `capturing()` gives the world a neutral input object with every field the update reads.
- `anchor()` subtracts the camera; `uiRects()` uses the same coordinates as `view`.
- `canOpen()` is false on every non-play screen; `clear()` runs on each new run.
- `radial.draw(ctx)` is the last call of every render path that shows the world.
- Walk one use with the defaults: hold Q 0.18 s (opens), hold 1 s open (12 world steps instead of 60), press Up (top slot highlighted), release: `onSelect` gets the top slot once; the world returns to full speed within about 8 steps.

## What it guarantees

- While open the world gets exactly `slowmo` of the steps (one step every 1/`slowmo` frames, accumulator-based, deterministic), easing in and out over about 8 steps. Proven by `checks/core-check.mjs` with mutants (no slow-down, gate ignored, keys leaking, no edge clamp, tap opening, wrong mouse angle, draw changing state).
- Selection works by keys (sticky: letting the arrows go keeps the slot), number keys, gamepad stick (sticky), mouse angle and touch drag (positional: back to the centre clears it; faint dots from the hub show the pointer direction even inside the dead zone). Releasing over no slot cancels and the next hold opens again; Escape, a click outside or a second finger cancel while the input is still held, and only that input's own release is swallowed.
- While open, every key-down and every click / touch on the canvas is swallowed (key-ups always pass so your held-key sets stay correct).
- The ring and its label never leave the view and keep off `uiRects`; they glide, never jump. Near an edge, when the ring cannot centre on the hero, it moves clear of him (a dotted tether points to him), so no slot or label ever covers the hero.
- No `Math.random`, no wall clock; `step` and `draw` never throw; throwing callbacks are counted in `stats().errors`.
