From 073b25ec606d0aca14572bebc0251d14301e2584 Mon Sep 17 00:00:00 2001 From: Braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Tue, 23 Nov 2021 23:50:10 +0100 Subject: [PATCH] feat(zoom): Add offset to fitview-params * Allow for offsetting fit view by x or y values Signed-off-by: Braks <78412429+bcakmakoglu@users.noreply.github.com> --- src/composables/useZoomPanHelper.ts | 1 + src/types/flow.ts | 4 ++++ src/utils/graph.ts | 8 ++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/composables/useZoomPanHelper.ts b/src/composables/useZoomPanHelper.ts index df40bc49..22da9637 100644 --- a/src/composables/useZoomPanHelper.ts +++ b/src/composables/useZoomPanHelper.ts @@ -25,6 +25,7 @@ export default (store = useStore()): UseZoomPanHelper => { options.minZoom ?? store.minZoom, options.maxZoom ?? store.maxZoom, options.padding ?? DEFAULT_PADDING, + options.offset, ) const transform = zoomIdentity.translate(x, y).scale(zoom) diff --git a/src/types/flow.ts b/src/types/flow.ts index 6b322c56..3d3ecdf2 100644 --- a/src/types/flow.ts +++ b/src/types/flow.ts @@ -63,6 +63,10 @@ export type FitViewParams = { includeHiddenNodes?: boolean minZoom?: number maxZoom?: number + offset?: { + x?: number + y?: number + } } export type FlowExportObject = { diff --git a/src/utils/graph.ts b/src/utils/graph.ts index 4798af03..443891dc 100644 --- a/src/utils/graph.ts +++ b/src/utils/graph.ts @@ -291,6 +291,10 @@ export const getTransformForBounds = ( minZoom: number, maxZoom: number, padding = 0.1, + offset: { + x?: number + y?: number + } = { x: 0, y: 0 }, ): Transform => { const xZoom = width / (bounds.width * (1 + padding)) const yZoom = height / (bounds.height * (1 + padding)) @@ -298,8 +302,8 @@ export const getTransformForBounds = ( 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 + const x = width / 2 - boundsCenterX * clampedZoom + (offset.x ?? 0) + const y = height / 2 - boundsCenterY * clampedZoom + (offset.y ?? 0) return [x, y, clampedZoom] }