Merge pull request #5276 from xyflow/feat/viewport-transition-config
Feat/viewport transition config
This commit is contained in:
7
.changeset/late-taxis-press.md
Normal file
7
.changeset/late-taxis-press.md
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
'@xyflow/react': minor
|
||||
'@xyflow/svelte': minor
|
||||
'@xyflow/system': minor
|
||||
---
|
||||
|
||||
Add an `ease` and `interpolate` option to all function that alter the viewport
|
||||
@@ -170,7 +170,6 @@ const UseZoomPanHelperFlow = () => {
|
||||
onConnect={onConnect}
|
||||
onPaneClick={onPaneClick}
|
||||
fitView
|
||||
fitViewOptions={{ duration: 1200, padding: 0.2 }}
|
||||
maxZoom={Infinity}
|
||||
>
|
||||
<Panel position="top-right">
|
||||
@@ -183,7 +182,16 @@ const UseZoomPanHelperFlow = () => {
|
||||
console.log('fit view success');
|
||||
}}
|
||||
>
|
||||
fitView
|
||||
fitView default
|
||||
</button>
|
||||
<button
|
||||
onClick={async () => {
|
||||
console.log('fit view start');
|
||||
await fitView({ duration: 1200, padding: 0.3, ease: (t) => +t });
|
||||
console.log('fit view success');
|
||||
}}
|
||||
>
|
||||
fitView linear
|
||||
</button>
|
||||
<button onClick={onAddNode}>add node</button>
|
||||
<button onClick={onResetNodes}>reset nodes</button>
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
</script>
|
||||
|
||||
<main>
|
||||
<SvelteFlow bind:nodes bind:edges fitView>
|
||||
<SvelteFlow bind:nodes bind:edges fitView maxZoom={4}>
|
||||
<Controls />
|
||||
<Background variant={BackgroundVariant.Dots} />
|
||||
<MiniMap />
|
||||
|
||||
@@ -30,7 +30,10 @@
|
||||
<button onclick={() => zoomIn()}>zoom in</button>
|
||||
<button onclick={() => zoomOut({ duration: 1000 })}>zoom out transition</button>
|
||||
<button onclick={() => setZoom(2)}>set zoom</button>
|
||||
<button onclick={() => fitView()}>fitView</button>
|
||||
<button onclick={() => fitView({ duration: 600 })}>fitView</button>
|
||||
<button onclick={() => fitView({ duration: 600, ease: (t) => +t, interpolate: 'linear' })}
|
||||
>fitView linear</button
|
||||
>
|
||||
<button onclick={() => setCenter(0, 0)}>setCenter 0, 0</button>
|
||||
<button onclick={() => setViewport({ x: 100, y: 100, zoom: 2 })}>setViewport</button>
|
||||
<button onclick={() => console.log(getViewport())}>getViewport</button>
|
||||
|
||||
@@ -53,7 +53,7 @@ const useViewportHelper = (): ViewportHelperFunctions => {
|
||||
y: viewport.y ?? tY,
|
||||
zoom: viewport.zoom ?? tZoom,
|
||||
},
|
||||
{ duration: options?.duration }
|
||||
options
|
||||
);
|
||||
|
||||
return Promise.resolve(true);
|
||||
@@ -78,7 +78,7 @@ const useViewportHelper = (): ViewportHelperFunctions => {
|
||||
y: centerY,
|
||||
zoom: nextZoom,
|
||||
},
|
||||
{ duration: options?.duration }
|
||||
{ duration: options?.duration, ease: options?.ease, interpolate: options?.interpolate }
|
||||
);
|
||||
|
||||
return Promise.resolve(true);
|
||||
@@ -91,7 +91,11 @@ const useViewportHelper = (): ViewportHelperFunctions => {
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
|
||||
await panZoom.setViewport(viewport, { duration: options?.duration });
|
||||
await panZoom.setViewport(viewport, {
|
||||
duration: options?.duration,
|
||||
ease: options?.ease,
|
||||
interpolate: options?.interpolate,
|
||||
});
|
||||
|
||||
return Promise.resolve(true);
|
||||
},
|
||||
|
||||
@@ -149,6 +149,7 @@ export type ViewportHelperFunctions = {
|
||||
*
|
||||
* @param viewport - the viewport to set
|
||||
* @param options.duration - optional duration. If set, a transition will be applied
|
||||
* @param options.ease - optional ease function.
|
||||
*/
|
||||
setViewport: SetViewport;
|
||||
/**
|
||||
@@ -164,6 +165,8 @@ export type ViewportHelperFunctions = {
|
||||
* @param x - x position
|
||||
* @param y - y position
|
||||
* @param options.zoom - optional zoom
|
||||
* @param options.duration - optional duration. If set, a transition will be applied
|
||||
* @param options.ease - optional ease function.
|
||||
*/
|
||||
setCenter: SetCenter;
|
||||
/**
|
||||
@@ -173,6 +176,8 @@ export type ViewportHelperFunctions = {
|
||||
*
|
||||
* @param bounds - the bounds ({ x: number, y: number, width: number, height: number }) to fit the view to
|
||||
* @param options.padding - optional padding
|
||||
* @param options.duration - optional duration. If set, a transition will be applied
|
||||
* @param options.ease - optional ease function.
|
||||
*/
|
||||
fitBounds: FitBounds;
|
||||
/**
|
||||
|
||||
@@ -360,7 +360,7 @@ export function useSvelteFlow<NodeType extends Node = Node, EdgeType extends Edg
|
||||
y: nextViewport.y ?? currentViewport.y,
|
||||
zoom: nextViewport.zoom ?? currentViewport.zoom
|
||||
},
|
||||
{ duration: options?.duration }
|
||||
options
|
||||
);
|
||||
|
||||
return Promise.resolve(true);
|
||||
@@ -380,7 +380,7 @@ export function useSvelteFlow<NodeType extends Node = Node, EdgeType extends Edg
|
||||
y: store.height / 2 - y * nextZoom,
|
||||
zoom: nextZoom
|
||||
},
|
||||
{ duration: options?.duration }
|
||||
{ duration: options?.duration, ease: options?.ease, interpolate: options?.interpolate }
|
||||
);
|
||||
|
||||
return Promise.resolve(true);
|
||||
@@ -402,7 +402,11 @@ export function useSvelteFlow<NodeType extends Node = Node, EdgeType extends Edg
|
||||
options?.padding ?? 0.1
|
||||
);
|
||||
|
||||
await store.panZoom.setViewport(viewport, { duration: options?.duration });
|
||||
await store.panZoom.setViewport(viewport, {
|
||||
duration: options?.duration,
|
||||
ease: options?.ease,
|
||||
interpolate: options?.interpolate
|
||||
});
|
||||
|
||||
return Promise.resolve(true);
|
||||
},
|
||||
|
||||
@@ -51,10 +51,12 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/d3-drag": "^3.0.7",
|
||||
"@types/d3-interpolate": "^3.0.4",
|
||||
"@types/d3-selection": "^3.0.10",
|
||||
"@types/d3-transition": "^3.0.8",
|
||||
"@types/d3-zoom": "^3.0.8",
|
||||
"d3-drag": "^3.0.0",
|
||||
"d3-interpolate": "^3.0.1",
|
||||
"d3-selection": "^3.0.0",
|
||||
"d3-zoom": "^3.0.0"
|
||||
},
|
||||
|
||||
@@ -130,6 +130,8 @@ export type FitViewOptionsBase<NodeType extends NodeBase = NodeBase> = {
|
||||
minZoom?: number;
|
||||
maxZoom?: number;
|
||||
duration?: number;
|
||||
ease?: (t: number) => number;
|
||||
interpolate?: 'smooth' | 'linear';
|
||||
nodes?: (NodeType | { id: string })[];
|
||||
};
|
||||
|
||||
@@ -173,6 +175,8 @@ export enum PanOnScrollMode {
|
||||
*/
|
||||
export type ViewportHelperFunctionOptions = {
|
||||
duration?: number;
|
||||
ease?: (t: number) => number;
|
||||
interpolate?: 'smooth' | 'linear';
|
||||
};
|
||||
|
||||
export type SetCenterOptions = ViewportHelperFunctionOptions & {
|
||||
|
||||
@@ -20,6 +20,8 @@ export type PanZoomParams = {
|
||||
|
||||
export type PanZoomTransformOptions = {
|
||||
duration?: number;
|
||||
ease?: (t: number) => number;
|
||||
interpolate?: 'smooth' | 'linear';
|
||||
};
|
||||
|
||||
export type OnPanZoom = (event: MouseEvent | TouchEvent | null, viewport: Viewport) => void;
|
||||
|
||||
@@ -378,7 +378,11 @@ export async function fitViewport<
|
||||
options?.padding ?? 0.1
|
||||
);
|
||||
|
||||
await panZoom.setViewport(viewport, { duration: options?.duration });
|
||||
await panZoom.setViewport(viewport, {
|
||||
duration: options?.duration,
|
||||
ease: options?.ease,
|
||||
interpolate: options?.interpolate,
|
||||
});
|
||||
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { type ZoomTransform, zoom, zoomTransform } from 'd3-zoom';
|
||||
import { select } from 'd3-selection';
|
||||
import { interpolateZoom, interpolate } from 'd3-interpolate';
|
||||
|
||||
import {
|
||||
type CoordinateExtent,
|
||||
@@ -19,6 +20,7 @@ import {
|
||||
createZoomOnScrollHandler,
|
||||
} from './eventhandler';
|
||||
import { createFilter } from './filter';
|
||||
import { transition } from 'd3-transition';
|
||||
|
||||
export type ZoomPanValues = {
|
||||
isZoomingOrPanning: boolean;
|
||||
@@ -78,8 +80,8 @@ export function XYPanZoom({
|
||||
function setTransform(transform: ZoomTransform, options?: PanZoomTransformOptions) {
|
||||
if (d3Selection) {
|
||||
return new Promise<boolean>((resolve) => {
|
||||
d3ZoomInstance?.transform(
|
||||
getD3Transition(d3Selection, options?.duration, () => resolve(true)),
|
||||
d3ZoomInstance?.interpolate(options?.interpolate === 'linear' ? interpolate : interpolateZoom).transform(
|
||||
getD3Transition(d3Selection, options?.duration, options?.ease, () => resolve(true)),
|
||||
transform
|
||||
);
|
||||
});
|
||||
@@ -114,22 +116,22 @@ export function XYPanZoom({
|
||||
|
||||
const wheelHandler = isPanOnScroll
|
||||
? createPanOnScrollHandler({
|
||||
zoomPanValues,
|
||||
noWheelClassName,
|
||||
d3Selection,
|
||||
d3Zoom: d3ZoomInstance,
|
||||
panOnScrollMode,
|
||||
panOnScrollSpeed,
|
||||
zoomOnPinch,
|
||||
onPanZoomStart,
|
||||
onPanZoom,
|
||||
onPanZoomEnd,
|
||||
})
|
||||
zoomPanValues,
|
||||
noWheelClassName,
|
||||
d3Selection,
|
||||
d3Zoom: d3ZoomInstance,
|
||||
panOnScrollMode,
|
||||
panOnScrollSpeed,
|
||||
zoomOnPinch,
|
||||
onPanZoomStart,
|
||||
onPanZoom,
|
||||
onPanZoomEnd,
|
||||
})
|
||||
: createZoomOnScrollHandler({
|
||||
noWheelClassName,
|
||||
preventScrolling,
|
||||
d3ZoomHandler,
|
||||
});
|
||||
noWheelClassName,
|
||||
preventScrolling,
|
||||
d3ZoomHandler,
|
||||
});
|
||||
|
||||
d3Selection.on('wheel.zoom', wheelHandler, { passive: false });
|
||||
|
||||
@@ -242,8 +244,8 @@ export function XYPanZoom({
|
||||
function scaleTo(zoom: number, options?: PanZoomTransformOptions) {
|
||||
if (d3Selection) {
|
||||
return new Promise<boolean>((resolve) => {
|
||||
d3ZoomInstance?.scaleTo(
|
||||
getD3Transition(d3Selection, options?.duration, () => resolve(true)),
|
||||
d3ZoomInstance?.interpolate(options?.interpolate === 'linear' ? interpolate : interpolateZoom).scaleTo(
|
||||
getD3Transition(d3Selection, options?.duration, options?.ease, () => resolve(true)),
|
||||
zoom
|
||||
);
|
||||
});
|
||||
@@ -255,8 +257,8 @@ export function XYPanZoom({
|
||||
function scaleBy(factor: number, options?: PanZoomTransformOptions) {
|
||||
if (d3Selection) {
|
||||
return new Promise<boolean>((resolve) => {
|
||||
d3ZoomInstance?.scaleBy(
|
||||
getD3Transition(d3Selection, options?.duration, () => resolve(true)),
|
||||
d3ZoomInstance?.interpolate(options?.interpolate === 'linear' ? interpolate : interpolateZoom).scaleBy(
|
||||
getD3Transition(d3Selection, options?.duration, options?.ease, () => resolve(true)),
|
||||
factor
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { type ZoomTransform, zoomIdentity } from 'd3-zoom';
|
||||
import { transition } from 'd3-transition';
|
||||
|
||||
import { type D3SelectionInstance, type Viewport } from '../types';
|
||||
import { isMacOs } from '../utils';
|
||||
@@ -21,14 +22,17 @@ export const isWrappedWithClass = (event: any, className: string | undefined) =>
|
||||
export const isRightClickPan = (panOnDrag: boolean | number[], usedButton: number) =>
|
||||
usedButton === 2 && Array.isArray(panOnDrag) && panOnDrag.includes(2);
|
||||
|
||||
export const getD3Transition = (selection: D3SelectionInstance, duration = 0, onEnd = () => {}) => {
|
||||
// taken from d3-ease: https://github.com/d3/d3-ease/blob/main/src/cubic.js
|
||||
const defaultEase = (t: number) => ((t *= 2) <= 1 ? t * t * t : (t -= 2) * t * t + 2) / 2;
|
||||
|
||||
export const getD3Transition = (selection: D3SelectionInstance, duration = 0, ease = defaultEase, onEnd = () => {}) => {
|
||||
const hasDuration = typeof duration === 'number' && duration > 0;
|
||||
|
||||
if (!hasDuration) {
|
||||
onEnd();
|
||||
}
|
||||
|
||||
return hasDuration ? selection.transition().duration(duration).on('end', onEnd) : selection;
|
||||
return hasDuration ? selection.transition().duration(duration).ease(ease).on('end', onEnd) : selection;
|
||||
};
|
||||
|
||||
export const wheelDelta = (event: any) => {
|
||||
|
||||
6
pnpm-lock.yaml
generated
6
pnpm-lock.yaml
generated
@@ -372,6 +372,9 @@ importers:
|
||||
'@types/d3-drag':
|
||||
specifier: ^3.0.7
|
||||
version: 3.0.7
|
||||
'@types/d3-interpolate':
|
||||
specifier: ^3.0.4
|
||||
version: 3.0.4
|
||||
'@types/d3-selection':
|
||||
specifier: ^3.0.10
|
||||
version: 3.0.10
|
||||
@@ -384,6 +387,9 @@ importers:
|
||||
d3-drag:
|
||||
specifier: ^3.0.0
|
||||
version: 3.0.0
|
||||
d3-interpolate:
|
||||
specifier: ^3.0.1
|
||||
version: 3.0.1
|
||||
d3-selection:
|
||||
specifier: ^3.0.0
|
||||
version: 3.0.0
|
||||
|
||||
Reference in New Issue
Block a user