NgDiagramViewportService
The 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”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.
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
centerOnRect()
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.
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
Example
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'] });