# Control playback

Play, pause, seek and change the speed of an animation, and move between its states.

Every export that plays with the player (an interactive flow, or a clip with the player loaded) has its player on its
root, `root.rupa`:

```js
const hero = document.querySelector(".hero").rupa;
hero.play("hover"); // a state: one of the clips of the scene
hero.pause();
hero.seek(500); // ms
hero.setSpeed(0.5); // hero.speed reads it
hero.on("statechange", (event) => console.log(event.state));
```

A single clip runs on CSS alone. Load the player on the page (`@rupamotion/player` or the CDN line) to control it.

Try it: each control below runs the line the code panel shows.

```js
import { mount } from "@rupamotion/player";

const player = mount(document.querySelector(".event-hero"));
player.play("drift");
```

## The Player object

```ts
interface Player<S extends string = string> {
	readonly root: Element;
	readonly state: S | null; // the state that plays
	readonly states: readonly S[];
	readonly time: number; // ms since this state began
	readonly duration: number | null; // length of the chain of the state; null when it has no end
	readonly paused: boolean;
	readonly speed: number;
	setSpeed(speed: number): void; // 1e-6 to 1e6
	play(state?: S | null): void; // no argument: go on; a state: start it from 0
	pause(): void;
	seek(ms: number): void; // clamped to [0, duration]
	on(
		type: "statechange" | "complete" | "play" | "pause" | "error",
		listener: (event: PlayerEvent<S>) => void,
	): () => void;
	destroy(): void;
}
```

## Good to know

- `root.rupa` is the player. A second `mount` on the same root returns the same object.
- `destroy()` lets go of the root at once (`root.rupa = 0`), even from a listener, so a `mount` after it makes a new
  player. Call it when you take a root out of the page (`<rupa-player>` and `<RupaPlayer>` do it for you).
- The player drives only the CSS animations of its export. A transition or `animate()` of your own inside the root, and
  a root nested in it, are left alone.
- Pausing freezes the run. Any state change plays again, and the speed carries to every later state.
- `play(state)` (or `data-rupa-state` set from your code) of a state a trigger plays starts it as if that trigger
  fired: a card flipped with `play("flip")` flips back on the next click, to what played before.
- A wrong call (`setSpeed(0)`, `seek(NaN)`, an unknown state) throws a `RangeError` or `TypeError` at your line.
  Events, timers and file loads never throw.
- Without JavaScript, the DOM attribute `data-rupa-state` still sets the state, and the root sends `rupastatechange`.
