Overview
Elements are the fundamental building blocks of any Excalidraw drawing. Every shape, line, text, image, and frame in Excalidraw is represented as an element object with a consistent base structure and type-specific properties.Element Types
Excalidraw supports multiple element types, each representing a different drawing primitive:- Shape Elements
- Linear Elements
- Container Elements
- Media Elements
Basic geometric shapes that form the foundation of drawings:
- rectangle - Rectangular shapes with configurable roundness
- diamond - Diamond/rhombus shapes
- ellipse - Circular and elliptical shapes
Base Element Structure
All element types extend a common base structure defined in_ExcalidrawElementBase:
packages/element/src/types.ts
Key Property Explanations
Key Property Explanations
- id: Unique identifier for the element, generated using
randomId() - seed: Random integer used to seed shape generation in rough.js, ensuring consistent rendering across sessions
- version: Integer that increments on each change, used for collaboration reconciliation
- versionNonce: Random integer regenerated on each change, used for deterministic reconciliation when versions are identical
- index: Fractional index string (using fractional indexing) for ordering in multiplayer scenarios
- updated: Epoch timestamp (ms) of last element update
- boundElements: Array of elements bound to this element (e.g., arrows, text labels)
- roundness: Configures corner rounding -
nullfor no rounding, or an object withtype(“adaptive” or “proportional”) and optionalvalue
Creating Elements
Excalidraw provides factory functions for creating new elements with proper initialization:Element factory functions automatically initialize required properties like
id, version, versionNonce, seed, and updated.Mutating Elements
Excalidraw provides two approaches for updating elements:1. Immutable Updates with newElementWith
Creates a new element object with updates applied:
packages/element/src/mutateElement.ts
2. Mutable Updates with mutateElement
Mutates an element in place for performance:
packages/element/src/mutateElement.ts
Version Bumping
Manually bump an element’s version without other changes:Element Properties
Styling Properties
- Colors
- Stroke
- Fill
Geometric Properties
Relationship Properties
Element State Management
Deletion
Elements are soft-deleted by setting theisDeleted flag:
Locking
Locked elements cannot be modified by user interactions:Ordering with Fractional Indices
Elements use fractional indices for consistent ordering in collaborative scenarios:packages/element/src/types.ts
Fractional indices are automatically synced with array order by
syncMovedIndices() and syncInvalidIndices() functions. You typically don’t need to manipulate them directly.Element Bindings
Excalidraw supports two types of element bindings:Arrow Bindings
Arrows can bind to bindable elements (rectangles, diamonds, ellipses, text, images, frames):packages/element/src/types.ts
Text Container Bindings
Text elements can bind to containers:Type Guards
Excalidraw provides comprehensive type guards for element types:Element Maps
For performance, Excalidraw uses Map structures to store elements:packages/element/src/types.ts
Custom Data
Elements support custom data storage for extensions:Best Practices
Performance Optimization
Performance Optimization
- Use
mutateElementfor batch updates instead of creating new elements - Access elements through Scene maps rather than iterating arrays
- Filter non-deleted elements once and reuse the filtered collection
- Use type guards early to avoid runtime checks
Collaboration Safety
Collaboration Safety
- Never mutate
versionorversionNoncemanually - Always use
mutateElementornewElementWithto ensure proper versioning - Respect element
lockedstate in custom interactions - Use fractional indices for ordering, don’t rely on array position
Type Safety
Type Safety
- Use specific element types rather than
ExcalidrawElementwhen possible - Leverage type guards to narrow element types
- Use branded types (
FileId,FractionalIndex) for type safety - Prefer
NonDeleted<T>types when working with active elements
Related Concepts
- Scene - Managing collections of elements
- App State - Global application state management
- Collaboration - Multi-user element synchronization