Scene-owned UI
Compose game UI as scenes so each layer owns its DOM, input, and cleanup.
What this example shows
The game starts with a gameplay scene and a HUD scene. Use the HUD actions to add a pause or dialogue scene above them, then remove that layer without rebuilding the world or HUD.
Scenes update and draw from the bottom of the stack to the top. Scene UI follows the same order in the private UI layer, so a later modal can appear above a persistent HUD while the underlying scenes remain alive.
Mount a persistent layer
const gameplay = scenes.create(() => ({
name: "gameplay",
draw: (frame) => frame.draw_commands([
{ kind: "rectangle", draw: { rectangle: { x: 0, y: 0, width: 800, height: 450 }, color: world_color } },
]),
}));
const hud = scenes.create(({ create_ui }) => {
const panel = create_ui(HudPanel, { ... });
return { name: "HUD", enter: () => panel.mount() };
});
await scenes.mount(gameplay, { index: 0 });
await scenes.mount(hud, { index: 1 });Add and remove temporary layers
let pause_handle;
pause_handle = await scenes.mount(pause_scene, { index: 2 });
await scenes.remove(pause_handle);Use this pattern for pause menus, dialogue, inventory, maps, tooltips, debug overlays, and other layers that should have explicit lifetime. A scene can also provide input handling, subscriptions, assets, and enter cleanup alongside its UI.
Why the handle is typed
create_ui checks component props at compile time and returns a scene-owned UiHandle. Mounting is explicit,
updates are typed, and removing the scene disposes every handle it created. The core runtime stays Svelte-independent; this bridge is
available through the Svelte game host.