motion-dom 13.2.0 → 13.3.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.
Files changed (89) hide show
  1. package/dist/cjs/index.js +3961 -3188
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/es/animation/AsyncMotionValueAnimation.mjs +41 -38
  4. package/dist/es/animation/AsyncMotionValueAnimation.mjs.map +1 -1
  5. package/dist/es/animation/FollowAnimation.mjs +152 -0
  6. package/dist/es/animation/FollowAnimation.mjs.map +1 -0
  7. package/dist/es/animation/JSAnimation.mjs +7 -11
  8. package/dist/es/animation/JSAnimation.mjs.map +1 -1
  9. package/dist/es/animation/NativeAnimation.mjs +2 -0
  10. package/dist/es/animation/NativeAnimation.mjs.map +1 -1
  11. package/dist/es/animation/NativeAnimationWrapper.mjs +7 -0
  12. package/dist/es/animation/NativeAnimationWrapper.mjs.map +1 -1
  13. package/dist/es/animation/animate/effects.mjs +70 -13
  14. package/dist/es/animation/animate/effects.mjs.map +1 -1
  15. package/dist/es/animation/animate/element.mjs +80 -0
  16. package/dist/es/animation/animate/element.mjs.map +1 -0
  17. package/dist/es/animation/animate/single-value.mjs.map +1 -1
  18. package/dist/es/animation/generators/keyframes.mjs +25 -1
  19. package/dist/es/animation/generators/keyframes.mjs.map +1 -1
  20. package/dist/es/animation/generators/spring.mjs +100 -52
  21. package/dist/es/animation/generators/spring.mjs.map +1 -1
  22. package/dist/es/animation/generators/utils/velocity.mjs +13 -1
  23. package/dist/es/animation/generators/utils/velocity.mjs.map +1 -1
  24. package/dist/es/animation/interfaces/motion-value.mjs.map +1 -1
  25. package/dist/es/animation/interfaces/visual-element-target.mjs.map +1 -1
  26. package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs +12 -6
  27. package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs.map +1 -1
  28. package/dist/es/animation/keyframes/KeyframesResolver.mjs +38 -7
  29. package/dist/es/animation/keyframes/KeyframesResolver.mjs.map +1 -1
  30. package/dist/es/animation/keyframes/utils/make-none-animatable.mjs +8 -2
  31. package/dist/es/animation/keyframes/utils/make-none-animatable.mjs.map +1 -1
  32. package/dist/es/animation/keyframes/utils/unit-conversion.mjs +45 -19
  33. package/dist/es/animation/keyframes/utils/unit-conversion.mjs.map +1 -1
  34. package/dist/es/animation/utils/WithPromise.mjs +12 -5
  35. package/dist/es/animation/utils/WithPromise.mjs.map +1 -1
  36. package/dist/es/animation/utils/arc.mjs +2 -1
  37. package/dist/es/animation/utils/arc.mjs.map +1 -1
  38. package/dist/es/animation/utils/can-animate.mjs +3 -1
  39. package/dist/es/animation/utils/can-animate.mjs.map +1 -1
  40. package/dist/es/animation/utils/notify-inspector.mjs +56 -0
  41. package/dist/es/animation/utils/notify-inspector.mjs.map +1 -0
  42. package/dist/es/animation/waapi/supports/waapi.mjs +8 -1
  43. package/dist/es/animation/waapi/supports/waapi.mjs.map +1 -1
  44. package/dist/es/effects/MotionValueState.mjs +83 -25
  45. package/dist/es/effects/MotionValueState.mjs.map +1 -1
  46. package/dist/es/effects/attr/index.mjs +15 -9
  47. package/dist/es/effects/attr/index.mjs.map +1 -1
  48. package/dist/es/effects/prop/index.mjs +15 -2
  49. package/dist/es/effects/prop/index.mjs.map +1 -1
  50. package/dist/es/effects/style/index.mjs +52 -9
  51. package/dist/es/effects/style/index.mjs.map +1 -1
  52. package/dist/es/effects/style/transform.mjs +28 -25
  53. package/dist/es/effects/style/transform.mjs.map +1 -1
  54. package/dist/es/effects/svg/index.mjs +40 -7
  55. package/dist/es/effects/svg/index.mjs.map +1 -1
  56. package/dist/es/effects/utils/create-dom-effect.mjs +9 -1
  57. package/dist/es/effects/utils/create-dom-effect.mjs.map +1 -1
  58. package/dist/es/effects/utils/create-effect.mjs +2 -0
  59. package/dist/es/effects/utils/create-effect.mjs.map +1 -1
  60. package/dist/es/frameloop/render-step.mjs +5 -2
  61. package/dist/es/frameloop/render-step.mjs.map +1 -1
  62. package/dist/es/index.mjs +5 -4
  63. package/dist/es/index.mjs.map +1 -1
  64. package/dist/es/layout/LayoutAnimationBuilder.mjs +9 -1
  65. package/dist/es/layout/LayoutAnimationBuilder.mjs.map +1 -1
  66. package/dist/es/projection/node/create-projection-node.mjs +4 -1
  67. package/dist/es/projection/node/create-projection-node.mjs.map +1 -1
  68. package/dist/es/render/VisualElement.mjs +11 -2
  69. package/dist/es/render/VisualElement.mjs.map +1 -1
  70. package/dist/es/render/utils/is-controlling-variants.mjs +8 -2
  71. package/dist/es/render/utils/is-controlling-variants.mjs.map +1 -1
  72. package/dist/es/utils/mix/color.mjs +3 -2
  73. package/dist/es/utils/mix/color.mjs.map +1 -1
  74. package/dist/es/utils/mix/index.mjs +28 -0
  75. package/dist/es/utils/mix/index.mjs.map +1 -1
  76. package/dist/es/value/follow-value.mjs +33 -30
  77. package/dist/es/value/follow-value.mjs.map +1 -1
  78. package/dist/es/value/index.mjs +73 -29
  79. package/dist/es/value/index.mjs.map +1 -1
  80. package/dist/es/value/types/complex/index.mjs +17 -4
  81. package/dist/es/value/types/complex/index.mjs.map +1 -1
  82. package/dist/index.d.ts +245 -50
  83. package/dist/motion-dom.dev.js +3961 -3188
  84. package/dist/motion-dom.js +1 -1
  85. package/dist/size-rollup-motion-value.js +1 -1
  86. package/dist/size-rollup-motion-value.js.map +1 -1
  87. package/dist/size-rollup-style-effect.js +1 -1
  88. package/dist/size-rollup-style-effect.js.map +1 -1
  89. package/package.json +4 -4
@@ -1,4 +1,5 @@
1
1
  import { NativeAnimation } from './NativeAnimation.mjs';
2
+ import { notifyAnimationStart } from './utils/notify-inspector.mjs';
2
3
 
3
4
  class NativeAnimationWrapper extends NativeAnimation {
4
5
  constructor(animation) {
@@ -8,6 +9,12 @@ class NativeAnimationWrapper extends NativeAnimation {
8
9
  this.finishedTime = this.time;
9
10
  this.notifyFinished();
10
11
  };
12
+ /**
13
+ * The optionless super() returns before NativeAnimation's own
14
+ * notify, so wrapped animations (view transition cross-fades)
15
+ * report here, after the wrapped WAAPI animation is attached.
16
+ */
17
+ notifyAnimationStart(this, {});
11
18
  }
12
19
  }
13
20
 
@@ -1 +1 @@
1
- {"version":3,"file":"NativeAnimationWrapper.mjs","sources":["../../../src/animation/NativeAnimationWrapper.ts"],"sourcesContent":["import { NativeAnimation } from \"./NativeAnimation\"\nimport { AnyResolvedKeyframe } from \"./types\"\n\nexport class NativeAnimationWrapper<\n T extends AnyResolvedKeyframe\n> extends NativeAnimation<T> {\n constructor(animation: Animation) {\n super()\n\n this.animation = animation\n animation.onfinish = () => {\n this.finishedTime = this.time\n this.notifyFinished()\n }\n }\n}\n"],"names":[],"mappings":";;AAGM,MAAO,sBAEX,SAAQ,eAAkB,CAAA;AACxB,IAAA,WAAA,CAAY,SAAoB,EAAA;AAC5B,QAAA,KAAK,EAAE;AAEP,QAAA,IAAI,CAAC,SAAS,GAAG,SAAS;AAC1B,QAAA,SAAS,CAAC,QAAQ,GAAG,MAAK;AACtB,YAAA,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,IAAI;YAC7B,IAAI,CAAC,cAAc,EAAE;AACzB,QAAA,CAAC;IACL;AACH;;;;"}
1
+ {"version":3,"file":"NativeAnimationWrapper.mjs","sources":["../../../src/animation/NativeAnimationWrapper.ts"],"sourcesContent":["import { NativeAnimation } from \"./NativeAnimation\"\nimport { AnyResolvedKeyframe } from \"./types\"\nimport { notifyAnimationStart } from \"./utils/notify-inspector\"\n\nexport class NativeAnimationWrapper<\n T extends AnyResolvedKeyframe\n> extends NativeAnimation<T> {\n constructor(animation: Animation) {\n super()\n\n this.animation = animation\n animation.onfinish = () => {\n this.finishedTime = this.time\n this.notifyFinished()\n }\n\n /**\n * The optionless super() returns before NativeAnimation's own\n * notify, so wrapped animations (view transition cross-fades)\n * report here, after the wrapped WAAPI animation is attached.\n */\n notifyAnimationStart(this, {})\n }\n}\n"],"names":[],"mappings":";;;AAIM,MAAO,sBAEX,SAAQ,eAAkB,CAAA;AACxB,IAAA,WAAA,CAAY,SAAoB,EAAA;AAC5B,QAAA,KAAK,EAAE;AAEP,QAAA,IAAI,CAAC,SAAS,GAAG,SAAS;AAC1B,QAAA,SAAS,CAAC,QAAQ,GAAG,MAAK;AACtB,YAAA,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,IAAI;YAC7B,IAAI,CAAC,cAAc,EAAE;AACzB,QAAA,CAAC;AAED;;;;AAIG;AACH,QAAA,oBAAoB,CAAC,IAAI,EAAE,EAAE,CAAC;IAClC;AACH;;;;"}
@@ -1,7 +1,8 @@
1
1
  import { invariant, removeItem } from 'motion-utils';
2
+ import { positionalKeys } from '../../render/utils/keys-position.mjs';
2
3
  import { motionValue } from '../../value/index.mjs';
3
4
  import { animateMotionValue } from '../interfaces/motion-value.mjs';
4
- import { getValueTransition } from '../utils/get-value-transition.mjs';
5
+ import { frame } from '../../frameloop/frame.mjs';
5
6
 
6
7
  /**
7
8
  * Effects registered via `animate.addEffect()`, most recent first.
@@ -19,30 +20,86 @@ function findEffect(subject) {
19
20
  return effects.find((effect) => effect.test(subject));
20
21
  }
21
22
  /**
22
- * Animate the keys of `subject` via `effect`. Motion values are created on
23
- * first animation, seeded from `effect.read()` or the first keyframe, and
24
- * then bound to the subject via the effect for the rest of its life.
23
+ * Animate each key in `keyframes` on the motion value `getValue` returns
24
+ * for it. `element` is what the keyframe resolver uses to read, render
25
+ * and measure the subject when the effect defers resolution to it.
25
26
  */
26
- function animateEffectSubject(effect, subject, keyframes, transition = {}) {
27
+ function animateValues(getValue, keyframes, transition = {}, element) {
27
28
  const animations = [];
29
+ const { velocity } = transition;
30
+ const reduceMotion = transition.reduceMotion ?? element?.shouldReduceMotion;
28
31
  for (const key in keyframes) {
32
+ if (key === "transition" || key === "transitionEnd")
33
+ continue;
29
34
  const target = keyframes[key];
35
+ if (target === undefined)
36
+ continue;
37
+ const value = getValue(key);
38
+ /**
39
+ * If the value is already at the defined target, skip the animation.
40
+ * We still re-assert the value via frame.update to take precedence
41
+ * over any stale transitionEnd callbacks from previous animations.
42
+ */
43
+ const current = value.get();
44
+ if (current !== undefined &&
45
+ !value.isAnimating() &&
46
+ !Array.isArray(target) &&
47
+ target === current &&
48
+ !velocity) {
49
+ frame.update(() => value.set(target));
50
+ continue;
51
+ }
52
+ value.start(animateMotionValue(key, value, target, reduceMotion && positionalKeys.has(key)
53
+ ? { type: false }
54
+ : transition, element));
55
+ value.animation &&
56
+ animations.push(value.animation);
57
+ }
58
+ const { transitionEnd } = keyframes;
59
+ if (transitionEnd) {
60
+ const applyTransitionEnd = () => frame.update(() => {
61
+ for (const key in transitionEnd) {
62
+ getValue(key).set(transitionEnd[key]);
63
+ }
64
+ });
65
+ animations.length
66
+ ? Promise.all(animations).then(applyTransitionEnd)
67
+ : applyTransitionEnd();
68
+ }
69
+ return animations;
70
+ }
71
+ /**
72
+ * Animate the keys of `subject` via `effect`. Motion values are created on
73
+ * first animation and bound to the subject through the effect for the
74
+ * rest of its life.
75
+ *
76
+ * Without `element`, a new value is seeded synchronously from the first
77
+ * keyframe or `effect.read()`. With it, the value starts undefined and the
78
+ * keyframe resolver reads the subject (batched, with unit conversion by
79
+ * measurement) via `element`.
80
+ */
81
+ function animateEffectSubject(effect, subject, keyframes, transition, element) {
82
+ return animateValues((key) => {
30
83
  let value = effect.get(subject, key);
31
84
  if (!value) {
32
- const initial = effect.read(subject, key, target) ?? firstKeyframe(target);
33
- invariant(initial !== undefined, `"${key}" can't be read from the animated subject. Provide [from, to] keyframes.`, "effect-unreadable-value");
34
- value = motionValue(initial);
85
+ let initial;
86
+ if (!element) {
87
+ const target = keyframes[key];
88
+ initial =
89
+ firstKeyframe(target) ??
90
+ effect.read(subject, key, target);
91
+ invariant(initial !== undefined, `"${key}" can't be read from the animated subject. Provide [from, to] keyframes.`, "effect-unreadable-value");
92
+ }
93
+ value = motionValue(initial, { owner: element });
35
94
  effect(subject, { [key]: value });
36
95
  }
37
- value.start(animateMotionValue(key, value, target, getValueTransition(transition, key)));
38
- value.animation && animations.push(value.animation);
39
- }
40
- return animations;
96
+ return value;
97
+ }, keyframes, transition, element);
41
98
  }
42
99
  function firstKeyframe(target) {
43
100
  const first = Array.isArray(target) ? target[0] : undefined;
44
101
  return first === null ? undefined : first;
45
102
  }
46
103
 
47
- export { addEffect, animateEffectSubject, findEffect, removeEffect };
104
+ export { addEffect, animateEffectSubject, animateValues, findEffect, removeEffect };
48
105
  //# sourceMappingURL=effects.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"effects.mjs","sources":["../../../../src/animation/animate/effects.ts"],"sourcesContent":["import { invariant, removeItem } from \"motion-utils\"\nimport { AnimateEffect } from \"../../effects/utils/create-effect\"\nimport { motionValue } from \"../../value\"\nimport { animateMotionValue } from \"../interfaces/motion-value\"\nimport {\n AnimationPlaybackControlsWithThen,\n UnresolvedValueKeyframe,\n ValueAnimationTransition,\n} from \"../types\"\nimport { getValueTransition } from \"../utils/get-value-transition\"\n\n/**\n * Effects registered via `animate.addEffect()`, most recent first.\n */\nconst effects: AnimateEffect<any>[] = []\n\nexport function addEffect(effect: AnimateEffect<any>): void {\n invariant(\n typeof effect.test === \"function\" && typeof effect.read === \"function\",\n \"Effects passed to animate.addEffect() need test() and read().\",\n \"effect-missing-test\"\n )\n\n removeEffect(effect)\n effects.unshift(effect)\n}\n\nexport function removeEffect(effect: AnimateEffect<any>): void {\n removeItem(effects, effect)\n}\n\nexport function findEffect(subject: unknown): AnimateEffect | undefined {\n return effects.find((effect) => effect.test(subject))\n}\n\nexport type EffectKeyframes = Record<\n string,\n UnresolvedValueKeyframe | UnresolvedValueKeyframe[]\n>\n\n/**\n * Effect subjects can expose any key, so per-key transition overrides\n * are keyed by arbitrary strings.\n */\nexport type EffectTransition = ValueAnimationTransition &\n Record<string, unknown>\n\n/**\n * Animate the keys of `subject` via `effect`. Motion values are created on\n * first animation, seeded from `effect.read()` or the first keyframe, and\n * then bound to the subject via the effect for the rest of its life.\n */\nexport function animateEffectSubject<Subject extends object>(\n effect: AnimateEffect<Subject>,\n subject: Subject,\n keyframes: EffectKeyframes,\n transition: EffectTransition = {}\n): AnimationPlaybackControlsWithThen[] {\n const animations: AnimationPlaybackControlsWithThen[] = []\n\n for (const key in keyframes) {\n const target = keyframes[key]\n let value = effect.get(subject, key)\n\n if (!value) {\n const initial =\n effect.read(subject, key, target) ?? firstKeyframe(target)\n\n invariant(\n initial !== undefined,\n `\"${key}\" can't be read from the animated subject. Provide [from, to] keyframes.`,\n \"effect-unreadable-value\"\n )\n\n value = motionValue(initial)\n effect(subject, { [key]: value })\n }\n\n value.start(\n animateMotionValue(\n key,\n value,\n target as any,\n getValueTransition(transition, key)\n )\n )\n\n value.animation && animations.push(value.animation)\n }\n\n return animations\n}\n\nfunction firstKeyframe(\n target: UnresolvedValueKeyframe | UnresolvedValueKeyframe[]\n) {\n const first = Array.isArray(target) ? target[0] : undefined\n return first === null ? undefined : first\n}\n"],"names":[],"mappings":";;;;;AAWA;;AAEG;AACH,MAAM,OAAO,GAAyB,EAAE;AAElC,SAAU,SAAS,CAAC,MAA0B,EAAA;AAChD,IAAA,SAAS,CACL,OAAO,MAAM,CAAC,IAAI,KAAK,UAAU,IAAI,OAAO,MAAM,CAAC,IAAI,KAAK,UAAU,EACtE,+DAA+D,EAC/D,qBAAqB,CACxB;IAED,YAAY,CAAC,MAAM,CAAC;AACpB,IAAA,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC;AAC3B;AAEM,SAAU,YAAY,CAAC,MAA0B,EAAA;AACnD,IAAA,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC;AAC/B;AAEM,SAAU,UAAU,CAAC,OAAgB,EAAA;AACvC,IAAA,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AACzD;AAcA;;;;AAIG;AACG,SAAU,oBAAoB,CAChC,MAA8B,EAC9B,OAAgB,EAChB,SAA0B,EAC1B,UAAA,GAA+B,EAAE,EAAA;IAEjC,MAAM,UAAU,GAAwC,EAAE;AAE1D,IAAA,KAAK,MAAM,GAAG,IAAI,SAAS,EAAE;AACzB,QAAA,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,CAAC;QAC7B,IAAI,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC;QAEpC,IAAI,CAAC,KAAK,EAAE;AACR,YAAA,MAAM,OAAO,GACT,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,MAAM,CAAC,IAAI,aAAa,CAAC,MAAM,CAAC;YAE9D,SAAS,CACL,OAAO,KAAK,SAAS,EACrB,CAAA,CAAA,EAAI,GAAG,CAAA,wEAAA,CAA0E,EACjF,yBAAyB,CAC5B;AAED,YAAA,KAAK,GAAG,WAAW,CAAC,OAAO,CAAC;YAC5B,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC,GAAG,GAAG,KAAK,EAAE,CAAC;QACrC;AAEA,QAAA,KAAK,CAAC,KAAK,CACP,kBAAkB,CACd,GAAG,EACH,KAAK,EACL,MAAa,EACb,kBAAkB,CAAC,UAAU,EAAE,GAAG,CAAC,CACtC,CACJ;QAED,KAAK,CAAC,SAAS,IAAI,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC;IACvD;AAEA,IAAA,OAAO,UAAU;AACrB;AAEA,SAAS,aAAa,CAClB,MAA2D,EAAA;AAE3D,IAAA,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,SAAS;IAC3D,OAAO,KAAK,KAAK,IAAI,GAAG,SAAS,GAAG,KAAK;AAC7C;;;;"}
1
+ {"version":3,"file":"effects.mjs","sources":["../../../../src/animation/animate/effects.ts"],"sourcesContent":["import { invariant, removeItem } from \"motion-utils\"\nimport { AnimateEffect } from \"../../effects/utils/create-effect\"\nimport { frame } from \"../../frameloop\"\nimport { positionalKeys } from \"../../render/utils/keys-position\"\nimport { MotionValue, motionValue } from \"../../value\"\nimport { animateMotionValue } from \"../interfaces/motion-value\"\nimport { AnimationElement } from \"../keyframes/types\"\nimport {\n AnimationPlaybackControlsWithThen,\n AnyResolvedKeyframe,\n UnresolvedValueKeyframe,\n ValueTransition,\n} from \"../types\"\n\n/**\n * Effects registered via `animate.addEffect()`, most recent first.\n */\nconst effects: AnimateEffect<any>[] = []\n\nexport function addEffect(effect: AnimateEffect<any>): void {\n invariant(\n typeof effect.test === \"function\" && typeof effect.read === \"function\",\n \"Effects passed to animate.addEffect() need test() and read().\",\n \"effect-missing-test\"\n )\n\n removeEffect(effect)\n effects.unshift(effect)\n}\n\nexport function removeEffect(effect: AnimateEffect<any>): void {\n removeItem(effects, effect)\n}\n\nexport function findEffect(subject: unknown): AnimateEffect | undefined {\n return effects.find((effect) => effect.test(subject))\n}\n\nexport interface EffectKeyframes {\n [key: string]:\n | UnresolvedValueKeyframe\n | UnresolvedValueKeyframe[]\n | Record<string, unknown>\n | undefined\n transition?: Record<string, unknown>\n transitionEnd?: Record<string, AnyResolvedKeyframe>\n}\n\n/**\n * Effect subjects can expose any key, so per-key transition overrides\n * are keyed by arbitrary strings.\n */\nexport type EffectTransition = ValueTransition & {\n reduceMotion?: boolean\n skipAnimations?: boolean\n} & Record<string, unknown>\n\n/**\n * Animate each key in `keyframes` on the motion value `getValue` returns\n * for it. `element` is what the keyframe resolver uses to read, render\n * and measure the subject when the effect defers resolution to it.\n */\nexport function animateValues(\n getValue: (key: string) => MotionValue,\n keyframes: EffectKeyframes,\n transition: EffectTransition = {},\n element?: AnimationElement\n): AnimationPlaybackControlsWithThen[] {\n const animations: AnimationPlaybackControlsWithThen[] = []\n const { velocity } = transition\n const reduceMotion = transition.reduceMotion ?? element?.shouldReduceMotion\n\n for (const key in keyframes) {\n if (key === \"transition\" || key === \"transitionEnd\") continue\n\n const target = keyframes[key]\n if (target === undefined) continue\n\n const value = getValue(key)\n\n /**\n * If the value is already at the defined target, skip the animation.\n * We still re-assert the value via frame.update to take precedence\n * over any stale transitionEnd callbacks from previous animations.\n */\n const current = value.get()\n if (\n current !== undefined &&\n !value.isAnimating() &&\n !Array.isArray(target) &&\n target === current &&\n !velocity\n ) {\n frame.update(() => value.set(target))\n continue\n }\n\n value.start(\n animateMotionValue(\n key,\n value,\n target as any,\n reduceMotion && positionalKeys.has(key)\n ? { type: false }\n : transition,\n element\n )\n )\n\n value.animation &&\n animations.push(\n value.animation as AnimationPlaybackControlsWithThen\n )\n }\n\n const { transitionEnd } = keyframes\n if (transitionEnd) {\n const applyTransitionEnd = () =>\n frame.update(() => {\n for (const key in transitionEnd) {\n getValue(key).set(transitionEnd[key])\n }\n })\n\n animations.length\n ? Promise.all(animations).then(applyTransitionEnd)\n : applyTransitionEnd()\n }\n\n return animations\n}\n\n/**\n * Animate the keys of `subject` via `effect`. Motion values are created on\n * first animation and bound to the subject through the effect for the\n * rest of its life.\n *\n * Without `element`, a new value is seeded synchronously from the first\n * keyframe or `effect.read()`. With it, the value starts undefined and the\n * keyframe resolver reads the subject (batched, with unit conversion by\n * measurement) via `element`.\n */\nexport function animateEffectSubject<Subject extends object>(\n effect: AnimateEffect<Subject>,\n subject: Subject,\n keyframes: EffectKeyframes,\n transition?: EffectTransition,\n element?: AnimationElement\n): AnimationPlaybackControlsWithThen[] {\n return animateValues(\n (key) => {\n let value = effect.get(subject, key)\n\n if (!value) {\n let initial: AnyResolvedKeyframe | undefined\n\n if (!element) {\n const target = keyframes[key] as\n | UnresolvedValueKeyframe\n | UnresolvedValueKeyframe[]\n initial =\n firstKeyframe(target) ??\n effect.read(subject, key, target)\n\n invariant(\n initial !== undefined,\n `\"${key}\" can't be read from the animated subject. Provide [from, to] keyframes.`,\n \"effect-unreadable-value\"\n )\n }\n\n value = motionValue(initial, { owner: element })\n effect(subject, { [key]: value })\n }\n\n return value\n },\n keyframes,\n transition,\n element\n )\n}\n\nfunction firstKeyframe(\n target: UnresolvedValueKeyframe | UnresolvedValueKeyframe[] | undefined\n) {\n const first = Array.isArray(target) ? target[0] : undefined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OAAwC;YAE5C,IAAI,CAAC,OAAO,EAAE;AACV,gBAAA,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,CAEG;gBAC/B,OAAO;oBACH,aAAa,CAAC,MAAM,CAAC;wBACrB,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,MAAM,CAAC;gBAErC,SAAS,CACL,OAAO,KAAK,SAAS,EACrB,CAAA,CAAA,EAAI,GAAG,CAAA,wEAAA,CAA0E,EACjF,yBAAyB,CAC5B;YACL;YAEA,KAAK,GAAG,WAAW,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;YAChD,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC,GAAG,GAAG,KAAK,EAAE,CAAC;QACrC;AAEA,QAAA,OAAO,KAAK;AAChB,IAAA,CAAC,EACD,SAAS,EACT,UAAU,EACV,OAAO,CACV;AACL;AAEA,SAAS,aAAa,CAClB,MAAuE,EAAA;AAEvE,IAAA,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,SAAS;IAC3D,OAAO,KAAK,KAAK,IAAI,GAAG,SAAS,GAAG,KAAK;AAC7C;;;;"}
@@ -0,0 +1,80 @@
1
+ import { styleSubjectEffect } from '../../effects/style/index.mjs';
2
+ import { svgSubjectEffect } from '../../effects/svg/index.mjs';
3
+ import { measureViewportBox } from '../../projection/utils/measure.mjs';
4
+ import { isSVGElement } from '../../utils/is-svg-element.mjs';
5
+ import { DOMKeyframesResolver } from '../keyframes/DOMKeyframesResolver.mjs';
6
+ import { animateValues, animateEffectSubject } from './effects.mjs';
7
+
8
+ const noProps = {};
9
+ const getElementEffect = (element) => (isSVGElement(element)
10
+ ? svgSubjectEffect
11
+ : styleSubjectEffect);
12
+ /**
13
+ * Presents an effect bound to a DOM element in the shape the DOM keyframe
14
+ * resolver and WAAPI expect, which is that of a VisualElement. Holds no
15
+ * state: everything delegates to the effect. Goes once the resolver takes
16
+ * an effect and subject directly.
17
+ */
18
+ class EffectSubject {
19
+ constructor(effect, current) {
20
+ this.effect = effect;
21
+ this.current = current;
22
+ this.KeyframeResolver = DOMKeyframesResolver;
23
+ }
24
+ getValue(key) {
25
+ return this.effect.get(this.current, key);
26
+ }
27
+ readValue(key, target) {
28
+ return this.effect.read(this.current, key, target);
29
+ }
30
+ render() {
31
+ this.effect.flush(this.current);
32
+ }
33
+ measureViewportBox() {
34
+ return measureViewportBox(this.current);
35
+ }
36
+ getProps() {
37
+ return noProps;
38
+ }
39
+ }
40
+ /**
41
+ * Animate a DOM element's styles. HTML elements are bound through
42
+ * styleEffect, SVG through svgEffect, and both are resolved with the DOM
43
+ * keyframe resolver so reads are batched, units are converted by
44
+ * measurement and eligible values run on WAAPI.
45
+ *
46
+ * Pass the element's VisualElement (a <motion.*> component or an
47
+ * animateLayout() node) to animate its values instead, so the two keep
48
+ * sharing values and a renderer.
49
+ */
50
+ function animateElement(element, keyframes, transition, visualElement) {
51
+ if (visualElement) {
52
+ return animateValues((key) => visualElement.getValue(key, null), keyframes, transition, visualElement);
53
+ }
54
+ const effect = getElementEffect(element);
55
+ return animateEffectSubject(effect, element, keyframes, transition, new EffectSubject(effect, element));
56
+ }
57
+ /**
58
+ * Hand the values styleEffect/svgEffect are rendering on `element`,
59
+ * whether bound by animate() or directly, to a VisualElement that now
60
+ * owns the element (e.g. created by animateLayout()), so a single
61
+ * renderer drives them alongside its own values.
62
+ */
63
+ function handOffElementState(element, visualElement) {
64
+ const state = getElementEffect(element).state(element);
65
+ if (!state)
66
+ return;
67
+ const { transformKeys } = state;
68
+ state.release().forEach((value, key) => {
69
+ /**
70
+ * The style effect derives transform (from the bound transform
71
+ * keys) and transformBox itself; a VisualElement builds its own.
72
+ */
73
+ const derived = key === "transformBox" ||
74
+ (key === "transform" && transformKeys?.length);
75
+ derived || visualElement.addValue(key, value);
76
+ });
77
+ }
78
+
79
+ export { animateElement, handOffElementState };
80
+ //# sourceMappingURL=element.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"element.mjs","sources":["../../../../src/animation/animate/element.ts"],"sourcesContent":["import { styleSubjectEffect, StyleSubject } from \"../../effects/style\"\nimport { svgSubjectEffect } from \"../../effects/svg\"\nimport { measureViewportBox } from \"../../projection/utils/measure\"\nimport type { VisualElement } from \"../../render/VisualElement\"\nimport { isSVGElement } from \"../../utils/is-svg-element\"\nimport { MotionValue, Owner } from \"../../value\"\nimport { DOMKeyframesResolver } from \"../keyframes/DOMKeyframesResolver\"\nimport { AnimationElement } from \"../keyframes/types\"\nimport { AnimationPlaybackControlsWithThen } from \"../types\"\nimport {\n animateEffectSubject,\n animateValues,\n EffectKeyframes,\n EffectTransition,\n} from \"./effects\"\n\nconst noProps = {}\n\ntype ElementEffect = typeof styleSubjectEffect\n\nconst getElementEffect = (element: Element) =>\n (isSVGElement(element)\n ? svgSubjectEffect\n : styleSubjectEffect) as ElementEffect\n\n/**\n * Presents an effect bound to a DOM element in the shape the DOM keyframe\n * resolver and WAAPI expect, which is that of a VisualElement. Holds no\n * state: everything delegates to the effect. Goes once the resolver takes\n * an effect and subject directly.\n */\nclass EffectSubject implements AnimationElement, Owner {\n KeyframeResolver = DOMKeyframesResolver\n\n constructor(private effect: ElementEffect, public current: StyleSubject) {}\n\n getValue(key: string) {\n return this.effect.get(this.current, key)\n }\n\n readValue(key: string, target?: unknown) {\n return this.effect.read(this.current, key, target as any)\n }\n\n render() {\n this.effect.flush(this.current)\n }\n\n measureViewportBox() {\n return measureViewportBox(this.current as HTMLElement)\n }\n\n getProps() {\n return noProps\n }\n}\n\n/**\n * Animate a DOM element's styles. HTML elements are bound through\n * styleEffect, SVG through svgEffect, and both are resolved with the DOM\n * keyframe resolver so reads are batched, units are converted by\n * measurement and eligible values run on WAAPI.\n *\n * Pass the element's VisualElement (a <motion.*> component or an\n * animateLayout() node) to animate its values instead, so the two keep\n * sharing values and a renderer.\n */\nexport function animateElement(\n element: StyleSubject,\n keyframes: EffectKeyframes,\n transition?: EffectTransition,\n visualElement?: VisualElement\n): AnimationPlaybackControlsWithThen[] {\n if (visualElement) {\n return animateValues(\n (key) => visualElement.getValue(key, null),\n keyframes,\n transition,\n visualElement\n )\n }\n\n const effect = getElementEffect(element)\n\n return animateEffectSubject(\n effect,\n element,\n keyframes,\n transition,\n new EffectSubject(effect, element)\n )\n}\n\n/**\n * Hand the values styleEffect/svgEffect are rendering on `element`,\n * whether bound by animate() or directly, to a VisualElement that now\n * owns the element (e.g. created by animateLayout()), so a single\n * renderer drives them alongside its own values.\n */\nexport function handOffElementState(\n element: Element,\n visualElement: VisualElement\n) {\n const state = getElementEffect(element).state(element as StyleSubject)\n if (!state) return\n\n const { transformKeys } = state\n state.release().forEach((value: MotionValue, key: string) => {\n /**\n * The style effect derives transform (from the bound transform\n * keys) and transformBox itself; a VisualElement builds its own.\n */\n const derived =\n key === \"transformBox\" ||\n (key === \"transform\" && transformKeys?.length)\n\n derived || visualElement.addValue(key, value)\n })\n}\n"],"names":[],"mappings":";;;;;;;AAgBA,MAAM,OAAO,GAAG,EAAE;AAIlB,MAAM,gBAAgB,GAAG,CAAC,OAAgB,MACrC,YAAY,CAAC,OAAO;AACjB,MAAE;MACA,kBAAkB,CAAkB;AAE9C;;;;;AAKG;AACH,MAAM,aAAa,CAAA;IAGf,WAAA,CAAoB,MAAqB,EAAS,OAAqB,EAAA;QAAnD,IAAA,CAAA,MAAM,GAAN,MAAM;QAAwB,IAAA,CAAA,OAAO,GAAP,OAAO;QAFzD,IAAA,CAAA,gBAAgB,GAAG,oBAAoB;IAEmC;AAE1E,IAAA,QAAQ,CAAC,GAAW,EAAA;AAChB,QAAA,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,CAAC;IAC7C;IAEA,SAAS,CAAC,GAAW,EAAE,MAAgB,EAAA;AACnC,QAAA,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAA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AAC,CAAC;AACN;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"single-value.mjs","sources":["../../../../src/animation/animate/single-value.ts"],"sourcesContent":["import { animateMotionValue } from \"../interfaces/motion-value\"\nimport type {\n AnimationPlaybackControlsWithThen,\n AnyResolvedKeyframe,\n UnresolvedValueKeyframe,\n ValueAnimationTransition,\n} from \"../types\"\nimport {\n motionValue as createMotionValue,\n MotionValue,\n} from \"../../value\"\nimport { isMotionValue } from \"../../value/utils/is-motion-value\"\n\nexport function animateSingleValue<V extends AnyResolvedKeyframe>(\n value: MotionValue<V> | V,\n keyframes: V | UnresolvedValueKeyframe<V>[],\n options?: ValueAnimationTransition\n): AnimationPlaybackControlsWithThen {\n const motionValue = isMotionValue(value) ? value : createMotionValue(value)\n\n motionValue.start(animateMotionValue(\"\", motionValue, keyframes, options))\n\n return motionValue.animation!\n}\n"],"names":["motionValue","createMotionValue"],"mappings":";;;;SAagB,kBAAkB,CAC9B,KAAyB,EACzB,SAA2C,EAC3C,OAAkC,EAAA;AAElC,IAAA,MAAMA,aAAW,GAAG,aAAa,CAAC,KAAK,CAAC,GAAG,KAAK,GAAGC,WAAiB,CAAC,KAAK,CAAC;AAE3E,IAAAD,aAAW,CAAC,KAAK,CAAC,kBAAkB,CAAC,EAAE,EAAEA,aAAW,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;IAE1E,OAAOA,aAAW,CAAC,SAAU;AACjC;;;;"}
1
+ {"version":3,"file":"single-value.mjs","sources":["../../../../src/animation/animate/single-value.ts"],"sourcesContent":["import { animateMotionValue } from \"../interfaces/motion-value\"\nimport type {\n AnimationPlaybackControlsWithThen,\n AnyResolvedKeyframe,\n UnresolvedValueKeyframe,\n ValueAnimationTransition,\n} from \"../types\"\nimport {\n motionValue as createMotionValue,\n MotionValue,\n} from \"../../value\"\nimport { isMotionValue } from \"../../value/utils/is-motion-value\"\n\nexport function animateSingleValue<V extends AnyResolvedKeyframe>(\n value: MotionValue<V> | V,\n keyframes: V | UnresolvedValueKeyframe<V>[],\n options?: ValueAnimationTransition\n): AnimationPlaybackControlsWithThen {\n const motionValue = isMotionValue(value) ? value : createMotionValue(value)\n\n motionValue.start(animateMotionValue(\"\", motionValue, keyframes, options))\n\n return motionValue.animation as AnimationPlaybackControlsWithThen\n}\n"],"names":["motionValue","createMotionValue"],"mappings":";;;;SAagB,kBAAkB,CAC9B,KAAyB,EACzB,SAA2C,EAC3C,OAAkC,EAAA;AAElC,IAAA,MAAMA,aAAW,GAAG,aAAa,CAAC,KAAK,CAAC,GAAG,KAAK,GAAGC,WAAiB,CAAC,KAAK,CAAC;AAE3E,IAAAD,aAAW,CAAC,KAAK,CAAC,kBAAkB,CAAC,EAAE,EAAEA,aAAW,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;IAE1E,OAAOA,aAAW,CAAC,SAA8C;AACrE;;;;"}
@@ -1,5 +1,6 @@
1
- import { easeInOut, isEasingArray, easingDefinitionToFunction } from 'motion-utils';
1
+ import { easeInOut, isEasingArray, easingDefinitionToFunction, MotionGlobalConfig, clamp } from 'motion-utils';
2
2
  import { interpolate } from '../../utils/interpolate.mjs';
3
+ import { mix } from '../../utils/mix/index.mjs';
3
4
  import { defaultOffset } from '../keyframes/offsets/default.mjs';
4
5
  import { convertOffsetToTimes } from '../keyframes/offsets/time.mjs';
5
6
 
@@ -22,6 +23,29 @@ function keyframes({ duration = 300, keyframes: keyframeValues, times, ease = "e
22
23
  done: false,
23
24
  value: keyframeValues[0],
24
25
  };
26
+ /**
27
+ * Fast setup path for two keyframes one easing.
28
+ */
29
+ if (keyframeValues.length === 2 &&
30
+ !Array.isArray(easingFunctions) &&
31
+ (!times ||
32
+ times.length !== 2 ||
33
+ (times[0] === 0 && times[1] === 1))) {
34
+ const [origin, target] = keyframeValues;
35
+ const mixer = origin === target
36
+ ? undefined
37
+ : (MotionGlobalConfig.mix || mix)(origin, target);
38
+ return {
39
+ calculatedDuration: duration,
40
+ next: (t) => {
41
+ state.value = mixer
42
+ ? mixer(easingFunctions(duration > 0 ? clamp(0, 1, t / duration) : 1))
43
+ : target;
44
+ state.done = t >= duration;
45
+ return state;
46
+ },
47
+ };
48
+ }
25
49
  /**
26
50
  * Create a times array based on the provided 0-1 offsets
27
51
  */
@@ -1 +1 @@
1
- {"version":3,"file":"keyframes.mjs","sources":["../../../../src/animation/generators/keyframes.ts"],"sourcesContent":["import {\n easeInOut,\n easingDefinitionToFunction,\n EasingFunction,\n isEasingArray,\n} from \"motion-utils\"\nimport { interpolate } from \"../../utils/interpolate\"\nimport { defaultOffset } from \"../keyframes/offsets/default\"\nimport { convertOffsetToTimes } from \"../keyframes/offsets/time\"\nimport {\n AnimationState,\n AnyResolvedKeyframe,\n KeyframeGenerator,\n ValueAnimationOptions,\n} from \"../types\"\n\nexport function defaultEasing(\n values: any[],\n easing?: EasingFunction\n): EasingFunction[] {\n return values.map(() => easing || easeInOut).splice(0, values.length - 1)\n}\n\nexport function keyframes<T extends AnyResolvedKeyframe>({\n duration = 300,\n keyframes: keyframeValues,\n times,\n ease = \"easeInOut\",\n}: ValueAnimationOptions<T>): KeyframeGenerator<T> {\n /**\n * Easing functions can be externally defined as strings. Here we convert them\n * into actual functions.\n */\n const easingFunctions = isEasingArray(ease)\n ? ease.map(easingDefinitionToFunction)\n : easingDefinitionToFunction(ease)\n\n /**\n * This is the Iterator-spec return value. We ensure it's mutable rather than using a generator\n * to reduce GC during animation.\n */\n const state: AnimationState<T> = {\n done: false,\n value: keyframeValues[0],\n }\n\n /**\n * Create a times array based on the provided 0-1 offsets\n */\n const absoluteTimes = convertOffsetToTimes(\n // Only use the provided offsets if they're the correct length\n // TODO Maybe we should warn here if there's a length mismatch\n times && times.length === keyframeValues.length\n ? times\n : defaultOffset(keyframeValues),\n duration\n )\n\n const mapTimeToKeyframe = interpolate<T>(absoluteTimes, keyframeValues, {\n ease: Array.isArray(easingFunctions)\n ? easingFunctions\n : defaultEasing(keyframeValues, easingFunctions),\n })\n\n return {\n calculatedDuration: duration,\n next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AQ;AAC1B,YAAA,OAAO,KAAK;QAChB,CAAC;KACJ;AACL;;;;"}
1
+ {"version":3,"file":"keyframes.mjs","sources":["../../../../src/animation/generators/keyframes.ts"],"sourcesContent":["import {\n clamp,\n easeInOut,\n easingDefinitionToFunction,\n EasingFunction,\n isEasingArray,\n MotionGlobalConfig,\n} from \"motion-utils\"\nimport { interpolate } from \"../../utils/interpolate\"\nimport { mix } from \"../../utils/mix\"\nimport { defaultOffset } from \"../keyframes/offsets/default\"\nimport { convertOffsetToTimes } from \"../keyframes/offsets/time\"\nimport {\n AnimationState,\n AnyResolvedKeyframe,\n KeyframeGenerator,\n ValueAnimationOptions,\n} from \"../types\"\n\nexport function defaultEasing(\n values: any[],\n easing?: EasingFunction\n): EasingFunction[] {\n return values.map(() => easing || easeInOut).splice(0, values.length - 1)\n}\n\nexport function keyframes<T extends AnyResolvedKeyframe>({\n duration = 300,\n keyframes: keyframeValues,\n times,\n ease = \"easeInOut\",\n}: ValueAnimationOptions<T>): KeyframeGenerator<T> {\n /**\n * Easing functions can be externally defined as strings. Here we convert them\n * into actual functions.\n */\n const easingFunctions = isEasingArray(ease)\n ? ease.map(easingDefinitionToFunction)\n : easingDefinitionToFunction(ease)\n\n /**\n * This is the Iterator-spec return value. We ensure it's mutable rather than using a generator\n * to reduce GC during animation.\n */\n const state: AnimationState<T> = {\n done: false,\n value: keyframeValues[0],\n }\n\n /**\n * Fast setup path for two keyframes one easing.\n */\n if (\n keyframeValues.length === 2 &&\n !Array.isArray(easingFunctions) &&\n (!times ||\n times.length !== 2 ||\n (times[0] === 0 && times[1] === 1))\n ) {\n const [origin, target] = keyframeValues\n const mixer =\n origin === target\n ? undefined\n : (MotionGlobalConfig.mix || mix)(origin, target)\n\n return {\n calculatedDuration: duration,\n next: (t: number) => {\n state.value = mixer\n ? mixer(\n easingFunctions(\n duration > 0 ? clamp(0, 1, t / duration) : 1\n )\n )\n : target\n state.done = t >= duration\n return state\n },\n }\n }\n\n /**\n * Create a times array based on the provided 0-1 offsets\n */\n const absoluteTimes = convertOffsetToTimes(\n // Only use the provided offsets if they're the correct length\n // TODO Maybe we should warn here if there's a length mismatch\n times && times.length === keyframeValues.length\n ? times\n : defaultOffset(keyframeValues),\n duration\n )\n\n const mapTimeToKeyframe = interpolate<T>(absoluteTimes, keyframeValues, {\n ease: Array.isArray(easingFunctions)\n ? easingFunctions\n : defaultEasing(keyframeValues, easingFunctions),\n })\n\n return {\n calculatedDuration: duration,\n next: (t: number) => {\n state.value = mapTimeToKeyframe(t)\n state.done = t >= duration\n return state\n },\n }\n}\n"],"names":[],"mappings":";;;;;;AAmBM,SAAU,aAAa,CACzB,MAAa,EACb,MAAuB,EAAA;IAEvB,OAAO,MAAM,CAAC,GAAG,CAAC,MAAM,MAAM,IAAI,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;AAC7E;SAEgB,SAAS,CAAgC,EACrD,QAAQ,GAAG,GAAG,EACd,SAAS,EAAE,cAAc,EACzB,KAAK,EACL,IAAI,GAAG,WAAW,GACK,EAAA;AACvB;;;AAGG;AACH,IAAA,MAAM,eAAe,GAAG,aAAa,CAAC,IAAI;AACtC,UAAE,IAAI,CAAC,GAAG,CAAC,0BAA0B;AACrC,UAAE,0BAA0B,CAAC,IAAI,CAAC;AAEtC;;;AAGG;AACH,IAAA,MAAM,KAAK,GAAsB;AAC7B,QAAA,IAAI,EAAE,KAAK;AACX,QAAA,KAAK,EAAE,cAAc,CAAC,CAAC,CAAC;KAC3B;AAED;;AAEG;AACH,IAAA,IACI,cAAc,CAAC,MAAM,KAAK,CAAC;AAC3B,QAAA,CAAC,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC;AAC/B,SAAC,CAAC,KAAK;YACH,KAAK,CAAC,MAAM,KAAK,CAAC;AAClB,aAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC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Duration = springDefaults.visualDuration, bounce
170
170
  bounce,
171
171
  }
172
172
  : optionsOrVisualDuration;
173
- let { restSpeed, restDelta } = options;
174
173
  const origin = options.keyframes[0];
175
174
  const target = options.keyframes[options.keyframes.length - 1];
176
175
  /**
@@ -182,11 +181,23 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
182
181
  ...options,
183
182
  velocity: -millisecondsToSeconds(options.velocity || 0),
184
183
  });
185
- const initialVelocity = velocity || 0.0;
186
184
  const dampingRatio = damping / (2 * Math.sqrt(stiffness * mass));
187
- const initialDelta = target - origin;
188
185
  const undampedAngularFreq = millisecondsToSeconds(Math.sqrt(stiffness / mass));
189
186
  const decay = dampingRatio * undampedAngularFreq;
187
+ /**
188
+ * Everything that changes when the spring is retargeted: written by
189
+ * retarget() and update(), read by the resolvers. Grouped on one object,
190
+ * like the coefficients (c) below. Writing doubles to object fields
191
+ * measured marginally faster than to captured let variables in optimised
192
+ * code; neither allocates, so this is a grouping choice, not a GC one.
193
+ */
194
+ const s = {
195
+ target,
196
+ delta: target - origin,
197
+ velocity: velocity || 0.0,
198
+ restSpeed: 0,
199
+ restDelta: 0,
200
+ };
190
201
  /**
191
202
  * If we're working on a granular scale, use smaller defaults for determining
192
203
  * when the spring is finished.
@@ -194,59 +205,72 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
194
205
  * These defaults have been selected emprically based on what strikes a good
195
206
  * ratio between feeling good and finishing as soon as changes are imperceptible.
196
207
  */
197
- const isGranularScale = Math.abs(initialDelta) < 5;
198
- restSpeed || (restSpeed = isGranularScale
199
- ? springDefaults.restSpeed.granular
200
- : springDefaults.restSpeed.default);
201
- restDelta || (restDelta = isGranularScale
202
- ? springDefaults.restDelta.granular
203
- : springDefaults.restDelta.default);
208
+ const setRestThresholds = () => {
209
+ const isGranularScale = Math.abs(s.delta) < 5;
210
+ s.restSpeed =
211
+ options.restSpeed ||
212
+ (isGranularScale
213
+ ? springDefaults.restSpeed.granular
214
+ : springDefaults.restSpeed.default);
215
+ s.restDelta =
216
+ options.restDelta ||
217
+ (isGranularScale
218
+ ? springDefaults.restDelta.granular
219
+ : springDefaults.restDelta.default);
220
+ };
221
+ setRestThresholds();
204
222
  let resolveSpring;
205
223
  let resolveVelocity;
224
+ /**
225
+ * Derives the coefficients that depend on origin, target and initial
226
+ * velocity. Called once now and again whenever the spring is retargeted.
227
+ */
228
+ let update;
206
229
  if (dampingRatio < 1) {
207
230
  const angularFreq = calcAngularFreq(undampedAngularFreq, dampingRatio);
208
- const A = (initialVelocity + decay * initialDelta) / angularFreq;
209
- // Coefficients for the analytical derivative (px/ms)
210
- const sinCoeff = decay * A + initialDelta * angularFreq;
211
- const cosCoeff = decay * initialDelta - A * angularFreq;
212
231
  /**
213
- * The underdamped hot path needs both position and velocity every
214
- * frame and they share the same exp/sin/cos terms, so sample both
215
- * at once, memoized by t, to only calculate them once per frame.
232
+ * A is the position coefficient, sinC/cosC the coefficients of the
233
+ * analytical derivative (px/ms). The exp/sin/cos terms depend only
234
+ * on t, so they're memoized by t independently of the target: a
235
+ * spring retargeted every frame samples the same t each frame and
236
+ * skips the transcendentals entirely.
216
237
  */
217
- let sampledT = -1;
218
- let position = 0;
219
- let velocityAtT = 0;
238
+ const c = { A: 0, sinC: 0, cosC: 0, t: -1, env: 0, sin: 0, cos: 0 };
239
+ update = () => {
240
+ c.A = (s.velocity + decay * s.delta) / angularFreq;
241
+ c.sinC = decay * c.A + s.delta * angularFreq;
242
+ c.cosC = decay * s.delta - c.A * angularFreq;
243
+ };
220
244
  const sample = (t) => {
221
- if (t !== sampledT) {
222
- sampledT = t;
223
- const envelope = Math.exp(-decay * t);
224
- const sin = Math.sin(angularFreq * t);
225
- const cos = Math.cos(angularFreq * t);
226
- position = target - envelope * (A * sin + initialDelta * cos);
227
- velocityAtT = envelope * (sinCoeff * sin + cosCoeff * cos);
245
+ if (t !== c.t) {
246
+ c.t = t;
247
+ c.env = Math.exp(-decay * t);
248
+ c.sin = Math.sin(angularFreq * t);
249
+ c.cos = Math.cos(angularFreq * t);
228
250
  }
229
251
  };
230
252
  // Underdamped spring
231
253
  resolveSpring = (t) => {
232
254
  sample(t);
233
- return position;
255
+ return s.target - c.env * (c.A * c.sin + s.delta * c.cos);
234
256
  };
235
257
  resolveVelocity = (t) => {
236
258
  sample(t);
237
- return velocityAtT;
259
+ return c.env * (c.sinC * c.sin + c.cosC * c.cos);
238
260
  };
239
261
  }
240
262
  else if (dampingRatio === 1) {
241
263
  // Critically damped spring
242
- resolveSpring = (t) => target -
264
+ resolveSpring = (t) => s.target -
243
265
  Math.exp(-undampedAngularFreq * t) *
244
- (initialDelta +
245
- (initialVelocity + undampedAngularFreq * initialDelta) * t);
266
+ (s.delta + (s.velocity + undampedAngularFreq * s.delta) * t);
246
267
  // Analytical derivative of critically damped spring (px/ms)
247
- const C = initialVelocity + undampedAngularFreq * initialDelta;
268
+ const c = { C: 0 };
269
+ update = () => {
270
+ c.C = s.velocity + undampedAngularFreq * s.delta;
271
+ };
248
272
  resolveVelocity = (t) => Math.exp(-undampedAngularFreq * t) *
249
- (undampedAngularFreq * C * t - initialVelocity);
273
+ (undampedAngularFreq * c.C * t - s.velocity);
250
274
  }
251
275
  else {
252
276
  // Overdamped spring
@@ -255,48 +279,72 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
255
279
  const envelope = Math.exp(-decay * t);
256
280
  // When performing sinh or cosh values can hit Infinity so we cap them here
257
281
  const freqForT = Math.min(dampedAngularFreq * t, 300);
258
- return (target -
282
+ return (s.target -
259
283
  (envelope *
260
- ((initialVelocity + decay * initialDelta) *
261
- Math.sinh(freqForT) +
262
- dampedAngularFreq *
263
- initialDelta *
264
- Math.cosh(freqForT))) /
284
+ ((s.velocity + decay * s.delta) * Math.sinh(freqForT) +
285
+ dampedAngularFreq * s.delta * Math.cosh(freqForT))) /
265
286
  dampedAngularFreq);
266
287
  };
267
288
  // Analytical derivative of overdamped spring (px/ms)
268
- const P = (initialVelocity + decay * initialDelta) / dampedAngularFreq;
269
- const sinhCoeff = decay * P - initialDelta * dampedAngularFreq;
270
- const coshCoeff = decay * initialDelta - P * dampedAngularFreq;
289
+ const c = { P: 0, sinh: 0, cosh: 0 };
290
+ update = () => {
291
+ c.P = (s.velocity + decay * s.delta) / dampedAngularFreq;
292
+ c.sinh = decay * c.P - s.delta * dampedAngularFreq;
293
+ c.cosh = decay * s.delta - c.P * dampedAngularFreq;
294
+ };
271
295
  resolveVelocity = (t) => {
272
296
  const envelope = Math.exp(-decay * t);
273
297
  const freqForT = Math.min(dampedAngularFreq * t, 300);
274
298
  return (envelope *
275
- (sinhCoeff * Math.sinh(freqForT) +
276
- coshCoeff * Math.cosh(freqForT)));
299
+ (c.sinh * Math.sinh(freqForT) + c.cosh * Math.cosh(freqForT)));
277
300
  };
278
301
  }
302
+ update();
303
+ /**
304
+ * Time-defined springs ignore inherited velocity, see getSpringOptions.
305
+ */
306
+ const ignoreVelocity = !isSpringType(options, physicsKeys) &&
307
+ isSpringType(options, durationKeys);
308
+ const calculatedDuration = isResolvedFromDuration ? duration || null : null;
279
309
  const generator = {
280
- calculatedDuration: isResolvedFromDuration ? duration || null : null,
310
+ calculatedDuration,
311
+ /**
312
+ * Aim the spring at a new target from its current position and
313
+ * velocity, reusing the resolved physics and closures.
314
+ */
315
+ retarget: (keyframes, newVelocity) => {
316
+ s.target = keyframes[keyframes.length - 1];
317
+ s.delta = s.target - keyframes[0];
318
+ s.velocity = ignoreVelocity
319
+ ? 0
320
+ : -millisecondsToSeconds(newVelocity);
321
+ // Default thresholds depend on the scale of the new delta
322
+ if (!(options.restSpeed && options.restDelta))
323
+ setRestThresholds();
324
+ // Invalidate any duration lazily cached by JSAnimation
325
+ generator.calculatedDuration = calculatedDuration;
326
+ state.done = false;
327
+ update();
328
+ },
281
329
  velocity: (t) => secondsToMilliseconds(resolveVelocity(t)),
282
330
  next: (t) => {
283
331
  const current = resolveSpring(t);
284
332
  if (!isResolvedFromDuration) {
285
333
  const currentVelocity = secondsToMilliseconds(resolveVelocity(t));
286
334
  state.done =
287
- Math.abs(currentVelocity) <= restSpeed &&
288
- Math.abs(target - current) <= restDelta;
335
+ Math.abs(currentVelocity) <= s.restSpeed &&
336
+ Math.abs(s.target - current) <= s.restDelta;
289
337
  }
290
338
  else {
291
339
  state.done = t >= duration;
292
340
  }
293
- state.value = state.done ? target : current;
341
+ state.value = state.done ? s.target : current;
294
342
  return state;
295
343
  },
296
344
  toString: () => {
297
- const calculatedDuration = Math.min(calcGeneratorDuration(generator), maxGeneratorDuration);
298
- const easing = generateLinearEasing((progress) => generator.next(calculatedDuration * progress).value, calculatedDuration, 30);
299
- return calculatedDuration + "ms " + easing;
345
+ const easingDuration = Math.min(calcGeneratorDuration(generator), maxGeneratorDuration);
346
+ const easing = generateLinearEasing((progress) => generator.next(easingDuration * progress).value, easingDuration, 30);
347
+ return easingDuration + "ms " + easing;
300
348
  },
301
349
  toTransition: () => { },
302
350
  };