# Route Graphics Route Graphics is a declarative 2D UI/rendering system built on PixiJS. You describe states with JSON, then Route Graphics handles element lifecycle, animations, audio, and interaction events for you. This README stays intentionally short. The hosted docs are the source of truth for setup guides, node reference pages, events, asset loading, plugin authoring, and live examples. ## Start Here - [Documentation](http://route-graphics.routevn.com/docs/introduction/introduction/) - [Playground](http://route-graphics.routevn.com/playground/) ## Installation ```bash bun install route-graphics ``` ## Minimal Usage ```javascript import createRouteGraphics, { createAssetBufferManager, textPlugin, rectPlugin, spritePlugin, containerPlugin, soundPlugin, } from "route-graphics"; const assets = { "circle-red": { url: "/public/circle-red.png", type: "image/png" }, "bgm-1": { url: "/public/bgm-1.mp3", type: "audio/mpeg" }, }; const assetBufferManager = createAssetBufferManager(); await assetBufferManager.load(assets); const app = createRouteGraphics(); await app.init({ width: 1280, height: 720, backgroundColor: 0x000000, rendererPreference: "webgl", rendererFallback: true, plugins: { elements: [textPlugin, rectPlugin, spritePlugin, containerPlugin], audio: [soundPlugin], }, eventHandler: (eventName, payload) => { console.log(eventName, payload); }, }); await app.loadAssets(assetBufferManager.getBufferMap()); document.body.appendChild(app.canvas); app.render({ id: "hello-state", elements: [ { id: "title", type: "text", x: 40, y: 40, content: "Hello Route Graphics", textStyle: { fill: "#ffffff", fontSize: 36, }, }, ], animations: [], audio: [], }); ``` `rendererPreference` accepts `"webgl"` or `"webgpu"`; `rendererFallback: false` makes initialization fail instead of selecting the other backend. The selected value is available as `app.rendererType`. Inline shader effects always provide both GLSL and WGSL so the same state works on either backend. For scene/editor/preview handoffs, use `await app.reset({ eventHandler })` instead of destroying and recreating the renderer. Reset clears the display tree, animations, audio playback, keyboard bindings, DOM input controls, cursor state, snapshot audio-effect requests, and pending render callbacks while preserving the same canvas and GPU context. A render that reset interrupts is reported to the previous `eventHandler` as `renderComplete` with `aborted: true`, and the first render after a reset calls `onFirstRender` again. It accepts runtime initialization overrides, including width/height and plugins; renderer backend, fallback policy, and debug mode must remain unchanged. Loaded assets stay owned by the instance: use `unloadAssets()` for media no longer needed, and `destroy()` when the rendering surface is permanently discarded. Serialize reset with asset loading and unloading. This avoids repeated WebGL context creation during rapid view changes, which can exhaust WebKit's context accounting even after explicit context loss ([WebKit bug 218305](https://bugs.webkit.org/show_bug.cgi?id=218305)). Run `bun run build && node scripts/testRendererReset.mjs` to verify 40 resets in Chromium and WebKit reuse one context, clear runtime state, and release the context on final destruction, and that reset removes input controls and keyboard bindings, stops animations, and keeps pointer events working. CI runs it in the browser suite; the `reset-stops-audio` AVT spec covers audio. `createAssetBufferManager()` may keep image and video assets as direct source URLs when possible, while audio and font assets remain buffer-backed. `render()` remains synchronous, but plugins may mount asynchronously. Before extracting pixels or reading a mounted layout, await the requested scene: ```javascript app.render(nextState); await app.whenRenderReady(); const image = await app.extractBase64(); ``` This waits for async element operations and their first renderer submission, not the end of animations/reveals, a GPU fence or browser composition. Identical renders share the current wait. Supersession or destruction before readiness rejects with `AbortError`; preparation/render errors reject with their original error. Before any render and after destruction there is no active wait. Capture the promise before requesting another state to observe that particular request. Use `renderComplete` when playback completion is required. The PNG CLI waits for scene readiness before applying `--time`; `--wait-for-render-complete` retains its separate playback-completion meaning. `extractBase64()` encodes the frame as a PNG data URL. To scale or re-encode the frame instead, such as for a thumbnail, take the same pixels as a canvas and skip encoding and decoding a full-size PNG: ```javascript const frame = await app.extractCanvas(); const thumbnail = document.createElement("canvas"); thumbnail.width = 400; thumbnail.height = 225; thumbnail.getContext("2d").drawImage(frame, 0, 0, 400, 225); ``` Both take an optional element label to extract that element alone. Assets loaded by an app instance can be released after no rendered or transitioning element still references them: ```javascript await app.unloadAssets(["circle-red", "bgm-1"]); ``` `unloadAssets()` removes renderer cache entries and releases texture, video, audio, and font resources owned by that Route Graphics instance. Unknown or already-unloaded keys are ignored, shared resources remain alive until their last Route Graphics consumer unloads them, and released keys can be loaded again. The event handler also receives `rendererContextLost` and `rendererContextRestored` lifecycle events. Fallback UI should live outside the renderer so it remains available while the graphics context is unavailable. WebGPU device loss emits `rendererContextLost`; because a lost WebGPU device is not restorable, consumers must recreate the Route Graphics instance rather than wait for `rendererContextRestored`. ### Hover-only elements A text, sprite or rect whose only pointer binding is `hover` keeps tracking hover, but presses, releases and wheel input pass through it to the element beneath. A hover-styled label drawn over a clickable button therefore does not block the button. To make such an element catch presses itself, give it a `click`, `rightClick`, `scrollUp`, `scrollDown` or `drag` binding. Containers are not affected. ### Semantic bounds hit testing `hitTestElementBounds({ x, y })` accepts renderer-space coordinates and returns all hit branches from front to back. Each branch contains a root-to-deepest `path`; every path entry includes the semantic element `id`, `type`, and live world-space bounds. `bounds.corners` preserves rotation and `x`/`y`/`width`/`height` is the enclosing axis-aligned rectangle. Container scroll offsets and viewport clipping are applied, while opacity and transparent pixels do not create click-through holes. ```javascript const hits = app.hitTestElementBounds({ x: pointerX, y: pointerY }); const frontmostPath = hits[0]?.path ?? []; ``` Route Graphics deliberately returns rendered semantic identities rather than editor document identities, and it does not apply consumer-specific filtering. Consumers remain responsible for mapping each render occurrence to an authored selection owner and ignoring any renderer elements they do not want to select. Bounds queries are observational. They do not alter or suppress existing hover, click, drag, keyboard, input, slider, or scroll behavior. A consumer can therefore observe a pointer gesture for selection while Route Graphics continues processing the same authored interaction. For complete usage details, go to: - [Getting Started](http://route-graphics.routevn.com/docs/introduction/getting-started/) - [Assets & Loading](http://route-graphics.routevn.com/docs/guides/assets-loading/) - [Events & Render Complete](http://route-graphics.routevn.com/docs/guides/events-render-complete/) - [Shaders](http://route-graphics.routevn.com/docs/guides/shaders/) - [Custom Plugins](http://route-graphics.routevn.com/docs/guides/custom-plugins/) Design notes (see the [documentation index](./docs/README.md) for current contracts): - [Audio Effects](./docs/audio-effects.md) - [Command-Controlled Sound Playback](./docs/audio-playback-commands.md) - [Animation Model](./docs/animation-model.md) - [Portable GSAP-Style Timelines](./docs/portable-gsap-timelines.md) - [Shared Timeline Design and Roadmap](./docs/shared-timeline-plan-design-and-roadmap.md) - [Shared Timeline Implementation Review](./docs/shared-timeline-implementation-review.md) - [Shader Interface](./docs/shader-interface.md) ## Render CLI This repo includes a CLI that renders RouteGraphics YAML into PNG or MP4 output by: 1. reading YAML in Node, 2. launching headless Chromium through Playwright, 3. rendering with the bundled `dist/RouteGraphics.js`, and 4. exporting either a still frame or an autoplayed state sequence. Full CLI reference: [`docs/render-cli.md`](./docs/render-cli.md) The legacy PNG-only reference is still available at [`docs/png-render-cli.md`](./docs/png-render-cli.md). ```bash # one-time browser install if Chromium is not already available npx playwright install chromium # render a YAML file to PNG or MP4 route-graphics render ./examples/hello.yaml -o ./out/hello.png route-graphics render ./examples/storyboard.yaml -o ./out/storyboard.mp4 ``` Supported top-level YAML shapes: - A single state object - An array of states - A wrapper object with `width`, `height`, `backgroundColor`, optional `assets`, and either `state` or `states` - Multiple YAML documents separated by `---` (treated as a state list) Minimal example: ```yaml width: 1280 height: 720 backgroundColor: "#101820" elements: - id: title type: text x: 80 y: 80 content: "Hello PNG" textStyle: fill: "#ffffff" fontSize: 48 - id: bar type: rect x: 80 y: 160 width: 320 height: 24 fill: "#4fd1c5" ``` Asset handling: - Asset-bearing render-state fields such as `src`, `thumbSrc`, `barSrc`, `inactiveBarSrc`, and `soundSrc` must reference top-level `assets` aliases. - Direct file paths and URLs are allowed inside top-level `assets` values. - Direct asset references inside `elements`, `audio`, or nested interaction config are rejected. Example with asset aliases: ```yaml width: 1280 height: 720 assets: hero: ./assets/hero.png uiFont: path: ./assets/fonts/NotoSans-Regular.ttf type: font/ttf fallbackFont: path: ./assets/fonts/Fallback-Regular.ttf type: font/ttf elements: - id: title type: text x: 60 y: 60 content: "Alias-backed render" textStyle: fill: "#ffffff" fontSize: 42 fontFamily: [uiFont, fallbackFont] - id: avatar type: sprite x: 60 y: 140 width: 128 height: 128 src: hero ``` Invalid example: ```yaml elements: - id: avatar type: sprite x: 60 y: 140 width: 128 height: 128 src: ./assets/hero.png # rejected by the CLI ``` Useful PNG flags: - `--state ` selects a state from an array or multi-document YAML file. - `--time ` samples animations at a specific manual timeline position. - `--wait-for-render-complete` waits for a `renderComplete` event before capture. Useful MP4 flags: - `--states ` selects state indexes/ranges such as `0,2-5`. - `--fps ` controls output frame rate. - `--hold`, `--initial-hold`, and `--final-hold` control state dwell time. - `--browser-executable ` uses a system Chrome/Chromium instead of Playwright's managed browser. ## Development ```bash # Run tests bun run test # Render a YAML file into a PNG or MP4 route-graphics render ./examples/hello.yaml -o ./out/hello.png route-graphics render ./examples/hello.yaml -o ./out/hello.mp4 # Ensure VT assets are real binaries, not Git LFS pointer files git lfs pull git lfs checkout # Build the local docs/playground site cd playground bun install bun run build ``` The docs site and playground source live under `playground/`. Visual regression assets under `vt/static/public` and `vt/reference` are stored in Git LFS. If those files are not checked out, VT pages will render blank and browser logs will show image/audio decode errors. ## Community Join us on [Discord](https://discord.gg/8J9dyZSu9C) to ask questions, report bugs, and stay up to date. ## License This project is licensed under the [MIT License](LICENSE).