react-zoom-pan-pinch 3.3.0 → 3.4.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 +38 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.esm.js +38 -2
- package/dist/index.esm.js.map +1 -1
- package/package.json +4 -1
- package/.eslintignore +0 -6
- package/.github/CODEOWNERS +0 -36
- package/.github/FUNDING.yml +0 -13
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- package/.github/workflows/docs.yml +0 -34
- package/.github/workflows/release.yml +0 -46
- package/.github/workflows/tests.yml +0 -42
- package/.prettierrc +0 -10
- package/.releaserc.json +0 -63
- package/.storybook/main.js +0 -23
- package/.storybook/preview-head.html +0 -5
- package/.storybook/preview.js +0 -11
- package/.storybook/theme-overrides.css +0 -49
- package/CHANGELOG.md +0 -26
- package/Code_of_Conduct.md +0 -61
- package/Contributing.md +0 -236
- package/__tests__/features/props.spec.tsx +0 -11
- package/__tests__/features/rendering.spec.tsx +0 -36
- package/__tests__/shared/render.shared.tsx +0 -41
- package/__tests__/utils/controls.utils.tsx +0 -25
- package/__tests__/utils/example.tsx +0 -81
- package/commitlint.config.js +0 -1
- package/dist/__tests__/features/props.spec.d.ts +0 -1
- package/dist/__tests__/features/rendering.spec.d.ts +0 -1
- package/dist/__tests__/shared/render.shared.d.ts +0 -11
- package/dist/__tests__/utils/controls.utils.d.ts +0 -3
- package/dist/__tests__/utils/example.d.ts +0 -6
- package/dist/src/components/index.d.ts +0 -4
- package/dist/src/components/keep-scale/keep-scale.d.ts +0 -2
- package/dist/src/components/mini-map/mini-map.d.ts +0 -8
- package/dist/src/components/mini-map/use-resize.hook.d.ts +0 -8
- package/dist/src/components/transform-component/transform-component.d.ts +0 -12
- package/dist/src/components/transform-wrapper/transform-wrapper.d.ts +0 -5
- package/dist/src/constants/state.constants.d.ts +0 -3
- package/dist/src/core/animations/animations.constants.d.ts +0 -16
- package/dist/src/core/animations/animations.utils.d.ts +0 -4
- package/dist/src/core/bounds/bounds.types.d.ts +0 -8
- package/dist/src/core/bounds/bounds.utils.d.ts +0 -17
- package/dist/src/core/double-click/double-click.logic.d.ts +0 -5
- package/dist/src/core/handlers/handlers.logic.d.ts +0 -8
- package/dist/src/core/handlers/handlers.utils.d.ts +0 -14
- package/dist/src/core/instance.core.d.ts +0 -77
- package/dist/src/core/pan/panning.logic.d.ts +0 -5
- package/dist/src/core/pan/panning.utils.d.ts +0 -10
- package/dist/src/core/pan/velocity.logic.d.ts +0 -5
- package/dist/src/core/pan/velocity.utils.d.ts +0 -5
- package/dist/src/core/pinch/pinch.logic.d.ts +0 -4
- package/dist/src/core/pinch/pinch.utils.d.ts +0 -6
- package/dist/src/core/wheel/wheel.logic.d.ts +0 -4
- package/dist/src/core/wheel/wheel.utils.d.ts +0 -7
- package/dist/src/core/zoom/zoom.logic.d.ts +0 -3
- package/dist/src/core/zoom/zoom.utils.d.ts +0 -3
- package/dist/src/hooks/index.d.ts +0 -4
- package/dist/src/hooks/use-controls.d.ts +0 -2
- package/dist/src/hooks/use-transform-context.d.ts +0 -1
- package/dist/src/hooks/use-transform-effect.d.ts +0 -2
- package/dist/src/hooks/use-transform-init.d.ts +0 -2
- package/dist/src/index.d.ts +0 -5
- package/dist/src/models/calculations.model.d.ts +0 -25
- package/dist/src/models/context.model.d.ts +0 -127
- package/dist/src/models/helpers.model.d.ts +0 -5
- package/dist/src/models/index.d.ts +0 -3
- package/dist/src/stories/docs/handlers.d.ts +0 -12
- package/dist/src/stories/docs/props.d.ts +0 -14
- package/dist/src/stories/examples/content-rerendering/example.d.ts +0 -2
- package/dist/src/stories/examples/image-responsive/example.d.ts +0 -2
- package/dist/src/stories/examples/keep-scale/example.d.ts +0 -2
- package/dist/src/stories/examples/mini-map/example.d.ts +0 -1
- package/dist/src/stories/examples/stress/example.d.ts +0 -2
- package/dist/src/stories/examples/svg-zoom-to-element/example.d.ts +0 -2
- package/dist/src/stories/examples/zoom-to-element/example.d.ts +0 -2
- package/dist/src/stories/types/args.types.d.ts +0 -280
- package/dist/src/stories/utils/args.utils.d.ts +0 -3
- package/dist/src/stories/utils/code.utils.d.ts +0 -6
- package/dist/src/stories/utils/controls.utils.d.ts +0 -3
- package/dist/src/stories/utils/index.d.ts +0 -3
- package/dist/src/utils/calculations.utils.d.ts +0 -11
- package/dist/src/utils/callback.utils.d.ts +0 -2
- package/dist/src/utils/context.utils.d.ts +0 -4
- package/dist/src/utils/event.utils.d.ts +0 -1
- package/dist/src/utils/helpers.utils.d.ts +0 -2
- package/dist/src/utils/index.d.ts +0 -8
- package/dist/src/utils/ref.utils.d.ts +0 -2
- package/dist/src/utils/state.utils.d.ts +0 -3
- package/dist/src/utils/styles.utils.d.ts +0 -4
- package/jest.config.ts +0 -45
- package/rollup.config.js +0 -51
- package/src/.eslintrc.json +0 -90
- package/src/components/index.ts +0 -4
- package/src/components/keep-scale/keep-scale.tsx +0 -28
- package/src/components/mini-map/mini-map.tsx +0 -188
- package/src/components/mini-map/use-resize.hook.ts +0 -69
- package/src/components/transform-component/transform-component.module.css +0 -30
- package/src/components/transform-component/transform-component.tsx +0 -61
- package/src/components/transform-wrapper/transform-wrapper.tsx +0 -39
- package/src/constants/state.constants.ts +0 -77
- package/src/core/animations/animations.constants.ts +0 -78
- package/src/core/animations/animations.utils.ts +0 -114
- package/src/core/bounds/bounds.types.ts +0 -8
- package/src/core/bounds/bounds.utils.ts +0 -187
- package/src/core/double-click/double-click.logic.ts +0 -116
- package/src/core/handlers/handlers.logic.ts +0 -115
- package/src/core/handlers/handlers.utils.ts +0 -195
- package/src/core/instance.core.ts +0 -503
- package/src/core/pan/panning.logic.ts +0 -100
- package/src/core/pan/panning.utils.ts +0 -196
- package/src/core/pan/velocity.logic.ts +0 -149
- package/src/core/pan/velocity.utils.ts +0 -79
- package/src/core/pinch/pinch.logic.ts +0 -81
- package/src/core/pinch/pinch.utils.ts +0 -88
- package/src/core/wheel/wheel.logic.ts +0 -121
- package/src/core/wheel/wheel.utils.ts +0 -122
- package/src/core/zoom/zoom.logic.ts +0 -69
- package/src/core/zoom/zoom.utils.ts +0 -58
- package/src/hooks/index.ts +0 -4
- package/src/hooks/use-controls.tsx +0 -9
- package/src/hooks/use-transform-context.tsx +0 -13
- package/src/hooks/use-transform-effect.tsx +0 -22
- package/src/hooks/use-transform-init.tsx +0 -27
- package/src/index.ts +0 -6
- package/src/models/calculations.model.ts +0 -26
- package/src/models/context.model.ts +0 -199
- package/src/models/helpers.model.ts +0 -15
- package/src/models/index.ts +0 -3
- package/src/react-app-env.d.ts +0 -1
- 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 +0 -1
- package/src/stories/assets/small-image.jpg +0 -0
- package/src/stories/assets/svg-example.svg +0 -14
- package/src/stories/docs/Introduction.stories.mdx +0 -174
- package/src/stories/docs/handlers.stories.mdx +0 -48
- package/src/stories/docs/handlers.tsx +0 -107
- package/src/stories/docs/props.stories.mdx +0 -92
- package/src/stories/docs/props.tsx +0 -526
- package/src/stories/examples/content/content.stories.mdx +0 -121
- package/src/stories/examples/content-rerendering/content-updating.stories.mdx +0 -45
- package/src/stories/examples/content-rerendering/example.tsx +0 -73
- package/src/stories/examples/controls/controls.stories.mdx +0 -71
- package/src/stories/examples/image-big/image-big.stories.mdx +0 -43
- package/src/stories/examples/image-medium/image-medium.stories.mdx +0 -41
- package/src/stories/examples/image-responsive/example.tsx +0 -103
- package/src/stories/examples/image-responsive/image-responsive.stories.mdx +0 -32
- package/src/stories/examples/image-small/image-small.stories.mdx +0 -41
- package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +0 -46
- package/src/stories/examples/keep-scale/example.tsx +0 -92
- package/src/stories/examples/keep-scale/keep-scale.stories.mdx +0 -45
- package/src/stories/examples/mini-map/example.tsx +0 -54
- package/src/stories/examples/mini-map/mini-map.stories.mdx +0 -33
- package/src/stories/examples/stress/example.tsx +0 -23
- package/src/stories/examples/stress/stress-test.stories.mdx +0 -28
- package/src/stories/examples/svg-zoom-to-element/creativity.svg +0 -143
- package/src/stories/examples/svg-zoom-to-element/example.tsx +0 -57
- package/src/stories/examples/svg-zoom-to-element/svg-zoom-to-element.stories.mdx +0 -28
- package/src/stories/examples/zoom-to-element/example.tsx +0 -94
- package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +0 -28
- package/src/stories/hooks/use-controls.stories.mdx +0 -24
- package/src/stories/hooks/use-transform-context.stories.mdx +0 -15
- package/src/stories/hooks/use-transform-effect.stories.mdx +0 -29
- package/src/stories/hooks/use-transform-init.stories.mdx +0 -25
- package/src/stories/types/args.types.ts +0 -211
- package/src/stories/utils/args.utils.ts +0 -34
- package/src/stories/utils/code.utils.tsx +0 -24
- package/src/stories/utils/controls.utils.tsx +0 -43
- package/src/stories/utils/index.ts +0 -3
- package/src/stories/utils/styles.module.css +0 -34
- package/src/styles.css +0 -8
- package/src/typings.d.ts +0 -10
- package/src/utils/calculations.utils.ts +0 -16
- package/src/utils/callback.utils.ts +0 -11
- package/src/utils/context.utils.ts +0 -48
- package/src/utils/event.utils.ts +0 -22
- package/src/utils/helpers.utils.ts +0 -27
- package/src/utils/index.ts +0 -8
- package/src/utils/ref.utils.ts +0 -16
- package/src/utils/state.utils.ts +0 -40
- package/src/utils/styles.utils.ts +0 -44
- package/tsconfig.json +0 -33
|
@@ -1,196 +0,0 @@
|
|
|
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 isWrapperChild = wrapperComponent?.contains(target);
|
|
20
|
-
const isAllowed = isInitialized && target && isWrapperChild;
|
|
21
|
-
|
|
22
|
-
if (!isAllowed) return false;
|
|
23
|
-
|
|
24
|
-
const isExcluded = isExcludedNode(target, excluded);
|
|
25
|
-
|
|
26
|
-
if (isExcluded) return false;
|
|
27
|
-
|
|
28
|
-
return true;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export const isPanningAllowed = (
|
|
32
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
33
|
-
): boolean => {
|
|
34
|
-
const { isInitialized, isPanning, setup } = contextInstance;
|
|
35
|
-
const { disabled } = setup.panning;
|
|
36
|
-
|
|
37
|
-
const isAllowed = isInitialized && isPanning && !disabled;
|
|
38
|
-
|
|
39
|
-
if (!isAllowed) return false;
|
|
40
|
-
|
|
41
|
-
return true;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export const handlePanningSetup = (
|
|
45
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
46
|
-
event: MouseEvent,
|
|
47
|
-
): void => {
|
|
48
|
-
const { positionX, positionY } = contextInstance.transformState;
|
|
49
|
-
|
|
50
|
-
contextInstance.isPanning = true;
|
|
51
|
-
|
|
52
|
-
// Panning with mouse
|
|
53
|
-
const x = event.clientX;
|
|
54
|
-
const y = event.clientY;
|
|
55
|
-
|
|
56
|
-
contextInstance.startCoords = { x: x - positionX, y: y - positionY };
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export const handleTouchPanningSetup = (
|
|
60
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
61
|
-
event: TouchEvent,
|
|
62
|
-
): void => {
|
|
63
|
-
const { touches } = event;
|
|
64
|
-
const { positionX, positionY } = contextInstance.transformState;
|
|
65
|
-
|
|
66
|
-
contextInstance.isPanning = true;
|
|
67
|
-
|
|
68
|
-
// Panning with touch
|
|
69
|
-
const oneFingerTouch = touches.length === 1;
|
|
70
|
-
if (oneFingerTouch) {
|
|
71
|
-
const x = touches[0].clientX;
|
|
72
|
-
const y = touches[0].clientY;
|
|
73
|
-
contextInstance.startCoords = { x: x - positionX, y: y - positionY };
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
export function handlePanToBounds(
|
|
77
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
78
|
-
): Omit<ReactZoomPanPinchState, "previousScale"> | undefined {
|
|
79
|
-
const { positionX, positionY, scale } = contextInstance.transformState;
|
|
80
|
-
const { disabled, limitToBounds, centerZoomedOut } = contextInstance.setup;
|
|
81
|
-
const { wrapperComponent } = contextInstance;
|
|
82
|
-
|
|
83
|
-
if (disabled || !wrapperComponent || !contextInstance.bounds) return;
|
|
84
|
-
|
|
85
|
-
const { maxPositionX, minPositionX, maxPositionY, minPositionY } =
|
|
86
|
-
contextInstance.bounds;
|
|
87
|
-
|
|
88
|
-
const xChanged = positionX > maxPositionX || positionX < minPositionX;
|
|
89
|
-
const yChanged = positionY > maxPositionY || positionY < minPositionY;
|
|
90
|
-
|
|
91
|
-
const mousePosX =
|
|
92
|
-
positionX > maxPositionX
|
|
93
|
-
? wrapperComponent.offsetWidth
|
|
94
|
-
: contextInstance.setup.minPositionX || 0;
|
|
95
|
-
const mousePosY =
|
|
96
|
-
positionY > maxPositionY
|
|
97
|
-
? wrapperComponent.offsetHeight
|
|
98
|
-
: contextInstance.setup.minPositionY || 0;
|
|
99
|
-
|
|
100
|
-
const { x, y } = handleCalculateZoomPositions(
|
|
101
|
-
contextInstance,
|
|
102
|
-
mousePosX,
|
|
103
|
-
mousePosY,
|
|
104
|
-
scale,
|
|
105
|
-
contextInstance.bounds,
|
|
106
|
-
limitToBounds || centerZoomedOut,
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
return {
|
|
110
|
-
scale,
|
|
111
|
-
positionX: xChanged ? x : positionX,
|
|
112
|
-
positionY: yChanged ? y : positionY,
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function handlePaddingAnimation(
|
|
117
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
118
|
-
positionX: number,
|
|
119
|
-
positionY: number,
|
|
120
|
-
): void {
|
|
121
|
-
const { scale } = contextInstance.transformState;
|
|
122
|
-
const { sizeX, sizeY } = contextInstance.setup.alignmentAnimation;
|
|
123
|
-
|
|
124
|
-
if (!sizeX && !sizeY) return;
|
|
125
|
-
|
|
126
|
-
contextInstance.setTransformState(scale, positionX, positionY);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export function handleNewPosition(
|
|
130
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
131
|
-
newPositionX: number,
|
|
132
|
-
newPositionY: number,
|
|
133
|
-
paddingValueX: number,
|
|
134
|
-
paddingValueY: number,
|
|
135
|
-
): void {
|
|
136
|
-
const { limitToBounds } = contextInstance.setup;
|
|
137
|
-
const { wrapperComponent, bounds } = contextInstance;
|
|
138
|
-
const { scale, positionX, positionY } = contextInstance.transformState;
|
|
139
|
-
|
|
140
|
-
if (
|
|
141
|
-
wrapperComponent === null ||
|
|
142
|
-
bounds === null ||
|
|
143
|
-
(newPositionX === positionX && newPositionY === positionY)
|
|
144
|
-
) {
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const { x, y } = getMouseBoundedPosition(
|
|
149
|
-
newPositionX,
|
|
150
|
-
newPositionY,
|
|
151
|
-
bounds,
|
|
152
|
-
limitToBounds,
|
|
153
|
-
paddingValueX,
|
|
154
|
-
paddingValueY,
|
|
155
|
-
wrapperComponent,
|
|
156
|
-
);
|
|
157
|
-
|
|
158
|
-
contextInstance.setTransformState(scale, x, y);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export const getPanningClientPosition = (
|
|
162
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
163
|
-
clientX: number,
|
|
164
|
-
clientY: number,
|
|
165
|
-
): PositionType => {
|
|
166
|
-
const { startCoords, transformState } = contextInstance;
|
|
167
|
-
const { panning } = contextInstance.setup;
|
|
168
|
-
const { lockAxisX, lockAxisY } = panning;
|
|
169
|
-
const { positionX, positionY } = transformState;
|
|
170
|
-
|
|
171
|
-
if (!startCoords) {
|
|
172
|
-
return { x: positionX, y: positionY };
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const mouseX = clientX - startCoords.x;
|
|
176
|
-
const mouseY = clientY - startCoords.y;
|
|
177
|
-
const newPositionX = lockAxisX ? positionX : mouseX;
|
|
178
|
-
const newPositionY = lockAxisY ? positionY : mouseY;
|
|
179
|
-
|
|
180
|
-
return { x: newPositionX, y: newPositionY };
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
export const getPaddingValue = (
|
|
184
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
185
|
-
size: number,
|
|
186
|
-
): number => {
|
|
187
|
-
const { setup, transformState } = contextInstance;
|
|
188
|
-
const { scale } = transformState;
|
|
189
|
-
const { minScale, disablePadding } = setup;
|
|
190
|
-
|
|
191
|
-
if (size > 0 && scale >= minScale && !disablePadding) {
|
|
192
|
-
return size;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return 0;
|
|
196
|
-
};
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-param-reassign */
|
|
2
|
-
import { PositionType } from "../../models";
|
|
3
|
-
import { ReactZoomPanPinchContext } from "../../models/context.model";
|
|
4
|
-
import { animations } from "../animations/animations.constants";
|
|
5
|
-
import { handleSetupAnimation } from "../animations/animations.utils";
|
|
6
|
-
import { getPaddingValue } from "./panning.utils";
|
|
7
|
-
import {
|
|
8
|
-
getVelocityPosition,
|
|
9
|
-
getVelocityMoveTime,
|
|
10
|
-
isVelocityAllowed,
|
|
11
|
-
isVelocityCalculationAllowed,
|
|
12
|
-
} from "./velocity.utils";
|
|
13
|
-
|
|
14
|
-
export function getSizeMultiplier(
|
|
15
|
-
wrapperComponent: HTMLDivElement,
|
|
16
|
-
equalToMove: boolean,
|
|
17
|
-
): number {
|
|
18
|
-
const defaultMultiplier = 1;
|
|
19
|
-
|
|
20
|
-
if (equalToMove) {
|
|
21
|
-
return Math.min(
|
|
22
|
-
defaultMultiplier,
|
|
23
|
-
wrapperComponent.offsetWidth / window.innerWidth,
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return defaultMultiplier;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function handleCalculateVelocity(
|
|
31
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
32
|
-
position: PositionType,
|
|
33
|
-
): void {
|
|
34
|
-
const isAllowed = isVelocityCalculationAllowed(contextInstance);
|
|
35
|
-
|
|
36
|
-
if (!isAllowed) {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const { lastMousePosition, velocityTime, setup } = contextInstance;
|
|
41
|
-
const { wrapperComponent } = contextInstance;
|
|
42
|
-
const { equalToMove } = setup.velocityAnimation;
|
|
43
|
-
|
|
44
|
-
const now = Date.now();
|
|
45
|
-
if (lastMousePosition && velocityTime && wrapperComponent) {
|
|
46
|
-
const sizeMultiplier = getSizeMultiplier(wrapperComponent, equalToMove);
|
|
47
|
-
|
|
48
|
-
const distanceX = position.x - lastMousePosition.x;
|
|
49
|
-
const distanceY = position.y - lastMousePosition.y;
|
|
50
|
-
|
|
51
|
-
const velocityX = distanceX / sizeMultiplier;
|
|
52
|
-
const velocityY = distanceY / sizeMultiplier;
|
|
53
|
-
|
|
54
|
-
const interval = now - velocityTime;
|
|
55
|
-
const speed = distanceX * distanceX + distanceY * distanceY;
|
|
56
|
-
const velocity = Math.sqrt(speed) / interval;
|
|
57
|
-
|
|
58
|
-
contextInstance.velocity = { velocityX, velocityY, total: velocity };
|
|
59
|
-
}
|
|
60
|
-
contextInstance.lastMousePosition = position;
|
|
61
|
-
contextInstance.velocityTime = now;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function handleVelocityPanning(
|
|
65
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
66
|
-
): void {
|
|
67
|
-
const { velocity, bounds, setup, wrapperComponent } = contextInstance;
|
|
68
|
-
const isAllowed = isVelocityAllowed(contextInstance);
|
|
69
|
-
|
|
70
|
-
if (!isAllowed || !velocity || !bounds || !wrapperComponent) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const { velocityX, velocityY, total } = velocity;
|
|
75
|
-
const { maxPositionX, minPositionX, maxPositionY, minPositionY } = bounds;
|
|
76
|
-
const { limitToBounds, alignmentAnimation } = setup;
|
|
77
|
-
const { zoomAnimation, panning } = setup;
|
|
78
|
-
const { lockAxisY, lockAxisX } = panning;
|
|
79
|
-
const { animationType } = zoomAnimation;
|
|
80
|
-
const { sizeX, sizeY, velocityAlignmentTime } = alignmentAnimation;
|
|
81
|
-
|
|
82
|
-
const alignAnimationTime = velocityAlignmentTime;
|
|
83
|
-
const moveAnimationTime = getVelocityMoveTime(contextInstance, total);
|
|
84
|
-
const finalAnimationTime = Math.max(moveAnimationTime, alignAnimationTime);
|
|
85
|
-
|
|
86
|
-
const paddingValueX = getPaddingValue(contextInstance, sizeX);
|
|
87
|
-
const paddingValueY = getPaddingValue(contextInstance, sizeY);
|
|
88
|
-
const paddingX = (paddingValueX * wrapperComponent.offsetWidth) / 100;
|
|
89
|
-
const paddingY = (paddingValueY * wrapperComponent.offsetHeight) / 100;
|
|
90
|
-
const maxTargetX = maxPositionX + paddingX;
|
|
91
|
-
const minTargetX = minPositionX - paddingX;
|
|
92
|
-
|
|
93
|
-
const maxTargetY = maxPositionY + paddingY;
|
|
94
|
-
const minTargetY = minPositionY - paddingY;
|
|
95
|
-
|
|
96
|
-
const startState = contextInstance.transformState;
|
|
97
|
-
|
|
98
|
-
const startTime = new Date().getTime();
|
|
99
|
-
handleSetupAnimation(
|
|
100
|
-
contextInstance,
|
|
101
|
-
animationType,
|
|
102
|
-
finalAnimationTime,
|
|
103
|
-
(step: number) => {
|
|
104
|
-
const { scale, positionX, positionY } = contextInstance.transformState;
|
|
105
|
-
const frameTime = new Date().getTime() - startTime;
|
|
106
|
-
const animationProgress = frameTime / alignAnimationTime;
|
|
107
|
-
const alignAnimation = animations[alignmentAnimation.animationType];
|
|
108
|
-
const alignStep = 1 - alignAnimation(Math.min(1, animationProgress));
|
|
109
|
-
|
|
110
|
-
const customStep = 1 - step;
|
|
111
|
-
|
|
112
|
-
const newPositionX = positionX + velocityX * customStep;
|
|
113
|
-
const newPositionY = positionY + velocityY * customStep;
|
|
114
|
-
|
|
115
|
-
const currentPositionX = getVelocityPosition(
|
|
116
|
-
newPositionX,
|
|
117
|
-
startState.positionX,
|
|
118
|
-
positionX,
|
|
119
|
-
lockAxisX,
|
|
120
|
-
limitToBounds,
|
|
121
|
-
minPositionX,
|
|
122
|
-
maxPositionX,
|
|
123
|
-
minTargetX,
|
|
124
|
-
maxTargetX,
|
|
125
|
-
alignStep,
|
|
126
|
-
);
|
|
127
|
-
const currentPositionY = getVelocityPosition(
|
|
128
|
-
newPositionY,
|
|
129
|
-
startState.positionY,
|
|
130
|
-
positionY,
|
|
131
|
-
lockAxisY,
|
|
132
|
-
limitToBounds,
|
|
133
|
-
minPositionY,
|
|
134
|
-
maxPositionY,
|
|
135
|
-
minTargetY,
|
|
136
|
-
maxTargetY,
|
|
137
|
-
alignStep,
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
if (positionX !== newPositionX || positionY !== newPositionY) {
|
|
141
|
-
contextInstance.setTransformState(
|
|
142
|
-
scale,
|
|
143
|
-
currentPositionX,
|
|
144
|
-
currentPositionY,
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
},
|
|
148
|
-
);
|
|
149
|
-
}
|
|
@@ -1,79 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,81 +0,0 @@
|
|
|
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 { handleCalculateBounds } from "../bounds/bounds.utils";
|
|
11
|
-
import { handleCalculateZoomPositions } from "../zoom/zoom.utils";
|
|
12
|
-
|
|
13
|
-
export const handlePinchStart = (
|
|
14
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
15
|
-
event: TouchEvent,
|
|
16
|
-
): void => {
|
|
17
|
-
const distance = getTouchDistance(event);
|
|
18
|
-
|
|
19
|
-
contextInstance.pinchStartDistance = distance;
|
|
20
|
-
contextInstance.lastDistance = distance;
|
|
21
|
-
contextInstance.pinchStartScale = contextInstance.transformState.scale;
|
|
22
|
-
contextInstance.isPanning = false;
|
|
23
|
-
|
|
24
|
-
handleCancelAnimation(contextInstance);
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export const handlePinchZoom = (
|
|
28
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
29
|
-
event: TouchEvent,
|
|
30
|
-
): void => {
|
|
31
|
-
const { contentComponent, pinchStartDistance } = contextInstance;
|
|
32
|
-
const { scale } = contextInstance.transformState;
|
|
33
|
-
const { limitToBounds, centerZoomedOut, zoomAnimation } =
|
|
34
|
-
contextInstance.setup;
|
|
35
|
-
const { disabled, size } = zoomAnimation;
|
|
36
|
-
|
|
37
|
-
// if one finger starts from outside of wrapper
|
|
38
|
-
if (pinchStartDistance === null || !contentComponent) return;
|
|
39
|
-
|
|
40
|
-
const midPoint = calculateTouchMidPoint(event, scale, contentComponent);
|
|
41
|
-
|
|
42
|
-
// if touches goes off of the wrapper element
|
|
43
|
-
if (!Number.isFinite(midPoint.x) || !Number.isFinite(midPoint.y)) return;
|
|
44
|
-
|
|
45
|
-
const currentDistance = getTouchDistance(event);
|
|
46
|
-
const newScale = calculatePinchZoom(contextInstance, currentDistance);
|
|
47
|
-
|
|
48
|
-
if (newScale === scale) return;
|
|
49
|
-
|
|
50
|
-
const bounds = handleCalculateBounds(contextInstance, newScale);
|
|
51
|
-
|
|
52
|
-
const isPaddingDisabled = disabled || size === 0 || centerZoomedOut;
|
|
53
|
-
const isLimitedToBounds = limitToBounds && isPaddingDisabled;
|
|
54
|
-
|
|
55
|
-
const { x, y } = handleCalculateZoomPositions(
|
|
56
|
-
contextInstance,
|
|
57
|
-
midPoint.x,
|
|
58
|
-
midPoint.y,
|
|
59
|
-
newScale,
|
|
60
|
-
bounds,
|
|
61
|
-
isLimitedToBounds,
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
contextInstance.pinchMidpoint = midPoint;
|
|
65
|
-
contextInstance.lastDistance = currentDistance;
|
|
66
|
-
|
|
67
|
-
contextInstance.setTransformState(newScale, x, y);
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
export const handlePinchStop = (
|
|
71
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
72
|
-
): void => {
|
|
73
|
-
const { pinchMidpoint } = contextInstance;
|
|
74
|
-
|
|
75
|
-
contextInstance.velocity = null;
|
|
76
|
-
contextInstance.lastDistance = null;
|
|
77
|
-
contextInstance.pinchMidpoint = null;
|
|
78
|
-
contextInstance.pinchStartScale = null;
|
|
79
|
-
contextInstance.pinchStartDistance = null;
|
|
80
|
-
handleAlignToScaleBounds(contextInstance, pinchMidpoint?.x, pinchMidpoint?.y);
|
|
81
|
-
};
|
|
@@ -1,88 +0,0 @@
|
|
|
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
|
-
(event.touches[0].pageX - event.touches[1].pageX) ** 2 +
|
|
58
|
-
(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
|
-
};
|