EditorEngine API Reference
Complete API reference for the EditorEngine class — the central entry point for all editor operations.
Constructor
Creating an Enginetypescript
import { EditorEngine } from '@design-on-web/core';
const canvasEl = document.createElement('canvas');
const engine = new EditorEngine({
canvasElement: canvasEl,
width: 1080,
height: 1080,
backgroundColor: '#ffffff',
});EditorEngineConfig
| Name | Type | Default | Description |
|---|---|---|---|
canvasElement* | HTMLCanvasElement | — | Canvas element for Fabric.js. Must be created programmatically with document.createElement('canvas'). |
width | number | 1080 | Initial canvas width in pixels. |
height | number | 1080 | Initial canvas height in pixels. |
backgroundColor | string | '#ffffff' | Initial canvas background color. |
Shape Operations
Adding Objectstypescript
// Add a rectangle
const rectId = engine.addShape('rect', {
left: 100, top: 100,
width: 200, height: 150,
fill: '#4f46e5',
stroke: '#312e81',
strokeWidth: 2,
rx: 8, ry: 8, // rounded corners
});
// Add a circle
const circleId = engine.addShape('circle', {
left: 300, top: 200,
radius: 75,
fill: '#ef4444',
});
// Add text
const textId = engine.addText('Hello World', {
left: 100, top: 400,
fontSize: 48,
fill: '#1f2937',
fontFamily: 'Inter',
fontWeight: 'bold',
});
// Add image (async — waits for image to load)
const imageId = await engine.addImage('https://example.com/photo.jpg', {
left: 50, top: 50,
scaleX: 0.5,
scaleY: 0.5,
});Methods
| Name | Type | Default | Description |
|---|---|---|---|
addShape(type, options?) | string | — | Adds a shape ('rect', 'circle', 'triangle', 'line', 'polygon') to the canvas. Returns the object ID. |
addText(text, options?) | string | — | Adds a text object to the canvas. Returns the object ID. |
addImage(src, options?) | Promise<string> | — | Loads an image from URL or data URI and adds it to the canvas. Returns a promise that resolves with the object ID. |
removeObject(id) | void | — | Removes the object with the given ID from the canvas. |
Selection
Selectiontypescript
// Get currently selected objects
const selected = engine.getSelectedObjects();
// => [{ id: 'abc123', type: 'rect', left: 100, top: 100, ... }]
// Select a specific object by ID
engine.selectObject('abc123');
// Deselect everything
engine.deselectAll();
// Remove an object
engine.removeObject('abc123');Methods
| Name | Type | Default | Description |
|---|---|---|---|
getSelectedObjects() | EditorObjectInfo[] | — | Returns an array of info objects for all currently selected objects. |
selectObject(id) | void | — | Programmatically selects the object with the given ID. |
deselectAll() | void | — | Deselects all objects on the canvas. |
Canvas State
Zoom and Canvas Sizetypescript
// Zoom
const currentZoom = engine.getZoom(); // => 1
engine.setZoom(1.5); // 150%
// Canvas size
const size = engine.getCanvasSize(); // => { width: 1080, height: 1080 }
engine.setCanvasSize({ width: 1920, height: 1080 });Methods
| Name | Type | Default | Description |
|---|---|---|---|
getZoom() | number | — | Returns the current zoom level (1 = 100%). |
setZoom(level) | void | — | Sets the zoom level. Pass 1 for 100%, 2 for 200%, etc. |
getCanvasSize() | { width: number; height: number } | — | Returns the current canvas dimensions. |
setCanvasSize(size) | void | — | Sets the canvas dimensions. Accepts { width: number; height: number }. |
History (Undo/Redo)
Undo / Redotypescript
// Undo the last operation
engine.undo();
// Redo the last undone operation
engine.redo();
// Check availability
if (engine.canUndo()) engine.undo();
if (engine.canRedo()) engine.redo();Methods
| Name | Type | Default | Description |
|---|---|---|---|
undo() | void | — | Reverses the last operation on the command stack. |
redo() | void | — | Re-applies the last undone operation. |
canUndo() | boolean | — | Returns true if there are operations to undo. |
canRedo() | boolean | — | Returns true if there are operations to redo. |
Serialization
Save and Loadtypescript
// Save canvas state to JSON string
const json = engine.toJSON();
localStorage.setItem('myDesign', json);
// Load canvas state from JSON string
const saved = localStorage.getItem('myDesign');
if (saved) {
await engine.loadJSON(saved);
}Methods
| Name | Type | Default | Description |
|---|---|---|---|
toJSON() | string | — | Serializes the entire canvas state (all pages, objects, settings) to a JSON string. |
loadJSON(json) | Promise<void> | — | Restores canvas state from a previously serialized JSON string. |
Export
Exportingtypescript
// Basic export
const pngBlob = await engine.exportAs('png');
// With options
const hiResBlob = await engine.exportAs('png', { multiplier: 2 });
const jpegBlob = await engine.exportAs('jpeg', { quality: 0.85 });
const pdfBlob = await engine.exportAs('pdf');Methods
| Name | Type | Default | Description |
|---|---|---|---|
exportAs(format, options?) | Promise<Blob> | — | Exports the canvas to the specified format. Returns a Blob. Supported formats: 'png', 'jpeg', 'webp', 'svg', 'pdf', 'psd', 'tiff', 'json'. |
See the Export API Reference for format-specific options and plugin registration.
Drawing
Freehand Drawingtypescript
// Enable freehand drawing mode
engine.setDrawingMode(true);
// Configure the brush
engine.updateBrush({
color: '#ff0000',
width: 5,
opacity: 0.8,
});
// Disable drawing mode
engine.setDrawingMode(false);Methods
| Name | Type | Default | Description |
|---|---|---|---|
setDrawingMode(enabled) | void | — | Enables or disables freehand drawing mode on the canvas. |
updateBrush(config) | void | — | Updates brush settings. Config: { color?: string, width?: number, opacity?: number }. |
Text Effects
Text Effectstypescript
// Apply text shadow
engine.applyTextShadow(textId, {
color: 'rgba(0,0,0,0.5)',
blur: 4,
offsetX: 2,
offsetY: 2,
});
// Remove text shadow
engine.applyTextShadow(textId, null);
// Apply text outline (stroke)
engine.applyTextOutline(textId, {
color: '#000000',
width: 2,
});
// Remove text outline
engine.applyTextOutline(textId, null);
// Apply text decoration
engine.applyTextDecoration(textId, {
underline: true,
linethrough: false,
});Methods
| Name | Type | Default | Description |
|---|---|---|---|
applyTextShadow(id, config | null) | void | — | Applies or removes a shadow on a text object. Config: { color, blur, offsetX, offsetY }. Pass null to remove. |
applyTextOutline(id, config | null) | void | — | Applies or removes a stroke outline on a text object. Config: { color, width }. Pass null to remove. |
applyTextDecoration(id, config) | void | — | Sets text decoration. Config: { underline?: boolean, linethrough?: boolean }. |
Gradient
Gradient Filltypescript
// Apply gradient fill
engine.applyGradientFill(rectId, {
type: 'linear', // 'linear' | 'radial'
angle: 90,
colorStops: [
{ offset: 0, color: '#4f46e5' },
{ offset: 1, color: '#7c3aed' },
],
});Methods
| Name | Type | Default | Description |
|---|---|---|---|
applyGradientFill(id, config) | void | — | Applies a gradient fill to an object. Config: { type: 'linear' | 'radial', angle?: number, colorStops: Array<{ offset: number, color: string }> }. |
Properties
Updating Propertiestypescript
// Update any object property
engine.applyProperties(rectId, {
fill: '#10b981',
opacity: 0.8,
angle: 45,
scaleX: 1.5,
scaleY: 1.5,
});Methods
| Name | Type | Default | Description |
|---|---|---|---|
applyProperties(id, properties) | void | — | Updates arbitrary properties on an object. Accepts any valid Fabric.js object properties: fill, opacity, angle, scaleX, scaleY, left, top, etc. |
Z-Order (Layering)
Z-Ordertypescript
// Move forward/backward in the layer stack
engine.bringForward(rectId);
engine.sendBackward(rectId);
engine.bringToFront(rectId);
engine.sendToBack(rectId);Methods
| Name | Type | Default | Description |
|---|---|---|---|
bringForward(id) | void | — | Moves the object one layer up in the stack. |
sendBackward(id) | void | — | Moves the object one layer down in the stack. |
bringToFront(id) | void | — | Moves the object to the top of the layer stack. |
sendToBack(id) | void | — | Moves the object to the bottom of the layer stack. |
Accessors
Internal Accessorstypescript
// Canvas instance (Fabric.js Canvas)
const canvas = engine.getCanvas();
// EventBus for pub/sub events
const eventBus = engine.getEventBus();
// Zustand store for state management
const store = engine.getStore();
// Plugin registry
const plugins = engine.getPluginRegistry();
// Manager instances
const exportManager = engine.getExportManager();
const canvasManager = engine.getCanvasManager();
const templateManager = engine.getTemplateManager();
const pageManager = engine.getPageManager();
const versionManager = engine.getVersionManager();Methods
| Name | Type | Default | Description |
|---|---|---|---|
getCanvas() | fabric.Canvas | — | Returns the underlying Fabric.js Canvas instance. |
getEventBus() | EventBus | — | Returns the EventBus for subscribing to editor events. |
getStore() | StoreApi | — | Returns the Zustand store for reading/subscribing to state. |
getPluginRegistry() | PluginRegistry | — | Returns the plugin registry for inspecting loaded plugins. |
getExportManager() | ExportManager | — | Returns the export manager for programmatic export control. |
getCanvasManager() | CanvasManager | — | Returns the canvas manager for low-level canvas operations. |
getTemplateManager() | TemplateManager | — | Returns the template manager for loading/saving templates. |
getPageManager() | PageManager | — | Returns the page manager for multi-page operations. |
getVersionManager() | VersionManager | — | Returns the version manager for version history operations. |
⚠️
Internal APIs
The accessor methods expose internal managers. Their APIs may change between minor versions. For most use cases, prefer the top-level EditorEngine methods instead.
Lifecycle
Register Plugintypescript
import { PsdExportPlugin } from '@design-on-web/export-psd';
await engine.registerPlugin(PsdExportPlugin);Destroytypescript
// Release all resources
engine.destroy();Methods
| Name | Type | Default | Description |
|---|---|---|---|
registerPlugin(plugin) | Promise<void> | — | Registers an editor plugin (e.g., export format). Returns a promise that resolves when the plugin is initialized. |
destroy() | void | — | Disposes the engine and releases all resources. Call this when removing the editor from the page. |