Field Notes

Tools and input

By the end of this page you can choose a board's tools, drive them from your own toolbar, and know what mouse, touch, pen and each key do.

Choose the tools

tools: 'default' registers nine tools and starts on select. Each has a name for setTool and a key:

  • select (V, SelectTool): selects, moves, resizes and rotates elements. A drag across empty board selects what the box touches, and Shift+click adds an element to the selection or takes it out.
  • hand (H, HandTool): pans the board.
  • pencil (P, PencilTool): draws smoothed freehand strokes.
  • eraser (E, EraserTool): erases the part of a stroke it passes over and leaves other elements alone.
  • arrow (A, ArrowTool): draws curved arrows that can attach to elements.
  • note (N, NoteTool): places sticky notes.
  • text (T, TextTool): places text.
  • shape (S, ShapeTool): draws rectangles, ellipses and lines.
  • frame (F, FrameTool): draws a frame that can group and clip its explicit children.

For a different set, pass a list. createDefaultTools() returns new instances of the nine, and ImageTool places images:

const viewport = new Viewport(document.getElementById('board')!, {
  tools: [...createDefaultTools(), new ImageTool()],
});

viewport.toolManager.register(tool) adds a tool to a board that is already running.

Switch tools from your toolbar

viewport.setTool(name) switches the tool. toolManager.onChange reports every switch, including one made with a key, so the toolbar can follow it:

const toolbar = document.getElementById('toolbar')!;
toolbar.addEventListener('click', (event) => {
  const button = event.target instanceof Element ? event.target.closest('button') : null;
  if (button?.dataset.tool) viewport.setTool(button.dataset.tool);
});
viewport.toolManager.onChange((name) => {
  for (const button of toolbar.querySelectorAll('button')) {
    button.setAttribute('aria-pressed', String(button.dataset.tool === name));
  }
});

Change tool options

Each tool takes options in its constructor, and setOptions changes them while the board runs. getTool finds a registered tool by name.

viewport.toolManager.getTool<PencilTool>('pencil')?.setOptions({ color: '#2F55A4', width: 3 });
viewport.toolManager.getTool<ShapeTool>('shape')?.setOptions({ shape: 'ellipse' });

viewport.toolManager.register(
  new PencilTool({
    name: 'highlighter',
    color: '#F2DE8A',
    width: 12,
    opacity: 0.4,
    blendMode: 'multiply',
  }),
);

The highlighter is a second PencilTool under its own name, so setTool('highlighter') picks it. A tool whose name is not in the default shortcut table has no default key.

EraserTool takes radius in screen pixels (default 20), and mode: 'stroke' makes it delete whole strokes. Hold Shift while drawing a line to snap it to 45° steps.

Mouse, touch and pen

The board reads Pointer Events, so mouse, touch and pen drive the same tools.

  • One pointer drives the active tool. When a second finger lands, the tool’s gesture is cancelled and two fingers pan and pinch-zoom the board. A stroke cut short this way leaves nothing behind.
  • The mouse wheel zooms at the cursor. Middle-button drag, or Space and drag, pans. A pan that ends in a flick coasts; panInertia: false turns that off.
  • A touch reaches the tool once it moves more than 3px or lifts. With the select tool, a 500ms long press opens the context menu.
  • Pen pressure sets the stroke’s width along its length, from 0.4 to 1.8 times the pencil’s width. Input without pressure counts as pressure 0.5, which draws at 1.1 times width.
  • While a pen is down, touches are ignored, so a palm resting on the screen draws nothing.

Double-click or double-tap a note, text element, rectangle, ellipse, or arrow to edit its text. Select one and press Enter to run selection.edit-text. Finish editing with Escape, mod+Enter, or a click outside the editor. A rectangle or ellipse grows for its text and cannot resize below the text height; it does not shrink automatically. With the select tool, right-click or a long press opens a menu with edit text, cut, copy, paste, duplicate, delete, order, rotate and lock. contextMenu: false removes it.

For a board inside a scrolling page, Boards in a scrolling page shows embedded, which leaves the plain wheel and one-finger swipes to the page.

Keyboard shortcuts

Shortcuts work while the board has focus, and a click on the board gives it focus. With shortcuts: { scope: 'window' } they work anywhere on the page. Keys typed into an input, a textarea, a select, editable content or a control on a live HTML element never reach the board. In both scopes, Enter and Space on a focused button, link or <summary> go to that element; other shortcuts still route to the board.

mod is Ctrl or ⌘. Each shortcut runs a named action:

  • Delete or Backspace: edit.delete
  • Escape: select.none
  • Enter: selection.edit-text, on one selected note, text element, rectangle, ellipse or arrow
  • mod+Z: edit.undo
  • mod+Y or mod+Shift+Z: edit.redo
  • mod+A: select.all
  • Tab and Shift+Tab: select.cycle and select.cycle-reverse
  • mod+C, mod+X and mod+D: edit.copy, edit.cut and edit.duplicate
  • mod+V pastes through the browser’s paste event; edit.paste has no key
  • ] and [: arrange.bring-forward and arrange.send-backward
  • mod+] and mod+[: arrange.bring-to-front and arrange.send-to-back
  • mod+G and mod+Shift+G: arrange.group and arrange.ungroup
  • mod+Shift+L: arrange.toggle-lock
  • R and Shift+R: arrange.rotate-cw and arrange.rotate-ccw
  • Arrow keys: arrange.nudge-left, arrange.nudge-right, arrange.nudge-up and arrange.nudge-down, 1 unit at a time or 10 with Shift
  • Shift+1: view.zoom-to-fit
  • mod+=, mod+- and mod+0: view.zoom-in, view.zoom-out and view.zoom-reset
  • L: tool.laser, when a LaserTool is registered (the default tools don’t include one)
  • mod+K: palette.open, which opens the command palette
  • ?: palette.help, which opens shortcuts help
  • The tool keys above: tool.select, tool.hand, tool.pencil, tool.eraser, tool.arrow, tool.note, tool.text and tool.shape

tool.frame is the F shortcut.

Frames

Frames are root-only, non-nested, non-rotating parents. A frame contains only elements explicitly added to it, so overlapping geometry does not create membership. Resizing a frame leaves its children’s geometry unchanged.

viewport.addToFrame(frameId, ids);
viewport.removeFromFrame(frameId, ids);
viewport.dissolveFrame(frameId);

Deleting a frame deletes its subtree. Dissolving a frame removes the frame and preserves its direct children as roots.

Rebind keys with shortcuts.bindings in the options, or later with viewport.shortcuts.rebind(action, keys). null as the keys, or disable(action), turns a shortcut off. reset() restores the defaults, and getBindings() returns the current table for a settings screen.

const viewport = new Viewport(document.getElementById('board')!, {
  tools: 'default',
  shortcuts: {
    bindings: {
      'edit.duplicate': 'mod+shift+d',
      'tool.pencil': ['p', 'b'],
    },
  },
});
viewport.shortcuts.disable('select.cycle');
viewport.shortcuts.disable('select.cycle-reverse');

On a board that sits in a page, disable select.cycle and select.cycle-reverse as above. Otherwise Tab moves between elements and keyboard users can’t leave the board. viewport.runAction(action) runs any action from your own buttons.

Command palette and find on the board

Ctrl/⌘+K opens the command palette. With an empty query, it lists enabled actions and their current keys. Typing filters the actions by name and searches notes, text (including text inside shapes and on arrows) and frame titles, showing up to 20 board results. Matches ignore case and accents. Elements on hidden layers are skipped.

Down and Up move through results, and the camera moves to each board result as it becomes active. Enter or a click runs the action or goes to the board result, then closes the palette. Escape or a click outside closes it and does not move the camera back. Camera moves keep to the zoom limits and any viewport.camera.addConstraint constraints. The active board result is outlined; after the palette closes the outline holds for 1.5 seconds, then fades. Revealing does not select the element or add an undo step. Opening the palette, searching and revealing write nothing to the element store or the saved board, and send nothing over sync.

viewport.palette.open('budget') opens the palette with budget already typed, for a search button in your own UI. Add Ctrl/⌘+F with shortcuts.rebind('palette.open', ['mod+k', 'mod+f']); Ctrl/⌘+F has no default binding. Pass palette: false to remove the palette and help. Pass palette: { strings } to translate the palette’s and help dialog’s own text: the placeholder, the empty state and result count (noResults, results, where {count} is replaced with the number of results), the result group titles (actions, board), the help title and its Close button (helpTitle, close), the help section headings (groups, keyed by action namespace such as edit) and the result badges (types, keyed by element type). Keys you leave out keep their English default. Action labels come from each action’s label, and the fixed help rows stay in English.

An element type from a plugin is searchable when its definition has searchText(element), which returns the text to match or null. Search finds a child clipped by its frame and reveals it inside the frame like any other element. The palette and shortcuts help mount inside the host container with position: fixed. If the host container has a CSS transform or filter, they are placed against the container instead of the browser window. Style both dialogs with --fieldnotes-palette-bg, -fg, -border, -active, -muted, -shadow and -font.

During a windowed presentation, Escape first closes the palette or shortcuts help, and the next Escape ends the presentation.

Shortcuts help

Press ? or call viewport.shortcutsHelp.open() to open shortcuts help. It is built from actions and current bindings, so a rebind is shown there. It also lists Paste, the fixed palette keys, and the fixed presentation keys. Escape or ? closes it and returns focus to the board.

Presentation

viewport.presentation.start() steps through every frame on a visible layer, in scene order. To show other areas, pass start({ slides }) with a camera view for each slide.

next(), previous() and goTo(index) move between slides and return false when they can’t; exit() ends the presentation. onChange reports every step, and getState() returns index and count for a slide counter.

While presenting, Right arrow, Down arrow, PageDown and Space go to the next slide; Left arrow, Up arrow, PageUp and Shift+Space go back; Home and End jump to the first and last slide; Escape ends the presentation. These keys can’t be rebound. They don’t reach the presentation while focus is on a button or link on the board or, when the board isn’t fullscreen, on anything outside its host container, so move focus back to the board after a click on your own controls. With a context menu open or a live HTML element in use, Escape closes that first and ends the presentation on the next press. L picks the laser pointer when a LaserTool is registered.

The viewport requests fullscreen on its host container and stays windowed if the browser denies it. Leaving fullscreen ends the presentation. Give the host container an opaque background, because the fullscreen backdrop is black. A board created with embedded ignores touch while presenting unless it is engaged, so call setEmbeddedEngaged(true) before start() when presenters use touch. Moving between slides adds no undo step.

Write your own tool

A tool is an object with a name, onPointerDown, onPointerMove and onPointerUp; register it with viewport.toolManager.register(tool). The store changes it makes from pointer down to pointer up are one undo step. When a second finger cancels the gesture, onPointerCancel must remove what the tool added, or it stays on the board as an undo step. The core README lists the whole Tool interface.