react-zoom-pan-pinch 2.1.2 → 2.2.0

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 (73) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +43 -0
  3. package/dist/index.cjs.js +87 -60
  4. package/dist/index.cjs.js.map +1 -1
  5. package/dist/index.d.ts +11 -2
  6. package/dist/index.esm.js +87 -60
  7. package/dist/index.esm.js.map +1 -1
  8. package/package.json +13 -12
  9. package/src/.eslintrc.js +36 -0
  10. package/src/components/transform-component.module.css +30 -0
  11. package/src/components/transform-component.tsx +52 -0
  12. package/src/components/transform-context.tsx +493 -0
  13. package/src/components/transform-wrapper.tsx +18 -0
  14. package/src/constants/state.constants.ts +88 -0
  15. package/src/core/animations/animations.constants.ts +76 -0
  16. package/src/core/animations/animations.utils.ts +109 -0
  17. package/src/core/bounds/bounds.types.ts +8 -0
  18. package/src/core/bounds/bounds.utils.ts +157 -0
  19. package/src/core/double-click/double-click.logic.ts +75 -0
  20. package/src/core/handlers/handlers.logic.ts +112 -0
  21. package/src/core/handlers/handlers.utils.ts +202 -0
  22. package/src/core/pan/panning.logic.ts +99 -0
  23. package/src/core/pan/panning.utils.ts +199 -0
  24. package/src/core/pan/velocity.logic.ts +148 -0
  25. package/src/core/pan/velocity.utils.ts +79 -0
  26. package/src/core/pinch/pinch.logic.ts +83 -0
  27. package/src/core/pinch/pinch.utils.ts +88 -0
  28. package/src/core/wheel/wheel.logic.ts +118 -0
  29. package/src/core/wheel/wheel.utils.ts +121 -0
  30. package/src/core/zoom/zoom.logic.ts +69 -0
  31. package/src/core/zoom/zoom.utils.ts +58 -0
  32. package/src/hooks/use-transform-context.tsx +16 -0
  33. package/src/index.ts +6 -0
  34. package/src/models/calculations.model.ts +26 -0
  35. package/src/models/context.model.ts +179 -0
  36. package/src/models/helpers.model.ts +15 -0
  37. package/src/models/index.ts +3 -0
  38. package/src/react-app-env.d.ts +1 -0
  39. package/src/stories/assets/big-image.jpeg +0 -0
  40. package/src/stories/assets/medium-image.jpg +0 -0
  41. package/src/stories/assets/small-image.jpg +0 -0
  42. package/src/stories/assets/svg-example.svg +14 -0
  43. package/src/stories/docs/Introduction.stories.mdx +97 -0
  44. package/src/stories/docs/handlers.stories.mdx +48 -0
  45. package/src/stories/docs/handlers.tsx +107 -0
  46. package/src/stories/docs/props.stories.mdx +92 -0
  47. package/src/stories/docs/props.tsx +490 -0
  48. package/src/stories/examples/content/content.stories.mdx +122 -0
  49. package/src/stories/examples/content-rerendering/content-updating.stories.mdx +42 -0
  50. package/src/stories/examples/content-rerendering/example.tsx +74 -0
  51. package/src/stories/examples/controls/controls.stories.mdx +71 -0
  52. package/src/stories/examples/image-big/image-big.stories.mdx +44 -0
  53. package/src/stories/examples/image-medium/image-medium.stories.mdx +42 -0
  54. package/src/stories/examples/image-small/image-small.stories.mdx +42 -0
  55. package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +47 -0
  56. package/src/stories/examples/zoom-to-element/example.tsx +91 -0
  57. package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +28 -0
  58. package/src/stories/types/args.types.ts +201 -0
  59. package/src/stories/utils/args.utils.ts +34 -0
  60. package/src/stories/utils/code.utils.tsx +21 -0
  61. package/src/stories/utils/controls.utils.tsx +26 -0
  62. package/src/stories/utils/index.ts +3 -0
  63. package/src/stories/utils/styles.module.css +29 -0
  64. package/src/styles.css +8 -0
  65. package/src/typings.d.ts +10 -0
  66. package/src/utils/calculations.utils.ts +16 -0
  67. package/src/utils/callback.utils.ts +11 -0
  68. package/src/utils/context.utils.ts +27 -0
  69. package/src/utils/event.utils.ts +22 -0
  70. package/src/utils/helpers.utils.ts +27 -0
  71. package/src/utils/index.ts +7 -0
  72. package/src/utils/state.utils.ts +40 -0
  73. package/src/utils/styles.utils.ts +27 -0
@@ -0,0 +1,109 @@
1
+ import { animations } from "./animations.constants";
2
+
3
+ import {
4
+ AnimationType,
5
+ ReactZoomPanPinchContext,
6
+ StateType,
7
+ } from "../../models";
8
+
9
+ const handleCancelAnimationFrame = (animation: AnimationType | null) => {
10
+ if (typeof animation === "number") {
11
+ cancelAnimationFrame(animation);
12
+ }
13
+ };
14
+
15
+ export const handleCancelAnimation = (
16
+ contextInstance: ReactZoomPanPinchContext,
17
+ ): void => {
18
+ if (!contextInstance.mounted) return;
19
+ handleCancelAnimationFrame(contextInstance.animation);
20
+ // Clear animation state
21
+ contextInstance.animate = false;
22
+ contextInstance.animation = null;
23
+ contextInstance.velocity = null;
24
+ };
25
+
26
+ export function handleSetupAnimation(
27
+ contextInstance: ReactZoomPanPinchContext,
28
+ animationName: string,
29
+ animationTime: number,
30
+ callback: (step: number) => void,
31
+ ): void {
32
+ if (!contextInstance.mounted) return;
33
+ const startTime = new Date().getTime();
34
+ const lastStep = 1;
35
+
36
+ // if another animation is active
37
+ handleCancelAnimation(contextInstance);
38
+
39
+ // new animation
40
+ contextInstance.animation = () => {
41
+ if (!contextInstance.mounted) {
42
+ return handleCancelAnimationFrame(contextInstance.animation);
43
+ }
44
+
45
+ const frameTime = new Date().getTime() - startTime;
46
+ const animationProgress = frameTime / animationTime;
47
+ const animationType = animations[animationName];
48
+
49
+ const step = animationType(animationProgress);
50
+
51
+ if (frameTime >= animationTime) {
52
+ callback(lastStep);
53
+ contextInstance.animation = null;
54
+ } else if (contextInstance.animation) {
55
+ callback(step);
56
+ requestAnimationFrame(contextInstance.animation);
57
+ }
58
+ };
59
+
60
+ requestAnimationFrame(contextInstance.animation);
61
+ }
62
+
63
+ export function animate(
64
+ contextInstance: ReactZoomPanPinchContext,
65
+ targetState: StateType,
66
+ animationTime: number,
67
+ animationName: string,
68
+ ): void {
69
+ const isValid = isValidTargetState(targetState);
70
+ if (!contextInstance.mounted || !isValid) return;
71
+ const { setTransformState } = contextInstance;
72
+ const { scale, positionX, positionY } = contextInstance.transformState;
73
+
74
+ const scaleDiff = targetState.scale - scale;
75
+ const positionXDiff = targetState.positionX - positionX;
76
+ const positionYDiff = targetState.positionY - positionY;
77
+
78
+ if (animationTime === 0) {
79
+ setTransformState(
80
+ targetState.scale,
81
+ targetState.positionX,
82
+ targetState.positionY,
83
+ );
84
+ } else {
85
+ // animation start timestamp
86
+ handleSetupAnimation(
87
+ contextInstance,
88
+ animationName,
89
+ animationTime,
90
+ (step: number) => {
91
+ const newScale = scale + scaleDiff * step;
92
+ const newPositionX = positionX + positionXDiff * step;
93
+ const newPositionY = positionY + positionYDiff * step;
94
+
95
+ setTransformState(newScale, newPositionX, newPositionY);
96
+ },
97
+ );
98
+ }
99
+ }
100
+
101
+ function isValidTargetState(targetState: StateType): boolean {
102
+ const { scale, positionX, positionY } = targetState;
103
+
104
+ if (isNaN(scale) || isNaN(positionX) || isNaN(positionY)) {
105
+ return false;
106
+ }
107
+
108
+ return true;
109
+ }
@@ -0,0 +1,8 @@
1
+ export type ComponentsSizesType = {
2
+ wrapperWidth: number;
3
+ wrapperHeight: number;
4
+ newContentWidth: number;
5
+ newDiffWidth: number;
6
+ newContentHeight: number;
7
+ newDiffHeight: number;
8
+ };
@@ -0,0 +1,157 @@
1
+ import { roundNumber } from "../../utils";
2
+
3
+ import {
4
+ BoundsType,
5
+ PositionType,
6
+ ReactZoomPanPinchContext,
7
+ } from "../../models";
8
+ import { ComponentsSizesType } from "./bounds.types";
9
+
10
+ export function getComponentsSizes(
11
+ wrapperComponent: HTMLDivElement,
12
+ contentComponent: HTMLDivElement,
13
+ newScale: number,
14
+ ): ComponentsSizesType {
15
+ const wrapperWidth = wrapperComponent.offsetWidth;
16
+ const wrapperHeight = wrapperComponent.offsetHeight;
17
+
18
+ const contentWidth = contentComponent.offsetWidth;
19
+ const contentHeight = contentComponent.offsetHeight;
20
+
21
+ const newContentWidth = contentWidth * newScale;
22
+ const newContentHeight = contentHeight * newScale;
23
+ const newDiffWidth = wrapperWidth - newContentWidth;
24
+ const newDiffHeight = wrapperHeight - newContentHeight;
25
+
26
+ return {
27
+ wrapperWidth,
28
+ wrapperHeight,
29
+ newContentWidth,
30
+ newDiffWidth,
31
+ newContentHeight,
32
+ newDiffHeight,
33
+ };
34
+ }
35
+
36
+ export const getBounds = (
37
+ wrapperWidth: number,
38
+ newContentWidth: number,
39
+ diffWidth: number,
40
+ wrapperHeight: number,
41
+ newContentHeight: number,
42
+ diffHeight: number,
43
+ centerZoomedOut: boolean,
44
+ ): BoundsType => {
45
+ const scaleWidthFactor =
46
+ wrapperWidth > newContentWidth
47
+ ? diffWidth * (centerZoomedOut ? 1 : 0.5)
48
+ : 0;
49
+ const scaleHeightFactor =
50
+ wrapperHeight > newContentHeight
51
+ ? diffHeight * (centerZoomedOut ? 1 : 0.5)
52
+ : 0;
53
+
54
+ const minPositionX = wrapperWidth - newContentWidth - scaleWidthFactor;
55
+ const maxPositionX = scaleWidthFactor;
56
+ const minPositionY = wrapperHeight - newContentHeight - scaleHeightFactor;
57
+ const maxPositionY = scaleHeightFactor;
58
+
59
+ return { minPositionX, maxPositionX, minPositionY, maxPositionY };
60
+ };
61
+
62
+ export const calculateBounds = (
63
+ contextInstance: ReactZoomPanPinchContext,
64
+ newScale: number,
65
+ ): BoundsType => {
66
+ const { wrapperComponent, contentComponent } = contextInstance;
67
+ const { centerZoomedOut } = contextInstance.setup;
68
+
69
+ if (!wrapperComponent || !contentComponent) {
70
+ throw new Error("Components are not mounted");
71
+ }
72
+
73
+ const {
74
+ wrapperWidth,
75
+ wrapperHeight,
76
+ newContentWidth,
77
+ newDiffWidth,
78
+ newContentHeight,
79
+ newDiffHeight,
80
+ } = getComponentsSizes(wrapperComponent, contentComponent, newScale);
81
+
82
+ const bounds = getBounds(
83
+ wrapperWidth,
84
+ newContentWidth,
85
+ newDiffWidth,
86
+ wrapperHeight,
87
+ newContentHeight,
88
+ newDiffHeight,
89
+ Boolean(centerZoomedOut),
90
+ );
91
+ return bounds;
92
+ };
93
+
94
+ export const handleCalculateBounds = (
95
+ contextInstance: ReactZoomPanPinchContext,
96
+ newScale: number,
97
+ ): BoundsType => {
98
+ const bounds = calculateBounds(contextInstance, newScale);
99
+
100
+ // Save bounds
101
+ contextInstance.bounds = bounds;
102
+ return bounds;
103
+ };
104
+
105
+ export function getMouseBoundedPosition(
106
+ positionX: number,
107
+ positionY: number,
108
+ bounds: BoundsType,
109
+ limitToBounds: boolean,
110
+ paddingValueX: number,
111
+ paddingValueY: number,
112
+ wrapperComponent: HTMLDivElement | null,
113
+ ): PositionType {
114
+ const { minPositionX, minPositionY, maxPositionX, maxPositionY } = bounds;
115
+
116
+ let paddingX = 0;
117
+ let paddingY = 0;
118
+
119
+ if (wrapperComponent) {
120
+ paddingX = paddingValueX;
121
+ paddingY = paddingValueY;
122
+ }
123
+
124
+ const x = boundLimiter(
125
+ positionX,
126
+ minPositionX - paddingX,
127
+ maxPositionX + paddingX,
128
+ limitToBounds,
129
+ );
130
+
131
+ const y = boundLimiter(
132
+ positionY,
133
+ minPositionY - paddingY,
134
+ maxPositionY + paddingY,
135
+ limitToBounds,
136
+ );
137
+ return { x, y };
138
+ }
139
+
140
+ /**
141
+ * Keeps value between given bounds, used for limiting view to given boundaries
142
+ * 1# eg. boundLimiter(2, 0, 3, true) => 2
143
+ * 2# eg. boundLimiter(4, 0, 3, true) => 3
144
+ * 3# eg. boundLimiter(-2, 0, 3, true) => 0
145
+ * 4# eg. boundLimiter(10, 0, 3, false) => 10
146
+ */
147
+ export const boundLimiter = (
148
+ value: number,
149
+ minBound: number,
150
+ maxBound: number,
151
+ isActive: boolean,
152
+ ): number => {
153
+ if (!isActive) return roundNumber(value, 2);
154
+ if (value < minBound) return roundNumber(minBound, 2);
155
+ if (value > maxBound) return roundNumber(maxBound, 2);
156
+ return roundNumber(value, 2);
157
+ };
@@ -0,0 +1,75 @@
1
+ import { ReactZoomPanPinchContext } from "../../models";
2
+
3
+ import { animate } from "../animations/animations.utils";
4
+ import { getMousePosition } from "core/wheel/wheel.utils";
5
+ import { handleZoomToPoint } from "core/zoom/zoom.logic";
6
+ import { isExcludedNode } from "utils";
7
+ import {
8
+ handleCalculateButtonZoom,
9
+ resetTransformations,
10
+ } from "core/handlers/handlers.utils";
11
+
12
+ export function handleDoubleClick(
13
+ contextInstance: ReactZoomPanPinchContext,
14
+ event: MouseEvent | TouchEvent,
15
+ ): void {
16
+ const {
17
+ disabled,
18
+ mode,
19
+ step,
20
+ animationTime,
21
+ animationType,
22
+ } = contextInstance.setup.doubleClick;
23
+
24
+ if (disabled) return;
25
+
26
+ if (mode === "reset") {
27
+ return resetTransformations(contextInstance, animationTime, animationType);
28
+ }
29
+
30
+ const { scale } = contextInstance.transformState;
31
+ const { contentComponent } = contextInstance;
32
+
33
+ if (!contentComponent) return console.error("No ContentComponent found");
34
+
35
+ const delta = mode === "zoomOut" ? -1 : 1;
36
+
37
+ const newScale = handleCalculateButtonZoom(contextInstance, delta, step);
38
+ const mousePosition = getMousePosition(event, contentComponent, scale);
39
+ const targetState = handleZoomToPoint(
40
+ contextInstance,
41
+ newScale,
42
+ mousePosition.x,
43
+ mousePosition.y,
44
+ );
45
+
46
+ if (!targetState) {
47
+ return console.error(
48
+ "Error during zoom event. New transformation state was not calculated.",
49
+ );
50
+ }
51
+
52
+ animate(contextInstance, targetState, animationTime, animationType);
53
+ }
54
+
55
+ export const isDoubleClickAllowed = (
56
+ contextInstance: ReactZoomPanPinchContext,
57
+ event: MouseEvent | TouchEvent,
58
+ ): boolean => {
59
+ const { isInitialized, setup, wrapperComponent } = contextInstance;
60
+ const { disabled, excluded } = setup.doubleClick;
61
+
62
+ const target = event.target as HTMLElement;
63
+ const isWrapperChild = wrapperComponent?.contains(target);
64
+ const isAllowed = isInitialized && target && isWrapperChild && !disabled;
65
+
66
+ if (!isAllowed) return false;
67
+
68
+ const isExcluded = isExcludedNode(target, excluded);
69
+
70
+ if (isExcluded) return false;
71
+
72
+ if (!isAllowed) return false;
73
+
74
+ return true;
75
+ };
@@ -0,0 +1,112 @@
1
+ import { ReactZoomPanPinchContext } from "../../models";
2
+ import {
3
+ calculateZoomToNode,
4
+ handleZoomToViewCenter,
5
+ isValidZoomNode,
6
+ resetTransformations,
7
+ } from "./handlers.utils";
8
+ import { animations } from "../animations/animations.constants";
9
+ import { animate, handleCancelAnimation } from "../animations/animations.utils";
10
+ import { getCenterPosition } from "../../utils";
11
+
12
+ export const zoomIn = (contextInstance: ReactZoomPanPinchContext) => (
13
+ step = 0.5,
14
+ animationTime = 300,
15
+ animationType: keyof typeof animations = "easeOut",
16
+ ): void => {
17
+ handleZoomToViewCenter(
18
+ contextInstance,
19
+ 1,
20
+ step,
21
+ animationTime,
22
+ animationType,
23
+ );
24
+ };
25
+
26
+ export const zoomOut = (contextInstance: ReactZoomPanPinchContext) => (
27
+ step = 0.5,
28
+ animationTime = 300,
29
+ animationType: keyof typeof animations = "easeOut",
30
+ ): void => {
31
+ handleZoomToViewCenter(
32
+ contextInstance,
33
+ -1,
34
+ step,
35
+ animationTime,
36
+ animationType,
37
+ );
38
+ };
39
+
40
+ export const setTransform = (contextInstance: ReactZoomPanPinchContext) => (
41
+ newPositionX: number,
42
+ newPositionY: number,
43
+ newScale: number,
44
+ animationTime = 300,
45
+ animationType: keyof typeof animations = "easeOut",
46
+ ): void => {
47
+ const { positionX, positionY, scale } = contextInstance.transformState;
48
+ const { wrapperComponent, contentComponent } = contextInstance;
49
+ const { disabled } = contextInstance.setup;
50
+
51
+ if (disabled || !wrapperComponent || !contentComponent) return;
52
+
53
+ const targetState = {
54
+ positionX: isNaN(newPositionX) ? positionX : newPositionX,
55
+ positionY: isNaN(newPositionY) ? positionY : newPositionY,
56
+ scale: isNaN(newScale) ? scale : newScale,
57
+ };
58
+
59
+ animate(contextInstance, targetState, animationTime, animationType);
60
+ };
61
+
62
+ export const resetTransform = (contextInstance: ReactZoomPanPinchContext) => (
63
+ animationTime = 200,
64
+ animationType: keyof typeof animations = "easeOut",
65
+ ): void => {
66
+ resetTransformations(contextInstance, animationTime, animationType);
67
+ };
68
+
69
+ export const centerView = (contextInstance: ReactZoomPanPinchContext) => (
70
+ scale?: number,
71
+ animationTime = 200,
72
+ animationType: keyof typeof animations = "easeOut",
73
+ ): void => {
74
+ const {
75
+ transformState,
76
+ wrapperComponent,
77
+ contentComponent,
78
+ } = contextInstance;
79
+ if (wrapperComponent && contentComponent) {
80
+ const targetState = getCenterPosition(
81
+ scale || transformState.scale,
82
+ wrapperComponent,
83
+ contentComponent,
84
+ );
85
+
86
+ animate(contextInstance, targetState, animationTime, animationType);
87
+ }
88
+ };
89
+
90
+ export const zoomToElement = (contextInstance: ReactZoomPanPinchContext) => (
91
+ node: HTMLElement | string,
92
+ scale?: number,
93
+ animationTime = 600,
94
+ animationType: keyof typeof animations = "easeOut",
95
+ ): void => {
96
+ handleCancelAnimation(contextInstance);
97
+
98
+ const { wrapperComponent } = contextInstance;
99
+
100
+ const target: HTMLElement | null =
101
+ typeof node === "string" ? document.getElementById(node) : node;
102
+
103
+ if (
104
+ wrapperComponent &&
105
+ isValidZoomNode(target) &&
106
+ target &&
107
+ wrapperComponent.contains(target)
108
+ ) {
109
+ const targetState = calculateZoomToNode(contextInstance, target, scale);
110
+ animate(contextInstance, targetState, animationTime, animationType);
111
+ }
112
+ };
@@ -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
+ }