Add pixel units and left and right padding options for fitView

This commit is contained in:
peterkogo
2025-03-05 17:38:30 +01:00
parent cb685281d0
commit cd03c46196
3 changed files with 13 additions and 5 deletions

View File

@@ -95,7 +95,8 @@ export type FitViewParamsBase<NodeType extends NodeBase> = {
* @inline
*/
export type FitViewOptionsBase<NodeType extends NodeBase = NodeBase> = {
padding?: number;
padding?: number | [number, number];
paddingUnit?: 'px' | '%';
includeHiddenNodes?: boolean;
minZoom?: number;
maxZoom?: number;

View File

@@ -195,10 +195,16 @@ export const getViewportForBounds = (
height: number,
minZoom: number,
maxZoom: number,
padding: number
padding: number | [number, number],
paddingUnit: 'px' | '%'
): Viewport => {
const xZoom = width / (bounds.width * (1 + padding));
const yZoom = height / (bounds.height * (1 + padding));
const [paddingX, paddingY] = Array.isArray(padding) ? [padding[0], padding[1]] : [padding, padding];
const isPixelPadding = paddingUnit === 'px';
const xZoom = width / (isPixelPadding ? bounds.width + paddingX : bounds.width * (1 + paddingX));
const yZoom = height / (isPixelPadding ? bounds.height + paddingY : bounds.height * (1 + paddingY));
const zoom = Math.min(xZoom, yZoom);
const clampedZoom = clamp(zoom, minZoom, maxZoom);
const boundsCenterX = bounds.x + bounds.width / 2;

View File

@@ -372,7 +372,8 @@ export async function fitViewport<
height,
options?.minZoom ?? minZoom,
options?.maxZoom ?? maxZoom,
options?.padding ?? 0.1
options?.padding ?? 0.1,
options?.paddingUnit ?? '%'
);
await panZoom.setViewport(viewport, { duration: options?.duration });