Getting Started with vanilla-diagram
<vn-diagram> is a canvas of shapes and the connectors between them, which
the user edits by mouse, touch or keyboard; <vn-diagram-palette> is the
palette they drag shapes from. Each step shows the running example and its
whole source, a complete page you can copy.
What you need. One script, vanilla-diagram.bundle.js, from the build's
dist/vanilla-components/latest/ folder. It holds the diagram and the
palette, and adds their stylesheets to the page. It needs no other component.
Step 4's toolbar is a component of its own, vanilla-diagram-toolbar.bundle.js,
from the same folder.
1. Your first diagram
Put a palette and a diagram on the page; the palette's diagram attribute
names the diagram by id. Once window.VanillaDiagramReady and the element's
ready() resolve, initializeDiagram() builds an empty canvas.
Drag a shape from the palette, or click one to add it in the middle. Hover a shape to see its ports, and drag from a port to another shape to connect them; the palette's last group picks the connector kind. Ctrl+Z undoes.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Your first diagram</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
body { display: flex; gap: 8px; box-sizing: border-box; padding: 8px; }
vn-diagram-palette { flex: 0 0 170px; }
vn-diagram { flex: 1 1 auto; min-width: 0; }
</style>
</head>
<body>
<!-- The palette names its diagram by id. -->
<vn-diagram-palette diagram="first"></vn-diagram-palette>
<vn-diagram id="first"></vn-diagram>
<script src="path/to/vanilla-diagram/vanilla-diagram.bundle.js"></script>
<script type="module">
await window.VanillaDiagramReady;
const diagram = document.getElementById('first');
await diagram.ready();
// An empty canvas: drag a shape from the palette, or click one to add
// it in the middle. Hover a shape to see its ports, and drag from a
// port to another shape to connect them. Ctrl+Z undoes.
diagram.initializeDiagram();
</script>
</body>
</html>
2. Load and save through a data manager
A node is a box in world px (x, y, width, height) drawn by a symbol,
with an optional text; a connector joins two nodes' ports (top, right,
bottom, left on the built-in shapes). VanillaDiagramStaticDataManager
holds them in memory: attach it with setDataManager() and load with
loadDiagramAsync().
Every change is a command: vn-diagram-changed announces it, and the diagram
hands it to the data manager's saveChanges(); vn-diagram-saved follows.
A manager of your own would send it to a server. toJSON() is the whole
diagram as a document the static manager accepts.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Load and save through a data manager</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
body { display: grid; grid-template-columns: 170px 1fr 260px; gap: 8px; box-sizing: border-box; padding: 8px; }
vn-diagram { min-height: 0; }
pre { margin: 0; padding: 8px; overflow: auto; font-size: 12px; background: #f5f5f5; border-radius: 6px; }
</style>
</head>
<body>
<vn-diagram-palette diagram="flow"></vn-diagram-palette>
<vn-diagram id="flow"></vn-diagram>
<pre id="log" aria-live="polite"></pre>
<script src="path/to/vanilla-diagram/vanilla-diagram.bundle.js"></script>
<script type="module">
await window.VanillaDiagramReady;
const diagram = document.getElementById('flow');
const log = document.getElementById('log');
const write = (line) => { log.textContent = line + '\n' + log.textContent; };
await diagram.ready();
// Nodes are boxes in world px, each drawn by a symbol; connectors join
// two nodes' ports (top, right, bottom, left on the basic shapes).
const nodes = [
{ id: 'start', symbol: 'ellipse', x: 0, y: 0, width: 120, height: 72, text: 'Start' },
{ id: 'check', symbol: 'diamond', x: 10, y: 130, width: 100, height: 80, text: 'Valid?' },
{ id: 'done', symbol: 'rounded-rectangle', x: 200, y: 140, width: 120, height: 60, text: 'Done' },
];
const connectors = [
{ id: 'c1', type: 'elbow', from: { node: 'start', port: 'bottom' }, to: { node: 'check', port: 'top' } },
{ id: 'c2', type: 'elbow', from: { node: 'check', port: 'right' }, to: { node: 'done', port: 'left' } },
];
// The static manager keeps its own copy and stores every change set it
// is given: a host's own manager would send them to a server.
const manager = new VanillaDiagramStaticDataManager({ nodes, connectors });
diagram.initializeDiagram();
diagram.setDataManager(manager);
// Every change is a command: announced, then stored by the manager.
diagram.addEventListener('vn-diagram-changed', (e) => write(e.detail.command.label + ' (' + e.detail.source + ')'));
diagram.addEventListener('vn-diagram-saved', () => write(' saved: ' + manager.getNodes().length + ' nodes, '
+ manager.getConnectors().length + ' connectors'));
await diagram.loadDiagramAsync();
// toJSON() is the whole diagram, as a document the static manager accepts.
write('Loaded: ' + JSON.stringify(diagram.toJSON()).length + ' characters of JSON');
</script>
</body>
</html>
3. Symbols of your own
A symbol is declarative, an SVG path of the node's size, or drawn by a
render callback that returns any SVG element; either can name its ports
and its text box. VanillaDiagramElement.registerSymbolSet() registers a set
of them, and the palette's sets attribute lists the sets to show, one group
each.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Custom symbols</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
body { display: flex; gap: 8px; box-sizing: border-box; padding: 8px; }
vn-diagram-palette { flex: 0 0 170px; }
vn-diagram { flex: 1 1 auto; min-width: 0; }
</style>
</head>
<body>
<!-- One group per set, in this order, then the connector kinds. -->
<vn-diagram-palette diagram="custom" sets="shop basic"></vn-diagram-palette>
<vn-diagram id="custom"></vn-diagram>
<script src="path/to/vanilla-diagram/vanilla-diagram.bundle.js"></script>
<script type="module">
await window.VanillaDiagramReady;
// Declarative: an SVG path of the node's size, so it keeps its shape
// at any size. Its ports are fractions of the box.
const tag = {
name: 'tag',
label: 'Price tag',
defaultSize: { width: 120, height: 56 },
path: (w, h) => `M0 0H${w - h / 2}L${w} ${h / 2}L${w - h / 2} ${h}H0Z`,
ports: [
{ id: 'left', x: 0, y: 0.5, side: 'left' },
{ id: 'right', x: 1, y: 0.5, side: 'right' },
],
textBox: { x: 0, y: 0, width: 0.8, height: 1 },
};
// Drawn by a callback: any SVG element, here a box with a band across
// its top. It paints with the theme's tokens.
const card = {
name: 'card',
label: 'Card',
defaultSize: { width: 140, height: 80 },
render: (w, h) => {
const ns = 'http://www.w3.org/2000/svg';
const g = document.createElementNS(ns, 'g');
const box = document.createElementNS(ns, 'rect');
box.setAttribute('width', w);
box.setAttribute('height', h);
box.setAttribute('rx', 6);
const band = document.createElementNS(ns, 'rect');
band.setAttribute('width', w);
band.setAttribute('height', Math.min(14, h / 4));
band.setAttribute('rx', 6);
band.style.fill = 'var(--vn-diagram-node-stroke)';
g.append(box, band);
return g;
},
textBox: { x: 0, y: 0.2, width: 1, height: 0.8 },
};
// A set registers its definitions; the palette shows it as a group.
VanillaDiagramElement.registerSymbolSet('shop', [tag, card], { label: 'Shop' });
const diagram = document.getElementById('custom');
await diagram.ready();
diagram.initializeDiagram();
diagram.setDataManager(new VanillaDiagramStaticDataManager({
nodes: [
{ id: 'item', symbol: 'card', x: 0, y: 0, width: 140, height: 80, text: 'Blue mug' },
{ id: 'price', symbol: 'tag', x: 220, y: 12, width: 120, height: 56, text: '12.50' },
],
connectors: [
{ id: 'c1', type: 'straight', from: { node: 'item', port: 'right' }, to: { node: 'price', port: 'left' } },
],
}));
await diagram.loadDiagramAsync();
</script>
</body>
</html>
4. A toolbar, colours and export
<vn-diagram-toolbar>, from its own script, names its diagram by id, as the
palette does. With nothing inside it draws its default bar: a status line at
the left, which setStatusMessage() fills, and at the right undo and redo,
zoom, delete, bring to front and send to back, a colour menu and two exports,
each enabled when it can run. Its
own guide chooses the
commands, their style, and adds commands of your own. A node takes one of ten colour slots,
col1 to col10, by name (swatch: 'col3'); the theme picks the colours. The
palette's swatches attribute gives each listed shape a slot, so it adds in
that colour, and grid-size turns on a grid that drags snap to.
Double-click a shape or a connector, or press Enter or F2, to edit its text;
Ctrl+C and Ctrl+V copy and paste. The export buttons download diagram.svg
and diagram.png; toSVG() and toPNG() give the picture to your own code.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>A toolbar, colours and export</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
body { display: grid; grid-template-columns: 170px 1fr; grid-template-rows: auto 1fr; gap: 8px; box-sizing: border-box; padding: 8px; }
vn-diagram-toolbar { grid-column: 1 / 3; }
vn-diagram { min-height: 0; }
</style>
</head>
<body>
<!-- The toolbar and the palette both name the diagram by id. -->
<vn-diagram-toolbar diagram="flow"></vn-diagram-toolbar>
<!-- swatches: each listed shape adds in its own colour slot. -->
<vn-diagram-palette diagram="flow" swatches="rectangle:col1 rounded-rectangle:col2 ellipse:col3 diamond:col7"></vn-diagram-palette>
<!-- A 20px grid: moves, resizes, nudges and drops snap to it. -->
<vn-diagram id="flow" grid-size="20"></vn-diagram>
<script src="path/to/vanilla-diagram/vanilla-diagram.bundle.js"></script>
<!-- The toolbar is a component of its own, loaded beside the diagram. -->
<script src="path/to/vanilla-diagram-toolbar/vanilla-diagram-toolbar.bundle.js"></script>
<script type="module">
await window.VanillaDiagramReady;
const diagram = document.getElementById('flow');
await diagram.ready();
// A node takes a colour slot by name, col1 to col10: the theme picks the
// colour. A connector may carry a label.
const nodes = [
{ id: 'start', symbol: 'ellipse', x: 0, y: 0, width: 120, height: 72, text: 'Start', swatch: 'col3' },
{ id: 'check', symbol: 'diamond', x: 10, y: 140, width: 100, height: 80, text: 'Valid?', swatch: 'col7' },
{ id: 'done', symbol: 'rounded-rectangle', x: 220, y: 150, width: 120, height: 60, text: 'Done', swatch: 'col2' },
];
const connectors = [
{ id: 'c1', type: 'elbow', from: { node: 'start', port: 'bottom' }, to: { node: 'check', port: 'top' } },
{ id: 'c2', type: 'elbow', from: { node: 'check', port: 'right' }, to: { node: 'done', port: 'left' }, label: 'yes' },
];
diagram.initializeDiagram();
diagram.setDataManager(new VanillaDiagramStaticDataManager({ nodes, connectors }));
await diagram.loadDiagramAsync();
// Double-click a shape or a connector (or press Enter or F2) to edit its
// text. Select shapes and pick a colour from the toolbar's colour menu;
// Ctrl+C and Ctrl+V copy and paste. The toolbar's last two buttons
// download the drawing as diagram.svg and diagram.png; a host that wants
// the picture itself cancels the command and calls toSVG() or toPNG().
// The status line at the toolbar's left is the host's: here, each change.
const toolbar = document.querySelector('vn-diagram-toolbar');
toolbar.setStatusMessage('Select shapes, then pick a colour.');
diagram.addEventListener('vn-diagram-changed', (e) => toolbar.setStatusMessage(e.detail.command.label));
</script>
</body>
</html>
Next steps
- The README lists every attribute, option, method, event, key and theme token.
- The process-flowchart-js sample shows an order-handling flowchart with a toolbar written in markup (with a command of its own), a grid and a coloured palette, a slow server and a failed save.
- The plant-automation-js sample registers three sets of industrial-automation symbols, each drawn by a render callback, and colours each set with its own slot.