motion-dom 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.
@@ -1,5 +1,7 @@
1
1
  import { SubscriptionManager, clamp } from 'motion-utils';
2
2
  import { animateSingleValue } from '../../animation/animate/single-value.mjs';
3
+ import { spring } from '../../animation/generators/spring.mjs';
4
+ import { calcGeneratorDuration } from '../../animation/generators/utils/calc-duration.mjs';
3
5
  import { notifyLayoutAnimationStart } from '../../animation/utils/notify-inspector.mjs';
4
6
  import { getOptimisedAppearId } from '../../animation/optimized-appear/get-appear-id.mjs';
5
7
  import { getValueTransition } from '../../animation/utils/get-value-transition.mjs';
@@ -15,6 +17,7 @@ import { mixNumber } from '../../utils/mix/number.mjs';
15
17
  import { motionValue } from '../../value/index.mjs';
16
18
  import { resolveMotionValue } from '../../value/utils/resolve-motion-value.mjs';
17
19
  import { mixValues } from '../animation/mix-values.mjs';
20
+ import { pointToProgressVelocity, resolveAutoMass } from '../animation/layout-velocity.mjs';
18
21
  import { transformGroups } from '../../animation/waapi/transforms/groups.mjs';
19
22
  import { copyBoxInto, copyAxisDeltaInto, copyAxisInto } from '../geometry/copy.mjs';
20
23
  import { translateAxis, transformBox, applyBoxDelta, applyTreeDeltas } from '../geometry/delta-apply.mjs';
@@ -655,6 +658,37 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
655
658
  !calcLength(this.snapshot.measuredBox.y)) {
656
659
  this.snapshot = undefined;
657
660
  }
661
+ /**
662
+ * Record how fast the element is moving when it's measured, so
663
+ * the next layout animation can start at the same speed.
664
+ */
665
+ if (this.snapshot) {
666
+ const { visualElement } = this.options;
667
+ /**
668
+ * A paused or stopped animation hands on no velocity.
669
+ */
670
+ const isRunning = this.currentAnimation?.state === "running";
671
+ const edges = isRunning && this.animationEdges;
672
+ /**
673
+ * A child moving inside its parent hands on only its
674
+ * velocity relative to the parent.
675
+ */
676
+ if (isRunning && this.relativeTarget) {
677
+ this.snapshot.relativeVelocity =
678
+ this.relativeVelocity?.slice() || [];
679
+ }
680
+ const progress = (edges && this.motionValue?.getVelocity()) || 0;
681
+ const velocity = [0, 1, 2, 3, 4, 5].map((i) => (i < 4
682
+ ? edges &&
683
+ (this.edgeValues
684
+ ? this.edgeVelocity(i)
685
+ : (-edges[i] * progress) / 1000)
686
+ : visualElement
687
+ ?.getValue(i < 5 ? "x" : "y")
688
+ ?.getVelocity()) || 0);
689
+ if (velocity.some(Boolean))
690
+ this.snapshot.velocity = velocity;
691
+ }
658
692
  }
659
693
  updateLayout() {
660
694
  if (!this.instance)
@@ -1180,11 +1214,62 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
1180
1214
  // The path decides whether the layout shift is worth curving
1181
1215
  // (distance floor) and resolves the interpolator from the delta.
1182
1216
  const interpolate = pathFn?.interpolateProjection(delta);
1217
+ const box = this.layout?.layoutBox;
1218
+ let edges = box &&
1219
+ [box.x.min, box.x.max, box.y.min, box.y.max].map((edge, i) => {
1220
+ const { translate, scale, originPoint } = delta[i < 2 ? "x" : "y"];
1221
+ return (edge - originPoint) * (scale - 1) + translate;
1222
+ });
1223
+ /**
1224
+ * A zero-length layout axis has no finite edge offsets.
1225
+ */
1226
+ if (!edges?.every(isFinite))
1227
+ edges = undefined;
1228
+ this.animationEdges = edges;
1229
+ this.edgeValues =
1230
+ this.startVelocity =
1231
+ this.impulse =
1232
+ this.relativeVelocity =
1233
+ this.relativeStart =
1234
+ undefined;
1235
+ /**
1236
+ * A shared snapshot is measured with the old element's own
1237
+ * transforms (like x/y from drag), so it hands their velocity on
1238
+ * too. Otherwise those values keep animating themselves.
1239
+ */
1240
+ const velocity = snapshot?.velocity;
1241
+ /**
1242
+ * A child that wasn't moving inside an animating parent before
1243
+ * moved in page space, so that is its relative velocity.
1244
+ */
1245
+ const relative = snapshot?.relativeVelocity || velocity;
1246
+ if (relative?.slice(0, 4).some(Boolean) &&
1247
+ this.getClosestProjectingParent()) {
1248
+ this.relativeStart = relative;
1249
+ }
1250
+ if (velocity && edges) {
1251
+ const v = velocity
1252
+ .slice(0, 4)
1253
+ .map((edge, i) => edge +
1254
+ (isSharedLayoutAnimation
1255
+ ? velocity[4 + (i >> 1)]
1256
+ : 0));
1257
+ if (v.some(Boolean)) {
1258
+ this.startVelocity = interpolate
1259
+ ? pointToProgressVelocity(delta.x.translate, delta.y.translate, { x: (v[0] + v[1]) / 2, y: (v[2] + v[3]) / 2 })
1260
+ : v;
1261
+ }
1262
+ }
1183
1263
  this.mixTargetDelta = (latest) => {
1184
1264
  const progress = latest / 1000;
1185
1265
  const point = interpolate?.(progress);
1186
- mixAxisDeltaLinear(targetDelta.x, delta.x, progress);
1187
- mixAxisDeltaLinear(targetDelta.y, delta.y, progress);
1266
+ const values = this.edgeValues;
1267
+ for (let i = 0; i < 2; i++) {
1268
+ const axis = i ? "y" : "x";
1269
+ values
1270
+ ? edgesToDelta(targetDelta[axis], delta[axis], this.layout.layoutBox[axis], values[i * 2], values[i * 2 + 1])
1271
+ : mixAxisDeltaLinear(targetDelta[axis], delta[axis], progress);
1272
+ }
1188
1273
  if (point) {
1189
1274
  targetDelta.x.translate = point.x;
1190
1275
  targetDelta.y.translate = point.y;
@@ -1196,7 +1281,41 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
1196
1281
  this.relativeParent &&
1197
1282
  this.relativeParent.layout) {
1198
1283
  calcRelativePosition(relativeLayout, this.layout.layoutBox, this.relativeParent.layout.layoutBox, this.options.layoutAnchor || undefined);
1199
- mixBox(this.relativeTarget, this.relativeTargetOrigin, relativeLayout, progress);
1284
+ const from = this.relativeTargetOrigin;
1285
+ const to = relativeLayout;
1286
+ /**
1287
+ * A child mixes its place in its parent by progress,
1288
+ * so a nested tree moves as one.
1289
+ */
1290
+ mixBox(this.relativeTarget, from, to, progress);
1291
+ /**
1292
+ * Springs are linear, so the child's own velocity adds
1293
+ * the impulse response on top of the mix.
1294
+ */
1295
+ /**
1296
+ * Only springs take velocity, so a tween skips this.
1297
+ */
1298
+ if (this.isSpringLayout) {
1299
+ const box = this.relativeTarget;
1300
+ const impulse = this.impulse || 0;
1301
+ const start = this.relativeStart;
1302
+ const now = time.now();
1303
+ const dt = (now - this.relativeTime) / 1000;
1304
+ this.relativeTime = now;
1305
+ const speeds = (this.relativeVelocity || (this.relativeVelocity = [0, 0, 0, 0]));
1306
+ for (let i = 0; i < 4; i++) {
1307
+ const axis = i < 2 ? "x" : "y";
1308
+ const edge = i & 1 ? "max" : "min";
1309
+ if (start)
1310
+ box[axis][edge] += start[i] * impulse;
1311
+ if (prevRelativeTarget && dt > 0) {
1312
+ speeds[i] =
1313
+ (box[axis][edge] -
1314
+ prevRelativeTarget[axis][edge]) /
1315
+ dt;
1316
+ }
1317
+ }
1318
+ }
1200
1319
  /**
1201
1320
  * If this is an unchanged relative target we can consider the
1202
1321
  * projection not dirty.
@@ -1228,8 +1347,11 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
1228
1347
  }
1229
1348
  startAnimation(options) {
1230
1349
  this.notifyListeners("animationStart");
1350
+ const isSpring = (this.isSpringLayout = options.type === "spring");
1231
1351
  this.currentAnimation?.stop();
1232
1352
  this.resumingFrom?.currentAnimation?.stop();
1353
+ if (this.resumingFrom)
1354
+ this.resumingFrom.edgeValues = undefined;
1233
1355
  if (this.pendingAnimation) {
1234
1356
  cancelFrame(this.pendingAnimation);
1235
1357
  this.pendingAnimation = undefined;
@@ -1243,9 +1365,114 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
1243
1365
  globalProjectionState.hasAnimatedSinceResize = true;
1244
1366
  this.motionValue || (this.motionValue = motionValue(0));
1245
1367
  this.motionValue.jump(0, false);
1246
- this.currentAnimation = animateSingleValue(this.motionValue, [0, 1000], {
1247
- ...options,
1248
- velocity: 0,
1368
+ /**
1369
+ * A duration spring ignores velocity, like in animate(), so
1370
+ * it ends on time.
1371
+ */
1372
+ const velocity = this.startVelocity;
1373
+ this.startVelocity = undefined;
1374
+ let transition = options;
1375
+ let mainVelocity = 0;
1376
+ if (isSpring) {
1377
+ /**
1378
+ * With mass: "auto", mass comes from the element's own
1379
+ * size.
1380
+ */
1381
+ if (options.mass === "auto") {
1382
+ const box = this.layout?.layoutBox;
1383
+ transition = resolveAutoMass(options, box &&
1384
+ Math.sqrt(calcLength(box.x) * calcLength(box.y)));
1385
+ }
1386
+ /**
1387
+ * Only springs take a velocity. A tween keeps one
1388
+ * driver, so its controls (pause, time) reach it all.
1389
+ */
1390
+ const relative = this.relativeStart;
1391
+ if ((velocity || relative) && !options.repeat) {
1392
+ if (typeof velocity === "number") {
1393
+ mainVelocity = velocity;
1394
+ }
1395
+ else {
1396
+ /**
1397
+ * Each edge (x min, x max, y min, y max) gets its
1398
+ * own spring in pixels, with its own start
1399
+ * velocity. An edge that doesn't move keeps
1400
+ * still, and a thrown element keeps its size.
1401
+ * One generator steps them all with the main
1402
+ * spring, so one animation drives them and its
1403
+ * controls reach them all.
1404
+ */
1405
+ const edges = velocity && this.animationEdges;
1406
+ transition = {
1407
+ ...transition,
1408
+ type: (o) => {
1409
+ const springs = [
1410
+ o.velocity,
1411
+ ...(velocity || []),
1412
+ ].map((v, i) => spring({
1413
+ ...o,
1414
+ keyframes: i
1415
+ ? [edges[i - 1], 0]
1416
+ : o.keyframes,
1417
+ velocity: v,
1418
+ }));
1419
+ const count = springs.length;
1420
+ /**
1421
+ * The response to 1000 px/s, for a
1422
+ * child's velocity inside its parent.
1423
+ * Coarse rest thresholds, because a
1424
+ * small offset of it is far below 1px.
1425
+ */
1426
+ relative &&
1427
+ springs.push(spring({
1428
+ ...o,
1429
+ keyframes: [0, 0],
1430
+ velocity: 1000,
1431
+ restDelta: 0.5,
1432
+ restSpeed: 5,
1433
+ }));
1434
+ let time = 0;
1435
+ const values = [];
1436
+ const generator = {
1437
+ calculatedDuration: null,
1438
+ next: (t) => {
1439
+ time = t;
1440
+ const state = springs[0].next(t);
1441
+ let done = state.done;
1442
+ for (let i = 1; i < springs.length; i++) {
1443
+ const { value, done: rest } = springs[i].next(t);
1444
+ if (i < count) {
1445
+ values[i - 1] = value;
1446
+ }
1447
+ else {
1448
+ this.impulse = value / 1000;
1449
+ }
1450
+ done = done && rest;
1451
+ }
1452
+ if (edges)
1453
+ this.edgeValues = values;
1454
+ state.done = done;
1455
+ return state;
1456
+ },
1457
+ };
1458
+ /**
1459
+ * Measure the duration now, then go back
1460
+ * to the start, so a delay shows the
1461
+ * start and not the end.
1462
+ */
1463
+ generator.calculatedDuration =
1464
+ calcGeneratorDuration(generator);
1465
+ generator.next(0);
1466
+ this.edgeVelocity = (i) => springs[i + 1].velocity(time);
1467
+ return generator;
1468
+ },
1469
+ };
1470
+ }
1471
+ }
1472
+ }
1473
+ const main = animateSingleValue(this.motionValue, [0, 1000], {
1474
+ ...transition,
1475
+ velocity: mainVelocity,
1249
1476
  isSync: true,
1250
1477
  onUpdate: (latest) => {
1251
1478
  this.mixTargetDelta(latest);
@@ -1256,6 +1483,7 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
1256
1483
  this.completeAnimation();
1257
1484
  },
1258
1485
  });
1486
+ this.currentAnimation = main;
1259
1487
  notifyLayoutAnimationStart(this.currentAnimation, this);
1260
1488
  if (this.resumingFrom) {
1261
1489
  this.resumingFrom.currentAnimation = this.currentAnimation;
@@ -1273,11 +1501,15 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
1273
1501
  this.resumingFrom =
1274
1502
  this.currentAnimation =
1275
1503
  this.animationValues =
1276
- undefined;
1504
+ this.edgeValues =
1505
+ this.edgeVelocity =
1506
+ this.impulse =
1507
+ undefined;
1277
1508
  this.notifyListeners("animationComplete");
1278
1509
  }
1279
1510
  finishAnimation() {
1280
1511
  if (this.currentAnimation) {
1512
+ this.edgeValues = this.impulse = undefined;
1281
1513
  this.mixTargetDelta?.(animationTarget);
1282
1514
  this.currentAnimation.stop();
1283
1515
  }
@@ -1773,6 +2005,18 @@ function calcProjection(node) {
1773
2005
  function resetSkewAndRotation(node) {
1774
2006
  node.resetSkewAndRotation();
1775
2007
  }
2008
+ /**
2009
+ * Build an axis delta that moves the layout's edges by these offsets (px).
2010
+ */
2011
+ function edgesToDelta(output, delta, layout, min, max) {
2012
+ const length = layout.max - layout.min;
2013
+ const scale = length ? Math.max(0, (length + max - min) / length) : 1;
2014
+ const origin = (output.originPoint = delta.originPoint);
2015
+ output.origin = delta.origin;
2016
+ output.scale = scale;
2017
+ output.translate =
2018
+ layout.min + min - (origin + (layout.min - origin) * scale);
2019
+ }
1776
2020
  function mixAxisDeltaLinear(output, delta, p) {
1777
2021
  output.translate = mixNumber(delta.translate, 0, p);
1778
2022
  output.scale = mixNumber(delta.scale, 1, p);