Pointer and Touch Implementation in Vanilla-Diagram
One gesture at a time. A press on the canvas classifies its target (a port,
a resize handle, the rotation handle, a connector end, a node, a connector,
or the empty canvas),
captures the pointer, and starts a gesture; pointercancel and
lostpointercapture cancel it, as on the resize box's handle. The canvas is
touch-action: none: it handles every gesture itself. The thresholds are the
Gantt's:
| Mouse and pen | a drag starts past 3px |
| Touch | a 120ms hold starts a drag; moving more than 8px first pans |
| Double-click | two clicks on the same item within 300ms |
| Auto-pan | within 32px of an edge, up to 16px a frame |
A press first commits a pending nudge burst and focuses the canvas.
Selection
- A click on a node or connector selects it alone; Ctrl or Cmd + click toggles it; Shift + click adds it (moved to the end). A click on the empty canvas clears the selection (not with a modifier) and disarms an armed port.
- Rubber band: a mouse or pen drag past 3px on the empty canvas (or on a connector), or a 120ms touch hold there, draws a band and selects the nodes whose boxes (a rotated node's bounding box) are fully inside it, live, each move, found through the node index (O(nodes in the band)), in drawing order. With Shift or Ctrl/Cmd held at the press, the band adds to the selection it started from.
- The selection is ordered; the last node in it is the focus node, where
the keys start.
vn-diagram-selection-changedfires when the list changes. Ids that leave the model leave the selection. - A selected node shows an outline; a selected connector its end handles.
- Containers take the pointer on their border and their label band only: a press takes a node in the nodes layer, else a connector, else a container's frame, and a press on a container's empty inside acts as on the empty canvas (13-containers).
Move
A press on a node, then 3px (or the touch hold), moves the selection; a
pressed node that isn't selected becomes the selection first. The move is a
preview, _preview = { kind: 'move', ids, dx, dy }, drawn by transform
with the nodes' connectors re-routed each frame; the model is untouched.
A selected container moves with every descendant (each node once). Release
clears the preview and commits one move command; each moved node that
isn't moving with its container takes the container under its centre as its
parent, or none, and a changed parent joins the move as one batch. While
the drag runs, the container the pressed node would join shows a dashed
outline. Escape,
pointercancel, losing the capture, turning read-only on, a load, a failed
save and destroy() cancel it, and every previewed node goes back. A drag
is also cancelled by a change that removes its subject, whatever made it (a
key, the API, an undo or a redo): every node a move drags, the node a resize
or a rotation drags, the connector of a reconnect or a route handle drag, or
the node a connect started from
(05-ports-and-connectors).
Its preview and guides go at once, and its release commits nothing, even
when an undo has added the item back by then. A move
of which only some nodes are removed goes on, and its release moves only
the nodes that still exist; a move never records a step for nodes that no
longer exist. Read-only:
a press on a node selects it, and a drag from it draws a band (or pans, on
touch). While the diagram is busy (a load, a layout or an export runs), no gesture
starts at all: a press selects nothing and draws no band, a drag pans
nothing, two fingers don't pinch, and hovering shows no ports
(01-web-component ยง The busy state);
a job beginning cancels a gesture in progress.
Resize
One selected node (and nothing else selected) shows eight handles, 8px in
screen space, turned with the node. Their hit area is 12px, 24px under
(pointer: coarse). A handle drag converts the pointer's world offset into
the node's own frame (turned back by its rotation), and
VanillaDiagramGeometry.resizeBox() computes the box: the dragged edges
move, the opposite edge or corner keeps its world position (the box is placed
around its new centre in the turned frame). Shift keeps the ratio, always for
a lockAspect symbol (on an edge handle, the other side grows evenly about
the centre line). The box never goes below the symbol's minSize, and a
container's frame doesn't shrink past its children's box plus 16 px. Release
commits one resize; cancellation is as for a move.
Rotate
The rotation handle, beyond the middle of the selected node's top edge
(03-rendering-and-viewport),
turns the node about its centre: the pointer's angle about the centre, less
its angle at the press, is added to the rotation at the press, rounded to 1
degree; Shift snaps the result to a multiple of 15. It previews as a resize
does (_preview = { kind: 'rotate', id, rotation }), re-routing the node's
connectors each frame; snapping doesn't apply. The release commits one
update of rotation, normalized to [0, 360) and labelled
command.rotate; no change commits nothing. Cancellation is as for a move.
A touch press on the handle starts the turn at once, as on a resize handle.
Snapping and guides
VanillaDiagramSnapping is pure; the gestures call it each frame.
- The grid (
grid-size, world px). A move snaps the selection's bounding box's top-left corner to the nearest multiple of the grid size. A resize snaps the dragged edges, for a node with no rotation, and not while the ratio is kept (Shift, or alockAspectsymbol): two snapped edges can't keep a ratio, and a rotated node's edges aren't axis-aligned. A palette drop snaps the new node's top-left corner. Shift + arrow nudges go to the next grid line (06-keyboard-and-focus). - Guides (
snap-guides, on by default). During a move or a resize, the moved box's left, centre and right, and top, middle and bottom (a resize: the dragged edges), snap to the same lines of the nodes in view when within 6 screen px, and a guide line (--vn-diagram-guide-color) shows each match in the adorner layer, across the gap: from the end of the moved shapes that faces the nearest node on that line to that node's end that faces them (a node below: from the moved shapes' bottom to its top, not its bottom), never on to a node past it. With nodes on both sides the line runs from the nearest node before to the nearest node after, over the moved shapes; with only nodes beside them (overlapping or touching), along the moved shapes. A shape meets the line where its drawn outline does: a symbol'spath, flattened to segments (VanillaDiagramGeometry.pathPolylines()), or its box for a symbol drawn byrender, whose shape isn't known. So the top line of a diamond's box meets it at its top corner, and a rounded rectangle's along its straight edge. A guide wins over the grid on its axis. Nudges and drops don't use guides. A snap that would take a resized box below its minimum size is dropped. - Alt held during a drag turns both off for that frame.
- Rotated nodes take part by their bounding box, and their outline turns with them: the left line of a node turned 45 degrees meets it at its left corner, not at its bounding box's top or bottom. A move's selection meets a line only where one of its nodes does.
- The API never snaps: the host's numbers stay exact.
The guide index collects the edges and centres of the nodes in view once, at drag start, through the node index (14-large-diagrams), and sorts them; each frame looks the moved box's six lines up by binary search (O(log n)). Where a node's outline meets a line is worked out the first time that line matches, so only the nodes on a matched line have their paths flattened (once per symbol and size). Snapping changes only the preview's offset or box, so a drag still commits one command.
Pan and pinch
A middle- or right-button drag, or a touch moved before its hold, pans; the
content follows the pointer from where it went down. The mouse buttons pan
before any hit test, so a drag that starts on a node, a handle or a port
pans and leaves it, in read-only mode too; a press that doesn't move does
nothing. Each move checks buttons: a pan whose button is up (its release
went to a menu that opened anyway, such as Firefox's on Shift + right
click) ends there. Space no longer pans.
The context menu. The canvas cancels contextmenu (a right click, the
Menu key, Shift + F10, macOS's Ctrl + click, a touch long-press where the
browser has a menu) but inside the open text editor, which keeps its
spelling and paste menu. The event still bubbles to the host, with
defaultPrevented set. Windows fires the menu after the release, where the
pointer is, so a right pan's release also arms a guard: a capture-phase
listener on window cancels the first menu anywhere, and is removed by
that menu, after 300ms, by a gesture cancel (leaving the DOM, Escape, a
load) or by destroy(). On Linux and macOS the menu fired at the press, on
the canvas, and the guard times out unused.
A second touch pointer cancels whatever the first started and pinches: the zoom follows the distance between the fingers, and the world point under their first midpoint stays under their current midpoint, so the pinch zooms and pans together.
Auto-pan
During a move, a resize, a band, a connect or a reconnect drag, a pointer within 32px of the canvas's edge pans it each frame, up to 16px by how close it is, and the dragged items follow the world under the pointer. A palette drag auto-pans only once the pointer has been inside the canvas past that zone, so crossing the edge on the way in doesn't pan.
Double-click
A second click on the same node or connector within 300ms fires
vn-diagram-node-dblclick or vn-diagram-connector-dblclick with a copy of
the item, then, unless the diagram is read-only, opens the text editor on it
(10-text-editing). Taps count the same
way, so a double-tap does both on touch. A double-click on a container's
label band edits its label. A press inside the open editor is left to it.