motion 14.11.1 → 14.13.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;
@@ -1283,7 +1283,7 @@
1283
1283
  if (isValidPhysics(value, canBeZero))
1284
1284
  return value;
1285
1285
  {
1286
- exports.warning(value === undefined, "Spring stiffness and mass must be positive, damping 0 or greater", "spring-invalid-physics");
1286
+ exports.warning(value === undefined || value === "auto", "Spring stiffness and mass must be positive, damping 0 or greater", "spring-invalid-physics");
1287
1287
  }
1288
1288
  return undefined;
1289
1289
  }
@@ -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")
@@ -7612,6 +7622,29 @@
7612
7622
  };
7613
7623
  }
7614
7624
 
7625
+ /**
7626
+ * A curved path ties x and y to one progress value, so it takes the part of
7627
+ * the velocity along the straight line from start to end.
7628
+ */
7629
+ const pointToProgressVelocity = (tx, ty, { x, y }) => {
7630
+ const lengthSquared = tx * tx + ty * ty;
7631
+ return lengthSquared < 1 ? 0 : (-1e3 * (x * tx + y * ty)) / lengthSquared;
7632
+ };
7633
+ /**
7634
+ * With `mass: "auto"`, mass comes from the element's size: a 300px square
7635
+ * has mass 1, bigger elements are heavier and slower (mass 0.25 to 2), and
7636
+ * damping scales with mass so the spring keeps its bounce. The result is a
7637
+ * physics spring.
7638
+ */
7639
+ const resolveAutoMass = (t, size = 300) => {
7640
+ const mass = clamp(0.25, 2, size / 300);
7641
+ const { stiffness, damping } = getSpringOptions({
7642
+ ...t,
7643
+ duration: t.duration && secondsToMilliseconds(t.duration),
7644
+ });
7645
+ return { ...t, stiffness, damping: damping * Math.sqrt(mass), mass };
7646
+ };
7647
+
7615
7648
  /**
7616
7649
  * Reset an axis to the provided origin box.
7617
7650
  *
@@ -8707,6 +8740,37 @@
8707
8740
  !calcLength(this.snapshot.measuredBox.y)) {
8708
8741
  this.snapshot = undefined;
8709
8742
  }
8743
+ /**
8744
+ * Record how fast the element is moving when it's measured, so
8745
+ * the next layout animation can start at the same speed.
8746
+ */
8747
+ if (this.snapshot) {
8748
+ const { visualElement } = this.options;
8749
+ /**
8750
+ * A paused or stopped animation hands on no velocity.
8751
+ */
8752
+ const isRunning = this.currentAnimation?.state === "running";
8753
+ const edges = isRunning && this.animationEdges;
8754
+ /**
8755
+ * A child moving inside its parent hands on only its
8756
+ * velocity relative to the parent.
8757
+ */
8758
+ if (isRunning && this.relativeTarget) {
8759
+ this.snapshot.relativeVelocity =
8760
+ this.relativeVelocity?.slice() || [];
8761
+ }
8762
+ const progress = (edges && this.motionValue?.getVelocity()) || 0;
8763
+ const velocity = [0, 1, 2, 3, 4, 5].map((i) => (i < 4
8764
+ ? edges &&
8765
+ (this.edgeValues
8766
+ ? this.edgeVelocity(i)
8767
+ : (-edges[i] * progress) / 1000)
8768
+ : visualElement
8769
+ ?.getValue(i < 5 ? "x" : "y")
8770
+ ?.getVelocity()) || 0);
8771
+ if (velocity.some(Boolean))
8772
+ this.snapshot.velocity = velocity;
8773
+ }
8710
8774
  }
8711
8775
  updateLayout() {
8712
8776
  if (!this.instance)
@@ -9232,11 +9296,62 @@
9232
9296
  // The path decides whether the layout shift is worth curving
9233
9297
  // (distance floor) and resolves the interpolator from the delta.
9234
9298
  const interpolate = pathFn?.interpolateProjection(delta);
9299
+ const box = this.layout?.layoutBox;
9300
+ let edges = box &&
9301
+ [box.x.min, box.x.max, box.y.min, box.y.max].map((edge, i) => {
9302
+ const { translate, scale, originPoint } = delta[i < 2 ? "x" : "y"];
9303
+ return (edge - originPoint) * (scale - 1) + translate;
9304
+ });
9305
+ /**
9306
+ * A zero-length layout axis has no finite edge offsets.
9307
+ */
9308
+ if (!edges?.every(isFinite))
9309
+ edges = undefined;
9310
+ this.animationEdges = edges;
9311
+ this.edgeValues =
9312
+ this.startVelocity =
9313
+ this.impulse =
9314
+ this.relativeVelocity =
9315
+ this.relativeStart =
9316
+ undefined;
9317
+ /**
9318
+ * A shared snapshot is measured with the old element's own
9319
+ * transforms (like x/y from drag), so it hands their velocity on
9320
+ * too. Otherwise those values keep animating themselves.
9321
+ */
9322
+ const velocity = snapshot?.velocity;
9323
+ /**
9324
+ * A child that wasn't moving inside an animating parent before
9325
+ * moved in page space, so that is its relative velocity.
9326
+ */
9327
+ const relative = snapshot?.relativeVelocity || velocity;
9328
+ if (relative?.slice(0, 4).some(Boolean) &&
9329
+ this.getClosestProjectingParent()) {
9330
+ this.relativeStart = relative;
9331
+ }
9332
+ if (velocity && edges) {
9333
+ const v = velocity
9334
+ .slice(0, 4)
9335
+ .map((edge, i) => edge +
9336
+ (isSharedLayoutAnimation
9337
+ ? velocity[4 + (i >> 1)]
9338
+ : 0));
9339
+ if (v.some(Boolean)) {
9340
+ this.startVelocity = interpolate
9341
+ ? pointToProgressVelocity(delta.x.translate, delta.y.translate, { x: (v[0] + v[1]) / 2, y: (v[2] + v[3]) / 2 })
9342
+ : v;
9343
+ }
9344
+ }
9235
9345
  this.mixTargetDelta = (latest) => {
9236
9346
  const progress = latest / 1000;
9237
9347
  const point = interpolate?.(progress);
9238
- mixAxisDeltaLinear(targetDelta.x, delta.x, progress);
9239
- mixAxisDeltaLinear(targetDelta.y, delta.y, progress);
9348
+ const values = this.edgeValues;
9349
+ for (let i = 0; i < 2; i++) {
9350
+ const axis = i ? "y" : "x";
9351
+ values
9352
+ ? edgesToDelta(targetDelta[axis], delta[axis], this.layout.layoutBox[axis], values[i * 2], values[i * 2 + 1])
9353
+ : mixAxisDeltaLinear(targetDelta[axis], delta[axis], progress);
9354
+ }
9240
9355
  if (point) {
9241
9356
  targetDelta.x.translate = point.x;
9242
9357
  targetDelta.y.translate = point.y;
@@ -9248,7 +9363,41 @@
9248
9363
  this.relativeParent &&
9249
9364
  this.relativeParent.layout) {
9250
9365
  calcRelativePosition(relativeLayout, this.layout.layoutBox, this.relativeParent.layout.layoutBox, this.options.layoutAnchor || undefined);
9251
- mixBox(this.relativeTarget, this.relativeTargetOrigin, relativeLayout, progress);
9366
+ const from = this.relativeTargetOrigin;
9367
+ const to = relativeLayout;
9368
+ /**
9369
+ * A child mixes its place in its parent by progress,
9370
+ * so a nested tree moves as one.
9371
+ */
9372
+ mixBox(this.relativeTarget, from, to, progress);
9373
+ /**
9374
+ * Springs are linear, so the child's own velocity adds
9375
+ * the impulse response on top of the mix.
9376
+ */
9377
+ /**
9378
+ * Only springs take velocity, so a tween skips this.
9379
+ */
9380
+ if (this.isSpringLayout) {
9381
+ const box = this.relativeTarget;
9382
+ const impulse = this.impulse || 0;
9383
+ const start = this.relativeStart;
9384
+ const now = time.now();
9385
+ const dt = (now - this.relativeTime) / 1000;
9386
+ this.relativeTime = now;
9387
+ const speeds = (this.relativeVelocity || (this.relativeVelocity = [0, 0, 0, 0]));
9388
+ for (let i = 0; i < 4; i++) {
9389
+ const axis = i < 2 ? "x" : "y";
9390
+ const edge = i & 1 ? "max" : "min";
9391
+ if (start)
9392
+ box[axis][edge] += start[i] * impulse;
9393
+ if (prevRelativeTarget && dt > 0) {
9394
+ speeds[i] =
9395
+ (box[axis][edge] -
9396
+ prevRelativeTarget[axis][edge]) /
9397
+ dt;
9398
+ }
9399
+ }
9400
+ }
9252
9401
  /**
9253
9402
  * If this is an unchanged relative target we can consider the
9254
9403
  * projection not dirty.
@@ -9280,8 +9429,11 @@
9280
9429
  }
9281
9430
  startAnimation(options) {
9282
9431
  this.notifyListeners("animationStart");
9432
+ const isSpring = (this.isSpringLayout = options.type === "spring");
9283
9433
  this.currentAnimation?.stop();
9284
9434
  this.resumingFrom?.currentAnimation?.stop();
9435
+ if (this.resumingFrom)
9436
+ this.resumingFrom.edgeValues = undefined;
9285
9437
  if (this.pendingAnimation) {
9286
9438
  cancelFrame(this.pendingAnimation);
9287
9439
  this.pendingAnimation = undefined;
@@ -9295,9 +9447,114 @@
9295
9447
  globalProjectionState.hasAnimatedSinceResize = true;
9296
9448
  this.motionValue || (this.motionValue = motionValue(0));
9297
9449
  this.motionValue.jump(0, false);
9298
- this.currentAnimation = animateSingleValue(this.motionValue, [0, 1000], {
9299
- ...options,
9300
- velocity: 0,
9450
+ /**
9451
+ * A duration spring ignores velocity, like in animate(), so
9452
+ * it ends on time.
9453
+ */
9454
+ const velocity = this.startVelocity;
9455
+ this.startVelocity = undefined;
9456
+ let transition = options;
9457
+ let mainVelocity = 0;
9458
+ if (isSpring) {
9459
+ /**
9460
+ * With mass: "auto", mass comes from the element's own
9461
+ * size.
9462
+ */
9463
+ if (options.mass === "auto") {
9464
+ const box = this.layout?.layoutBox;
9465
+ transition = resolveAutoMass(options, box &&
9466
+ Math.sqrt(calcLength(box.x) * calcLength(box.y)));
9467
+ }
9468
+ /**
9469
+ * Only springs take a velocity. A tween keeps one
9470
+ * driver, so its controls (pause, time) reach it all.
9471
+ */
9472
+ const relative = this.relativeStart;
9473
+ if ((velocity || relative) && !options.repeat) {
9474
+ if (typeof velocity === "number") {
9475
+ mainVelocity = velocity;
9476
+ }
9477
+ else {
9478
+ /**
9479
+ * Each edge (x min, x max, y min, y max) gets its
9480
+ * own spring in pixels, with its own start
9481
+ * velocity. An edge that doesn't move keeps
9482
+ * still, and a thrown element keeps its size.
9483
+ * One generator steps them all with the main
9484
+ * spring, so one animation drives them and its
9485
+ * controls reach them all.
9486
+ */
9487
+ const edges = velocity && this.animationEdges;
9488
+ transition = {
9489
+ ...transition,
9490
+ type: (o) => {
9491
+ const springs = [
9492
+ o.velocity,
9493
+ ...(velocity || []),
9494
+ ].map((v, i) => spring({
9495
+ ...o,
9496
+ keyframes: i
9497
+ ? [edges[i - 1], 0]
9498
+ : o.keyframes,
9499
+ velocity: v,
9500
+ }));
9501
+ const count = springs.length;
9502
+ /**
9503
+ * The response to 1000 px/s, for a
9504
+ * child's velocity inside its parent.
9505
+ * Coarse rest thresholds, because a
9506
+ * small offset of it is far below 1px.
9507
+ */
9508
+ relative &&
9509
+ springs.push(spring({
9510
+ ...o,
9511
+ keyframes: [0, 0],
9512
+ velocity: 1000,
9513
+ restDelta: 0.5,
9514
+ restSpeed: 5,
9515
+ }));
9516
+ let time = 0;
9517
+ const values = [];
9518
+ const generator = {
9519
+ calculatedDuration: null,
9520
+ next: (t) => {
9521
+ time = t;
9522
+ const state = springs[0].next(t);
9523
+ let done = state.done;
9524
+ for (let i = 1; i < springs.length; i++) {
9525
+ const { value, done: rest } = springs[i].next(t);
9526
+ if (i < count) {
9527
+ values[i - 1] = value;
9528
+ }
9529
+ else {
9530
+ this.impulse = value / 1000;
9531
+ }
9532
+ done = done && rest;
9533
+ }
9534
+ if (edges)
9535
+ this.edgeValues = values;
9536
+ state.done = done;
9537
+ return state;
9538
+ },
9539
+ };
9540
+ /**
9541
+ * Measure the duration now, then go back
9542
+ * to the start, so a delay shows the
9543
+ * start and not the end.
9544
+ */
9545
+ generator.calculatedDuration =
9546
+ calcGeneratorDuration(generator);
9547
+ generator.next(0);
9548
+ this.edgeVelocity = (i) => springs[i + 1].velocity(time);
9549
+ return generator;
9550
+ },
9551
+ };
9552
+ }
9553
+ }
9554
+ }
9555
+ const main = animateSingleValue(this.motionValue, [0, 1000], {
9556
+ ...transition,
9557
+ velocity: mainVelocity,
9301
9558
  isSync: true,
9302
9559
  onUpdate: (latest) => {
9303
9560
  this.mixTargetDelta(latest);
@@ -9308,6 +9565,7 @@
9308
9565
  this.completeAnimation();
9309
9566
  },
9310
9567
  });
9568
+ this.currentAnimation = main;
9311
9569
  notifyLayoutAnimationStart(this.currentAnimation, this);
9312
9570
  if (this.resumingFrom) {
9313
9571
  this.resumingFrom.currentAnimation = this.currentAnimation;
@@ -9325,11 +9583,15 @@
9325
9583
  this.resumingFrom =
9326
9584
  this.currentAnimation =
9327
9585
  this.animationValues =
9328
- undefined;
9586
+ this.edgeValues =
9587
+ this.edgeVelocity =
9588
+ this.impulse =
9589
+ undefined;
9329
9590
  this.notifyListeners("animationComplete");
9330
9591
  }
9331
9592
  finishAnimation() {
9332
9593
  if (this.currentAnimation) {
9594
+ this.edgeValues = this.impulse = undefined;
9333
9595
  this.mixTargetDelta?.(animationTarget);
9334
9596
  this.currentAnimation.stop();
9335
9597
  }
@@ -9825,6 +10087,18 @@
9825
10087
  function resetSkewAndRotation(node) {
9826
10088
  node.resetSkewAndRotation();
9827
10089
  }
10090
+ /**
10091
+ * Build an axis delta that moves the layout's edges by these offsets (px).
10092
+ */
10093
+ function edgesToDelta(output, delta, layout, min, max) {
10094
+ const length = layout.max - layout.min;
10095
+ const scale = length ? Math.max(0, (length + max - min) / length) : 1;
10096
+ const origin = (output.originPoint = delta.originPoint);
10097
+ output.origin = delta.origin;
10098
+ output.scale = scale;
10099
+ output.translate =
10100
+ layout.min + min - (origin + (layout.min - origin) * scale);
10101
+ }
9828
10102
  function mixAxisDeltaLinear(output, delta, p) {
9829
10103
  output.translate = mixNumber$1(delta.translate, 0, p);
9830
10104
  output.scale = mixNumber$1(delta.scale, 1, p);
@@ -12713,15 +12987,14 @@
12713
12987
  * step and the focal point stays in place.
12714
12988
  */
12715
12989
  const tick = () => {
12716
- if (dragControls()?.isDragging)
12717
- return stopSnap();
12718
12990
  const { value: p, done } = generator.next(time.now() - startTime);
12719
12991
  if (done)
12720
12992
  stopSnap();
12721
12993
  const [k, r, fx, fy] = at(p);
12722
12994
  scaleOption !== false && scale.set(k);
12723
12995
  rotateOption !== false && rotate.set(r);
12724
- if (moves) {
12996
+ // A drag that starts mid-snap owns x/y from then on.
12997
+ if (moves && !dragControls()?.isDragging) {
12725
12998
  x.set(snapToOrigin ? mixNumber$1(x0, 0, p) : fx);
12726
12999
  y.set(snapToOrigin ? mixNumber$1(y0, 0, p) : fy);
12727
13000
  }
@@ -17946,6 +18219,7 @@
17946
18219
  exports.getMixer = getMixer;
17947
18220
  exports.getOptimisedAppearId = getOptimisedAppearId;
17948
18221
  exports.getOriginIndex = getOriginIndex;
18222
+ exports.getSpringOptions = getSpringOptions;
17949
18223
  exports.getValueAsType = getValueAsType;
17950
18224
  exports.getValueTransition = getValueTransition$1;
17951
18225
  exports.getVariableValue = getVariableValue;