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.
- package/README.md +43 -0
- package/dist/index.cjs.js +60 -34
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.esm.js +60 -34
- package/dist/index.esm.js.map +1 -1
- package/package.json +5 -4
- package/src/.eslintrc.js +36 -0
- package/src/components/transform-component.module.css +30 -0
- package/src/components/transform-component.tsx +52 -0
- package/src/components/transform-context.tsx +493 -0
- package/src/components/transform-wrapper.tsx +18 -0
- package/src/constants/state.constants.ts +88 -0
- package/src/core/animations/animations.constants.ts +76 -0
- package/src/core/animations/animations.utils.ts +109 -0
- package/src/core/bounds/bounds.types.ts +8 -0
- package/src/core/bounds/bounds.utils.ts +157 -0
- package/src/core/double-click/double-click.logic.ts +75 -0
- package/src/core/handlers/handlers.logic.ts +112 -0
- package/src/core/handlers/handlers.utils.ts +202 -0
- package/src/core/pan/panning.logic.ts +99 -0
- package/src/core/pan/panning.utils.ts +199 -0
- package/src/core/pan/velocity.logic.ts +148 -0
- package/src/core/pan/velocity.utils.ts +79 -0
- package/src/core/pinch/pinch.logic.ts +83 -0
- package/src/core/pinch/pinch.utils.ts +88 -0
- package/src/core/wheel/wheel.logic.ts +118 -0
- package/src/core/wheel/wheel.utils.ts +121 -0
- package/src/core/zoom/zoom.logic.ts +69 -0
- package/src/core/zoom/zoom.utils.ts +58 -0
- package/src/hooks/use-transform-context.tsx +16 -0
- package/src/index.ts +6 -0
- package/src/models/calculations.model.ts +26 -0
- package/src/models/context.model.ts +179 -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/medium-image.jpg +0 -0
- package/src/stories/assets/small-image.jpg +0 -0
- package/src/stories/assets/svg-example.svg +14 -0
- package/src/stories/docs/Introduction.stories.mdx +97 -0
- package/src/stories/docs/handlers.stories.mdx +48 -0
- package/src/stories/docs/handlers.tsx +107 -0
- package/src/stories/docs/props.stories.mdx +92 -0
- package/src/stories/docs/props.tsx +490 -0
- package/src/stories/examples/content/content.stories.mdx +122 -0
- package/src/stories/examples/content-rerendering/content-updating.stories.mdx +42 -0
- package/src/stories/examples/content-rerendering/example.tsx +74 -0
- package/src/stories/examples/controls/controls.stories.mdx +71 -0
- package/src/stories/examples/image-big/image-big.stories.mdx +44 -0
- package/src/stories/examples/image-medium/image-medium.stories.mdx +42 -0
- package/src/stories/examples/image-small/image-small.stories.mdx +42 -0
- package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +47 -0
- package/src/stories/examples/zoom-to-element/example.tsx +91 -0
- package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +28 -0
- package/src/stories/types/args.types.ts +201 -0
- package/src/stories/utils/args.utils.ts +34 -0
- package/src/stories/utils/code.utils.tsx +21 -0
- package/src/stories/utils/controls.utils.tsx +26 -0
- package/src/stories/utils/index.ts +3 -0
- package/src/stories/utils/styles.module.css +29 -0
- package/src/styles.css +8 -0
- package/src/typings.d.ts +10 -0
- package/src/utils/calculations.utils.ts +16 -0
- package/src/utils/callback.utils.ts +11 -0
- package/src/utils/context.utils.ts +27 -0
- package/src/utils/event.utils.ts +22 -0
- package/src/utils/helpers.utils.ts +27 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/state.utils.ts +40 -0
- package/src/utils/styles.utils.ts +35 -0
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { ReactZoomPanPinchContext } from "../../models";
|
|
2
|
+
import { handleCallback } from "../../utils/callback.utils";
|
|
3
|
+
import { getContext } from "../../utils/context.utils";
|
|
4
|
+
import { cancelTimeout } from "../../utils/helpers.utils";
|
|
5
|
+
import { handleCancelAnimation } from "../animations/animations.utils";
|
|
6
|
+
import { handleCalculateBounds } from "../bounds/bounds.utils";
|
|
7
|
+
import {
|
|
8
|
+
getDelta,
|
|
9
|
+
handleCalculateWheelZoom,
|
|
10
|
+
handleWheelZoomStop,
|
|
11
|
+
getMousePosition,
|
|
12
|
+
} from "./wheel.utils";
|
|
13
|
+
import { handleAlignToScaleBounds } from "../zoom/zoom.logic";
|
|
14
|
+
import { handleCalculateZoomPositions } from "../zoom/zoom.utils";
|
|
15
|
+
|
|
16
|
+
const wheelStopEventTime = 160;
|
|
17
|
+
const wheelAnimationTime = 100;
|
|
18
|
+
|
|
19
|
+
export const handleWheelStart = (
|
|
20
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
21
|
+
event: WheelEvent,
|
|
22
|
+
): void => {
|
|
23
|
+
const { onWheelStart, onZoomStart } = contextInstance.props;
|
|
24
|
+
|
|
25
|
+
if (!contextInstance.wheelStopEventTimer) {
|
|
26
|
+
handleCancelAnimation(contextInstance);
|
|
27
|
+
handleCallback(getContext(contextInstance), event, onWheelStart);
|
|
28
|
+
handleCallback(getContext(contextInstance), event, onZoomStart);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const handleWheelZoom = (
|
|
33
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
34
|
+
event: WheelEvent,
|
|
35
|
+
): void => {
|
|
36
|
+
const { onWheel, onZoom } = contextInstance.props;
|
|
37
|
+
|
|
38
|
+
const { contentComponent, setup, transformState } = contextInstance;
|
|
39
|
+
const { scale } = transformState;
|
|
40
|
+
const {
|
|
41
|
+
limitToBounds,
|
|
42
|
+
centerZoomedOut,
|
|
43
|
+
zoomAnimation,
|
|
44
|
+
wheel,
|
|
45
|
+
disablePadding,
|
|
46
|
+
} = setup;
|
|
47
|
+
const { size, disabled } = zoomAnimation;
|
|
48
|
+
const { step } = wheel;
|
|
49
|
+
|
|
50
|
+
if (!contentComponent) {
|
|
51
|
+
throw new Error("Component not mounted");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
event.preventDefault();
|
|
55
|
+
event.stopPropagation();
|
|
56
|
+
|
|
57
|
+
const delta = getDelta(event, null);
|
|
58
|
+
const newScale = handleCalculateWheelZoom(
|
|
59
|
+
contextInstance,
|
|
60
|
+
delta,
|
|
61
|
+
step,
|
|
62
|
+
!event.ctrlKey,
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
// if scale not change
|
|
66
|
+
if (scale === newScale) return;
|
|
67
|
+
|
|
68
|
+
const bounds = handleCalculateBounds(contextInstance, newScale);
|
|
69
|
+
|
|
70
|
+
const mousePosition = getMousePosition(event, contentComponent, scale);
|
|
71
|
+
|
|
72
|
+
const isPaddingDisabled =
|
|
73
|
+
disabled || size === 0 || centerZoomedOut || disablePadding;
|
|
74
|
+
const isLimitedToBounds = limitToBounds && isPaddingDisabled;
|
|
75
|
+
|
|
76
|
+
const { x, y } = handleCalculateZoomPositions(
|
|
77
|
+
contextInstance,
|
|
78
|
+
mousePosition.x,
|
|
79
|
+
mousePosition.y,
|
|
80
|
+
newScale,
|
|
81
|
+
bounds,
|
|
82
|
+
isLimitedToBounds,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
contextInstance.previousWheelEvent = event;
|
|
86
|
+
|
|
87
|
+
contextInstance.setTransformState(newScale, x, y);
|
|
88
|
+
|
|
89
|
+
handleCallback(getContext(contextInstance), event, onWheel);
|
|
90
|
+
handleCallback(getContext(contextInstance), event, onZoom);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const handleWheelStop = (
|
|
94
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
95
|
+
event: WheelEvent,
|
|
96
|
+
): void => {
|
|
97
|
+
const { onWheelStop, onZoomStop } = contextInstance.props;
|
|
98
|
+
|
|
99
|
+
// fire animation
|
|
100
|
+
cancelTimeout(contextInstance.wheelAnimationTimer);
|
|
101
|
+
contextInstance.wheelAnimationTimer = setTimeout(() => {
|
|
102
|
+
if (!contextInstance.mounted) return;
|
|
103
|
+
handleAlignToScaleBounds(contextInstance, event.x, event.y);
|
|
104
|
+
contextInstance.wheelAnimationTimer = null;
|
|
105
|
+
}, wheelAnimationTime);
|
|
106
|
+
|
|
107
|
+
// Wheel stop event
|
|
108
|
+
const hasStoppedZooming = handleWheelZoomStop(contextInstance, event);
|
|
109
|
+
if (hasStoppedZooming) {
|
|
110
|
+
cancelTimeout(contextInstance.wheelStopEventTimer);
|
|
111
|
+
contextInstance.wheelStopEventTimer = setTimeout(() => {
|
|
112
|
+
if (!contextInstance.mounted) return;
|
|
113
|
+
contextInstance.wheelStopEventTimer = null;
|
|
114
|
+
handleCallback(getContext(contextInstance), event, onWheelStop);
|
|
115
|
+
handleCallback(getContext(contextInstance), event, onZoomStop);
|
|
116
|
+
}, wheelStopEventTime);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { ReactZoomPanPinchContext } from "../../models";
|
|
2
|
+
import { PositionType } from "../../models";
|
|
3
|
+
|
|
4
|
+
import { checkIsNumber, isExcludedNode, roundNumber } from "../../utils";
|
|
5
|
+
import { checkZoomBounds } from "../zoom/zoom.utils";
|
|
6
|
+
|
|
7
|
+
export const isWheelAllowed = (
|
|
8
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
9
|
+
event: WheelEvent,
|
|
10
|
+
): boolean => {
|
|
11
|
+
const {
|
|
12
|
+
disabled,
|
|
13
|
+
wheelDisabled,
|
|
14
|
+
touchPadDisabled,
|
|
15
|
+
excluded,
|
|
16
|
+
} = contextInstance.setup.wheel;
|
|
17
|
+
const { isInitialized, isPanning } = contextInstance;
|
|
18
|
+
|
|
19
|
+
const target = event.target as HTMLElement;
|
|
20
|
+
const isAllowed = isInitialized && !isPanning && !disabled && target;
|
|
21
|
+
|
|
22
|
+
if (!isAllowed) return false;
|
|
23
|
+
// Event ctrlKey detects if touchpad action is executing wheel or pinch gesture
|
|
24
|
+
if (wheelDisabled && !event.ctrlKey) return false;
|
|
25
|
+
if (touchPadDisabled && event.ctrlKey) return false;
|
|
26
|
+
|
|
27
|
+
const isExcluded = isExcludedNode(target, excluded);
|
|
28
|
+
|
|
29
|
+
if (isExcluded) return false;
|
|
30
|
+
|
|
31
|
+
return true;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export function getDelta(
|
|
35
|
+
event: WheelEvent,
|
|
36
|
+
customDelta?: number | null,
|
|
37
|
+
): number {
|
|
38
|
+
const deltaY = event ? (event.deltaY < 0 ? 1 : -1) : 0;
|
|
39
|
+
const delta = checkIsNumber(customDelta, deltaY);
|
|
40
|
+
return delta;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function getMousePosition(
|
|
44
|
+
event: WheelEvent | MouseEvent | TouchEvent,
|
|
45
|
+
contentComponent: HTMLDivElement,
|
|
46
|
+
scale: number,
|
|
47
|
+
): PositionType {
|
|
48
|
+
const contentRect = contentComponent.getBoundingClientRect();
|
|
49
|
+
|
|
50
|
+
let mouseX = 0;
|
|
51
|
+
let mouseY = 0;
|
|
52
|
+
|
|
53
|
+
if ("clientX" in event) {
|
|
54
|
+
// mouse position x, y over wrapper component
|
|
55
|
+
mouseX = (event.clientX - contentRect.left) / scale;
|
|
56
|
+
mouseY = (event.clientY - contentRect.top) / scale;
|
|
57
|
+
} else {
|
|
58
|
+
const touch = event.touches[0];
|
|
59
|
+
mouseX = (touch.clientX - contentRect.left) / scale;
|
|
60
|
+
mouseY = (touch.clientY - contentRect.top) / scale;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (isNaN(mouseX) || isNaN(mouseY))
|
|
64
|
+
console.error("No mouse or touch offset found");
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
x: mouseX,
|
|
68
|
+
y: mouseY,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export const handleCalculateWheelZoom = (
|
|
73
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
74
|
+
delta: number,
|
|
75
|
+
step: number,
|
|
76
|
+
disable: boolean,
|
|
77
|
+
getTarget?: boolean,
|
|
78
|
+
): number => {
|
|
79
|
+
const { scale } = contextInstance.transformState;
|
|
80
|
+
const { wrapperComponent, setup } = contextInstance;
|
|
81
|
+
const { maxScale, minScale, zoomAnimation, disablePadding } = setup;
|
|
82
|
+
const { size, disabled } = zoomAnimation;
|
|
83
|
+
|
|
84
|
+
if (!wrapperComponent) {
|
|
85
|
+
throw new Error("Wrapper is not mounted");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const targetScale = scale + delta * (scale - scale * step) * step;
|
|
89
|
+
|
|
90
|
+
if (getTarget) return targetScale;
|
|
91
|
+
const paddingEnabled = disable ? false : !disabled;
|
|
92
|
+
const newScale = checkZoomBounds(
|
|
93
|
+
roundNumber(targetScale, 3),
|
|
94
|
+
minScale,
|
|
95
|
+
maxScale,
|
|
96
|
+
size,
|
|
97
|
+
paddingEnabled && !disablePadding,
|
|
98
|
+
);
|
|
99
|
+
return newScale;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export const handleWheelZoomStop = (
|
|
103
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
104
|
+
event: WheelEvent,
|
|
105
|
+
): boolean => {
|
|
106
|
+
const { previousWheelEvent } = contextInstance;
|
|
107
|
+
const { scale } = contextInstance.transformState;
|
|
108
|
+
const { maxScale, minScale } = contextInstance.setup;
|
|
109
|
+
|
|
110
|
+
if (!previousWheelEvent) return false;
|
|
111
|
+
if (scale < maxScale || scale > minScale) return true;
|
|
112
|
+
if (Math.sign(previousWheelEvent.deltaY) !== Math.sign(event.deltaY))
|
|
113
|
+
return true;
|
|
114
|
+
if (previousWheelEvent.deltaY > 0 && previousWheelEvent.deltaY < event.deltaY)
|
|
115
|
+
return true;
|
|
116
|
+
if (previousWheelEvent.deltaY < 0 && previousWheelEvent.deltaY > event.deltaY)
|
|
117
|
+
return true;
|
|
118
|
+
if (Math.sign(previousWheelEvent.deltaY) !== Math.sign(event.deltaY))
|
|
119
|
+
return true;
|
|
120
|
+
return false;
|
|
121
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
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 handleAlignToScaleBounds(
|
|
9
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
10
|
+
mousePositionX?: number,
|
|
11
|
+
mousePositionY?: number,
|
|
12
|
+
): void {
|
|
13
|
+
const { scale } = contextInstance.transformState;
|
|
14
|
+
const { wrapperComponent } = contextInstance;
|
|
15
|
+
const { minScale, limitToBounds, zoomAnimation } = contextInstance.setup;
|
|
16
|
+
const { disabled, animationTime, animationType } = zoomAnimation;
|
|
17
|
+
|
|
18
|
+
const isDisabled = disabled || scale >= minScale;
|
|
19
|
+
|
|
20
|
+
if (scale >= 1 || limitToBounds) {
|
|
21
|
+
// fire fit to bounds animation
|
|
22
|
+
handleAlignToBounds(contextInstance);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (isDisabled || !wrapperComponent || !contextInstance.mounted) return;
|
|
26
|
+
|
|
27
|
+
const mouseX = mousePositionX || wrapperComponent.offsetWidth / 2;
|
|
28
|
+
const mouseY = mousePositionY || wrapperComponent.offsetHeight / 2;
|
|
29
|
+
|
|
30
|
+
const targetState = handleZoomToPoint(
|
|
31
|
+
contextInstance,
|
|
32
|
+
minScale,
|
|
33
|
+
mouseX,
|
|
34
|
+
mouseY,
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
if (targetState) {
|
|
38
|
+
animate(contextInstance, targetState, animationTime, animationType);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function handleZoomToPoint(
|
|
43
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
44
|
+
scale: number,
|
|
45
|
+
mouseX: number,
|
|
46
|
+
mouseY: number,
|
|
47
|
+
): Omit<ReactZoomPanPinchState, "previousScale"> | undefined {
|
|
48
|
+
const { minScale, maxScale, limitToBounds } = contextInstance.setup;
|
|
49
|
+
|
|
50
|
+
const newScale = checkZoomBounds(
|
|
51
|
+
roundNumber(scale, 2),
|
|
52
|
+
minScale,
|
|
53
|
+
maxScale,
|
|
54
|
+
0,
|
|
55
|
+
false,
|
|
56
|
+
);
|
|
57
|
+
const bounds = handleCalculateBounds(contextInstance, newScale);
|
|
58
|
+
|
|
59
|
+
const { x, y } = handleCalculateZoomPositions(
|
|
60
|
+
contextInstance,
|
|
61
|
+
mouseX,
|
|
62
|
+
mouseY,
|
|
63
|
+
newScale,
|
|
64
|
+
bounds,
|
|
65
|
+
limitToBounds,
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
return { scale: newScale, positionX: x, positionY: y };
|
|
69
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
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.transformState;
|
|
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
|
+
export function checkZoomBounds(
|
|
45
|
+
zoom: number,
|
|
46
|
+
minScale: number,
|
|
47
|
+
maxScale: number,
|
|
48
|
+
zoomPadding: number,
|
|
49
|
+
enablePadding: boolean,
|
|
50
|
+
): number {
|
|
51
|
+
const scalePadding = enablePadding ? zoomPadding : 0;
|
|
52
|
+
const minScaleWithPadding = minScale - scalePadding;
|
|
53
|
+
|
|
54
|
+
if (!isNaN(maxScale) && zoom >= maxScale) return maxScale;
|
|
55
|
+
if (!isNaN(minScale) && zoom <= minScaleWithPadding)
|
|
56
|
+
return minScaleWithPadding;
|
|
57
|
+
return zoom;
|
|
58
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { getContext } from "utils";
|
|
3
|
+
import { Context } from "../components/transform-context";
|
|
4
|
+
import { ReactZoomPanPinchRef } from "../models";
|
|
5
|
+
|
|
6
|
+
const useTransformContext = (): ReactZoomPanPinchRef => {
|
|
7
|
+
const libraryContext = useContext(Context);
|
|
8
|
+
|
|
9
|
+
if (!libraryContext.contextInstance) {
|
|
10
|
+
throw new Error("Transform context mus be placed inside TransformWrapper");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return getContext(libraryContext.contextInstance);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default useTransformContext;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { default as TransformWrapper } from "./components/transform-wrapper";
|
|
2
|
+
export { default as TransformComponent } from "./components/transform-component";
|
|
3
|
+
export { default as useTransformContext } from "./hooks/use-transform-context";
|
|
4
|
+
|
|
5
|
+
export * from "./models/context.model";
|
|
6
|
+
export * from "./models/calculations.model";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type SizeType = {
|
|
2
|
+
width: number;
|
|
3
|
+
height: number;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type PositionType = {
|
|
7
|
+
x: number;
|
|
8
|
+
y: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type StateType = { scale: number; positionX: number; positionY: number };
|
|
12
|
+
|
|
13
|
+
export type VelocityType = {
|
|
14
|
+
velocityX: number;
|
|
15
|
+
velocityY: number;
|
|
16
|
+
total: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type BoundsType = {
|
|
20
|
+
minPositionX: number;
|
|
21
|
+
maxPositionX: number;
|
|
22
|
+
minPositionY: number;
|
|
23
|
+
maxPositionY: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type AnimationType = () => void | number;
|