Skip to content

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.

Viewport x 0 · y 0 · zoom 1.00 OnMove x 0 · y 0 · zoom 1.00
Node 1
Node 2
Node 3
Arrow keys move the view, Home shows the whole flow.
bit Blow

Try 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

  • Viewport is a BlowViewport (X, Y, Zoom): the screen position of the flow's origin and the scale. Left null (the default) the canvas owns its viewport; set, the canvas adopts each new value you hand it and leaves your pans alone between them.
  • OnViewportChanged reports 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 of FitViewOnInit included - so binding it back to the field keeps the two in step.
  • OnMove reports every step of a gesture as well, between OnMoveStart and OnMoveEnd. 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 of GetZoom() and 1.5, so it zooms in but never out.
  • PaneSize is the pane's measured size in screen pixels, which is all Center origin needs: a flow point lands mid-pane when the viewport's X and Y are half the pane.
  • MinZoom and MaxZoom (0.2 and 2.5) bound every zoom, from the user or from code. TranslateExtent is a flow-space BlowRect the 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 through SetViewport(viewport), which clamps it, applies it and reports it - the view snaps inside the bounds and the field agrees with the screen.
  • BlowViewportPortal draws the dashed box in flow coordinates inside the zoomed layer.
An unhandled error has occurred. Reload 🗙