react-zoom-pan-pinch 3.7.0 → 4.0.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.
- package/dist/index.cjs.js +785 -310
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +117 -23
- package/dist/index.esm.js +781 -302
- package/dist/index.esm.js.map +1 -1
- package/package.json +73 -62
- package/src/components/index.ts +5 -0
- package/src/components/keep-scale/keep-scale.tsx +28 -0
- package/src/components/mini-map/mini-map.tsx +292 -0
- package/src/components/mini-map/use-resize.hook.ts +74 -0
- package/src/components/transform-component/transform-component.module.css +43 -0
- package/src/components/transform-component/transform-component.tsx +96 -0
- package/src/components/transform-wrapper/transform-wrapper.tsx +39 -0
- package/src/components/virtualize/virtualize.tsx +133 -0
- package/src/components/virtualize/virtualize.utils.ts +104 -0
- package/src/constants/state.constants.ts +99 -0
- package/src/core/animations/animations.constants.ts +78 -0
- package/src/core/animations/animations.utils.ts +115 -0
- package/src/core/bounds/bounds.types.ts +8 -0
- package/src/core/bounds/bounds.utils.ts +226 -0
- package/src/core/double-click/double-click.logic.ts +127 -0
- package/src/core/handlers/handlers.logic.ts +122 -0
- package/src/core/handlers/handlers.utils.ts +243 -0
- package/src/core/instance.core.ts +704 -0
- package/src/core/pan/panning.logic.ts +122 -0
- package/src/core/pan/panning.utils.ts +216 -0
- package/src/core/pan/velocity.logic.ts +187 -0
- package/src/core/pan/velocity.utils.ts +83 -0
- package/src/core/pinch/pinch.logic.ts +139 -0
- package/src/core/pinch/pinch.utils.ts +95 -0
- package/src/core/wheel/wheel.logic.ts +161 -0
- package/src/core/wheel/wheel.utils.ts +157 -0
- package/src/core/zoom/zoom.logic.ts +71 -0
- package/src/core/zoom/zoom.utils.ts +62 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/use-controls.tsx +9 -0
- package/src/hooks/use-transform-component.tsx +30 -0
- package/src/hooks/use-transform-context.tsx +13 -0
- package/src/hooks/use-transform-effect.tsx +22 -0
- package/src/hooks/use-transform-init.tsx +28 -0
- package/src/hooks/use-zoom-pan-pinch.tsx +48 -0
- package/src/index.ts +6 -0
- package/src/models/calculations.model.ts +34 -0
- package/src/models/context.model.ts +218 -0
- package/src/models/helpers.model.ts +15 -0
- package/src/models/index.ts +3 -0
- package/src/react-app-env.d.ts +1 -0
- package/src/stories/assets/big-image.jpeg +0 -0
- package/src/stories/assets/map.jpg +0 -0
- package/src/stories/assets/medium-image.jpg +0 -0
- package/src/stories/assets/pin.svg +1 -0
- package/src/stories/assets/small-image.jpg +0 -0
- package/src/stories/assets/svg-example.svg +14 -0
- package/src/stories/docs/DocTable.tsx +59 -0
- package/src/stories/docs/HandlerCards.tsx +102 -0
- package/src/stories/docs/Introduction.stories.mdx +135 -0
- package/src/stories/docs/PropTable.tsx +116 -0
- package/src/stories/docs/docs.module.css +370 -0
- package/src/stories/docs/handlers.stories.mdx +7 -0
- package/src/stories/docs/handlers.tsx +111 -0
- package/src/stories/docs/props.stories.mdx +11 -0
- package/src/stories/docs/props.tsx +571 -0
- package/src/stories/examples/axis-lock/axis-lock.stories.mdx +35 -0
- package/src/stories/examples/axis-lock/example.tsx +114 -0
- package/src/stories/examples/bounds/bounds.data.tsx +191 -0
- package/src/stories/examples/bounds/bounds.stories.mdx +99 -0
- package/src/stories/examples/center-zoomed-out/center-zoomed-out.stories.mdx +206 -0
- package/src/stories/examples/cinema/cinema-data.tsx +578 -0
- package/src/stories/examples/cinema/cinema.stories.mdx +30 -0
- package/src/stories/examples/cinema/example.tsx +378 -0
- package/src/stories/examples/cinema/image.png +0 -0
- package/src/stories/examples/content/content.stories.mdx +172 -0
- package/src/stories/examples/content/mixed-content.data.tsx +624 -0
- package/src/stories/examples/content-rerendering/content-updating.stories.mdx +34 -0
- package/src/stories/examples/content-rerendering/example.tsx +631 -0
- package/src/stories/examples/controls/controls.stories.mdx +120 -0
- package/src/stories/examples/image/example.tsx +149 -0
- package/src/stories/examples/image/image.stories.mdx +46 -0
- package/src/stories/examples/image-annotations/example.tsx +1442 -0
- package/src/stories/examples/image-annotations/image-annotations.stories.mdx +39 -0
- package/src/stories/examples/image-responsive/example.tsx +152 -0
- package/src/stories/examples/image-responsive/image-responsive.stories.mdx +51 -0
- package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +107 -0
- package/src/stories/examples/initial-transform/example.tsx +233 -0
- package/src/stories/examples/initial-transform/initial-transform.stories.mdx +37 -0
- package/src/stories/examples/keep-scale/example.tsx +882 -0
- package/src/stories/examples/keep-scale/keep-scale.stories.mdx +30 -0
- package/src/stories/examples/keep-scale/map.stories.mdx +28 -0
- package/src/stories/examples/medical-viewer/chest-xray.tsx +246 -0
- package/src/stories/examples/medical-viewer/example.tsx +690 -0
- package/src/stories/examples/medical-viewer/medical-viewer.stories.mdx +49 -0
- package/src/stories/examples/mini-map/example.tsx +122 -0
- package/src/stories/examples/mini-map/mini-map.stories.mdx +26 -0
- package/src/stories/examples/miro/example.tsx +1573 -0
- package/src/stories/examples/miro/miro.stories.mdx +31 -0
- package/src/stories/examples/padding/example.tsx +147 -0
- package/src/stories/examples/padding/padding.stories.mdx +35 -0
- package/src/stories/examples/product-card/example.tsx +594 -0
- package/src/stories/examples/product-card/product-card.stories.mdx +29 -0
- package/src/stories/examples/stadium/example.tsx +1040 -0
- package/src/stories/examples/stadium/stadium-data.ts +224 -0
- package/src/stories/examples/stadium/stadium.stories.mdx +32 -0
- package/src/stories/examples/stress/example.tsx +59 -0
- package/src/stories/examples/stress/stress-test.stories.mdx +28 -0
- package/src/stories/examples/svg-zoom-to-element/creativity.svg +143 -0
- package/src/stories/examples/svg-zoom-to-element/example.tsx +95 -0
- package/src/stories/examples/svg-zoom-to-element/svg-zoom-to-element.stories.mdx +28 -0
- package/src/stories/examples/text-selection/example.tsx +221 -0
- package/src/stories/examples/text-selection/text-selection.stories.mdx +31 -0
- package/src/stories/examples/velocity/example.tsx +908 -0
- package/src/stories/examples/velocity/velocity.stories.mdx +35 -0
- package/src/stories/examples/virtualize/example.tsx +239 -0
- package/src/stories/examples/virtualize/virtualize.stories.mdx +84 -0
- package/src/stories/examples/zoom-to-element/example.tsx +219 -0
- package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +31 -0
- package/src/stories/hooks/use-controls.stories.mdx +86 -0
- package/src/stories/hooks/use-transform-component.stories.mdx +108 -0
- package/src/stories/hooks/use-transform-context.stories.mdx +92 -0
- package/src/stories/hooks/use-transform-effect.stories.mdx +114 -0
- package/src/stories/hooks/use-transform-init.stories.mdx +105 -0
- package/src/stories/types/args.types.ts +192 -0
- package/src/stories/utils/args.utils.ts +35 -0
- package/src/stories/utils/bounds-overlay.tsx +168 -0
- package/src/stories/utils/code.utils.tsx +22 -0
- package/src/stories/utils/controls.utils.tsx +347 -0
- package/src/stories/utils/index.ts +5 -0
- package/src/stories/utils/styles.module.css +223 -0
- package/src/stories/utils/viewer.styles.ts +34 -0
- package/src/styles.css +8 -0
- package/src/typings.d.ts +21 -0
- package/src/utils/calculations.utils.ts +16 -0
- package/src/utils/callback.utils.ts +11 -0
- package/src/utils/context.utils.ts +49 -0
- package/src/utils/event.utils.ts +38 -0
- package/src/utils/helpers.utils.ts +22 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/ref.utils.ts +32 -0
- package/src/utils/state.utils.ts +77 -0
- package/src/utils/styles.utils.ts +49 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
import { ReactZoomPanPinchContext } from "../../models";
|
|
3
|
+
import { handleCancelAnimation } from "../animations/animations.utils";
|
|
4
|
+
import { handleAlignToScaleBounds } from "../zoom/zoom.logic";
|
|
5
|
+
import {
|
|
6
|
+
calculatePinchZoom,
|
|
7
|
+
calculateTouchMidPoint,
|
|
8
|
+
getTouchDistance,
|
|
9
|
+
} from "./pinch.utils";
|
|
10
|
+
import {
|
|
11
|
+
getMouseBoundedPosition,
|
|
12
|
+
handleCalculateBounds,
|
|
13
|
+
} from "../bounds/bounds.utils";
|
|
14
|
+
import { handleCalculateZoomPositions } from "../zoom/zoom.utils";
|
|
15
|
+
import { getPaddingValue } from "core/pan/panning.utils";
|
|
16
|
+
|
|
17
|
+
const getTouchCenter = (event: TouchEvent) => {
|
|
18
|
+
let totalX = 0;
|
|
19
|
+
let totalY = 0;
|
|
20
|
+
// Sum up the positions of all touches
|
|
21
|
+
for (let i = 0; i < 2; i += 1) {
|
|
22
|
+
totalX += event.touches[i].clientX;
|
|
23
|
+
totalY += event.touches[i].clientY;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Calculate the average position
|
|
27
|
+
const x = totalX / 2;
|
|
28
|
+
const y = totalY / 2;
|
|
29
|
+
|
|
30
|
+
return { x, y };
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const handlePinchStart = (
|
|
34
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
35
|
+
event: TouchEvent,
|
|
36
|
+
): void => {
|
|
37
|
+
const distance = getTouchDistance(event);
|
|
38
|
+
contextInstance.pinchStartDistance = distance;
|
|
39
|
+
contextInstance.lastDistance = distance;
|
|
40
|
+
contextInstance.pinchStartScale = contextInstance.state.scale;
|
|
41
|
+
contextInstance.isPanning = false;
|
|
42
|
+
contextInstance.isPinching = true;
|
|
43
|
+
|
|
44
|
+
contextInstance.pinchPreviousCenter = getTouchCenter(event);
|
|
45
|
+
|
|
46
|
+
handleCancelAnimation(contextInstance);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const handlePinchZoom = (
|
|
50
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
51
|
+
event: TouchEvent,
|
|
52
|
+
): void => {
|
|
53
|
+
const {
|
|
54
|
+
contentComponent,
|
|
55
|
+
pinchStartDistance,
|
|
56
|
+
wrapperComponent,
|
|
57
|
+
pinchPreviousCenter,
|
|
58
|
+
} = contextInstance;
|
|
59
|
+
const { scale } = contextInstance.state;
|
|
60
|
+
const {
|
|
61
|
+
limitToBounds,
|
|
62
|
+
centerZoomedOut,
|
|
63
|
+
zoomAnimation,
|
|
64
|
+
autoAlignment,
|
|
65
|
+
pinch,
|
|
66
|
+
panning,
|
|
67
|
+
} = contextInstance.setup;
|
|
68
|
+
const { disabled, size } = zoomAnimation;
|
|
69
|
+
const { allowPanning } = pinch;
|
|
70
|
+
|
|
71
|
+
// if one finger starts from outside of wrapper
|
|
72
|
+
if (pinchStartDistance === null || !contentComponent) return;
|
|
73
|
+
const midPoint = calculateTouchMidPoint(event, scale, contentComponent);
|
|
74
|
+
// if touches goes off of the wrapper element
|
|
75
|
+
if (!Number.isFinite(midPoint.x) || !Number.isFinite(midPoint.y)) return;
|
|
76
|
+
const currentDistance = getTouchDistance(event);
|
|
77
|
+
const newScale = calculatePinchZoom(contextInstance, currentDistance);
|
|
78
|
+
|
|
79
|
+
const center = getTouchCenter(event);
|
|
80
|
+
|
|
81
|
+
// pan should be scale invariant.
|
|
82
|
+
const scaleDiff = scale / newScale;
|
|
83
|
+
|
|
84
|
+
const panX = (center.x - (pinchPreviousCenter?.x || 0)) * scaleDiff;
|
|
85
|
+
const panY = (center.y - (pinchPreviousCenter?.y || 0)) * scaleDiff;
|
|
86
|
+
|
|
87
|
+
if (newScale === scale && panX === 0 && panY === 0) return;
|
|
88
|
+
|
|
89
|
+
contextInstance.pinchPreviousCenter = center;
|
|
90
|
+
|
|
91
|
+
const bounds = handleCalculateBounds(contextInstance, newScale);
|
|
92
|
+
|
|
93
|
+
const isPaddingDisabled = disabled || size === 0 || centerZoomedOut;
|
|
94
|
+
const isLimitedToBounds = limitToBounds && isPaddingDisabled;
|
|
95
|
+
const { x, y } = handleCalculateZoomPositions(
|
|
96
|
+
contextInstance,
|
|
97
|
+
midPoint.x,
|
|
98
|
+
midPoint.y,
|
|
99
|
+
newScale,
|
|
100
|
+
bounds,
|
|
101
|
+
isLimitedToBounds,
|
|
102
|
+
);
|
|
103
|
+
contextInstance.pinchMidpoint = midPoint;
|
|
104
|
+
contextInstance.lastDistance = currentDistance;
|
|
105
|
+
|
|
106
|
+
if (panning.disabled || !allowPanning) {
|
|
107
|
+
contextInstance.setState(newScale, x, y);
|
|
108
|
+
} else {
|
|
109
|
+
const { sizeX, sizeY } = autoAlignment;
|
|
110
|
+
const paddingValueX = getPaddingValue(contextInstance, sizeX, newScale);
|
|
111
|
+
const paddingValueY = getPaddingValue(contextInstance, sizeY, newScale);
|
|
112
|
+
|
|
113
|
+
const newPositionX = x + panX;
|
|
114
|
+
const newPositionY = y + panY;
|
|
115
|
+
const { x: finalX, y: finalY } = getMouseBoundedPosition(
|
|
116
|
+
newPositionX,
|
|
117
|
+
newPositionY,
|
|
118
|
+
bounds,
|
|
119
|
+
limitToBounds,
|
|
120
|
+
paddingValueX,
|
|
121
|
+
paddingValueY,
|
|
122
|
+
wrapperComponent,
|
|
123
|
+
);
|
|
124
|
+
contextInstance.setState(newScale, finalX, finalY);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export const handlePinchStop = (
|
|
129
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
130
|
+
): void => {
|
|
131
|
+
const { pinchMidpoint } = contextInstance;
|
|
132
|
+
contextInstance.velocity = null;
|
|
133
|
+
contextInstance.lastDistance = null;
|
|
134
|
+
contextInstance.pinchMidpoint = null;
|
|
135
|
+
contextInstance.pinchStartScale = null;
|
|
136
|
+
contextInstance.pinchStartDistance = null;
|
|
137
|
+
contextInstance.isPinching = false;
|
|
138
|
+
handleAlignToScaleBounds(contextInstance, pinchMidpoint?.x, pinchMidpoint?.y);
|
|
139
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
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 !== null;
|
|
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 = touches[0].clientX - contentRect.left;
|
|
45
|
+
const firstPointY = touches[0].clientY - contentRect.top;
|
|
46
|
+
const secondPointX = touches[1].clientX - contentRect.left;
|
|
47
|
+
const secondPointY = touches[1].clientY - contentRect.top;
|
|
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
|
+
(event.touches[0].pageX - event.touches[1].pageX) ** 2 +
|
|
58
|
+
(event.touches[0].pageY - event.touches[1].pageY) ** 2,
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const DEFAULT_PINCH_STEP = 5;
|
|
63
|
+
|
|
64
|
+
export const calculatePinchZoom = (
|
|
65
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
66
|
+
currentDistance: number,
|
|
67
|
+
): number => {
|
|
68
|
+
const { pinchStartScale, pinchStartDistance, setup } = contextInstance;
|
|
69
|
+
const { maxScale, minScale, zoomAnimation, disablePadding, pinch } = setup;
|
|
70
|
+
const { size, disabled } = zoomAnimation;
|
|
71
|
+
const { step } = pinch;
|
|
72
|
+
|
|
73
|
+
if (!pinchStartScale || pinchStartDistance === null) {
|
|
74
|
+
throw new Error("Pinch touches distance was not provided");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (currentDistance < 0) {
|
|
78
|
+
return contextInstance.state.scale;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const touchProportion = currentDistance / pinchStartDistance;
|
|
82
|
+
const rawScale = touchProportion * pinchStartScale;
|
|
83
|
+
const scaleDelta = (rawScale - pinchStartScale) * (step / DEFAULT_PINCH_STEP);
|
|
84
|
+
const computed = pinchStartScale + scaleDelta;
|
|
85
|
+
|
|
86
|
+
const scale = computed === Infinity ? 0 : roundNumber(computed, 10);
|
|
87
|
+
|
|
88
|
+
return checkZoomBounds(
|
|
89
|
+
scale,
|
|
90
|
+
minScale,
|
|
91
|
+
maxScale,
|
|
92
|
+
size,
|
|
93
|
+
!disabled && !disablePadding,
|
|
94
|
+
);
|
|
95
|
+
};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
import { ReactZoomPanPinchContext } from "../../models";
|
|
3
|
+
import { handleCallback } from "../../utils/callback.utils";
|
|
4
|
+
import { getContext } from "../../utils/context.utils";
|
|
5
|
+
import { cancelTimeout } from "../../utils/helpers.utils";
|
|
6
|
+
import { handleCancelAnimation } from "../animations/animations.utils";
|
|
7
|
+
import { handleCalculateBounds } from "../bounds/bounds.utils";
|
|
8
|
+
import {
|
|
9
|
+
getDelta,
|
|
10
|
+
handleCalculateWheelZoom,
|
|
11
|
+
handleWheelZoomStop,
|
|
12
|
+
getMousePosition,
|
|
13
|
+
} from "./wheel.utils";
|
|
14
|
+
import { handleAlignToScaleBounds } from "../zoom/zoom.logic";
|
|
15
|
+
import { handleCalculateZoomPositions } from "../zoom/zoom.utils";
|
|
16
|
+
|
|
17
|
+
const wheelStopEventTime = 160;
|
|
18
|
+
const wheelAnimationTime = 100;
|
|
19
|
+
|
|
20
|
+
export const handleWheelStart = (
|
|
21
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
22
|
+
event: WheelEvent,
|
|
23
|
+
): void => {
|
|
24
|
+
const { onWheelStart, onZoomStart } = contextInstance.props;
|
|
25
|
+
|
|
26
|
+
if (!contextInstance.wheelStopEventTimer) {
|
|
27
|
+
handleCancelAnimation(contextInstance);
|
|
28
|
+
handleCallback(getContext(contextInstance), event, onWheelStart);
|
|
29
|
+
handleCallback(getContext(contextInstance), event, onZoomStart);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const handleWheelZoom = (
|
|
34
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
35
|
+
event: WheelEvent,
|
|
36
|
+
): void => {
|
|
37
|
+
const { onWheel, onZoom } = contextInstance.props;
|
|
38
|
+
|
|
39
|
+
const { contentComponent, setup, state } = contextInstance;
|
|
40
|
+
const { scale } = state;
|
|
41
|
+
const {
|
|
42
|
+
limitToBounds,
|
|
43
|
+
centerZoomedOut,
|
|
44
|
+
zoomAnimation,
|
|
45
|
+
wheel,
|
|
46
|
+
disablePadding,
|
|
47
|
+
smooth,
|
|
48
|
+
} = setup;
|
|
49
|
+
const { size, disabled } = zoomAnimation;
|
|
50
|
+
const { step } = wheel;
|
|
51
|
+
|
|
52
|
+
if (!contentComponent) {
|
|
53
|
+
throw new Error("Component not mounted");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
event.stopPropagation();
|
|
58
|
+
|
|
59
|
+
const delta = getDelta(event, null);
|
|
60
|
+
const zoomStep = smooth ? step * Math.abs(event.deltaY) : step;
|
|
61
|
+
const newScale = handleCalculateWheelZoom(
|
|
62
|
+
contextInstance,
|
|
63
|
+
delta,
|
|
64
|
+
zoomStep,
|
|
65
|
+
!event.ctrlKey,
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
// if scale not change
|
|
69
|
+
if (scale === newScale) return;
|
|
70
|
+
|
|
71
|
+
const bounds = handleCalculateBounds(contextInstance, newScale);
|
|
72
|
+
|
|
73
|
+
const mousePosition = getMousePosition(event, contentComponent, scale);
|
|
74
|
+
|
|
75
|
+
const isPaddingDisabled =
|
|
76
|
+
disabled || size === 0 || centerZoomedOut || disablePadding;
|
|
77
|
+
const isLimitedToBounds = limitToBounds && isPaddingDisabled;
|
|
78
|
+
|
|
79
|
+
const { x, y } = handleCalculateZoomPositions(
|
|
80
|
+
contextInstance,
|
|
81
|
+
mousePosition.x,
|
|
82
|
+
mousePosition.y,
|
|
83
|
+
newScale,
|
|
84
|
+
bounds,
|
|
85
|
+
isLimitedToBounds,
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
contextInstance.previousWheelEvent = event;
|
|
89
|
+
|
|
90
|
+
contextInstance.setState(newScale, x, y);
|
|
91
|
+
|
|
92
|
+
handleCallback(getContext(contextInstance), event, onWheel);
|
|
93
|
+
handleCallback(getContext(contextInstance), event, onZoom);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export const handleWheelStop = (
|
|
97
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
98
|
+
event: WheelEvent,
|
|
99
|
+
): void => {
|
|
100
|
+
const { onWheelStop, onZoomStop } = contextInstance.props;
|
|
101
|
+
|
|
102
|
+
// fire animation
|
|
103
|
+
cancelTimeout(contextInstance.wheelAnimationTimer);
|
|
104
|
+
contextInstance.wheelAnimationTimer = setTimeout(() => {
|
|
105
|
+
if (!contextInstance.mounted) return;
|
|
106
|
+
handleAlignToScaleBounds(contextInstance, event.x, event.y);
|
|
107
|
+
contextInstance.wheelAnimationTimer = null;
|
|
108
|
+
}, wheelAnimationTime);
|
|
109
|
+
|
|
110
|
+
// Wheel stop event
|
|
111
|
+
const hasStoppedZooming = handleWheelZoomStop(contextInstance, event);
|
|
112
|
+
if (hasStoppedZooming) {
|
|
113
|
+
cancelTimeout(contextInstance.wheelStopEventTimer);
|
|
114
|
+
contextInstance.wheelStopEventTimer = setTimeout(() => {
|
|
115
|
+
if (!contextInstance.mounted) return;
|
|
116
|
+
contextInstance.wheelStopEventTimer = null;
|
|
117
|
+
handleCallback(getContext(contextInstance), event, onWheelStop);
|
|
118
|
+
handleCallback(getContext(contextInstance), event, onZoomStop);
|
|
119
|
+
}, wheelStopEventTime);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export const handleWheelPanningStart = (
|
|
124
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
125
|
+
event: WheelEvent,
|
|
126
|
+
): void => {
|
|
127
|
+
const { onWheelStart, onPanningStart } = contextInstance.props;
|
|
128
|
+
|
|
129
|
+
if (!contextInstance.wheelStopEventTimer) {
|
|
130
|
+
handleCancelAnimation(contextInstance);
|
|
131
|
+
handleCallback(getContext(contextInstance), event, onWheelStart);
|
|
132
|
+
handleCallback(getContext(contextInstance), event, onPanningStart);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export const handleWheelPanningStop = (
|
|
137
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
138
|
+
event: WheelEvent,
|
|
139
|
+
): void => {
|
|
140
|
+
const { onWheelStop, onPanningStop } = contextInstance.props;
|
|
141
|
+
|
|
142
|
+
// fire animation
|
|
143
|
+
cancelTimeout(contextInstance.wheelAnimationTimer);
|
|
144
|
+
contextInstance.wheelAnimationTimer = setTimeout(() => {
|
|
145
|
+
if (!contextInstance.mounted) return;
|
|
146
|
+
handleAlignToScaleBounds(contextInstance, event.x, event.y);
|
|
147
|
+
contextInstance.wheelAnimationTimer = null;
|
|
148
|
+
}, wheelAnimationTime);
|
|
149
|
+
|
|
150
|
+
// Wheel stop event
|
|
151
|
+
const hasStoppedZooming = handleWheelZoomStop(contextInstance, event);
|
|
152
|
+
if (hasStoppedZooming) {
|
|
153
|
+
cancelTimeout(contextInstance.wheelStopEventTimer);
|
|
154
|
+
contextInstance.wheelStopEventTimer = setTimeout(() => {
|
|
155
|
+
if (!contextInstance.mounted) return;
|
|
156
|
+
contextInstance.wheelStopEventTimer = null;
|
|
157
|
+
handleCallback(getContext(contextInstance), event, onWheelStop);
|
|
158
|
+
handleCallback(getContext(contextInstance), event, onPanningStop);
|
|
159
|
+
}, wheelStopEventTime);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { ReactZoomPanPinchContext, PositionType } from "../../models";
|
|
2
|
+
import { checkIsNumber, isExcludedNode } from "../../utils";
|
|
3
|
+
import { checkZoomBounds } from "../zoom/zoom.utils";
|
|
4
|
+
|
|
5
|
+
export const isWheelAllowed = (
|
|
6
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
7
|
+
event: WheelEvent,
|
|
8
|
+
): boolean => {
|
|
9
|
+
const { disabled, wheelDisabled, touchPadDisabled, excluded } =
|
|
10
|
+
contextInstance.setup.wheel;
|
|
11
|
+
const { isInitialized, isPanning } = contextInstance;
|
|
12
|
+
|
|
13
|
+
const target = event.target as HTMLElement;
|
|
14
|
+
const isAllowed = isInitialized && !isPanning && !disabled && target;
|
|
15
|
+
|
|
16
|
+
if (!isAllowed) return false;
|
|
17
|
+
// Event ctrlKey detects if touchpad action is executing wheel or pinch gesture
|
|
18
|
+
if (wheelDisabled && !event.ctrlKey) return false;
|
|
19
|
+
if (touchPadDisabled && event.ctrlKey) return false;
|
|
20
|
+
|
|
21
|
+
const isExcluded = isExcludedNode(target, excluded);
|
|
22
|
+
|
|
23
|
+
if (isExcluded) return false;
|
|
24
|
+
|
|
25
|
+
const keysPressed = contextInstance.isPressingKeys(
|
|
26
|
+
contextInstance.setup.wheel.activationKeys,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
if (!keysPressed) return false;
|
|
30
|
+
|
|
31
|
+
return true;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const isWheelPanningAllowed = (
|
|
35
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
36
|
+
event: WheelEvent,
|
|
37
|
+
): boolean => {
|
|
38
|
+
const { disabled, trackPadPanning } = contextInstance.setup;
|
|
39
|
+
const { activationKeys, excluded } = trackPadPanning;
|
|
40
|
+
|
|
41
|
+
if (!contextInstance.wrapperComponent || !contextInstance.contentComponent) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (disabled || trackPadPanning.disabled || event.ctrlKey) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const isAllowed = isWheelAllowed(contextInstance, event);
|
|
50
|
+
// Cannot execute at the same time as wheel zoom
|
|
51
|
+
if (isAllowed) return false;
|
|
52
|
+
|
|
53
|
+
const target = event.target as HTMLElement;
|
|
54
|
+
const isExcluded = isExcludedNode(target, excluded);
|
|
55
|
+
if (isExcluded) return false;
|
|
56
|
+
|
|
57
|
+
const keysPressed = contextInstance.isPressingKeys(activationKeys);
|
|
58
|
+
if (!keysPressed) return false;
|
|
59
|
+
|
|
60
|
+
return true;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const getDeltaY = (event?: WheelEvent) => {
|
|
64
|
+
if (event) {
|
|
65
|
+
return event.deltaY < 0 ? 1 : -1;
|
|
66
|
+
}
|
|
67
|
+
return 0;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export function getDelta(
|
|
71
|
+
event: WheelEvent,
|
|
72
|
+
customDelta?: number | null,
|
|
73
|
+
): number {
|
|
74
|
+
const deltaY = getDeltaY(event);
|
|
75
|
+
const delta = checkIsNumber(customDelta, deltaY);
|
|
76
|
+
return delta;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function getMousePosition(
|
|
80
|
+
event: WheelEvent | MouseEvent | TouchEvent,
|
|
81
|
+
contentComponent: HTMLDivElement,
|
|
82
|
+
scale: number,
|
|
83
|
+
): PositionType {
|
|
84
|
+
const contentRect = contentComponent.getBoundingClientRect();
|
|
85
|
+
|
|
86
|
+
let mouseX = 0;
|
|
87
|
+
let mouseY = 0;
|
|
88
|
+
|
|
89
|
+
if ("clientX" in event) {
|
|
90
|
+
// mouse position x, y over wrapper component
|
|
91
|
+
mouseX = (event.clientX - contentRect.left) / scale;
|
|
92
|
+
mouseY = (event.clientY - contentRect.top) / scale;
|
|
93
|
+
} else {
|
|
94
|
+
const touch = event.touches[0];
|
|
95
|
+
mouseX = (touch.clientX - contentRect.left) / scale;
|
|
96
|
+
mouseY = (touch.clientY - contentRect.top) / scale;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (Number.isNaN(mouseX) || Number.isNaN(mouseY))
|
|
100
|
+
console.error("No mouse or touch offset found");
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
x: mouseX,
|
|
104
|
+
y: mouseY,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export const handleCalculateWheelZoom = (
|
|
109
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
110
|
+
delta: number,
|
|
111
|
+
step: number,
|
|
112
|
+
disable: boolean,
|
|
113
|
+
getTarget?: boolean,
|
|
114
|
+
): number => {
|
|
115
|
+
const { scale } = contextInstance.state;
|
|
116
|
+
const { wrapperComponent, setup } = contextInstance;
|
|
117
|
+
const { maxScale, minScale, zoomAnimation, disablePadding } = setup;
|
|
118
|
+
const { size, disabled } = zoomAnimation;
|
|
119
|
+
|
|
120
|
+
if (!wrapperComponent) {
|
|
121
|
+
throw new Error("Wrapper is not mounted");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const targetScale = scale + delta * step;
|
|
125
|
+
|
|
126
|
+
if (getTarget) return targetScale;
|
|
127
|
+
const paddingEnabled = disable ? false : !disabled;
|
|
128
|
+
const newScale = checkZoomBounds(
|
|
129
|
+
targetScale,
|
|
130
|
+
minScale,
|
|
131
|
+
maxScale,
|
|
132
|
+
size,
|
|
133
|
+
paddingEnabled && !disablePadding,
|
|
134
|
+
);
|
|
135
|
+
return newScale;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export const handleWheelZoomStop = (
|
|
139
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
140
|
+
event: WheelEvent,
|
|
141
|
+
): boolean => {
|
|
142
|
+
const { previousWheelEvent } = contextInstance;
|
|
143
|
+
const { scale } = contextInstance.state;
|
|
144
|
+
const { maxScale, minScale } = contextInstance.setup;
|
|
145
|
+
|
|
146
|
+
if (!previousWheelEvent) return false;
|
|
147
|
+
if (scale < maxScale || scale > minScale) return true;
|
|
148
|
+
if (Math.sign(previousWheelEvent.deltaY) !== Math.sign(event.deltaY))
|
|
149
|
+
return true;
|
|
150
|
+
if (previousWheelEvent.deltaY > 0 && previousWheelEvent.deltaY < event.deltaY)
|
|
151
|
+
return true;
|
|
152
|
+
if (previousWheelEvent.deltaY < 0 && previousWheelEvent.deltaY > event.deltaY)
|
|
153
|
+
return true;
|
|
154
|
+
if (Math.sign(previousWheelEvent.deltaY) !== Math.sign(event.deltaY))
|
|
155
|
+
return true;
|
|
156
|
+
return false;
|
|
157
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ReactZoomPanPinchContext, ReactZoomPanPinchState } from "../../models";
|
|
2
|
+
import { roundNumber } from "../../utils";
|
|
3
|
+
import { animate } from "../animations/animations.utils";
|
|
4
|
+
import { handleCalculateBounds } from "../bounds/bounds.utils";
|
|
5
|
+
import { handleAlignToBounds } from "../pan/panning.logic";
|
|
6
|
+
import { checkZoomBounds, handleCalculateZoomPositions } from "./zoom.utils";
|
|
7
|
+
|
|
8
|
+
export function handleZoomToPoint(
|
|
9
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
10
|
+
scale: number,
|
|
11
|
+
mouseX: number,
|
|
12
|
+
mouseY: number,
|
|
13
|
+
): Omit<ReactZoomPanPinchState, "previousScale"> | undefined {
|
|
14
|
+
const { minScale, maxScale, limitToBounds } = contextInstance.setup;
|
|
15
|
+
|
|
16
|
+
const newScale = checkZoomBounds(
|
|
17
|
+
roundNumber(scale, 2),
|
|
18
|
+
minScale,
|
|
19
|
+
maxScale,
|
|
20
|
+
0,
|
|
21
|
+
false,
|
|
22
|
+
);
|
|
23
|
+
const bounds = handleCalculateBounds(contextInstance, newScale);
|
|
24
|
+
|
|
25
|
+
const { x, y } = handleCalculateZoomPositions(
|
|
26
|
+
contextInstance,
|
|
27
|
+
mouseX,
|
|
28
|
+
mouseY,
|
|
29
|
+
newScale,
|
|
30
|
+
bounds,
|
|
31
|
+
limitToBounds,
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
return { scale: newScale, positionX: x, positionY: y };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function handleAlignToScaleBounds(
|
|
38
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
39
|
+
mousePositionX?: number,
|
|
40
|
+
mousePositionY?: number,
|
|
41
|
+
): void {
|
|
42
|
+
const { scale } = contextInstance.state;
|
|
43
|
+
const { wrapperComponent } = contextInstance;
|
|
44
|
+
const { minScale, maxScale, limitToBounds, zoomAnimation } =
|
|
45
|
+
contextInstance.setup;
|
|
46
|
+
const { disabled, animationTime, animationType } = zoomAnimation;
|
|
47
|
+
|
|
48
|
+
const isWithinBounds = scale >= minScale && scale <= maxScale;
|
|
49
|
+
const isDisabled = disabled || isWithinBounds;
|
|
50
|
+
|
|
51
|
+
if (scale >= 1 || limitToBounds) {
|
|
52
|
+
handleAlignToBounds(contextInstance);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (isDisabled || !wrapperComponent || !contextInstance.mounted) return;
|
|
56
|
+
|
|
57
|
+
const mouseX = mousePositionX || wrapperComponent.offsetWidth / 2;
|
|
58
|
+
const mouseY = mousePositionY || wrapperComponent.offsetHeight / 2;
|
|
59
|
+
|
|
60
|
+
const targetScale = scale < minScale ? minScale : maxScale;
|
|
61
|
+
const targetState = handleZoomToPoint(
|
|
62
|
+
contextInstance,
|
|
63
|
+
targetScale,
|
|
64
|
+
mouseX,
|
|
65
|
+
mouseY,
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
if (targetState) {
|
|
69
|
+
animate(contextInstance, targetState, animationTime, animationType);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BoundsType,
|
|
3
|
+
PositionType,
|
|
4
|
+
ReactZoomPanPinchContext,
|
|
5
|
+
} from "../../models";
|
|
6
|
+
import { getMouseBoundedPosition } from "../bounds/bounds.utils";
|
|
7
|
+
|
|
8
|
+
export function handleCalculateZoomPositions(
|
|
9
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
10
|
+
mouseX: number,
|
|
11
|
+
mouseY: number,
|
|
12
|
+
newScale: number,
|
|
13
|
+
bounds: BoundsType,
|
|
14
|
+
limitToBounds: boolean,
|
|
15
|
+
): PositionType {
|
|
16
|
+
const { scale, positionX, positionY } = contextInstance.state;
|
|
17
|
+
|
|
18
|
+
const scaleDifference = newScale - scale;
|
|
19
|
+
|
|
20
|
+
if (typeof mouseX !== "number" || typeof mouseY !== "number") {
|
|
21
|
+
console.error("Mouse X and Y position were not provided!");
|
|
22
|
+
return { x: positionX, y: positionY };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const calculatedPositionX = positionX - mouseX * scaleDifference;
|
|
26
|
+
const calculatedPositionY = positionY - mouseY * scaleDifference;
|
|
27
|
+
|
|
28
|
+
// do not limit to bounds when there is padding animation,
|
|
29
|
+
// it causes animation strange behaviour
|
|
30
|
+
|
|
31
|
+
const newPositions = getMouseBoundedPosition(
|
|
32
|
+
calculatedPositionX,
|
|
33
|
+
calculatedPositionY,
|
|
34
|
+
bounds,
|
|
35
|
+
limitToBounds,
|
|
36
|
+
0,
|
|
37
|
+
0,
|
|
38
|
+
null,
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
return newPositions;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const MIN_SAFE_SCALE = 1e-7;
|
|
45
|
+
|
|
46
|
+
export function checkZoomBounds(
|
|
47
|
+
zoom: number,
|
|
48
|
+
minScale: number,
|
|
49
|
+
maxScale: number,
|
|
50
|
+
zoomPadding: number,
|
|
51
|
+
enablePadding: boolean,
|
|
52
|
+
): number {
|
|
53
|
+
const scalePadding = enablePadding ? zoomPadding : 0;
|
|
54
|
+
const minScaleWithPadding = Math.max(minScale - scalePadding, MIN_SAFE_SCALE);
|
|
55
|
+
const maxScaleWithPadding = maxScale + scalePadding;
|
|
56
|
+
|
|
57
|
+
if (!Number.isNaN(maxScale) && zoom >= maxScaleWithPadding)
|
|
58
|
+
return maxScaleWithPadding;
|
|
59
|
+
if (!Number.isNaN(minScale) && zoom <= minScaleWithPadding)
|
|
60
|
+
return minScaleWithPadding;
|
|
61
|
+
return Math.max(zoom, MIN_SAFE_SCALE);
|
|
62
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ReactZoomPanPinchContentRef } from "models";
|
|
2
|
+
import { getControls } from "utils";
|
|
3
|
+
import { useTransformContext } from "./use-transform-context";
|
|
4
|
+
|
|
5
|
+
export const useControls = (): ReactZoomPanPinchContentRef => {
|
|
6
|
+
const libraryContext = useTransformContext();
|
|
7
|
+
|
|
8
|
+
return getControls(libraryContext);
|
|
9
|
+
};
|