From 3fe9cfbf22d7b7ef782bf7d9f53b443a975ac4c0 Mon Sep 17 00:00:00 2001 From: moklick Date: Mon, 3 Aug 2020 23:48:12 +0200 Subject: [PATCH] refactor(fitView): consider min and max zoom --- src/store/index.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/store/index.ts b/src/store/index.ts index d70802eca..b99bc1f8a 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -424,19 +424,20 @@ export const storeModel: StoreModel = { fitView: action((state, payload = { padding: 0.1 }) => { const { padding } = payload; - const { nodes, width, height, d3Selection, d3Zoom } = state; + const { nodes, width, height, d3Selection, minZoom, maxZoom } = state; - if (!d3Selection || !d3Zoom || !nodes.length) { + if (!d3Selection || !nodes.length) { return; } const bounds = getRectOfNodes(nodes); const maxBoundsSize = Math.max(bounds.width, bounds.height); - const k = Math.min(width, height) / (maxBoundsSize + maxBoundsSize * padding); + const zoom = Math.min(width, height) / (maxBoundsSize + maxBoundsSize * padding); + const clampedZoom = clamp(zoom, minZoom, maxZoom); const boundsCenterX = bounds.x + bounds.width / 2; const boundsCenterY = bounds.y + bounds.height / 2; - const transform = [width / 2 - boundsCenterX * k, height / 2 - boundsCenterY * k]; - const fittedTransform = zoomIdentity.translate(transform[0], transform[1]).scale(k); + const transform = [width / 2 - boundsCenterX * clampedZoom, height / 2 - boundsCenterY * clampedZoom]; + const fittedTransform = zoomIdentity.translate(transform[0], transform[1]).scale(clampedZoom); // we need to sync the d3 zoom transform with the fitted transform d3Selection.property('__zoom', fittedTransform);