Fabric.js Document Engine

Search documentation

Find a page or section

中文

createDocumentEngine

View as Markdown

Connect the engine to your Fabric.js canvas.

createDocumentEngine connects the engine to an existing Fabric.js canvas and returns a DocumentEngine. Below are all createDocumentEngine options.

function createDocumentEngine(options: DocumentEngineOptions): DocumentEngine

Connects the engine to an existing Fabric.js canvas. The canvas stays yours: the engine only listens to it, and changes it when you load, undo or restore.

Usage

import { createDocumentEngine } from "fabricjs-document-engine";
import { createLocalStorage } from "fabricjs-document-engine/storage";
 
const engine = createDocumentEngine({
  canvas,
  storage: createLocalStorage(),
  autosave: true,
  history: { limit: 50 },
});

Only canvas is required. Without storage, you can still save with toDocument() and load with loadDocument(), but save(), load(id), autosave and versions are not available.

createDocumentEngine options

NameTypeDefaultDescription
canvas*StaticCanvas—Your Fabric.js canvas, a Canvas or a StaticCanvas.
storageDocumentStoragenoneWhere documents are saved and loaded. Needed for load, save, autosave and versions.
customObjectsCustomObjectDefinition[][]Your own Fabric classes and the extra properties they need to keep.
documentNewDocumentOptionsa new idThe id and metadata of the first, empty document.
historyHistoryOptions{ limit: 100, maxBytes: 64 MB }How many undo steps to keep, and how much memory they may use.
autosaveboolean | AutosaveOptionsoffSaves on its own after edits. true means { delay: 1000, maxWait: 10000 }. Needs storage.
saveRetryRetryOptions{ attempts: 3, baseDelay: 500, maxDelay: 8000 }How failed saves are retried.
assetsAssetOptions{}How images and fonts are resolved, checked and uploaded.
recoveryRecoveryOptionsoffKeeps local copies of unsaved work, so it can be restored after a crash or refresh.
versionsVersionOptions{ autoEvery: 0, keepAuto: 20 }Keeps an automatic version every N saves, and how many of them to keep.
limitsContentLimits{ maxObjects: 50000, maxDepth: 100 }Safety limits for documents that are loaded or imported.

Nested options

Autosave

NameTypeDefaultDescription
delaynumber1000Save after this many milliseconds without an edit.
maxWaitnumber10000Save at the latest this many milliseconds after the first unsaved edit, even while the user keeps editing.

Save retries

NameTypeDefaultDescription
attemptsnumber3How many times to retry.
baseDelaynumber500The first wait, in milliseconds.
maxDelaynumber8000The longest wait, in milliseconds.

History

The history limit and memory budget for undo:

NameTypeDefaultDescription
limitnumber100How many undo steps to keep.
maxBytesnumber64 MBThe memory undo history may use. The oldest steps are dropped first.

Assets

NameTypeDefaultDescription
resolveUrl(url: string) => string | Promise<string>—Rewrites each stored image URL before loading, for example to sign it or map it to a CDN.
replaceMissingImage(image: ImageAsset) => string | null | undefined | Promise<string | null | undefined>—Supplies a replacement URL for an image that did not load. Return null to leave it missing.
upload(request: UploadRequest) => Promise<string>—Stores blob: and data: images while saving, and returns their permanent URL.
loadFontFontLoader—Loads a font before text is created.
checkImagesbooleantrueSet to false to skip loading images during the check.
requireFontsbooleanfalseFail loading with MISSING_FONTS, and export with MISSING_FONT, instead of warning.

Custom objects

NameTypeDefaultDescription
fabricClass*{ type: string; fromObject?: unknown; }—Your Fabric class. It needs a static type.
propertiesstring[][]The extra properties to save and load with the object.

Recovery

NameTypeDefaultDescription
store*RecoveryStore—Where copies are kept, such as createIndexedDbRecovery().
intervalnumber2000Write a copy at most once every this many milliseconds while there are unsaved changes.

Versions

NameTypeDefaultDescription
keepAutonumber20How many automatic versions to keep. Named versions are never pruned.
autoEverynumber0 (off)Keep an automatic version after every N successful saves.

Limits

NameTypeDefaultDescription
maxObjectsnumber50000Refuse documents with more objects than this.
maxDepthnumber100Refuse documents nested deeper than this.
isAllowedUrl(url: string) => booleanisSafeImageUrlDecides which image addresses may be fetched, after resolveUrl.

Most createDocumentEngine options are read once, when the engine is created. To change them, create a new engine.

createDocumentEngine options | Fabric.js Document Engine