@canvist/scene
canvist canvas scenes on a server, for Node, Deno, Bun, and Workers: load scene JSON or Yjs state, run canvas commands and inspector edits, and export SVG and PNG with the renderer @canvist/canvas draws with. Use it for thumbnails, social cards, and designs an agent writes.
npm install @canvist/sceneimport { createSceneEngine } from "@canvist/scene";
const engine = await createSceneEngine();
using doc = engine.fromJson({
type: "canvist-scene",
version: 1,
nodes: [{
type: "page",
children: [{
type: "rectangle",
props: { size: [120, 80], fills: [{ type: "solid", color: "#3b2cf5" }] },
}],
}],
});
const png = doc.exportPng({ scale: 2 });- Scenes:
engine.create()starts from one empty page,engine.fromJson(scene)reads scene JSON, nested or flat, anddoc.toJson()returns the canonical flat form. - Edits:
run(name, args)runs a canvas command,select(ids)andselection()set and read the selection,inspect()andsetField(key, value)read and edit it as the inspector does, andundo()reverts the latest change.engine.metadata()lists the tools, commands, node types, and inspector sections. - Export:
exportSvg(options)andexportPng(options)export the scene, or the nodesoptionsnames.addImage(asset, width, height, rgba)provides an image’s pixels; without them, an image is drawn as a grey placeholder. - Collaboration:
share(),encodeState(),applyUpdate(update), andtakeUpdates(), andengine.fromYjs(state)joins a peer’s scene. - Workers: runtimes without a file system pass the module they imported:
createSceneEngine({ wasm }), withwasmfrom@canvist/scene/wasm/canvist_scene_bg.wasm.
The scene types (SceneJson, ExportOptions, NodeId, …) are exported here, and from @canvist/core/scene for code that only needs the types. Scenes live in their own WebAssembly module, so document packages such as @canvist/headless stay small.
Development
(cd ../canvist-core && npm ci && npm run build) # the package this one builds on
npm ci
npm run build:wasm # the scene module into wasm/
npm test # tests in Node
deno task test # the same, in Deno
npm run check:types # tsc --noEmit
npm run build # JavaScript and declarations for npm, into dist/