motion-dom 14.9.0 → 14.10.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,4 +1,4 @@
1
- import { isBezierDefinition, millisecondsToSeconds, clamp, progress } from 'motion-utils';
1
+ import { isEasingArray, isBezierDefinition, easingDefinitionToFunction, millisecondsToSeconds, clamp, progress } from 'motion-utils';
2
2
  import { frame } from '../../../frameloop/frame.mjs';
3
3
  import { frameData } from '../../../frameloop/frame-data.mjs';
4
4
  import { time } from '../../../frameloop/sync-time.mjs';
@@ -37,10 +37,39 @@ const scaleTolerance = 0.001;
37
37
  * rebuilding the WAAPI animation every frame.
38
38
  */
39
39
  const churnWindow = 100;
40
+ /**
41
+ * A WAAPI animation that starts moving later than this, as most segments
42
+ * of a long sequence do, is built this long before it starts, rather than
43
+ * all at once at the start.
44
+ */
45
+ const buildAhead = 100;
40
46
  /**
41
47
  * Named easings that are the same cubic-bezier in JS and CSS.
42
48
  */
43
49
  const isNativeEasing = (ease) => /^(linear|ease(In|Out|InOut))$/.test(ease);
50
+ /**
51
+ * The last trace of each easing WAAPI can't run into linear(), with its
52
+ * number of samples, so the elements of a sequence that share an easing
53
+ * and duration trace it once.
54
+ */
55
+ const traced = new WeakMap();
56
+ const trace = (ease, samples) => {
57
+ let last = traced.get(ease);
58
+ if (last?.[0] !== samples) {
59
+ last = [
60
+ samples,
61
+ "linear(" +
62
+ defaultOffset(Array(samples + 1)).map((p) => round(ease(p)) + " " + round(p * 100) + "%") +
63
+ ")",
64
+ ];
65
+ traced.set(ease, last);
66
+ }
67
+ return last[1];
68
+ };
69
+ /**
70
+ * How many samples trace an easing between two timestamps.
71
+ */
72
+ const countSamples = (from, to) => Math.min(maxSamples, Math.max(2, Math.ceil((to - from) / sampleDelta)));
44
73
  /**
45
74
  * Whether two eased progresses match. False if either is NaN.
46
75
  */
@@ -199,22 +228,48 @@ class TransformAnimation extends JSAnimation {
199
228
  return this.isAccelerated() ? this.sampleAt(time.now()) : undefined;
200
229
  }
201
230
  /**
202
- * If this is a two-keyframe tween whose easing WAAPI runs exactly,
203
- * when it starts and stops moving, and that easing.
231
+ * If this moves between two keyframes only, holding still either side
232
+ * of them, as each value of a sequence segment does, when it starts
233
+ * and stops moving, and its easing: as a string if WAAPI runs it
234
+ * exactly, otherwise its function, to trace.
204
235
  */
205
236
  tween() {
206
- const { keyframes: keyframes$1, type, ease, times, repeat, delay = 0 } = this.options;
207
- const easing = isBezierDefinition(ease)
208
- ? cubicBezierAsString(ease)
209
- : isNativeEasing(ease) && supportedWaapiEasing[ease];
210
- if (easing &&
211
- !repeat &&
212
- !times &&
213
- keyframes$1.length === 2 &&
214
- type === keyframes &&
215
- this.holdTime === null) {
216
- const start = this.startTime + delay / this.speed;
217
- return [start, start + this.resolvedDuration / this.speed, easing];
237
+ const { keyframes: keyframes$1, type, times, repeat, delay = 0 } = this.options;
238
+ let span = -1;
239
+ if (repeat || type !== keyframes || this.holdTime !== null) {
240
+ return;
241
+ }
242
+ for (let i = 1; i < keyframes$1.length; i++) {
243
+ if (keyframes$1[i] !== keyframes$1[i - 1]) {
244
+ if (span >= 0)
245
+ return;
246
+ span = i - 1;
247
+ }
248
+ }
249
+ let { ease } = this.options;
250
+ if (isEasingArray(ease))
251
+ ease = ease[span];
252
+ const offsets = times || defaultOffset(keyframes$1);
253
+ const at = (i) => this.startTime +
254
+ (delay + offsets[i] * this.totalDuration) / this.speed;
255
+ if (span >= 0) {
256
+ const easing = isBezierDefinition(ease)
257
+ ? cubicBezierAsString(ease)
258
+ : isNativeEasing(ease)
259
+ ? supportedWaapiEasing[ease]
260
+ : easingDefinitionToFunction(ease);
261
+ /**
262
+ * An easing that doesn't run from 0 to 1 doesn't move between
263
+ * the keyframes, so is traced from the values it gives instead.
264
+ */
265
+ return [
266
+ at(span),
267
+ at(span + 1),
268
+ typeof easing === "string" ||
269
+ (isNear(easing(0), 0) && isNear(easing(1), 1))
270
+ ? easing
271
+ : undefined,
272
+ ];
218
273
  }
219
274
  }
220
275
  /**
@@ -258,7 +313,14 @@ class TransformAnimation extends JSAnimation {
258
313
  */
259
314
  sync() {
260
315
  const now = time.now();
261
- this.options.motionValue.setWithVelocity(this.sampleAt(now - sampleDelta, true), this.sampleAt(now, true), sampleDelta);
316
+ const value = this.sampleAt(now, true);
317
+ /**
318
+ * A held value doesn't move, so there's no need to sample it
319
+ * twice.
320
+ */
321
+ this.options.motionValue.setWithVelocity(this.holdTime === null
322
+ ? this.sampleAt(now - sampleDelta, true)
323
+ : value, value, sampleDelta);
262
324
  }
263
325
  play() {
264
326
  /**
@@ -272,19 +334,18 @@ class TransformAnimation extends JSAnimation {
272
334
  }
273
335
  pause() {
274
336
  super.pause();
275
- if (this.isAccelerated()) {
276
- this.sync();
277
- this.group.schedule(false);
278
- }
337
+ this.isAccelerated() && this.group.schedule(false);
279
338
  }
280
339
  /**
281
- * JSAnimation finishes on its next tick, which only happens on the
282
- * main thread, so finish now.
340
+ * JSAnimation finishes on its next tick, which only happens once the
341
+ * group is on the main thread, so finish now. That includes an
342
+ * animation that can't be accelerated but reached its end before the
343
+ * group could move there.
283
344
  */
284
345
  complete() {
285
346
  super.complete();
286
347
  const { group } = this;
287
- group && !group.isJS && this.canAccelerate && this.tick(time.now());
348
+ group && !group.isJS && this.tick(time.now());
288
349
  }
289
350
  /**
290
351
  * Without a tick to rebase it, a held time still includes the delay,
@@ -296,14 +357,8 @@ class TransformAnimation extends JSAnimation {
296
357
  }
297
358
  return super.time;
298
359
  }
299
- /**
300
- * A JS animation sets its motion value from a seek on its next tick.
301
- * This doesn't tick, so set it now, rather than leave it to be set
302
- * when this next stops.
303
- */
304
360
  set time(newTime) {
305
361
  super.time = newTime;
306
- this.isAccelerated() && this.sync();
307
362
  }
308
363
  /**
309
364
  * Scroll timelines set time every frame, so run these on the main
@@ -315,6 +370,20 @@ class TransformAnimation extends JSAnimation {
315
370
  return super.attachTimeline(timeline);
316
371
  }
317
372
  }
373
+ /**
374
+ * Groups that build straight after the current task build in one
375
+ * microtask, rather than one each.
376
+ */
377
+ let builds;
378
+ const buildSoon = (build) => {
379
+ builds ||
380
+ queueMicrotask(() => {
381
+ const queue = builds;
382
+ builds = undefined;
383
+ queue.forEach((build) => build());
384
+ });
385
+ (builds || (builds = [])).push(build);
386
+ };
318
387
  function createGroup(owner) {
319
388
  const element = owner.current;
320
389
  const tracks = new Set();
@@ -334,6 +403,7 @@ function createGroup(owner) {
334
403
  let isEnded = false;
335
404
  let hasChurned = false;
336
405
  let lastChurn = -Infinity;
406
+ let wake;
337
407
  const has = (key) => {
338
408
  for (const track of tracks)
339
409
  if (track.name === key)
@@ -387,13 +457,23 @@ function createGroup(owner) {
387
457
  const commit = (values = valuesAt()) => {
388
458
  element.style.transform = toTransform(values, true);
389
459
  };
460
+ /**
461
+ * Cancel a WAAPI animation and remove its handlers, as WebKit never
462
+ * collects an animation once it has run, nor anything its handlers
463
+ * reference (#4034).
464
+ */
465
+ const cancel = (animation) => {
466
+ animation.onfinish = animation.oncancel = null;
467
+ animation.cancel();
468
+ };
390
469
  /**
391
470
  * Cancel the WAAPI animations, by default first committing the
392
471
  * transform. The renderer writes its own version on its next render.
393
472
  */
394
473
  const stop = (shouldCommit = true) => {
474
+ clearTimeout(wake);
395
475
  shouldCommit && commit();
396
- segments.forEach(({ animation }) => animation.cancel());
476
+ segments.forEach(({ animation }) => cancel(animation));
397
477
  segments = [];
398
478
  };
399
479
  /**
@@ -414,7 +494,7 @@ function createGroup(owner) {
414
494
  animation.onfinish = () => {
415
495
  segment.isDone = true;
416
496
  if (segments.some((s) => s.end === Infinity)) {
417
- animation.cancel();
497
+ cancel(animation);
418
498
  segments = segments.filter((s) => s !== segment);
419
499
  }
420
500
  group.schedule(false);
@@ -445,12 +525,31 @@ function createGroup(owner) {
445
525
  * two timestamps, the easing that describes it.
446
526
  */
447
527
  const getEasing = (moving, from, to, bounds) => {
448
- const samples = Math.min(maxSamples, Math.max(2, Math.ceil((to - from) / sampleDelta)));
528
+ const samples = countSamples(from, to);
449
529
  /**
450
530
  * Too many keyframes or repeats to follow with this many samples.
451
531
  */
452
532
  if (bounds.length * 4 > samples)
453
533
  return;
534
+ /**
535
+ * First check half way, where values with different easings
536
+ * usually differ, before sampling every value many times.
537
+ */
538
+ let quick;
539
+ for (const track of moving) {
540
+ const first = track.numberAt(from);
541
+ const p = (track.numberAt((from + to) / 2) - first) /
542
+ (track.numberAt(to) - first);
543
+ /**
544
+ * NaN if it doesn't move.
545
+ */
546
+ if (p !== p)
547
+ continue;
548
+ if (quick === undefined)
549
+ quick = p;
550
+ else if (!isNear(p, quick))
551
+ return;
552
+ }
454
553
  const at = spread(from, to, samples).concat(bounds).sort(byTime);
455
554
  const position = (t) => progress(from, to, t);
456
555
  let shared;
@@ -507,11 +606,30 @@ function createGroup(owner) {
507
606
  */
508
607
  const sample = (moving, from, to, bounds) => {
509
608
  const times = [from];
609
+ /**
610
+ * A linear tween is straight between its keyframes, so only the
611
+ * other values can curve, and a tween only while it moves.
612
+ */
613
+ const curved = [];
614
+ let curveFrom = to;
615
+ let curveTo = from;
616
+ for (const track of moving) {
617
+ const tween = track.tween();
618
+ if (tween?.[2] !== "linear") {
619
+ const [trackEnd, cycle] = track.timing();
620
+ const [a, b] = tween || [from, cycle ? to : trackEnd];
621
+ curveFrom = Math.min(curveFrom, a);
622
+ curveTo = Math.max(curveTo, b);
623
+ curved.push(track);
624
+ }
625
+ }
626
+ curveFrom = Math.max(from, curveFrom);
627
+ curveTo = Math.min(to, curveTo);
510
628
  /**
511
629
  * Values that aren't numbers, like calc(), are left to the
512
630
  * browser to interpolate.
513
631
  */
514
- const isStraight = (a, b) => moving.every((track) => !(Math.abs(track.numberAt((a + b) / 2) -
632
+ const isStraight = (a, b) => curved.every((track) => !(Math.abs(track.numberAt((a + b) / 2) -
515
633
  (track.numberAt(a) + track.numberAt(b)) / 2) >
516
634
  (track.name.startsWith("scale")
517
635
  ? scaleTolerance
@@ -536,10 +654,13 @@ function createGroup(owner) {
536
654
  }
537
655
  };
538
656
  /**
539
- * Split at least every 100ms, and at every keyframe and repeat,
540
- * either side of where a value can jump.
657
+ * Split at least every 100ms where a value can curve, and at every
658
+ * keyframe and repeat, either side of where a value can jump.
541
659
  */
542
- const ends = spread(from, to, Math.ceil((to - from) / 100));
660
+ const ends = curveTo > curveFrom
661
+ ? spread(curveFrom, curveTo, Math.ceil((curveTo - curveFrom) / 100))
662
+ : [];
663
+ ends.push(to);
543
664
  for (const t of bounds)
544
665
  ends.push(t, Math.min(t + 0.01, to));
545
666
  let start = from;
@@ -559,12 +680,20 @@ function createGroup(owner) {
559
680
  for (const track of moving) {
560
681
  const tween = track.tween();
561
682
  if (!tween ||
562
- shared?.some((v, i) => i < 2
563
- ? Math.abs(tween[i] - v) > 1
564
- : tween[i] !== v)) {
683
+ (shared &&
684
+ (Math.abs(tween[0] - shared[0]) > 1 ||
685
+ Math.abs(tween[1] - shared[1]) > 1))) {
565
686
  return;
566
687
  }
567
- shared = tween;
688
+ /**
689
+ * Easings that differ, like the springs a sequence makes for
690
+ * each value, can still be the same curve.
691
+ */
692
+ shared = [
693
+ tween[0],
694
+ tween[1],
695
+ !shared || tween[2] === shared[2] ? tween[2] : undefined,
696
+ ];
568
697
  }
569
698
  return shared;
570
699
  };
@@ -619,14 +748,8 @@ function createGroup(owner) {
619
748
  const [trackEnd, trackCycle] = track.timing();
620
749
  const isHeld = trackEnd === Infinity;
621
750
  built.set(track, [track.at(now), track.speed, isHeld]);
622
- /**
623
- * Held animations don't move, so keep their motion values up
624
- * to date, as a JSAnimation would on a seek.
625
- */
626
- if (isHeld) {
627
- track.sync();
751
+ if (isHeld)
628
752
  continue;
629
- }
630
753
  if (track.speed < 0)
631
754
  return;
632
755
  if (trackCycle) {
@@ -666,12 +789,35 @@ function createGroup(owner) {
666
789
  */
667
790
  const tween = !cycle && getTween(moving);
668
791
  try {
669
- if (tween && tween[1] > now) {
670
- const [from, to, easing] = tween;
671
- add(run(moving, from, to, easing), from - now, to - from);
672
- }
673
- else if (end > now) {
674
- add(run(moving, now, end), 0, end - now);
792
+ if (end > now) {
793
+ let easing;
794
+ if (tween && tween[1] > now) {
795
+ const [from, to, ease] = tween;
796
+ /**
797
+ * Until it's built, the motion values hold where it
798
+ * starts, for the renderer to write.
799
+ */
800
+ if (from - now > buildAhead) {
801
+ moving.forEach((track) => track.sync());
802
+ wake = setTimeout(() => group.schedule(true), from - now - buildAhead);
803
+ return true;
804
+ }
805
+ easing =
806
+ typeof ease === "string"
807
+ ? ease
808
+ : ease
809
+ ? trace(ease, countSamples(from, to))
810
+ : getEasing(moving, from, to, []);
811
+ /**
812
+ * It holds its last keyframe until the animations end.
813
+ */
814
+ easing &&
815
+ add(run(moving, from, to, easing, {
816
+ fill: "both",
817
+ endDelay: end - to,
818
+ }), from - now, end - from);
819
+ }
820
+ easing || add(run(moving, now, end), 0, end - now);
675
821
  /**
676
822
  * Animations that end sooner finish on the events of
677
823
  * empty animations that end with them.
@@ -720,9 +866,14 @@ function createGroup(owner) {
720
866
  const [builtTime, builtSpeed, wasHeld] = state;
721
867
  const trackTime = track.at(now);
722
868
  const isHeld = track.timing()[0] === Infinity;
869
+ /**
870
+ * With nothing on the compositor, a held animation is set
871
+ * where it's held, so it can be seeked as often as needed.
872
+ */
723
873
  if (wasHeld) {
724
- if (isHeld && trackTime === builtTime)
874
+ if (isHeld && (trackTime === builtTime || !segments.length)) {
725
875
  continue;
876
+ }
726
877
  return false;
727
878
  }
728
879
  const trackElapsed = (trackTime - builtTime) / builtSpeed;
@@ -852,6 +1003,12 @@ function createGroup(owner) {
852
1003
  if (!animate(now))
853
1004
  return group.demote();
854
1005
  }
1006
+ /**
1007
+ * Held animations don't move, so set their motion values to where
1008
+ * they're held, as a JSAnimation would on its next tick. Pausing
1009
+ * and seeking schedule this, so it's once however many there are.
1010
+ */
1011
+ tracks.forEach((track) => track.timing()[0] === Infinity && track.sync());
855
1012
  };
856
1013
  const group = {
857
1014
  tracks,
@@ -871,7 +1028,7 @@ function createGroup(owner) {
871
1028
  isScheduled = true;
872
1029
  frameData.isProcessing
873
1030
  ? frame.render(build, false, true)
874
- : queueMicrotask(build);
1031
+ : buildSoon(build);
875
1032
  }
876
1033
  },
877
1034
  /**