resetViewport method
void
resetViewport()
Resets the viewport to zoom 1.0 and centers on all nodes in the graph.
If there are no nodes, resets to origin (0, 0) with zoom 1.0. If there are nodes, centers the viewport on their geometric center.
Example:
controller.resetViewport(); // Reset to default view
Implementation
void resetViewport() {
const zoom = 1.0;
// If there are no nodes, just reset to origin
if (_nodes.isEmpty || _screenSize.value == Size.zero) {
setViewport(GraphViewport(x: 0, y: 0, zoom: zoom));
return;
}
// Calculate center of all nodes
final bounds = nodesBounds;
if (bounds == Rect.zero) {
setViewport(GraphViewport(x: 0, y: 0, zoom: zoom));
return;
}
final centerX = bounds.left + bounds.width / 2;
final centerY = bounds.top + bounds.height / 2;
// Center the viewport on the content
setViewport(
GraphViewport(
x: _screenSize.value.width / 2 - centerX * zoom,
y: _screenSize.value.height / 2 - centerY * zoom,
zoom: zoom,
),
);
}