Skip to main content

Element Utilities

Utility functions for creating, mutating, and working with Excalidraw elements.

Import

Element Creation

newElement

Creates a new generic Excalidraw element.
opts.type
string
required
Element type (e.g., “rectangle”, “ellipse”, “diamond”)
opts.x
number
required
X coordinate of the element
opts.y
number
required
Y coordinate of the element
opts.width
number
default:"0"
Width of the element
opts.height
number
default:"0"
Height of the element
opts.angle
Radians
default:"0"
Rotation angle in radians
opts.strokeColor
string
Stroke color for the element
opts.backgroundColor
string
Background fill color
opts.fillStyle
string
Fill style (“solid”, “hachure”, “cross-hatch”)
opts.strokeWidth
number
Width of the stroke
opts.roughness
number
Roughness level for hand-drawn appearance
opts.opacity
number
Opacity value (0-100)
element
ExcalidrawElement
The newly created element with all properties initialized
Example:

newTextElement

Creates a new text element with automatic dimension calculations.
opts.text
string
required
Text content to display
opts.fontSize
number
Font size in pixels
opts.fontFamily
FontFamilyValues
Font family identifier (FONT_FAMILY constant)
opts.textAlign
'left' | 'center' | 'right'
Horizontal text alignment
opts.verticalAlign
'top' | 'middle'
Vertical text alignment
opts.containerId
string | null
ID of container element (for bound text)
opts.autoResize
boolean
default:"true"
Whether text should auto-resize
Example:

newLinearElement

Creates a line or arrow element.
opts.type
'line' | 'arrow'
required
Type of linear element
opts.points
LocalPoint[]
Array of points [x, y] defining the line path
Example:

newArrowElement

Creates an arrow element with optional arrowheads.
opts.startArrowhead
Arrowhead | null
Arrowhead style for start point (“arrow”, “dot”, “bar”, etc.)
opts.endArrowhead
Arrowhead | null
Arrowhead style for end point
opts.elbowed
boolean
Whether the arrow should use elbow routing
Example:

newImageElement

Creates an image element.
opts.fileId
string | null
ID of the file in BinaryFiles
opts.status
'pending' | 'saved' | 'error'
Loading status of the image
opts.scale
[number, number]
Scale factors [x, y] for the image

newFrameElement

Creates a frame element for grouping.
opts.name
string | null
Optional name for the frame

Element Mutation

mutateElement

Mutates an existing element with updates and bumps its version.
element
ExcalidrawElement
required
The element to mutate
elementsMap
ElementsMap
required
Map of all elements (for context)
updates
Partial<TElement>
required
Properties to update (excludes ‘id’ and ‘updated’)
options.isDragging
boolean
Whether the element is being dragged
element
TElement
The mutated element with updated version and versionNonce
Example:

newElementWith

Creates a new element instance with updates (immutable operation).
element
ExcalidrawElement
required
The base element
updates
Partial<TElement>
required
Properties to update
force
boolean
default:"false"
Force regeneration even if no changes detected
element
TElement
New element instance with updates applied
Example:

bumpVersion

Bumps element version, versionNonce, and updated timestamp.
element
ExcalidrawElement
required
The element to bump
version
number
Optional specific version to set (will be incremented by 1)
Example:

Element Queries

getNonDeletedElements

Filters out deleted elements from an array.
elements
readonly ExcalidrawElement[]
required
Array of elements to filter
elements
NonDeleted<ExcalidrawElement>[]
Array of non-deleted elements
Example:

getSceneVersion

Calculates the scene version by summing element versions.
elements
readonly ExcalidrawElement[]
required
Elements to calculate version from
version
number
Sum of all element versions
Note: This function is deprecated. Use hashElementsVersion instead for better performance.

hashElementsVersion

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

hashString

Hashes a string using the djb2 algorithm.
s
string
required
String to hash
hash
number
Unsigned 32-bit integer hash

Element Bounds

getElementAbsoluteCoords

Gets absolute coordinates of an element in scene coordinates.
element
ExcalidrawElement
required
The element to get coordinates for
elementsMap
ElementsMap
required
Map of all elements
includeBoundText
boolean
default:"false"
Whether to include bound text in calculations
coords
[x1, y1, x2, y2, cx, cy]
Array containing: [x1, y1, x2, y2, centerX, centerY]
Example:

getElementBounds

Gets the axis-aligned bounding box for an element.
element
ExcalidrawElement
required
The element to get bounds for
elementsMap
ElementsMap
required
Map of all elements
nonRotated
boolean
default:"false"
Whether to get bounds without rotation
bounds
[minX, minY, maxX, maxY]
Bounding box coordinates

getCommonBounds

Gets the common bounding box for multiple elements.
elements
ElementsMapOrArray
required
Elements to get common bounds for
elementsMap
ElementsMap
Optional elements map for context
bounds
[minX, minY, maxX, maxY]
Common bounding box containing all elements
Example:

getVisibleSceneBounds

Gets the visible bounds of the canvas viewport in scene coordinates.
state.scrollX
number
required
Horizontal scroll offset
state.scrollY
number
required
Vertical scroll offset
state.width
number
required
Canvas width
state.height
number
required
Canvas height
state.zoom
{ value: number }
required
Current zoom value
bounds
[sceneX, sceneY, sceneX2, sceneY2]
Visible scene bounds

Element Text

refreshTextDimensions

Recalculates text element dimensions based on content and container.
textElement
ExcalidrawTextElement
required
The text element to refresh
container
ExcalidrawTextContainer | null
required
Container element (if text is bound)
elementsMap
ElementsMap
required
Map of all elements
text
string
Optional text override
dimensions
object
Updated text, position, and dimensions

Bounding Box Utilities

elementsOverlappingBBox

Finds elements that overlap with, contain, or are inside a bounding box.
elements
readonly NonDeletedExcalidrawElement[]
required
Elements to check against the bounding box.
bounds
Bounds | ExcalidrawElement
required
Bounding box as [x1, y1, x2, y2] or an element to use its bounds.
errorMargin
number
default:"0"
Safety offset in pixels to expand the bounding box.
type
'overlap' | 'contain' | 'inside'
required
  • overlap: Elements overlapping or inside bounds
  • contain: Elements inside bounds or bounds inside elements
  • inside: Elements inside bounds only
elements
NonDeletedExcalidrawElement[]
Array of elements matching the criteria.
Example:

isElementInsideBBox

Checks if an element is inside a bounding box.
element
NonDeletedExcalidrawElement
required
Element to check.
bbox
Bounds
required
Bounding box as [x1, y1, x2, y2].
eitherDirection
boolean
default:"false"
If true, also returns true if bbox is inside element.
result
boolean
true if element is inside the bounding box.
Example:

elementPartiallyOverlapsWithOrContainsBBox

Checks if an element partially overlaps with or contains a bounding box.
element
NonDeletedExcalidrawElement
required
Element to check.
bbox
Bounds
required
Bounding box as [x1, y1, x2, y2].
result
boolean
true if element overlaps or contains the bounding box.
Example:

Data Utilities

getDataURL

Converts a Blob or File to a Data URL (async).
file
Blob | File
required
File or Blob to convert.
dataURL
DataURL
Base64-encoded data URL string.
Example:

Library Utilities

parseLibraryTokensFromUrl

Extracts library installation URL and ID token from the current page URL.
result
object | null
Object with libraryUrl and idToken, or null if not found.
Example:

useHandleLibrary

Hook for handling library loading, updates, and persistence.
excalidrawAPI
ExcalidrawImperativeAPI | null
required
Excalidraw API instance.
validateLibraryUrl
function
Custom validator for library installation URLs.
adapter
LibraryPersistenceAdapter
Adapter for persisting library to storage.
migrationAdapter
LibraryMigrationAdapter
Optional adapter for migrating from legacy storage.
Example:

Text Utilities

setCustomTextMetricsProvider

Sets a custom text metrics provider for measuring text dimensions.
provider
function
required
Function that measures text and returns width/height.
Example:

Types

CaptureUpdateAction

Enum-like object that controls when element updates are captured in the undo/redo history.
Values:
  • 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.
Example:

See Also