feat(utils): export getTransformForBounds func
This commit is contained in:
+21
-1
@@ -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];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user