react-zoom-pan-pinch 3.6.1 → 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 +770 -277
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +116 -22
- package/dist/index.esm.js +766 -269
- package/dist/index.esm.js.map +1 -1
- package/package.json +73 -60
- 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,122 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
import { DeviceType } from "models";
|
|
3
|
+
import { ReactZoomPanPinchContext } from "../../models/context.model";
|
|
4
|
+
import { animate, handleCancelAnimation } from "../animations/animations.utils";
|
|
5
|
+
import { handleCalculateBounds } from "../bounds/bounds.utils";
|
|
6
|
+
import {
|
|
7
|
+
getPaddingValue,
|
|
8
|
+
getPanningClientPosition,
|
|
9
|
+
handleNewPosition,
|
|
10
|
+
handlePanningSetup,
|
|
11
|
+
handlePanToBounds,
|
|
12
|
+
handleTouchPanningSetup,
|
|
13
|
+
} from "./panning.utils";
|
|
14
|
+
import {
|
|
15
|
+
handleVelocityPanning,
|
|
16
|
+
handleCalculateVelocity,
|
|
17
|
+
} from "./velocity.logic";
|
|
18
|
+
|
|
19
|
+
export function handlePanningStart(
|
|
20
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
21
|
+
event: MouseEvent | TouchEvent,
|
|
22
|
+
): void {
|
|
23
|
+
const { scale, positionX, positionY } = contextInstance.state;
|
|
24
|
+
|
|
25
|
+
contextInstance.panStartPosition = { x: positionX, y: positionY };
|
|
26
|
+
handleCancelAnimation(contextInstance);
|
|
27
|
+
handleCalculateBounds(contextInstance, scale);
|
|
28
|
+
if (window.TouchEvent !== undefined && event instanceof TouchEvent) {
|
|
29
|
+
handleTouchPanningSetup(contextInstance, event as TouchEvent);
|
|
30
|
+
} else {
|
|
31
|
+
handlePanningSetup(contextInstance, event as MouseEvent);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function handleAlignToBounds(
|
|
36
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
37
|
+
customAnimationTime?: number,
|
|
38
|
+
): void {
|
|
39
|
+
const { scale } = contextInstance.state;
|
|
40
|
+
const { minScale, autoAlignment } = contextInstance.setup;
|
|
41
|
+
const { disabled, sizeX, sizeY, animationTime, animationType } =
|
|
42
|
+
autoAlignment;
|
|
43
|
+
|
|
44
|
+
const isDisabled = disabled || scale < minScale || (!sizeX && !sizeY);
|
|
45
|
+
|
|
46
|
+
if (isDisabled) return;
|
|
47
|
+
|
|
48
|
+
const targetState = handlePanToBounds(contextInstance);
|
|
49
|
+
|
|
50
|
+
if (targetState) {
|
|
51
|
+
animate(
|
|
52
|
+
contextInstance,
|
|
53
|
+
targetState,
|
|
54
|
+
customAnimationTime ?? animationTime,
|
|
55
|
+
animationType,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function handlePanning(
|
|
61
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
62
|
+
clientX: number,
|
|
63
|
+
clientY: number,
|
|
64
|
+
device: DeviceType.MOUSE | DeviceType.TOUCH,
|
|
65
|
+
): void {
|
|
66
|
+
const { startCoords, setup } = contextInstance;
|
|
67
|
+
const { sizeX, sizeY } = setup.autoAlignment;
|
|
68
|
+
|
|
69
|
+
if (!startCoords) return;
|
|
70
|
+
|
|
71
|
+
const { x, y } = getPanningClientPosition(contextInstance, clientX, clientY);
|
|
72
|
+
const paddingValueX = getPaddingValue(contextInstance, sizeX);
|
|
73
|
+
const paddingValueY = getPaddingValue(contextInstance, sizeY);
|
|
74
|
+
|
|
75
|
+
handleCalculateVelocity(contextInstance, { x, y }, device);
|
|
76
|
+
handleNewPosition(contextInstance, x, y, paddingValueX, paddingValueY);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function handlePanningEnd(
|
|
80
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
81
|
+
velocityDisabled: boolean,
|
|
82
|
+
): void {
|
|
83
|
+
if (contextInstance.isPanning) {
|
|
84
|
+
const { velocity, wrapperComponent, contentComponent } = contextInstance;
|
|
85
|
+
|
|
86
|
+
contextInstance.isPanning = false;
|
|
87
|
+
|
|
88
|
+
const { positionX, positionY, scale } = contextInstance.state;
|
|
89
|
+
const start = contextInstance.panStartPosition;
|
|
90
|
+
contextInstance.panStartPosition = null;
|
|
91
|
+
|
|
92
|
+
if (start) {
|
|
93
|
+
const dx = positionX - start.x;
|
|
94
|
+
const dy = positionY - start.y;
|
|
95
|
+
if (dx * dx + dy * dy <= 25) return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
contextInstance.isAnimating = false;
|
|
99
|
+
contextInstance.animation = null;
|
|
100
|
+
|
|
101
|
+
const wrapperWidth = wrapperComponent?.offsetWidth || 0;
|
|
102
|
+
const wrapperHeight = wrapperComponent?.offsetHeight || 0;
|
|
103
|
+
const contentWidth = (contentComponent?.offsetWidth || 0) * scale;
|
|
104
|
+
const contentHeight = (contentComponent?.offsetHeight || 0) * scale;
|
|
105
|
+
const isContentOverflowing =
|
|
106
|
+
!contextInstance.setup.limitToBounds ||
|
|
107
|
+
wrapperWidth < contentWidth ||
|
|
108
|
+
wrapperHeight < contentHeight;
|
|
109
|
+
|
|
110
|
+
const shouldAnimate =
|
|
111
|
+
!velocityDisabled &&
|
|
112
|
+
velocity &&
|
|
113
|
+
velocity.total > 0.1 &&
|
|
114
|
+
isContentOverflowing;
|
|
115
|
+
|
|
116
|
+
if (shouldAnimate) {
|
|
117
|
+
handleVelocityPanning(contextInstance);
|
|
118
|
+
} else {
|
|
119
|
+
handleAlignToBounds(contextInstance);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
import {
|
|
3
|
+
PositionType,
|
|
4
|
+
ReactZoomPanPinchContext,
|
|
5
|
+
ReactZoomPanPinchState,
|
|
6
|
+
} from "../../models";
|
|
7
|
+
import { isExcludedNode } from "../../utils";
|
|
8
|
+
import { getMouseBoundedPosition } from "../bounds/bounds.utils";
|
|
9
|
+
import { handleCalculateZoomPositions } from "../zoom/zoom.utils";
|
|
10
|
+
|
|
11
|
+
export const isPanningStartAllowed = (
|
|
12
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
13
|
+
event: MouseEvent | TouchEvent,
|
|
14
|
+
): boolean => {
|
|
15
|
+
const { excluded } = contextInstance.setup.panning;
|
|
16
|
+
const { isInitialized, wrapperComponent } = contextInstance;
|
|
17
|
+
|
|
18
|
+
const target = event.target as HTMLElement;
|
|
19
|
+
const targetIsShadowDom = "shadowRoot" in target && "composedPath" in event;
|
|
20
|
+
const isWrapperChild = targetIsShadowDom
|
|
21
|
+
? event.composedPath().some((el) => {
|
|
22
|
+
if (!(el instanceof Element)) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return wrapperComponent?.contains(el);
|
|
27
|
+
})
|
|
28
|
+
: wrapperComponent?.contains(target);
|
|
29
|
+
|
|
30
|
+
const isAllowed = isInitialized && target && isWrapperChild;
|
|
31
|
+
|
|
32
|
+
if (!isAllowed) return false;
|
|
33
|
+
|
|
34
|
+
const isExcluded = isExcludedNode(target, excluded);
|
|
35
|
+
|
|
36
|
+
if (isExcluded) return false;
|
|
37
|
+
|
|
38
|
+
if (
|
|
39
|
+
target.getAttribute("draggable") === "true" ||
|
|
40
|
+
target.getAttribute("contenteditable") === "true" ||
|
|
41
|
+
target.isContentEditable
|
|
42
|
+
) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return true;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const isPanningAllowed = (
|
|
50
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
51
|
+
): boolean => {
|
|
52
|
+
const { isInitialized, isPanning, setup } = contextInstance;
|
|
53
|
+
const { disabled } = setup.panning;
|
|
54
|
+
|
|
55
|
+
const isAllowed = isInitialized && isPanning && !disabled;
|
|
56
|
+
|
|
57
|
+
if (!isAllowed) return false;
|
|
58
|
+
|
|
59
|
+
return true;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const handlePanningSetup = (
|
|
63
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
64
|
+
event: MouseEvent,
|
|
65
|
+
): void => {
|
|
66
|
+
const { positionX, positionY } = contextInstance.state;
|
|
67
|
+
|
|
68
|
+
contextInstance.isPanning = true;
|
|
69
|
+
|
|
70
|
+
// Panning with mouse
|
|
71
|
+
const x = event.clientX;
|
|
72
|
+
const y = event.clientY;
|
|
73
|
+
|
|
74
|
+
contextInstance.startCoords = { x: x - positionX, y: y - positionY };
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const handleTouchPanningSetup = (
|
|
78
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
79
|
+
event: TouchEvent,
|
|
80
|
+
): void => {
|
|
81
|
+
const { touches } = event;
|
|
82
|
+
const { positionX, positionY } = contextInstance.state;
|
|
83
|
+
|
|
84
|
+
contextInstance.isPanning = true;
|
|
85
|
+
|
|
86
|
+
// Panning with touch
|
|
87
|
+
const oneFingerTouch = touches.length === 1;
|
|
88
|
+
if (oneFingerTouch) {
|
|
89
|
+
const x = touches[0].clientX;
|
|
90
|
+
const y = touches[0].clientY;
|
|
91
|
+
contextInstance.startCoords = { x: x - positionX, y: y - positionY };
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
export function handlePanToBounds(
|
|
95
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
96
|
+
): Omit<ReactZoomPanPinchState, "previousScale"> | undefined {
|
|
97
|
+
const { positionX, positionY, scale } = contextInstance.state;
|
|
98
|
+
const { disabled, limitToBounds, centerZoomedOut } = contextInstance.setup;
|
|
99
|
+
const { wrapperComponent } = contextInstance;
|
|
100
|
+
|
|
101
|
+
if (disabled || !wrapperComponent || !contextInstance.bounds) return;
|
|
102
|
+
|
|
103
|
+
const { maxPositionX, minPositionX, maxPositionY, minPositionY } =
|
|
104
|
+
contextInstance.bounds;
|
|
105
|
+
|
|
106
|
+
const xChanged = positionX > maxPositionX || positionX < minPositionX;
|
|
107
|
+
const yChanged = positionY > maxPositionY || positionY < minPositionY;
|
|
108
|
+
|
|
109
|
+
const mousePosX =
|
|
110
|
+
positionX > maxPositionX
|
|
111
|
+
? wrapperComponent.offsetWidth
|
|
112
|
+
: contextInstance.setup.minPositionX || 0;
|
|
113
|
+
const mousePosY =
|
|
114
|
+
positionY > maxPositionY
|
|
115
|
+
? wrapperComponent.offsetHeight
|
|
116
|
+
: contextInstance.setup.minPositionY || 0;
|
|
117
|
+
|
|
118
|
+
const { x, y } = handleCalculateZoomPositions(
|
|
119
|
+
contextInstance,
|
|
120
|
+
mousePosX,
|
|
121
|
+
mousePosY,
|
|
122
|
+
scale,
|
|
123
|
+
contextInstance.bounds,
|
|
124
|
+
limitToBounds || centerZoomedOut,
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
scale,
|
|
129
|
+
positionX: xChanged ? x : positionX,
|
|
130
|
+
positionY: yChanged ? y : positionY,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function handlePaddingAnimation(
|
|
135
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
136
|
+
positionX: number,
|
|
137
|
+
positionY: number,
|
|
138
|
+
): void {
|
|
139
|
+
const { scale } = contextInstance.state;
|
|
140
|
+
const { sizeX, sizeY } = contextInstance.setup.autoAlignment;
|
|
141
|
+
|
|
142
|
+
if (!sizeX && !sizeY) return;
|
|
143
|
+
|
|
144
|
+
contextInstance.setState(scale, positionX, positionY);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function handleNewPosition(
|
|
148
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
149
|
+
newPositionX: number,
|
|
150
|
+
newPositionY: number,
|
|
151
|
+
paddingValueX: number,
|
|
152
|
+
paddingValueY: number,
|
|
153
|
+
): void {
|
|
154
|
+
const { limitToBounds } = contextInstance.setup;
|
|
155
|
+
const { wrapperComponent, bounds } = contextInstance;
|
|
156
|
+
const { scale, positionX, positionY } = contextInstance.state;
|
|
157
|
+
|
|
158
|
+
if (
|
|
159
|
+
wrapperComponent === null ||
|
|
160
|
+
bounds === null ||
|
|
161
|
+
(newPositionX === positionX && newPositionY === positionY)
|
|
162
|
+
) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const { x, y } = getMouseBoundedPosition(
|
|
167
|
+
newPositionX,
|
|
168
|
+
newPositionY,
|
|
169
|
+
bounds,
|
|
170
|
+
limitToBounds,
|
|
171
|
+
paddingValueX,
|
|
172
|
+
paddingValueY,
|
|
173
|
+
wrapperComponent,
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
contextInstance.setState(scale, x, y);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export const getPanningClientPosition = (
|
|
180
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
181
|
+
clientX: number,
|
|
182
|
+
clientY: number,
|
|
183
|
+
): PositionType => {
|
|
184
|
+
const { startCoords, state } = contextInstance;
|
|
185
|
+
const { panning } = contextInstance.setup;
|
|
186
|
+
const { lockAxisX, lockAxisY } = panning;
|
|
187
|
+
const { positionX, positionY } = state;
|
|
188
|
+
|
|
189
|
+
if (!startCoords) {
|
|
190
|
+
return { x: positionX, y: positionY };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const mouseX = clientX - startCoords.x;
|
|
194
|
+
const mouseY = clientY - startCoords.y;
|
|
195
|
+
const newPositionX = lockAxisX ? positionX : mouseX;
|
|
196
|
+
const newPositionY = lockAxisY ? positionY : mouseY;
|
|
197
|
+
|
|
198
|
+
return { x: newPositionX, y: newPositionY };
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
export const getPaddingValue = (
|
|
202
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
203
|
+
size: number,
|
|
204
|
+
explicitScale?: number,
|
|
205
|
+
): number => {
|
|
206
|
+
const { setup, state } = contextInstance;
|
|
207
|
+
const { minScale, disablePadding, centerZoomedOut } = setup;
|
|
208
|
+
|
|
209
|
+
const scale = explicitScale ?? state.scale;
|
|
210
|
+
|
|
211
|
+
if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
|
|
212
|
+
return size;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return 0;
|
|
216
|
+
};
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
import { DeviceType, PositionType } from "../../models";
|
|
3
|
+
import { ReactZoomPanPinchContext } from "../../models/context.model";
|
|
4
|
+
import { getContext } from "../../utils";
|
|
5
|
+
import { animations } from "../animations/animations.constants";
|
|
6
|
+
import { handleSetupAnimation } from "../animations/animations.utils";
|
|
7
|
+
import { getPaddingValue } from "./panning.utils";
|
|
8
|
+
import {
|
|
9
|
+
getVelocityPosition,
|
|
10
|
+
getVelocityMoveTime,
|
|
11
|
+
isVelocityAllowed,
|
|
12
|
+
isVelocityCalculationAllowed,
|
|
13
|
+
} from "./velocity.utils";
|
|
14
|
+
|
|
15
|
+
export function getSizeMultiplier(wrapperComponent: HTMLDivElement): number {
|
|
16
|
+
const defaultMultiplier = 1;
|
|
17
|
+
const value = wrapperComponent.offsetWidth / window.innerWidth;
|
|
18
|
+
|
|
19
|
+
if (Number.isNaN(value)) {
|
|
20
|
+
return defaultMultiplier;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return Math.min(defaultMultiplier, value);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const getMinMaxVelocity = (
|
|
27
|
+
velocity: number,
|
|
28
|
+
maxStrength: number,
|
|
29
|
+
sensitivity: number,
|
|
30
|
+
) => {
|
|
31
|
+
const defaultMultiplier = 0;
|
|
32
|
+
const value = velocity * sensitivity;
|
|
33
|
+
if (Number.isNaN(value)) {
|
|
34
|
+
return defaultMultiplier;
|
|
35
|
+
}
|
|
36
|
+
if (velocity < 0) {
|
|
37
|
+
return Math.max(value, -maxStrength);
|
|
38
|
+
}
|
|
39
|
+
return Math.min(value, maxStrength);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export function handleCalculateVelocity(
|
|
43
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
44
|
+
position: PositionType,
|
|
45
|
+
device: DeviceType.MOUSE | DeviceType.TOUCH,
|
|
46
|
+
): void {
|
|
47
|
+
const isAllowed = isVelocityCalculationAllowed(contextInstance);
|
|
48
|
+
|
|
49
|
+
if (!isAllowed) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const { lastMousePosition, velocityTime, setup } = contextInstance;
|
|
54
|
+
const { wrapperComponent } = contextInstance;
|
|
55
|
+
const {
|
|
56
|
+
maxStrengthMouse,
|
|
57
|
+
maxStrengthTouch,
|
|
58
|
+
sensitivityTouch,
|
|
59
|
+
sensitivityMouse,
|
|
60
|
+
} = setup.velocityAnimation;
|
|
61
|
+
|
|
62
|
+
const now = Date.now();
|
|
63
|
+
if (lastMousePosition && velocityTime && wrapperComponent) {
|
|
64
|
+
const sizeMultiplier = getSizeMultiplier(wrapperComponent);
|
|
65
|
+
const sensitivity = {
|
|
66
|
+
[DeviceType.TOUCH]: sensitivityTouch,
|
|
67
|
+
[DeviceType.MOUSE]: sensitivityMouse,
|
|
68
|
+
}[device];
|
|
69
|
+
const maxStrength = {
|
|
70
|
+
[DeviceType.TOUCH]: maxStrengthTouch,
|
|
71
|
+
[DeviceType.MOUSE]: maxStrengthMouse,
|
|
72
|
+
}[device];
|
|
73
|
+
|
|
74
|
+
const distanceX = position.x - lastMousePosition.x;
|
|
75
|
+
const distanceY = position.y - lastMousePosition.y;
|
|
76
|
+
|
|
77
|
+
const velocityX = getMinMaxVelocity(
|
|
78
|
+
distanceX / sizeMultiplier,
|
|
79
|
+
maxStrength,
|
|
80
|
+
sensitivity,
|
|
81
|
+
);
|
|
82
|
+
const velocityY = getMinMaxVelocity(
|
|
83
|
+
distanceY / sizeMultiplier,
|
|
84
|
+
maxStrength,
|
|
85
|
+
sensitivity,
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
const interval = now - velocityTime;
|
|
89
|
+
const speed = distanceX * distanceX + distanceY * distanceY;
|
|
90
|
+
const velocity = getMinMaxVelocity(
|
|
91
|
+
Math.sqrt(speed) / interval,
|
|
92
|
+
maxStrength,
|
|
93
|
+
sensitivity,
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
contextInstance.velocity = { velocityX, velocityY, total: velocity };
|
|
97
|
+
}
|
|
98
|
+
contextInstance.lastMousePosition = position;
|
|
99
|
+
contextInstance.velocityTime = now;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function handleVelocityPanning(
|
|
103
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
104
|
+
): void {
|
|
105
|
+
const { velocity, bounds, setup, wrapperComponent } = contextInstance;
|
|
106
|
+
const isAllowed = isVelocityAllowed(contextInstance);
|
|
107
|
+
|
|
108
|
+
if (!isAllowed || !velocity || !bounds || !wrapperComponent) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const { velocityX, velocityY, total } = velocity;
|
|
113
|
+
const { maxPositionX, minPositionX, maxPositionY, minPositionY } = bounds;
|
|
114
|
+
const { limitToBounds, autoAlignment } = setup;
|
|
115
|
+
const { zoomAnimation, panning } = setup;
|
|
116
|
+
const { lockAxisY, lockAxisX } = panning;
|
|
117
|
+
const { animationType } = zoomAnimation;
|
|
118
|
+
const { sizeX, sizeY, velocityAlignmentTime } = autoAlignment;
|
|
119
|
+
|
|
120
|
+
const alignAnimationTime = velocityAlignmentTime;
|
|
121
|
+
const moveAnimationTime = getVelocityMoveTime(contextInstance, total);
|
|
122
|
+
const finalAnimationTime = Math.max(moveAnimationTime, alignAnimationTime);
|
|
123
|
+
|
|
124
|
+
const paddingValueX = getPaddingValue(contextInstance, sizeX);
|
|
125
|
+
const paddingValueY = getPaddingValue(contextInstance, sizeY);
|
|
126
|
+
const paddingX = (paddingValueX * wrapperComponent.offsetWidth) / 100;
|
|
127
|
+
const paddingY = (paddingValueY * wrapperComponent.offsetHeight) / 100;
|
|
128
|
+
const maxTargetX = maxPositionX + paddingX;
|
|
129
|
+
const minTargetX = minPositionX - paddingX;
|
|
130
|
+
|
|
131
|
+
const maxTargetY = maxPositionY + paddingY;
|
|
132
|
+
const minTargetY = minPositionY - paddingY;
|
|
133
|
+
|
|
134
|
+
const startState = contextInstance.state;
|
|
135
|
+
|
|
136
|
+
const startTime = new Date().getTime();
|
|
137
|
+
handleSetupAnimation(
|
|
138
|
+
contextInstance,
|
|
139
|
+
animationType,
|
|
140
|
+
finalAnimationTime,
|
|
141
|
+
(step: number) => {
|
|
142
|
+
const { scale, positionX, positionY } = contextInstance.state;
|
|
143
|
+
const frameTime = new Date().getTime() - startTime;
|
|
144
|
+
const animationProgress = frameTime / alignAnimationTime;
|
|
145
|
+
const alignAnimation = animations[autoAlignment.animationType];
|
|
146
|
+
const alignStep = 1 - alignAnimation(Math.min(1, animationProgress));
|
|
147
|
+
|
|
148
|
+
const customStep = 1 - step;
|
|
149
|
+
|
|
150
|
+
const newPositionX = positionX + velocityX * customStep;
|
|
151
|
+
const newPositionY = positionY + velocityY * customStep;
|
|
152
|
+
|
|
153
|
+
const currentPositionX = getVelocityPosition(
|
|
154
|
+
newPositionX,
|
|
155
|
+
startState.positionX,
|
|
156
|
+
positionX,
|
|
157
|
+
lockAxisX,
|
|
158
|
+
limitToBounds,
|
|
159
|
+
minPositionX,
|
|
160
|
+
maxPositionX,
|
|
161
|
+
minTargetX,
|
|
162
|
+
maxTargetX,
|
|
163
|
+
alignStep,
|
|
164
|
+
);
|
|
165
|
+
const currentPositionY = getVelocityPosition(
|
|
166
|
+
newPositionY,
|
|
167
|
+
startState.positionY,
|
|
168
|
+
positionY,
|
|
169
|
+
lockAxisY,
|
|
170
|
+
limitToBounds,
|
|
171
|
+
minPositionY,
|
|
172
|
+
maxPositionY,
|
|
173
|
+
minTargetY,
|
|
174
|
+
maxTargetY,
|
|
175
|
+
alignStep,
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
if (positionX !== newPositionX || positionY !== newPositionY) {
|
|
179
|
+
contextInstance.setState(scale, currentPositionX, currentPositionY);
|
|
180
|
+
const { onPanning } = contextInstance.props;
|
|
181
|
+
if (onPanning) {
|
|
182
|
+
onPanning(getContext(contextInstance), {} as MouseEvent);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
);
|
|
187
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
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, wrapperComponent, contentComponent } = contextInstance;
|
|
8
|
+
const { disabled, velocityAnimation, limitToBounds } = contextInstance.setup;
|
|
9
|
+
const { scale } = contextInstance.state;
|
|
10
|
+
const { disabled: disabledVelocity } = velocityAnimation;
|
|
11
|
+
|
|
12
|
+
if (disabledVelocity || disabled || !mounted) return false;
|
|
13
|
+
if (!wrapperComponent || !contentComponent) return false;
|
|
14
|
+
|
|
15
|
+
if (!limitToBounds) return true;
|
|
16
|
+
|
|
17
|
+
const contentOverflows =
|
|
18
|
+
wrapperComponent.offsetWidth < contentComponent.offsetWidth * scale ||
|
|
19
|
+
wrapperComponent.offsetHeight < contentComponent.offsetHeight * scale;
|
|
20
|
+
|
|
21
|
+
return contentOverflows;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const isVelocityAllowed = (
|
|
25
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
26
|
+
): boolean => {
|
|
27
|
+
const { mounted, velocity, bounds } = contextInstance;
|
|
28
|
+
const { disabled, velocityAnimation } = contextInstance.setup;
|
|
29
|
+
const { disabled: disabledVelocity } = velocityAnimation;
|
|
30
|
+
|
|
31
|
+
const isAllowed = !disabledVelocity && !disabled && mounted;
|
|
32
|
+
|
|
33
|
+
if (!isAllowed) return false;
|
|
34
|
+
if (!velocity || !bounds) return false;
|
|
35
|
+
|
|
36
|
+
return true;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export function getVelocityMoveTime(
|
|
40
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
41
|
+
velocity: number,
|
|
42
|
+
): number {
|
|
43
|
+
const { velocityAnimation } = contextInstance.setup;
|
|
44
|
+
const { animationTime, maxAnimationTime, inertia } = velocityAnimation;
|
|
45
|
+
|
|
46
|
+
return Math.min(
|
|
47
|
+
animationTime * Math.max(1, Math.abs(velocity / inertia)),
|
|
48
|
+
maxAnimationTime,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function getVelocityPosition(
|
|
53
|
+
newPosition: number,
|
|
54
|
+
startPosition: number,
|
|
55
|
+
currentPosition: number,
|
|
56
|
+
isLocked: boolean,
|
|
57
|
+
limitToBounds: boolean,
|
|
58
|
+
minPosition: number,
|
|
59
|
+
maxPosition: number,
|
|
60
|
+
minTarget: number,
|
|
61
|
+
maxTarget: number,
|
|
62
|
+
step: number,
|
|
63
|
+
): number {
|
|
64
|
+
if (limitToBounds) {
|
|
65
|
+
if (startPosition > maxPosition && currentPosition > maxPosition) {
|
|
66
|
+
const calculatedPosition =
|
|
67
|
+
maxPosition + (newPosition - maxPosition) * step;
|
|
68
|
+
|
|
69
|
+
if (calculatedPosition > maxTarget) return maxTarget;
|
|
70
|
+
if (calculatedPosition < maxPosition) return maxPosition;
|
|
71
|
+
return calculatedPosition;
|
|
72
|
+
}
|
|
73
|
+
if (startPosition < minPosition && currentPosition < minPosition) {
|
|
74
|
+
const calculatedPosition =
|
|
75
|
+
minPosition + (newPosition - minPosition) * step;
|
|
76
|
+
if (calculatedPosition < minTarget) return minTarget;
|
|
77
|
+
if (calculatedPosition > minPosition) return minPosition;
|
|
78
|
+
return calculatedPosition;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (isLocked) return startPosition;
|
|
82
|
+
return boundLimiter(newPosition, minPosition, maxPosition, limitToBounds);
|
|
83
|
+
}
|