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

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 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.