getViewportCenter method
Gets the center point of the current viewport in graph coordinates.
This is useful for determining where to place new nodes so they appear in the center of the visible area.
Returns GraphPosition.zero if the screen size is zero.
Returns the center point of the viewport in graph coordinates.
Example:
// Get the viewport center to place a new node there
final center = controller.getViewportCenter();
final newNode = Node(
id: 'new-node',
type: 'process',
position: center.offset, // Node will appear at viewport center
);
controller.addNode(newNode);
Implementation
GraphPosition getViewportCenter() {
if (_screenSize.value == Size.zero) return GraphPosition.zero;
// Get the screen center point
final screenCenter = ScreenPosition.fromXY(
_screenSize.value.width / 2,
_screenSize.value.height / 2,
);
// Convert to graph coordinates
return screenToGraph(screenCenter);
}