motion 14.11.0 → 14.12.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.
@@ -1204,9 +1204,9 @@
1204
1204
  */
1205
1205
  const safeMin = 0.001;
1206
1206
  /**
1207
- * Assumes zero initial velocity and the default mass: time-defined springs
1208
- * ignore inherited velocity and only resolve without valid physics. Returns
1209
- * NaN physics when the root search doesn't converge.
1207
+ * Assumes zero initial velocity and the default mass: duration springs start
1208
+ * from rest so they end exactly at their duration. Only resolves without
1209
+ * valid physics. Returns NaN physics when the root search doesn't converge.
1210
1210
  */
1211
1211
  function findSpring({ duration = defaultDuration, bounce = defaultBounce, }) {
1212
1212
  let envelope;
@@ -1351,11 +1351,12 @@
1351
1351
  const state = { done: false, value: origin };
1352
1352
  const { stiffness, damping, mass, duration, isResolvedFromDuration, isTimeDefined, } = getSpringOptions({ ...options });
1353
1353
  /**
1354
- * Time-defined springs ignore inherited velocity. Velocity from
1355
- * interrupted animations causes massive oscillation on small-range
1356
- * animations.
1354
+ * A duration spring must end exactly at its duration, and it's settled
1355
+ * from rest by then. Inherited velocity could leave it still moving, so
1356
+ * it starts from rest. Every other spring, visualDuration included, ends
1357
+ * when it comes to rest, so inherits velocity as its initial condition.
1357
1358
  */
1358
- const inheritVelocity = (velocity) => isTimeDefined ? 0 : -millisecondsToSeconds(velocity);
1359
+ const inheritVelocity = (velocity) => isResolvedFromDuration ? 0 : -millisecondsToSeconds(velocity);
1359
1360
  const dampingRatio = damping / (2 * Math.sqrt(stiffness * mass));
1360
1361
  const undampedAngularFreq = millisecondsToSeconds(Math.sqrt(stiffness / mass));
1361
1362
  const decay = dampingRatio * undampedAngularFreq;
@@ -1369,7 +1370,7 @@
1369
1370
  const s = {
1370
1371
  target,
1371
1372
  delta: target - origin,
1372
- velocity: inheritVelocity(options.velocity || 0) || 0,
1373
+ velocity: inheritVelocity(options.velocity) || 0,
1373
1374
  restSpeed: 0,
1374
1375
  restDelta: 0,
1375
1376
  };
@@ -1516,7 +1517,6 @@
1516
1517
  const easing = generateLinearEasing((progress) => generator.next(easingDuration * progress).value, easingDuration, 30);
1517
1518
  return easingDuration + "ms " + easing;
1518
1519
  },
1519
- toTransition: () => { },
1520
1520
  };
1521
1521
  return generator;
1522
1522
  }
@@ -1994,8 +1994,18 @@
1994
1994
  */
1995
1995
  this.stop = () => {
1996
1996
  const { motionValue } = this.options;
1997
- if (motionValue && motionValue.updatedAt !== time.now()) {
1998
- this.tick(time.now());
1997
+ const now = time.now();
1998
+ if (motionValue && motionValue.updatedAt !== now) {
1999
+ /**
2000
+ * Catch the value up to now, but measure its velocity from the
2001
+ * previous frame: marking it as updated now keeps the value's
2002
+ * previous-frame sample. This tick is usually less than a frame
2003
+ * after the last, and the animation's time is rounded to whole
2004
+ * milliseconds, so measured from the last frame alone the
2005
+ * velocity an interrupting animation inherits could read as 0,
2006
+ * or a multiple of the real one.
2007
+ */
2008
+ this.tick((motionValue.updatedAt = now));
1999
2009
  }
2000
2010
  this.isStopped = true;
2001
2011
  if (this.state === "idle")
@@ -12520,6 +12530,11 @@
12520
12530
  ? { min: side(elastic.min), max: side(elastic.max) }
12521
12531
  : { min: side(elastic), max: side(elastic) };
12522
12532
  };
12533
+ /**
12534
+ * Whether the gesture writes `x`/`y`: to follow the focal point, or to
12535
+ * keep an `origin` on the element in place.
12536
+ */
12537
+ const writesXY = ({ pan, origin }) => pan !== false || !!origin;
12523
12538
  const clampTo = (value, { min = -Infinity, max = Infinity }) => clamp(min, max, value);
12524
12539
  /**
12525
12540
  * The raw gesture value that elastic shows as `value`. A pinch that starts
@@ -12564,6 +12579,8 @@
12564
12579
  let values;
12565
12580
  let originX = 0;
12566
12581
  let originY = 0;
12582
+ let width = 0;
12583
+ let height = 0;
12567
12584
  let lastX = 0;
12568
12585
  let lastY = 0;
12569
12586
  let hasMoved = false;
@@ -12594,8 +12611,9 @@
12594
12611
  * centre (the default transform origin), so they don't move it.
12595
12612
  */
12596
12613
  const measure = () => {
12597
- const { left, top, width, height } = element.getBoundingClientRect();
12598
- const { scale, rotate, pan, elastic } = getOptions();
12614
+ const box = element.getBoundingClientRect();
12615
+ const { left, top } = box;
12616
+ const { scale, rotate, elastic } = getOptions();
12599
12617
  const [s, r, x, y] = (values = getValues());
12600
12618
  const resistance = resolveElastic(elastic);
12601
12619
  /**
@@ -12606,16 +12624,35 @@
12606
12624
  rotate !== false && r.stop();
12607
12625
  rawScale = unconstrain(s.get(), scale, resistance);
12608
12626
  rawRotate = unconstrain(r.get(), rotate, resistance);
12609
- if (pan !== false) {
12627
+ width = element.offsetWidth ?? box.width;
12628
+ height = element.offsetHeight ?? box.height;
12629
+ if (writesXY(options)) {
12610
12630
  x.stop();
12611
12631
  y.stop();
12612
- toPixels(x, element.offsetWidth ?? width);
12613
- toPixels(y, element.offsetHeight ?? height);
12632
+ toPixels(x, width);
12633
+ toPixels(y, height);
12614
12634
  }
12615
12635
  lastX = x.get();
12616
12636
  lastY = y.get();
12617
- originX = left + width / 2 - lastX;
12618
- originY = top + height / 2 - lastY;
12637
+ originX = left + box.width / 2 - lastX;
12638
+ originY = top + box.height / 2 - lastY;
12639
+ };
12640
+ /**
12641
+ * The point scale and rotation pivot around, in client coordinates:
12642
+ * the focal point, or `origin` on the element at scale `s`, rotation
12643
+ * `r` and offset `x`/`y`.
12644
+ */
12645
+ const pivot = (focal, s, r, x, y) => {
12646
+ const { origin } = options;
12647
+ if (typeof origin !== "object")
12648
+ return focal;
12649
+ const a = (r * Math.PI) / 180;
12650
+ const u = (origin.x - 0.5) * width * s;
12651
+ const v = (origin.y - 0.5) * height * s;
12652
+ return {
12653
+ x: originX + x + Math.cos(a) * u - Math.sin(a) * v,
12654
+ y: originY + y + Math.sin(a) * u + Math.cos(a) * v,
12655
+ };
12619
12656
  };
12620
12657
  const dragControls = () => elementDragControls.get(visualElementStore.get(element));
12621
12658
  /**
@@ -12624,7 +12661,7 @@
12624
12661
  * back to its origin with `snapToOrigin`.
12625
12662
  */
12626
12663
  const snap = () => {
12627
- const { scale: scaleOption, rotate: rotateOption, pan, snapToOrigin, transition, } = options;
12664
+ const { scale: scaleOption, rotate: rotateOption, snapToOrigin, transition, } = options;
12628
12665
  const [scale, rotate, x, y] = values;
12629
12666
  const s0 = scale.get();
12630
12667
  const r0 = rotate.get();
@@ -12632,7 +12669,14 @@
12632
12669
  const y0 = y.get();
12633
12670
  const { snap: step } = limitsOf(rotateOption);
12634
12671
  let s1 = clampTo(s0, limitsOf(scaleOption));
12635
- let r1 = clampTo(step ? Math.round(r0 / step) * step : r0, limitsOf(rotateOption));
12672
+ /**
12673
+ * Snap to the step nearest where the release velocity would carry
12674
+ * the rotation, found as inertia finds its target (with a lower power
12675
+ * than inertia's 0.8).
12676
+ */
12677
+ let r1 = clampTo(step
12678
+ ? Math.round((r0 + 0.2 * rotate.getVelocity()) / step) * step
12679
+ : r0, limitsOf(rotateOption));
12636
12680
  if (snapToOrigin) {
12637
12681
  s1 = 1;
12638
12682
  // The nearest upright angle, so it turns the shortest way
@@ -12642,9 +12686,27 @@
12642
12686
  * Keep the focal point in place, unless the element doesn't move
12643
12687
  * with it, or a drag carries on and owns x/y.
12644
12688
  */
12645
- const moves = pan !== false && !dragControls()?.isDragging;
12646
- const vx = originX + x0 - current.x;
12647
- const vy = originY + y0 - current.y;
12689
+ const moves = writesXY(options) && !dragControls()?.isDragging;
12690
+ const p0 = pivot(current, s0, r0, x0, y0);
12691
+ const vx = originX + x0 - p0.x;
12692
+ const vy = originY + y0 - p0.y;
12693
+ /**
12694
+ * Scale, rotation and x/y at progress `p` of the snap, keeping the
12695
+ * focal point in place.
12696
+ */
12697
+ const at = (p) => {
12698
+ const k = mixNumber$1(s0, s1, p);
12699
+ const r = mixNumber$1(r0, r1, p);
12700
+ const angle = ((r - r0) * Math.PI) / 180;
12701
+ const cos = (Math.cos(angle) * k) / s0;
12702
+ const sin = (Math.sin(angle) * k) / s0;
12703
+ return [
12704
+ k,
12705
+ r,
12706
+ x0 + cos * vx - sin * vy - vx,
12707
+ y0 + sin * vx + cos * vy - vy,
12708
+ ];
12709
+ };
12648
12710
  if (!s0 ||
12649
12711
  (s1 === s0 && r1 === r0 && !(snapToOrigin && moves && (x0 || y0)))) {
12650
12712
  return;
@@ -12666,20 +12728,12 @@
12666
12728
  const { value: p, done } = generator.next(time.now() - startTime);
12667
12729
  if (done)
12668
12730
  stopSnap();
12669
- const k = mixNumber$1(s0, s1, p);
12670
- const r = mixNumber$1(r0, r1, p);
12671
- const angle = ((r - r0) * Math.PI) / 180;
12672
- const cos = (Math.cos(angle) * k) / s0;
12673
- const sin = (Math.sin(angle) * k) / s0;
12731
+ const [k, r, fx, fy] = at(p);
12674
12732
  scaleOption !== false && scale.set(k);
12675
12733
  rotateOption !== false && rotate.set(r);
12676
12734
  if (moves) {
12677
- x.set(snapToOrigin
12678
- ? mixNumber$1(x0, 0, p)
12679
- : x0 + cos * vx - sin * vy - vx);
12680
- y.set(snapToOrigin
12681
- ? mixNumber$1(y0, 0, p)
12682
- : y0 + sin * vx + cos * vy - vy);
12735
+ x.set(snapToOrigin ? mixNumber$1(x0, 0, p) : fx);
12736
+ y.set(snapToOrigin ? mixNumber$1(y0, 0, p) : fy);
12683
12737
  }
12684
12738
  };
12685
12739
  stopSnap = () => cancelFrame(tick);
@@ -12698,6 +12752,13 @@
12698
12752
  const [scale, rotate, x, y] = values;
12699
12753
  const resistance = resolveElastic(elastic);
12700
12754
  const latest = scale.get();
12755
+ const drag = dragControls();
12756
+ const isDragging = drag?.isDragging;
12757
+ if (!isDragging) {
12758
+ lastX = x.get();
12759
+ lastY = y.get();
12760
+ }
12761
+ const p = pivot(prev, latest, rotate.get(), lastX, lastY);
12701
12762
  let theta = 0;
12702
12763
  let k = 1;
12703
12764
  /**
@@ -12725,7 +12786,7 @@
12725
12786
  if (theta)
12726
12787
  rotate.set(next);
12727
12788
  }
12728
- if (pan !== false) {
12789
+ if (writesXY(options)) {
12729
12790
  /**
12730
12791
  * Pinch owns x/y while it runs, so stop any animation started
12731
12792
  * on them since (like a drag's snap back) taking over after.
@@ -12741,23 +12802,19 @@
12741
12802
  * element, and its release velocity only counts movement after
12742
12803
  * the pinch, so a zoom doesn't end in a fling.
12743
12804
  */
12744
- const drag = dragControls();
12745
- const isDragging = drag?.isDragging;
12746
- if (!isDragging) {
12747
- lastX = x.get();
12748
- lastY = y.get();
12749
- }
12750
12805
  const angle = (theta * Math.PI) / 180;
12751
12806
  const cos = Math.cos(angle) * k;
12752
12807
  const sin = Math.sin(angle) * k;
12753
- const dx = prev.x - originX - lastX;
12754
- const dy = prev.y - originY - lastY;
12808
+ const dx = p.x - originX - lastX;
12809
+ const dy = p.y - originY - lastY;
12810
+ const panX = pan !== false ? current.x - prev.x : 0;
12811
+ const panY = pan !== false ? current.y - prev.y : 0;
12755
12812
  /**
12756
- * Move the centre so the previous focal point, scaled and
12757
- * rotated around the centre, lands on the current focal point.
12813
+ * Move the centre so the pivot, scaled and rotated around the
12814
+ * centre, stays in place, plus the focal point's movement.
12758
12815
  */
12759
- lastX += dx + current.x - prev.x - (cos * dx - sin * dy);
12760
- lastY += dy + current.y - prev.y - (sin * dx + cos * dy);
12816
+ lastX += dx + panX - (cos * dx - sin * dy);
12817
+ lastY += dy + panY - (sin * dx + cos * dy);
12761
12818
  if (isDragging) {
12762
12819
  const history = drag.panSession.history;
12763
12820
  history.splice(0, history.length - 1);