Scenes are the top-level containers for game content in tiny-engine. Each scene holds a root node tree that the engine starts, updates, and renders every frame. Swapping scenes — for example, transitioning from a main menu to a level — is handled by theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/sanchedev/tiny-engine/llms.txt
Use this file to discover all available pages before exploring further.
SceneManager, which tears down the old node tree and loads the new one cleanly.
Creating a Scene
AScene is constructed with a single function argument that returns a Node (or a Promise<Node> for async loading). The simplest form passes the root node directly; the lazy-loaded form uses a dynamic import() so the scene’s code is only fetched when it is first needed.
scenes/game.js (or any lazily imported module), export a root Node as the default export:
SceneManager
EveryGame instance owns a SceneManager at Game.sceneManager. Use it to register scenes and switch between them at runtime.
addScene(name, scene, autoLoad?)
Registers a scene under a name. If the optional third argument is true, the scene is loaded immediately as well.
setScene(name)
Switches to a previously registered scene. The current node tree is destroyed first, then the new scene is loaded and started. Pass null to unload the current scene without loading a new one.
preloadScene(name)
Preloads a scene in the background while the game is still running and returns a setter function you can call later to switch without any loading delay:
Game Lifecycle
Game.setup(options)
Game.setup() must be called before anything else. It creates the canvas element, appends it to the provided root element, and initialises the engine subsystems.
| Option | Type | Required | Description |
|---|---|---|---|
width | number | ✅ | Canvas width in pixels |
height | number | ✅ | Canvas height in pixels |
root | HTMLElement | ✅ | Parent element the canvas is appended to |
theme | Theme | — | Default visual theme; a plain Theme instance is used if omitted |
testOptions | Partial<TestOptions> | — | Testing helpers (e.g. showClickables) |
inputOptions | InputOptions | — | Pointer and keyboard input configuration |
Game.play()
Starts (or resumes) the game loop. Must be called after Game.setup(). Each frame the engine clears the canvas, calls update on the current scene’s node tree, resolves collisions, and calls draw.
Game.pause()
Suspends the game loop. The current frame finishes, then requestAnimationFrame is no longer scheduled. Call Game.play() to resume.
Game.destroy()
Stops the game loop, removes all event listeners, destroys the current scene node tree, and resets all internal state so Game.setup() can be called again.
Auto-pause on Blur
tiny-engine automatically pauses the game when the browser window loses focus (window blur). The Game.blured event is emitted at that point. Once the user returns to the tab and calls Game.play(), the loop resumes.