Element Utilities
Utility functions for creating, mutating, and working with Excalidraw elements.Import
Element Creation
newElement
Creates a new generic Excalidraw element.Element type (e.g., “rectangle”, “ellipse”, “diamond”)
X coordinate of the element
Y coordinate of the element
Width of the element
Height of the element
Rotation angle in radians
Stroke color for the element
Background fill color
Fill style (“solid”, “hachure”, “cross-hatch”)
Width of the stroke
Roughness level for hand-drawn appearance
Opacity value (0-100)
The newly created element with all properties initialized
newTextElement
Creates a new text element with automatic dimension calculations.Text content to display
Font size in pixels
Font family identifier (FONT_FAMILY constant)
Horizontal text alignment
Vertical text alignment
ID of container element (for bound text)
Whether text should auto-resize
newLinearElement
Creates a line or arrow element.Type of linear element
Array of points [x, y] defining the line path
newArrowElement
Creates an arrow element with optional arrowheads.Arrowhead style for start point (“arrow”, “dot”, “bar”, etc.)
Arrowhead style for end point
Whether the arrow should use elbow routing
newImageElement
Creates an image element.ID of the file in BinaryFiles
Loading status of the image
Scale factors [x, y] for the image
newFrameElement
Creates a frame element for grouping.Optional name for the frame
Element Mutation
mutateElement
Mutates an existing element with updates and bumps its version.The element to mutate
Map of all elements (for context)
Properties to update (excludes ‘id’ and ‘updated’)
Whether the element is being dragged
The mutated element with updated version and versionNonce
newElementWith
Creates a new element instance with updates (immutable operation).The base element
Properties to update
Force regeneration even if no changes detected
New element instance with updates applied
bumpVersion
Bumps element version, versionNonce, and updated timestamp.The element to bump
Optional specific version to set (will be incremented by 1)
Element Queries
getNonDeletedElements
Filters out deleted elements from an array.Array of elements to filter
Array of non-deleted elements
getSceneVersion
Calculates the scene version by summing element versions.Elements to calculate version from
Sum of all element versions
hashElementsVersion instead for better performance.
hashElementsVersion
Generates a hash of elements’ versionNonce values using the djb2 algorithm.Elements to hash (array or Map)
Unsigned 32-bit integer hash
hashString
Hashes a string using the djb2 algorithm.String to hash
Unsigned 32-bit integer hash
Element Bounds
getElementAbsoluteCoords
Gets absolute coordinates of an element in scene coordinates.The element to get coordinates for
Map of all elements
Whether to include bound text in calculations
Array containing: [x1, y1, x2, y2, centerX, centerY]
getElementBounds
Gets the axis-aligned bounding box for an element.The element to get bounds for
Map of all elements
Whether to get bounds without rotation
Bounding box coordinates
getCommonBounds
Gets the common bounding box for multiple elements.Elements to get common bounds for
Optional elements map for context
Common bounding box containing all elements
getVisibleSceneBounds
Gets the visible bounds of the canvas viewport in scene coordinates.Horizontal scroll offset
Vertical scroll offset
Canvas width
Canvas height
Current zoom value
Visible scene bounds
Element Text
refreshTextDimensions
Recalculates text element dimensions based on content and container.The text element to refresh
Container element (if text is bound)
Map of all elements
Optional text override
Updated text, position, and dimensions
Bounding Box Utilities
elementsOverlappingBBox
Finds elements that overlap with, contain, or are inside a bounding box.Elements to check against the bounding box.
Bounding box as
[x1, y1, x2, y2] or an element to use its bounds.Safety offset in pixels to expand the bounding box.
overlap: Elements overlapping or inside boundscontain: Elements inside bounds or bounds inside elementsinside: Elements inside bounds only
Array of elements matching the criteria.
isElementInsideBBox
Checks if an element is inside a bounding box.Element to check.
Bounding box as
[x1, y1, x2, y2].If
true, also returns true if bbox is inside element.true if element is inside the bounding box.elementPartiallyOverlapsWithOrContainsBBox
Checks if an element partially overlaps with or contains a bounding box.Element to check.
Bounding box as
[x1, y1, x2, y2].true if element overlaps or contains the bounding box.Data Utilities
getDataURL
Converts a Blob or File to a Data URL (async).File or Blob to convert.
Base64-encoded data URL string.
Library Utilities
parseLibraryTokensFromUrl
Extracts library installation URL and ID token from the current page URL.Object with
libraryUrl and idToken, or null if not found.useHandleLibrary
Hook for handling library loading, updates, and persistence.Excalidraw API instance.
Custom validator for library installation URLs.
Adapter for persisting library to storage.
Optional adapter for migrating from legacy storage.
Text Utilities
setCustomTextMetricsProvider
Sets a custom text metrics provider for measuring text dimensions.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