motion 14.12.0 → 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.
@@ -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
  }
@@ -7622,6 +7622,29 @@
7622
7622
  };
7623
7623
  }
7624
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
+
7625
7648
  /**
7626
7649
  * Reset an axis to the provided origin box.
7627
7650
  *
@@ -8717,6 +8740,37 @@
8717
8740
  !calcLength(this.snapshot.measuredBox.y)) {
8718
8741
  this.snapshot = undefined;
8719
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
+ }
8720
8774
  }
8721
8775
  updateLayout() {
8722
8776
  if (!this.instance)
@@ -9242,11 +9296,62 @@
9242
9296
  // The path decides whether the layout shift is worth curving
9243
9297
  // (distance floor) and resolves the interpolator from the delta.
9244
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
+ }
9245
9345
  this.mixTargetDelta = (latest) => {
9246
9346
  const progress = latest / 1000;
9247
9347
  const point = interpolate?.(progress);
9248
- mixAxisDeltaLinear(targetDelta.x, delta.x, progress);
9249
- 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
+ }
9250
9355
  if (point) {
9251
9356
  targetDelta.x.translate = point.x;
9252
9357
  targetDelta.y.translate = point.y;
@@ -9258,7 +9363,41 @@
9258
9363
  this.relativeParent &&
9259
9364
  this.relativeParent.layout) {
9260
9365
  calcRelativePosition(relativeLayout, this.layout.layoutBox, this.relativeParent.layout.layoutBox, this.options.layoutAnchor || undefined);
9261
- 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
+ }
9262
9401
  /**
9263
9402
  * If this is an unchanged relative target we can consider the
9264
9403
  * projection not dirty.
@@ -9290,8 +9429,11 @@
9290
9429
  }
9291
9430
  startAnimation(options) {
9292
9431
  this.notifyListeners("animationStart");
9432
+ const isSpring = (this.isSpringLayout = options.type === "spring");
9293
9433
  this.currentAnimation?.stop();
9294
9434
  this.resumingFrom?.currentAnimation?.stop();
9435
+ if (this.resumingFrom)
9436
+ this.resumingFrom.edgeValues = undefined;
9295
9437
  if (this.pendingAnimation) {
9296
9438
  cancelFrame(this.pendingAnimation);
9297
9439
  this.pendingAnimation = undefined;
@@ -9305,9 +9447,114 @@
9305
9447
  globalProjectionState.hasAnimatedSinceResize = true;
9306
9448
  this.motionValue || (this.motionValue = motionValue(0));
9307
9449
  this.motionValue.jump(0, false);
9308
- this.currentAnimation = animateSingleValue(this.motionValue, [0, 1000], {
9309
- ...options,
9310
- 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,
9311
9558
  isSync: true,
9312
9559
  onUpdate: (latest) => {
9313
9560
  this.mixTargetDelta(latest);
@@ -9318,6 +9565,7 @@
9318
9565
  this.completeAnimation();
9319
9566
  },
9320
9567
  });
9568
+ this.currentAnimation = main;
9321
9569
  notifyLayoutAnimationStart(this.currentAnimation, this);
9322
9570
  if (this.resumingFrom) {
9323
9571
  this.resumingFrom.currentAnimation = this.currentAnimation;
@@ -9335,11 +9583,15 @@
9335
9583
  this.resumingFrom =
9336
9584
  this.currentAnimation =
9337
9585
  this.animationValues =
9338
- undefined;
9586
+ this.edgeValues =
9587
+ this.edgeVelocity =
9588
+ this.impulse =
9589
+ undefined;
9339
9590
  this.notifyListeners("animationComplete");
9340
9591
  }
9341
9592
  finishAnimation() {
9342
9593
  if (this.currentAnimation) {
9594
+ this.edgeValues = this.impulse = undefined;
9343
9595
  this.mixTargetDelta?.(animationTarget);
9344
9596
  this.currentAnimation.stop();
9345
9597
  }
@@ -9835,6 +10087,18 @@
9835
10087
  function resetSkewAndRotation(node) {
9836
10088
  node.resetSkewAndRotation();
9837
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
+ }
9838
10102
  function mixAxisDeltaLinear(output, delta, p) {
9839
10103
  output.translate = mixNumber$1(delta.translate, 0, p);
9840
10104
  output.scale = mixNumber$1(delta.scale, 1, p);
@@ -12723,15 +12987,14 @@
12723
12987
  * step and the focal point stays in place.
12724
12988
  */
12725
12989
  const tick = () => {
12726
- if (dragControls()?.isDragging)
12727
- return stopSnap();
12728
12990
  const { value: p, done } = generator.next(time.now() - startTime);
12729
12991
  if (done)
12730
12992
  stopSnap();
12731
12993
  const [k, r, fx, fy] = at(p);
12732
12994
  scaleOption !== false && scale.set(k);
12733
12995
  rotateOption !== false && rotate.set(r);
12734
- if (moves) {
12996
+ // A drag that starts mid-snap owns x/y from then on.
12997
+ if (moves && !dragControls()?.isDragging) {
12735
12998
  x.set(snapToOrigin ? mixNumber$1(x0, 0, p) : fx);
12736
12999
  y.set(snapToOrigin ? mixNumber$1(y0, 0, p) : fy);
12737
13000
  }
@@ -17956,6 +18219,7 @@
17956
18219
  exports.getMixer = getMixer;
17957
18220
  exports.getOptimisedAppearId = getOptimisedAppearId;
17958
18221
  exports.getOriginIndex = getOriginIndex;
18222
+ exports.getSpringOptions = getSpringOptions;
17959
18223
  exports.getValueAsType = getValueAsType;
17960
18224
  exports.getValueTransition = getValueTransition$1;
17961
18225
  exports.getVariableValue = getVariableValue;