react-zoom-pan-pinch 3.1.1 → 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.
package/dist/index.cjs.js CHANGED
@@ -224,6 +224,28 @@ var calculateBounds = function (contextInstance, newScale) {
224
224
  var bounds = getBounds(wrapperWidth, newContentWidth, newDiffWidth, wrapperHeight, newContentHeight, newDiffHeight, Boolean(centerZoomedOut));
225
225
  return bounds;
226
226
  };
227
+ function clamp(v, min, max) {
228
+ return Math.max(min, Math.min(v, max));
229
+ }
230
+ // Based on @aholachek ;)
231
+ // https://twitter.com/chpwn/status/285540192096497664
232
+ // iOS constant = 0.55
233
+ // https://medium.com/@nathangitter/building-fluid-interfaces-ios-swift-9732bb934bf5
234
+ function rubberband(distance, dimension, constant) {
235
+ if (dimension === 0 || Math.abs(dimension) === Infinity)
236
+ return Math.pow(distance, (constant * 5));
237
+ return (distance * dimension * constant) / (dimension + constant * distance);
238
+ }
239
+ function rubberbandIfOutOfBounds(position, min, max, constant) {
240
+ if (constant === void 0) { constant = 0.15; }
241
+ if (constant === 0)
242
+ return clamp(position, min, max);
243
+ if (position < min)
244
+ return -rubberband(min - position, max - min, constant) + min;
245
+ if (position > max)
246
+ return +rubberband(position - max, max - min, constant) + max;
247
+ return position;
248
+ }
227
249
  /**
228
250
  * Keeps value between given bounds, used for limiting view to given boundaries
229
251
  * 1# eg. boundLimiter(2, 0, 3, true) => 2
@@ -254,8 +276,10 @@ function getMouseBoundedPosition(positionX, positionY, bounds, limitToBounds, pa
254
276
  paddingX = paddingValueX;
255
277
  paddingY = paddingValueY;
256
278
  }
257
- var x = boundLimiter(positionX, minPositionX - paddingX, maxPositionX + paddingX, limitToBounds);
258
- var y = boundLimiter(positionY, minPositionY - paddingY, maxPositionY + paddingY, limitToBounds);
279
+ var rubberbandX = rubberbandIfOutOfBounds(positionX, minPositionX, maxPositionX);
280
+ var rubberbandY = rubberbandIfOutOfBounds(positionY, minPositionY, maxPositionY);
281
+ var x = boundLimiter(rubberbandX, minPositionX - paddingX, maxPositionX + paddingX, limitToBounds);
282
+ var y = boundLimiter(rubberbandY, minPositionY - paddingY, maxPositionY + paddingY, limitToBounds);
259
283
  return { x: x, y: y };
260
284
  }
261
285