Concepts
The viewport
The viewport is the pan and the zoom of one flow on screen, held by the engine as view state, changed by gestures and actions, and never stored in the document.
The viewport is what part of the flow the root shows, and how big: { x, y, zoom }, where
x and y are the screen offset of the flow's origin from the root's top-left corner, in pixels,
and zoom the scale. A flow point p is drawn at p * zoom + (x, y). It is view state: the
engine holds it, the document never does, so two views of one model can each have their own.
Gestures
The core's viewport needs no feature. On the root:
| gesture | does | root prop (default) |
|---|---|---|
| drag on the pane | pans | panOnDrag (true: the primary button, a finger, a pen) |
| drag anywhere, nodes included, with Space held | pans | panActivationKey (" ") |
| wheel | zooms around the pointer | zoomOnScroll (true) |
wheel, with panOnScroll | pans (Ctrl and the wheel zoom) | panOnScroll (false) |
| two-finger pinch, trackpad pinch | zooms | zoomOnPinch (true) |
| double click on the pane | doubles the zoom around the pointer | zoomOnDoubleClick (true) |
| a drag held near the root's edge | pans towards that edge | autoPan (true) |
The zoom stays within minZoom (0.5) and maxZoom (2); zoomStep (1.2) is the factor of one
step; translateExtent, a flow rectangle, keeps the view inside it. Every one of these props
applies at once when it changes. With selection({ boxOnDrag: true }), a drag on the pane draws
a selection box instead of panning.
Pan buttons and keys
panOnDrag also takes the mouse buttons that pan, 0 (primary), 1 (middle) and 2 (right):
[1, 2] leaves the primary button to a selection box (with selection({ boxOnDrag: true }))
and pans with the other two, as a design tool does. A middle or right press pans only from the
pane and never selects; with 2 in the list, a right click on the pane opens no context menu on
macOS and Linux.
panActivationKey is the key that, held, makes a primary drag pan from anywhere, a node or a
handle included, and start nothing else (Space by default, null for none). It is read from the
root's document, so it works without focusing the flow, and never while the focus is in a field.
With panOnScroll, the wheel pans: panOnScrollMode keeps it to one axis ("horizontal",
"vertical", or "free"), panOnScrollSpeed scales it (default 1), Shift turns a vertical
wheel sideways (but in "vertical" mode), and the wheel zooms while zoomActivationKey is held
(Meta on Apple platforms, Control elsewhere; null for none).
<Flow.Root
features={[selection({ boxOnDrag: true }), drag()]}
defaultNodes={nodes}
panOnDrag={[1, 2]}
panOnScroll
panOnScrollMode="vertical"
panOnScrollSpeed={0.8}
zoomActivationKey="Alt"
className="h-96"
>
…
</Flow.Root>Auto-pan
While a node is dragged, a connection or a reconnection drawn, a selection box drawn or a node
resized, holding the pointer near the root's edge pans the view towards it, faster the nearer it
is, and the gesture keeps following the pointer: a node can be dragged to a place off screen. The
pan stops when the pointer leaves the edge, the gesture ends or the view reaches
translateExtent. autoPan={false} turns it off; autoPan={{ speed: 20, margin: 40 }} pans 20
pixels a frame at the edge and starts 40 pixels from it (defaults 10 and 30). A pan and a minimap
drag never auto-pan.
Actions
Change the viewport from code with the engine's actions (useFlow().engine, or a flow ref):
import { useFlow } from "@fragiola/flow-react";
export function ViewButtons({ nodeId }: { nodeId: string }) {
const { engine } = useFlow();
return (
<div data-flow-no-gesture="" className="absolute top-2 left-2 flex gap-1">
<button type="button" onClick={() => engine.run("fit-view", { padding: 0.2 })}>
Fit
</button>
<button type="button" onClick={() => engine.run("fit-view", { nodeIds: [nodeId], maxZoom: 1.5 })}>
Show the node
</button>
<button type="button" onClick={() => engine.run("zoom-to", { zoom: 1 })}>
100%
</button>
<button type="button" disabled={!engine.can("zoom-in")} onClick={() => engine.run("zoom-in")}>
Zoom in
</button>
</div>
);
}| action | payload |
|---|---|
set-viewport | { x?, y?, zoom?, duration? } |
pan-by | { x, y }, in pixels |
zoom-in, zoom-out | { step?, around?, duration? }: refused at the limit |
zoom-to | { zoom, around?, duration? } |
fit-view | { padding?, nodeIds?, includeHidden?, minZoom?, maxZoom?, duration? }: refused before a size is known (below) |
center-on | { x, y, zoom?, duration? }, a flow point |
focus-node | { nodeId } |
around is a point relative to the root, in pixels (default its centre). Flow.ZoomIn,
Flow.ZoomOut and Flow.FitView are these actions as buttons, disabled while engine.can says
no (Minimap and controls).
What fit-view frames
fit-view frames every node that is not hidden, keeping room around them, within the zoom
limits. Its options (also the root's fitView, and Flow.FitView's props):
padding: the room kept free, by default0.1. A number is a share of the root's size (0.1of its width on the left and on the right, of its height above and below); a"<n>px"string is pixels; an object gives each side its own ({ top: "80px", left: 0.2 }), a side left out keeping none. Pixels keep a toolbar or a panel over the canvas clear at any root size.nodeIds: only these nodes (a hidden one among them too, once its size is known).includeHidden: withoutnodeIds, the hidden nodes of known size are framed too.minZoom,maxZoom: the zoom it may pick. They narrow the engine's own limits, never widen them.duration: how long it animates, in milliseconds.
// a header 64 px high over the canvas, and a side panel 280 px wide on the right
<Flow.FitView aria-label="Fit view" padding={{ top: "88px", right: "304px", bottom: 0.05, left: 0.05 }} maxZoom={1.5}>
<MaximizeIcon />
</Flow.FitView>;
engine.run("fit-view", { includeHidden: true, padding: "24px" });A prop of Flow.FitView left out is left out of its action, so the engine's default applies. The
same payload frames the flow once it is measured with the root's fitView
(fitView={{ padding: "40px" }}), and viewport-for-bounds takes the same padding forms.
Frame an area
fit-view frames nodes. To frame any rectangle of the flow (a region, a search hit and its
neighbours, the selection with room around it), ask the engine for the viewport that shows it,
then go there. viewport-for-bounds answers what fit-view would set (the padding, the zoom
limits and the translateExtent kept), so nothing moves until you run it:
/** Frames the selected nodes with 30 % of room around them, in 300 ms. */
function frameSelection(model: FlowModel<Types, typeof features>, engine: FlowEngine<Types, typeof features>) {
const nodeIds = model.get("selected-nodes").map((node) => node.id);
const bounds = engine.get("nodes-bounds", { nodeIds });
if (!bounds) return;
const viewport = engine.get("viewport-for-bounds", { bounds, padding: 0.3, maxZoom: 1.5 });
if (viewport) engine.run("set-viewport", { ...viewport, duration: 300 });
}Call it from a button inside the root with useFlow()'s model and engine (injectFlow() in
Angular, whose model is a signal: this.flow.model()). The read is undefined while the root
has no size.
The same framing without an engine is the pure viewportForBounds(bounds, width, height, options?), for a picture of a size you choose: an export, a thumbnail, a server render. It knows
no root, so it takes the size and its own limits (padding 0.1, minZoom 0.5, maxZoom 2 by
default):
import { viewportForBounds } from "@fragiola/flow"; // the core: every adapter re-exports it too
// an image of 1200 × 630 pixels showing the whole flow
const bounds = engine.get("nodes-bounds", {});
if (bounds) {
const { x, y, zoom } = viewportForBounds(bounds, 1200, 630, { padding: 0.05, minZoom: 0.1 });
// draw the viewport's content at translate(x, y) scale(zoom) into a 1200 × 630 frame
}Both are on the engine page, beside intersecting-nodes, which lists the
nodes inside or overlapping a rectangle of the flow.
Animated changes
duration, in milliseconds, animates an action instead of jumping (default 0: at once). The
root's easing shapes every animation, a function of the share of time gone, from 0 to 1, to the
share of the way (default a smooth ease in and out). Each frame is a viewport-change with
gesture: false; a pan, the wheel, a pinch, another action or cancel-gesture stops the
animation where it is. The controls take it as a prop
(Minimap and controls).
import { useFlow, type ViewportEasing } from "@fragiola/flow-react";
const easeOut: ViewportEasing = (t) => 1 - (1 - t) ** 3;
export function GoToNode({ nodeId }: { nodeId: string }) {
const { engine } = useFlow();
return (
<button
type="button"
data-flow-no-gesture=""
onClick={() => engine.run("fit-view", { nodeIds: [nodeId], maxZoom: 1.5, duration: 400 })}
>
Go to the node
</button>
);
}
// on the root: <Flow.Root easing={easeOut} …>Screen and flow coordinates
engine.get("screen-to-flow", point) turns a client point (a pointer's clientX and clientY, a
drop's) into flow coordinates, and flow-to-screen the other way round. That is how a palette
drops a node where the pointer is:
function onDrop(event: React.DragEvent) {
const point = engine.get("screen-to-flow", { x: event.clientX, y: event.clientY });
model.run("node.add", { type: "task", position: point, data: { title: "New task", done: false } });
}Reading it
useViewport() re-renders a component when the viewport changes (a zoom readout);
engine.get("viewport") reads it once. onViewportChange(viewport, gesture) on the root (Vue:
@update:viewport; Svelte: onviewportchange; Angular: (viewportMove)) hears every change, with gesture true for a pan, a zoom or a pinch by the user.
Controlled or not
defaultViewport (default { x: 0, y: 0, zoom: 1 }) is where the flow starts, read once;
fitView fits the nodes once they are measured. A controlled viewport is set on the engine
whenever it differs:
const [viewport, setViewport] = useState<Viewport>({ x: 0, y: 0, zoom: 1 });
<Flow.Root viewport={viewport} onViewportChange={setViewport} defaultNodes={nodes} className="h-96">
…
</Flow.Root>;Neither React, Vue, Svelte nor Angular renders the viewport's transform: the engine writes it on Flow.Viewport's element
itself, so a pan re-renders only what shows the viewport (the background, the minimap, toolbars,
the zoom buttons), never the nodes.
Snapping is the drag's, the grid is yours
The snap-to-grid example snaps dragged nodes with drag({ snapGrid: [20, 20] }) and draws the
same grid with Flow.Background gap={20}: the background is a pattern in flow units that pans and
zooms with the viewport, but it does not know about the snap. Keep the two numbers equal.
Large flows
onlyRenderVisible on the root renders only the nodes and edges in view (visible-nodes,
visible-edges, plus the nodes not measured yet), for flows of hundreds of nodes and more. A node
that leaves the view keeps its last size, so its edges are still drawn right.
Large flows has what else keeps a flow of thousands of nodes smooth,
and the measured frame times.