Element Utilities
Utility functions for creating, mutating, and working with Excalidraw elements.Import
Element Creation
newElement
Creates a new generic Excalidraw element.string
required
Element type (e.g., “rectangle”, “ellipse”, “diamond”)
number
required
X coordinate of the element
number
required
Y coordinate of the element
number
default:"0"
Width of the element
number
default:"0"
Height of the element
Radians
default:"0"
Rotation angle in radians
string
Stroke color for the element
string
Background fill color
string
Fill style (“solid”, “hachure”, “cross-hatch”)
number
Width of the stroke
number
Roughness level for hand-drawn appearance
number
Opacity value (0-100)
ExcalidrawElement
The newly created element with all properties initialized
newTextElement
Creates a new text element with automatic dimension calculations.string
required
Text content to display
number
Font size in pixels
FontFamilyValues
Font family identifier (FONT_FAMILY constant)
'left' | 'center' | 'right'
Horizontal text alignment
'top' | 'middle'
Vertical text alignment
string | null
ID of container element (for bound text)
boolean
default:"true"
Whether text should auto-resize
newLinearElement
Creates a line or arrow element.'line' | 'arrow'
required
Type of linear element
LocalPoint[]
Array of points [x, y] defining the line path
newArrowElement
Creates an arrow element with optional arrowheads.Arrowhead | null
Arrowhead style for start point (“arrow”, “dot”, “bar”, etc.)
Arrowhead | null
Arrowhead style for end point
boolean
Whether the arrow should use elbow routing
newImageElement
Creates an image element.string | null
ID of the file in BinaryFiles
'pending' | 'saved' | 'error'
Loading status of the image
[number, number]
Scale factors [x, y] for the image
newFrameElement
Creates a frame element for grouping.string | null
Optional name for the frame
Element Mutation
mutateElement
Mutates an existing element with updates and bumps its version.ExcalidrawElement
required
The element to mutate
ElementsMap
required
Map of all elements (for context)
Partial<TElement>
required
Properties to update (excludes ‘id’ and ‘updated’)
boolean
Whether the element is being dragged
TElement
The mutated element with updated version and versionNonce
newElementWith
Creates a new element instance with updates (immutable operation).ExcalidrawElement
required
The base element
Partial<TElement>
required
Properties to update
boolean
default:"false"
Force regeneration even if no changes detected
TElement
New element instance with updates applied
bumpVersion
Bumps element version, versionNonce, and updated timestamp.ExcalidrawElement
required
The element to bump
number
Optional specific version to set (will be incremented by 1)
Element Queries
getNonDeletedElements
Filters out deleted elements from an array.readonly ExcalidrawElement[]
required
Array of elements to filter
NonDeleted<ExcalidrawElement>[]
Array of non-deleted elements
getSceneVersion
Calculates the scene version by summing element versions.readonly ExcalidrawElement[]
required
Elements to calculate version from
number
Sum of all element versions
hashElementsVersion instead for better performance.
hashElementsVersion
Generates a hash of elements’ versionNonce values using the djb2 algorithm.ElementsMapOrArray
required
Elements to hash (array or Map)
number
Unsigned 32-bit integer hash
hashString
Hashes a string using the djb2 algorithm.string
required
String to hash
number
Unsigned 32-bit integer hash
Element Bounds
getElementAbsoluteCoords
Gets absolute coordinates of an element in scene coordinates.ExcalidrawElement
required
The element to get coordinates for
ElementsMap
required
Map of all elements
boolean
default:"false"
Whether to include bound text in calculations
[x1, y1, x2, y2, cx, cy]
Array containing: [x1, y1, x2, y2, centerX, centerY]
getElementBounds
Gets the axis-aligned bounding box for an element.ExcalidrawElement
required
The element to get bounds for
ElementsMap
required
Map of all elements
boolean
default:"false"
Whether to get bounds without rotation
[minX, minY, maxX, maxY]
Bounding box coordinates
getCommonBounds
Gets the common bounding box for multiple elements.ElementsMapOrArray
required
Elements to get common bounds for
ElementsMap
Optional elements map for context
[minX, minY, maxX, maxY]
Common bounding box containing all elements
getVisibleSceneBounds
Gets the visible bounds of the canvas viewport in scene coordinates.number
required
Horizontal scroll offset
number
required
Vertical scroll offset
number
required
Canvas width
number
required
Canvas height
{ value: number }
required
Current zoom value
[sceneX, sceneY, sceneX2, sceneY2]
Visible scene bounds
Element Text
refreshTextDimensions
Recalculates text element dimensions based on content and container.ExcalidrawTextElement
required
The text element to refresh
ExcalidrawTextContainer | null
required
Container element (if text is bound)
ElementsMap
required
Map of all elements
string
Optional text override
object
Updated text, position, and dimensions
Bounding Box Utilities
elementsOverlappingBBox
Finds elements that overlap with, contain, or are inside a bounding box.readonly NonDeletedExcalidrawElement[]
required
Elements to check against the bounding box.
Bounds | ExcalidrawElement
required
Bounding box as
[x1, y1, x2, y2] or an element to use its bounds.number
default:"0"
Safety offset in pixels to expand the bounding box.
'overlap' | 'contain' | 'inside'
required
overlap: Elements overlapping or inside boundscontain: Elements inside bounds or bounds inside elementsinside: Elements inside bounds only
NonDeletedExcalidrawElement[]
Array of elements matching the criteria.
isElementInsideBBox
Checks if an element is inside a bounding box.NonDeletedExcalidrawElement
required
Element to check.
Bounds
required
Bounding box as
[x1, y1, x2, y2].boolean
default:"false"
If
true, also returns true if bbox is inside element.boolean
true if element is inside the bounding box.elementPartiallyOverlapsWithOrContainsBBox
Checks if an element partially overlaps with or contains a bounding box.NonDeletedExcalidrawElement
required
Element to check.
Bounds
required
Bounding box as
[x1, y1, x2, y2].boolean
true if element overlaps or contains the bounding box.Data Utilities
getDataURL
Converts a Blob or File to a Data URL (async).Blob | File
required
File or Blob to convert.
DataURL
Base64-encoded data URL string.
Library Utilities
parseLibraryTokensFromUrl
Extracts library installation URL and ID token from the current page URL.object | null
Object with
libraryUrl and idToken, or null if not found.useHandleLibrary
Hook for handling library loading, updates, and persistence.ExcalidrawImperativeAPI | null
required
Excalidraw API instance.
function
Custom validator for library installation URLs.
LibraryPersistenceAdapter
Adapter for persisting library to storage.
LibraryMigrationAdapter
Optional adapter for migrating from legacy storage.
Text Utilities
setCustomTextMetricsProvider
Sets a custom text metrics provider for measuring text dimensions.function
required
Function that measures text and returns width/height.
Types
CaptureUpdateAction
Enum-like object that controls when element updates are captured in the undo/redo history.IMMEDIATELY- Updates are immediately undoable. Use for most local updates.NEVER- Updates never make it to undo/redo stack. Use for remote updates or scene initialization.EVENTUALLY- Updates will eventually be captured as part of a future increment.
See Also
- Export Utils - Functions for exporting elements
- Scene Utils - Scene management utilities
- Restore Utils - Data restoration functions