NgDiagramViewportService
since v0.8.0The NgDiagramViewportService provides methods and signals for interacting with the diagram viewport.
Example usage
Section titled “Example usage”private viewportService = inject(NgDiagramViewportService);
// Move viewport to (100, 200)this.viewportService.moveViewport(100, 200);
// Zoom in by a factor of 1.2this.viewportService.zoom(1.2);Extends
Section titled “Extends”NgDiagramBaseService
Properties
Section titled “Properties”canZoomIn
Section titled “canZoomIn”canZoomIn:
Signal<boolean>
Returns true if the current zoom level is below the maximum and can be increased.
canZoomOut
Section titled “canZoomOut”canZoomOut:
Signal<boolean>
Returns true if the current zoom level is above the minimum and can be decreased.
scale:
Signal<number>
Returns a computed signal for the scale that safely handles uninitialized state.
viewport
Section titled “viewport”viewport:
Signal<Viewport>
Returns a computed signal for the viewport that safely handles uninitialized state.
Accessors
Section titled “Accessors”maxZoom
Section titled “maxZoom”Get Signature
Section titled “Get Signature”get maxZoom():
number
Returns the maximum zoom scale from the diagram configuration.
Returns
Section titled “Returns”number
minZoom
Section titled “minZoom”Get Signature
Section titled “Get Signature”get minZoom():
number
Returns the minimum zoom scale from the diagram configuration.
Returns
Section titled “Returns”number
Methods
Section titled “Methods”centerOnNode()
Section titled “centerOnNode()”centerOnNode(
nodeOrId):void
Centers the Node within the current viewport bounds.
Parameters
Section titled “Parameters”nodeOrId
Section titled “nodeOrId”The ID of the node or the node object to center on.
string | Node
Returns
Section titled “Returns”void
Remarks
Section titled “Remarks”When calling centerOnNode() immediately after adding or modifying a node, its dimensions may not be measured yet.
Use the waitForMeasurements transaction option to ensure accurate centering:
await this.ngDiagramService.transaction(() => { this.modelService.addNodes([newNode]);}, { waitForMeasurements: true });this.viewportService.centerOnNode(newNode.id); // Now centers correctlycenterOnRect()
Section titled “centerOnRect()”centerOnRect(
rect):void
Centers the rectangle within the current viewport bounds.
Parameters
Section titled “Parameters”The rectangle to center on.
Returns
Section titled “Returns”void
clientToFlowPosition()
Section titled “clientToFlowPosition()”clientToFlowPosition(
clientPosition):Point
Converts a client position to a flow position.
Parameters
Section titled “Parameters”clientPosition
Section titled “clientPosition”Client position to convert.
Returns
Section titled “Returns”Flow position.
clientToFlowViewportPosition()
Section titled “clientToFlowViewportPosition()”clientToFlowViewportPosition(
clientPosition):Point
Converts a client position to a position relative to the flow viewport.
Parameters
Section titled “Parameters”clientPosition
Section titled “clientPosition”Client position.
Returns
Section titled “Returns”Position on the flow viewport.
flowToClientPosition()
Section titled “flowToClientPosition()”flowToClientPosition(
flowPosition):Point
Converts a flow position to a client position.
Parameters
Section titled “Parameters”flowPosition
Section titled “flowPosition”Flow position to convert.
Returns
Section titled “Returns”Client position.
moveViewport()
Section titled “moveViewport()”moveViewport(
x,y):void
Moves the viewport to the specified coordinates.
Parameters
Section titled “Parameters”number
The x-coordinate to move the viewport to.
number
The y-coordinate to move the viewport to.
Returns
Section titled “Returns”void
moveViewportBy()
Section titled “moveViewportBy()”moveViewportBy(
dx,dy):void
Moves the viewport by the specified amounts.
Parameters
Section titled “Parameters”number
The amount to move the viewport in the x-direction.
number
The amount to move the viewport in the y-direction.
Returns
Section titled “Returns”void
zoom()
Section titled “zoom()”zoom(
factor,center?):void
Zooms the viewport by the specified factor.
Parameters
Section titled “Parameters”factor
Section titled “factor”number
The factor to zoom by (e.g., 1.1 for 10% zoom in, 0.9 for 10% zoom out).
center?
Section titled “center?”The center point to zoom towards.
Returns
Section titled “Returns”void
zoomToFit()
Section titled “zoomToFit()”zoomToFit(
options?):void
Automatically adjusts the viewport to fit all diagram content (or a specified subset) within the visible area.
Parameters
Section titled “Parameters”options?
Section titled “options?”Optional configuration object
edgeIds?
Section titled “edgeIds?”string[]
Array of edge IDs to fit. If not provided, all edges are included.
nodeIds?
Section titled “nodeIds?”string[]
Array of node IDs to fit. If not provided, all nodes are included.
padding?
Section titled “padding?”number | [number, number] | [number, number, number] | [number, number, number, number]
Padding around the content (default: 50). Supports CSS-like syntax:
- Single number: uniform padding on all sides
- [top/bottom, left/right]: vertical and horizontal padding
- [top, left/right, bottom]: top, horizontal, bottom padding
- [top, right, bottom, left]: individual padding for each side
Returns
Section titled “Returns”void
Remarks
Section titled “Remarks”When calling zoomToFit() immediately after adding or modifying nodes/edges, their dimensions may not be measured yet.
Use the waitForMeasurements transaction option to ensure accurate results:
await this.ngDiagramService.transaction(() => { this.modelService.addNodes([newNode]);}, { waitForMeasurements: true });this.viewportService.zoomToFit(); // Now includes new node dimensionsExample
Section titled “Example”// Fit all nodes and edges with default paddingthis.viewportService.zoomToFit();
// Fit with custom uniform paddingthis.viewportService.zoomToFit({ padding: 100 });
// Fit with different padding on each side [top, right, bottom, left]this.viewportService.zoomToFit({ padding: [50, 100, 50, 100] });
// Fit only specific nodesthis.viewportService.zoomToFit({ nodeIds: ['node1', 'node2'] });