Skip to main content

Engine

The <KritzelEngine> is the low-level headless canvas primitive. It handles rendering, viewport management, pointer events, undo/redo, and workspace management with no built-in controls.

All methods are async and return Promises. The <KritzelEditor> exposes all the same methods.

Common props

PropertyTypeDefaultDescription
cursorTargetHTMLElementdocument.bodyElement to apply cursor styles to
globalContextMenuItemsContextMenuItem[]undefinedContext menu items for canvas background
objectContextMenuItemsContextMenuItem[]undefinedContext menu items for selected objects
lockDrawingScalebooleantrueFixed visual size for drawn objects regardless of zoom
scaleMaxnumberABSOLUTE_SCALE_MAXMaximum zoom scale
scaleMinnumberABSOLUTE_SCALE_MINMinimum zoom scale
syncConfigKritzelSyncConfigundefinedSync provider configuration
theme'light' | 'dark''light'Active theme
viewportBoundaryLeftnumber-InfinityLeft pan boundary
viewportBoundaryRightnumberInfinityRight pan boundary
viewportBoundaryTopnumber-InfinityTop pan boundary
viewportBoundaryBottomnumberInfinityBottom pan boundary
workspacesKritzelWorkspace[]undefinedOptional authoritative workspace catalog for this engine instance. Provider-only workspaces are hidden but not deleted.

Common events

EventPayload TypeDescription
isEngineReadyKritzelEngineStateEmitted when the engine is fully initialized
activeToolChangeKritzelBaseToolEmitted when the active tool changes
longpressPointerEventEmitted on long-press (useful for mobile context menus)
objectsChangeKritzelBaseObject[]Emitted when objects are added, removed, or modified
objectsAddedObjectsAddedEventEmitted when objects are added
objectsRemovedObjectsRemovedEventEmitted when objects are removed
objectsUpdatedObjectsUpdatedEventEmitted when objects are updated
objectsInViewportChangeKritzelBaseObject[]Emitted when visible objects change (after pan/zoom)
objectsSelectionChangevoidEmitted when selected objects change
undoStateChangeKritzelUndoStateEmitted when undo/redo availability changes
viewportChangeKritzelViewportStateEmitted on pan, zoom, or resize
workspacesChangeKritzelWorkspace[]Emitted when workspaces are created, updated, or deleted

Object​

Props​

PropertyTypeDescription
objectConfigKritzelObjectConfigConfigures how objects behave, in one place for all objects: which objects get a titlebar, and which can be rotated or resized. Each option is a boolean or a function of the object. See Configuring Objects.

Methods​

addObject​

Adds a new object to the canvas. The object is automatically assigned an ID, core reference, and z-index.

addObject<T extends KritzelBaseObject>(object: T): Promise<T | null>

addObjects​

Adds multiple objects in one undo step and render cycle.

addObjects<T extends KritzelBaseObject>(objects: T[]): Promise<T[]>

removeObject​

Removes an object from the canvas.

removeObject<T extends KritzelBaseObject>(object: T): Promise<T | null>

removeObjects​

Removes multiple objects in one batch operation.

removeObjects<T extends KritzelBaseObject>(objects: T[]): Promise<T[]>

updateObject​

Updates properties of an existing canvas object.

updateObject<T extends KritzelBaseObject>(object: T, updatedProperties: Partial<T>): Promise<T | null>

getObjectById​

Retrieves a canvas object by its unique ID.

getObjectById<T extends KritzelBaseObject>(id: string): Promise<T | null>

getAllObjects​

Returns all objects on the canvas across all layers.

getAllObjects<T extends KritzelBaseObject>: Promise<T[]>

findObjects​

Returns all objects matching the given predicate. Excludes internal selection-related objects.

findObjects<T extends KritzelBaseObject>(predicate: (obj: KritzelBaseObject) => boolean): Promise<T[]>

getObjectsTotalCount​

Returns the total number of objects on the canvas.

getObjectsTotalCount: Promise<number>

getObjectsInViewport​

Returns all objects currently visible within the viewport bounds.

getObjectsInViewport: Promise<KritzelBaseObject[]>

Selection​

getSelectedObjects​

Returns the currently selected objects. Returns an empty array if nothing is selected.

getSelectedObjects: Promise<KritzelBaseObject[]>

selectObjects​

Programmatically selects the given objects. Switches to the selection tool automatically.

selectObjects(objects: KritzelBaseObject[]): Promise<void>

selectAllObjectsInViewport​

Selects all objects currently visible in the viewport. Switches to the selection tool automatically.

selectAllObjectsInViewport: Promise<void>

clearSelection​

Deselects all currently selected objects.

clearSelection: Promise<void>

triggerSelectionChange​

Manually triggers the objectsSelectionChange event.

triggerSelectionChange: Promise<void>

Viewport & Navigation​

getViewport​

Returns the current viewport state including position, scale, and dimensions.

getViewport: Promise<KritzelViewportState>

setViewport​

Sets the viewport to center on the given world coordinates at the specified scale.

setViewport(x: number, y: number, scale: number): Promise<void>

panTo​

Pans the viewport to center on the given world coordinates without changing the scale.

panTo(x: number, y: number): Promise<void>

panToObject​

Pans to an object without changing scale.

panToObject(object: KritzelBaseObject): Promise<void>

zoomTo​

Zooms the viewport to the given scale. Optionally centers on a world point; if omitted, zooms around the viewport center.

zoomTo(scale: number, worldX?: number, worldY?: number): Promise<void>

zoomIn​

Zooms in by one configured step: zoomIn(): Promise<void>.

zoomOut​

Zooms out by one configured step: zoomOut(): Promise<void>.

centerObjectInViewport​

Pans and zooms the viewport to center on the given object.

centerObjectInViewport(object: KritzelBaseObject): Promise<KritzelBaseObject>

centerObjects​

Centers the combined bounds of supplied objects: centerObjects(objects: KritzelBaseObject[]): Promise<void>.

centerAllObjects​

Centers the combined bounds of all objects: centerAllObjects(): Promise<void>.

backToContent​

Pans and zooms the viewport to fit the nearest content, with padding. Useful when the user has panned away from all objects.

backToContent: Promise<boolean>

Returns true if content was found and the viewport was adjusted, false otherwise.

screenToWorld​

Converts screen-relative pixel coordinates to world coordinates.

screenToWorld(x: number, y: number): Promise<{ x: number; y: number }>

worldToScreen​

Converts world coordinates to screen-relative pixel coordinates.

worldToScreen(x: number, y: number): Promise<{ x: number; y: number }>

Workspace​

setActiveWorkspace​

Switches the active workspace shown in the engine by ID.

setActiveWorkspace(id: string): Promise<void>

createWorkspace​

Creates a new workspace and emits a workspacesChange event. Accepts a KritzelWorkspace instance.

createWorkspace(workspace: KritzelWorkspace): Promise<KritzelWorkspace | null>

updateWorkspace​

Applies a workspace as declared and emits a workspacesChange event. Metadata and viewport are always applied. When workspace.objects is set it is treated as the complete desired object set: objects are matched by ID, unknown IDs are added, known IDs are updated, and objects missing from the list are removed. Leave objects undefined to keep the current objects, or pass [] to remove them all. Updating the objects of a workspace that is not currently active requires a configured sync provider.

updateWorkspace(workspace: KritzelWorkspace): Promise<void>

deleteWorkspace​

Deletes a workspace and emits a workspacesChange event.

deleteWorkspace(workspace: KritzelWorkspace): Promise<void>

getWorkspaces​

Returns all available workspaces.

getWorkspaces: Promise<KritzelWorkspace[]>

getActiveWorkspace​

Returns the currently active workspace.

getActiveWorkspace: Promise<KritzelWorkspace>

getIsPublic​

Returns whether the active workspace is public: getIsPublic(): Promise<boolean>.

loadSharedWorkspace​

Loads a shared workspace: loadSharedWorkspace(workspaceId: string): Promise<void>.

reinitSync​

Reinitializes the active synchronization provider: reinitSync(): Promise<void>.


Tool Management​

registerTool​

Registers a new drawing tool with the engine.

registerTool(
toolName: string,
toolClass: any,
toolConfig?: KritzelBrushToolConfig | KritzelLineToolConfig | KritzelTextToolConfig | KritzelShapeToolConfig
): Promise<KritzelBaseTool>

setActiveTool​

Switches the active drawing tool. Deactivates the current tool and clears any selection.

setActiveTool(toolName: string): Promise<void>

disable​

Disables all user interaction with the engine (pointer, keyboard, etc.).

disable: Promise<void>

enable​

Re-enables user interaction after a call to disable.

enable: Promise<void>

Clipboard & Editing​

copy​

Copies the currently selected objects to the internal clipboard.

copy: Promise<void>

cut​

Cuts selected objects to the clipboard.

cut(): Promise<void>

paste​

Pastes previously copied objects at the specified world coordinates.

paste(x: number, y: number): Promise<void>

delete​

Deletes the currently selected objects from the canvas.

delete: Promise<void>

Ordering​

bringForward​

Moves an object one layer forward in the z-order, past the next layer above it. If omitted, applies to the current selection and moves each selected layer. A group is one layer, so all of its children move together; a grouped object moves within its group.

bringForward(object?: KritzelBaseObject): Promise<void>

sendBackward​

Moves an object one layer backward in the z-order, past the next layer below it. If omitted, applies to the current selection and moves each selected layer. A group is one layer, so all of its children move together; a grouped object moves within its group.

sendBackward(object?: KritzelBaseObject): Promise<void>

bringToFront​

Moves an object to the very front of the z-order. If omitted, applies to the current selection, keeping the relative order of the selected layers. A group moves together with all of its children; a grouped object moves to the front of its group.

bringToFront(object?: KritzelBaseObject): Promise<void>

sendToBack​

Moves an object to the very back of the z-order. If omitted, applies to the current selection, keeping the relative order of the selected layers. A group moves together with all of its children; a grouped object moves to the back of its group.

sendToBack(object?: KritzelBaseObject): Promise<void>

Alignment​

alignObjects​

Aligns the currently selected objects according to the specified alignment.

alignObjects(alignment: KritzelAlignment): Promise<void>

Alignment values: StartHorizontal, CenterHorizontal, EndHorizontal, StartVertical, CenterVertical, EndVertical.


Grouping​

group​

Creates a group (KritzelGroup) from the given objects. Requires at least two objects. They become one layer at the position of the topmost object, in a single undoable step. Objects are resolved by id, so stale references work; objects that are not in the active workspace are skipped with a console warning. Objects inside a common group are grouped within it; objects from different groups are grouped at the level that contains all of them. Selected objects that end up in the group are replaced by the group in the selection. Resolves to the created group, or null (with a console warning) when fewer than two objects could be grouped. To group the current selection, pass await getSelectedObjects().

group(objects: KritzelBaseObject[]): Promise<KritzelGroup | null>

ungroup​

Ungroups the given groups. Their children become individual objects again and keep their stacking order; the children of a nested group stay in its parent group. All groups are dissolved in a single undoable step. Groups are resolved by id, so stale references work; entries that are not a KritzelGroup or not in the active workspace are skipped with a console warning. Dissolved groups in the selection are replaced by their children. Resolves to the extracted children. To ungroup the current selection, pass the KritzelGroup objects from await getSelectedObjects().

ungroup(groups: KritzelGroup[]): Promise<KritzelBaseObject[]>

Undo & Redo​

undo​

Undoes the last action.

undo: Promise<void>

redo​

Redoes the last undone action.

redo: Promise<void>

Export & Import​

getScreenshot​

Captures a screenshot of the current viewport as a data URL.

getScreenshot(format?: 'png' | 'svg'): Promise<string>

exportViewportAsPng​

Exports the current viewport as a PNG file and triggers a browser download.

exportViewportAsPng: Promise<void>

exportViewportAsSvg​

Exports the current viewport as an SVG file and triggers a browser download.

exportViewportAsSvg: Promise<void>

canExportSelectedObjectAs​

Checks whether the selected object supports a format: canExportSelectedObjectAs(format: KritzelExportFormat): Promise<boolean>.

exportSelectedObject​

Exports the selected object: exportSelectedObject(format: KritzelExportFormat): Promise<void>.

getSelectedObjectSupportedExportFormats​

Returns supported selected-object formats: getSelectedObjectSupportedExportFormats(): Promise<KritzelExportFormat[]>.

exportAsJson​

Serializes the current workspace: exportAsJson(): Promise<string>.

importFromJson​

Imports a serialized workspace: importFromJson(json: string): Promise<void>.

loadObjectsFromJson​

Loads serialized objects: loadObjectsFromJson(json: string): Promise<number>.

downloadAsJson​

Downloads the current workspace: downloadAsJson(filename?: string): Promise<void>.

importFromFile​

Opens a file picker and imports a workspace: importFromFile(): Promise<void>.


Localization & Settings​

Props​

PropertyTypeDescription
fallbackLocaleLocaleCodeLocale used to resolve terms missing from the active locale.
localeLocaleCodeCurrent locale code applied to the engine.
localesKritzelLocale[]Available locale definitions with optional term overrides.
themeThemeCodeCurrent theme applied to the engine.
themesKritzelTheme[]Available themes for the engine.

Methods​

registerLocales​

Registers locale definitions: registerLocales(locales: KritzelLocale[]): Promise<void>.

setLocale​

Sets the active locale: setLocale(code: LocaleCode): Promise<void>.

getLocale​

Returns the active locale: getLocale(): Promise<LocaleCode>.

getAvailableLocales​

Returns available locale codes: getAvailableLocales(): Promise<LocaleCode[]>.

getAvailableLocaleOptions​

Returns available code/label options: getAvailableLocaleOptions(): Promise<{ code: LocaleCode; label: string }[]>.

t​

Resolves a localized term: t(key: KritzelTermKey, vars?: KritzelTermVars): Promise<string>.

getResolvedTerms​

Returns all resolved terms: getResolvedTerms(): Promise<Partial<Record<KritzelTermKey, string>>>.

saveSettings​

Persists settings: saveSettings(settings: KritzelSettingsConfig): Promise<void>.

loadSettings​

Loads persisted settings: loadSettings(): Promise<Partial<KritzelSettingsConfig> | null>.

Context Menu & Lifecycle​

Props​

PropertyTypeDescription
assetStorageConfigKritzelAssetStorageConfigConfiguration for binary asset storage providers.
cursorTargetHTMLElementElement to apply cursor styles to.
debugInfoKritzelDebugInfoControls visible debug overlays.
editorIdstringIdentifier for namespacing storage keys across engine instances.
globalContextMenuItemsContextMenuItem[]Items shown on the canvas background context menu.
isWatermarkVisiblebooleanShow or hide the "Powered by Kritzel" watermark (default true). Keeping it visible helps support the project.
lockDrawingScalebooleanKeeps drawn objects at a fixed visual size across zoom levels.
objectContextMenuItemsContextMenuItem[]Items shown for selected objects.

Events​

EventPayload TypeDescription
contextMenuStateChangeKritzelContextMenuStateEmitted when context-menu state changes.
isEngineReadyKritzelEngineStateEmitted when the engine is ready for interaction.
longpressPointerEventEmitted on touch or pen long-press.
notificationsChangeKritzelNotificationEmitted when notifications change.

Methods​

openContextMenu​

Opens a context menu: openContextMenu(options: { x: number; y: number; objectId?: string }): Promise<void>.

hideContextMenu​

Hides the context menu: hideContextMenu(): Promise<void>.

getDisplayableShortcuts​

Returns shortcuts without actions or conditions: getDisplayableShortcuts(): Promise<Omit<KritzelShortcut, "action" | "condition">[]>.

beginSceneBootstrap​

Starts scene bootstrap and hides the scene: beginSceneBootstrap(): Promise<void>.

endSceneBootstrap​

Ends scene bootstrap: endSceneBootstrap(): Promise<void>.

Agentic Cursors​

Props​

No agentic-cursor-specific props.

Events​

EventPayload TypeDescription
agenticCursorDetachedKritzelAgenticCursorDetachedEventEmitted when an agentic cursor is detached from an object, either manually or because the object was removed.

Methods​

addAgenticCursor​

Adds a programmatically controlled cursor. Agentic cursors are rendered locally only.

addAgenticCursor(options?: KritzelAgenticCursorOptions): Promise<KritzelAgenticCursor>

updateAgenticCursor​

Updates the name, color, status or visibility of an agentic cursor. Pass status: null to clear the status.

updateAgenticCursor(id: string, patch: KritzelAgenticCursorPatch): Promise<KritzelAgenticCursor>

moveAgenticCursor​

Smoothly moves an agentic cursor to world coordinates or to an anchor on an object. Object targets attach the cursor by default. Resolves when the animation ends; completed is false if the move was interrupted.

moveAgenticCursor(id: string, target: KritzelAgenticCursorTarget, options?: KritzelAgenticCursorMoveOptions): Promise<KritzelAgenticCursorMoveResult>

detachAgenticCursor​

Detaches an agentic cursor from its object. The cursor stays at its current position.

detachAgenticCursor(id: string): Promise<void>

removeAgenticCursor​

Removes an agentic cursor. A running move resolves with completed: false.

removeAgenticCursor(id: string): Promise<boolean>

clearAgenticCursors​

Removes all agentic cursors.

clearAgenticCursors(): Promise<void>

getAgenticCursor​

Returns a snapshot of an agentic cursor, or null if it doesn't exist.

getAgenticCursor(id: string): Promise<KritzelAgenticCursor | null>

getAgenticCursors​

Returns snapshots of all agentic cursors.

getAgenticCursors(): Promise<KritzelAgenticCursor[]>

Object Titlebar​

Props​

PropertyTypeDescription
objectTitlebarKritzelObjectTitlebarConfigContent of the titlebar rendered above every object for which objectConfig.hasTitlebar is true. The same configuration applies to all objects. Leave unset to disable titlebars. See Object Titlebar.

Events​

EventPayload TypeDescription
objectTitlebarActionKritzelObjectTitlebarActionEventEmitted when a button in an object titlebar is clicked or a menu item of a titlebar menu is selected, in addition to calling the optional action callback of the button or menu item.
objectTitlebarMenuStateChangeKritzelObjectTitlebarMenuStateEmitted when a titlebar menu opens or closes. Contains the resolved menu items and the menu button as anchor, so that a host can render the menu. <KritzelEditor> renders titlebar menus itself and does not re-emit this event.

Methods​

closeObjectTitlebarMenu​

Closes the open titlebar menu, if any.

closeObjectTitlebarMenu(): Promise<void>

selectObjectTitlebarMenuItem​

Selects a menu item of the open titlebar menu. Closes the menu, calls the menu item's action, and emits objectTitlebarAction with the id of the menu as elementId and menuItemId. Does nothing if no menu is open or the menu item is hidden or disabled.

selectObjectTitlebarMenuItem(menuItemId: string): Promise<void>

Use updateObject to change the name of an existing object.

Support us

Kritzel is an MIT-licensed open source project. If you like the idea of a framework-agnostic infinite canvas library and want to support our work, leave us a star or consider becoming a sponsor.