Skip to content

Commit 848250a

Browse files
committed
feat: adds scale bounds to zoom
1 parent 0e1c535 commit 848250a

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

packages/graph/src/components/Navigation.tsx

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,12 @@ const MAX_AXIS_SIZE = 10000;
1010

1111
const MIN_AXIS_SIZE = 0.025;
1212

13-
const limitScale = (scale: number, coordBox: BBox) => {
14-
const { horizontal, vertical } = coordBox;
15-
const hSize = Math.abs(horizontal.x - horizontal.y);
16-
const vSize = Math.abs(vertical.x - vertical.y);
17-
18-
if (scale > 1) {
19-
return Math.min(scale, MAX_AXIS_SIZE / hSize, MAX_AXIS_SIZE / vSize);
20-
}
21-
return Math.max(scale, MIN_AXIS_SIZE / hSize, MIN_AXIS_SIZE / vSize);
22-
};
23-
2413
const scaleBBoxAroundOrigin = (
2514
bbox: BBox,
2615
{ x, y }: Vec2,
2716
scale: number,
2817
{ x: offsetX, y: offsetY }: Vec2 = point(0, 0)
2918
) => {
30-
scale = limitScale(scale, bbox);
3119
let { x: hMin, y: hMax } = bbox.horizontal;
3220
let { x: vMin, y: vMax } = bbox.vertical;
3321
hMin += offsetX;
@@ -101,7 +89,6 @@ const Component = ({
10189
origin[0] - x,
10290
origin[1] - y,
10391
]);
104-
const normalizedCoordBox = normalizeBBox(rawCoordBox);
10592

10693
memo = {
10794
scaleOrigin,
@@ -110,6 +97,7 @@ const Component = ({
11097
};
11198
}
11299
if (!memo) return;
100+
const sensitivityFactor = 0.5;
113101

114102
onCoordBoxChange(
115103
scaleBBoxAroundOrigin(
@@ -132,6 +120,17 @@ const Component = ({
132120
pinchOnWheel: true,
133121
preventDefault: true,
134122
eventOptions: { passive: false },
123+
scaleBounds: (state) => {
124+
const { horizontal, vertical } = normalizedCoordBox;
125+
const s = state?.offset[0] || 1;
126+
const hSize = Math.abs(horizontal.x - horizontal.y) * s;
127+
const vSize = Math.abs(vertical.x - vertical.y) * s;
128+
129+
return {
130+
max: Math.min(MAX_AXIS_SIZE / hSize, MAX_AXIS_SIZE / vSize),
131+
min: Math.max(MIN_AXIS_SIZE / hSize, MIN_AXIS_SIZE / vSize),
132+
};
133+
},
135134
},
136135
}
137136
);

0 commit comments

Comments
 (0)