Advanced / Scene · guide
Scene — overview
Current Apexify.js 6.0.0 documentation for Scene — overview.
Scene is Apexify.js's declarative composition graph: a positive width, positive height, optional background, and an ordered layers array. The same scene can render to PNG directly or feed the scene→GIF / scene→video helpers.
Types: SceneRenderInput, SceneLayer, SceneRenderOptions from apexify.js (or the type-only apexify.js/types subpath).
On ApexPainter: createScene, renderScene, renderSceneToGIF, renderSceneToVideoFrames, validateSceneRenderInput.
Higher-level templates, named assets, components, and plugins are documented in the Composition hub; they ultimately produce or transform normal scene data.
Validation is mandatory
Scene validation runs before rendering. There is no public validate: false bypass.
Validation covers more than root dimensions:
- finite positive canvas/surface dimensions and canvas resource budgets,
- aggregate scene pixel budget across the root and nested surfaces,
- total scene layer count,
- nested surface count/depth,
- image count,
- text-layer count and aggregate text content,
- chart count,
- remote asset count,
- image/text domain validation,
- finite numeric transform leaves and opacity ranges,
- authoritative scene/surface dimensions (background configs may not redefine width/height).
SceneRenderOptions.maxSurfaceDepth may lower the configured maximum for one render, but cannot raise the global runtime limit.
Use validateSceneRenderInput(input, { maxSurfaceDepth? }) when you want to reject untrusted composition data before entering a larger workflow; the renderer will validate again at its own boundary.
SceneBuilder contract
createScene() returns a mutable builder with copy-on-ingress semantics. Caller-owned layer/background objects and Buffers are copied when added. A later mutation of the original input does not silently change the builder.
Stack methods include:
addLayer,addLayersinsertLayer,insertLayersinsertBefore,insertAfterreplaceLayer,replaceLayersmoveLayerremoveLayer,clearLayerssetBackground,clearBackgroundlayerCount
Invalid indices throw ApexifyInputError instead of being silently clamped.
toRenderInput() returns an isolated snapshot. Mutating the returned structure does not mutate the builder. render() also renders a snapshot, so later caller mutation cannot affect an already-started render.
SceneBuilder.render() leaves asset references unchanged by default. A builder created by ApexPainter.createScene() can opt in with { resolveAssetRefs: true }.
Paint order
Layer array order is stable bottom → top. Earlier entries paint first; later entries paint over them.
Builder insert/move/replace operations change this array order directly; no hidden z-index is applied.
Nested surfaces
A surface is a child scene with explicit placement dimensions. Its background and child layers render into a child canvas, then that canvas is composited directly onto the parent with placement opacity/composite mode/rotation/scale.
Child surfaces remain canvases until parent compositing; Apexify.js does not PNG-encode then decode every nested surface. This avoids a major composition roundtrip while preserving clipping and transform isolation.
The placement width/height are authoritative; a surface background must not define its own width/height.
Asset references
renderScene,renderSceneToGIF,renderSceneToVideoFrames:$refsresolve by default.- Pass
resolveAssetRefs: falsein the matching scene-render options when the scene is already resolved or when$should remain literal data. SceneBuilder.render: resolution is opt-in.
See Named assets for $$ escaping, dotted paths, Buffer refs, embedded scalar refs, and replacement semantics.
Outputs
| Output | API | Behavior |
|---|---|---|
| PNG | renderScene / SceneBuilder.render() | Validate, compose ordered layers, encode final root PNG. |
| GIF | renderSceneToGIF | Validate scene/GIF inputs, compose the scene frame, merge optional repeated tail frames, then encode GIF. |
| Video | renderSceneToVideoFrames | Validate frame configuration before wasting scene raster work; compose/merge frames, then delegate to the video creator/FFmpeg. |
Minimal builder example
Chapter guide
| # | Topic | Link |
|---|---|---|
| 1 | Scene types & builder | Types & builder |
| 2 | Image/text/path/imageBuffer | Raster & vector layers |
| 3 | Chart layers | Chart layers |
| 4 | Custom lines & surfaces | Connectors & surfaces |
| 5 | Backgrounds | Scene backgrounds |
| 6 | Rendering & asset resolution | Render scene |
| 7 | Scene → GIF | Scene → GIF |
| 8 | Scene → video | Scene → video |
| 9 | Paint order & performance | Paint order & performance |