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.
Files changed (139) hide show
  1. package/dist/index.cjs.js +770 -277
  2. package/dist/index.cjs.js.map +1 -1
  3. package/dist/index.d.ts +116 -22
  4. package/dist/index.esm.js +766 -269
  5. package/dist/index.esm.js.map +1 -1
  6. package/package.json +73 -60
  7. package/src/components/index.ts +5 -0
  8. package/src/components/keep-scale/keep-scale.tsx +28 -0
  9. package/src/components/mini-map/mini-map.tsx +292 -0
  10. package/src/components/mini-map/use-resize.hook.ts +74 -0
  11. package/src/components/transform-component/transform-component.module.css +43 -0
  12. package/src/components/transform-component/transform-component.tsx +96 -0
  13. package/src/components/transform-wrapper/transform-wrapper.tsx +39 -0
  14. package/src/components/virtualize/virtualize.tsx +133 -0
  15. package/src/components/virtualize/virtualize.utils.ts +104 -0
  16. package/src/constants/state.constants.ts +99 -0
  17. package/src/core/animations/animations.constants.ts +78 -0
  18. package/src/core/animations/animations.utils.ts +115 -0
  19. package/src/core/bounds/bounds.types.ts +8 -0
  20. package/src/core/bounds/bounds.utils.ts +226 -0
  21. package/src/core/double-click/double-click.logic.ts +127 -0
  22. package/src/core/handlers/handlers.logic.ts +122 -0
  23. package/src/core/handlers/handlers.utils.ts +243 -0
  24. package/src/core/instance.core.ts +704 -0
  25. package/src/core/pan/panning.logic.ts +122 -0
  26. package/src/core/pan/panning.utils.ts +216 -0
  27. package/src/core/pan/velocity.logic.ts +187 -0
  28. package/src/core/pan/velocity.utils.ts +83 -0
  29. package/src/core/pinch/pinch.logic.ts +139 -0
  30. package/src/core/pinch/pinch.utils.ts +95 -0
  31. package/src/core/wheel/wheel.logic.ts +161 -0
  32. package/src/core/wheel/wheel.utils.ts +157 -0
  33. package/src/core/zoom/zoom.logic.ts +71 -0
  34. package/src/core/zoom/zoom.utils.ts +62 -0
  35. package/src/hooks/index.ts +5 -0
  36. package/src/hooks/use-controls.tsx +9 -0
  37. package/src/hooks/use-transform-component.tsx +30 -0
  38. package/src/hooks/use-transform-context.tsx +13 -0
  39. package/src/hooks/use-transform-effect.tsx +22 -0
  40. package/src/hooks/use-transform-init.tsx +28 -0
  41. package/src/hooks/use-zoom-pan-pinch.tsx +48 -0
  42. package/src/index.ts +6 -0
  43. package/src/models/calculations.model.ts +34 -0
  44. package/src/models/context.model.ts +218 -0
  45. package/src/models/helpers.model.ts +15 -0
  46. package/src/models/index.ts +3 -0
  47. package/src/react-app-env.d.ts +1 -0
  48. package/src/stories/assets/big-image.jpeg +0 -0
  49. package/src/stories/assets/map.jpg +0 -0
  50. package/src/stories/assets/medium-image.jpg +0 -0
  51. package/src/stories/assets/pin.svg +1 -0
  52. package/src/stories/assets/small-image.jpg +0 -0
  53. package/src/stories/assets/svg-example.svg +14 -0
  54. package/src/stories/docs/DocTable.tsx +59 -0
  55. package/src/stories/docs/HandlerCards.tsx +102 -0
  56. package/src/stories/docs/Introduction.stories.mdx +135 -0
  57. package/src/stories/docs/PropTable.tsx +116 -0
  58. package/src/stories/docs/docs.module.css +370 -0
  59. package/src/stories/docs/handlers.stories.mdx +7 -0
  60. package/src/stories/docs/handlers.tsx +111 -0
  61. package/src/stories/docs/props.stories.mdx +11 -0
  62. package/src/stories/docs/props.tsx +571 -0
  63. package/src/stories/examples/axis-lock/axis-lock.stories.mdx +35 -0
  64. package/src/stories/examples/axis-lock/example.tsx +114 -0
  65. package/src/stories/examples/bounds/bounds.data.tsx +191 -0
  66. package/src/stories/examples/bounds/bounds.stories.mdx +99 -0
  67. package/src/stories/examples/center-zoomed-out/center-zoomed-out.stories.mdx +206 -0
  68. package/src/stories/examples/cinema/cinema-data.tsx +578 -0
  69. package/src/stories/examples/cinema/cinema.stories.mdx +30 -0
  70. package/src/stories/examples/cinema/example.tsx +378 -0
  71. package/src/stories/examples/cinema/image.png +0 -0
  72. package/src/stories/examples/content/content.stories.mdx +172 -0
  73. package/src/stories/examples/content/mixed-content.data.tsx +624 -0
  74. package/src/stories/examples/content-rerendering/content-updating.stories.mdx +34 -0
  75. package/src/stories/examples/content-rerendering/example.tsx +631 -0
  76. package/src/stories/examples/controls/controls.stories.mdx +120 -0
  77. package/src/stories/examples/image/example.tsx +149 -0
  78. package/src/stories/examples/image/image.stories.mdx +46 -0
  79. package/src/stories/examples/image-annotations/example.tsx +1442 -0
  80. package/src/stories/examples/image-annotations/image-annotations.stories.mdx +39 -0
  81. package/src/stories/examples/image-responsive/example.tsx +152 -0
  82. package/src/stories/examples/image-responsive/image-responsive.stories.mdx +51 -0
  83. package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +107 -0
  84. package/src/stories/examples/initial-transform/example.tsx +233 -0
  85. package/src/stories/examples/initial-transform/initial-transform.stories.mdx +37 -0
  86. package/src/stories/examples/keep-scale/example.tsx +882 -0
  87. package/src/stories/examples/keep-scale/keep-scale.stories.mdx +30 -0
  88. package/src/stories/examples/keep-scale/map.stories.mdx +28 -0
  89. package/src/stories/examples/medical-viewer/chest-xray.tsx +246 -0
  90. package/src/stories/examples/medical-viewer/example.tsx +690 -0
  91. package/src/stories/examples/medical-viewer/medical-viewer.stories.mdx +49 -0
  92. package/src/stories/examples/mini-map/example.tsx +122 -0
  93. package/src/stories/examples/mini-map/mini-map.stories.mdx +26 -0
  94. package/src/stories/examples/miro/example.tsx +1573 -0
  95. package/src/stories/examples/miro/miro.stories.mdx +31 -0
  96. package/src/stories/examples/padding/example.tsx +147 -0
  97. package/src/stories/examples/padding/padding.stories.mdx +35 -0
  98. package/src/stories/examples/product-card/example.tsx +594 -0
  99. package/src/stories/examples/product-card/product-card.stories.mdx +29 -0
  100. package/src/stories/examples/stadium/example.tsx +1040 -0
  101. package/src/stories/examples/stadium/stadium-data.ts +224 -0
  102. package/src/stories/examples/stadium/stadium.stories.mdx +32 -0
  103. package/src/stories/examples/stress/example.tsx +59 -0
  104. package/src/stories/examples/stress/stress-test.stories.mdx +28 -0
  105. package/src/stories/examples/svg-zoom-to-element/creativity.svg +143 -0
  106. package/src/stories/examples/svg-zoom-to-element/example.tsx +95 -0
  107. package/src/stories/examples/svg-zoom-to-element/svg-zoom-to-element.stories.mdx +28 -0
  108. package/src/stories/examples/text-selection/example.tsx +221 -0
  109. package/src/stories/examples/text-selection/text-selection.stories.mdx +31 -0
  110. package/src/stories/examples/velocity/example.tsx +908 -0
  111. package/src/stories/examples/velocity/velocity.stories.mdx +35 -0
  112. package/src/stories/examples/virtualize/example.tsx +239 -0
  113. package/src/stories/examples/virtualize/virtualize.stories.mdx +84 -0
  114. package/src/stories/examples/zoom-to-element/example.tsx +219 -0
  115. package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +31 -0
  116. package/src/stories/hooks/use-controls.stories.mdx +86 -0
  117. package/src/stories/hooks/use-transform-component.stories.mdx +108 -0
  118. package/src/stories/hooks/use-transform-context.stories.mdx +92 -0
  119. package/src/stories/hooks/use-transform-effect.stories.mdx +114 -0
  120. package/src/stories/hooks/use-transform-init.stories.mdx +105 -0
  121. package/src/stories/types/args.types.ts +192 -0
  122. package/src/stories/utils/args.utils.ts +35 -0
  123. package/src/stories/utils/bounds-overlay.tsx +168 -0
  124. package/src/stories/utils/code.utils.tsx +22 -0
  125. package/src/stories/utils/controls.utils.tsx +347 -0
  126. package/src/stories/utils/index.ts +5 -0
  127. package/src/stories/utils/styles.module.css +223 -0
  128. package/src/stories/utils/viewer.styles.ts +34 -0
  129. package/src/styles.css +8 -0
  130. package/src/typings.d.ts +21 -0
  131. package/src/utils/calculations.utils.ts +16 -0
  132. package/src/utils/callback.utils.ts +11 -0
  133. package/src/utils/context.utils.ts +49 -0
  134. package/src/utils/event.utils.ts +38 -0
  135. package/src/utils/helpers.utils.ts +22 -0
  136. package/src/utils/index.ts +8 -0
  137. package/src/utils/ref.utils.ts +32 -0
  138. package/src/utils/state.utils.ts +77 -0
  139. package/src/utils/styles.utils.ts +49 -0
@@ -0,0 +1,96 @@
1
+ /* eslint-disable react/require-default-props */
2
+ import React, { useContext, useEffect, useRef } from "react";
3
+
4
+ import { Context } from "../transform-wrapper/transform-wrapper";
5
+ import { baseClasses } from "../../constants/state.constants";
6
+ import { getTransformStyles } from "../../utils/styles.utils";
7
+
8
+ import styles from "./transform-component.module.css";
9
+
10
+ type Props = {
11
+ children: React.ReactNode;
12
+ wrapperClass?: string;
13
+ contentClass?: string;
14
+ wrapperStyle?: React.CSSProperties;
15
+ contentStyle?: React.CSSProperties;
16
+ wrapperProps?: React.HTMLAttributes<HTMLDivElement>;
17
+ contentProps?: React.HTMLAttributes<HTMLDivElement>;
18
+ /**
19
+ * When true, renders an infinite dot-grid background behind the content
20
+ * that scales and pans in sync with the transform. Pair with
21
+ * `limitToBounds={false}` on TransformWrapper for full effect.
22
+ */
23
+ infinite?: boolean;
24
+ };
25
+
26
+ export const TransformComponent: React.FC<Props> = ({
27
+ children,
28
+ wrapperClass = "",
29
+ contentClass = "",
30
+ wrapperStyle,
31
+ contentStyle,
32
+ wrapperProps = {},
33
+ contentProps = {},
34
+ infinite = false,
35
+ }: Props) => {
36
+ const instance = useContext(Context);
37
+ const { init, cleanupWindowEvents } = instance;
38
+
39
+ const wrapperRef = useRef<HTMLDivElement | null>(null);
40
+ const contentRef = useRef<HTMLDivElement | null>(null);
41
+ const gridRef = useRef<HTMLDivElement | null>(null);
42
+
43
+ useEffect(() => {
44
+ const wrapper = wrapperRef.current;
45
+ const content = contentRef.current;
46
+ if (wrapper !== null && content !== null && init) {
47
+ init?.(wrapper, content);
48
+ }
49
+ return () => {
50
+ cleanupWindowEvents?.();
51
+ };
52
+ // eslint-disable-next-line react-hooks/exhaustive-deps
53
+ }, []);
54
+
55
+ useEffect(() => {
56
+ if (!infinite) return;
57
+ const grid = gridRef.current;
58
+ if (!grid) return;
59
+
60
+ const sync = () => {
61
+ const { positionX, positionY } = instance.state;
62
+ grid.style.backgroundPosition = `${positionX}px ${positionY}px`;
63
+ };
64
+
65
+ sync();
66
+ return instance.onChange(sync);
67
+ }, [infinite, instance]);
68
+
69
+ return (
70
+ <div
71
+ {...wrapperProps}
72
+ ref={wrapperRef}
73
+ className={`${baseClasses.wrapperClass} ${styles.wrapper} ${wrapperClass}`}
74
+ style={wrapperStyle}
75
+ >
76
+ {infinite && (
77
+ <div ref={gridRef} className={styles.infiniteGrid} aria-hidden />
78
+ )}
79
+ <div
80
+ {...contentProps}
81
+ ref={contentRef}
82
+ className={`${baseClasses.contentClass} ${styles.content} ${contentClass}`}
83
+ style={{
84
+ ...contentStyle,
85
+ transform: getTransformStyles(
86
+ instance.state.positionX,
87
+ instance.state.positionY,
88
+ instance.state.scale,
89
+ ),
90
+ }}
91
+ >
92
+ {children}
93
+ </div>
94
+ </div>
95
+ );
96
+ };
@@ -0,0 +1,39 @@
1
+ import React, { useEffect, useImperativeHandle, useRef } from "react";
2
+
3
+ import { ZoomPanPinch } from "../../core/instance.core";
4
+ import {
5
+ ReactZoomPanPinchContentRef,
6
+ ReactZoomPanPinchProps,
7
+ } from "../../models";
8
+ import { getControls } from "../../utils";
9
+
10
+ export const Context = React.createContext<ZoomPanPinch>(null as any);
11
+
12
+ const getContent = (
13
+ children: ReactZoomPanPinchProps["children"],
14
+ ctx: ReactZoomPanPinchContentRef,
15
+ ) => {
16
+ if (typeof children === "function") {
17
+ return children(ctx);
18
+ }
19
+ return children;
20
+ };
21
+
22
+ export const TransformWrapper = React.forwardRef(
23
+ (
24
+ props: Omit<ReactZoomPanPinchProps, "ref">,
25
+ ref: React.Ref<ReactZoomPanPinchContentRef>,
26
+ ) => {
27
+ const instance = useRef(new ZoomPanPinch(props)).current;
28
+
29
+ const content = getContent(props.children, getControls(instance));
30
+
31
+ useImperativeHandle(ref, () => getControls(instance), [instance]);
32
+
33
+ useEffect(() => {
34
+ instance.update(props);
35
+ }, [instance, props]);
36
+
37
+ return <Context.Provider value={instance}>{content}</Context.Provider>;
38
+ },
39
+ );
@@ -0,0 +1,133 @@
1
+ import React, { useContext, useEffect, useRef, useState } from "react";
2
+
3
+ import { Context } from "../transform-wrapper/transform-wrapper";
4
+ import { isElementVisible } from "./virtualize.utils";
5
+
6
+ /* eslint-disable react/require-default-props */
7
+ export type VirtualizeProps = {
8
+ /** Horizontal position of the element in content (unscaled) space. */
9
+ x: number;
10
+ /** Vertical position of the element in content (unscaled) space. */
11
+ y: number;
12
+ /** Width of the element in content (unscaled) space. */
13
+ width: number;
14
+ /** Height of the element in content (unscaled) space. */
15
+ height: number;
16
+
17
+ /**
18
+ * Extra pixels around the viewport that still count as "visible".
19
+ * Positive values mount children before they scroll into view,
20
+ * acting like `IntersectionObserver`'s `rootMargin`.
21
+ * @default 0
22
+ */
23
+ margin?: number;
24
+
25
+ /**
26
+ * Fraction of the element area (0-1) that must overlap the (expanded)
27
+ * viewport for the children to be mounted.
28
+ *
29
+ * - `0` — any overlap is enough (default).
30
+ * - `1` — the element must be fully inside the viewport.
31
+ * @default 0
32
+ */
33
+ threshold?: number;
34
+
35
+ /**
36
+ * Content rendered in place of children when the element is outside
37
+ * the viewport. Defaults to `null` (nothing rendered).
38
+ */
39
+ placeholder?: React.ReactNode;
40
+
41
+ /** Fires when the element enters the viewport and children are mounted. */
42
+ onShow?: () => void;
43
+ /** Fires when the element leaves the viewport and children are unmounted. */
44
+ onHide?: () => void;
45
+
46
+ children: React.ReactNode;
47
+ className?: string;
48
+ style?: React.CSSProperties;
49
+ };
50
+
51
+ export const Virtualize = React.forwardRef<HTMLDivElement, VirtualizeProps>(
52
+ (
53
+ {
54
+ x,
55
+ y,
56
+ width,
57
+ height,
58
+ margin = 0,
59
+ threshold = 0,
60
+ placeholder = null,
61
+ onShow,
62
+ onHide,
63
+ children,
64
+ className,
65
+ style,
66
+ },
67
+ ref,
68
+ ) => {
69
+ const instance = useContext(Context);
70
+ const [visible, setVisible] = useState(false);
71
+ const visibleRef = useRef(false);
72
+
73
+ const onShowRef = useRef(onShow);
74
+ const onHideRef = useRef(onHide);
75
+ onShowRef.current = onShow;
76
+ onHideRef.current = onHide;
77
+
78
+ useEffect(() => {
79
+ const check = () => {
80
+ const wrapper = instance.wrapperComponent;
81
+ if (!wrapper) return;
82
+
83
+ const nowVisible = isElementVisible({
84
+ elementX: x,
85
+ elementY: y,
86
+ elementWidth: width,
87
+ elementHeight: height,
88
+ scale: instance.state.scale,
89
+ positionX: instance.state.positionX,
90
+ positionY: instance.state.positionY,
91
+ viewportWidth: wrapper.offsetWidth,
92
+ viewportHeight: wrapper.offsetHeight,
93
+ margin,
94
+ threshold,
95
+ });
96
+
97
+ if (nowVisible !== visibleRef.current) {
98
+ visibleRef.current = nowVisible;
99
+ setVisible(nowVisible);
100
+ if (nowVisible) {
101
+ onShowRef.current?.();
102
+ } else {
103
+ onHideRef.current?.();
104
+ }
105
+ }
106
+ };
107
+
108
+ check();
109
+ const unsubChange = instance.onChange(check);
110
+
111
+ let unsubInit: (() => void) | undefined;
112
+ if (!instance.wrapperComponent) {
113
+ unsubInit = instance.onInit(() => check());
114
+ }
115
+
116
+ return () => {
117
+ unsubChange();
118
+ unsubInit?.();
119
+ };
120
+ }, [instance, x, y, width, height, margin, threshold]);
121
+
122
+ if (!visible) {
123
+ // eslint-disable-next-line react/jsx-no-useless-fragment
124
+ return placeholder ? <>{placeholder}</> : null;
125
+ }
126
+
127
+ return (
128
+ <div ref={ref} className={className} style={style}>
129
+ {children}
130
+ </div>
131
+ );
132
+ },
133
+ );
@@ -0,0 +1,104 @@
1
+ export type Rect = {
2
+ x: number;
3
+ y: number;
4
+ width: number;
5
+ height: number;
6
+ };
7
+
8
+ /**
9
+ * Compute the overlap area between two axis-aligned rectangles.
10
+ * Returns 0 when the rectangles do not intersect.
11
+ */
12
+ export function getOverlapArea(a: Rect, b: Rect): number {
13
+ const overlapX = Math.max(
14
+ 0,
15
+ Math.min(a.x + a.width, b.x + b.width) - Math.max(a.x, b.x),
16
+ );
17
+ const overlapY = Math.max(
18
+ 0,
19
+ Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y),
20
+ );
21
+ return overlapX * overlapY;
22
+ }
23
+
24
+ export type IsElementVisibleOptions = {
25
+ /** Element position and size in content (unscaled) coordinates. */
26
+ elementX: number;
27
+ elementY: number;
28
+ elementWidth: number;
29
+ elementHeight: number;
30
+
31
+ /** Current transform state. */
32
+ scale: number;
33
+ positionX: number;
34
+ positionY: number;
35
+
36
+ /** Viewport (wrapper) dimensions in pixels. */
37
+ viewportWidth: number;
38
+ viewportHeight: number;
39
+
40
+ /**
41
+ * Extra pixels around the viewport that still count as "visible".
42
+ * Positive values mount elements before they scroll into view.
43
+ * Default: 0
44
+ */
45
+ margin?: number;
46
+
47
+ /**
48
+ * Fraction of the element area (0-1) that must overlap the viewport for
49
+ * the element to be considered visible.
50
+ * 0 = any overlap is enough (default).
51
+ * 1 = element must be fully inside the viewport.
52
+ */
53
+ threshold?: number;
54
+ };
55
+
56
+ /**
57
+ * Determine whether an element's content-space bounding box is visible
58
+ * in the current viewport, accounting for transform, margin, and threshold.
59
+ */
60
+ export function isElementVisible(opts: IsElementVisibleOptions): boolean {
61
+ const {
62
+ elementX,
63
+ elementY,
64
+ elementWidth,
65
+ elementHeight,
66
+ scale,
67
+ positionX,
68
+ positionY,
69
+ viewportWidth,
70
+ viewportHeight,
71
+ margin = 0,
72
+ threshold = 0,
73
+ } = opts;
74
+
75
+ const viewport: Rect = {
76
+ x: -margin,
77
+ y: -margin,
78
+ width: viewportWidth + 2 * margin,
79
+ height: viewportHeight + 2 * margin,
80
+ };
81
+
82
+ const element: Rect = {
83
+ x: elementX * scale + positionX,
84
+ y: elementY * scale + positionY,
85
+ width: elementWidth * scale,
86
+ height: elementHeight * scale,
87
+ };
88
+
89
+ if (threshold <= 0) {
90
+ const intersectsX =
91
+ element.x < viewport.x + viewport.width &&
92
+ element.x + element.width > viewport.x;
93
+ const intersectsY =
94
+ element.y < viewport.y + viewport.height &&
95
+ element.y + element.height > viewport.y;
96
+ return intersectsX && intersectsY;
97
+ }
98
+
99
+ const elementArea = element.width * element.height;
100
+ if (elementArea <= 0) return false;
101
+
102
+ const overlap = getOverlapArea(viewport, element);
103
+ return overlap / elementArea >= threshold;
104
+ }
@@ -0,0 +1,99 @@
1
+ import {
2
+ LibrarySetup,
3
+ ReactZoomPanPinchState,
4
+ ReactZoomPanPinchBaseClasses,
5
+ } from "../models/context.model";
6
+
7
+ export const initialState: ReactZoomPanPinchState = {
8
+ previousScale: 1,
9
+ scale: 1,
10
+ positionX: 0,
11
+ positionY: 0,
12
+ };
13
+
14
+ export const initialSetup: LibrarySetup = {
15
+ disabled: false,
16
+ minPositionX: null,
17
+ maxPositionX: null,
18
+ minPositionY: null,
19
+ maxPositionY: null,
20
+ minScale: 1,
21
+ maxScale: 8,
22
+ limitToBounds: true,
23
+ centerZoomedOut: false,
24
+ centerOnInit: false,
25
+ disablePadding: false,
26
+ smooth: true,
27
+ detached: false,
28
+ wheel: {
29
+ step: 0.015,
30
+ disabled: false,
31
+ wheelDisabled: false,
32
+ touchPadDisabled: false,
33
+ activationKeys: [],
34
+ excluded: [],
35
+ },
36
+ trackPadPanning: {
37
+ disabled: true,
38
+ velocityDisabled: false,
39
+ lockAxisX: false,
40
+ lockAxisY: false,
41
+ activationKeys: [],
42
+ excluded: [],
43
+ },
44
+ panning: {
45
+ disabled: false,
46
+ velocityDisabled: false,
47
+ lockAxisX: false,
48
+ lockAxisY: false,
49
+ allowLeftClickPan: true,
50
+ allowMiddleClickPan: true,
51
+ allowRightClickPan: true,
52
+ activationKeys: [],
53
+ excluded: [],
54
+ },
55
+ pinch: {
56
+ step: 5,
57
+ disabled: false,
58
+ allowPanning: true,
59
+ excluded: [],
60
+ },
61
+ doubleClick: {
62
+ disabled: false,
63
+ step: 0.7,
64
+ mode: "zoomIn",
65
+ animationType: "easeOut",
66
+ animationTime: 200,
67
+ excluded: [],
68
+ },
69
+ zoomAnimation: {
70
+ disabled: false,
71
+ size: 0.4,
72
+ animationTime: 200,
73
+ animationType: "easeOut",
74
+ },
75
+ autoAlignment: {
76
+ disabled: false,
77
+ sizeX: 100,
78
+ sizeY: 100,
79
+ animationTime: 200,
80
+ velocityAlignmentTime: 400,
81
+ animationType: "easeOut",
82
+ },
83
+ velocityAnimation: {
84
+ disabled: false,
85
+ sensitivityMouse: 1,
86
+ sensitivityTouch: 1.2,
87
+ maxStrengthMouse: 20,
88
+ maxStrengthTouch: 40,
89
+ inertia: 1,
90
+ animationTime: 300,
91
+ maxAnimationTime: 800,
92
+ animationType: "easeOut",
93
+ },
94
+ };
95
+
96
+ export const baseClasses: ReactZoomPanPinchBaseClasses = {
97
+ wrapperClass: "react-transform-wrapper",
98
+ contentClass: "react-transform-component",
99
+ };
@@ -0,0 +1,78 @@
1
+ /* eslint-disable no-plusplus */
2
+ /* eslint-disable no-param-reassign */
3
+ /**
4
+ * Functions should return denominator of the target value, which is the next animation step.
5
+ * t is a value from 0 to 1, reflecting the percentage of animation status.
6
+ */
7
+ const easeOut = (t: number): number => {
8
+ return -Math.cos(t * Math.PI) / 2 + 0.5;
9
+ };
10
+ // linear
11
+ const linear = (t: number): number => {
12
+ return t;
13
+ };
14
+ // accelerating from zero velocity
15
+ const easeInQuad = (t: number): number => {
16
+ return t * t;
17
+ };
18
+ // decelerating to zero velocity
19
+ const easeOutQuad = (t: number): number => {
20
+ return t * (2 - t);
21
+ };
22
+ // acceleration until halfway, then deceleration
23
+ const easeInOutQuad = (t: number): number => {
24
+ return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
25
+ };
26
+ // accelerating from zero velocity
27
+ const easeInCubic = (t: number): number => {
28
+ return t * t * t;
29
+ };
30
+ // decelerating to zero velocity
31
+ const easeOutCubic = (t: number): number => {
32
+ return --t * t * t + 1;
33
+ };
34
+ // acceleration until halfway, then deceleration
35
+ const easeInOutCubic = (t: number): number => {
36
+ return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
37
+ };
38
+ // accelerating from zero velocity
39
+ const easeInQuart = (t: number): number => {
40
+ return t * t * t * t;
41
+ };
42
+ // decelerating to zero velocity
43
+ const easeOutQuart = (t: number): number => {
44
+ return 1 - --t * t * t * t;
45
+ };
46
+ // acceleration until halfway, then deceleration
47
+ const easeInOutQuart = (t: number): number => {
48
+ return t < 0.5 ? 8 * t * t * t * t : 1 - 8 * --t * t * t * t;
49
+ };
50
+ // accelerating from zero velocity
51
+ const easeInQuint = (t: number): number => {
52
+ return t * t * t * t * t;
53
+ };
54
+ // decelerating to zero velocity
55
+ const easeOutQuint = (t: number): number => {
56
+ return 1 + --t * t * t * t * t;
57
+ };
58
+ // acceleration until halfway, then deceleration
59
+ const easeInOutQuint = (t: number): number => {
60
+ return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * --t * t * t * t * t;
61
+ };
62
+
63
+ export const animations = {
64
+ easeOut,
65
+ linear,
66
+ easeInQuad,
67
+ easeOutQuad,
68
+ easeInOutQuad,
69
+ easeInCubic,
70
+ easeOutCubic,
71
+ easeInOutCubic,
72
+ easeInQuart,
73
+ easeOutQuart,
74
+ easeInOutQuart,
75
+ easeInQuint,
76
+ easeOutQuint,
77
+ easeInOutQuint,
78
+ };
@@ -0,0 +1,115 @@
1
+ /* eslint-disable no-param-reassign */
2
+ import { animations } from "./animations.constants";
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.isAnimating = 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 as keyof typeof animations];
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
+ function isValidTargetState(targetState: StateType): boolean {
64
+ const { scale, positionX, positionY } = targetState;
65
+
66
+ if (
67
+ Number.isNaN(scale) ||
68
+ Number.isNaN(positionX) ||
69
+ Number.isNaN(positionY)
70
+ ) {
71
+ return false;
72
+ }
73
+
74
+ return true;
75
+ }
76
+
77
+ export function animate(
78
+ contextInstance: ReactZoomPanPinchContext,
79
+ targetState: StateType,
80
+ animationTime: number,
81
+ animationName: string,
82
+ ): void {
83
+ const isValid = isValidTargetState(targetState);
84
+ if (!contextInstance.mounted || !isValid) return;
85
+
86
+ const { setState } = contextInstance;
87
+ const { scale, positionX, positionY } = contextInstance.state;
88
+
89
+ const scaleDiff = targetState.scale - scale;
90
+ const positionXDiff = targetState.positionX - positionX;
91
+ const positionYDiff = targetState.positionY - positionY;
92
+
93
+ if (animationTime === 0) {
94
+ setState(targetState.scale, targetState.positionX, targetState.positionY);
95
+ } else {
96
+ // animation start timestamp
97
+ handleSetupAnimation(
98
+ contextInstance,
99
+ animationName,
100
+ animationTime,
101
+ (step: number) => {
102
+ if (step !== 1) {
103
+ contextInstance.isAnimating = true;
104
+ } else {
105
+ contextInstance.isAnimating = false;
106
+ }
107
+ const newScale = scale + scaleDiff * step;
108
+ const newPositionX = positionX + positionXDiff * step;
109
+ const newPositionY = positionY + positionYDiff * step;
110
+
111
+ setState(newScale, newPositionX, newPositionY);
112
+ },
113
+ );
114
+ }
115
+ }
@@ -0,0 +1,8 @@
1
+ export type ComponentsSizesType = {
2
+ wrapperWidth: number;
3
+ wrapperHeight: number;
4
+ newContentWidth: number;
5
+ newDiffWidth: number;
6
+ newContentHeight: number;
7
+ newDiffHeight: number;
8
+ };