react-zoom-pan-pinch 2.1.3 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/README.md +43 -0
  2. package/dist/index.cjs.js +60 -34
  3. package/dist/index.cjs.js.map +1 -1
  4. package/dist/index.d.ts +9 -1
  5. package/dist/index.esm.js +60 -34
  6. package/dist/index.esm.js.map +1 -1
  7. package/package.json +5 -4
  8. package/src/.eslintrc.js +36 -0
  9. package/src/components/transform-component.module.css +30 -0
  10. package/src/components/transform-component.tsx +52 -0
  11. package/src/components/transform-context.tsx +493 -0
  12. package/src/components/transform-wrapper.tsx +18 -0
  13. package/src/constants/state.constants.ts +88 -0
  14. package/src/core/animations/animations.constants.ts +76 -0
  15. package/src/core/animations/animations.utils.ts +109 -0
  16. package/src/core/bounds/bounds.types.ts +8 -0
  17. package/src/core/bounds/bounds.utils.ts +157 -0
  18. package/src/core/double-click/double-click.logic.ts +75 -0
  19. package/src/core/handlers/handlers.logic.ts +112 -0
  20. package/src/core/handlers/handlers.utils.ts +202 -0
  21. package/src/core/pan/panning.logic.ts +99 -0
  22. package/src/core/pan/panning.utils.ts +199 -0
  23. package/src/core/pan/velocity.logic.ts +148 -0
  24. package/src/core/pan/velocity.utils.ts +79 -0
  25. package/src/core/pinch/pinch.logic.ts +83 -0
  26. package/src/core/pinch/pinch.utils.ts +88 -0
  27. package/src/core/wheel/wheel.logic.ts +118 -0
  28. package/src/core/wheel/wheel.utils.ts +121 -0
  29. package/src/core/zoom/zoom.logic.ts +69 -0
  30. package/src/core/zoom/zoom.utils.ts +58 -0
  31. package/src/hooks/use-transform-context.tsx +16 -0
  32. package/src/index.ts +6 -0
  33. package/src/models/calculations.model.ts +26 -0
  34. package/src/models/context.model.ts +179 -0
  35. package/src/models/helpers.model.ts +15 -0
  36. package/src/models/index.ts +3 -0
  37. package/src/react-app-env.d.ts +1 -0
  38. package/src/stories/assets/big-image.jpeg +0 -0
  39. package/src/stories/assets/medium-image.jpg +0 -0
  40. package/src/stories/assets/small-image.jpg +0 -0
  41. package/src/stories/assets/svg-example.svg +14 -0
  42. package/src/stories/docs/Introduction.stories.mdx +97 -0
  43. package/src/stories/docs/handlers.stories.mdx +48 -0
  44. package/src/stories/docs/handlers.tsx +107 -0
  45. package/src/stories/docs/props.stories.mdx +92 -0
  46. package/src/stories/docs/props.tsx +490 -0
  47. package/src/stories/examples/content/content.stories.mdx +122 -0
  48. package/src/stories/examples/content-rerendering/content-updating.stories.mdx +42 -0
  49. package/src/stories/examples/content-rerendering/example.tsx +74 -0
  50. package/src/stories/examples/controls/controls.stories.mdx +71 -0
  51. package/src/stories/examples/image-big/image-big.stories.mdx +44 -0
  52. package/src/stories/examples/image-medium/image-medium.stories.mdx +42 -0
  53. package/src/stories/examples/image-small/image-small.stories.mdx +42 -0
  54. package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +47 -0
  55. package/src/stories/examples/zoom-to-element/example.tsx +91 -0
  56. package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +28 -0
  57. package/src/stories/types/args.types.ts +201 -0
  58. package/src/stories/utils/args.utils.ts +34 -0
  59. package/src/stories/utils/code.utils.tsx +21 -0
  60. package/src/stories/utils/controls.utils.tsx +26 -0
  61. package/src/stories/utils/index.ts +3 -0
  62. package/src/stories/utils/styles.module.css +29 -0
  63. package/src/styles.css +8 -0
  64. package/src/typings.d.ts +10 -0
  65. package/src/utils/calculations.utils.ts +16 -0
  66. package/src/utils/callback.utils.ts +11 -0
  67. package/src/utils/context.utils.ts +27 -0
  68. package/src/utils/event.utils.ts +22 -0
  69. package/src/utils/helpers.utils.ts +27 -0
  70. package/src/utils/index.ts +7 -0
  71. package/src/utils/state.utils.ts +40 -0
  72. package/src/utils/styles.utils.ts +35 -0
@@ -0,0 +1,202 @@
1
+ import { ReactZoomPanPinchContext } from "../../models";
2
+ import { animations } from "../animations/animations.constants";
3
+ import { handleZoomToPoint } from "../zoom/zoom.logic";
4
+ import { animate } from "../animations/animations.utils";
5
+ import { createState } from "../../utils/state.utils";
6
+ import { checkZoomBounds } from "../zoom/zoom.utils";
7
+ import { roundNumber } from "../../utils";
8
+ import { initialState } from "../../constants/state.constants";
9
+ import { PositionType } from "../../models/calculations.model";
10
+ import {
11
+ calculateBounds,
12
+ getMouseBoundedPosition,
13
+ } from "core/bounds/bounds.utils";
14
+
15
+ export const handleCalculateButtonZoom = (
16
+ contextInstance: ReactZoomPanPinchContext,
17
+ delta: number,
18
+ step: number,
19
+ ): number => {
20
+ const { scale } = contextInstance.transformState;
21
+ const { wrapperComponent, setup } = contextInstance;
22
+ const { maxScale, minScale, zoomAnimation } = setup;
23
+ const { size } = zoomAnimation;
24
+
25
+ if (!wrapperComponent) {
26
+ throw new Error("Wrapper is not mounted");
27
+ }
28
+
29
+ const targetScale = scale * Math.exp(delta * step);
30
+
31
+ const newScale = checkZoomBounds(
32
+ roundNumber(targetScale, 3),
33
+ minScale,
34
+ maxScale,
35
+ size,
36
+ false,
37
+ );
38
+ return newScale;
39
+ };
40
+
41
+ export function handleZoomToViewCenter(
42
+ contextInstance: ReactZoomPanPinchContext,
43
+ delta: number,
44
+ step: number,
45
+ animationTime: number,
46
+ animationType: keyof typeof animations,
47
+ ): void {
48
+ const { wrapperComponent } = contextInstance;
49
+ const { scale, positionX, positionY } = contextInstance.transformState;
50
+
51
+ if (!wrapperComponent) return console.error("No WrapperComponent found");
52
+
53
+ const wrapperWidth = wrapperComponent.offsetWidth;
54
+ const wrapperHeight = wrapperComponent.offsetHeight;
55
+ const mouseX = (wrapperWidth / 2 - positionX) / scale;
56
+ const mouseY = (wrapperHeight / 2 - positionY) / scale;
57
+
58
+ const newScale = handleCalculateButtonZoom(contextInstance, delta, step);
59
+
60
+ const targetState = handleZoomToPoint(
61
+ contextInstance,
62
+ newScale,
63
+ mouseX,
64
+ mouseY,
65
+ );
66
+
67
+ if (!targetState) {
68
+ return console.error(
69
+ "Error during zoom event. New transformation state was not calculated.",
70
+ );
71
+ }
72
+
73
+ animate(contextInstance, targetState, animationTime, animationType);
74
+ }
75
+
76
+ export function resetTransformations(
77
+ contextInstance: ReactZoomPanPinchContext,
78
+ animationTime: number,
79
+ animationType: keyof typeof animations,
80
+ ): void {
81
+ const { setup, wrapperComponent } = contextInstance;
82
+ const { limitToBounds } = setup;
83
+ const initialTransformation = createState(contextInstance.props);
84
+ const { scale, positionX, positionY } = contextInstance.transformState;
85
+
86
+ if (!wrapperComponent) return;
87
+
88
+ const newBounds = calculateBounds(
89
+ contextInstance,
90
+ initialTransformation.scale,
91
+ );
92
+
93
+ const boundedPositions = getMouseBoundedPosition(
94
+ initialTransformation.positionX,
95
+ initialTransformation.positionY,
96
+ newBounds,
97
+ limitToBounds,
98
+ 0,
99
+ 0,
100
+ wrapperComponent,
101
+ );
102
+
103
+ const newState = {
104
+ scale: initialTransformation.scale,
105
+ positionX: boundedPositions.x,
106
+ positionY: boundedPositions.y,
107
+ };
108
+
109
+ if (
110
+ scale === initialTransformation.scale &&
111
+ positionX === initialTransformation.positionX &&
112
+ positionY === initialTransformation.positionY
113
+ ) {
114
+ return;
115
+ }
116
+
117
+ animate(contextInstance, newState, animationTime, animationType);
118
+ }
119
+
120
+ export function calculateZoomToNode(
121
+ contextInstance: ReactZoomPanPinchContext,
122
+ node: HTMLElement,
123
+ customZoom?: number,
124
+ ): { positionX: number; positionY: number; scale: number } {
125
+ const { wrapperComponent } = contextInstance;
126
+ const { limitToBounds, minScale, maxScale } = contextInstance.setup;
127
+ if (!wrapperComponent) return initialState;
128
+
129
+ const wrapperRect = wrapperComponent.getBoundingClientRect();
130
+ const nodeRect = getOffset(node);
131
+
132
+ const nodeLeft = nodeRect.x;
133
+ const nodeTop = nodeRect.y;
134
+ const nodeWidth = node.offsetWidth;
135
+ const nodeHeight = node.offsetHeight;
136
+
137
+ const scaleX = wrapperComponent.offsetWidth / nodeWidth;
138
+ const scaleY = wrapperComponent.offsetHeight / nodeHeight;
139
+
140
+ const newScale = checkZoomBounds(
141
+ customZoom || Math.min(scaleX, scaleY),
142
+ minScale,
143
+ maxScale,
144
+ 0,
145
+ false,
146
+ );
147
+
148
+ const offsetX = (wrapperRect.width - nodeWidth * newScale) / 2;
149
+ const offsetY = (wrapperRect.height - nodeHeight * newScale) / 2;
150
+
151
+ const newPositionX = (wrapperRect.left - nodeLeft) * newScale + offsetX;
152
+ const newPositionY = (wrapperRect.top - nodeTop) * newScale + offsetY;
153
+
154
+ const bounds = calculateBounds(contextInstance, newScale);
155
+
156
+ const { x, y } = getMouseBoundedPosition(
157
+ newPositionX,
158
+ newPositionY,
159
+ bounds,
160
+ limitToBounds,
161
+ 0,
162
+ 0,
163
+ wrapperComponent,
164
+ );
165
+
166
+ return { positionX: x, positionY: y, scale: newScale };
167
+ }
168
+
169
+ function getOffset(element: HTMLElement): PositionType {
170
+ let el = element;
171
+
172
+ let offsetLeft = 0;
173
+ let offsetTop = 0;
174
+
175
+ while (el) {
176
+ offsetLeft += el.offsetLeft;
177
+ offsetTop += el.offsetTop;
178
+
179
+ el = el.offsetParent as HTMLElement;
180
+ }
181
+
182
+ return {
183
+ x: offsetLeft - window.scrollX,
184
+ y: offsetTop - window.scrollY,
185
+ };
186
+ }
187
+
188
+ export function isValidZoomNode(node: HTMLElement | null): boolean {
189
+ if (!node) {
190
+ console.error("Zoom node not found");
191
+ return false;
192
+ } else if (
193
+ node?.offsetWidth === undefined ||
194
+ node?.offsetHeight === undefined
195
+ ) {
196
+ console.error(
197
+ "Zoom node is not valid - it must contain offsetWidth and offsetHeight",
198
+ );
199
+ return false;
200
+ }
201
+ return true;
202
+ }
@@ -0,0 +1,99 @@
1
+ import { ReactZoomPanPinchContext } from "../../models/context.model";
2
+ import { animate, handleCancelAnimation } from "../animations/animations.utils";
3
+ import { handleCalculateBounds } from "../bounds/bounds.utils";
4
+ import {
5
+ getPaddingValue,
6
+ getPanningClientPosition,
7
+ handleNewPosition,
8
+ handlePanningSetup,
9
+ handlePanToBounds,
10
+ handleTouchPanningSetup,
11
+ } from "./panning.utils";
12
+ import {
13
+ handleVelocityPanning,
14
+ handleCalculateVelocity,
15
+ } from "./velocity.logic";
16
+
17
+ export function handlePanningStart(
18
+ contextInstance: ReactZoomPanPinchContext,
19
+ event: MouseEvent | TouchEvent,
20
+ ): void {
21
+ const { scale } = contextInstance.transformState;
22
+
23
+ handleCancelAnimation(contextInstance);
24
+ handleCalculateBounds(contextInstance, scale);
25
+ if (window.TouchEvent !== undefined && event instanceof TouchEvent) {
26
+ handleTouchPanningSetup(contextInstance, event as TouchEvent);
27
+ } else {
28
+ handlePanningSetup(contextInstance, event as MouseEvent);
29
+ }
30
+ }
31
+
32
+ export function handlePanning(
33
+ contextInstance: ReactZoomPanPinchContext,
34
+ clientX: number,
35
+ clientY: number,
36
+ ): void {
37
+ const { startCoords, setup } = contextInstance;
38
+ const { sizeX, sizeY } = setup.alignmentAnimation;
39
+
40
+ if (!startCoords) return;
41
+
42
+ const { x, y } = getPanningClientPosition(contextInstance, clientX, clientY);
43
+ const paddingValueX = getPaddingValue(contextInstance, sizeX);
44
+ const paddingValueY = getPaddingValue(contextInstance, sizeY);
45
+
46
+ handleCalculateVelocity(contextInstance, { x, y });
47
+ handleNewPosition(contextInstance, x, y, paddingValueX, paddingValueY);
48
+ }
49
+
50
+ export function handlePanningEnd(
51
+ contextInstance: ReactZoomPanPinchContext,
52
+ ): void {
53
+ if (contextInstance.isPanning) {
54
+ const { velocityDisabled } = contextInstance.setup.panning;
55
+ const { velocity, wrapperComponent, contentComponent } = contextInstance;
56
+
57
+ contextInstance.isPanning = false;
58
+ contextInstance.animate = false;
59
+ contextInstance.animation = null;
60
+
61
+ const wrapperRect = wrapperComponent?.getBoundingClientRect();
62
+ const contentRect = contentComponent?.getBoundingClientRect();
63
+
64
+ const wrapperWidth = wrapperRect?.width || 0;
65
+ const wrapperHeight = wrapperRect?.height || 0;
66
+ const contentWidth = contentRect?.width || 0;
67
+ const contentHeight = contentRect?.height || 0;
68
+ const isZoomed =
69
+ wrapperWidth < contentWidth || wrapperHeight < contentHeight;
70
+
71
+ const shouldAnimate =
72
+ !velocityDisabled && velocity && velocity?.total > 0.1 && isZoomed;
73
+
74
+ if (shouldAnimate) {
75
+ handleVelocityPanning(contextInstance);
76
+ } else {
77
+ handleAlignToBounds(contextInstance);
78
+ }
79
+ }
80
+ }
81
+
82
+ export function handleAlignToBounds(
83
+ contextInstance: ReactZoomPanPinchContext,
84
+ ): void {
85
+ const { scale } = contextInstance.transformState;
86
+ const { minScale, alignmentAnimation } = contextInstance.setup;
87
+ const { disabled, sizeX, sizeY, animationTime, animationType } =
88
+ alignmentAnimation;
89
+
90
+ const isDisabled = disabled || scale < minScale || (!sizeX && !sizeY);
91
+
92
+ if (isDisabled) return;
93
+
94
+ const targetState = handlePanToBounds(contextInstance);
95
+
96
+ if (targetState) {
97
+ animate(contextInstance, targetState, animationTime, animationType);
98
+ }
99
+ }
@@ -0,0 +1,199 @@
1
+ import {
2
+ PositionType,
3
+ ReactZoomPanPinchContext,
4
+ ReactZoomPanPinchState,
5
+ } from "../../models";
6
+ import { isExcludedNode } from "../../utils";
7
+ import { getMouseBoundedPosition } from "../bounds/bounds.utils";
8
+ import { handleCalculateZoomPositions } from "../zoom/zoom.utils";
9
+
10
+ export const isPanningStartAllowed = (
11
+ contextInstance: ReactZoomPanPinchContext,
12
+ event: MouseEvent | TouchEvent,
13
+ ): boolean => {
14
+ const { excluded } = contextInstance.setup.panning;
15
+ const { isInitialized, wrapperComponent } = contextInstance;
16
+
17
+ const target = event.target as HTMLElement;
18
+ const isWrapperChild = wrapperComponent?.contains(target);
19
+ const isAllowed = isInitialized && target && isWrapperChild;
20
+
21
+ if (!isAllowed) return false;
22
+
23
+ const isExcluded = isExcludedNode(target, excluded);
24
+
25
+ if (isExcluded) return false;
26
+
27
+ return true;
28
+ };
29
+
30
+ export const isPanningAllowed = (
31
+ contextInstance: ReactZoomPanPinchContext,
32
+ ): boolean => {
33
+ const { isInitialized, isPanning, setup } = contextInstance;
34
+ const { disabled } = setup.panning;
35
+
36
+ const isAllowed = isInitialized && isPanning && !disabled;
37
+
38
+ if (!isAllowed) return false;
39
+
40
+ return true;
41
+ };
42
+
43
+ export const handlePanningSetup = (
44
+ contextInstance: ReactZoomPanPinchContext,
45
+ event: MouseEvent,
46
+ ): void => {
47
+ const { positionX, positionY } = contextInstance.transformState;
48
+
49
+ contextInstance.isPanning = true;
50
+
51
+ // Panning with mouse
52
+ const x = event.clientX;
53
+ const y = event.clientY;
54
+
55
+ contextInstance.startCoords = { x: x - positionX, y: y - positionY };
56
+ };
57
+
58
+ export const handleTouchPanningSetup = (
59
+ contextInstance: ReactZoomPanPinchContext,
60
+ event: TouchEvent,
61
+ ): void => {
62
+ const touches = event.touches;
63
+ const { positionX, positionY } = contextInstance.transformState;
64
+
65
+ contextInstance.isPanning = true;
66
+
67
+ // Panning with touch
68
+ const oneFingerTouch = touches.length === 1;
69
+ if (oneFingerTouch) {
70
+ const x = touches[0].clientX;
71
+ const y = touches[0].clientY;
72
+ contextInstance.startCoords = { x: x - positionX, y: y - positionY };
73
+ }
74
+ };
75
+ export function handlePanToBounds(
76
+ contextInstance: ReactZoomPanPinchContext,
77
+ ): Omit<ReactZoomPanPinchState, "previousScale"> | undefined {
78
+ const { positionX, positionY, scale } = contextInstance.transformState;
79
+ const { disabled, limitToBounds, centerZoomedOut } = contextInstance.setup;
80
+ const { wrapperComponent } = contextInstance;
81
+
82
+ if (disabled || !wrapperComponent || !contextInstance.bounds) return;
83
+
84
+ const {
85
+ maxPositionX,
86
+ minPositionX,
87
+ maxPositionY,
88
+ minPositionY,
89
+ } = contextInstance.bounds;
90
+
91
+ const xChanged = positionX > maxPositionX || positionX < minPositionX;
92
+ const yChanged = positionY > maxPositionY || positionY < minPositionY;
93
+
94
+ const mousePosX =
95
+ positionX > maxPositionX
96
+ ? wrapperComponent.offsetWidth
97
+ : contextInstance.setup.minPositionX || 0;
98
+ const mousePosY =
99
+ positionY > maxPositionY
100
+ ? wrapperComponent.offsetHeight
101
+ : contextInstance.setup.minPositionY || 0;
102
+
103
+ const { x, y } = handleCalculateZoomPositions(
104
+ contextInstance,
105
+ mousePosX,
106
+ mousePosY,
107
+ scale,
108
+ contextInstance.bounds,
109
+ limitToBounds || centerZoomedOut,
110
+ );
111
+
112
+ return {
113
+ scale,
114
+ positionX: xChanged ? x : positionX,
115
+ positionY: yChanged ? y : positionY,
116
+ };
117
+ }
118
+
119
+ export function handlePaddingAnimation(
120
+ contextInstance: ReactZoomPanPinchContext,
121
+ positionX: number,
122
+ positionY: number,
123
+ ): void {
124
+ const { scale } = contextInstance.transformState;
125
+ const { sizeX, sizeY } = contextInstance.setup.alignmentAnimation;
126
+
127
+ if (!sizeX && !sizeY) return;
128
+
129
+ contextInstance.setTransformState(scale, positionX, positionY);
130
+ }
131
+
132
+ export function handleNewPosition(
133
+ contextInstance: ReactZoomPanPinchContext,
134
+ newPositionX: number,
135
+ newPositionY: number,
136
+ paddingValueX: number,
137
+ paddingValueY: number,
138
+ ): void {
139
+ const { limitToBounds } = contextInstance.setup;
140
+ const { wrapperComponent, bounds } = contextInstance;
141
+ const { scale, positionX, positionY } = contextInstance.transformState;
142
+
143
+ if (
144
+ wrapperComponent === null ||
145
+ bounds === null ||
146
+ (newPositionX === positionX && newPositionY === positionY)
147
+ ) {
148
+ return;
149
+ }
150
+
151
+ const { x, y } = getMouseBoundedPosition(
152
+ newPositionX,
153
+ newPositionY,
154
+ bounds,
155
+ limitToBounds,
156
+ paddingValueX,
157
+ paddingValueY,
158
+ wrapperComponent,
159
+ );
160
+
161
+ contextInstance.setTransformState(scale, x, y);
162
+ }
163
+
164
+ export const getPanningClientPosition = (
165
+ contextInstance: ReactZoomPanPinchContext,
166
+ clientX: number,
167
+ clientY: number,
168
+ ): PositionType => {
169
+ const { startCoords, transformState } = contextInstance;
170
+ const { panning } = contextInstance.setup;
171
+ const { lockAxisX, lockAxisY } = panning;
172
+ const { positionX, positionY } = transformState;
173
+
174
+ if (!startCoords) {
175
+ return { x: positionX, y: positionY };
176
+ }
177
+
178
+ const mouseX = clientX - startCoords.x;
179
+ const mouseY = clientY - startCoords.y;
180
+ const newPositionX = lockAxisX ? positionX : mouseX;
181
+ const newPositionY = lockAxisY ? positionY : mouseY;
182
+
183
+ return { x: newPositionX, y: newPositionY };
184
+ };
185
+
186
+ export const getPaddingValue = (
187
+ contextInstance: ReactZoomPanPinchContext,
188
+ size: number,
189
+ ): number => {
190
+ const { setup, transformState } = contextInstance;
191
+ const { scale } = transformState;
192
+ const { minScale, disablePadding } = setup;
193
+
194
+ if (size > 0 && scale >= minScale && !disablePadding) {
195
+ return size;
196
+ }
197
+
198
+ return 0;
199
+ };
@@ -0,0 +1,148 @@
1
+ import { PositionType } from "../../models";
2
+ import { ReactZoomPanPinchContext } from "../../models/context.model";
3
+ import { animations } from "../animations/animations.constants";
4
+ import { handleSetupAnimation } from "../animations/animations.utils";
5
+ import { getPaddingValue } from "./panning.utils";
6
+ import {
7
+ getVelocityPosition,
8
+ getVelocityMoveTime,
9
+ isVelocityAllowed,
10
+ isVelocityCalculationAllowed,
11
+ } from "./velocity.utils";
12
+
13
+ export function getSizeMultiplier(
14
+ wrapperComponent: HTMLDivElement,
15
+ equalToMove: boolean,
16
+ ): number {
17
+ const defaultMultiplier = 1;
18
+
19
+ if (equalToMove) {
20
+ return Math.min(
21
+ defaultMultiplier,
22
+ wrapperComponent.offsetWidth / window.innerWidth,
23
+ );
24
+ }
25
+
26
+ return defaultMultiplier;
27
+ }
28
+
29
+ export function handleCalculateVelocity(
30
+ contextInstance: ReactZoomPanPinchContext,
31
+ position: PositionType,
32
+ ): void {
33
+ const isAllowed = isVelocityCalculationAllowed(contextInstance);
34
+
35
+ if (!isAllowed) {
36
+ return;
37
+ }
38
+
39
+ const { lastMousePosition, velocityTime, setup } = contextInstance;
40
+ const { wrapperComponent } = contextInstance;
41
+ const { equalToMove } = setup.velocityAnimation;
42
+
43
+ const now = Date.now();
44
+ if (lastMousePosition && velocityTime && wrapperComponent) {
45
+ const sizeMultiplier = getSizeMultiplier(wrapperComponent, equalToMove);
46
+
47
+ const distanceX = position.x - lastMousePosition.x;
48
+ const distanceY = position.y - lastMousePosition.y;
49
+
50
+ const velocityX = distanceX / sizeMultiplier;
51
+ const velocityY = distanceY / sizeMultiplier;
52
+
53
+ const interval = now - velocityTime;
54
+ const speed = distanceX * distanceX + distanceY * distanceY;
55
+ const velocity = Math.sqrt(speed) / interval;
56
+
57
+ contextInstance.velocity = { velocityX, velocityY, total: velocity };
58
+ }
59
+ contextInstance.lastMousePosition = position;
60
+ contextInstance.velocityTime = now;
61
+ }
62
+
63
+ export function handleVelocityPanning(
64
+ contextInstance: ReactZoomPanPinchContext,
65
+ ): void {
66
+ const { velocity, bounds, setup, wrapperComponent } = contextInstance;
67
+ const isAllowed = isVelocityAllowed(contextInstance);
68
+
69
+ if (!isAllowed || !velocity || !bounds || !wrapperComponent) {
70
+ return;
71
+ }
72
+
73
+ const { velocityX, velocityY, total } = velocity;
74
+ const { maxPositionX, minPositionX, maxPositionY, minPositionY } = bounds;
75
+ const { limitToBounds, alignmentAnimation } = setup;
76
+ const { zoomAnimation, panning } = setup;
77
+ const { lockAxisY, lockAxisX } = panning;
78
+ const { animationType } = zoomAnimation;
79
+ const { sizeX, sizeY, velocityAlignmentTime } = alignmentAnimation;
80
+
81
+ const alignAnimationTime = velocityAlignmentTime;
82
+ const moveAnimationTime = getVelocityMoveTime(contextInstance, total);
83
+ const finalAnimationTime = Math.max(moveAnimationTime, alignAnimationTime);
84
+
85
+ const paddingValueX = getPaddingValue(contextInstance, sizeX);
86
+ const paddingValueY = getPaddingValue(contextInstance, sizeY);
87
+ const paddingX = (paddingValueX * wrapperComponent.offsetWidth) / 100;
88
+ const paddingY = (paddingValueY * wrapperComponent.offsetHeight) / 100;
89
+ const maxTargetX = maxPositionX + paddingX;
90
+ const minTargetX = minPositionX - paddingX;
91
+
92
+ const maxTargetY = maxPositionY + paddingY;
93
+ const minTargetY = minPositionY - paddingY;
94
+
95
+ const startState = contextInstance.transformState;
96
+
97
+ const startTime = new Date().getTime();
98
+ handleSetupAnimation(
99
+ contextInstance,
100
+ animationType,
101
+ finalAnimationTime,
102
+ (step: number) => {
103
+ const { scale, positionX, positionY } = contextInstance.transformState;
104
+ const frameTime = new Date().getTime() - startTime;
105
+ const animationProgress = frameTime / alignAnimationTime;
106
+ const alignAnimation = animations[alignmentAnimation.animationType];
107
+ const alignStep = 1 - alignAnimation(Math.min(1, animationProgress));
108
+
109
+ const customStep = 1 - step;
110
+
111
+ const newPositionX = positionX + velocityX * customStep;
112
+ const newPositionY = positionY + velocityY * customStep;
113
+
114
+ const currentPositionX = getVelocityPosition(
115
+ newPositionX,
116
+ startState.positionX,
117
+ positionX,
118
+ lockAxisX,
119
+ limitToBounds,
120
+ minPositionX,
121
+ maxPositionX,
122
+ minTargetX,
123
+ maxTargetX,
124
+ alignStep,
125
+ );
126
+ const currentPositionY = getVelocityPosition(
127
+ newPositionY,
128
+ startState.positionY,
129
+ positionY,
130
+ lockAxisY,
131
+ limitToBounds,
132
+ minPositionY,
133
+ maxPositionY,
134
+ minTargetY,
135
+ maxTargetY,
136
+ alignStep,
137
+ );
138
+
139
+ if (positionX !== newPositionX || positionY !== newPositionY) {
140
+ contextInstance.setTransformState(
141
+ scale,
142
+ currentPositionX,
143
+ currentPositionY,
144
+ );
145
+ }
146
+ },
147
+ );
148
+ }