react-zoom-pan-pinch 3.1.0 → 3.2.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.
@@ -3,6 +3,8 @@ import { ComponentsSizesType } from "./bounds.types";
3
3
  export declare function getComponentsSizes(wrapperComponent: HTMLDivElement, contentComponent: HTMLDivElement, newScale: number): ComponentsSizesType;
4
4
  export declare const getBounds: (wrapperWidth: number, newContentWidth: number, diffWidth: number, wrapperHeight: number, newContentHeight: number, diffHeight: number, centerZoomedOut: boolean) => BoundsType;
5
5
  export declare const calculateBounds: (contextInstance: ReactZoomPanPinchContext, newScale: number) => BoundsType;
6
+ export declare function clamp(v: number, min: number, max: number): number;
7
+ export declare function rubberbandIfOutOfBounds(position: number, min: number, max: number, constant?: number): number;
6
8
  /**
7
9
  * Keeps value between given bounds, used for limiting view to given boundaries
8
10
  * 1# eg. boundLimiter(2, 0, 3, true) => 2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-zoom-pan-pinch",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "Zoom and pan html elements in easy way",
5
5
  "author": "prc5",
6
6
  "license": "MIT",
@@ -32,6 +32,9 @@ export const TransformWrapper = React.forwardRef(
32
32
 
33
33
  useEffect(() => {
34
34
  instance.update(props);
35
+ return () => {
36
+ instance.cleanupWindowEvents();
37
+ };
35
38
  }, [instance, props]);
36
39
 
37
40
  return <Context.Provider value={instance}>{content}</Context.Provider>;
@@ -91,6 +91,36 @@ export const calculateBounds = (
91
91
  return bounds;
92
92
  };
93
93
 
94
+ export function clamp(v: number, min: number, max: number) {
95
+ return Math.max(min, Math.min(v, max));
96
+ }
97
+
98
+ // Based on @aholachek ;)
99
+ // https://twitter.com/chpwn/status/285540192096497664
100
+ // iOS constant = 0.55
101
+
102
+ // https://medium.com/@nathangitter/building-fluid-interfaces-ios-swift-9732bb934bf5
103
+
104
+ function rubberband(distance: number, dimension: number, constant: number) {
105
+ if (dimension === 0 || Math.abs(dimension) === Infinity)
106
+ return distance ** (constant * 5);
107
+ return (distance * dimension * constant) / (dimension + constant * distance);
108
+ }
109
+
110
+ export function rubberbandIfOutOfBounds(
111
+ position: number,
112
+ min: number,
113
+ max: number,
114
+ constant = 0.15,
115
+ ) {
116
+ if (constant === 0) return clamp(position, min, max);
117
+ if (position < min)
118
+ return -rubberband(min - position, max - min, constant) + min;
119
+ if (position > max)
120
+ return +rubberband(position - max, max - min, constant) + max;
121
+ return position;
122
+ }
123
+
94
124
  /**
95
125
  * Keeps value between given bounds, used for limiting view to given boundaries
96
126
  * 1# eg. boundLimiter(2, 0, 3, true) => 2
@@ -140,15 +170,26 @@ export function getMouseBoundedPosition(
140
170
  paddingY = paddingValueY;
141
171
  }
142
172
 
143
- const x = boundLimiter(
173
+ const rubberbandX = rubberbandIfOutOfBounds(
144
174
  positionX,
175
+ minPositionX,
176
+ maxPositionX,
177
+ );
178
+ const rubberbandY = rubberbandIfOutOfBounds(
179
+ positionY,
180
+ minPositionY,
181
+ maxPositionY,
182
+ );
183
+
184
+ const x = boundLimiter(
185
+ rubberbandX,
145
186
  minPositionX - paddingX,
146
187
  maxPositionX + paddingX,
147
188
  limitToBounds,
148
189
  );
149
190
 
150
191
  const y = boundLimiter(
151
- positionY,
192
+ rubberbandY,
152
193
  minPositionY - paddingY,
153
194
  maxPositionY + paddingY,
154
195
  limitToBounds,
@@ -492,7 +492,7 @@ export const wrapperPropsTable: ComponentProps = {
492
492
  },
493
493
  onTransformed: {
494
494
  type: [
495
- "(ref: ReactZoomPanPinchRef, scale: number, positionX: number, positionY: number ) => void",
495
+ "(ref: ReactZoomPanPinchRef, state: { scale: number; positionX: number; positionY: number } ) => void",
496
496
  ],
497
497
  defaultValue: "undefined",
498
498
  description: "Callback fired when on each transform",