# State Machines

Interactive scenes with states that play or scrub clips, triggered by hover, click, scroll, keys and timers.

`state_machines` make a scene interactive. Each machine has states, an `initial` one, and transitions that fire on triggers.

```json title="apps/docs/examples/state-machines.json"
{
  "camera": { "position": [0, 0, 6], "target": [0, 0, 0], "fov": 40 },
  "lighting": { "rig": "studio_soft" },
  "environment": {
    "background": {
      "radial_gradient": { "inner": "#1b2233", "outer": "#07090f" }
    }
  },
  "nodes": {
    "button": {
      "interactive": true,
      "mesh": {
        "geometry": {
          "rounded_box": { "size": [2.4, 0.8, 0.3], "radius": 0.15 }
        },
        "material": "preset:glossy_plastic"
      },
      "color": "#3d85c6"
    },
    "label": {
      "parent": "button",
      "transform": { "position": [0, 0, 0.2] },
      "text": { "content": "Hover me", "font": "inter_bold", "size": 0.3 }
    }
  },
  "state_machines": {
    "button": {
      "initial": "idle",
      "states": {
        "idle": {},
        "hover": {
          "set": [
            {
              "target": "button",
              "property": "scale",
              "value": [1.15, 1.15, 1.15]
            },
            { "target": "button", "property": "color", "value": "#e07a5f" }
          ]
        }
      },
      "transitions": [
        {
          "from": "idle",
          "to": "hover",
          "trigger": { "hover_enter": { "node": "button" } }
        },
        {
          "from": "hover",
          "to": "idle",
          "trigger": { "hover_leave": { "node": "button" } },
          "easing": "spring_bouncy"
        }
      ]
    }
  }
}
```

![Render of state-machines](/renders/state-machines.jpg)

## States

A state can play a `clip` (from `clips`), `scrub` a clip from an input instead of the clock (`scroll`, `pointer_x` or `pointer_y`), and `set` values to hold while it's active.

## Transitions

Transitions are evaluated in order and the first match wins. `from` is a state or `"*"` for any. Each blends over `duration` with `easing`, `spring_snappy` by default.

## Triggers

`hover_enter`, `hover_leave` and `click` on a node (children count; mark the node `interactive`), `scroll` progress entering `[min, max]`, `in_view`, `after` a number of seconds, `clip_end`, a `key`, and a custom `event` sent by the host with `player.event(name)`.

See [`examples/interactive-state-machine.json`](https://github.com/brainfish-ai/motion/blob/main/examples/interactive-state-machine.json).

## API Reference

### StateMachine

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `initial` (required) | `string` | — | State entered on load. |
| `states` | `map of State` | `{}` | States keyed by name. |
| `transitions` | `Transition[]` | `[]` | Evaluated in order; the first matching transition wins. |
### State

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `clip` | `string` | — | Clip (from `clips`) played when entering this state. |
| `scrub` | `ScrubSource` | — | Drive the clip's time from an input instead of the clock. |
| `set` | `PropertySet[]` | — | Property values held while in this state. |
### Transition

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `duration` | `number (≥ 0)` | `0.35` | Blend time in seconds. |
| `easing` | `Easing` | `"spring_snappy"` | Curve used to blend between the states. |
| `from` (required) | `string` | — | Source state name, or `"*"` for any state. |
| `to` (required) | `string` | — | Target state name. |
| `trigger` (required) | `Trigger` | — | What fires the transition. |
### Trigger

What fires a transition. Written as `{"click": {"node": "button"}}`, `{"scroll": {"min": 0.5}}`, `{"after": {"seconds": 2}}`, `"clip_end"`, ...

| Value | Description |
| --- | --- |
| `"in_view"` | The embed scrolls into the viewport. |
| `"clip_end"` | The current state's clip finished (once/ping-pong clips). |

- `{"hover_enter": { … }}`: Pointer starts hovering a node (or any of its descendants).

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `node` (required) | `string` | — | Node id. Hovering a child counts. |

- `{"hover_leave": { … }}`: Pointer stops hovering a node.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `node` (required) | `string` | — | Node id. |

- `{"click": { … }}`: Node is clicked/tapped.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `node` (required) | `string` | — | Node id. Clicking a child counts. |

- `{"scroll": { … }}`: Scroll progress (0..1) enters `[min, max]`.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `max` | `number` | `1` | Upper bound of scroll progress, 0 to 1. |
| `min` | `number` | `0` | Lower bound of scroll progress, 0 to 1. |

- `{"after": { … }}`: Time spent in the current state reaches `seconds`.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `seconds` (required) | `number` | — | Seconds spent in the state before it fires. |

- `{"key": { … }}`: Keyboard key (KeyboardEvent.key) pressed while focused.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `key` (required) | `string` | — | Key name, like `ArrowRight` or `Space`. |

- `{"event": { … }}`: Custom event sent from the host app via the SDK.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `name` (required) | `string` | — | Custom event name the host sends. |
### PropertySet

Pin a property of a node (or `$camera`) to a value.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `property` (required) | `Property` | — | The property to pin. |
| `target` (required) | `string` | — | Node id, or `$camera`. |
| `value` (required) | `AnimValue` | — | The value to hold. |
### ScrubSource

Input that scrubs a clip (0..1 maps to the clip's 0..duration).

| Value | Description |
| --- | --- |
| `"scroll"` | Page scroll progress of the embed through the viewport. |
| `"pointer_x"` | Horizontal pointer position over the canvas. |
| `"pointer_y"` | Vertical pointer position over the canvas. |
