Developer Guidelines & Constraints
When extending or writing code for Canvus, adhere to these rules to maintain codebase integrity and consistency.
1. Framework Independence
Do not introduce React, Vue, Svelte, or other UI framework dependencies. Write clean, vanilla, highly optimized TypeScript.
The SDK must remain a zero-dependency vanilla TypeScript library. Any framework-specific code belongs in the host application, not the SDK.
2. Coordinate Clarity
Two coordinate systems exist in the workspace:
| Space | Units | Used In |
|---|---|---|
| Screen space | Client pixels (clientX/clientY) | Raw mouse events, DOM overlay alignments |
| Canvas space (World) | Scaled/translated viewport units | NodeTree caches, bounding boxes (Rect), snapping, drawing |
Always use screenToCanvas and canvasToScreen from matrix.ts for conversions. Never mix coordinate spaces.
3. Strict Styling Isolation
Never apply styles directly inside ShadowMount that could bleed or pollute the host. Use SHADOW_RESET_CSS inside shadow-mount.ts to reset the mount’s environment.
4. Geometry and Tree Synchronization
- Never mutate
parentIdorchildIdsof a node directly - Always use the mutation APIs in
NodeTree(addNode,removeNode,reparentNode,reorderChild) - When mutating DOM structures (e.g., reparenting), update both the
ShadowMountDOM tree wrappers and theNodeTreemodel
5. Minimize Layout Thrashing
- Avoid calling
getBoundingClientRect()repeatedly in hot loops - Use cached values from
currentRectinsideWebHTMLNodewhenever possible - Use
suppressObserverflags inShadowMountduring programmatic node manipulations to avoid observer feedback loops
6. Pristine Flat String Exports
Ensure any new wrapper classes, metadata attributes, or alignment markers are properly cleaned and excluded from ShadowMount.extractHTML so the consumer receives completely clean user markup.
7. rAF-Throttled Rendering
When Workspace.render() is called:
- Queue a redraw pass for the next animation frame
- Do not execute canvas draws synchronously
- Multiple mutations within a single tick render as a single redraw
8. Local Development
To view changes in real-time:
- Run
npm run buildto compile the TypeScript sources - Run
npm run demoto host the test workbench locally (http://localhost:3000) - The demo workspace allows you to test:
- Multi-direction panning and cursor-anchored zooming
- Node resizing, flow reflow, alignment snapping guides
- Display style hot-swapping (Flexbox, CSS Grid, block layouts)
- The flat string bridge output logs