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,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
+ }
@@ -0,0 +1,79 @@
1
+ import { ReactZoomPanPinchContext } from "../../models/context.model";
2
+ import { boundLimiter } from "../bounds/bounds.utils";
3
+
4
+ export const isVelocityCalculationAllowed = (
5
+ contextInstance: ReactZoomPanPinchContext,
6
+ ): boolean => {
7
+ const { mounted } = contextInstance;
8
+ const { disabled, velocityAnimation } = contextInstance.setup;
9
+ const { scale } = contextInstance.transformState;
10
+ const { disabled: disabledVelocity } = velocityAnimation;
11
+
12
+ const isAllowed = !disabledVelocity || scale > 1 || !disabled || mounted;
13
+
14
+ if (!isAllowed) return false;
15
+
16
+ return true;
17
+ };
18
+
19
+ export const isVelocityAllowed = (
20
+ contextInstance: ReactZoomPanPinchContext,
21
+ ): boolean => {
22
+ const { mounted, velocity, bounds } = contextInstance;
23
+ const { disabled, velocityAnimation } = contextInstance.setup;
24
+ const { scale } = contextInstance.transformState;
25
+ const { disabled: disabledVelocity } = velocityAnimation;
26
+
27
+ const isAllowed = !disabledVelocity || scale > 1 || !disabled || mounted;
28
+
29
+ if (!isAllowed) return false;
30
+ if (!velocity || !bounds) return false;
31
+
32
+ return true;
33
+ };
34
+
35
+ export function getVelocityMoveTime(
36
+ contextInstance: ReactZoomPanPinchContext,
37
+ velocity: number,
38
+ ): number {
39
+ const { velocityAnimation } = contextInstance.setup;
40
+ const { equalToMove, animationTime, sensitivity } = velocityAnimation;
41
+
42
+ if (equalToMove) {
43
+ return animationTime * velocity * sensitivity;
44
+ }
45
+ return animationTime;
46
+ }
47
+
48
+ export function getVelocityPosition(
49
+ newPosition: number,
50
+ startPosition: number,
51
+ currentPosition: number,
52
+ isLocked: boolean,
53
+ limitToBounds: boolean,
54
+ minPosition: number,
55
+ maxPosition: number,
56
+ minTarget: number,
57
+ maxTarget: number,
58
+ step: number,
59
+ ): number {
60
+ if (limitToBounds) {
61
+ if (startPosition > maxPosition && currentPosition > maxPosition) {
62
+ const calculatedPosition =
63
+ maxPosition + (newPosition - maxPosition) * step;
64
+
65
+ if (calculatedPosition > maxTarget) return maxTarget;
66
+ if (calculatedPosition < maxPosition) return maxPosition;
67
+ return calculatedPosition;
68
+ }
69
+ if (startPosition < minPosition && currentPosition < minPosition) {
70
+ const calculatedPosition =
71
+ minPosition + (newPosition - minPosition) * step;
72
+ if (calculatedPosition < minTarget) return minTarget;
73
+ if (calculatedPosition > minPosition) return minPosition;
74
+ return calculatedPosition;
75
+ }
76
+ }
77
+ if (isLocked) return startPosition;
78
+ return boundLimiter(newPosition, minPosition, maxPosition, limitToBounds);
79
+ }
@@ -0,0 +1,83 @@
1
+ import { ReactZoomPanPinchContext } from "../../models";
2
+ import { handleCancelAnimation } from "../animations/animations.utils";
3
+ import { handleAlignToScaleBounds } from "../zoom/zoom.logic";
4
+ import {
5
+ calculatePinchZoom,
6
+ calculateTouchMidPoint,
7
+ getTouchDistance,
8
+ } from "./pinch.utils";
9
+ import { handleCalculateBounds } from "../bounds/bounds.utils";
10
+ import { handleCalculateZoomPositions } from "../zoom/zoom.utils";
11
+
12
+ export const handlePinchStart = (
13
+ contextInstance: ReactZoomPanPinchContext,
14
+ event: TouchEvent,
15
+ ): void => {
16
+ const distance = getTouchDistance(event);
17
+
18
+ contextInstance.pinchStartDistance = distance;
19
+ contextInstance.lastDistance = distance;
20
+ contextInstance.pinchStartScale = contextInstance.transformState.scale;
21
+ contextInstance.isPanning = false;
22
+
23
+ handleCancelAnimation(contextInstance);
24
+ };
25
+
26
+ export const handlePinchZoom = (
27
+ contextInstance: ReactZoomPanPinchContext,
28
+ event: TouchEvent,
29
+ ): void => {
30
+ const { contentComponent, pinchStartDistance } = contextInstance;
31
+ const { scale } = contextInstance.transformState;
32
+ const {
33
+ limitToBounds,
34
+ centerZoomedOut,
35
+ zoomAnimation,
36
+ } = contextInstance.setup;
37
+ const { disabled, size } = zoomAnimation;
38
+
39
+ // if one finger starts from outside of wrapper
40
+ if (pinchStartDistance === null || !contentComponent) return;
41
+
42
+ const midPoint = calculateTouchMidPoint(event, scale, contentComponent);
43
+
44
+ // if touches goes off of the wrapper element
45
+ if (!isFinite(midPoint.x) || !isFinite(midPoint.y)) return;
46
+
47
+ const currentDistance = getTouchDistance(event);
48
+ const newScale = calculatePinchZoom(contextInstance, currentDistance);
49
+
50
+ if (newScale === scale) return;
51
+
52
+ const bounds = handleCalculateBounds(contextInstance, newScale);
53
+
54
+ const isPaddingDisabled = disabled || size === 0 || centerZoomedOut;
55
+ const isLimitedToBounds = limitToBounds && isPaddingDisabled;
56
+
57
+ const { x, y } = handleCalculateZoomPositions(
58
+ contextInstance,
59
+ midPoint.x,
60
+ midPoint.y,
61
+ newScale,
62
+ bounds,
63
+ isLimitedToBounds,
64
+ );
65
+
66
+ contextInstance.pinchMidpoint = midPoint;
67
+ contextInstance.lastDistance = currentDistance;
68
+
69
+ contextInstance.setTransformState(newScale, x, y);
70
+ };
71
+
72
+ export const handlePinchStop = (
73
+ contextInstance: ReactZoomPanPinchContext,
74
+ ): void => {
75
+ const { pinchMidpoint } = contextInstance;
76
+
77
+ contextInstance.velocity = null;
78
+ contextInstance.lastDistance = null;
79
+ contextInstance.pinchMidpoint = null;
80
+ contextInstance.pinchStartScale = null;
81
+ contextInstance.pinchStartDistance = null;
82
+ handleAlignToScaleBounds(contextInstance, pinchMidpoint?.x, pinchMidpoint?.y);
83
+ };
@@ -0,0 +1,88 @@
1
+ import { PositionType, ReactZoomPanPinchContext } from "../../models";
2
+ import { isExcludedNode, roundNumber } from "../../utils";
3
+ import { checkZoomBounds } from "../zoom/zoom.utils";
4
+
5
+ export const isPinchStartAllowed = (
6
+ contextInstance: ReactZoomPanPinchContext,
7
+ event: TouchEvent,
8
+ ): boolean => {
9
+ const { disabled, excluded } = contextInstance.setup.pinch;
10
+ const { isInitialized } = contextInstance;
11
+
12
+ const target = event.target as HTMLElement;
13
+ const isAllowed = isInitialized && !disabled && target;
14
+
15
+ if (!isAllowed) return false;
16
+
17
+ const isExcluded = isExcludedNode(target, excluded);
18
+
19
+ if (isExcluded) return false;
20
+
21
+ return true;
22
+ };
23
+
24
+ export const isPinchAllowed = (
25
+ contextInstance: ReactZoomPanPinchContext,
26
+ ): boolean => {
27
+ const { disabled } = contextInstance.setup.pinch;
28
+ const { isInitialized, pinchStartDistance } = contextInstance;
29
+
30
+ const isAllowed = isInitialized && !disabled && pinchStartDistance;
31
+
32
+ if (!isAllowed) return false;
33
+
34
+ return true;
35
+ };
36
+
37
+ export const calculateTouchMidPoint = (
38
+ event: TouchEvent,
39
+ scale: number,
40
+ contentComponent: HTMLDivElement,
41
+ ): PositionType => {
42
+ const contentRect = contentComponent.getBoundingClientRect();
43
+ const { touches } = event;
44
+ const firstPointX = roundNumber(touches[0].clientX - contentRect.left, 5);
45
+ const firstPointY = roundNumber(touches[0].clientY - contentRect.top, 5);
46
+ const secondPointX = roundNumber(touches[1].clientX - contentRect.left, 5);
47
+ const secondPointY = roundNumber(touches[1].clientY - contentRect.top, 5);
48
+
49
+ return {
50
+ x: (firstPointX + secondPointX) / 2 / scale,
51
+ y: (firstPointY + secondPointY) / 2 / scale,
52
+ };
53
+ };
54
+
55
+ export const getTouchDistance = (event: TouchEvent): number => {
56
+ return Math.sqrt(
57
+ Math.pow(event.touches[0].pageX - event.touches[1].pageX, 2) +
58
+ Math.pow(event.touches[0].pageY - event.touches[1].pageY, 2),
59
+ );
60
+ };
61
+
62
+ export const calculatePinchZoom = (
63
+ contextInstance: ReactZoomPanPinchContext,
64
+ currentDistance: number,
65
+ ): number => {
66
+ const { pinchStartScale, pinchStartDistance, setup } = contextInstance;
67
+ const { maxScale, minScale, zoomAnimation, disablePadding } = setup;
68
+ const { size, disabled } = zoomAnimation;
69
+
70
+ if (!pinchStartScale || pinchStartDistance === null || !currentDistance) {
71
+ throw new Error("Pinch touches distance was not provided");
72
+ }
73
+
74
+ if (currentDistance < 0) {
75
+ return contextInstance.transformState.scale;
76
+ }
77
+
78
+ const touchProportion = currentDistance / pinchStartDistance;
79
+ const scaleDifference = touchProportion * pinchStartScale;
80
+
81
+ return checkZoomBounds(
82
+ roundNumber(scaleDifference, 2),
83
+ minScale,
84
+ maxScale,
85
+ size,
86
+ !disabled && !disablePadding,
87
+ );
88
+ };