Skip to content

Commit 1d3a88b

Browse files
authored
Merge pull request #368 from reaviz/anton/min-max-zoom-fix
Fix for the Min/Max distance #217
2 parents b573bb1 + 6676c22 commit 1d3a88b

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/GraphCanvas/GraphCanvas.tsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,38 @@ export const GraphCanvas: FC<GraphCanvasProps & { ref?: Ref<GraphCanvasRef> }> =
148148
rendererRef.current?.centerGraph(nodeIds, opts),
149149
fitNodesInView: (nodeIds, opts) =>
150150
rendererRef.current?.fitNodesInView(nodeIds, opts),
151-
zoomIn: () => controlsRef.current?.zoomIn(),
152-
zoomOut: () => controlsRef.current?.zoomOut(),
151+
zoomIn: () => {
152+
const controls = controlsRef.current?.controls;
153+
if (!controls) return;
154+
155+
const currentDistance = controls.distance;
156+
const currentZoom = controls.camera.zoom;
157+
158+
// Calculate what the new zoom would be has to match CameraControls logic
159+
const newZoom = currentZoom + currentZoom / 2;
160+
const newEffectiveDistance = currentDistance / newZoom;
161+
162+
// Check if zooming in would violate minDistance constraint
163+
if (!minDistance || newEffectiveDistance >= minDistance) {
164+
controlsRef.current?.zoomIn();
165+
}
166+
},
167+
zoomOut: () => {
168+
const controls = controlsRef.current?.controls;
169+
if (!controls) return;
170+
171+
const currentDistance = controls.distance;
172+
const currentZoom = controls.camera.zoom;
173+
174+
// Calculate what the new zoom would be (matches CameraControls logic)
175+
const newZoom = currentZoom - currentZoom / 2;
176+
const newEffectiveDistance = currentDistance / newZoom;
177+
178+
// Check if zooming out would violate maxDistance constraint
179+
if (!maxDistance || newEffectiveDistance <= maxDistance) {
180+
controlsRef.current?.zoomOut();
181+
}
182+
},
153183
dollyIn: distance => controlsRef.current?.dollyIn(distance),
154184
dollyOut: distance => controlsRef.current?.dollyOut(distance),
155185
panLeft: () => controlsRef.current?.panLeft(),

stories/demos/Basic.story.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ export const Circular = () => (
280280
source: '1',
281281
target: '1',
282282
id: '1-1',
283-
label: '1-1',
283+
label: '1-1'
284284
},
285285
{
286286
source: '2',
@@ -295,7 +295,7 @@ export const Circular = () => (
295295
id: '3-3',
296296
label: '3-3',
297297
arrowPlacement: 'none'
298-
},
298+
}
299299
]}
300300
/>
301301
);

0 commit comments

Comments
 (0)