Overview
TheScene class is the central data structure in Excalidraw that manages all elements in a drawing. It provides a reactive system for element storage, retrieval, mutation, and change tracking, serving as the single source of truth for the canvas state.
Scene Architecture
The Scene maintains multiple internal data structures optimized for different access patterns:packages/element/src/Scene.ts
The Scene maintains both array and map representations of elements. Arrays preserve ordering, while maps provide O(1) lookup by element ID.
Creating a Scene
packages/element/src/Scene.ts
Element Access
Getting Elements
packages/element/src/Scene.ts
Getting Individual Elements
packages/element/src/Scene.ts
Container Relationships
packages/element/src/Scene.ts
Modifying the Scene
Replacing All Elements
The primary method for updating the scene:packages/element/src/Scene.ts
Mapping Elements
Update elements with a transformation function:packages/element/src/Scene.ts
Inserting Elements
packages/element/src/Scene.ts
Insert methods automatically sync fractional indices using
syncMovedIndices to maintain proper ordering.Mutating Elements
Mutate elements in place while triggering scene updates:packages/element/src/Scene.ts
When to Use informMutation: false
When to Use informMutation: false
Set
informMutation: false when:- Batching multiple mutations and want a single update at the end
- Making temporary changes that will be reverted
- Updating elements that aren’t in the scene (e.g., during element creation)
Selection Management
The Scene caches selected elements for performance:packages/element/src/Scene.ts
- Selection Caching
- Selection Options
The Scene maintains a sophisticated selection cache:Cache key includes:
selectedElementIdsreferenceelementsreferenceincludeBoundTextElementflagincludeElementsInFramesflag
- When
selectedElementIdschange - When scene elements change
- When selection options change
Change Tracking
Scene Nonce
The Scene generates a random nonce on each update for cache invalidation:packages/element/src/Scene.ts
Subscribing to Updates
Register callbacks to react to scene changes:packages/element/src/Scene.ts
Manual Update Trigger
packages/element/src/Scene.ts
Fractional Indices
The Scene automatically manages fractional indices for consistent element ordering:packages/element/src/Scene.ts
Fractional Index Implementation
Fractional Index Implementation
Excalidraw uses the fractional indexing algorithm for element ordering:
- Indices are strings like
"a0","a1","a0V", etc. - Allow inserting between elements without reindexing
- Critical for collaboration where multiple users insert elements
- Automatically validated in development/test environments
Scene Lifecycle
packages/element/src/Scene.ts
Integration with App Component
The Scene is integrated into the main App component:Performance Considerations
- Element Access
- Batch Updates
- Selection Caching
- Validation
Scene Data Type
When updating scenes through the API, use theSceneData type:
packages/excalidraw/types.ts
Best Practices
Element Management
Element Management
- Use
mapElementsfor transformations instead of manual array mapping - Always prefer map lookups over array iteration for finding elements
- Use
getNonDeletedElementswhen you only need active elements - Call
destroy()when disposing of Scene instances
Performance
Performance
- Skip validation with
{ skipValidation: true }when loading trusted data - Batch element updates into a single
replaceAllElementscall - Use
informMutation: falsewhen batching mutations - Cache selection options objects to benefit from selection cache
Change Tracking
Change Tracking
- Subscribe to scene updates only when necessary
- Always unsubscribe in cleanup functions
- Use scene nonce for cache invalidation in renderers
- Avoid triggering updates during render cycles
Collaboration
Collaboration
- Never mutate elements directly without using Scene methods
- Rely on fractional indices for element ordering
- Let Scene manage index synchronization automatically
- Use version and versionNonce for conflict resolution
Common Patterns
Bulk Element Update
Filtering and Replacing
Adding New Elements
Removing Elements
Related Concepts
- Elements - Element structure and properties
- App State - Global application state
- Collaboration - Multi-user scene synchronization