feat(utils): export getTransformForBounds func

This commit is contained in:
moklick
2021-03-01 14:02:42 +01:00
parent 89b2348c4b
commit 1745320101
3 changed files with 32 additions and 26 deletions
+21 -1
View File
@@ -1,6 +1,6 @@
import { Store } from 'redux';
import { clampPosition } from '../utils';
import { clampPosition, clamp } from '../utils';
import {
ElementId,
@@ -285,3 +285,23 @@ export const onLoadToObject = (currentStore: Store<ReactFlowState>) => {
};
};
};
export const getTransformForBounds = (
bounds: Rect,
width: number,
height: number,
minZoom: number,
maxZoom: number,
padding: number = 0.1
): Transform => {
const xZoom = width / (bounds.width * (1 + padding));
const yZoom = height / (bounds.height * (1 + padding));
const zoom = Math.min(xZoom, yZoom);
const clampedZoom = clamp(zoom, minZoom, maxZoom);
const boundsCenterX = bounds.x + bounds.width / 2;
const boundsCenterY = bounds.y + bounds.height / 2;
const x = width / 2 - boundsCenterX * clampedZoom;
const y = height / 2 - boundsCenterY * clampedZoom;
return [x, y, clampedZoom];
};