Advanced / Scene · guide
Scene — types & SceneBuilder
Current Apexify.js 6.0.0 documentation for Scene — types & SceneBuilder.
SceneRenderInput
Used by renderScene, renderSceneToGIF, and renderSceneToVideoFrames.
| Field | Type | Notes |
|---|---|---|
width | number | Positive root width in pixels. |
height | number | Positive root height in pixels. |
background | SceneBackground = Omit<CanvasConfig, "width" | "height"> | Paint configuration only; root dimensions come exclusively from width / height. |
layers | SceneLayer[] | Deterministic bottom → top paint order. |
SceneRenderResult is a PNG Buffer.
SceneRenderOptions
Passed to renderScene / SceneBuilder.render and through GIF/video helpers as sceneRender.
| Field | Behavior |
|---|---|
validate | Deprecated compatibility property. Safety validation is mandatory; this value is ignored and cannot disable validation. |
maxSurfaceDepth | Optional stricter nesting cap. It may not exceed the configured runtime maxSceneDepth (default 32). |
resolveAssetRefs | renderScene / scene→GIF / scene→video default true. SceneBuilder.render defaults false unless you opt in. |
There is no trusted-input validation bypass. Call painter.validateSceneRenderInput(input, options?) when you want an explicit preflight before a larger workflow; the renderer validates again at its own boundary.
Validation covers dimensions, aggregate scene pixel budget, total layers/surfaces/images/text/charts/remote assets, total text content, nested depth, image/text domain validation, finite transforms, and opacity ranges.
SceneBuilder
createScene() returns a mutable builder around scene descriptions with copy-on-ingress and snapshot isolation.
When you pass a background, layer object, nested array/object, or Buffer into the builder, the builder captures an isolated composition copy. Later mutation of the caller-owned value does not silently change the builder.
Background
| Method | Returns | Role |
|---|---|---|
setBackground(bg) | this | Replace the root background with a copied value. |
clearBackground() | this | Remove the root background. |
Stack editing
| Method | Returns | Role |
|---|---|---|
addLayer(layer) | this | Append one copied layer. |
addLayers(layers) | this | Append copied layers in order. |
insertLayer(index, layer) | this | Insert at an exact valid index. index === layerCount appends. |
insertLayers(index, layers) | this | Insert several layers preserving order. |
insertBefore(index, layer) | this | Insert directly below the layer currently at index. |
insertAfter(index, layer) | this | Insert directly above the layer currently at index. |
replaceLayer(index, layer) | this | Replace exactly one layer. |
replaceLayers(layers) | this | Replace the whole stack. |
removeLayer(index) | this | Remove exactly one layer. |
moveLayer(fromIndex, toIndex) | this | Reorder one layer. |
clearLayers() | this | Empty the stack. |
Invalid indices throw ApexifyInputError; indices are not silently clamped.
Snapshot & render
| Method | Returns | Role |
|---|---|---|
toRenderInput() | SceneRenderInput | Deep composition snapshot, including copied Buffers. Mutating it does not mutate the builder. |
render(options?) | Promise<Buffer> | Renders a fresh isolated snapshot. $ asset resolution is opt-in on the builder. |
Read-only builder properties: width, height, and layerCount.
createScene overloads
Config object
Width + height
Passing only numeric width is invalid; numeric construction requires both width and height.
Editor-style ordering example
Every render recomposites the current stack; stack methods edit descriptions/order rather than caching a partially painted raster.