Skip to content

NgDiagramViewportService

The NgDiagramViewportService provides methods and signals for interacting with the diagram viewport.

private viewportService = inject(NgDiagramViewportService);
// Move viewport to (100, 200)
this.viewportService.moveViewport(100, 200);
// Zoom in by a factor of 1.2
this.viewportService.zoom(1.2);
  • NgDiagramBaseService

scale: Signal<number>

Returns a computed signal for the scale that safely handles uninitialized state.


viewport: Signal<Viewport>

Returns a computed signal for the viewport that safely handles uninitialized state.

centerOnNode(nodeOrId): void

Centers the Node within the current viewport bounds.

The ID of the node or the node object to center on.

string | Node

void


centerOnRect(rect): void

Centers the rectangle within the current viewport bounds.

Rect

The rectangle to center on.

void


clientToFlowPosition(clientPosition): Point

Converts a client position to a flow position.

Point

Client position to convert.

Point

Flow position.


clientToFlowViewportPosition(clientPosition): Point

Converts a client position to a position relative to the flow viewport.

Point

Client position.

Point

Position on the flow viewport.


flowToClientPosition(flowPosition): Point

Converts a flow position to a client position.

Point

Flow position to convert.

Point

Client position.


moveViewport(x, y): void

Moves the viewport to the specified coordinates.

number

The x-coordinate to move the viewport to.

number

The y-coordinate to move the viewport to.

void


moveViewportBy(dx, dy): void

Moves the viewport by the specified amounts.

number

The amount to move the viewport in the x-direction.

number

The amount to move the viewport in the y-direction.

void


zoom(factor, center?): void

Zooms the viewport by the specified factor.

number

The factor to zoom by.

Point

The center point to zoom towards.

void


zoomToFit(options?): void

Automatically adjusts the viewport to fit all diagram content (or a specified subset) within the visible area.

Optional configuration object

string[]

Array of edge IDs to fit. If not provided, all edges are included.

string[]

Array of node IDs to fit. If not provided, all nodes are included.

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

void

// Fit all nodes and edges with default padding
this.viewportService.zoomToFit();
// Fit with custom uniform padding
this.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 nodes
this.viewportService.zoomToFit({ nodeIds: ['node1', 'node2'] });