Rendering and Viewport Implementation in Vanilla-Diagram

One coordinate transform

World units are CSS px at zoom 1. A node's x, y is its top-left corner before rotation; width, height its box; rotation turns it (degrees, clockwise on screen) about its centre. The world has no bounds.

The viewport { x, y, zoom } is the world point at the canvas's top-left corner and the scale: screen = (world - viewport) * zoom. VanillaDiagramGeometry.toWorld() and toScreen() are the only conversions; a pointer handler subtracts the canvas's client rectangle first. The geometry module also turns a node's own frame into the world (localToWorld(), worldToLocal()), places ports (a port's side turns with the node, rounded to the nearest of the four directions), gives a rotated node's bounding box, and resizes in a node's own frame (04-pointer-and-touch).

Layers

<svg class="vn-diagram-svg">
  <defs>                         two arrowhead markers per diagram (normal, selected)
  <g class="vn-diagram-world">   style="transform: matrix(zoom, 0, 0, zoom, tx, ty)"
    <g class="vn-diagram-grid">         one <rect> filled with a dot <pattern>, under everything
    <g class="vn-diagram-frames">       one <g> per container node, depth-first (a container under the
                                          containers inside it), siblings by z; as a node's <g>, plus a
                                          border hit path and a label-band hit rect
    <g class="vn-diagram-connectors">   one <g> per connector: a 12px transparent hit path, the visible path,
                                          and a <foreignObject> label box when it has a label
    <g class="vn-diagram-nodes">        one <g> per node, in z order, data-swatch="<slot>" when it has one:
                                          transform="translate(x y) rotate(r w/2 h/2)"
                                          the shape (a <path>, or the render callback's element)
                                          a <foreignObject> on the text box holding an HTML <div>
    <g class="vn-diagram-preview">      the connect preview, the palette's drop outline
  <g class="vn-diagram-adorners">       screen space: snap guides, selection outlines, the joining outline,
                                        the focus ring, ports, route handles and tangents, resize handles,
                                        the rotation handle, connector end handles, the rubber band

The grid

grid-size (world px, 0 off) draws dots with an SVG <pattern> (spacing the grid size, offset half a cell so a dot sits on each multiple, a dot of 1 screen px) on one <rect> in the vn-diagram-grid group, first in the world layer. The rect covers three views' width and height around the view and is rewritten with each viewport write, so the grid is O(1) per pan or zoom step. Below 8 screen px between dots the grid hides: it would be noise. The grid takes no pointer events, and the export leaves it out. Its dots paint with --vn-diagram-grid-color.

What is in the DOM

Only what meets the drawn rect (the view grown by a quarter of its size on each side; below zoom 0.4 by one 256 px bucket), plus the items a gesture previews and the text editor's item: a host's code or CSS can't expect one .vn-diagram-node per node. A diagram that fits the view after its load's zoom to fit draws every item. Items entering the rect are drawn under a per-frame budget, nearest the view's centre first, in records that leaving items gave to a free list (14-large-diagrams). Every node and connector group is aria-hidden, with no role or aria-label, but the text editor's (15-screen-readers).

The far view. Below zoom 0.4 a drawn item's text box and connector label are display="none" and not written, ports show on the hovered node only, and the hit bands are 40 world px strokes rather than non-scaling ones. The shapes, connectors, arrowheads, the selection and the adorners stay; the export and the text editor show the text at any zoom.

The write pass

A change marks ids dirty (_markNode(), _markConnector(), _markEffect(); a node's connectors with it); a selection change marks only the ids that joined or left it. One requestAnimationFrame pass (_renderNow()) reads the view once, works the drawn rect out again when the view left it, syncs the dirty nodes (their index entries, then drawn, queued or released), keeps the obstacle index of the routed elbows on the nodes as drawn, syncs the dirty connectors, draws the entering items under the budget and releases the leaving ones, puts entering node groups in z order (a binary search of the drawn order; the frames layer in its depth-first order), writes the viewport, redraws the overlays and adorners, and fires the viewport event. Every attribute is written only when its value changed, as the Gantt's link layer does. A node's shape is rebuilt only when its symbol, size, or (for a render callback) text or data changed. A change of more than 2,000 items is synced over several passes. A ResizeObserver on the canvas schedules a pass when it resizes.

A gesture's preview (_preview: a move offset, a resized box or a rotation) is applied by _effectiveNode() when drawing, so the model is untouched until the gesture commits.

Pan and zoom

Zoom stays within minZoom and maxZoom (0.1 and 4 by default), which getZoomLimits() returns; zooming about a point keeps the world point under it. vn-diagram-viewport-changed fires from the write pass, so at most once per frame. An API call marks the viewport as set by the host: a load then keeps it instead of zooming to fit.

Why a CSS transform. The world group's transform is the CSS transform property, not the transform attribute (an SVG element's transform origin is 0 0, so the matrix means the same). Chromium lays the whole SVG subtree out again when the attribute changes, about 20ms at 1,000 nodes, and not for the property, about 5ms. For the same reason nothing is written on the <svg> per zoom step: a custom property there restyles every descendant. The matrix takes the zoom unrounded, the very value the screen-space adorners use: a scale rounded to 0.001 moved a shape away from its outline, handles and ports by its world coordinate times the rounding, a few px at 10,000 world px from the origin. Only the offsets round, to 0.001 screen px.