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

NameTypeDefaultDescription
canvasElement*HTMLCanvasElementCanvas element for Fabric.js. Must be created programmatically with document.createElement('canvas').
widthnumber1080Initial canvas width in pixels.
heightnumber1080Initial canvas height in pixels.
backgroundColorstring'#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

NameTypeDefaultDescription
addShape(type, options?)stringAdds a shape ('rect', 'circle', 'triangle', 'line', 'polygon') to the canvas. Returns the object ID.
addText(text, options?)stringAdds 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)voidRemoves 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

NameTypeDefaultDescription
getSelectedObjects()EditorObjectInfo[]Returns an array of info objects for all currently selected objects.
selectObject(id)voidProgrammatically selects the object with the given ID.
deselectAll()voidDeselects 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

NameTypeDefaultDescription
getZoom()numberReturns the current zoom level (1 = 100%).
setZoom(level)voidSets the zoom level. Pass 1 for 100%, 2 for 200%, etc.
getCanvasSize(){ width: number; height: number }Returns the current canvas dimensions.
setCanvasSize(size)voidSets 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

NameTypeDefaultDescription
undo()voidReverses the last operation on the command stack.
redo()voidRe-applies the last undone operation.
canUndo()booleanReturns true if there are operations to undo.
canRedo()booleanReturns 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

NameTypeDefaultDescription
toJSON()stringSerializes 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

NameTypeDefaultDescription
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

NameTypeDefaultDescription
setDrawingMode(enabled)voidEnables or disables freehand drawing mode on the canvas.
updateBrush(config)voidUpdates 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

NameTypeDefaultDescription
applyTextShadow(id, config | null)voidApplies or removes a shadow on a text object. Config: { color, blur, offsetX, offsetY }. Pass null to remove.
applyTextOutline(id, config | null)voidApplies or removes a stroke outline on a text object. Config: { color, width }. Pass null to remove.
applyTextDecoration(id, config)voidSets 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

NameTypeDefaultDescription
applyGradientFill(id, config)voidApplies 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

NameTypeDefaultDescription
applyProperties(id, properties)voidUpdates 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

NameTypeDefaultDescription
bringForward(id)voidMoves the object one layer up in the stack.
sendBackward(id)voidMoves the object one layer down in the stack.
bringToFront(id)voidMoves the object to the top of the layer stack.
sendToBack(id)voidMoves 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

NameTypeDefaultDescription
getCanvas()fabric.CanvasReturns the underlying Fabric.js Canvas instance.
getEventBus()EventBusReturns the EventBus for subscribing to editor events.
getStore()StoreApiReturns the Zustand store for reading/subscribing to state.
getPluginRegistry()PluginRegistryReturns the plugin registry for inspecting loaded plugins.
getExportManager()ExportManagerReturns the export manager for programmatic export control.
getCanvasManager()CanvasManagerReturns the canvas manager for low-level canvas operations.
getTemplateManager()TemplateManagerReturns the template manager for loading/saving templates.
getPageManager()PageManagerReturns the page manager for multi-page operations.
getVersionManager()VersionManagerReturns 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

NameTypeDefaultDescription
registerPlugin(plugin)Promise<void>Registers an editor plugin (e.g., export format). Returns a promise that resolves when the plugin is initialized.
destroy()voidDisposes the engine and releases all resources. Call this when removing the editor from the page.