motion-dom 14.11.0 → 14.11.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/dist/cjs/index.js CHANGED
@@ -12219,6 +12219,11 @@ const resolveElastic = (elastic = defaultElastic) => {
12219
12219
  ? { min: side(elastic.min), max: side(elastic.max) }
12220
12220
  : { min: side(elastic), max: side(elastic) };
12221
12221
  };
12222
+ /**
12223
+ * Whether the gesture writes `x`/`y`: to follow the focal point, or to
12224
+ * keep an `origin` on the element in place.
12225
+ */
12226
+ const writesXY = ({ pan, origin }) => pan !== false || !!origin;
12222
12227
  const clampTo = (value, { min = -Infinity, max = Infinity }) => motionUtils.clamp(min, max, value);
12223
12228
  /**
12224
12229
  * The raw gesture value that elastic shows as `value`. A pinch that starts
@@ -12263,6 +12268,8 @@ function addPinchListeners(element, getValues, getEnabledOptions, onPinchStart)
12263
12268
  let values;
12264
12269
  let originX = 0;
12265
12270
  let originY = 0;
12271
+ let width = 0;
12272
+ let height = 0;
12266
12273
  let lastX = 0;
12267
12274
  let lastY = 0;
12268
12275
  let hasMoved = false;
@@ -12293,8 +12300,9 @@ function addPinchListeners(element, getValues, getEnabledOptions, onPinchStart)
12293
12300
  * centre (the default transform origin), so they don't move it.
12294
12301
  */
12295
12302
  const measure = () => {
12296
- const { left, top, width, height } = element.getBoundingClientRect();
12297
- const { scale, rotate, pan, elastic } = getOptions();
12303
+ const box = element.getBoundingClientRect();
12304
+ const { left, top } = box;
12305
+ const { scale, rotate, elastic } = getOptions();
12298
12306
  const [s, r, x, y] = (values = getValues());
12299
12307
  const resistance = resolveElastic(elastic);
12300
12308
  /**
@@ -12305,16 +12313,35 @@ function addPinchListeners(element, getValues, getEnabledOptions, onPinchStart)
12305
12313
  rotate !== false && r.stop();
12306
12314
  rawScale = unconstrain(s.get(), scale, resistance);
12307
12315
  rawRotate = unconstrain(r.get(), rotate, resistance);
12308
- if (pan !== false) {
12316
+ width = element.offsetWidth ?? box.width;
12317
+ height = element.offsetHeight ?? box.height;
12318
+ if (writesXY(options)) {
12309
12319
  x.stop();
12310
12320
  y.stop();
12311
- toPixels(x, element.offsetWidth ?? width);
12312
- toPixels(y, element.offsetHeight ?? height);
12321
+ toPixels(x, width);
12322
+ toPixels(y, height);
12313
12323
  }
12314
12324
  lastX = x.get();
12315
12325
  lastY = y.get();
12316
- originX = left + width / 2 - lastX;
12317
- originY = top + height / 2 - lastY;
12326
+ originX = left + box.width / 2 - lastX;
12327
+ originY = top + box.height / 2 - lastY;
12328
+ };
12329
+ /**
12330
+ * The point scale and rotation pivot around, in client coordinates:
12331
+ * the focal point, or `origin` on the element at scale `s`, rotation
12332
+ * `r` and offset `x`/`y`.
12333
+ */
12334
+ const pivot = (focal, s, r, x, y) => {
12335
+ const { origin } = options;
12336
+ if (typeof origin !== "object")
12337
+ return focal;
12338
+ const a = (r * Math.PI) / 180;
12339
+ const u = (origin.x - 0.5) * width * s;
12340
+ const v = (origin.y - 0.5) * height * s;
12341
+ return {
12342
+ x: originX + x + Math.cos(a) * u - Math.sin(a) * v,
12343
+ y: originY + y + Math.sin(a) * u + Math.cos(a) * v,
12344
+ };
12318
12345
  };
12319
12346
  const dragControls = () => elementDragControls.get(visualElementStore.get(element));
12320
12347
  /**
@@ -12323,7 +12350,7 @@ function addPinchListeners(element, getValues, getEnabledOptions, onPinchStart)
12323
12350
  * back to its origin with `snapToOrigin`.
12324
12351
  */
12325
12352
  const snap = () => {
12326
- const { scale: scaleOption, rotate: rotateOption, pan, snapToOrigin, transition, } = options;
12353
+ const { scale: scaleOption, rotate: rotateOption, snapToOrigin, transition, } = options;
12327
12354
  const [scale, rotate, x, y] = values;
12328
12355
  const s0 = scale.get();
12329
12356
  const r0 = rotate.get();
@@ -12331,7 +12358,14 @@ function addPinchListeners(element, getValues, getEnabledOptions, onPinchStart)
12331
12358
  const y0 = y.get();
12332
12359
  const { snap: step } = limitsOf(rotateOption);
12333
12360
  let s1 = clampTo(s0, limitsOf(scaleOption));
12334
- let r1 = clampTo(step ? Math.round(r0 / step) * step : r0, limitsOf(rotateOption));
12361
+ /**
12362
+ * Snap to the step nearest where the release velocity would carry
12363
+ * the rotation, found as inertia finds its target (with a lower power
12364
+ * than inertia's 0.8).
12365
+ */
12366
+ let r1 = clampTo(step
12367
+ ? Math.round((r0 + 0.2 * rotate.getVelocity()) / step) * step
12368
+ : r0, limitsOf(rotateOption));
12335
12369
  if (snapToOrigin) {
12336
12370
  s1 = 1;
12337
12371
  // The nearest upright angle, so it turns the shortest way
@@ -12341,9 +12375,27 @@ function addPinchListeners(element, getValues, getEnabledOptions, onPinchStart)
12341
12375
  * Keep the focal point in place, unless the element doesn't move
12342
12376
  * with it, or a drag carries on and owns x/y.
12343
12377
  */
12344
- const moves = pan !== false && !dragControls()?.isDragging;
12345
- const vx = originX + x0 - current.x;
12346
- const vy = originY + y0 - current.y;
12378
+ const moves = writesXY(options) && !dragControls()?.isDragging;
12379
+ const p0 = pivot(current, s0, r0, x0, y0);
12380
+ const vx = originX + x0 - p0.x;
12381
+ const vy = originY + y0 - p0.y;
12382
+ /**
12383
+ * Scale, rotation and x/y at progress `p` of the snap, keeping the
12384
+ * focal point in place.
12385
+ */
12386
+ const at = (p) => {
12387
+ const k = mixNumber$1(s0, s1, p);
12388
+ const r = mixNumber$1(r0, r1, p);
12389
+ const angle = ((r - r0) * Math.PI) / 180;
12390
+ const cos = (Math.cos(angle) * k) / s0;
12391
+ const sin = (Math.sin(angle) * k) / s0;
12392
+ return [
12393
+ k,
12394
+ r,
12395
+ x0 + cos * vx - sin * vy - vx,
12396
+ y0 + sin * vx + cos * vy - vy,
12397
+ ];
12398
+ };
12347
12399
  if (!s0 ||
12348
12400
  (s1 === s0 && r1 === r0 && !(snapToOrigin && moves && (x0 || y0)))) {
12349
12401
  return;
@@ -12365,20 +12417,12 @@ function addPinchListeners(element, getValues, getEnabledOptions, onPinchStart)
12365
12417
  const { value: p, done } = generator.next(time.now() - startTime);
12366
12418
  if (done)
12367
12419
  stopSnap();
12368
- const k = mixNumber$1(s0, s1, p);
12369
- const r = mixNumber$1(r0, r1, p);
12370
- const angle = ((r - r0) * Math.PI) / 180;
12371
- const cos = (Math.cos(angle) * k) / s0;
12372
- const sin = (Math.sin(angle) * k) / s0;
12420
+ const [k, r, fx, fy] = at(p);
12373
12421
  scaleOption !== false && scale.set(k);
12374
12422
  rotateOption !== false && rotate.set(r);
12375
12423
  if (moves) {
12376
- x.set(snapToOrigin
12377
- ? mixNumber$1(x0, 0, p)
12378
- : x0 + cos * vx - sin * vy - vx);
12379
- y.set(snapToOrigin
12380
- ? mixNumber$1(y0, 0, p)
12381
- : y0 + sin * vx + cos * vy - vy);
12424
+ x.set(snapToOrigin ? mixNumber$1(x0, 0, p) : fx);
12425
+ y.set(snapToOrigin ? mixNumber$1(y0, 0, p) : fy);
12382
12426
  }
12383
12427
  };
12384
12428
  stopSnap = () => cancelFrame(tick);
@@ -12397,6 +12441,13 @@ function addPinchListeners(element, getValues, getEnabledOptions, onPinchStart)
12397
12441
  const [scale, rotate, x, y] = values;
12398
12442
  const resistance = resolveElastic(elastic);
12399
12443
  const latest = scale.get();
12444
+ const drag = dragControls();
12445
+ const isDragging = drag?.isDragging;
12446
+ if (!isDragging) {
12447
+ lastX = x.get();
12448
+ lastY = y.get();
12449
+ }
12450
+ const p = pivot(prev, latest, rotate.get(), lastX, lastY);
12400
12451
  let theta = 0;
12401
12452
  let k = 1;
12402
12453
  /**
@@ -12424,7 +12475,7 @@ function addPinchListeners(element, getValues, getEnabledOptions, onPinchStart)
12424
12475
  if (theta)
12425
12476
  rotate.set(next);
12426
12477
  }
12427
- if (pan !== false) {
12478
+ if (writesXY(options)) {
12428
12479
  /**
12429
12480
  * Pinch owns x/y while it runs, so stop any animation started
12430
12481
  * on them since (like a drag's snap back) taking over after.
@@ -12440,23 +12491,19 @@ function addPinchListeners(element, getValues, getEnabledOptions, onPinchStart)
12440
12491
  * element, and its release velocity only counts movement after
12441
12492
  * the pinch, so a zoom doesn't end in a fling.
12442
12493
  */
12443
- const drag = dragControls();
12444
- const isDragging = drag?.isDragging;
12445
- if (!isDragging) {
12446
- lastX = x.get();
12447
- lastY = y.get();
12448
- }
12449
12494
  const angle = (theta * Math.PI) / 180;
12450
12495
  const cos = Math.cos(angle) * k;
12451
12496
  const sin = Math.sin(angle) * k;
12452
- const dx = prev.x - originX - lastX;
12453
- const dy = prev.y - originY - lastY;
12497
+ const dx = p.x - originX - lastX;
12498
+ const dy = p.y - originY - lastY;
12499
+ const panX = pan !== false ? current.x - prev.x : 0;
12500
+ const panY = pan !== false ? current.y - prev.y : 0;
12454
12501
  /**
12455
- * Move the centre so the previous focal point, scaled and
12456
- * rotated around the centre, lands on the current focal point.
12502
+ * Move the centre so the pivot, scaled and rotated around the
12503
+ * centre, stays in place, plus the focal point's movement.
12457
12504
  */
12458
- lastX += dx + current.x - prev.x - (cos * dx - sin * dy);
12459
- lastY += dy + current.y - prev.y - (sin * dx + cos * dy);
12505
+ lastX += dx + panX - (cos * dx - sin * dy);
12506
+ lastY += dy + panY - (sin * dx + cos * dy);
12460
12507
  if (isDragging) {
12461
12508
  const history = drag.panSession.history;
12462
12509
  history.splice(0, history.length - 1);