motion-dom 14.10.0 → 14.10.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.
Files changed (32) hide show
  1. package/dist/cjs/index.js +339 -124
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/es/animation/GroupAnimation.mjs +21 -15
  4. package/dist/es/animation/GroupAnimation.mjs.map +1 -1
  5. package/dist/es/animation/animate/effects.mjs +15 -4
  6. package/dist/es/animation/animate/effects.mjs.map +1 -1
  7. package/dist/es/animation/animate/single-value.mjs +1 -1
  8. package/dist/es/animation/animate/single-value.mjs.map +1 -1
  9. package/dist/es/animation/generators/keyframes.mjs +44 -16
  10. package/dist/es/animation/generators/keyframes.mjs.map +1 -1
  11. package/dist/es/animation/interfaces/visual-element-target.mjs +1 -1
  12. package/dist/es/animation/interfaces/visual-element-target.mjs.map +1 -1
  13. package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs +8 -3
  14. package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs.map +1 -1
  15. package/dist/es/animation/keyframes/get-final.mjs +18 -4
  16. package/dist/es/animation/keyframes/get-final.mjs.map +1 -1
  17. package/dist/es/animation/waapi/transforms/TransformAnimation.mjs +155 -42
  18. package/dist/es/animation/waapi/transforms/TransformAnimation.mjs.map +1 -1
  19. package/dist/es/projection/styles/transform.mjs +39 -17
  20. package/dist/es/projection/styles/transform.mjs.map +1 -1
  21. package/dist/es/value/index.mjs +24 -20
  22. package/dist/es/value/index.mjs.map +1 -1
  23. package/dist/es/value/types/utils/animatable-none.mjs +13 -1
  24. package/dist/es/value/types/utils/animatable-none.mjs.map +1 -1
  25. package/dist/index.d.ts +17 -3
  26. package/dist/motion-dom.dev.js +339 -124
  27. package/dist/motion-dom.js +1 -1
  28. package/dist/size-rollup-motion-value.js +1 -1
  29. package/dist/size-rollup-motion-value.js.map +1 -1
  30. package/dist/size-rollup-style-effect.js +1 -1
  31. package/dist/size-rollup-style-effect.js.map +1 -1
  32. package/package.json +2 -2
@@ -1421,25 +1421,53 @@
1421
1421
  value: keyframeValues[0],
1422
1422
  };
1423
1423
  /**
1424
- * Fast setup path for two keyframes one easing. interpolate() handles
1425
- * a custom mixer, and falls back to the usual one where it declines.
1426
- */
1427
- if (!customMixer &&
1428
- keyframeValues.length === 2 &&
1429
- !Array.isArray(easingFunctions) &&
1430
- (!times ||
1431
- times.length !== 2 ||
1432
- (times[0] === 0 && times[1] === 1))) {
1433
- const [origin, target] = keyframeValues;
1434
- const mixer = origin === target
1435
- ? undefined
1436
- : (motionUtils.MotionGlobalConfig.mix || mix)(origin, target);
1424
+ * Two keyframes, or keyframes that hold, move once, then hold, as each
1425
+ * value of a sequence does, ease and mix only the pair that moves.
1426
+ */
1427
+ let span = -1;
1428
+ if (!customMixer) {
1429
+ for (let i = 1; i < keyframeValues.length; i++) {
1430
+ if (keyframeValues[i] !== keyframeValues[i - 1]) {
1431
+ if (span >= 0) {
1432
+ span = -2;
1433
+ break;
1434
+ }
1435
+ span = i - 1;
1436
+ }
1437
+ }
1438
+ }
1439
+ if (span >= 0) {
1440
+ const offsets = times && times.length === keyframeValues.length
1441
+ ? times
1442
+ : defaultOffset(keyframeValues);
1443
+ const from = offsets[span] * duration;
1444
+ const to = offsets[span + 1] * duration;
1445
+ const origin = keyframeValues[span];
1446
+ const target = keyframeValues[span + 1];
1447
+ const ease = Array.isArray(easingFunctions)
1448
+ ? easingFunctions[span]
1449
+ : easingFunctions;
1450
+ const mixer = (motionUtils.MotionGlobalConfig.mix || mix)(origin, target);
1451
+ const last = keyframeValues.length - 2;
1437
1452
  return {
1438
1453
  calculatedDuration: duration,
1439
1454
  next: (t) => {
1440
- state.value = mixer
1441
- ? mixer(easingFunctions(duration > 0 ? motionUtils.clamp(0, 1, t / duration) : 1))
1442
- : target;
1455
+ /**
1456
+ * Held keyframes either side of the move are mixed as
1457
+ * interpolate() mixes them, so colours keep its format.
1458
+ */
1459
+ state.value =
1460
+ span && t < from
1461
+ ? mixer(0)
1462
+ : span < last && (t >= to || !duration)
1463
+ ? typeof target === "number"
1464
+ ? target
1465
+ : mixer(1)
1466
+ : mixer(ease(t >= to || !duration
1467
+ ? 1
1468
+ : t <= from
1469
+ ? 0
1470
+ : (t - from) / (to - from)));
1443
1471
  state.done = t >= duration;
1444
1472
  return state;
1445
1473
  },
@@ -1488,13 +1516,27 @@
1488
1516
  : getGeneratorVelocity((s) => generator.next(s).value, t, generator.next(t).value);
1489
1517
  }
1490
1518
 
1491
- const isNotNull = (value) => value !== null;
1492
1519
  function getFinalKeyframe(keyframes, { repeat, repeatType = "loop" }, finalKeyframe, speed = 1) {
1493
- const resolvedKeyframes = keyframes.filter(isNotNull);
1520
+ /**
1521
+ * The first and last keyframes that aren't null, and how many there
1522
+ * are. Found in one pass rather than filtering into a new array, as
1523
+ * this runs for every value of every animation.
1524
+ */
1525
+ let first;
1526
+ let last;
1527
+ let numResolved = 0;
1528
+ for (let i = 0; i < keyframes.length; i++) {
1529
+ const keyframe = keyframes[i];
1530
+ if (keyframe !== null) {
1531
+ if (!numResolved++)
1532
+ first = keyframe;
1533
+ last = keyframe;
1534
+ }
1535
+ }
1494
1536
  const useFirstKeyframe = speed < 0 || (repeat && repeatType !== "loop" && repeat % 2 === 1);
1495
- const index = useFirstKeyframe ? 0 : resolvedKeyframes.length - 1;
1537
+ const index = useFirstKeyframe ? 0 : numResolved - 1;
1496
1538
  return !index || finalKeyframe === undefined
1497
- ? resolvedKeyframes[index]
1539
+ ? (index ? last : first)
1498
1540
  : finalKeyframe;
1499
1541
  }
1500
1542
 
@@ -2173,14 +2215,26 @@
2173
2215
  const getDefaultValueType = (key) => defaultValueTypes[key];
2174
2216
 
2175
2217
  const customTypes = /*@__PURE__*/ new Set([filter, mask]);
2218
+ /**
2219
+ * The last call and its result. animate() on many elements asks for the
2220
+ * same none for each one, and parsing the value is the slow part.
2221
+ */
2222
+ let lastKey;
2223
+ let lastValue;
2224
+ let lastNone;
2176
2225
  function getAnimatableNone(key, value) {
2226
+ if (key === lastKey && value === lastValue)
2227
+ return lastNone;
2177
2228
  let defaultValueType = getDefaultValueType(key);
2178
2229
  if (!customTypes.has(defaultValueType))
2179
2230
  defaultValueType = complex;
2180
2231
  // If value is not recognised as animatable, ie "none", create an animatable version origin based on the target
2181
- return defaultValueType.getAnimatableNone
2232
+ lastNone = defaultValueType.getAnimatableNone
2182
2233
  ? defaultValueType.getAnimatableNone(value)
2183
2234
  : undefined;
2235
+ lastKey = key;
2236
+ lastValue = value;
2237
+ return lastNone;
2184
2238
  }
2185
2239
 
2186
2240
  function fillWildcards(keyframes) {
@@ -3215,7 +3269,8 @@
3215
3269
  /**
3216
3270
  * Named easings that are the same cubic-bezier in JS and CSS.
3217
3271
  */
3218
- const isNativeEasing = (ease) => /^(linear|ease(In|Out|InOut))$/.test(ease);
3272
+ const nativeEasings = new Set(["linear", "easeIn", "easeOut", "easeInOut"]);
3273
+ const isNativeEasing = (ease) => nativeEasings.has(ease);
3219
3274
  /**
3220
3275
  * The last trace of each easing WAAPI can't run into linear(), with its
3221
3276
  * number of samples, so the elements of a sequence that share an easing
@@ -3299,6 +3354,54 @@
3299
3354
  */
3300
3355
  !needsMixer(options));
3301
3356
  }
3357
+ /**
3358
+ * Starting or stopping tells the group to update its WAAPI animation.
3359
+ * The frame loop driver is only made once the group is on the main
3360
+ * thread, as accelerated animations never tick.
3361
+ */
3362
+ class TransformDriver {
3363
+ constructor(element, update, makeDriver, animation) {
3364
+ this.element = element;
3365
+ this.update = update;
3366
+ this.makeDriver = makeDriver;
3367
+ this.animation = animation;
3368
+ }
3369
+ get js() {
3370
+ return (this.driver || (this.driver = this.makeDriver(this.update)));
3371
+ }
3372
+ now() {
3373
+ return this.driver || this.makeDriver !== frameloopDriver
3374
+ ? this.js.now()
3375
+ : frameData.isProcessing
3376
+ ? frameData.timestamp
3377
+ : time.now();
3378
+ }
3379
+ /**
3380
+ * keepAlive is false when seeking. Seeking the WAAPI animation is
3381
+ * free, but if a seek needs a rebuild and keeps happening, the group
3382
+ * moves to the main thread.
3383
+ */
3384
+ start(keepAlive = true) {
3385
+ const group = getGroup(this.element);
3386
+ !group || group.isJS
3387
+ ? this.js.start(keepAlive)
3388
+ : group.schedule(false, !keepAlive);
3389
+ }
3390
+ stopJS() {
3391
+ this.driver?.stop();
3392
+ }
3393
+ stop() {
3394
+ this.stopJS();
3395
+ const group = getGroup(this.element);
3396
+ group && !group.isFinishing && group.release(this.animation);
3397
+ }
3398
+ }
3399
+ /**
3400
+ * JSAnimation's time setter, looked up on first use rather than at module
3401
+ * load, so the module has no top-level side effects and stays
3402
+ * tree-shakeable in flattened bundles.
3403
+ */
3404
+ let setTime;
3302
3405
  class TransformAnimation extends JSAnimation {
3303
3406
  constructor(options) {
3304
3407
  const element = getOwner(options).current;
@@ -3310,35 +3413,16 @@
3310
3413
  const canAccelerate = canAccelerateTransform(options);
3311
3414
  /**
3312
3415
  * The driver ticks on the main thread only once the group has
3313
- * moved there. Until then, starting or stopping tells the group to
3314
- * update its WAAPI animation.
3416
+ * moved there, so its frame loop driver is made then.
3315
3417
  */
3316
- options.driver = (update) => {
3317
- const js = driver(update);
3318
- return {
3319
- js,
3320
- now: js.now,
3321
- start: (keepAlive = true) => {
3322
- const group = getGroup(element);
3323
- const isJS = !group || group.isJS;
3324
- /**
3325
- * keepAlive is false when seeking. Seeking the WAAPI
3326
- * animation is free, but if a seek needs a rebuild and
3327
- * keeps happening, the group moves to the main thread.
3328
- */
3329
- isJS
3330
- ? js.start(keepAlive)
3331
- : group.schedule(false, !keepAlive);
3332
- },
3333
- stop: () => {
3334
- js.stop();
3335
- const group = getGroup(element);
3336
- group && !group.isFinishing && group.release(animation);
3337
- },
3338
- };
3339
- };
3418
+ options.driver = (update) => new TransformDriver(element, update, driver, animation);
3340
3419
  super(options);
3341
3420
  animation = this;
3421
+ /**
3422
+ * The first driver is made in super(), before this was assigned.
3423
+ */
3424
+ const firstDriver = this.driver;
3425
+ firstDriver && (firstDriver.animation = this);
3342
3426
  this.canAccelerate = canAccelerate;
3343
3427
  /**
3344
3428
  * stop is bound in JSAnimation, so wrap it rather than override.
@@ -3352,12 +3436,29 @@
3352
3436
  stop();
3353
3437
  };
3354
3438
  }
3439
+ /**
3440
+ * Each keyframe's offset, made once for the keyframes rather than on
3441
+ * every build.
3442
+ */
3443
+ offsets() {
3444
+ const { keyframes, times } = this.options;
3445
+ if (times)
3446
+ return times;
3447
+ if (this.offsetsOf !== keyframes) {
3448
+ this.offsetsOf = keyframes;
3449
+ this.cachedOffsets = defaultOffset(keyframes);
3450
+ }
3451
+ return this.cachedOffsets;
3452
+ }
3355
3453
  get group() {
3356
3454
  return getGroup(getOwner(this.options).current);
3357
3455
  }
3358
3456
  get js() {
3359
3457
  return this.driver?.js;
3360
3458
  }
3459
+ stopJS() {
3460
+ this.driver?.stopJS();
3461
+ }
3361
3462
  get name() {
3362
3463
  return this.options.name;
3363
3464
  }
@@ -3403,7 +3504,7 @@
3403
3504
  * exactly, otherwise its function, to trace.
3404
3505
  */
3405
3506
  tween() {
3406
- const { keyframes: keyframes$1, type, times, repeat, delay = 0 } = this.options;
3507
+ const { keyframes: keyframes$1, type, repeat, delay = 0 } = this.options;
3407
3508
  let span = -1;
3408
3509
  if (repeat || type !== keyframes || this.holdTime !== null) {
3409
3510
  return;
@@ -3418,7 +3519,7 @@
3418
3519
  let { ease } = this.options;
3419
3520
  if (motionUtils.isEasingArray(ease))
3420
3521
  ease = ease[span];
3421
- const offsets = times || defaultOffset(keyframes$1);
3522
+ const offsets = this.offsets();
3422
3523
  const at = (i) => this.startTime +
3423
3524
  (delay + offsets[i] * this.totalDuration) / this.speed;
3424
3525
  if (span >= 0) {
@@ -3447,9 +3548,9 @@
3447
3548
  */
3448
3549
  boundaries(from, to) {
3449
3550
  const { startTime, speed, resolvedDuration, options } = this;
3450
- const { delay = 0, repeat = 0, repeatDelay = 0, repeatType, times, keyframes: keyframes$1, type, } = options;
3551
+ const { delay = 0, repeat = 0, repeatDelay = 0, repeatType, type, } = options;
3451
3552
  const offsets = (type || keyframes) === keyframes
3452
- ? times || defaultOffset(keyframes$1)
3553
+ ? this.offsets()
3453
3554
  : [0, 1];
3454
3555
  const start = startTime + delay / speed;
3455
3556
  const result = [];
@@ -3526,8 +3627,12 @@
3526
3627
  }
3527
3628
  return super.time;
3528
3629
  }
3630
+ /**
3631
+ * Calls JSAnimation's setter directly, as `super.time =` is a slow
3632
+ * path in V8 and this runs for every animation of a seeked sequence.
3633
+ */
3529
3634
  set time(newTime) {
3530
- super.time = newTime;
3635
+ (setTime ?? (setTime = Object.getOwnPropertyDescriptor(JSAnimation.prototype, "time").set)).call(this, newTime);
3531
3636
  }
3532
3637
  /**
3533
3638
  * Scroll timelines set time every frame, so run these on the main
@@ -3573,6 +3678,12 @@
3573
3678
  let hasChurned = false;
3574
3679
  let lastChurn = -Infinity;
3575
3680
  let wake;
3681
+ let wakeAt = Infinity;
3682
+ /**
3683
+ * While waiting to build a tween that starts later, how long after
3684
+ * the last build to build it.
3685
+ */
3686
+ let buildAt;
3576
3687
  const has = (key) => {
3577
3688
  for (const track of tracks)
3578
3689
  if (track.name === key)
@@ -3641,6 +3752,8 @@
3641
3752
  */
3642
3753
  const stop = (shouldCommit = true) => {
3643
3754
  clearTimeout(wake);
3755
+ wakeAt = Infinity;
3756
+ buildAt = undefined;
3644
3757
  shouldCommit && commit();
3645
3758
  segments.forEach(({ animation }) => cancel(animation));
3646
3759
  segments = [];
@@ -3870,7 +3983,13 @@
3870
3983
  * A WAAPI animation of the composed transform between two timestamps.
3871
3984
  */
3872
3985
  const run = (moving, from, to, nativeEasing, options = { fill: "both" }, settled) => {
3873
- const bounds = moving.flatMap((track) => track.boundaries(from, to));
3986
+ /**
3987
+ * With an easing given, there's nothing to sample, so no need
3988
+ * for the boundaries.
3989
+ */
3990
+ const bounds = nativeEasing
3991
+ ? []
3992
+ : moving.flatMap((track) => track.boundaries(from, to));
3874
3993
  const easing = nativeEasing || getEasing(moving, from, to, bounds);
3875
3994
  const times = easing ? [from, to] : sample(moving, from, to, bounds);
3876
3995
  const transform = times.map((t) => compose(t, settled));
@@ -3893,6 +4012,23 @@
3893
4012
  easing: easing || "linear",
3894
4013
  });
3895
4014
  };
4015
+ /**
4016
+ * Check whether it's time to build in this long. A check that's due
4017
+ * sooner is kept, and checks again if it's early, so pausing and
4018
+ * playing don't each replace a timer. One due later, after a seek or
4019
+ * speed change while held, is replaced.
4020
+ */
4021
+ const wakeIn = (delay) => {
4022
+ const at = time.now() + delay;
4023
+ if (at < wakeAt) {
4024
+ clearTimeout(wake);
4025
+ wakeAt = at;
4026
+ wake = setTimeout(() => {
4027
+ wakeAt = Infinity;
4028
+ group.schedule(false);
4029
+ }, delay);
4030
+ }
4031
+ };
3896
4032
  /**
3897
4033
  * Replace the WAAPI animations with ones built from now. Returns
3898
4034
  * undefined if the animations can't be composed.
@@ -3968,7 +4104,7 @@
3968
4104
  */
3969
4105
  if (from - now > buildAhead) {
3970
4106
  moving.forEach((track) => track.sync());
3971
- wake = setTimeout(() => group.schedule(true), from - now - buildAhead);
4107
+ wakeIn((buildAt = from - now - buildAhead));
3972
4108
  return true;
3973
4109
  }
3974
4110
  easing =
@@ -4023,9 +4159,11 @@
4023
4159
  /**
4024
4160
  * If every animation has been paused, played, seeked or sped up
4025
4161
  * together since the WAAPI animation was built, do the same to it.
4026
- * Returns false if it needs rebuilding instead.
4162
+ * Returns false if it needs rebuilding instead. Seeking to before a
4163
+ * tween starts rebuilds, so a running group seeked every frame still
4164
+ * moves to the main thread.
4027
4165
  */
4028
- const retime = (now) => {
4166
+ const retime = (now, isSeek) => {
4029
4167
  let elapsed;
4030
4168
  let rate = 0;
4031
4169
  for (const track of tracks) {
@@ -4058,12 +4196,33 @@
4058
4196
  }
4059
4197
  if (elapsed === undefined)
4060
4198
  return true;
4061
- if (!segments.length)
4062
- return false;
4199
+ /**
4200
+ * Waiting to build a tween that starts later. Until then the
4201
+ * values hold where they start, so only when to build changes.
4202
+ * Held, it has nothing on the compositor, so it can be seeked.
4203
+ */
4204
+ if (!segments.length) {
4205
+ if (buildAt === undefined || (isSeek && rate))
4206
+ return false;
4207
+ if (rate) {
4208
+ const delay = (buildAt - elapsed) / rate;
4209
+ if (delay < 1)
4210
+ return false;
4211
+ wakeIn(delay);
4212
+ }
4213
+ return true;
4214
+ }
4215
+ /**
4216
+ * Only a tween is built to start later, and it holds its first
4217
+ * keyframe until then, so it can be paused and played before it
4218
+ * starts.
4219
+ */
4063
4220
  for (const { offset, end, isDone } of segments) {
4064
4221
  const local = elapsed - offset;
4065
4222
  if (end < Infinity &&
4066
- (isDone ? local < end - 1 : local < 0 || local >= end)) {
4223
+ (isDone
4224
+ ? local < end - 1
4225
+ : (local < 0 && (isSeek || offset <= 0)) || local >= end)) {
4067
4226
  return false;
4068
4227
  }
4069
4228
  }
@@ -4084,7 +4243,8 @@
4084
4243
  animation.updatePlaybackRate(rate);
4085
4244
  }
4086
4245
  else if (rate) {
4087
- animation.playbackRate = rate;
4246
+ animation.playbackRate !== rate &&
4247
+ (animation.playbackRate = rate);
4088
4248
  animation.startTime = now - local / rate;
4089
4249
  }
4090
4250
  else {
@@ -4158,12 +4318,12 @@
4158
4318
  if (group.isJS) {
4159
4319
  group.isJS = false;
4160
4320
  needsBuild = true;
4161
- tracks.forEach((track) => track.js?.stop());
4321
+ tracks.forEach((track) => track.stopJS());
4162
4322
  }
4163
4323
  const shouldBuild = needsBuild;
4164
4324
  const churned = hasChurned;
4165
4325
  needsBuild = hasChurned = false;
4166
- if (shouldBuild || !retime(now)) {
4326
+ if (shouldBuild || !retime(now, churned)) {
4167
4327
  if (churned) {
4168
4328
  if (now - lastChurn < churnWindow)
4169
4329
  return group.demote();
@@ -4172,6 +4332,13 @@
4172
4332
  if (!animate(now))
4173
4333
  return group.demote();
4174
4334
  }
4335
+ else if (buildAt !== undefined && !churned) {
4336
+ /**
4337
+ * Still waiting to build, so the values still hold where
4338
+ * they start.
4339
+ */
4340
+ return;
4341
+ }
4175
4342
  /**
4176
4343
  * Held animations don't move, so set their motion values to where
4177
4344
  * they're held, as a JSAnimation would on its next tick. Pausing
@@ -4439,7 +4606,10 @@
4439
4606
  class GroupAnimation {
4440
4607
  constructor(animations) {
4441
4608
  // Bound to accommodate common `return animation.stop` pattern
4442
- this.stop = () => this.runAll("stop");
4609
+ this.stop = () => {
4610
+ for (const animation of this.animations)
4611
+ animation.stop();
4612
+ };
4443
4613
  this.animations = animations.filter(Boolean);
4444
4614
  }
4445
4615
  get finished() {
@@ -4451,11 +4621,6 @@
4451
4621
  getAll(propName) {
4452
4622
  return this.animations[0][propName];
4453
4623
  }
4454
- setAll(propName, newValue) {
4455
- for (let i = 0; i < this.animations.length; i++) {
4456
- this.animations[i][propName] = newValue;
4457
- }
4458
- }
4459
4624
  attachTimeline(timeline) {
4460
4625
  const subscriptions = this.animations.map((animation) => animation.attachTimeline(timeline));
4461
4626
  return () => {
@@ -4468,14 +4633,21 @@
4468
4633
  get time() {
4469
4634
  return this.getAll("time");
4470
4635
  }
4636
+ /**
4637
+ * Setters and methods name the property directly, rather than through
4638
+ * a shared helper, so each call site stays monomorphic. Sequences
4639
+ * call these on every animation, many times a second.
4640
+ */
4471
4641
  set time(time) {
4472
- this.setAll("time", time);
4642
+ for (const animation of this.animations)
4643
+ animation.time = time;
4473
4644
  }
4474
4645
  get speed() {
4475
4646
  return this.getAll("speed");
4476
4647
  }
4477
4648
  set speed(speed) {
4478
- this.setAll("speed", speed);
4649
+ for (const animation of this.animations)
4650
+ animation.speed = speed;
4479
4651
  }
4480
4652
  get state() {
4481
4653
  return this.getAll("state");
@@ -4489,20 +4661,21 @@
4489
4661
  get iterationDuration() {
4490
4662
  return getMax(this.animations, "iterationDuration");
4491
4663
  }
4492
- runAll(methodName) {
4493
- this.animations.forEach((controls) => controls[methodName]());
4494
- }
4495
4664
  play() {
4496
- this.runAll("play");
4665
+ for (const animation of this.animations)
4666
+ animation.play();
4497
4667
  }
4498
4668
  pause() {
4499
- this.runAll("pause");
4669
+ for (const animation of this.animations)
4670
+ animation.pause();
4500
4671
  }
4501
4672
  cancel() {
4502
- this.runAll("cancel");
4673
+ for (const animation of this.animations)
4674
+ animation.cancel();
4503
4675
  }
4504
4676
  complete() {
4505
- this.runAll("complete");
4677
+ for (const animation of this.animations)
4678
+ animation.complete();
4506
4679
  }
4507
4680
  }
4508
4681
  function getMax(animations, propName) {
@@ -4862,28 +5035,32 @@
4862
5035
  * @param animation - A function that starts the provided animation
4863
5036
  */
4864
5037
  start(startAnimation) {
5038
+ return new Promise((resolve) => this.run(startAnimation, resolve));
5039
+ }
5040
+ /**
5041
+ * As start(), without making a promise, for animations started
5042
+ * internally, which complete through their own controls.
5043
+ */
5044
+ run(startAnimation, onComplete) {
4865
5045
  this.stop();
4866
- return new Promise((resolve) => {
4867
- this.hasAnimated = true;
4868
- /**
4869
- * Complete synchronously via the callback rather than chaining a
4870
- * second promise per animation. Guard against animations that
4871
- * complete before startAnimation has returned.
4872
- */
4873
- let isComplete = false;
4874
- let animation;
4875
- // eslint-disable-next-line prefer-const -- read by the callback before assignment when completing synchronously
4876
- animation = startAnimation(() => {
4877
- isComplete = true;
4878
- this.events.animationComplete?.notify();
4879
- if (this.animation === animation)
4880
- this.animation = undefined;
4881
- resolve();
4882
- });
4883
- if (!isComplete)
4884
- this.animation = animation;
4885
- this.events.animationStart?.notify();
5046
+ this.hasAnimated = true;
5047
+ /**
5048
+ * Guard against animations that complete before startAnimation
5049
+ * has returned.
5050
+ */
5051
+ let isComplete = false;
5052
+ let animation;
5053
+ // eslint-disable-next-line prefer-const -- read by the callback before assignment when completing synchronously
5054
+ animation = startAnimation(() => {
5055
+ isComplete = true;
5056
+ this.events.animationComplete?.notify();
5057
+ if (this.animation === animation)
5058
+ this.animation = undefined;
5059
+ onComplete?.();
4886
5060
  });
5061
+ if (!isComplete)
5062
+ this.animation = animation;
5063
+ this.events.animationStart?.notify();
4887
5064
  }
4888
5065
  /**
4889
5066
  * Stop the currently active animation.
@@ -5505,7 +5682,7 @@
5505
5682
  }
5506
5683
  addValueToWillChange(visualElement, key);
5507
5684
  const shouldReduceMotion = reduceMotion ?? visualElement.shouldReduceMotion;
5508
- value.start(animateMotionValue(key, value, valueTarget, shouldReduceMotion && positionalKeys.has(key)
5685
+ value.run(animateMotionValue(key, value, valueTarget, shouldReduceMotion && positionalKeys.has(key)
5509
5686
  ? { type: false }
5510
5687
  : valueTransition, visualElement, isHandoff));
5511
5688
  const animation = value.animation;
@@ -5640,9 +5817,14 @@
5640
5817
  }
5641
5818
 
5642
5819
  class DOMKeyframesResolver extends KeyframeResolver {
5643
- constructor() {
5644
- super(...arguments);
5645
- this.isAsync = true;
5820
+ /**
5821
+ * Passes isAsync to the base constructor rather than setting it as a
5822
+ * class field, which would compile to a constructor that spreads
5823
+ * `arguments` into super(). That spread is slow in the interpreter,
5824
+ * and animate() constructs one resolver per value.
5825
+ */
5826
+ constructor(unresolvedKeyframes, onComplete, name, motionValue, element) {
5827
+ super(unresolvedKeyframes, onComplete, name, motionValue, element, true);
5646
5828
  }
5647
5829
  /**
5648
5830
  * Keyframes that don't need reading from the DOM, or measuring,
@@ -5941,7 +6123,7 @@
5941
6123
  frame.update(() => value.set(target));
5942
6124
  continue;
5943
6125
  }
5944
- value.start(animateMotionValue(key, value, target, reduceMotion && positionalKeys.has(key)
6126
+ value.run(animateMotionValue(key, value, target, reduceMotion && positionalKeys.has(key)
5945
6127
  ? { type: false }
5946
6128
  : transition, element));
5947
6129
  value.animation &&
@@ -5971,8 +6153,14 @@
5971
6153
  * measurement) via `element`.
5972
6154
  */
5973
6155
  function animateEffectSubject(effect, subject, keyframes, transition, element) {
5974
- return animateValues((key) => {
5975
- let value = effect.get(subject, key);
6156
+ /**
6157
+ * Values made for this call are bound with one effect call, after the
6158
+ * loop, rather than one each.
6159
+ */
6160
+ let created;
6161
+ let isBound = false;
6162
+ const animations = animateValues((key) => {
6163
+ let value = created?.[key] ?? effect.get(subject, key);
5976
6164
  if (!value) {
5977
6165
  let initial;
5978
6166
  if (!element) {
@@ -5983,10 +6171,15 @@
5983
6171
  motionUtils.invariant(initial !== undefined, `"${key}" can't be read from the animated subject. Provide [from, to] keyframes.`, "effect-unreadable-value");
5984
6172
  }
5985
6173
  value = motionValue(initial, { owner: element });
5986
- effect(subject, { [key]: value });
6174
+ isBound
6175
+ ? effect(subject, { [key]: value })
6176
+ : ((created || (created = {}))[key] = value);
5987
6177
  }
5988
6178
  return value;
5989
6179
  }, keyframes, transition, element);
6180
+ isBound = true;
6181
+ created && effect(subject, created);
6182
+ return animations;
5990
6183
  }
5991
6184
  function firstKeyframe(target) {
5992
6185
  const first = Array.isArray(target) ? target[0] : undefined;
@@ -6982,7 +7175,7 @@
6982
7175
 
6983
7176
  function animateSingleValue(value, keyframes, options) {
6984
7177
  const motionValue$1 = isMotionValue(value) ? value : motionValue(value);
6985
- motionValue$1.start(animateMotionValue("", motionValue$1, keyframes, options));
7178
+ motionValue$1.run(animateMotionValue("", motionValue$1, keyframes, options));
6986
7179
  return motionValue$1.animation;
6987
7180
  }
6988
7181
 
@@ -7489,13 +7682,7 @@
7489
7682
  if (xTranslate || yTranslate || zTranslate) {
7490
7683
  transform = `translate3d(${xTranslate}px, ${yTranslate}px, ${zTranslate}px) `;
7491
7684
  }
7492
- /**
7493
- * Apply scale correction for the tree transform.
7494
- * This will apply scale to the screen-orientated axes.
7495
- */
7496
- if (treeScale.x !== 1 || treeScale.y !== 1) {
7497
- transform += `scale(${1 / treeScale.x}, ${1 / treeScale.y}) `;
7498
- }
7685
+ let angles = "";
7499
7686
  if (latestTransform) {
7500
7687
  const { transformPerspective } = latestTransform;
7501
7688
  if (transformPerspective)
@@ -7504,20 +7691,48 @@
7504
7691
  const key = angleTransforms[i];
7505
7692
  const value = latestTransform[key];
7506
7693
  if (value)
7507
- transform += `${key}(${value}deg) `;
7694
+ angles += `${key}(${value}deg) `;
7508
7695
  // Additive `rotate()` so user `rotate` isn't clobbered.
7509
7696
  if (!i && latestTransform.pathRotation)
7510
- transform += `rotate(${latestTransform.pathRotation}deg) `;
7697
+ angles += `rotate(${latestTransform.pathRotation}deg) `;
7511
7698
  }
7512
7699
  }
7513
- /**
7514
- * Apply scale to match the size of the element to the size we want it.
7515
- * This will apply scale to the element-orientated axes.
7516
- */
7517
- const elementScaleX = delta.x.scale * treeScale.x;
7518
- const elementScaleY = delta.y.scale * treeScale.y;
7519
- if (elementScaleX !== 1 || elementScaleY !== 1) {
7520
- transform += `scale(${elementScaleX}, ${elementScaleY})`;
7700
+ if (angles) {
7701
+ /**
7702
+ * Apply scale correction for the tree transform.
7703
+ * This will apply scale to the screen-orientated axes.
7704
+ */
7705
+ if (treeScale.x !== 1 || treeScale.y !== 1) {
7706
+ transform += `scale(${1 / treeScale.x}, ${1 / treeScale.y}) `;
7707
+ }
7708
+ transform += angles;
7709
+ /**
7710
+ * Apply scale to match the size of the element to the size we want it.
7711
+ * This will apply scale to the element-orientated axes.
7712
+ */
7713
+ const elementScaleX = delta.x.scale * treeScale.x;
7714
+ const elementScaleY = delta.y.scale * treeScale.y;
7715
+ if (elementScaleX !== 1 || elementScaleY !== 1) {
7716
+ transform += `scale(${elementScaleX}, ${elementScaleY})`;
7717
+ }
7718
+ }
7719
+ else if (delta.x.scale !== 1 || delta.y.scale !== 1) {
7720
+ /**
7721
+ * With no rotation or skew between them, the tree scale correction
7722
+ * (1 / treeScale) and the element scale (delta.scale * treeScale)
7723
+ * combine into a single scale. A shorter transform is quicker for
7724
+ * the browser to parse, which matters when it's set every frame.
7725
+ */
7726
+ transform += `scale(${delta.x.scale}, ${delta.y.scale})`;
7727
+ }
7728
+ else if (!transform && (treeScale.x !== 1 || treeScale.y !== 1)) {
7729
+ /**
7730
+ * Inside a scaled tree the transform must never be "none", even when
7731
+ * this element's own delta is the identity: scale correction for
7732
+ * borderRadius and boxShadow is skipped for "none", and the element
7733
+ * would gain and lose its stacking context as the delta snaps.
7734
+ */
7735
+ transform = "scale(1, 1)";
7521
7736
  }
7522
7737
  return transform || "none";
7523
7738
  }