Controlled Viewport
Drive the viewport from your own state: the canvas's Viewport parameter is bound to a field that OnViewportChanged keeps up to date, so writing the field moves the view and every pan, zoom or method call reports back into it. MinZoom, MaxZoom and TranslateExtent bound wherever the view can go, whoever moves it.
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
- Drag the background to pan and scroll to zoom, and watch the two readouts: OnMove follows every step of a drag, while the bound Viewport field changes when you let go - and on every scroll step, since a wheel has no release.
- Reset writes the identity viewport (no pan, zoom 1) into the field, and Center origin writes one that puts the flow's (0, 0) in the middle of the pane at 1.5x; the canvas follows the field.
- Fit and Center Node 2 call methods on the canvas instead; the field still ends up holding the result, because the canvas reports every change through the same callback.
- Pick a Zoom range, then scroll or use the control bar: zooming stops at the new limits, and the bar's zoom buttons grey out when a limit is reached.
- Tick Keep the view inside the box and pan: the view stops at the dashed border. Zoomed out until the pane is bigger than the box, the box is centred and the view will not pan at all.
- With a range or the box on, press Reset or Center origin: the value you write is clamped too.
How it works
Viewportis aBlowViewport(X,Y,Zoom): the screen position of the flow's origin and the scale. Leftnull(the default) the canvas owns its viewport; set, the canvas adopts each new value you hand it and leaves your pans alone between them.OnViewportChangedreports each viewport the canvas commits - at the release of a drag-pan or pinch, on every wheel step and on every change made from code, the first fit ofFitViewOnInitincluded - so binding it back to the field keeps the two in step.OnMovereports every step of a gesture as well, betweenOnMoveStartandOnMoveEnd. A parent that re-renders on it re-renders every frame, so keep that work cheap.FitView()fits the graph with a 10% margin.SetCenter(x, y, zoom)puts a flow point in the middle of the pane; Center Node 2 passes the larger ofGetZoom()and 1.5, so it zooms in but never out.PaneSizeis the pane's measured size in screen pixels, which is all Center origin needs: a flow point lands mid-pane when the viewport'sXandYare half the pane.MinZoomandMaxZoom(0.2 and 2.5) bound every zoom, from the user or from code.TranslateExtentis a flow-spaceBlowRectthe visible area may not leave; when the pane is larger than it, it is centred.- The canvas clamps a viewport it adopts but reports nothing back, and a new range or extent applies from the next move. So after each of those, the page hands the current viewport, read with
GetViewport(), back throughSetViewport(viewport), which clamps it, applies it and reports it - the view snaps inside the bounds and the field agrees with the screen. BlowViewportPortaldraws the dashed box in flow coordinates inside the zoomed layer.