Skip to content

NgDiagramViewportService

since v0.8.0

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

canZoomIn: Signal<boolean>

Returns true if the current zoom level is below the maximum and can be increased.


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: Signal<Viewport>

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

get maxZoom(): number

Returns the maximum zoom scale from the diagram configuration.

number


get minZoom(): number

Returns the minimum zoom scale from the diagram configuration.

number

centerOnNode(nodeOrId): Promise<void>

Centers the Node within the current viewport bounds.

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

string | Node

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.

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 correctly

centerOnRect(rect): Promise<void>

Centers the rectangle within the current viewport bounds.

Rect

The rectangle to center on.

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.


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): Promise<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.

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.


moveViewportBy(dx, dy): Promise<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.

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.


setViewport(x, y, scale): Promise<void>

Sets the viewport to an absolute position and scale.

number

The absolute x-coordinate for the viewport.

number

The absolute y-coordinate for the viewport.

number

The absolute zoom scale (clamped to configured min/max).

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.

// Reset to origin at 50% zoom
this.viewportService.setViewport(0, 0, 0.5);

1.2.0


zoom(factor, center?): Promise<void>

Zooms the viewport by the specified factor.

number

The factor to zoom by (e.g., 1.1 for 10% zoom in, 0.9 for 10% zoom out).

Point

The center point to zoom towards.

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.


zoomToFit(options?): Promise<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, only edges with both ends among the fitted nodes are included. When nodeIds is not provided either, 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

Promise<void>

Always await the preceding model mutation (e.g. await modelService.deleteNodes(...)) before calling zoomToFit() — an un-awaited mutation is not yet committed when zoomToFit() reads the model, so the viewport would fit the old content.

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 dimensions
// 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'] });
// Custom zoomToFit with anchor positioning using setViewport
// anchor: (0,0) = top-left, (0.5,0.5) = center, (1,1) = bottom-right
const anchor = { x: 0.5, y: 0.5 };
const { width, height } = this.modelService.metadata().viewport;
const bounds = this.modelService.computePartsBounds(nodes, edges);
if (!bounds.width || !bounds.height) return; // nothing measurable to fit
const scale = Math.min(width / bounds.width, height / bounds.height);
const x = width * anchor.x - (bounds.x + bounds.width * anchor.x) * scale;
const y = height * anchor.y - (bounds.y + bounds.height * anchor.y) * scale;
this.viewportService.setViewport(x, y, scale);