Responsive Canvas
The canvas watches its own container and every node for size changes, so a resized pane or a node whose content grows keeps hit-testing, edge anchors and the minimap right with no code of yours. OnPaneResize tells the page when the pane changes size, which is all keeping the graph fitted takes. The viewport is controlled from the page's own state at the same time.
viewport x 60 · y 40
· zoom 1.00
Press Tab to move between the nodes and edges of the diagram.
Press Enter or Space to select it, Shift with Enter to add it to the selection, the arrow keys to move the selected nodes, Delete to remove them and Escape to clear the selection. Press C to start a connection from it.
Press Enter or Space to select it, Delete to remove it and Escape to clear the selection. Press C to move its end to another node.
bit BlowTry it
- Press Narrower and Wider to resize the box the canvas sits in, 120 pixels at a time: the control bar and the minimap keep to their corners, and the minimap's window shrinks or grows with the visible area. The width is capped at the page column, which is why the requested width can run ahead of what you see.
- Resize the browser window: the canvas follows the same way.
- Tick Keep fitted, then press Narrower and Wider or resize the window: the graph is fitted to the pane again after every change of size, with even margins.
- Press Grow node content to add a line to the middle node, up to six before it starts again from one: the node grows, its bottom handle moves down with it, and the animated edge leaving it follows - as does its shape on the minimap.
- Drag the background or scroll: the readout in the corner follows, because the viewport lives in the page's own field. Recenter viewport writes that field back to where it started.
- Drag a node after any resize: it still lands under the pointer, because the canvas's idea of where the pane is on screen is kept up to date.
How it works
- A
BlowCanvasfills its container. AResizeObserverin the browser layer watches the pane, so when the box changes size the canvas re-reads its bounds - which pointer hit-testing,ScreenToFlowPosition,PaneSizeand the minimap's window all depend on. - Every node is observed too. A node with no explicit
WidthandHeightsizes to its content and is re-measured whenever that changes - with its handles - so the edges anchored to it follow.Measuredholds the size it was last measured at. - What the browser cannot report is a handle that moved without anything resizing - switched to another side, say. After such a change call
UpdateNodeInternals(). OnPaneResizeis raised with the pane's new size oncePaneSizehas it; with Keep fitted on, the page callsFitView()there. NoResizeObserverof the page's own is needed - and one would not need a delay either, asFitViewmeasures the pane when the canvas has not yet heard of a resize.Viewportis bound to a field thatOnViewportChangedkeeps up to date, so the panel's readout always shows the current pan and zoom; writing the field moves the view.NodeTemplatedraws the nodes, with aBlowHandleat the top and at the bottom;BlowPanelpins the readout to the top-left corner.