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,88 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LibrarySetup,
|
|
3
|
+
ReactZoomPanPinchContext,
|
|
4
|
+
} from "../models/context.model";
|
|
5
|
+
import {
|
|
6
|
+
ReactZoomPanPinchComponentHelpers,
|
|
7
|
+
ReactZoomPanPinchState,
|
|
8
|
+
} from "../models/context.model";
|
|
9
|
+
|
|
10
|
+
export const initialState: ReactZoomPanPinchState = {
|
|
11
|
+
previousScale: 1,
|
|
12
|
+
scale: 1,
|
|
13
|
+
positionX: 0,
|
|
14
|
+
positionY: 0,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const contextInitialState: ReactZoomPanPinchComponentHelpers &
|
|
18
|
+
ReactZoomPanPinchState & {
|
|
19
|
+
contextInstance: ReactZoomPanPinchContext | null;
|
|
20
|
+
} = {
|
|
21
|
+
...initialState,
|
|
22
|
+
setComponents: () => undefined,
|
|
23
|
+
contextInstance: null,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const initialSetup: LibrarySetup = {
|
|
27
|
+
disabled: false,
|
|
28
|
+
minPositionX: null,
|
|
29
|
+
maxPositionX: null,
|
|
30
|
+
minPositionY: null,
|
|
31
|
+
maxPositionY: null,
|
|
32
|
+
minScale: 1,
|
|
33
|
+
maxScale: 8,
|
|
34
|
+
limitToBounds: true,
|
|
35
|
+
centerZoomedOut: false,
|
|
36
|
+
centerOnInit: false,
|
|
37
|
+
disablePadding: false,
|
|
38
|
+
wheel: {
|
|
39
|
+
step: 0.2,
|
|
40
|
+
disabled: false,
|
|
41
|
+
wheelDisabled: false,
|
|
42
|
+
touchPadDisabled: false,
|
|
43
|
+
activationKeys: [],
|
|
44
|
+
excluded: [],
|
|
45
|
+
},
|
|
46
|
+
panning: {
|
|
47
|
+
disabled: false,
|
|
48
|
+
velocityDisabled: false,
|
|
49
|
+
lockAxisX: false,
|
|
50
|
+
lockAxisY: false,
|
|
51
|
+
activationKeys: [],
|
|
52
|
+
excluded: [],
|
|
53
|
+
},
|
|
54
|
+
pinch: {
|
|
55
|
+
step: 5,
|
|
56
|
+
disabled: false,
|
|
57
|
+
excluded: [],
|
|
58
|
+
},
|
|
59
|
+
doubleClick: {
|
|
60
|
+
disabled: false,
|
|
61
|
+
step: 0.7,
|
|
62
|
+
mode: "zoomIn",
|
|
63
|
+
animationType: "easeOut",
|
|
64
|
+
animationTime: 200,
|
|
65
|
+
excluded: [],
|
|
66
|
+
},
|
|
67
|
+
zoomAnimation: {
|
|
68
|
+
disabled: false,
|
|
69
|
+
size: 0.4,
|
|
70
|
+
animationTime: 200,
|
|
71
|
+
animationType: "easeOut",
|
|
72
|
+
},
|
|
73
|
+
alignmentAnimation: {
|
|
74
|
+
disabled: false,
|
|
75
|
+
sizeX: 100,
|
|
76
|
+
sizeY: 100,
|
|
77
|
+
animationTime: 200,
|
|
78
|
+
velocityAlignmentTime: 400,
|
|
79
|
+
animationType: "easeOut",
|
|
80
|
+
},
|
|
81
|
+
velocityAnimation: {
|
|
82
|
+
disabled: false,
|
|
83
|
+
sensitivity: 1,
|
|
84
|
+
animationTime: 400,
|
|
85
|
+
animationType: "easeOut",
|
|
86
|
+
equalToMove: true,
|
|
87
|
+
},
|
|
88
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Functions should return denominator of the target value, which is the next animation step.
|
|
3
|
+
* t is a value from 0 to 1, reflecting the percentage of animation status.
|
|
4
|
+
*/
|
|
5
|
+
const easeOut = function (t: number): number {
|
|
6
|
+
return -Math.cos(t * Math.PI) / 2 + 0.5;
|
|
7
|
+
};
|
|
8
|
+
// linear
|
|
9
|
+
const linear = function (t: number): number {
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
// accelerating from zero velocity
|
|
13
|
+
const easeInQuad = function (t: number): number {
|
|
14
|
+
return t * t;
|
|
15
|
+
};
|
|
16
|
+
// decelerating to zero velocity
|
|
17
|
+
const easeOutQuad = function (t: number): number {
|
|
18
|
+
return t * (2 - t);
|
|
19
|
+
};
|
|
20
|
+
// acceleration until halfway, then deceleration
|
|
21
|
+
const easeInOutQuad = function (t: number): number {
|
|
22
|
+
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
|
|
23
|
+
};
|
|
24
|
+
// accelerating from zero velocity
|
|
25
|
+
const easeInCubic = function (t: number): number {
|
|
26
|
+
return t * t * t;
|
|
27
|
+
};
|
|
28
|
+
// decelerating to zero velocity
|
|
29
|
+
const easeOutCubic = function (t: number): number {
|
|
30
|
+
return --t * t * t + 1;
|
|
31
|
+
};
|
|
32
|
+
// acceleration until halfway, then deceleration
|
|
33
|
+
const easeInOutCubic = function (t: number): number {
|
|
34
|
+
return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
|
|
35
|
+
};
|
|
36
|
+
// accelerating from zero velocity
|
|
37
|
+
const easeInQuart = function (t: number): number {
|
|
38
|
+
return t * t * t * t;
|
|
39
|
+
};
|
|
40
|
+
// decelerating to zero velocity
|
|
41
|
+
const easeOutQuart = function (t: number): number {
|
|
42
|
+
return 1 - --t * t * t * t;
|
|
43
|
+
};
|
|
44
|
+
// acceleration until halfway, then deceleration
|
|
45
|
+
const easeInOutQuart = function (t: number): number {
|
|
46
|
+
return t < 0.5 ? 8 * t * t * t * t : 1 - 8 * --t * t * t * t;
|
|
47
|
+
};
|
|
48
|
+
// accelerating from zero velocity
|
|
49
|
+
const easeInQuint = function (t: number): number {
|
|
50
|
+
return t * t * t * t * t;
|
|
51
|
+
};
|
|
52
|
+
// decelerating to zero velocity
|
|
53
|
+
const easeOutQuint = function (t: number): number {
|
|
54
|
+
return 1 + --t * t * t * t * t;
|
|
55
|
+
};
|
|
56
|
+
// acceleration until halfway, then deceleration
|
|
57
|
+
const easeInOutQuint = function (t: number): number {
|
|
58
|
+
return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * --t * t * t * t * t;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const animations = {
|
|
62
|
+
easeOut,
|
|
63
|
+
linear,
|
|
64
|
+
easeInQuad,
|
|
65
|
+
easeOutQuad,
|
|
66
|
+
easeInOutQuad,
|
|
67
|
+
easeInCubic,
|
|
68
|
+
easeOutCubic,
|
|
69
|
+
easeInOutCubic,
|
|
70
|
+
easeInQuart,
|
|
71
|
+
easeOutQuart,
|
|
72
|
+
easeInOutQuart,
|
|
73
|
+
easeInQuint,
|
|
74
|
+
easeOutQuint,
|
|
75
|
+
easeInOutQuint,
|
|
76
|
+
};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { animations } from "./animations.constants";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
AnimationType,
|
|
5
|
+
ReactZoomPanPinchContext,
|
|
6
|
+
StateType,
|
|
7
|
+
} from "../../models";
|
|
8
|
+
|
|
9
|
+
const handleCancelAnimationFrame = (animation: AnimationType | null) => {
|
|
10
|
+
if (typeof animation === "number") {
|
|
11
|
+
cancelAnimationFrame(animation);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const handleCancelAnimation = (
|
|
16
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
17
|
+
): void => {
|
|
18
|
+
if (!contextInstance.mounted) return;
|
|
19
|
+
handleCancelAnimationFrame(contextInstance.animation);
|
|
20
|
+
// Clear animation state
|
|
21
|
+
contextInstance.animate = false;
|
|
22
|
+
contextInstance.animation = null;
|
|
23
|
+
contextInstance.velocity = null;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function handleSetupAnimation(
|
|
27
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
28
|
+
animationName: string,
|
|
29
|
+
animationTime: number,
|
|
30
|
+
callback: (step: number) => void,
|
|
31
|
+
): void {
|
|
32
|
+
if (!contextInstance.mounted) return;
|
|
33
|
+
const startTime = new Date().getTime();
|
|
34
|
+
const lastStep = 1;
|
|
35
|
+
|
|
36
|
+
// if another animation is active
|
|
37
|
+
handleCancelAnimation(contextInstance);
|
|
38
|
+
|
|
39
|
+
// new animation
|
|
40
|
+
contextInstance.animation = () => {
|
|
41
|
+
if (!contextInstance.mounted) {
|
|
42
|
+
return handleCancelAnimationFrame(contextInstance.animation);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const frameTime = new Date().getTime() - startTime;
|
|
46
|
+
const animationProgress = frameTime / animationTime;
|
|
47
|
+
const animationType = animations[animationName];
|
|
48
|
+
|
|
49
|
+
const step = animationType(animationProgress);
|
|
50
|
+
|
|
51
|
+
if (frameTime >= animationTime) {
|
|
52
|
+
callback(lastStep);
|
|
53
|
+
contextInstance.animation = null;
|
|
54
|
+
} else if (contextInstance.animation) {
|
|
55
|
+
callback(step);
|
|
56
|
+
requestAnimationFrame(contextInstance.animation);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
requestAnimationFrame(contextInstance.animation);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function animate(
|
|
64
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
65
|
+
targetState: StateType,
|
|
66
|
+
animationTime: number,
|
|
67
|
+
animationName: string,
|
|
68
|
+
): void {
|
|
69
|
+
const isValid = isValidTargetState(targetState);
|
|
70
|
+
if (!contextInstance.mounted || !isValid) return;
|
|
71
|
+
const { setTransformState } = contextInstance;
|
|
72
|
+
const { scale, positionX, positionY } = contextInstance.transformState;
|
|
73
|
+
|
|
74
|
+
const scaleDiff = targetState.scale - scale;
|
|
75
|
+
const positionXDiff = targetState.positionX - positionX;
|
|
76
|
+
const positionYDiff = targetState.positionY - positionY;
|
|
77
|
+
|
|
78
|
+
if (animationTime === 0) {
|
|
79
|
+
setTransformState(
|
|
80
|
+
targetState.scale,
|
|
81
|
+
targetState.positionX,
|
|
82
|
+
targetState.positionY,
|
|
83
|
+
);
|
|
84
|
+
} else {
|
|
85
|
+
// animation start timestamp
|
|
86
|
+
handleSetupAnimation(
|
|
87
|
+
contextInstance,
|
|
88
|
+
animationName,
|
|
89
|
+
animationTime,
|
|
90
|
+
(step: number) => {
|
|
91
|
+
const newScale = scale + scaleDiff * step;
|
|
92
|
+
const newPositionX = positionX + positionXDiff * step;
|
|
93
|
+
const newPositionY = positionY + positionYDiff * step;
|
|
94
|
+
|
|
95
|
+
setTransformState(newScale, newPositionX, newPositionY);
|
|
96
|
+
},
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function isValidTargetState(targetState: StateType): boolean {
|
|
102
|
+
const { scale, positionX, positionY } = targetState;
|
|
103
|
+
|
|
104
|
+
if (isNaN(scale) || isNaN(positionX) || isNaN(positionY)) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { roundNumber } from "../../utils";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
BoundsType,
|
|
5
|
+
PositionType,
|
|
6
|
+
ReactZoomPanPinchContext,
|
|
7
|
+
} from "../../models";
|
|
8
|
+
import { ComponentsSizesType } from "./bounds.types";
|
|
9
|
+
|
|
10
|
+
export function getComponentsSizes(
|
|
11
|
+
wrapperComponent: HTMLDivElement,
|
|
12
|
+
contentComponent: HTMLDivElement,
|
|
13
|
+
newScale: number,
|
|
14
|
+
): ComponentsSizesType {
|
|
15
|
+
const wrapperWidth = wrapperComponent.offsetWidth;
|
|
16
|
+
const wrapperHeight = wrapperComponent.offsetHeight;
|
|
17
|
+
|
|
18
|
+
const contentWidth = contentComponent.offsetWidth;
|
|
19
|
+
const contentHeight = contentComponent.offsetHeight;
|
|
20
|
+
|
|
21
|
+
const newContentWidth = contentWidth * newScale;
|
|
22
|
+
const newContentHeight = contentHeight * newScale;
|
|
23
|
+
const newDiffWidth = wrapperWidth - newContentWidth;
|
|
24
|
+
const newDiffHeight = wrapperHeight - newContentHeight;
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
wrapperWidth,
|
|
28
|
+
wrapperHeight,
|
|
29
|
+
newContentWidth,
|
|
30
|
+
newDiffWidth,
|
|
31
|
+
newContentHeight,
|
|
32
|
+
newDiffHeight,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const getBounds = (
|
|
37
|
+
wrapperWidth: number,
|
|
38
|
+
newContentWidth: number,
|
|
39
|
+
diffWidth: number,
|
|
40
|
+
wrapperHeight: number,
|
|
41
|
+
newContentHeight: number,
|
|
42
|
+
diffHeight: number,
|
|
43
|
+
centerZoomedOut: boolean,
|
|
44
|
+
): BoundsType => {
|
|
45
|
+
const scaleWidthFactor =
|
|
46
|
+
wrapperWidth > newContentWidth
|
|
47
|
+
? diffWidth * (centerZoomedOut ? 1 : 0.5)
|
|
48
|
+
: 0;
|
|
49
|
+
const scaleHeightFactor =
|
|
50
|
+
wrapperHeight > newContentHeight
|
|
51
|
+
? diffHeight * (centerZoomedOut ? 1 : 0.5)
|
|
52
|
+
: 0;
|
|
53
|
+
|
|
54
|
+
const minPositionX = wrapperWidth - newContentWidth - scaleWidthFactor;
|
|
55
|
+
const maxPositionX = scaleWidthFactor;
|
|
56
|
+
const minPositionY = wrapperHeight - newContentHeight - scaleHeightFactor;
|
|
57
|
+
const maxPositionY = scaleHeightFactor;
|
|
58
|
+
|
|
59
|
+
return { minPositionX, maxPositionX, minPositionY, maxPositionY };
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const calculateBounds = (
|
|
63
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
64
|
+
newScale: number,
|
|
65
|
+
): BoundsType => {
|
|
66
|
+
const { wrapperComponent, contentComponent } = contextInstance;
|
|
67
|
+
const { centerZoomedOut } = contextInstance.setup;
|
|
68
|
+
|
|
69
|
+
if (!wrapperComponent || !contentComponent) {
|
|
70
|
+
throw new Error("Components are not mounted");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const {
|
|
74
|
+
wrapperWidth,
|
|
75
|
+
wrapperHeight,
|
|
76
|
+
newContentWidth,
|
|
77
|
+
newDiffWidth,
|
|
78
|
+
newContentHeight,
|
|
79
|
+
newDiffHeight,
|
|
80
|
+
} = getComponentsSizes(wrapperComponent, contentComponent, newScale);
|
|
81
|
+
|
|
82
|
+
const bounds = getBounds(
|
|
83
|
+
wrapperWidth,
|
|
84
|
+
newContentWidth,
|
|
85
|
+
newDiffWidth,
|
|
86
|
+
wrapperHeight,
|
|
87
|
+
newContentHeight,
|
|
88
|
+
newDiffHeight,
|
|
89
|
+
Boolean(centerZoomedOut),
|
|
90
|
+
);
|
|
91
|
+
return bounds;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export const handleCalculateBounds = (
|
|
95
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
96
|
+
newScale: number,
|
|
97
|
+
): BoundsType => {
|
|
98
|
+
const bounds = calculateBounds(contextInstance, newScale);
|
|
99
|
+
|
|
100
|
+
// Save bounds
|
|
101
|
+
contextInstance.bounds = bounds;
|
|
102
|
+
return bounds;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export function getMouseBoundedPosition(
|
|
106
|
+
positionX: number,
|
|
107
|
+
positionY: number,
|
|
108
|
+
bounds: BoundsType,
|
|
109
|
+
limitToBounds: boolean,
|
|
110
|
+
paddingValueX: number,
|
|
111
|
+
paddingValueY: number,
|
|
112
|
+
wrapperComponent: HTMLDivElement | null,
|
|
113
|
+
): PositionType {
|
|
114
|
+
const { minPositionX, minPositionY, maxPositionX, maxPositionY } = bounds;
|
|
115
|
+
|
|
116
|
+
let paddingX = 0;
|
|
117
|
+
let paddingY = 0;
|
|
118
|
+
|
|
119
|
+
if (wrapperComponent) {
|
|
120
|
+
paddingX = paddingValueX;
|
|
121
|
+
paddingY = paddingValueY;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const x = boundLimiter(
|
|
125
|
+
positionX,
|
|
126
|
+
minPositionX - paddingX,
|
|
127
|
+
maxPositionX + paddingX,
|
|
128
|
+
limitToBounds,
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
const y = boundLimiter(
|
|
132
|
+
positionY,
|
|
133
|
+
minPositionY - paddingY,
|
|
134
|
+
maxPositionY + paddingY,
|
|
135
|
+
limitToBounds,
|
|
136
|
+
);
|
|
137
|
+
return { x, y };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Keeps value between given bounds, used for limiting view to given boundaries
|
|
142
|
+
* 1# eg. boundLimiter(2, 0, 3, true) => 2
|
|
143
|
+
* 2# eg. boundLimiter(4, 0, 3, true) => 3
|
|
144
|
+
* 3# eg. boundLimiter(-2, 0, 3, true) => 0
|
|
145
|
+
* 4# eg. boundLimiter(10, 0, 3, false) => 10
|
|
146
|
+
*/
|
|
147
|
+
export const boundLimiter = (
|
|
148
|
+
value: number,
|
|
149
|
+
minBound: number,
|
|
150
|
+
maxBound: number,
|
|
151
|
+
isActive: boolean,
|
|
152
|
+
): number => {
|
|
153
|
+
if (!isActive) return roundNumber(value, 2);
|
|
154
|
+
if (value < minBound) return roundNumber(minBound, 2);
|
|
155
|
+
if (value > maxBound) return roundNumber(maxBound, 2);
|
|
156
|
+
return roundNumber(value, 2);
|
|
157
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ReactZoomPanPinchContext } from "../../models";
|
|
2
|
+
|
|
3
|
+
import { animate } from "../animations/animations.utils";
|
|
4
|
+
import { getMousePosition } from "core/wheel/wheel.utils";
|
|
5
|
+
import { handleZoomToPoint } from "core/zoom/zoom.logic";
|
|
6
|
+
import { isExcludedNode } from "utils";
|
|
7
|
+
import {
|
|
8
|
+
handleCalculateButtonZoom,
|
|
9
|
+
resetTransformations,
|
|
10
|
+
} from "core/handlers/handlers.utils";
|
|
11
|
+
|
|
12
|
+
export function handleDoubleClick(
|
|
13
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
14
|
+
event: MouseEvent | TouchEvent,
|
|
15
|
+
): void {
|
|
16
|
+
const {
|
|
17
|
+
disabled,
|
|
18
|
+
mode,
|
|
19
|
+
step,
|
|
20
|
+
animationTime,
|
|
21
|
+
animationType,
|
|
22
|
+
} = contextInstance.setup.doubleClick;
|
|
23
|
+
|
|
24
|
+
if (disabled) return;
|
|
25
|
+
|
|
26
|
+
if (mode === "reset") {
|
|
27
|
+
return resetTransformations(contextInstance, animationTime, animationType);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const { scale } = contextInstance.transformState;
|
|
31
|
+
const { contentComponent } = contextInstance;
|
|
32
|
+
|
|
33
|
+
if (!contentComponent) return console.error("No ContentComponent found");
|
|
34
|
+
|
|
35
|
+
const delta = mode === "zoomOut" ? -1 : 1;
|
|
36
|
+
|
|
37
|
+
const newScale = handleCalculateButtonZoom(contextInstance, delta, step);
|
|
38
|
+
const mousePosition = getMousePosition(event, contentComponent, scale);
|
|
39
|
+
const targetState = handleZoomToPoint(
|
|
40
|
+
contextInstance,
|
|
41
|
+
newScale,
|
|
42
|
+
mousePosition.x,
|
|
43
|
+
mousePosition.y,
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
if (!targetState) {
|
|
47
|
+
return console.error(
|
|
48
|
+
"Error during zoom event. New transformation state was not calculated.",
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
animate(contextInstance, targetState, animationTime, animationType);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const isDoubleClickAllowed = (
|
|
56
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
57
|
+
event: MouseEvent | TouchEvent,
|
|
58
|
+
): boolean => {
|
|
59
|
+
const { isInitialized, setup, wrapperComponent } = contextInstance;
|
|
60
|
+
const { disabled, excluded } = setup.doubleClick;
|
|
61
|
+
|
|
62
|
+
const target = event.target as HTMLElement;
|
|
63
|
+
const isWrapperChild = wrapperComponent?.contains(target);
|
|
64
|
+
const isAllowed = isInitialized && target && isWrapperChild && !disabled;
|
|
65
|
+
|
|
66
|
+
if (!isAllowed) return false;
|
|
67
|
+
|
|
68
|
+
const isExcluded = isExcludedNode(target, excluded);
|
|
69
|
+
|
|
70
|
+
if (isExcluded) return false;
|
|
71
|
+
|
|
72
|
+
if (!isAllowed) return false;
|
|
73
|
+
|
|
74
|
+
return true;
|
|
75
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { ReactZoomPanPinchContext } from "../../models";
|
|
2
|
+
import {
|
|
3
|
+
calculateZoomToNode,
|
|
4
|
+
handleZoomToViewCenter,
|
|
5
|
+
isValidZoomNode,
|
|
6
|
+
resetTransformations,
|
|
7
|
+
} from "./handlers.utils";
|
|
8
|
+
import { animations } from "../animations/animations.constants";
|
|
9
|
+
import { animate, handleCancelAnimation } from "../animations/animations.utils";
|
|
10
|
+
import { getCenterPosition } from "../../utils";
|
|
11
|
+
|
|
12
|
+
export const zoomIn = (contextInstance: ReactZoomPanPinchContext) => (
|
|
13
|
+
step = 0.5,
|
|
14
|
+
animationTime = 300,
|
|
15
|
+
animationType: keyof typeof animations = "easeOut",
|
|
16
|
+
): void => {
|
|
17
|
+
handleZoomToViewCenter(
|
|
18
|
+
contextInstance,
|
|
19
|
+
1,
|
|
20
|
+
step,
|
|
21
|
+
animationTime,
|
|
22
|
+
animationType,
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const zoomOut = (contextInstance: ReactZoomPanPinchContext) => (
|
|
27
|
+
step = 0.5,
|
|
28
|
+
animationTime = 300,
|
|
29
|
+
animationType: keyof typeof animations = "easeOut",
|
|
30
|
+
): void => {
|
|
31
|
+
handleZoomToViewCenter(
|
|
32
|
+
contextInstance,
|
|
33
|
+
-1,
|
|
34
|
+
step,
|
|
35
|
+
animationTime,
|
|
36
|
+
animationType,
|
|
37
|
+
);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const setTransform = (contextInstance: ReactZoomPanPinchContext) => (
|
|
41
|
+
newPositionX: number,
|
|
42
|
+
newPositionY: number,
|
|
43
|
+
newScale: number,
|
|
44
|
+
animationTime = 300,
|
|
45
|
+
animationType: keyof typeof animations = "easeOut",
|
|
46
|
+
): void => {
|
|
47
|
+
const { positionX, positionY, scale } = contextInstance.transformState;
|
|
48
|
+
const { wrapperComponent, contentComponent } = contextInstance;
|
|
49
|
+
const { disabled } = contextInstance.setup;
|
|
50
|
+
|
|
51
|
+
if (disabled || !wrapperComponent || !contentComponent) return;
|
|
52
|
+
|
|
53
|
+
const targetState = {
|
|
54
|
+
positionX: isNaN(newPositionX) ? positionX : newPositionX,
|
|
55
|
+
positionY: isNaN(newPositionY) ? positionY : newPositionY,
|
|
56
|
+
scale: isNaN(newScale) ? scale : newScale,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
animate(contextInstance, targetState, animationTime, animationType);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const resetTransform = (contextInstance: ReactZoomPanPinchContext) => (
|
|
63
|
+
animationTime = 200,
|
|
64
|
+
animationType: keyof typeof animations = "easeOut",
|
|
65
|
+
): void => {
|
|
66
|
+
resetTransformations(contextInstance, animationTime, animationType);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const centerView = (contextInstance: ReactZoomPanPinchContext) => (
|
|
70
|
+
scale?: number,
|
|
71
|
+
animationTime = 200,
|
|
72
|
+
animationType: keyof typeof animations = "easeOut",
|
|
73
|
+
): void => {
|
|
74
|
+
const {
|
|
75
|
+
transformState,
|
|
76
|
+
wrapperComponent,
|
|
77
|
+
contentComponent,
|
|
78
|
+
} = contextInstance;
|
|
79
|
+
if (wrapperComponent && contentComponent) {
|
|
80
|
+
const targetState = getCenterPosition(
|
|
81
|
+
scale || transformState.scale,
|
|
82
|
+
wrapperComponent,
|
|
83
|
+
contentComponent,
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
animate(contextInstance, targetState, animationTime, animationType);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export const zoomToElement = (contextInstance: ReactZoomPanPinchContext) => (
|
|
91
|
+
node: HTMLElement | string,
|
|
92
|
+
scale?: number,
|
|
93
|
+
animationTime = 600,
|
|
94
|
+
animationType: keyof typeof animations = "easeOut",
|
|
95
|
+
): void => {
|
|
96
|
+
handleCancelAnimation(contextInstance);
|
|
97
|
+
|
|
98
|
+
const { wrapperComponent } = contextInstance;
|
|
99
|
+
|
|
100
|
+
const target: HTMLElement | null =
|
|
101
|
+
typeof node === "string" ? document.getElementById(node) : node;
|
|
102
|
+
|
|
103
|
+
if (
|
|
104
|
+
wrapperComponent &&
|
|
105
|
+
isValidZoomNode(target) &&
|
|
106
|
+
target &&
|
|
107
|
+
wrapperComponent.contains(target)
|
|
108
|
+
) {
|
|
109
|
+
const targetState = calculateZoomToNode(contextInstance, target, scale);
|
|
110
|
+
animate(contextInstance, targetState, animationTime, animationType);
|
|
111
|
+
}
|
|
112
|
+
};
|