HTML elements
By the end of this page you have buttons and links that work on the board, HTML elements that come back after a reload, and a board that sits in a scrolling page.
Add an element
viewport.addHtmlElement(node, position, size, options) puts a DOM node on the board and returns the element’s id. The node pans and zooms with the board, is drawn in layer order with the other elements, and is selected, moved, resized and rotated like a note. Without size it is 200 by 150 board units.
const card = document.createElement('div');
card.style.padding = '16px';
card.innerHTML = '<h3>Initiative</h3><button type="button">Next turn</button>';
viewport.addHtmlElement(card, { x: 100, y: 200 }, { w: 250, h: 150 }, { interactive: true });
Live elements
interactive: true makes the element live:
- A press on a control goes to the control, under every tool and from mouse, touch or pen. The board draws nothing, selects nothing and records no undo step.
- A press on any other part selects and moves the element, as on a note. An element made only of controls can’t be selected or moved, so give it padding, as the card above has.
- When the element is selected, its resize and rotate handles take presses before the controls under them.
- While a control has focus, board shortcuts are off, Escape, Delete and undo included.
- Controls keep working on a locked layer.
The controls are a[href], button, input, select, textarea, label, summary, editable content, audio[controls], video[controls] and iframe.
The board below starts on the pencil. A click on Next turn goes to the button and draws nothing.
Without interactive, a press anywhere on the element selects and drags it. A double click or double tap enters interact mode, where its controls work, and Escape or a press outside the element leaves it. viewport.stopInteracting() leaves it from code.
Controls and drag areas
Two attributes change what counts as a control:
data-fn-controlmakes any element a control, such as a custom slider that handles its own pointer events.data-fn-dragmakes an area move the element, even inside a control.
The attribute nearest the pressed node wins. This card is a link from edge to edge, so only its Move grip drags it:
const link = document.createElement('a');
link.href = 'https://example.com/session-notes';
link.textContent = 'Session notes';
const grip = document.createElement('span');
grip.dataset.fnDrag = '';
grip.textContent = 'Move';
link.prepend(grip);
viewport.addHtmlElement(link, { x: 400, y: 200 }, { w: 240, h: 80 }, { interactive: true });
Keep elements after a reload
A saved board keeps each HTML element’s position, size, htmlType, data and interactive. The DOM node is not saved. Register a renderer for the htmlType before you load, and the board builds the node again from data. The renderer also runs when another person on a shared board adds the element, and when an undo brings a deleted one back.
function initiativeCard(title: string): HTMLElement {
const card = document.createElement('div');
card.style.padding = '16px';
const heading = document.createElement('h3');
heading.textContent = title;
const button = document.createElement('button');
button.type = 'button';
button.textContent = 'Next turn';
card.append(heading, button);
return card;
}
const viewport = new Viewport(document.getElementById('board')!, { tools: 'default' });
viewport.registerHtmlRenderer('initiative', (element) => {
const title = element.data?.title;
return initiativeCard(typeof title === 'string' ? title : '');
});
const saved = localStorage.getItem('board');
if (saved) viewport.loadJSON(saved);
viewport.addHtmlElement(
initiativeCard('Initiative'),
{ x: 100, y: 200 },
{ w: 250, h: 150 },
{ htmlType: 'initiative', data: { title: 'Initiative' }, interactive: true },
);
addHtmlElement keeps the node you pass it and doesn’t call the renderer. An element with no node and no renderer shows a placeholder named after its htmlType, such as “initiative widget”.
On a shared board, data and interactive come from other people too. Build nodes with textContent, and check any URL from data before it goes into href or src.
Boards in a scrolling page
embedded: true makes the board share the page’s scrolling:
- A plain wheel scrolls the page. Ctrl+wheel or ⌘+wheel zooms at the cursor.
- Touch is ignored until your page engages the board, so a one-finger swipe scrolls the page. Mouse and pen always work.
- While engaged, the board takes touch as before. Escape anywhere on the page disengages it, unless focus is in a text field or editable content.
The viewport reads embedded once, when it is created. setEmbeddedEngaged(engaged) engages or disengages the board, isEmbeddedEngaged() reads the state, and onEmbeddedEngagedChange(listener) reports each change, Escape included. Disengaging cancels touch gestures in progress without an undo step and keeps the selection.
onWheelPassThrough(listener) fires for every plain wheel the board leaves to the page, for a “Hold Ctrl to zoom” hint. It is not throttled.
const viewport = new Viewport(document.getElementById('board')!, {
tools: 'default',
embedded: true,
});
viewport.shortcuts.disable('select.cycle');
viewport.shortcuts.disable('select.cycle-reverse');
const toggle = document.getElementById('draw-toggle')!;
toggle.addEventListener('click', () =>
viewport.setEmbeddedEngaged(!viewport.isEmbeddedEngaged()),
);
viewport.onEmbeddedEngagedChange((engaged) => {
toggle.textContent = engaged ? 'Done' : 'Draw on this board';
});
const hint = document.getElementById('zoom-hint')!;
let timer: ReturnType<typeof setTimeout> | undefined;
viewport.onWheelPassThrough(() => {
hint.hidden = false;
clearTimeout(timer);
timer = setTimeout(() => (hint.hidden = true), 1500);
});
A board without embedded reports true from isEmbeddedEngaged(), ignores setEmbeddedEngaged and never calls the listeners. The boards in these docs are embedded; on a touch screen, Draw on this board engages one.
Disengaged touch still reaches the minimap, setActivation, PingInput and CameraAnimator. Two parts have only unit tests: the guard that stops a disengaged Apple Pencil or Android stylus from scrolling the page has not been checked on a device, and wheel and gesture handling has not been run in Safari.