Skip to main content

Scene Utilities

Utilities for managing scenes, element collections, and scene state in Excalidraw.

Scene Class

The Scene class is the core container for managing Excalidraw elements and their state.

Constructor

Creates a new Scene instance.
ElementsMapOrArray | null
Initial elements (array or Map)
boolean
Skip validation of fractional indices
Example:

Scene Methods

getElementsIncludingDeleted

Returns all elements including deleted ones.
ExcalidrawElement[]
All elements in the scene

getNonDeletedElements

Returns only non-deleted elements.
NonDeleted<ExcalidrawElement>[]
All non-deleted elements

getNonDeletedElementsMap

Returns a Map of non-deleted elements indexed by ID.
Map<string, ExcalidrawElement>
Map of element IDs to elements
Example:

getElement

Gets a specific element by ID (including deleted).
string
required
Element ID to retrieve
ExcalidrawElement | null
The element or null if not found

getNonDeletedElement

Gets a non-deleted element by ID.
string
required
Element ID to retrieve
NonDeleted<ExcalidrawElement> | null
The non-deleted element or null

getSelectedElements

Returns currently selected elements.
Map<string, true>
required
Map of selected element IDs
ElementsMapOrArray
Custom elements to use (defaults to scene elements)
boolean
Include text bound to selected containers
boolean
Include elements inside selected frames
NonDeleted<ExcalidrawElement>[]
Selected elements
Example:

insertElement

Inserts a single element into the scene.
ExcalidrawElement
required
Element to insert
Example:

insertElements

Inserts multiple elements into the scene.
ExcalidrawElement[]
required
Elements to insert

insertElementAtIndex

Inserts an element at a specific index in the z-order.
ExcalidrawElement
required
Element to insert
number
required
Z-index position (0 = bottom)

replaceAllElements

Replaces all elements in the scene.
ElementsMapOrArray
required
New elements (array or Map)
boolean
Skip fractional index validation
Example:

mapElements

Maps over all elements, optionally modifying them.
function
required
Function that receives each element and returns the element (modified or unchanged)
boolean
Whether any elements were modified
Example:

mutateElement

Mutates a single element and triggers scene update.
ExcalidrawElement
required
Element to mutate
Partial<TElement>
required
Properties to update
boolean
default:"true"
Whether to trigger scene update
boolean
default:"false"
Whether element is being dragged
ExcalidrawElement
The mutated element
Example:

getElementIndex

Gets the z-index of an element.
string
required
Element ID
number
Z-index of the element (-1 if not found)

getContainerElement

Gets the container element for a bound element.
ExcalidrawElement
required
Element with containerId property
ExcalidrawElement | null
The container element or null

getSceneNonce

Gets a random nonce that changes with each scene update.
number | undefined
Random integer regenerated on each update
Usage: Used for cache invalidation in renderers.

onUpdate

Registers a callback for scene updates.
function
required
Function to call on scene updates
function
Function to unregister the callback
Example:

triggerUpdate

Manually triggers scene update callbacks.

destroy

Cleans up the scene and removes all elements.
Example:

Scene Version Functions

getSceneVersion

Calculates scene version by summing element versions.
readonly ExcalidrawElement[]
required
Elements to calculate version from
number
Sum of all element versions
Note: Deprecated in favor of hashElementsVersion.

hashElementsVersion

Generates a hash of elements’ versionNonce values.
ElementsMapOrArray
required
Elements to hash (array or Map)
number
Unsigned 32-bit integer hash
Example:

Complete Scene Example

See Also