# Introduction What @rupamotion/player is, what it adds to a Rupamotion export, and when you need it. `@rupamotion/player` is a small runtime that plays and controls the animations a Rupamotion export writes. The animations stay plain CSS, so they run without JavaScript. The player holds the animations the browser makes from that CSS and drives them through the Web Animations API: states, hover, click and in-view triggers, run back, loop, play, pause, seek, speed and events. It carries no keyframes and evaluates nothing. - **Tiny.** The core is a few kilobytes gzipped. The exact numbers are on the [size page](/reference/size/). - **Never crashes the page.** Every entry point is guarded. An internal error makes that one player inert and the CSS goes on playing. - **No telemetry.** The only network requests are the ones your page asks for: a `.rupaplay` file and its font stylesheet. - **MIT licensed**, ES modules, typed. ## What you need it for A single clip runs on CSS alone. Load the player when you want to control an animation from code, switch between the states of an interactive flow, react to events, or load an animation from a `.rupaplay` file while keeping your own markup on the page. ## The four entries | Entry | What it holds | | - | - | | `@rupamotion/player` | The core: `mount` and the `Player` type. | | `@rupamotion/player/file` | `loadFile` and `mountFile` for `.rupaplay` files. | | `@rupamotion/player/element` | The `` custom element, and every snippet root mounted. | | `@rupamotion/player/react` | `RupaPlayer`, `useRupaPlayer` and `usePlayerState` for React 18 or 19. | ## Where next 1. [Install the player](/start/installation/). 2. [Play your first animation](/start/quick-start/). 3. [Choose a way to use it](/guides/choose-a-way/). # Installation Install @rupamotion/player from npm or load it from a CDN, and what TypeScript needs. ## From npm ```sh npm i @rupamotion/player ``` The types come with the entries (the package `exports`), so TypeScript needs `moduleResolution` set to `bundler`, `node16` or `nodenext`. The classic `node` resolution does not read `exports`. The package has no `engines` field: it runs in browsers and imports on any server. ## From a CDN, without a build step Always name an exact version, never `latest`: ```html ``` That line loads the `` element and mounts every snippet root on the page. ## Pin a version ```sh npm i @rupamotion/player@0.1.0 ``` ## Next [Quick start](/start/quick-start/) # Quick start Play an exported .rupaplay file in a page with one script line and one element. Export your animation from Rupamotion as a **Player file** (a `.rupaplay` file) and put it next to your page. This page is complete: save it, serve it, and the animation plays. ```html Rupamotion player ``` The element loads the file, writes its markup and CSS, and mounts the player (`element.player`). `cloak` keeps the root hidden until the CSS is installed. That is it: the animation plays. This one is a `.rupaplay` file loaded and played by the player of this version: ## Need control? The element's `player` is the player once the file is mounted. `rupaready` says it is mounted, `rupaerror` says the load failed. Add a script after the element: ```html ``` See [Control playback](/guides/control/) for `play`, `pause`, `seek`, speed and states, and [Events](/guides/events/) for what a player tells you. ## Next - [Choose a way to use it](/guides/choose-a-way/) - [Control playback](/guides/control/) - [Your framework](/frameworks/html/) # Choose a way Three ways to use the player: your own markup, the full snippet, or the file alone. There are three ways to put a Rupamotion animation in a page. They differ in who owns the markup and where the animation comes from. | Way | Markup | Animation comes from | Use it when | | - | - | - | - | | [Your own markup](/guides/your-markup/) | Yours, kept on the page | A `.rupaplay` file | Search engines must read your text, or the text changes | | [The full snippet](/guides/full-snippet/) | The exported markup in the page | CSS in the page plus a JSON table | The animation is large, or must work before any script | | [The file alone](/guides/file-alone/) | Written from the file | A `.rupaplay` file | You want the simplest embed | If you are unsure, start with the file alone. If the page has thousands of animated elements, read [large animations](/guides/large-animations/). # Your own markup Keep your own headings and text and let a .rupaplay file add the design, animation and control. Export the animation as a `.rupaplay` file and copy its markup (the export dialog has **Copy markup**). The page keeps your `h1`, `p` and `div`, and the player adds the design, the animation and the control. ```html

Your own, editable heading

Text a search engine reads without JavaScript.

``` Or from code, on any element that holds your markup: ```js import { mountFile } from "@rupamotion/player/file"; const result = await mountFile(document.querySelector("#hero"), "/hero.rupaplay", { markup: "keep" }); if (result.error) console.warn(result.error.code, result.error.message); else result.player.play("exit"); ``` ## The markup option `markup` says what happens to the markup inside the root: - `keep` (the default of `mountFile` and ``) keeps it and animates what matches the classes of the file. - `replace` writes the markup of the file. - `auto` (the default of ``) writes the markup of the file into an empty root, keeps a root that has every class of the file, and replaces any other. **The classes in the markup are addresses. Do not rename them**: the animation finds its elements by them. Below, the page holds the markup and the `.rupaplay` file adds the rest: ```js import { mountFile } from "@rupamotion/player/file"; const { player } = await mountFile(document.querySelector("#event-hero"), "/event-hero.rupaplay", { markup: "keep" }); player.play("drift"); ``` # The full snippet Use the exported markup and CSS in your page and add the player from npm or a CDN to control it. Export the snippet with the "npm / CDN" runtime. It is markup, CSS and a ` ``` Importing `@rupamotion/player/element` mounts every snippet root of the page. In a bundler, mount a root yourself: ```js import { mount } from "@rupamotion/player"; const player = mount(document.querySelector(".hero-root")); // reads the table of the page // the same player: document.querySelector(".hero-root").rupa player.on("statechange", (event) => console.log(event.state)); ``` This snippet replays its highlight each time it scrolls into view: ```js import { mount } from "@rupamotion/player"; const player = mount(document.querySelector(".marker-highlight")); player.play("replay"); ``` ## Files mode The files mode of the export writes the same table as `.player.json` (data only: the package has the code and the types). Import it and name the states with the generic: ```ts import { mount } from "@rupamotion/player"; import data from "./hero.player.json"; const player = mount<"enter" | "hover">(document.querySelector(".hero-root")!, data); player.play("hover"); // a state name TypeScript checks ``` `data` takes the JSON as TypeScript reads it. The player checks the table when it mounts and leaves out a part that fails. ## The inline runtime With the "Inline" runtime the snippet carries the same core itself, and `root.rupa` is the same object. # The file alone Load a .rupaplay file with the rupa-player element and let it write the markup and CSS. ```html ``` The element loads the file, writes its markup and CSS, and mounts the player (`element.player`). `cloak` keeps the root hidden until the CSS is installed. `integrity` locks the page to the exact file: the player refuses a file that was changed, and you update the value after every re-export. ```js import { mountFile } from "@rupamotion/player/file"; const { player } = await mountFile(document.querySelector("#card-flip"), "/card-flip.rupaplay", { markup: "replace" }); player.play("flip"); ``` The element reports on itself with two events: ```js const el = document.querySelector("rupa-player"); el.addEventListener("rupaready", () => el.player.play()); // the file is loaded and the player mounted el.addEventListener("rupaerror", (event) => console.warn(event.detail.error.code)); // a failed load ``` `mountFile` sends the same `rupaerror` on its root. The `error` event of a `Player` exists only after a successful mount. See the [element reference](/reference/rupa-player-element/) for every attribute. # 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 { 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) => 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 (`` and `` 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`. # Events Listen to statechange, complete, play, pause and error on a player, and the DOM events of the element. `player.on(type, listener)` returns a function that removes the listener. The listener gets `{ type, player, state, fatal?, error? }`. ```js const off = player.on("statechange", ({ state }) => console.log("now", state)); player.on("complete", ({ state }) => console.log(state, "ended")); off(); ``` Click the card, or pick a state: the log shows each event as the player sends it. ```js import { mount } from "@rupamotion/player"; const player = mount(document.querySelector(".card-flip")); player.play("flip"); ``` | Event | When | | - | - | | `statechange` | A state starts: a trigger, `play(state)`, a loop back. While paused it is sent after `play`. | | `complete` | A state with a finite end has ended. Again after a seek back in it; never for an endless one. | | `play` | `paused` changed to false, whatever changed it. | | `pause` | `paused` changed to true, whatever changed it. | | `error` | `{ fatal: true, error }`: an internal error made the player inert (see [errors](/guides/errors/)). | ## Rules for listeners - A listener that throws is reported the way an `EventTarget` reports one (`reportError`). The other listeners and the player go on. - An API call made while an event is sent (for example `play` inside `statechange`) is queued and runs right after, in a task of its own. A listener that plays on every `statechange` cannot hang the page. The argument is still checked at once. - `destroy` is not queued: the root is let go at once. ## DOM events The root sends `rupastatechange` with `detail.state`. The `` element sends `rupaready` and `rupaerror`; see the [element reference](/reference/rupa-player-element/). # Errors and inert players How the player reports failures: file errors, the fatal error event, and what an inert player does. ## File errors `loadFile` and `mountFile` never reject. They resolve to `{ file }` or `{ player }`, or to `{ error }`: ```js import { mountFile } from "@rupamotion/player/file"; const { player, error } = await mountFile(document.querySelector("#hero"), "/hero.rupaplay"); if (error) console.warn(error.code, error.message); else player.play(); ``` `error.code` is one of the [error codes](/reference/error-codes/). ## Wrong calls A wrong call to a player throws a `RangeError` or `TypeError` at your line: `setSpeed(0)`, `seek(NaN)`, an unknown state. Events, timers and file loads never throw. ## Inert players An internal error makes that one player inert. It lets go of its listeners, timers and observers, sends `error` once with `fatal: true`, then drops its listeners. The DOM and the CSS stay, so the snippet keeps playing its CSS. ```js player.on("error", (event) => { if (event.fatal) reportToMyMonitoring(event.error); }); ``` An inert player keeps `root.rupa`. Every method does nothing without an exception, except that `play(state)` still sets `data-rupa-state`, so the CSS plays the state. `destroy()` is the same release without an error event. The player has no telemetry. Forward the `error` event to your own monitoring if you want it. # Large animations How long a large .rupaplay file takes to install, and how to keep a page with thousands of animated elements fast. A `.rupaplay` file downloads and unpacks without blocking the page. Installing it then runs in one go: the markup is rebuilt element by element, the CSS is parsed and checked rule by rule, and the player takes the animations over. The animation itself is the CSS of the browser, so once installed a large file plays as smoothly as a small one. A file holds at most 20,000 CSS rules. For a page with thousands of animated elements, put the animation in the HTML of the page instead of loading it as a file: - [The full snippet](/guides/full-snippet/): the markup and CSS go into the page, so the browser reads them while it parses the page, with nothing to rebuild or check. - [Your own markup](/guides/your-markup/) with `markup="keep"`: your markup stays in the page, so only the CSS of the file is installed. Both ways are described in [choose a way](/guides/choose-a-way/). The size of the player itself is on the [size page](/reference/size/). # Security How the player treats a .rupaplay file as untrusted data: sanitising, integrity checks, size limits and Trusted Types. A `.rupaplay` file is data that comes from somewhere else, so it is treated as untrusted. - The markup is parsed by `DOMParser` (inert), then **rebuilt** node by node from an allow-list of elements and attributes into a new fragment. Nothing is serialised and parsed again. Never written: `script`, `iframe`, `object`, `embed`, `foreignObject`, `style`, `link`, `meta`, `base`, `form`, `on*` attributes. A `href` is a `#fragment`; an image is `data:image/*`, `https:` or relative. - The CSS goes into a constructable stylesheet (inert, allowed by a strict CSP, no inline `