From 104133de96feed262721f4033795cc7dfe5807d0 Mon Sep 17 00:00:00 2001 From: Grant Steffen Date: Wed, 13 Mar 2024 12:39:47 -0700 Subject: [PATCH 1/2] Re-center camera when nodes update --- src/CameraControls/useCenterGraph.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/CameraControls/useCenterGraph.ts b/src/CameraControls/useCenterGraph.ts index ee68e2d5..6fc1bce3 100644 --- a/src/CameraControls/useCenterGraph.ts +++ b/src/CameraControls/useCenterGraph.ts @@ -139,11 +139,14 @@ export const useCenterGraph = ({ if (controls && nodes?.length && !mounted.current) { await centerNodes(nodes, false); mounted.current = true; + // If node positions have changed and some aren't in view, center the graph + } else if (controls && nodes?.length) { + await centerNodes(nodes, animated); } } load(); - }, [controls, centerNodes, nodes]); + }, [controls, centerNodes, nodes, animated]); useHotkeys([ { From 36c0e34df2ecdd63743810525303b0065d8a5e41 Mon Sep 17 00:00:00 2001 From: amcdnl Date: Mon, 18 Mar 2024 05:57:55 -0400 Subject: [PATCH 2/2] tweak logic --- src/CameraControls/useCenterGraph.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/CameraControls/useCenterGraph.ts b/src/CameraControls/useCenterGraph.ts index 6fc1bce3..d07201ad 100644 --- a/src/CameraControls/useCenterGraph.ts +++ b/src/CameraControls/useCenterGraph.ts @@ -135,13 +135,16 @@ export const useCenterGraph = ({ useLayoutEffect(() => { async function load() { - // Center the graph once nodes are loaded on mount - if (controls && nodes?.length && !mounted.current) { - await centerNodes(nodes, false); - mounted.current = true; - // If node positions have changed and some aren't in view, center the graph - } else if (controls && nodes?.length) { - await centerNodes(nodes, animated); + // Once we've loaded controls and we have nodes, let's recenter + if (controls && nodes?.length) { + if (!mounted.current) { + // Center the graph once nodes are loaded on mount + await centerNodes(nodes, false); + mounted.current = true; + } else { + // If node positions have changed and some aren't in view, center the graph + await centerNodes(nodes, animated); + } } }