hydro-draw
v0.0.2
Published
Embeddable infinite canvas Viewer & Editor SDK with programmatic object state, animations, and high-fidelity SVG export.
Readme
hydro-draw (v0.0.2)
Embeddable infinite canvas Viewer and Editor SDK with programmatic object state control, interpolation & choreography animations, color themes, and 1:1 Canvas-parity SVG export.
Installation & Build
npm install [email protected]Building both the Studio app and the publishable SDK package (dist-sdk/ ESM, Browser IIFE, UMD/CJS, and TypeScript types):
npm run buildQuickstart
ESM / Bundlers (Vite, Next.js, Webpack, Rollup)
import { createHydroDraw } from 'hydro-draw';Direct Browser <script type="module"> (Zero Bundler)
<div id="canvas-viewer" style="width: 800px; height: 480px;"></div>
<script type="module">
import { createHydroDraw } from './dist-sdk/hydro-draw.js';
const viewer = createHydroDraw({
container: '#canvas-viewer',
mode: 'viewer',
themeMode: 'dark',
colorTheme: 'tokyo-night',
fitOnMount: true,
initialShapes: [
{ id: 'box-1', type: 'rectangle', x: 80, y: 120, width: 170, height: 84, text: 'Hello ESM' }
]
});
</script>Classic Browser <script> Global (window.HydroDraw)
<div id="canvas-viewer" style="width: 800px; height: 480px;"></div>
<script src="./dist-sdk/hydro-draw.browser.js"></script>
<script>
const viewer = window.HydroDraw.createHydroDraw({
container: '#canvas-viewer',
mode: 'viewer',
initialShapes: [
{ id: 'box-1', type: 'rectangle', x: 80, y: 120, width: 170, height: 84, text: 'Hello Browser' }
]
});
</script>Embed a Read-Only Interactive Viewer
import { createHydroDraw } from 'hydro-draw';
const viewer = createHydroDraw({
container: '#canvas-viewer',
mode: 'viewer',
themeMode: 'dark',
colorTheme: 'tokyo-night',
fitOnMount: true,
initialShapes: [
{
id: 'service-a',
type: 'rectangle',
x: 80,
y: 120,
width: 170,
height: 84,
text: 'API Gateway',
style: { color: 'blue', fill: 'semi', font: 'sans' },
},
{
id: 'service-b',
type: 'ellipse',
x: 340,
y: 110,
width: 150,
height: 100,
text: 'Worker',
style: { color: 'mint', fill: 'pattern', font: 'mono' },
},
{
id: 'edge-1',
type: 'arrow',
start: { x: 250, y: 162 },
end: { x: 340, y: 162 },
bend: -24,
startBinding: { shapeId: 'service-a', normalizedAnchor: { x: 1, y: 0.5 } },
endBinding: { shapeId: 'service-b', normalizedAnchor: { x: 0, y: 0.5 } },
style: { color: 'purple', arrowheads: 'end' },
},
],
onShapeClick: ({ shape }) => {
console.log('Clicked:', shape.id);
},
});Embed a Full Interactive Editor
import { createHydroDraw } from 'hydro-draw';
const editor = createHydroDraw({
container: document.getElementById('editor-root')!,
mode: 'editor',
showUI: true,
onChange: (shapes) => {
localStorage.setItem('board-shapes', JSON.stringify(shapes));
},
});
// Switch dynamically between viewer & editor at runtime
editor.setMode('viewer');Programmatic State & Object Control API
| Method | Description |
| --- | --- |
| getShapes() / getShape(id) | Read current shapes or look up one shape by ID |
| addShape(input, recordHistory?) | Insert a shape with smart defaults; syncs bound arrows automatically |
| addShapes(inputs, recordHistory?) | Batch-insert multiple shapes |
| setShapes(shapesOrUpdater, recordHistory?) | Replace all shapes or transform list |
| updateShape(id, patchOrFn, recordHistory?) | Partially patch geometry, style, text, or custom properties |
| updateShapes(updatesOrFn, recordHistory?) | Batch-update multiple shapes cleanly |
| removeShape(id) / removeShapes(ids) | Delete shapes & stop active animations |
| clearShapes() | Remove all shapes from the instance |
| setShapeStyle(id, stylePatch) | Update color, fill, dash, size, font, opacity, arrowheads, stroke/fill overrides |
| setShapeText(id, text) | Update text content on shapes or sticky notes |
| setShapePosition(id, x, y) | Move shape (including freehand draw points) and sync bound arrows |
| setShapeSize(id, width, height) | Resize box/geometric/text/note shapes |
| setShapeRotation(id, radians) | Rotate shape in radians |
| setShapeVisibility(id, visible) | Hide or show shapes (hidden skips rendering & hit-testing) |
| bringToFront(id) / sendToBack(id) | Move shape to top or bottom of z-order stack |
| bringForward(id) / sendBackward(id) | Step shape one layer up or down in z-order stack |
| groupShapes(ids) / ungroupShapes(groupId) | Group or ungroup shapes |
| duplicateShapes(ids?, offset?) | Clone shapes and select the duplicates |
| alignShapes(ids, type) / alignSelected(type) | Align (left, center-h, right, top, center-v, bottom) |
| distributeShapes(ids, dir) / packShapes(ids, dir, gap?) | Evenly distribute or pack shapes (horizontal / vertical) |
| rotateShapes90(ids, dir) / flipShapes(ids, axis) | Rotate 90° (cw/ccw) or flip (h/v) shapes cleanly |
| stretchShapes(ids, axis) | Stretch shapes to collective horizontal or vertical bounds |
| lockShapes(ids) / unlockShapes(ids) / toggleLockShapes(ids) | Lock/unlock shapes against interactive edits |
| frameShapes(ids, label?) | Wrap shapes in a labeled dashed container frame |
| analyzeContrast(ids?) / getLowContrastShapeIds(threshold?) | Audit WCAG contrast ratios across text-bearing shapes |
| fixTextContrast(modeOrIds?, mode?) / fixShapeContrast(id, mode?) | Auto-fix low contrast text via high-contrast color or outline halo ('auto' \| 'color' \| 'outline') |
Animation Engine
Smoothly animate object coordinates, dimensions, rotation, opacity, draw stroke points, arrow endpoints & curve bend, font size, dot size, and hex colors (strokeColor, fillColor, textColor).
const handle = sdk.animateShape(
'service-a',
{
x: 240,
y: 150,
rotation: Math.PI / 6,
opacity: 0.9,
strokeColor: '#06b6d4',
},
{
duration: 700,
easing: 'spring', // linear | easeInQuad | easeOutQuad | easeInOutQuad | easeInCubic | easeOutCubic | easeInOutCubic | easeInOutSine | easeOutBack | spring
yoyo: true,
loop: 2,
onUpdate: (shape, progress) => {},
onComplete: (shape) => {},
}
);
// Wait, pause, resume, or cancel
handle.pause();
handle.resume();
await handle.finished;
// Parallel & sequential choreographies
await sdk.animateShapes([
{ id: 'service-a', target: { y: 80 }, options: { duration: 500, easing: 'easeOutBack' } },
{ id: 'edge-1', target: { bend: 40 }, options: { duration: 500, easing: 'easeInOutCubic' } },
]);
await sdk.animateSequence([
{ id: 'service-a', target: { x: 180 }, options: { duration: 300 } },
{ id: 'service-b', target: { rotation: Math.PI }, options: { duration: 400 } },
]);Camera, Themes & Events
panTo(x, y, { duration?, easing? })zoomTo(zoom, { duration?, easing? })/resetZoom()/zoomToFit()/focusShape(id)setThemeMode('light' | 'dark' | 'system')/toggleDarkMode()setColorTheme(name)(default,solarized,dracula,nord,monokai,gruvbox,catppuccin,one-dark,tokyo-night,everforest,github,flexoki,night-owl,high-contrast)on('change' | 'selectionChange' | 'cameraChange' | 'modeChange' | 'themeChange' | 'shapeClick' | 'shapeDoubleClick' | 'shapeHover' | 'destroy', fn)
High-Fidelity SVG, PNG & Document Export
exportSVG() uses a 1:1 pure serializer aligned with the Canvas 2D renderer, supporting dynamic calligraphy outline polygons, sketchy wobble strokes, diagonal hatch <pattern> fills, opacity separation, rotated shapes, curved/elbow arrows & arrowheads, sticky note shadows/clipping, and multiline text with stroke halos.
const svg = sdk.exportSVG({ transparent: false, padding: 32 });
const pngDataUrl = sdk.toDataURL({ scale: 2, transparent: true });
const doc = sdk.exportDocument('Architecture Diagram');
sdk.loadDocument(doc);