motion-dom 14.1.0 → 14.2.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.
- package/dist/cjs/index.js +1165 -187
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/animation/AsyncMotionValueAnimation.mjs +19 -1
- package/dist/es/animation/AsyncMotionValueAnimation.mjs.map +1 -1
- package/dist/es/animation/JSAnimation.mjs +54 -24
- package/dist/es/animation/JSAnimation.mjs.map +1 -1
- package/dist/es/animation/NativeAnimationExtended.mjs +47 -20
- package/dist/es/animation/NativeAnimationExtended.mjs.map +1 -1
- package/dist/es/animation/generators/utils/create-generator-easing.mjs +14 -1
- package/dist/es/animation/generators/utils/create-generator-easing.mjs.map +1 -1
- package/dist/es/animation/interfaces/motion-value.mjs +1 -0
- package/dist/es/animation/interfaces/motion-value.mjs.map +1 -1
- package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs +25 -1
- package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs.map +1 -1
- package/dist/es/animation/waapi/supports/waapi.mjs +1 -1
- package/dist/es/animation/waapi/supports/waapi.mjs.map +1 -1
- package/dist/es/animation/waapi/transforms/TransformAnimation.mjs +854 -0
- package/dist/es/animation/waapi/transforms/TransformAnimation.mjs.map +1 -0
- package/dist/es/animation/waapi/transforms/groups.mjs +4 -0
- package/dist/es/animation/waapi/transforms/groups.mjs.map +1 -0
- package/dist/es/effects/style/index.mjs +6 -3
- package/dist/es/effects/style/index.mjs.map +1 -1
- package/dist/es/effects/style/transform.mjs +2 -7
- package/dist/es/effects/style/transform.mjs.map +1 -1
- package/dist/es/index.mjs +3 -3
- package/dist/es/projection/node/create-projection-node.mjs +10 -0
- package/dist/es/projection/node/create-projection-node.mjs.map +1 -1
- package/dist/es/render/VisualElement.mjs +16 -3
- package/dist/es/render/VisualElement.mjs.map +1 -1
- package/dist/es/render/html/utils/build-transform.mjs +1 -1
- package/dist/es/render/html/utils/build-transform.mjs.map +1 -1
- package/dist/es/value/index.mjs +12 -0
- package/dist/es/value/index.mjs.map +1 -1
- package/dist/index.d.ts +77 -12
- package/dist/motion-dom.dev.js +1165 -187
- package/dist/motion-dom.js +1 -1
- package/dist/size-rollup-motion-value.js +1 -1
- package/dist/size-rollup-motion-value.js.map +1 -1
- package/dist/size-rollup-style-effect.js +1 -1
- package/dist/size-rollup-style-effect.js.map +1 -1
- package/package.json +3 -3
|
@@ -9,6 +9,7 @@ import { makeAnimationInstant } from './utils/make-animation-instant.mjs';
|
|
|
9
9
|
import { resolveStartTime } from './utils/resolve-start-time.mjs';
|
|
10
10
|
import { WithPromise } from './utils/WithPromise.mjs';
|
|
11
11
|
import { supportsBrowserAnimation } from './waapi/supports/waapi.mjs';
|
|
12
|
+
import { canGroupTransform, TransformAnimation } from './waapi/transforms/TransformAnimation.mjs';
|
|
12
13
|
|
|
13
14
|
class AsyncMotionValueAnimation extends WithPromise {
|
|
14
15
|
constructor(options) {
|
|
@@ -79,6 +80,11 @@ class AsyncMotionValueAnimation extends WithPromise {
|
|
|
79
80
|
* values may not be valid CSS and would trigger browser warnings.
|
|
80
81
|
*/
|
|
81
82
|
const useWaapi = canAnimateValue && !isHandoff && supportsBrowserAnimation(options);
|
|
83
|
+
/**
|
|
84
|
+
* Independent transforms (x, scale etc) on an HTML element are
|
|
85
|
+
* composed into one WAAPI transform animation per element.
|
|
86
|
+
*/
|
|
87
|
+
const isGroupedTransform = !useWaapi && canGroupTransform(options);
|
|
82
88
|
/**
|
|
83
89
|
* Resolve startTime for the animation. A startTime passed in options
|
|
84
90
|
* (an optimised appear handoff syncing to its WAAPI animation) takes
|
|
@@ -94,7 +100,10 @@ class AsyncMotionValueAnimation extends WithPromise {
|
|
|
94
100
|
* animation started in that moment stays in sync. WAAPI has no frame
|
|
95
101
|
* of ours to do that on.
|
|
96
102
|
*/
|
|
97
|
-
if (sync &&
|
|
103
|
+
if (sync &&
|
|
104
|
+
(useWaapi ||
|
|
105
|
+
isGroupedTransform ||
|
|
106
|
+
this.resolvedAt !== this.createdAt)) {
|
|
98
107
|
options.startTime ?? (options.startTime = resolveStartTime(this.createdAt, this.resolvedAt));
|
|
99
108
|
}
|
|
100
109
|
let animation;
|
|
@@ -112,6 +121,9 @@ class AsyncMotionValueAnimation extends WithPromise {
|
|
|
112
121
|
animation = new JSAnimation(options);
|
|
113
122
|
}
|
|
114
123
|
}
|
|
124
|
+
else if (isGroupedTransform) {
|
|
125
|
+
animation = new TransformAnimation(options);
|
|
126
|
+
}
|
|
115
127
|
else {
|
|
116
128
|
animation = new JSAnimation(options);
|
|
117
129
|
}
|
|
@@ -127,6 +139,12 @@ class AsyncMotionValueAnimation extends WithPromise {
|
|
|
127
139
|
then(onResolve, _onReject) {
|
|
128
140
|
return this.finished.finally(onResolve).then(() => { });
|
|
129
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* The value now, if the animation runs on the compositor.
|
|
144
|
+
*/
|
|
145
|
+
liveValue() {
|
|
146
|
+
return this._animation?.liveValue?.();
|
|
147
|
+
}
|
|
130
148
|
get animation() {
|
|
131
149
|
if (!this._animation) {
|
|
132
150
|
this.keyframeResolver?.resume();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AsyncMotionValueAnimation.mjs","sources":["../../../src/animation/AsyncMotionValueAnimation.ts"],"sourcesContent":["import { MotionGlobalConfig } from \"motion-utils\"\nimport { time } from \"../frameloop/sync-time\"\nimport { JSAnimation } from \"./JSAnimation\"\nimport { getFinalKeyframe } from \"./keyframes/get-final\"\nimport {\n KeyframeResolver as DefaultKeyframeResolver,\n flushKeyframeResolvers,\n ResolvedKeyframes,\n} from \"./keyframes/KeyframesResolver\"\nimport { NativeAnimationExtended } from \"./NativeAnimationExtended\"\nimport {\n AnimationPlaybackControls,\n AnyResolvedKeyframe,\n TimelineWithFallback,\n ValueAnimationOptions,\n} from \"./types\"\nimport { canAnimate } from \"./utils/can-animate\"\nimport { makeAnimationInstant } from \"./utils/make-animation-instant\"\nimport { resolveStartTime } from \"./utils/resolve-start-time\"\nimport { WithPromise } from \"./utils/WithPromise\"\nimport { supportsBrowserAnimation } from \"./waapi/supports/waapi\"\n\ntype ResolvedOptions<T extends AnyResolvedKeyframe> =\n ValueAnimationOptions<T> & {\n startTime?: number\n finalKeyframe?: T\n }\n\nexport class AsyncMotionValueAnimation<T extends AnyResolvedKeyframe>\n extends WithPromise\n implements AnimationPlaybackControls\n{\n private createdAt: number\n\n private resolvedAt: number | undefined\n\n private _animation: AnimationPlaybackControls | undefined\n\n private pendingTimeline: TimelineWithFallback | undefined\n\n private keyframeResolver: DefaultKeyframeResolver | undefined\n\n private stopTimeline: VoidFunction | undefined\n\n constructor(options: ValueAnimationOptions<T>) {\n super()\n\n this.createdAt = time.now()\n\n const { keyframes, name, motionValue, element } = options\n\n /**\n * animateMotionValue builds a fresh options object per value, so\n * it's completed in place as keyframes resolve rather than copied.\n */\n const optionsWithDefaults = options as ResolvedOptions<T>\n optionsWithDefaults.autoplay ??= true\n optionsWithDefaults.delay ??= 0\n optionsWithDefaults.type ??= \"keyframes\"\n optionsWithDefaults.repeat ??= 0\n optionsWithDefaults.repeatDelay ??= 0\n optionsWithDefaults.repeatType ??= \"loop\"\n\n const KeyframeResolver =\n element?.KeyframeResolver || DefaultKeyframeResolver\n\n this.keyframeResolver = new KeyframeResolver(\n keyframes,\n (\n resolvedKeyframes: ResolvedKeyframes<T>,\n finalKeyframe: T,\n forced: boolean\n ) =>\n this.onKeyframesResolved(\n resolvedKeyframes,\n finalKeyframe,\n optionsWithDefaults,\n !forced\n ),\n name,\n motionValue,\n element\n )\n this.keyframeResolver?.scheduleResolve()\n }\n\n onKeyframesResolved(\n keyframes: ResolvedKeyframes<T>,\n finalKeyframe: T,\n options: ResolvedOptions<T>,\n sync: boolean\n ) {\n this.keyframeResolver = undefined\n\n const { name, type, velocity, delay, isHandoff, onUpdate } = options\n this.resolvedAt = time.now()\n\n /**\n * If we can't animate this value with the resolved keyframes\n * then we should complete it immediately.\n */\n let canAnimateValue = true\n if (!canAnimate(keyframes, name, type, velocity)) {\n canAnimateValue = false\n\n if (MotionGlobalConfig.instantAnimations || !delay) {\n onUpdate?.(getFinalKeyframe(keyframes, options, finalKeyframe))\n }\n\n keyframes[0] = keyframes[keyframes.length - 1]\n\n makeAnimationInstant(options)\n options.repeat = 0\n }\n\n const { onComplete } = options\n options.finalKeyframe = finalKeyframe\n options.keyframes = keyframes\n /**\n * JSAnimation and NativeAnimation call onComplete exactly when\n * their own `finished` resolves, so this replaces a promise chain\n * per value with a callback.\n */\n options.onComplete = () => {\n onComplete?.()\n this.notifyFinished()\n }\n\n /**\n * Animate via WAAPI if possible. If this is a handoff animation, the optimised animation will be running via\n * WAAPI. Therefore, this animation must be JS to ensure it runs \"under\" the\n * optimised animation.\n *\n * Also skip WAAPI when keyframes aren't animatable, as the resolved\n * values may not be valid CSS and would trigger browser warnings.\n */\n const useWaapi =\n canAnimateValue && !isHandoff && supportsBrowserAnimation(options)\n\n /**\n * Resolve startTime for the animation. A startTime passed in options\n * (an optimised appear handoff syncing to its WAAPI animation) takes\n * precedence.\n *\n * *Ideally*, we would use the createdAt time as t=0 as the following\n * frame would then be the first frame of the animation in progress,\n * which would feel snappier. If keyframes resolved on a later frame,\n * and long after creation, we start from then instead.\n *\n * If they resolved immediately, a JSAnimation picks its own start\n * time and resolves it the same way on its first frame, so every\n * animation started in that moment stays in sync. WAAPI has no frame\n * of ours to do that on.\n */\n if (sync && (useWaapi || this.resolvedAt !== this.createdAt)) {\n options.startTime ??= resolveStartTime(\n this.createdAt,\n this.resolvedAt!\n )\n }\n\n let animation: AnimationPlaybackControls\n if (useWaapi) {\n /**\n * The resolver needed the VisualElement, WAAPI needs the DOM\n * element. JSAnimation reads neither, so this is safe to\n * leave in place if we fall back to it.\n */\n options.element = options.motionValue?.owner?.current\n try {\n animation = new NativeAnimationExtended(options as any)\n } catch {\n animation = new JSAnimation(options)\n }\n } else {\n animation = new JSAnimation(options)\n }\n\n if (this.pendingTimeline) {\n this.stopTimeline = animation.attachTimeline(this.pendingTimeline)\n this.pendingTimeline = undefined\n }\n\n this._animation = animation\n }\n\n get finished() {\n return this._animation ? this._animation.finished : super.finished\n }\n\n then(onResolve: VoidFunction, _onReject?: VoidFunction) {\n return this.finished.finally(onResolve).then(() => {})\n }\n\n get animation(): AnimationPlaybackControls {\n if (!this._animation) {\n this.keyframeResolver?.resume()\n flushKeyframeResolvers()\n }\n\n return this._animation!\n }\n\n get duration() {\n return this.animation.duration\n }\n\n get iterationDuration() {\n return this.animation.iterationDuration\n }\n\n get time() {\n return this.animation.time\n }\n\n set time(newTime: number) {\n this.animation.time = newTime\n }\n\n get speed() {\n return this.animation.speed\n }\n\n get state() {\n return this.animation.state\n }\n\n set speed(newSpeed: number) {\n this.animation.speed = newSpeed\n }\n\n get startTime() {\n return this.animation.startTime\n }\n\n attachTimeline(timeline: TimelineWithFallback) {\n if (this._animation) {\n this.stopTimeline = this.animation.attachTimeline(timeline)\n } else {\n this.pendingTimeline = timeline\n }\n\n return () => this.stop()\n }\n\n play() {\n this.animation.play()\n }\n\n pause() {\n this.animation.pause()\n }\n\n complete() {\n this.animation.complete()\n }\n\n cancel() {\n if (this._animation) {\n this.animation.cancel()\n }\n\n this.keyframeResolver?.cancel()\n }\n\n /**\n * Bound to support return animation.stop pattern\n */\n stop = () => {\n if (this._animation) {\n this._animation.stop()\n this.stopTimeline?.()\n }\n\n this.keyframeResolver?.cancel()\n }\n}\n"],"names":["KeyframeResolver","DefaultKeyframeResolver"],"mappings":";;;;;;;;;;;;AA4BM,MAAO,yBACT,SAAQ,WAAW,CAAA;AAenB,IAAA,WAAA,CAAY,OAAiC,EAAA;AACzC,QAAA,KAAK,EAAE;AA4NX;;AAEG;QACH,IAAA,CAAA,IAAI,GAAG,MAAK;AACR,YAAA,IAAI,IAAI,CAAC,UAAU,EAAE;AACjB,gBAAA,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE;AACtB,gBAAA,IAAI,CAAC,YAAY,IAAI;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
1
|
+
{"version":3,"file":"AsyncMotionValueAnimation.mjs","sources":["../../../src/animation/AsyncMotionValueAnimation.ts"],"sourcesContent":["import { MotionGlobalConfig } from \"motion-utils\"\nimport { time } from \"../frameloop/sync-time\"\nimport { JSAnimation } from \"./JSAnimation\"\nimport { getFinalKeyframe } from \"./keyframes/get-final\"\nimport {\n KeyframeResolver as DefaultKeyframeResolver,\n flushKeyframeResolvers,\n ResolvedKeyframes,\n} from \"./keyframes/KeyframesResolver\"\nimport { NativeAnimationExtended } from \"./NativeAnimationExtended\"\nimport {\n AnimationPlaybackControls,\n AnyResolvedKeyframe,\n TimelineWithFallback,\n ValueAnimationOptions,\n} from \"./types\"\nimport { canAnimate } from \"./utils/can-animate\"\nimport { makeAnimationInstant } from \"./utils/make-animation-instant\"\nimport { resolveStartTime } from \"./utils/resolve-start-time\"\nimport { WithPromise } from \"./utils/WithPromise\"\nimport { supportsBrowserAnimation } from \"./waapi/supports/waapi\"\nimport {\n canGroupTransform,\n TransformAnimation,\n} from \"./waapi/transforms/TransformAnimation\"\n\ntype ResolvedOptions<T extends AnyResolvedKeyframe> =\n ValueAnimationOptions<T> & {\n startTime?: number\n finalKeyframe?: T\n }\n\nexport class AsyncMotionValueAnimation<T extends AnyResolvedKeyframe>\n extends WithPromise\n implements AnimationPlaybackControls\n{\n private createdAt: number\n\n private resolvedAt: number | undefined\n\n private _animation: AnimationPlaybackControls | undefined\n\n private pendingTimeline: TimelineWithFallback | undefined\n\n private keyframeResolver: DefaultKeyframeResolver | undefined\n\n private stopTimeline: VoidFunction | undefined\n\n constructor(options: ValueAnimationOptions<T>) {\n super()\n\n this.createdAt = time.now()\n\n const { keyframes, name, motionValue, element } = options\n\n /**\n * animateMotionValue builds a fresh options object per value, so\n * it's completed in place as keyframes resolve rather than copied.\n */\n const optionsWithDefaults = options as ResolvedOptions<T>\n optionsWithDefaults.autoplay ??= true\n optionsWithDefaults.delay ??= 0\n optionsWithDefaults.type ??= \"keyframes\"\n optionsWithDefaults.repeat ??= 0\n optionsWithDefaults.repeatDelay ??= 0\n optionsWithDefaults.repeatType ??= \"loop\"\n\n const KeyframeResolver =\n element?.KeyframeResolver || DefaultKeyframeResolver\n\n this.keyframeResolver = new KeyframeResolver(\n keyframes,\n (\n resolvedKeyframes: ResolvedKeyframes<T>,\n finalKeyframe: T,\n forced: boolean\n ) =>\n this.onKeyframesResolved(\n resolvedKeyframes,\n finalKeyframe,\n optionsWithDefaults,\n !forced\n ),\n name,\n motionValue,\n element\n )\n this.keyframeResolver?.scheduleResolve()\n }\n\n onKeyframesResolved(\n keyframes: ResolvedKeyframes<T>,\n finalKeyframe: T,\n options: ResolvedOptions<T>,\n sync: boolean\n ) {\n this.keyframeResolver = undefined\n\n const { name, type, velocity, delay, isHandoff, onUpdate } = options\n this.resolvedAt = time.now()\n\n /**\n * If we can't animate this value with the resolved keyframes\n * then we should complete it immediately.\n */\n let canAnimateValue = true\n if (!canAnimate(keyframes, name, type, velocity)) {\n canAnimateValue = false\n\n if (MotionGlobalConfig.instantAnimations || !delay) {\n onUpdate?.(getFinalKeyframe(keyframes, options, finalKeyframe))\n }\n\n keyframes[0] = keyframes[keyframes.length - 1]\n\n makeAnimationInstant(options)\n options.repeat = 0\n }\n\n const { onComplete } = options\n options.finalKeyframe = finalKeyframe\n options.keyframes = keyframes\n /**\n * JSAnimation and NativeAnimation call onComplete exactly when\n * their own `finished` resolves, so this replaces a promise chain\n * per value with a callback.\n */\n options.onComplete = () => {\n onComplete?.()\n this.notifyFinished()\n }\n\n /**\n * Animate via WAAPI if possible. If this is a handoff animation, the optimised animation will be running via\n * WAAPI. Therefore, this animation must be JS to ensure it runs \"under\" the\n * optimised animation.\n *\n * Also skip WAAPI when keyframes aren't animatable, as the resolved\n * values may not be valid CSS and would trigger browser warnings.\n */\n const useWaapi =\n canAnimateValue && !isHandoff && supportsBrowserAnimation(options)\n\n /**\n * Independent transforms (x, scale etc) on an HTML element are\n * composed into one WAAPI transform animation per element.\n */\n const isGroupedTransform = !useWaapi && canGroupTransform(options)\n\n /**\n * Resolve startTime for the animation. A startTime passed in options\n * (an optimised appear handoff syncing to its WAAPI animation) takes\n * precedence.\n *\n * *Ideally*, we would use the createdAt time as t=0 as the following\n * frame would then be the first frame of the animation in progress,\n * which would feel snappier. If keyframes resolved on a later frame,\n * and long after creation, we start from then instead.\n *\n * If they resolved immediately, a JSAnimation picks its own start\n * time and resolves it the same way on its first frame, so every\n * animation started in that moment stays in sync. WAAPI has no frame\n * of ours to do that on.\n */\n if (\n sync &&\n (useWaapi ||\n isGroupedTransform ||\n this.resolvedAt !== this.createdAt)\n ) {\n options.startTime ??= resolveStartTime(\n this.createdAt,\n this.resolvedAt!\n )\n }\n\n let animation: AnimationPlaybackControls\n if (useWaapi) {\n /**\n * The resolver needed the VisualElement, WAAPI needs the DOM\n * element. JSAnimation reads neither, so this is safe to\n * leave in place if we fall back to it.\n */\n options.element = options.motionValue?.owner?.current\n try {\n animation = new NativeAnimationExtended(options as any)\n } catch {\n animation = new JSAnimation(options)\n }\n } else if (isGroupedTransform) {\n animation = new TransformAnimation(options)\n } else {\n animation = new JSAnimation(options)\n }\n\n if (this.pendingTimeline) {\n this.stopTimeline = animation.attachTimeline(this.pendingTimeline)\n this.pendingTimeline = undefined\n }\n\n this._animation = animation\n }\n\n get finished() {\n return this._animation ? this._animation.finished : super.finished\n }\n\n then(onResolve: VoidFunction, _onReject?: VoidFunction) {\n return this.finished.finally(onResolve).then(() => {})\n }\n\n /**\n * The value now, if the animation runs on the compositor.\n */\n liveValue() {\n return (\n this._animation as { liveValue?: () => T | undefined } | undefined\n )?.liveValue?.()\n }\n\n get animation(): AnimationPlaybackControls {\n if (!this._animation) {\n this.keyframeResolver?.resume()\n flushKeyframeResolvers()\n }\n\n return this._animation!\n }\n\n get duration() {\n return this.animation.duration\n }\n\n get iterationDuration() {\n return this.animation.iterationDuration\n }\n\n get time() {\n return this.animation.time\n }\n\n set time(newTime: number) {\n this.animation.time = newTime\n }\n\n get speed() {\n return this.animation.speed\n }\n\n get state() {\n return this.animation.state\n }\n\n set speed(newSpeed: number) {\n this.animation.speed = newSpeed\n }\n\n get startTime() {\n return this.animation.startTime\n }\n\n attachTimeline(timeline: TimelineWithFallback) {\n if (this._animation) {\n this.stopTimeline = this.animation.attachTimeline(timeline)\n } else {\n this.pendingTimeline = timeline\n }\n\n return () => this.stop()\n }\n\n play() {\n this.animation.play()\n }\n\n pause() {\n this.animation.pause()\n }\n\n complete() {\n this.animation.complete()\n }\n\n cancel() {\n if (this._animation) {\n this.animation.cancel()\n }\n\n this.keyframeResolver?.cancel()\n }\n\n /**\n * Bound to support return animation.stop pattern\n */\n stop = () => {\n if (this._animation) {\n this._animation.stop()\n this.stopTimeline?.()\n }\n\n this.keyframeResolver?.cancel()\n }\n}\n"],"names":["KeyframeResolver","DefaultKeyframeResolver"],"mappings":";;;;;;;;;;;;;AAgCM,MAAO,yBACT,SAAQ,WAAW,CAAA;AAenB,IAAA,WAAA,CAAY,OAAiC,EAAA;AACzC,QAAA,KAAK,EAAE;AAkPX;;AAEG;QACH,IAAA,CAAA,IAAI,GAAG,MAAK;AACR,YAAA,IAAI,IAAI,CAAC,UAAU,EAAE;AACjB,gBAAA,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE;AACtB,gBAAA,IAAI,CAAC,YAAY,IAAI;YACzB;AAEA,YAAA,IAAI,CAAC,gBAAgB,EAAE,MAAM,EAAE;AACnC,QAAA,CAAC;AA1PG,QAAA,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE;QAE3B,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,OAAO;AAEzD;;;AAGG;QACH,MAAM,mBAAmB,GAAG,OAA6B;QACzD,mBAAmB,CAAC,QAAQ,KAA5B,mBAAmB,CAAC,QAAQ,GAAK,IAAI,CAAA;QACrC,mBAAmB,CAAC,KAAK,KAAzB,mBAAmB,CAAC,KAAK,GAAK,CAAC,CAAA;Q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AAe,EAAA;AACpB,QAAA,IAAI,CAAC,SAAS,CAAC,IAAI,GAAG,OAAO;IACjC;AAEA,IAAA,IAAI,KAAK,GAAA;AACL,QAAA,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK;IAC/B;AAEA,IAAA,IAAI,KAAK,GAAA;AACL,QAAA,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK;IAC/B;IAEA,IAAI,KAAK,CAAC,QAAgB,EAAA;AACtB,QAAA,IAAI,CAAC,SAAS,CAAC,KAAK,GAAG,QAAQ;IACnC;AAEA,IAAA,IAAI,SAAS,GAAA;AACT,QAAA,OAAO,IAAI,CAAC,SAAS,CAAC,SAAS;IACnC;AAEA,IAAA,cAAc,CAAC,QAA8B,EAAA;AACzC,QAAA,IAAI,IAAI,CAAC,UAAU,EAAE;YACjB,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,QAAQ,CAAC;QAC/D;aAAO;AACH,YAAA,IAAI,CAAC,eAAe,GAAG,QAAQ;QACnC;AAEA,QAAA,OAAO,MAAM,IAAI,CAAC,IAAI,EAAE;IAC5B;IAEA,IAAI,GAAA;AACA,QAAA,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE;IACzB;IAEA,KAAK,GAAA;AACD,QAAA,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE;IAC1B;IAEA,QAAQ,GAAA;AACJ,QAAA,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE;IAC7B;IAEA,MAAM,GAAA;AACF,QAAA,IAAI,IAAI,CAAC,UAAU,EAAE;AACjB,YAAA,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE;QAC3B;AAEA,QAAA,IAAI,CAAC,gBAAgB,EAAE,MAAM,EAAE;IACnC;AAaH;;;;"}
|
|
@@ -13,6 +13,7 @@ import { notifyAnimationStart } from './utils/notify-inspector.mjs';
|
|
|
13
13
|
import { WithPromise } from './utils/WithPromise.mjs';
|
|
14
14
|
|
|
15
15
|
const percentToProgress = (percent) => percent / 100;
|
|
16
|
+
const round = (value) => Math.round(value * 10000) / 10000;
|
|
16
17
|
class JSAnimation extends WithPromise {
|
|
17
18
|
constructor(options) {
|
|
18
19
|
super();
|
|
@@ -121,10 +122,10 @@ class JSAnimation extends WithPromise {
|
|
|
121
122
|
}
|
|
122
123
|
}
|
|
123
124
|
tick(timestamp, sample = false) {
|
|
124
|
-
const { generator, totalDuration,
|
|
125
|
+
const { generator, totalDuration, calculatedDuration } = this;
|
|
125
126
|
if (this.startTime === null)
|
|
126
127
|
return generator.next(0);
|
|
127
|
-
const { delay = 0, keyframes,
|
|
128
|
+
const { delay = 0, keyframes, type, onUpdate, finalKeyframe, } = this.options;
|
|
128
129
|
if (this.startTime === this.pendingStartTime) {
|
|
129
130
|
this.startTime = resolveStartTime(this.startTime, timestamp);
|
|
130
131
|
}
|
|
@@ -157,7 +158,55 @@ class JSAnimation extends WithPromise {
|
|
|
157
158
|
if (this.state === "finished" && this.holdTime === null) {
|
|
158
159
|
this.currentTime = totalDuration;
|
|
159
160
|
}
|
|
160
|
-
|
|
161
|
+
const state = this.generate(this.currentTime, isInDelayPhase);
|
|
162
|
+
let { done } = state;
|
|
163
|
+
if (!isInDelayPhase && calculatedDuration !== null) {
|
|
164
|
+
done =
|
|
165
|
+
this.playbackSpeed >= 0
|
|
166
|
+
? this.currentTime >= totalDuration
|
|
167
|
+
: this.currentTime <= 0;
|
|
168
|
+
}
|
|
169
|
+
const isAnimationFinished = this.holdTime === null &&
|
|
170
|
+
(this.state === "finished" || (this.state === "running" && done));
|
|
171
|
+
// TODO: The exception for inertia could be cleaner here
|
|
172
|
+
if (isAnimationFinished && type !== inertia) {
|
|
173
|
+
state.value = getFinalKeyframe(keyframes, this.options, finalKeyframe, this.speed);
|
|
174
|
+
}
|
|
175
|
+
if (onUpdate) {
|
|
176
|
+
onUpdate(state.value);
|
|
177
|
+
}
|
|
178
|
+
if (isAnimationFinished) {
|
|
179
|
+
this.finish();
|
|
180
|
+
}
|
|
181
|
+
return state;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* This animation's time, including delay, at a timestamp.
|
|
185
|
+
*/
|
|
186
|
+
at(timestamp) {
|
|
187
|
+
return (this.holdTime ??
|
|
188
|
+
round((timestamp - this.startTime) * this.playbackSpeed));
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* The value at a timestamp, without changing this animation's state.
|
|
192
|
+
* If resolved, it ends at the last resolved keyframe, which is in the
|
|
193
|
+
* same unit as the others, rather than at finalKeyframe.
|
|
194
|
+
*/
|
|
195
|
+
sampleAt(timestamp, resolved) {
|
|
196
|
+
const { options, totalDuration } = this;
|
|
197
|
+
const t = this.at(timestamp) - (options.delay || 0);
|
|
198
|
+
return t >= totalDuration && options.type !== inertia
|
|
199
|
+
? getFinalKeyframe(options.keyframes, options, resolved ? undefined : options.finalKeyframe)
|
|
200
|
+
: this.generate(Math.max(t, 0), t < 0).value;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* The generator's state `currentTime` ms after the delay, without
|
|
204
|
+
* changing the animation's own state.
|
|
205
|
+
*/
|
|
206
|
+
generate(currentTime, isInDelayPhase) {
|
|
207
|
+
const { generator, totalDuration, mixKeyframes, mirroredGenerator, resolvedDuration, } = this;
|
|
208
|
+
const { keyframes, repeat, repeatType, repeatDelay } = this.options;
|
|
209
|
+
let elapsed = currentTime;
|
|
161
210
|
let frameGenerator = generator;
|
|
162
211
|
if (repeat) {
|
|
163
212
|
/**
|
|
@@ -165,7 +214,7 @@ class JSAnimation extends WithPromise {
|
|
|
165
214
|
* than duration we'll get values like 2.5 (midway through the
|
|
166
215
|
* third iteration)
|
|
167
216
|
*/
|
|
168
|
-
const progress = Math.min(
|
|
217
|
+
const progress = Math.min(currentTime, totalDuration) / resolvedDuration;
|
|
169
218
|
/**
|
|
170
219
|
* Get the current iteration (0 indexed). For instance the floor of
|
|
171
220
|
* 2.5 is 2.
|
|
@@ -218,25 +267,6 @@ class JSAnimation extends WithPromise {
|
|
|
218
267
|
if (mixKeyframes && !isInDelayPhase) {
|
|
219
268
|
state.value = mixKeyframes(state.value);
|
|
220
269
|
}
|
|
221
|
-
let { done } = state;
|
|
222
|
-
if (!isInDelayPhase && calculatedDuration !== null) {
|
|
223
|
-
done =
|
|
224
|
-
this.playbackSpeed >= 0
|
|
225
|
-
? this.currentTime >= totalDuration
|
|
226
|
-
: this.currentTime <= 0;
|
|
227
|
-
}
|
|
228
|
-
const isAnimationFinished = this.holdTime === null &&
|
|
229
|
-
(this.state === "finished" || (this.state === "running" && done));
|
|
230
|
-
// TODO: The exception for inertia could be cleaner here
|
|
231
|
-
if (isAnimationFinished && type !== inertia) {
|
|
232
|
-
state.value = getFinalKeyframe(keyframes, this.options, finalKeyframe, this.speed);
|
|
233
|
-
}
|
|
234
|
-
if (onUpdate) {
|
|
235
|
-
onUpdate(state.value);
|
|
236
|
-
}
|
|
237
|
-
if (isAnimationFinished) {
|
|
238
|
-
this.finish();
|
|
239
|
-
}
|
|
240
270
|
return state;
|
|
241
271
|
}
|
|
242
272
|
/**
|
|
@@ -384,5 +414,5 @@ function animateValue(options) {
|
|
|
384
414
|
return new JSAnimation(options);
|
|
385
415
|
}
|
|
386
416
|
|
|
387
|
-
export { JSAnimation, animateValue };
|
|
417
|
+
export { JSAnimation, animateValue, round };
|
|
388
418
|
//# sourceMappingURL=JSAnimation.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JSAnimation.mjs","sources":["../../../src/animation/JSAnimation.ts"],"sourcesContent":["import {\n clamp,\n invariant,\n millisecondsToSeconds,\n pipe,\n secondsToMilliseconds,\n} from \"motion-utils\"\nimport { time } from \"../frameloop/sync-time\"\nimport { mix } from \"../utils/mix\"\nimport { Mixer } from \"../utils/mix/types\"\nimport { frameloopDriver } from \"./drivers/frame\"\nimport { DriverControls } from \"./drivers/types\"\nimport { inertia } from \"./generators/inertia\"\nimport { keyframes as keyframesGenerator } from \"./generators/keyframes\"\nimport { calcGeneratorDuration } from \"./generators/utils/calc-duration\"\nimport { calcGeneratorVelocity } from \"./generators/utils/velocity\"\nimport { getFinalKeyframe } from \"./keyframes/get-final\"\nimport {\n AnimationPlaybackControlsWithThen,\n AnimationState,\n GeneratorFactory,\n KeyframeGenerator,\n TimelineWithFallback,\n ValueAnimationOptions,\n} from \"./types\"\nimport { replaceTransitionType } from \"./utils/replace-transition-type\"\nimport { resolveStartTime } from \"./utils/resolve-start-time\"\nimport { notifyAnimationStart } from \"./utils/notify-inspector\"\nimport { WithPromise } from \"./utils/WithPromise\"\n\nconst percentToProgress = (percent: number) => percent / 100\n\nexport class JSAnimation<T extends number | string>\n extends WithPromise\n implements AnimationPlaybackControlsWithThen\n{\n state: AnimationPlayState = \"idle\"\n\n startTime: number | null = null\n\n /**\n * The driver that's controlling the animation loop. Normally this is a requestAnimationFrame loop\n * but in tests we can pass in a synchronous loop.\n */\n private driver?: DriverControls\n\n private isStopped = false\n\n /**\n * The start time this animation picked for itself, until its first tick.\n */\n private pendingStartTime?: number\n\n private generator: KeyframeGenerator<T>\n\n private calculatedDuration: number\n\n private resolvedDuration: number\n\n private totalDuration: number\n\n private options: ValueAnimationOptions<T>\n\n /**\n * The current time of the animation.\n */\n private currentTime: number = 0\n\n /**\n * The time at which the animation was paused.\n */\n private holdTime: number | null = null\n\n /**\n * Playback speed as a factor. 0 would be stopped, -1 reverse and 2 double speed.\n */\n private playbackSpeed = 1\n\n /*\n * If our generator doesn't support mixing numbers, we need to replace keyframes with\n * [0, 100] and then make a function that maps that to the actual keyframes.\n *\n * 100 is chosen instead of 1 as it works nicer with spring animations.\n */\n private mixKeyframes: Mixer<T> | undefined\n\n private mirroredGenerator: KeyframeGenerator<T> | undefined\n\n /**\n * Reusable state object for the delay phase to avoid\n * allocating a new object every frame.\n */\n private delayState: AnimationState<T> = {\n done: false,\n value: undefined as unknown as T,\n }\n\n constructor(options: ValueAnimationOptions<T>) {\n super()\n\n this.options = options\n this.initAnimation()\n this.play()\n\n if (options.autoplay === false) this.pause()\n\n notifyAnimationStart(this, this.options)\n }\n\n initAnimation() {\n const { options } = this\n\n replaceTransitionType(options)\n\n const {\n type = keyframesGenerator,\n repeat = 0,\n repeatDelay = 0,\n repeatType,\n velocity = 0,\n } = options\n let { keyframes } = options\n\n const generatorFactory =\n (type as GeneratorFactory) || keyframesGenerator\n\n if (\n process.env.NODE_ENV !== \"production\" &&\n generatorFactory !== keyframesGenerator\n ) {\n invariant(\n keyframes.length <= 2,\n `Only two keyframes currently supported with spring and inertia animations. Trying to animate ${keyframes}`,\n \"spring-two-frames\"\n )\n }\n\n if (\n generatorFactory !== keyframesGenerator &&\n typeof keyframes[0] !== \"number\"\n ) {\n this.mixKeyframes = pipe(\n percentToProgress,\n mix(keyframes[0], keyframes[1])\n ) as (t: number) => T\n\n keyframes = [0 as T, 100 as T]\n }\n\n const generator = generatorFactory(\n keyframes === options.keyframes\n ? options\n : { ...options, keyframes }\n )\n\n /**\n * If we have a mirror repeat type we need to create a second generator that outputs the\n * mirrored (not reversed) animation and later ping pong between the two generators.\n */\n if (repeatType === \"mirror\") {\n this.mirroredGenerator = generatorFactory({\n ...options,\n keyframes: [...keyframes].reverse(),\n velocity: -velocity,\n })\n }\n\n /**\n * If duration is undefined and we have repeat options,\n * we need to calculate a duration from the generator.\n *\n * We set it to the generator itself to cache the duration.\n * Any timeline resolver will need to have already precalculated\n * the duration by this step.\n */\n if (generator.calculatedDuration === null) {\n generator.calculatedDuration = calcGeneratorDuration(generator)\n }\n\n const { calculatedDuration } = generator\n this.calculatedDuration = calculatedDuration\n this.resolvedDuration = calculatedDuration + repeatDelay\n this.totalDuration = this.resolvedDuration * (repeat + 1) - repeatDelay\n this.generator = generator\n }\n\n updateTime(timestamp: number) {\n const animationTime =\n Math.round(timestamp - this.startTime!) * this.playbackSpeed\n\n // Update currentTime\n if (this.holdTime !== null) {\n this.currentTime = this.holdTime\n } else {\n // Rounding the time because floating point arithmetic is not always accurate, e.g. 3000.367 - 1000.367 =\n // 2000.0000000000002. This is a problem when we are comparing the currentTime with the duration, for\n // example.\n this.currentTime = animationTime\n }\n }\n\n tick(timestamp: number, sample = false) {\n const {\n generator,\n totalDuration,\n mixKeyframes,\n mirroredGenerator,\n resolvedDuration,\n calculatedDuration,\n } = this\n\n if (this.startTime === null) return generator.next(0)\n\n const {\n delay = 0,\n keyframes,\n repeat,\n repeatType,\n repeatDelay,\n type,\n onUpdate,\n finalKeyframe,\n } = this.options\n\n if (this.startTime === this.pendingStartTime) {\n this.startTime = resolveStartTime(this.startTime, timestamp)\n }\n this.pendingStartTime = undefined\n\n /**\n * requestAnimationFrame timestamps can come through as lower than\n * the startTime as set by performance.now(). Here we prevent this,\n * though in the future it could be possible to make setting startTime\n * a pending operation that gets resolved here.\n */\n if (this.speed > 0) {\n this.startTime = Math.min(this.startTime, timestamp)\n } else if (this.speed < 0) {\n this.startTime = Math.min(\n timestamp - totalDuration / this.speed,\n this.startTime\n )\n }\n\n if (sample) {\n this.currentTime = timestamp\n } else {\n this.updateTime(timestamp)\n }\n\n // Rebase on delay\n const timeWithoutDelay =\n this.currentTime - delay * (this.playbackSpeed >= 0 ? 1 : -1)\n const isInDelayPhase =\n this.playbackSpeed >= 0\n ? timeWithoutDelay < 0\n : timeWithoutDelay > totalDuration\n this.currentTime = Math.max(timeWithoutDelay, 0)\n\n // If this animation has finished, set the current time to the total duration.\n if (this.state === \"finished\" && this.holdTime === null) {\n this.currentTime = totalDuration\n }\n\n let elapsed = this.currentTime\n let frameGenerator = generator\n\n if (repeat) {\n /**\n * Get the current progress (0-1) of the animation. If t is >\n * than duration we'll get values like 2.5 (midway through the\n * third iteration)\n */\n const progress =\n Math.min(this.currentTime, totalDuration) / resolvedDuration\n\n /**\n * Get the current iteration (0 indexed). For instance the floor of\n * 2.5 is 2.\n */\n let currentIteration = Math.floor(progress)\n\n /**\n * Get the current progress of the iteration by taking the remainder\n * so 2.5 is 0.5 through iteration 2\n */\n let iterationProgress = progress % 1.0\n\n /**\n * If iteration progress is 1 we count that as the end\n * of the previous iteration.\n */\n if (!iterationProgress && progress >= 1) {\n iterationProgress = 1\n }\n\n iterationProgress === 1 && currentIteration--\n\n currentIteration = Math.min(currentIteration, repeat + 1)\n\n /**\n * Reverse progress if we're not running in \"normal\" direction\n */\n\n const isOddIteration = Boolean(currentIteration % 2)\n if (isOddIteration) {\n if (repeatType === \"reverse\") {\n iterationProgress = 1 - iterationProgress\n if (repeatDelay) {\n iterationProgress -= repeatDelay / resolvedDuration\n }\n } else if (repeatType === \"mirror\") {\n frameGenerator = mirroredGenerator!\n }\n }\n\n elapsed = clamp(0, 1, iterationProgress) * resolvedDuration\n }\n\n /**\n * If we're in negative time, set state as the initial keyframe.\n * This prevents delay: x, duration: 0 animations from finishing\n * instantly.\n */\n let state: AnimationState<T>\n if (isInDelayPhase) {\n this.delayState.value = keyframes[0]\n state = this.delayState\n } else {\n state = frameGenerator.next(elapsed)\n }\n\n if (mixKeyframes && !isInDelayPhase) {\n state.value = mixKeyframes(state.value as number)\n }\n\n let { done } = state\n\n if (!isInDelayPhase && calculatedDuration !== null) {\n done =\n this.playbackSpeed >= 0\n ? this.currentTime >= totalDuration\n : this.currentTime <= 0\n }\n\n const isAnimationFinished =\n this.holdTime === null &&\n (this.state === \"finished\" || (this.state === \"running\" && done))\n\n // TODO: The exception for inertia could be cleaner here\n if (isAnimationFinished && type !== inertia) {\n state.value = getFinalKeyframe(\n keyframes,\n this.options,\n finalKeyframe,\n this.speed\n )\n }\n\n if (onUpdate) {\n onUpdate(state.value)\n }\n\n if (isAnimationFinished) {\n this.finish()\n }\n\n return state\n }\n\n /**\n * Allows the returned animation to be awaited or promise-chained. Currently\n * resolves when the animation finishes at all but in a future update could/should\n * reject if its cancels.\n */\n then(resolve: VoidFunction, reject?: VoidFunction) {\n return this.finished.then(resolve, reject)\n }\n\n get duration() {\n return millisecondsToSeconds(this.calculatedDuration)\n }\n\n get iterationDuration() {\n const { delay = 0 } = this.options || {}\n return this.duration + millisecondsToSeconds(delay)\n }\n\n get time() {\n return millisecondsToSeconds(this.currentTime)\n }\n\n set time(newTime: number) {\n newTime = secondsToMilliseconds(newTime)\n this.currentTime = newTime\n\n if (\n this.startTime === null ||\n this.holdTime !== null ||\n this.playbackSpeed === 0\n ) {\n this.holdTime = newTime\n } else if (this.driver) {\n this.startTime = this.driver.now() - newTime / this.playbackSpeed\n }\n\n if (this.driver) {\n this.driver.start(false)\n } else {\n this.startTime = 0\n this.state = \"paused\"\n this.holdTime = newTime\n this.tick(newTime)\n }\n }\n\n /**\n * Returns the generator's velocity at the current time in units/second.\n * Uses the analytical derivative when available (springs), avoiding\n * the MotionValue's frame-dependent velocity estimation.\n */\n getGeneratorVelocity(): number {\n return calcGeneratorVelocity(\n this.generator,\n this.currentTime,\n this.options.velocity\n )\n }\n\n get speed() {\n return this.playbackSpeed\n }\n\n set speed(newSpeed: number) {\n const hasChanged = this.playbackSpeed !== newSpeed\n\n if (hasChanged && this.driver) {\n this.updateTime(time.now())\n }\n\n this.playbackSpeed = newSpeed\n\n if (hasChanged && this.driver) {\n this.time = millisecondsToSeconds(this.currentTime)\n }\n }\n\n play() {\n if (this.isStopped) return\n\n const { driver = frameloopDriver, startTime } = this.options\n\n if (!this.driver) {\n this.driver = driver((timestamp) => this.tick(timestamp))\n }\n\n this.options.onPlay?.()\n\n const now = this.driver.now()\n\n if (this.state === \"finished\") {\n this.updateFinished()\n this.startTime = now\n } else if (this.holdTime !== null) {\n this.startTime = now - this.holdTime / (this.playbackSpeed || 1)\n } else if (!this.startTime) {\n this.startTime = startTime ?? (this.pendingStartTime = now)\n }\n\n if (this.state === \"finished\" && this.speed < 0) {\n this.startTime += this.calculatedDuration\n }\n\n this.holdTime = null\n\n /**\n * Set playState to running only after we've used it in\n * the previous logic.\n */\n this.state = \"running\"\n\n this.driver.start()\n }\n\n pause() {\n this.state = \"paused\"\n this.updateTime(time.now())\n this.holdTime = this.currentTime\n }\n\n /**\n * This method is bound to the instance to fix a pattern where\n * animation.stop is returned as a reference from a useEffect.\n */\n stop = () => {\n const { motionValue } = this.options\n if (motionValue && motionValue.updatedAt !== time.now()) {\n this.tick(time.now())\n }\n\n this.isStopped = true\n if (this.state === \"idle\") return\n this.teardown()\n this.options.onStop?.()\n }\n\n complete() {\n if (this.state !== \"running\") {\n this.play()\n }\n\n this.state = \"finished\"\n this.holdTime = null\n }\n\n finish() {\n this.notifyFinished()\n this.teardown()\n this.state = \"finished\"\n\n this.options.onComplete?.()\n }\n\n cancel() {\n this.holdTime = null\n this.startTime = 0\n this.tick(0)\n this.teardown()\n this.options.onCancel?.()\n }\n\n private teardown() {\n this.state = \"idle\"\n this.stopDriver()\n this.startTime = this.holdTime = null\n }\n\n private stopDriver() {\n if (!this.driver) return\n this.driver.stop()\n this.driver = undefined\n }\n\n sample(sampleTime: number): AnimationState<T> {\n this.startTime = 0\n return this.tick(sampleTime, true)\n }\n\n attachTimeline(timeline: TimelineWithFallback): VoidFunction {\n if (this.options.allowFlatten) {\n this.options.type = \"keyframes\"\n this.options.ease = \"linear\"\n this.initAnimation()\n }\n\n this.driver?.stop()\n return timeline.observe(this)\n }\n}\n\n// Legacy function support\nexport function animateValue<T extends number | string>(\n options: ValueAnimationOptions<T>\n) {\n return new JSAnimation(options)\n}\n"],"names":["keyframesGenerator","keyframes"],"mappings":";;;;;;;;;;;;;;AA8BA,MAAM,iBAAiB,GAAG,CAAC,OAAe,KAAK,OAAO,GAAG,GAAG;AAEtD,MAAO,WACT,SAAQ,WAAW,CAAA;AAgEnB,IAAA,WAAA,CAAY,OAAiC,EAAA;AACzC,QAAA,KAAK,EAAE;QA9DX,IAAA,CAAA,KAAK,GAAuB,MAAM;QAElC,IAAA,CAAA,SAAS,GAAkB,IAAI;QAQvB,IAAA,CAAA,SAAS,GAAG,KAAK;AAiBzB;;AAEG;QACK,IAAA,CAAA,WAAW,GAAW,CAAC;AAE/B;;AAEG;QACK,IAAA,CAAA,QAAQ,GAAkB,IAAI;AAEtC;;AAEG;QACK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AAO,CAAC,IAAI,GAAG,WAAW;AAC/B,YAAA,IAAI,CAAC,OAAO,CAAC,IAAI,GAAG,QAAQ;YAC5B,IAAI,CAAC,aAAa,EAAE;QACxB;AAEA,QAAA,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE;AACnB,QAAA,OAAO,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC;IACjC;AACH;AAED;AACM,SAAU,YAAY,CACxB,OAAiC,EAAA;AAEjC,IAAA,OAAO,IAAI,WAAW,CAAC,OAAO,CAAC;AACnC;;;;"}
|
|
1
|
+
{"version":3,"file":"JSAnimation.mjs","sources":["../../../src/animation/JSAnimation.ts"],"sourcesContent":["import {\n clamp,\n invariant,\n millisecondsToSeconds,\n pipe,\n secondsToMilliseconds,\n} from \"motion-utils\"\nimport { time } from \"../frameloop/sync-time\"\nimport { mix } from \"../utils/mix\"\nimport { Mixer } from \"../utils/mix/types\"\nimport { frameloopDriver } from \"./drivers/frame\"\nimport { DriverControls } from \"./drivers/types\"\nimport { inertia } from \"./generators/inertia\"\nimport { keyframes as keyframesGenerator } from \"./generators/keyframes\"\nimport { calcGeneratorDuration } from \"./generators/utils/calc-duration\"\nimport { calcGeneratorVelocity } from \"./generators/utils/velocity\"\nimport { getFinalKeyframe } from \"./keyframes/get-final\"\nimport {\n AnimationPlaybackControlsWithThen,\n AnimationState,\n GeneratorFactory,\n KeyframeGenerator,\n TimelineWithFallback,\n ValueAnimationOptions,\n} from \"./types\"\nimport { replaceTransitionType } from \"./utils/replace-transition-type\"\nimport { resolveStartTime } from \"./utils/resolve-start-time\"\nimport { notifyAnimationStart } from \"./utils/notify-inspector\"\nimport { WithPromise } from \"./utils/WithPromise\"\n\nconst percentToProgress = (percent: number) => percent / 100\n\nexport const round = (value: number) => Math.round(value * 10000) / 10000\n\nexport class JSAnimation<T extends number | string>\n extends WithPromise\n implements AnimationPlaybackControlsWithThen\n{\n state: AnimationPlayState = \"idle\"\n\n startTime: number | null = null\n\n /**\n * The driver that's controlling the animation loop. Normally this is a requestAnimationFrame loop\n * but in tests we can pass in a synchronous loop.\n */\n protected driver?: DriverControls\n\n protected isStopped = false\n\n /**\n * The start time this animation picked for itself, until its first tick.\n */\n private pendingStartTime?: number\n\n private generator: KeyframeGenerator<T>\n\n private calculatedDuration: number\n\n protected resolvedDuration: number\n\n protected totalDuration: number\n\n protected options: ValueAnimationOptions<T>\n\n /**\n * The current time of the animation.\n */\n private currentTime: number = 0\n\n /**\n * The time at which the animation was paused.\n */\n protected holdTime: number | null = null\n\n /**\n * Playback speed as a factor. 0 would be stopped, -1 reverse and 2 double speed.\n */\n private playbackSpeed = 1\n\n /*\n * If our generator doesn't support mixing numbers, we need to replace keyframes with\n * [0, 100] and then make a function that maps that to the actual keyframes.\n *\n * 100 is chosen instead of 1 as it works nicer with spring animations.\n */\n private mixKeyframes: Mixer<T> | undefined\n\n private mirroredGenerator: KeyframeGenerator<T> | undefined\n\n /**\n * Reusable state object for the delay phase to avoid\n * allocating a new object every frame.\n */\n private delayState: AnimationState<T> = {\n done: false,\n value: undefined as unknown as T,\n }\n\n constructor(options: ValueAnimationOptions<T>) {\n super()\n\n this.options = options\n this.initAnimation()\n this.play()\n\n if (options.autoplay === false) this.pause()\n\n notifyAnimationStart(this, this.options)\n }\n\n initAnimation() {\n const { options } = this\n\n replaceTransitionType(options)\n\n const {\n type = keyframesGenerator,\n repeat = 0,\n repeatDelay = 0,\n repeatType,\n velocity = 0,\n } = options\n let { keyframes } = options\n\n const generatorFactory =\n (type as GeneratorFactory) || keyframesGenerator\n\n if (\n process.env.NODE_ENV !== \"production\" &&\n generatorFactory !== keyframesGenerator\n ) {\n invariant(\n keyframes.length <= 2,\n `Only two keyframes currently supported with spring and inertia animations. Trying to animate ${keyframes}`,\n \"spring-two-frames\"\n )\n }\n\n if (\n generatorFactory !== keyframesGenerator &&\n typeof keyframes[0] !== \"number\"\n ) {\n this.mixKeyframes = pipe(\n percentToProgress,\n mix(keyframes[0], keyframes[1])\n ) as (t: number) => T\n\n keyframes = [0 as T, 100 as T]\n }\n\n const generator = generatorFactory(\n keyframes === options.keyframes\n ? options\n : { ...options, keyframes }\n )\n\n /**\n * If we have a mirror repeat type we need to create a second generator that outputs the\n * mirrored (not reversed) animation and later ping pong between the two generators.\n */\n if (repeatType === \"mirror\") {\n this.mirroredGenerator = generatorFactory({\n ...options,\n keyframes: [...keyframes].reverse(),\n velocity: -velocity,\n })\n }\n\n /**\n * If duration is undefined and we have repeat options,\n * we need to calculate a duration from the generator.\n *\n * We set it to the generator itself to cache the duration.\n * Any timeline resolver will need to have already precalculated\n * the duration by this step.\n */\n if (generator.calculatedDuration === null) {\n generator.calculatedDuration = calcGeneratorDuration(generator)\n }\n\n const { calculatedDuration } = generator\n this.calculatedDuration = calculatedDuration\n this.resolvedDuration = calculatedDuration + repeatDelay\n this.totalDuration = this.resolvedDuration * (repeat + 1) - repeatDelay\n this.generator = generator\n }\n\n updateTime(timestamp: number) {\n const animationTime =\n Math.round(timestamp - this.startTime!) * this.playbackSpeed\n\n // Update currentTime\n if (this.holdTime !== null) {\n this.currentTime = this.holdTime\n } else {\n // Rounding the time because floating point arithmetic is not always accurate, e.g. 3000.367 - 1000.367 =\n // 2000.0000000000002. This is a problem when we are comparing the currentTime with the duration, for\n // example.\n this.currentTime = animationTime\n }\n }\n\n tick(timestamp: number, sample = false) {\n const { generator, totalDuration, calculatedDuration } = this\n\n if (this.startTime === null) return generator.next(0)\n\n const {\n delay = 0,\n keyframes,\n type,\n onUpdate,\n finalKeyframe,\n } = this.options\n\n if (this.startTime === this.pendingStartTime) {\n this.startTime = resolveStartTime(this.startTime, timestamp)\n }\n this.pendingStartTime = undefined\n\n /**\n * requestAnimationFrame timestamps can come through as lower than\n * the startTime as set by performance.now(). Here we prevent this,\n * though in the future it could be possible to make setting startTime\n * a pending operation that gets resolved here.\n */\n if (this.speed > 0) {\n this.startTime = Math.min(this.startTime, timestamp)\n } else if (this.speed < 0) {\n this.startTime = Math.min(\n timestamp - totalDuration / this.speed,\n this.startTime\n )\n }\n\n if (sample) {\n this.currentTime = timestamp\n } else {\n this.updateTime(timestamp)\n }\n\n // Rebase on delay\n const timeWithoutDelay =\n this.currentTime - delay * (this.playbackSpeed >= 0 ? 1 : -1)\n const isInDelayPhase =\n this.playbackSpeed >= 0\n ? timeWithoutDelay < 0\n : timeWithoutDelay > totalDuration\n this.currentTime = Math.max(timeWithoutDelay, 0)\n\n // If this animation has finished, set the current time to the total duration.\n if (this.state === \"finished\" && this.holdTime === null) {\n this.currentTime = totalDuration\n }\n\n const state = this.generate(this.currentTime, isInDelayPhase)\n\n let { done } = state\n\n if (!isInDelayPhase && calculatedDuration !== null) {\n done =\n this.playbackSpeed >= 0\n ? this.currentTime >= totalDuration\n : this.currentTime <= 0\n }\n\n const isAnimationFinished =\n this.holdTime === null &&\n (this.state === \"finished\" || (this.state === \"running\" && done))\n\n // TODO: The exception for inertia could be cleaner here\n if (isAnimationFinished && type !== inertia) {\n state.value = getFinalKeyframe(\n keyframes,\n this.options,\n finalKeyframe,\n this.speed\n )\n }\n\n if (onUpdate) {\n onUpdate(state.value)\n }\n\n if (isAnimationFinished) {\n this.finish()\n }\n\n return state\n }\n\n /**\n * This animation's time, including delay, at a timestamp.\n */\n at(timestamp: number) {\n return (\n this.holdTime ??\n round((timestamp - this.startTime!) * this.playbackSpeed)\n )\n }\n\n /**\n * The value at a timestamp, without changing this animation's state.\n * If resolved, it ends at the last resolved keyframe, which is in the\n * same unit as the others, rather than at finalKeyframe.\n */\n sampleAt(timestamp: number, resolved?: boolean): T {\n const { options, totalDuration } = this\n const t = this.at(timestamp) - (options.delay || 0)\n\n return t >= totalDuration && options.type !== inertia\n ? getFinalKeyframe(\n options.keyframes,\n options,\n resolved ? undefined : options.finalKeyframe\n )\n : this.generate(Math.max(t, 0), t < 0).value\n }\n\n /**\n * The generator's state `currentTime` ms after the delay, without\n * changing the animation's own state.\n */\n protected generate(\n currentTime: number,\n isInDelayPhase: boolean\n ): AnimationState<T> {\n const {\n generator,\n totalDuration,\n mixKeyframes,\n mirroredGenerator,\n resolvedDuration,\n } = this\n const { keyframes, repeat, repeatType, repeatDelay } = this.options\n\n let elapsed = currentTime\n let frameGenerator = generator\n\n if (repeat) {\n /**\n * Get the current progress (0-1) of the animation. If t is >\n * than duration we'll get values like 2.5 (midway through the\n * third iteration)\n */\n const progress =\n Math.min(currentTime, totalDuration) / resolvedDuration\n\n /**\n * Get the current iteration (0 indexed). For instance the floor of\n * 2.5 is 2.\n */\n let currentIteration = Math.floor(progress)\n\n /**\n * Get the current progress of the iteration by taking the remainder\n * so 2.5 is 0.5 through iteration 2\n */\n let iterationProgress = progress % 1.0\n\n /**\n * If iteration progress is 1 we count that as the end\n * of the previous iteration.\n */\n if (!iterationProgress && progress >= 1) {\n iterationProgress = 1\n }\n\n iterationProgress === 1 && currentIteration--\n\n currentIteration = Math.min(currentIteration, repeat + 1)\n\n /**\n * Reverse progress if we're not running in \"normal\" direction\n */\n\n const isOddIteration = Boolean(currentIteration % 2)\n if (isOddIteration) {\n if (repeatType === \"reverse\") {\n iterationProgress = 1 - iterationProgress\n if (repeatDelay) {\n iterationProgress -= repeatDelay / resolvedDuration\n }\n } else if (repeatType === \"mirror\") {\n frameGenerator = mirroredGenerator!\n }\n }\n\n elapsed = clamp(0, 1, iterationProgress) * resolvedDuration\n }\n\n /**\n * If we're in negative time, set state as the initial keyframe.\n * This prevents delay: x, duration: 0 animations from finishing\n * instantly.\n */\n let state: AnimationState<T>\n if (isInDelayPhase) {\n this.delayState.value = keyframes[0]\n state = this.delayState\n } else {\n state = frameGenerator.next(elapsed)\n }\n\n if (mixKeyframes && !isInDelayPhase) {\n state.value = mixKeyframes(state.value as number)\n }\n\n return state\n }\n\n /**\n * Allows the returned animation to be awaited or promise-chained. Currently\n * resolves when the animation finishes at all but in a future update could/should\n * reject if its cancels.\n */\n then(resolve: VoidFunction, reject?: VoidFunction) {\n return this.finished.then(resolve, reject)\n }\n\n get duration() {\n return millisecondsToSeconds(this.calculatedDuration)\n }\n\n get iterationDuration() {\n const { delay = 0 } = this.options || {}\n return this.duration + millisecondsToSeconds(delay)\n }\n\n get time() {\n return millisecondsToSeconds(this.currentTime)\n }\n\n set time(newTime: number) {\n newTime = secondsToMilliseconds(newTime)\n this.currentTime = newTime\n\n if (\n this.startTime === null ||\n this.holdTime !== null ||\n this.playbackSpeed === 0\n ) {\n this.holdTime = newTime\n } else if (this.driver) {\n this.startTime = this.driver.now() - newTime / this.playbackSpeed\n }\n\n if (this.driver) {\n this.driver.start(false)\n } else {\n this.startTime = 0\n this.state = \"paused\"\n this.holdTime = newTime\n this.tick(newTime)\n }\n }\n\n /**\n * Returns the generator's velocity at the current time in units/second.\n * Uses the analytical derivative when available (springs), avoiding\n * the MotionValue's frame-dependent velocity estimation.\n */\n getGeneratorVelocity(): number {\n return calcGeneratorVelocity(\n this.generator,\n this.currentTime,\n this.options.velocity\n )\n }\n\n get speed() {\n return this.playbackSpeed\n }\n\n set speed(newSpeed: number) {\n const hasChanged = this.playbackSpeed !== newSpeed\n\n if (hasChanged && this.driver) {\n this.updateTime(time.now())\n }\n\n this.playbackSpeed = newSpeed\n\n if (hasChanged && this.driver) {\n this.time = millisecondsToSeconds(this.currentTime)\n }\n }\n\n play() {\n if (this.isStopped) return\n\n const { driver = frameloopDriver, startTime } = this.options\n\n if (!this.driver) {\n this.driver = driver((timestamp) => this.tick(timestamp))\n }\n\n this.options.onPlay?.()\n\n const now = this.driver.now()\n\n if (this.state === \"finished\") {\n this.updateFinished()\n this.startTime = now\n } else if (this.holdTime !== null) {\n this.startTime = now - this.holdTime / (this.playbackSpeed || 1)\n } else if (!this.startTime) {\n this.startTime = startTime ?? (this.pendingStartTime = now)\n }\n\n if (this.state === \"finished\" && this.speed < 0) {\n this.startTime += this.calculatedDuration\n }\n\n this.holdTime = null\n\n /**\n * Set playState to running only after we've used it in\n * the previous logic.\n */\n this.state = \"running\"\n\n this.driver.start()\n }\n\n pause() {\n this.state = \"paused\"\n this.updateTime(time.now())\n this.holdTime = this.currentTime\n }\n\n /**\n * This method is bound to the instance to fix a pattern where\n * animation.stop is returned as a reference from a useEffect.\n */\n stop = () => {\n const { motionValue } = this.options\n if (motionValue && motionValue.updatedAt !== time.now()) {\n this.tick(time.now())\n }\n\n this.isStopped = true\n if (this.state === \"idle\") return\n this.teardown()\n this.options.onStop?.()\n }\n\n complete() {\n if (this.state !== \"running\") {\n this.play()\n }\n\n this.state = \"finished\"\n this.holdTime = null\n }\n\n finish() {\n this.notifyFinished()\n this.teardown()\n this.state = \"finished\"\n\n this.options.onComplete?.()\n }\n\n cancel() {\n this.holdTime = null\n this.startTime = 0\n this.tick(0)\n this.teardown()\n this.options.onCancel?.()\n }\n\n private teardown() {\n this.state = \"idle\"\n this.stopDriver()\n this.startTime = this.holdTime = null\n }\n\n private stopDriver() {\n if (!this.driver) return\n this.driver.stop()\n this.driver = undefined\n }\n\n sample(sampleTime: number): AnimationState<T> {\n this.startTime = 0\n return this.tick(sampleTime, true)\n }\n\n attachTimeline(timeline: TimelineWithFallback): VoidFunction {\n if (this.options.allowFlatten) {\n this.options.type = \"keyframes\"\n this.options.ease = \"linear\"\n this.initAnimation()\n }\n\n this.driver?.stop()\n return timeline.observe(this)\n }\n}\n\n// Legacy function support\nexport function animateValue<T extends number | string>(\n options: ValueAnimationOptions<T>\n) {\n return new JSAnimation(options)\n}\n"],"names":["keyframesGenerator","keyframes"],"mappings":";;;;;;;;;;;;;;AA8BA,MAAM,iBAAiB,GAAG,CAAC,OAAe,KAAK,OAAO,GAAG,GAAG;AAErD,MAAM,KAAK,GAAG,CAAC,KAAa,KAAK,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG;AAE9D,MAAO,WACT,SAAQ,WAAW,CAAA;AAgEnB,IAAA,WAAA,CAAY,OAAiC,EAAA;AACzC,QAAA,KAAK,EAAE;QA9DX,IAAA,CAAA,KAAK,GAAuB,MAAM;QAElC,IAAA,CAAA,SAAS,GAAkB,IAAI;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,aAAa,EAAE;QACxB;AAEA,QAAA,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE;AACnB,QAAA,OAAO,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC;IACjC;AACH;AAED;AACM,SAAU,YAAY,CACxB,OAAiC,EAAA;AAEjC,IAAA,OAAO,IAAI,WAAW,CAAC,OAAO,CAAC;AACnC;;;;"}
|
|
@@ -43,46 +43,73 @@ class NativeAnimationExtended extends NativeAnimation {
|
|
|
43
43
|
}
|
|
44
44
|
this.options = options;
|
|
45
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* The animation's time now, in ms. Wall-clock rather than WAAPI's
|
|
48
|
+
* currentTime while running, as under CPU load currentTime may not
|
|
49
|
+
* reflect the actual elapsed time, causing incorrect sampling and
|
|
50
|
+
* visual jumps.
|
|
51
|
+
*/
|
|
52
|
+
elapsed() {
|
|
53
|
+
return this.state === "running"
|
|
54
|
+
? (time.now() - this.startTime) * this.speed
|
|
55
|
+
: this.time * 1000;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The value at a time, from a renderless JS animation.
|
|
59
|
+
*/
|
|
60
|
+
sample(sampleTime) {
|
|
61
|
+
if (!this.sampler) {
|
|
62
|
+
const { motionValue, onUpdate, onComplete, onPlay, onStop, element, ...options } = this.options;
|
|
63
|
+
/**
|
|
64
|
+
* Stop it straight away so its driver doesn't run every frame.
|
|
65
|
+
* It can still be sampled.
|
|
66
|
+
*/
|
|
67
|
+
this.sampler = new JSAnimation({ ...options, autoplay: false });
|
|
68
|
+
this.sampler.stop();
|
|
69
|
+
}
|
|
70
|
+
return this.sampler.sample(Math.max(0, sampleTime)).value;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The value now, as WAAPI doesn't set the motion value while it runs.
|
|
74
|
+
*/
|
|
75
|
+
liveValue() {
|
|
76
|
+
const { state } = this;
|
|
77
|
+
if (state === "running" || state === "paused") {
|
|
78
|
+
return this.sample(this.elapsed());
|
|
79
|
+
}
|
|
80
|
+
}
|
|
46
81
|
/**
|
|
47
82
|
* WAAPI doesn't natively have any interruption capabilities.
|
|
48
83
|
*
|
|
49
84
|
* Rather than read committed styles back out of the DOM, we can
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
85
|
+
* sample a renderless JS animation twice to calculate its current
|
|
86
|
+
* value, "previous" value, and therefore allow Motion to calculate
|
|
87
|
+
* velocity for any subsequent animation.
|
|
53
88
|
*/
|
|
54
89
|
updateMotionValue(value) {
|
|
55
|
-
const { motionValue,
|
|
90
|
+
const { motionValue, element, name } = this.options;
|
|
56
91
|
if (!motionValue)
|
|
57
92
|
return;
|
|
58
93
|
if (value !== undefined) {
|
|
59
94
|
motionValue.set(value);
|
|
60
95
|
return;
|
|
61
96
|
}
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
* Under CPU load, WAAPI's currentTime may not reflect actual
|
|
69
|
-
* elapsed time, causing incorrect sampling and visual jumps.
|
|
70
|
-
*/
|
|
71
|
-
const sampleTime = Math.max(sampleDelta, time.now() - this.startTime);
|
|
72
|
-
const delta = clamp(0, sampleDelta, sampleTime - sampleDelta);
|
|
73
|
-
const current = sampleAnimation.sample(sampleTime).value;
|
|
97
|
+
const { speed } = this;
|
|
98
|
+
const sampleTime = this.elapsed();
|
|
99
|
+
const delta = this.state === "running" && speed
|
|
100
|
+
? clamp(0, sampleDelta, sampleTime)
|
|
101
|
+
: 0;
|
|
102
|
+
const current = this.sample(sampleTime);
|
|
74
103
|
/**
|
|
75
104
|
* Write the estimated value to inline style so it persists
|
|
76
105
|
* after cancel(), covering the async gap before the next
|
|
77
106
|
* animation starts.
|
|
78
107
|
*/
|
|
79
|
-
const { name } = this.options;
|
|
80
108
|
if (element && name)
|
|
81
109
|
setStyle(element, name, current);
|
|
82
|
-
motionValue.setWithVelocity(
|
|
83
|
-
sampleAnimation.stop();
|
|
110
|
+
motionValue.setWithVelocity(this.sample(sampleTime - delta * Math.sign(speed)), current, delta / Math.abs(speed || 1));
|
|
84
111
|
}
|
|
85
112
|
}
|
|
86
113
|
|
|
87
|
-
export { NativeAnimationExtended };
|
|
114
|
+
export { NativeAnimationExtended, sampleDelta };
|
|
88
115
|
//# sourceMappingURL=NativeAnimationExtended.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NativeAnimationExtended.mjs","sources":["../../../src/animation/NativeAnimationExtended.ts"],"sourcesContent":["import { clamp } from \"motion-utils\"\nimport { time } from \"../frameloop/sync-time\"\nimport { setStyle } from \"../render/dom/style-set\"\nimport { JSAnimation } from \"./JSAnimation\"\nimport { NativeAnimation, NativeAnimationOptions } from \"./NativeAnimation\"\nimport { AnyResolvedKeyframe, ValueAnimationOptions } from \"./types\"\nimport { replaceTransitionType } from \"./utils/replace-transition-type\"\nimport { replaceStringEasing } from \"./waapi/utils/unsupported-easing\"\n\nexport type NativeAnimationOptionsExtended<T extends AnyResolvedKeyframe> =\n NativeAnimationOptions & ValueAnimationOptions<T> & NativeAnimationOptions\n\n/**\n * 10ms is chosen here as it strikes a balance between smooth\n * results (more than one keyframe per frame at 60fps) and\n * keyframe quantity.\n */\
|
|
1
|
+
{"version":3,"file":"NativeAnimationExtended.mjs","sources":["../../../src/animation/NativeAnimationExtended.ts"],"sourcesContent":["import { clamp } from \"motion-utils\"\nimport { time } from \"../frameloop/sync-time\"\nimport { setStyle } from \"../render/dom/style-set\"\nimport { JSAnimation } from \"./JSAnimation\"\nimport { NativeAnimation, NativeAnimationOptions } from \"./NativeAnimation\"\nimport { AnyResolvedKeyframe, ValueAnimationOptions } from \"./types\"\nimport { replaceTransitionType } from \"./utils/replace-transition-type\"\nimport { replaceStringEasing } from \"./waapi/utils/unsupported-easing\"\n\nexport type NativeAnimationOptionsExtended<T extends AnyResolvedKeyframe> =\n NativeAnimationOptions & ValueAnimationOptions<T> & NativeAnimationOptions\n\n/**\n * 10ms is chosen here as it strikes a balance between smooth\n * results (more than one keyframe per frame at 60fps) and\n * keyframe quantity.\n */\nexport const sampleDelta = 10 //ms\n\nexport class NativeAnimationExtended<\n T extends AnyResolvedKeyframe\n> extends NativeAnimation<T> {\n options: NativeAnimationOptionsExtended<T>\n\n constructor(options: NativeAnimationOptionsExtended<T>) {\n /**\n * The base NativeAnimation function only supports a subset\n * of Motion easings, and WAAPI also only supports some\n * easing functions via string/cubic-bezier definitions.\n *\n * This function replaces those unsupported easing functions\n * with a JS easing function. This will later get compiled\n * to a linear() easing function.\n */\n replaceStringEasing(options)\n\n /**\n * Ensure we replace the transition type with a generator function\n * before passing to WAAPI.\n *\n * TODO: Does this have a better home? It could be shared with\n * JSAnimation.\n */\n replaceTransitionType(options)\n\n super(options)\n\n /**\n * Only set startTime when the animation should autoplay.\n * Setting startTime on a paused WAAPI animation unpauses it\n * (per the WAAPI spec), which breaks autoplay: false.\n */\n if (options.startTime !== undefined && options.autoplay !== false) {\n this.startTime = options.startTime\n }\n\n this.options = options\n }\n\n private sampler?: JSAnimation<T>\n\n /**\n * The animation's time now, in ms. Wall-clock rather than WAAPI's\n * currentTime while running, as under CPU load currentTime may not\n * reflect the actual elapsed time, causing incorrect sampling and\n * visual jumps.\n */\n private elapsed() {\n return this.state === \"running\"\n ? (time.now() - this.startTime) * this.speed\n : this.time * 1000\n }\n\n /**\n * The value at a time, from a renderless JS animation.\n */\n private sample(sampleTime: number) {\n if (!this.sampler) {\n const {\n motionValue,\n onUpdate,\n onComplete,\n onPlay,\n onStop,\n element,\n ...options\n } = this.options\n\n /**\n * Stop it straight away so its driver doesn't run every frame.\n * It can still be sampled.\n */\n this.sampler = new JSAnimation({ ...options, autoplay: false })\n this.sampler.stop()\n }\n\n return this.sampler.sample(Math.max(0, sampleTime)).value\n }\n\n /**\n * The value now, as WAAPI doesn't set the motion value while it runs.\n */\n liveValue() {\n const { state } = this\n\n if (state === \"running\" || state === \"paused\") {\n return this.sample(this.elapsed())\n }\n }\n\n /**\n * WAAPI doesn't natively have any interruption capabilities.\n *\n * Rather than read committed styles back out of the DOM, we can\n * sample a renderless JS animation twice to calculate its current\n * value, \"previous\" value, and therefore allow Motion to calculate\n * velocity for any subsequent animation.\n */\n updateMotionValue(value?: T) {\n const { motionValue, element, name } = this.options\n\n if (!motionValue) return\n\n if (value !== undefined) {\n motionValue.set(value)\n return\n }\n\n const { speed } = this\n const sampleTime = this.elapsed()\n const delta =\n this.state === \"running\" && speed\n ? clamp(0, sampleDelta, sampleTime)\n : 0\n const current = this.sample(sampleTime)\n\n /**\n * Write the estimated value to inline style so it persists\n * after cancel(), covering the async gap before the next\n * animation starts.\n */\n if (element && name) setStyle(element, name, current)\n\n motionValue.setWithVelocity(\n this.sample(sampleTime - delta * Math.sign(speed)),\n current,\n delta / Math.abs(speed || 1)\n )\n }\n}\n"],"names":[],"mappings":";;;;;;;;AAYA;;;;AAIG;AACI,MAAM,WAAW,GAAG,GAAE;AAEvB,MAAO,uBAEX,SAAQ,eAAkB,CAAA;AAGxB,IAAA,WAAA,CAAY,OAA0C,EAAA;AAClD;;;;;;;;AAQG;QACH,mBAAmB,CAAC,OAAO,CAAC;AAE5B;;;;;;AAMG;QACH,qBAAqB,CAAC,OAAO,CAAC;QAE9B,KAAK,CAAC,OAAO,CAAC;AAEd;;;;AAIG;AACH,QAAA,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS,IAAI,OAAO,CAAC,QAAQ,KAAK,KAAK,EAAE;AAC/D,YAAA,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS;QACtC;AAEA,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO;IAC1B;AAIA;;;;;AAKG;IACK,OAAO,GAAA;AACX,QAAA,OAAO,IAAI,CAAC,KAAK,KAAK;AAClB,cAAE,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC;AACvC,cAAE,IAAI,CAAC,IAAI,GAAG,IAAI;IAC1B;AAEA;;AAEG;AACK,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AM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC;AAEvC;;;;AAIG;QACH,IAAI,OAAO,IAAI,IAAI;AAAE,YAAA,QAAQ,CAAC,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC;AAErD,QAAA,WAAW,CAAC,eAAe,CACvB,IAAI,CAAC,MAAM,CAAC,UAAU,GAAG,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAClD,OAAO,EACP,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC,CAAC,CAC/B;IACL;AACH;;;;"}
|
|
@@ -5,7 +5,20 @@ import { calcGeneratorDuration, maxGeneratorDuration } from './calc-duration.mjs
|
|
|
5
5
|
* Create a progress => progress easing function from a generator.
|
|
6
6
|
*/
|
|
7
7
|
function createGeneratorEasing(options, scale = 100, createGenerator) {
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* The easing is generated over 0 - scale, but velocity is in the
|
|
10
|
+
* value's own units, so scale it to match. Otherwise an interrupted
|
|
11
|
+
* spring hands off the wrong speed, or direction. Keyframes with
|
|
12
|
+
* units (colours, filters, mixed units) give NaN, which is falsy,
|
|
13
|
+
* so their velocity passes through unscaled.
|
|
14
|
+
*/
|
|
15
|
+
const { keyframes: k, velocity: v } = options;
|
|
16
|
+
const delta = k && k[k.length - 1] - k[0];
|
|
17
|
+
const generator = createGenerator({
|
|
18
|
+
...options,
|
|
19
|
+
velocity: v && delta ? (v * scale) / delta : v,
|
|
20
|
+
keyframes: [0, scale],
|
|
21
|
+
});
|
|
9
22
|
const duration = Math.min(calcGeneratorDuration(generator), maxGeneratorDuration);
|
|
10
23
|
return {
|
|
11
24
|
type: "keyframes",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-generator-easing.mjs","sources":["../../../../../src/animation/generators/utils/create-generator-easing.ts"],"sourcesContent":["import { millisecondsToSeconds } from \"motion-utils\"\nimport { GeneratorFactory, Transition } from \"../../types\"\nimport { calcGeneratorDuration, maxGeneratorDuration } from \"./calc-duration\"\n\n/**\n * Create a progress => progress easing function from a generator.\n */\nexport function createGeneratorEasing(\n options: Transition,\n scale = 100,\n createGenerator: GeneratorFactory\n) {\n const generator = createGenerator({
|
|
1
|
+
{"version":3,"file":"create-generator-easing.mjs","sources":["../../../../../src/animation/generators/utils/create-generator-easing.ts"],"sourcesContent":["import { millisecondsToSeconds } from \"motion-utils\"\nimport { GeneratorFactory, Transition } from \"../../types\"\nimport { calcGeneratorDuration, maxGeneratorDuration } from \"./calc-duration\"\n\n/**\n * Create a progress => progress easing function from a generator.\n */\nexport function createGeneratorEasing(\n options: Transition,\n scale = 100,\n createGenerator: GeneratorFactory\n) {\n /**\n * The easing is generated over 0 - scale, but velocity is in the\n * value's own units, so scale it to match. Otherwise an interrupted\n * spring hands off the wrong speed, or direction. Keyframes with\n * units (colours, filters, mixed units) give NaN, which is falsy,\n * so their velocity passes through unscaled.\n */\n const { keyframes: k, velocity: v } = options as any\n const delta = k && k[k.length - 1] - k[0]\n\n const generator = createGenerator({\n ...options,\n velocity: v && delta ? (v * scale) / delta : v,\n keyframes: [0, scale],\n })\n const duration = Math.min(\n calcGeneratorDuration(generator),\n maxGeneratorDuration\n )\n\n return {\n type: \"keyframes\",\n ease: (progress: number) => {\n return generator.next(duration * progress).value / scale\n },\n duration: millisecondsToSeconds(duration),\n }\n}\n"],"names":[],"mappings":";;;AAIA;;AAEG;AACG,SAAU,qBAAqB,CACjC,OAAmB,EACnB,KAAK,GAAG,GAAG,EACX,eAAiC,EAAA;AAEjC;;;;;;AAMG;IACH,MAAM,EAAE,SAAS,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,GAAG,OAAc;AACpD,IAAA,MAAM,KAAK,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAEzC,MAAM,SAAS,GAAG,eAAe,CAAC;AAC9B,QAAA,GAAG,OAAO;AACV,QAAA,QAAQ,EAAE,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC,GAAG,KAAK,IAAI,KAAK,GAAG,CAAC;AAC9C,QAAA,SAAS,EAAE,CAAC,CAAC,EAAE,KAAK,CAAC;AACxB,KAAA,CAAC;AACF,IAAA,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CACrB,qBAAqB,CAAC,SAAS,CAAC,EAChC,oBAAoB,CACvB;IAED,OAAO;AACH,QAAA,IAAI,EAAE,WAAW;AACjB,QAAA,IAAI,EAAE,CAAC,QAAgB,KAAI;AACvB,YAAA,OAAO,SAAS,CAAC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC,CAAC,KAAK,GAAG,KAAK;QAC5D,CAAC;AACD,QAAA,QAAQ,EAAE,qBAAqB,CAAC,QAAQ,CAAC;KAC5C;AACL;;;;"}
|
|
@@ -39,6 +39,7 @@ const animateMotionValue = (name, value, target, transition = {}, element, isHan
|
|
|
39
39
|
name,
|
|
40
40
|
motionValue: value,
|
|
41
41
|
element: isHandoff ? undefined : element,
|
|
42
|
+
hasOnUpdate: !!valueTransition.onUpdate,
|
|
42
43
|
};
|
|
43
44
|
/**
|
|
44
45
|
* If there's no transition defined for this value, we can generate
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"motion-value.mjs","sources":["../../../../src/animation/interfaces/motion-value.ts"],"sourcesContent":["import { MotionGlobalConfig, secondsToMilliseconds } from \"motion-utils\"\nimport { AsyncMotionValueAnimation } from \"../AsyncMotionValueAnimation\"\nimport { JSAnimation } from \"../JSAnimation\"\nimport type {\n AnyResolvedKeyframe,\n ValueAnimationOptions,\n ValueTransition,\n} from \"../types\"\nimport type { UnresolvedKeyframes } from \"../keyframes/KeyframesResolver\"\nimport { getValueTransition } from \"../utils/get-value-transition\"\nimport { makeAnimationInstant } from \"../utils/make-animation-instant\"\nimport { getDefaultTransition } from \"../utils/default-transitions\"\nimport { getFinalKeyframe } from \"../keyframes/get-final\"\nimport { isTransitionDefined } from \"../utils/is-transition-defined\"\nimport { frame } from \"../../frameloop\"\nimport type { MotionValue, StartAnimation } from \"../../value\"\nimport type { AnimationElement } from \"../keyframes/types\"\n\nexport const animateMotionValue =\n <V extends AnyResolvedKeyframe>(\n name: string,\n value: MotionValue<V>,\n target: V | UnresolvedKeyframes<V>,\n transition: ValueTransition & { elapsed?: number } = {},\n element?: AnimationElement,\n isHandoff?: boolean\n ): StartAnimation =>\n (onComplete) => {\n const valueTransition = getValueTransition(transition, name) || {}\n\n /**\n * Most transition values are currently completely overwritten by value-specific\n * transitions. In the future it'd be nicer to blend these transitions. But for now\n * delay actually does inherit from the root transition if not value-specific.\n */\n const delay = valueTransition.delay || transition.delay || 0\n\n /**\n * Elapsed isn't a public transition option but can be passed through from\n * optimized appear effects in milliseconds.\n */\n let { elapsed = 0 } = transition\n elapsed = elapsed - secondsToMilliseconds(delay)\n\n const options: ValueAnimationOptions = {\n keyframes: Array.isArray(target) ? target : [null, target],\n ease: \"easeOut\",\n velocity: value.getVelocity(),\n ...valueTransition,\n delay: -elapsed,\n onUpdate: (v) => {\n value.set(v)\n valueTransition.onUpdate && valueTransition.onUpdate(v)\n },\n onComplete: () => {\n onComplete()\n valueTransition.onComplete && valueTransition.onComplete()\n },\n name,\n motionValue: value,\n element: isHandoff ? undefined : element,\n }\n\n /**\n * If there's no transition defined for this value, we can generate\n * unique transition settings for this value.\n */\n if (!isTransitionDefined(valueTransition)) {\n Object.assign(options, getDefaultTransition(name, options))\n }\n\n /**\n * Both WAAPI and our internal animation functions use durations\n * as defined by milliseconds, while our external API defines them\n * as seconds.\n */\n options.duration &&= secondsToMilliseconds(options.duration)\n options.repeatDelay &&= secondsToMilliseconds(options.repeatDelay)\n\n /**\n * Support deprecated way to set initial value. Prefer keyframe syntax.\n */\n if (options.from !== undefined) {\n options.keyframes[0] = options.from as any\n }\n\n let shouldSkip = false\n\n if (\n (options as any).type === false ||\n (options.duration === 0 && !options.repeatDelay)\n ) {\n makeAnimationInstant(options)\n\n if (options.delay === 0) {\n shouldSkip = true\n }\n }\n\n if (\n MotionGlobalConfig.instantAnimations ||\n MotionGlobalConfig.skipAnimations ||\n element?.shouldSkipAnimations ||\n valueTransition.skipAnimations\n ) {\n shouldSkip = true\n makeAnimationInstant(options)\n options.delay = 0\n }\n\n /**\n * If the transition type or easing has been explicitly set by the user\n * then we don't want to allow flattening the animation.\n */\n options.allowFlatten = !valueTransition.type && !valueTransition.ease\n\n /**\n * If we can or must skip creating the animation, and apply only\n * the final keyframe, do so. We also check once keyframes are resolved but\n * this early check prevents the need to create an animation at all.\n */\n if (shouldSkip && !isHandoff && value.get() !== undefined) {\n const finalKeyframe = getFinalKeyframe<V>(\n options.keyframes as V[],\n valueTransition\n )\n\n if (finalKeyframe !== undefined) {\n frame.update(() => {\n options.onUpdate!(finalKeyframe)\n options.onComplete!()\n })\n\n return\n }\n }\n\n return valueTransition.isSync\n ? new JSAnimation(options)\n : new AsyncMotionValueAnimation(options)\n }\n"],"names":[],"mappings":";;;;;;;;;;MAkBa,kBAAkB,GAC3B,CACI,IAAY,EACZ,KAAqB,EACrB,MAAkC,EAClC,aAAqD,EAAE,EACvD,OAA0B,EAC1B,SAAmB,KAEvB,CAAC,UAAU,KAAI;IACX,MAAM,eAAe,GAAG,kBAAkB,CAAC,UAAU,EAAE,IAAI,CAAC,IAAI,EAAE;AAElE;;;;AAIG;IACH,MAAM,KAAK,GAAG,eAAe,CAAC,KAAK,IAAI,UAAU,CAAC,KAAK,IAAI,CAAC;AAE5D;;;AAGG;AACH,IAAA,IAAI,EAAE,OAAO,GAAG,CAAC,EAAE,GAAG,UAAU;AAChC,IAAA,OAAO,GAAG,OAAO,GAAG,qBAAqB,CAAC,KAAK,CAAC;AAEhD,IAAA,MAAM,OAAO,GAA0B;AACnC,QAAA,SAAS,EAAE,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC;AAC1D,QAAA,IAAI,EAAE,SAAS;AACf,QAAA,QAAQ,EAAE,KAAK,CAAC,WAAW,EAAE;AAC7B,QAAA,GAAG,eAAe;QAClB,KAAK,EAAE,CAAC,OAAO;AACf,QAAA,QAAQ,EAAE,CAAC,CAAC,KAAI;AACZ,YAAA,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;YACZ,eAAe,CAAC,QAAQ,IAAI,eAAe,CAAC,QAAQ,CAAC,CAAC,CAAC;QAC3D,CAAC;QACD,UAAU,EAAE,MAAK;AACb,YAAA,UAAU,EAAE;AACZ,YAAA,eAAe,CAAC,UAAU,IAAI,eAAe,CAAC,UAAU,EAAE;QAC9D,CAAC;QACD,IAAI;AACJ,QAAA,WAAW,EAAE,KAAK;QAClB,OAAO,EAAE,SAAS,GAAG,SAAS,GAAG,OAAO;
|
|
1
|
+
{"version":3,"file":"motion-value.mjs","sources":["../../../../src/animation/interfaces/motion-value.ts"],"sourcesContent":["import { MotionGlobalConfig, secondsToMilliseconds } from \"motion-utils\"\nimport { AsyncMotionValueAnimation } from \"../AsyncMotionValueAnimation\"\nimport { JSAnimation } from \"../JSAnimation\"\nimport type {\n AnyResolvedKeyframe,\n ValueAnimationOptions,\n ValueTransition,\n} from \"../types\"\nimport type { UnresolvedKeyframes } from \"../keyframes/KeyframesResolver\"\nimport { getValueTransition } from \"../utils/get-value-transition\"\nimport { makeAnimationInstant } from \"../utils/make-animation-instant\"\nimport { getDefaultTransition } from \"../utils/default-transitions\"\nimport { getFinalKeyframe } from \"../keyframes/get-final\"\nimport { isTransitionDefined } from \"../utils/is-transition-defined\"\nimport { frame } from \"../../frameloop\"\nimport type { MotionValue, StartAnimation } from \"../../value\"\nimport type { AnimationElement } from \"../keyframes/types\"\n\nexport const animateMotionValue =\n <V extends AnyResolvedKeyframe>(\n name: string,\n value: MotionValue<V>,\n target: V | UnresolvedKeyframes<V>,\n transition: ValueTransition & { elapsed?: number } = {},\n element?: AnimationElement,\n isHandoff?: boolean\n ): StartAnimation =>\n (onComplete) => {\n const valueTransition = getValueTransition(transition, name) || {}\n\n /**\n * Most transition values are currently completely overwritten by value-specific\n * transitions. In the future it'd be nicer to blend these transitions. But for now\n * delay actually does inherit from the root transition if not value-specific.\n */\n const delay = valueTransition.delay || transition.delay || 0\n\n /**\n * Elapsed isn't a public transition option but can be passed through from\n * optimized appear effects in milliseconds.\n */\n let { elapsed = 0 } = transition\n elapsed = elapsed - secondsToMilliseconds(delay)\n\n const options: ValueAnimationOptions = {\n keyframes: Array.isArray(target) ? target : [null, target],\n ease: \"easeOut\",\n velocity: value.getVelocity(),\n ...valueTransition,\n delay: -elapsed,\n onUpdate: (v) => {\n value.set(v)\n valueTransition.onUpdate && valueTransition.onUpdate(v)\n },\n onComplete: () => {\n onComplete()\n valueTransition.onComplete && valueTransition.onComplete()\n },\n name,\n motionValue: value,\n element: isHandoff ? undefined : element,\n hasOnUpdate: !!valueTransition.onUpdate,\n }\n\n /**\n * If there's no transition defined for this value, we can generate\n * unique transition settings for this value.\n */\n if (!isTransitionDefined(valueTransition)) {\n Object.assign(options, getDefaultTransition(name, options))\n }\n\n /**\n * Both WAAPI and our internal animation functions use durations\n * as defined by milliseconds, while our external API defines them\n * as seconds.\n */\n options.duration &&= secondsToMilliseconds(options.duration)\n options.repeatDelay &&= secondsToMilliseconds(options.repeatDelay)\n\n /**\n * Support deprecated way to set initial value. Prefer keyframe syntax.\n */\n if (options.from !== undefined) {\n options.keyframes[0] = options.from as any\n }\n\n let shouldSkip = false\n\n if (\n (options as any).type === false ||\n (options.duration === 0 && !options.repeatDelay)\n ) {\n makeAnimationInstant(options)\n\n if (options.delay === 0) {\n shouldSkip = true\n }\n }\n\n if (\n MotionGlobalConfig.instantAnimations ||\n MotionGlobalConfig.skipAnimations ||\n element?.shouldSkipAnimations ||\n valueTransition.skipAnimations\n ) {\n shouldSkip = true\n makeAnimationInstant(options)\n options.delay = 0\n }\n\n /**\n * If the transition type or easing has been explicitly set by the user\n * then we don't want to allow flattening the animation.\n */\n options.allowFlatten = !valueTransition.type && !valueTransition.ease\n\n /**\n * If we can or must skip creating the animation, and apply only\n * the final keyframe, do so. We also check once keyframes are resolved but\n * this early check prevents the need to create an animation at all.\n */\n if (shouldSkip && !isHandoff && value.get() !== undefined) {\n const finalKeyframe = getFinalKeyframe<V>(\n options.keyframes as V[],\n valueTransition\n )\n\n if (finalKeyframe !== undefined) {\n frame.update(() => {\n options.onUpdate!(finalKeyframe)\n options.onComplete!()\n })\n\n return\n }\n }\n\n return valueTransition.isSync\n ? new JSAnimation(options)\n : new AsyncMotionValueAnimation(options)\n }\n"],"names":[],"mappings":";;;;;;;;;;MAkBa,kBAAkB,GAC3B,CACI,IAAY,EACZ,KAAqB,EACrB,MAAkC,EAClC,aAAqD,EAAE,EACvD,OAA0B,EAC1B,SAAmB,KAEvB,CAAC,UAAU,KAAI;IACX,MAAM,eAAe,GAAG,kBAAkB,CAAC,UAAU,EAAE,IAAI,CAAC,IAAI,EAAE;AAElE;;;;AAIG;IACH,MAAM,KAAK,GAAG,eAAe,CAAC,KAAK,IAAI,UAAU,CAAC,KAAK,IAAI,CAAC;AAE5D;;;AAGG;AACH,IAAA,IAAI,EAAE,OAAO,GAAG,CAAC,EAAE,GAAG,UAAU;AAChC,IAAA,OAAO,GAAG,OAAO,GAAG,qBAAqB,CAAC,KAAK,CAAC;AAEhD,IAAA,MAAM,OAAO,GAA0B;AACnC,QAAA,SAAS,EAAE,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC;AAC1D,QAAA,IAAI,EAAE,SAAS;AACf,QAAA,QAAQ,EAAE,KAAK,CAAC,WAAW,EAAE;AAC7B,QAAA,GAAG,eAAe;QAClB,KAAK,EAAE,CAAC,OAAO;AACf,QAAA,QAAQ,EAAE,CAAC,CAAC,KAAI;AACZ,YAAA,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;YACZ,eAAe,CAAC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