Scene Utilities
Utilities for managing scenes, element collections, and scene state in Excalidraw.Scene Class
TheScene 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
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
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
insertElement
Inserts a single element into the scene.ExcalidrawElement
required
Element to insert
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
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
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
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
onUpdate
Registers a callback for scene updates.function
required
Function to call on scene updates
function
Function to unregister the callback
triggerUpdate
Manually triggers scene update callbacks.destroy
Cleans up the scene and removes all elements.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
hashElementsVersion.
hashElementsVersion
Generates a hash of elements’ versionNonce values.ElementsMapOrArray
required
Elements to hash (array or Map)
number
Unsigned 32-bit integer hash
Complete Scene Example
See Also
- Element Utils - Creating and manipulating elements
- Export Utils - Exporting scenes
- Restore Utils - Loading and restoring data