Canvas Model

The scene, camera, snapping and undo behind every canvas, as pure functions you test and run on a server.

Preview

See the source below for this component’s API.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/canvas-model

Usage

import { commit, type Edit, emptyHistory, undo } from "./ui/uicontext/components/canvas/History.ts"import { alignChanges, moveChanges, type Shape } from "./ui/uicontext/components/canvas/Scene.ts"import { snapMove } from "./ui/uicontext/components/canvas/Snap.ts"
export const nudgeThenAlign = (shapes: ReadonlyArray<Shape>) => {	let state: Edit = { shapes, selection: ["a", "b"], history: emptyHistory }	state = commit(state, [moveChanges(state.shapes, ["a"], 8, 0)])	state = commit(state, alignChanges(state.shapes, ["a", "b"], "top"))	return undo(state)}export const snapped = snapMove({ x: 103, y: 0, w: 50, h: 50 }, [{ x: 0, y: 0, w: 100, h: 100 }], 5)

Source