Skip to content

Commit c751240

Browse files
committed
fix(graph-ui): keep the camera on the focused node after fly-to
The fly-to animation moved only the camera and called lookAt, leaving the OrbitControls pivot at the origin, so OrbitControls re-centred the view on the next frame and snapped the camera back once the animation ended. Lerp the controls target to the focus point as well so the view stays on the clicked node or cluster and orbits around it afterwards. Signed-off-by: Zadak <rarepops@protonmail.com>
1 parent dc8c5be commit c751240

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

graph-ui/src/components/GraphScene.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ interface CameraTarget {
1717
lookAt: THREE.Vector3;
1818
}
1919

20-
function CameraAnimator({ target }: { target: CameraTarget | null }) {
20+
function CameraAnimator({
21+
target,
22+
controlsRef,
23+
}: {
24+
target: CameraTarget | null;
25+
controlsRef: React.RefObject<OrbitControlsImpl | null>;
26+
}) {
2127
const { camera } = useThree();
2228
const targetRef = useRef<CameraTarget | null>(null);
2329
const progress = useRef(1);
@@ -36,7 +42,17 @@ function CameraAnimator({ target }: { target: CameraTarget | null }) {
3642
const t = 1 - Math.pow(1 - progress.current, 3); /* ease-out cubic */
3743

3844
camera.position.lerp(targetRef.current.position, t * 0.08);
39-
camera.lookAt(targetRef.current.lookAt);
45+
46+
/* Move the OrbitControls pivot to the focus point as well. Otherwise the
47+
* controls keep their target at the origin and re-center the view on the
48+
* next frame, snapping the camera back to the middle after the fly-to. */
49+
const controls = controlsRef.current;
50+
if (controls) {
51+
controls.target.lerp(targetRef.current.lookAt, t * 0.08);
52+
controls.update();
53+
} else {
54+
camera.lookAt(targetRef.current.lookAt);
55+
}
4056
});
4157

4258
return null;
@@ -179,7 +195,7 @@ export function GraphScene({
179195

180196
{hovered && <NodeTooltip node={hovered} />}
181197

182-
<CameraAnimator target={cameraTarget} />
198+
<CameraAnimator target={cameraTarget} controlsRef={controlsRef} />
183199
<IdleAutoRotate controlsRef={controlsRef} />
184200

185201
<EffectComposer multisampling={GRAPH_COMPOSER_MULTISAMPLING}>

0 commit comments

Comments
 (0)