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.
- package/dist/cjs/index.js +3961 -3188
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/animation/AsyncMotionValueAnimation.mjs +41 -38
- package/dist/es/animation/AsyncMotionValueAnimation.mjs.map +1 -1
- package/dist/es/animation/FollowAnimation.mjs +152 -0
- package/dist/es/animation/FollowAnimation.mjs.map +1 -0
- package/dist/es/animation/JSAnimation.mjs +7 -11
- package/dist/es/animation/JSAnimation.mjs.map +1 -1
- package/dist/es/animation/NativeAnimation.mjs +2 -0
- package/dist/es/animation/NativeAnimation.mjs.map +1 -1
- package/dist/es/animation/NativeAnimationWrapper.mjs +7 -0
- package/dist/es/animation/NativeAnimationWrapper.mjs.map +1 -1
- package/dist/es/animation/animate/effects.mjs +70 -13
- package/dist/es/animation/animate/effects.mjs.map +1 -1
- package/dist/es/animation/animate/element.mjs +80 -0
- package/dist/es/animation/animate/element.mjs.map +1 -0
- package/dist/es/animation/animate/single-value.mjs.map +1 -1
- package/dist/es/animation/generators/keyframes.mjs +25 -1
- package/dist/es/animation/generators/keyframes.mjs.map +1 -1
- package/dist/es/animation/generators/spring.mjs +100 -52
- package/dist/es/animation/generators/spring.mjs.map +1 -1
- package/dist/es/animation/generators/utils/velocity.mjs +13 -1
- package/dist/es/animation/generators/utils/velocity.mjs.map +1 -1
- package/dist/es/animation/interfaces/motion-value.mjs.map +1 -1
- package/dist/es/animation/interfaces/visual-element-target.mjs.map +1 -1
- package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs +12 -6
- package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs.map +1 -1
- package/dist/es/animation/keyframes/KeyframesResolver.mjs +38 -7
- package/dist/es/animation/keyframes/KeyframesResolver.mjs.map +1 -1
- package/dist/es/animation/keyframes/utils/make-none-animatable.mjs +8 -2
- package/dist/es/animation/keyframes/utils/make-none-animatable.mjs.map +1 -1
- package/dist/es/animation/keyframes/utils/unit-conversion.mjs +45 -19
- package/dist/es/animation/keyframes/utils/unit-conversion.mjs.map +1 -1
- package/dist/es/animation/utils/WithPromise.mjs +12 -5
- package/dist/es/animation/utils/WithPromise.mjs.map +1 -1
- package/dist/es/animation/utils/arc.mjs +2 -1
- package/dist/es/animation/utils/arc.mjs.map +1 -1
- package/dist/es/animation/utils/can-animate.mjs +3 -1
- package/dist/es/animation/utils/can-animate.mjs.map +1 -1
- package/dist/es/animation/utils/notify-inspector.mjs +56 -0
- package/dist/es/animation/utils/notify-inspector.mjs.map +1 -0
- package/dist/es/animation/waapi/supports/waapi.mjs +8 -1
- package/dist/es/animation/waapi/supports/waapi.mjs.map +1 -1
- package/dist/es/effects/MotionValueState.mjs +83 -25
- package/dist/es/effects/MotionValueState.mjs.map +1 -1
- package/dist/es/effects/attr/index.mjs +15 -9
- package/dist/es/effects/attr/index.mjs.map +1 -1
- package/dist/es/effects/prop/index.mjs +15 -2
- package/dist/es/effects/prop/index.mjs.map +1 -1
- package/dist/es/effects/style/index.mjs +52 -9
- package/dist/es/effects/style/index.mjs.map +1 -1
- package/dist/es/effects/style/transform.mjs +28 -25
- package/dist/es/effects/style/transform.mjs.map +1 -1
- package/dist/es/effects/svg/index.mjs +40 -7
- package/dist/es/effects/svg/index.mjs.map +1 -1
- package/dist/es/effects/utils/create-dom-effect.mjs +9 -1
- package/dist/es/effects/utils/create-dom-effect.mjs.map +1 -1
- package/dist/es/effects/utils/create-effect.mjs +2 -0
- package/dist/es/effects/utils/create-effect.mjs.map +1 -1
- package/dist/es/frameloop/render-step.mjs +5 -2
- package/dist/es/frameloop/render-step.mjs.map +1 -1
- package/dist/es/index.mjs +5 -4
- package/dist/es/index.mjs.map +1 -1
- package/dist/es/layout/LayoutAnimationBuilder.mjs +9 -1
- package/dist/es/layout/LayoutAnimationBuilder.mjs.map +1 -1
- package/dist/es/projection/node/create-projection-node.mjs +4 -1
- package/dist/es/projection/node/create-projection-node.mjs.map +1 -1
- package/dist/es/render/VisualElement.mjs +11 -2
- package/dist/es/render/VisualElement.mjs.map +1 -1
- package/dist/es/render/utils/is-controlling-variants.mjs +8 -2
- package/dist/es/render/utils/is-controlling-variants.mjs.map +1 -1
- package/dist/es/utils/mix/color.mjs +3 -2
- package/dist/es/utils/mix/color.mjs.map +1 -1
- package/dist/es/utils/mix/index.mjs +28 -0
- package/dist/es/utils/mix/index.mjs.map +1 -1
- package/dist/es/value/follow-value.mjs +33 -30
- package/dist/es/value/follow-value.mjs.map +1 -1
- package/dist/es/value/index.mjs +73 -29
- package/dist/es/value/index.mjs.map +1 -1
- package/dist/es/value/types/complex/index.mjs +17 -4
- package/dist/es/value/types/complex/index.mjs.map +1 -1
- package/dist/index.d.ts +245 -50
- package/dist/motion-dom.dev.js +3961 -3188
- 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 +4 -4
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MotionGlobalConfig
|
|
1
|
+
import { MotionGlobalConfig } from 'motion-utils';
|
|
2
2
|
import { time } from '../frameloop/sync-time.mjs';
|
|
3
3
|
import { JSAnimation } from './JSAnimation.mjs';
|
|
4
4
|
import { getFinalKeyframe } from './keyframes/get-final.mjs';
|
|
@@ -19,7 +19,7 @@ import { supportsBrowserAnimation } from './waapi/supports/waapi.mjs';
|
|
|
19
19
|
*/
|
|
20
20
|
const MAX_RESOLVE_DELAY = 40;
|
|
21
21
|
class AsyncMotionValueAnimation extends WithPromise {
|
|
22
|
-
constructor(
|
|
22
|
+
constructor(options) {
|
|
23
23
|
super();
|
|
24
24
|
/**
|
|
25
25
|
* Bound to support return animation.stop pattern
|
|
@@ -32,18 +32,18 @@ class AsyncMotionValueAnimation extends WithPromise {
|
|
|
32
32
|
this.keyframeResolver?.cancel();
|
|
33
33
|
};
|
|
34
34
|
this.createdAt = time.now();
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
35
|
+
const { keyframes, name, motionValue, element } = options;
|
|
36
|
+
/**
|
|
37
|
+
* animateMotionValue builds a fresh options object per value, so
|
|
38
|
+
* it's completed in place as keyframes resolve rather than copied.
|
|
39
|
+
*/
|
|
40
|
+
const optionsWithDefaults = options;
|
|
41
|
+
optionsWithDefaults.autoplay ?? (optionsWithDefaults.autoplay = true);
|
|
42
|
+
optionsWithDefaults.delay ?? (optionsWithDefaults.delay = 0);
|
|
43
|
+
optionsWithDefaults.type ?? (optionsWithDefaults.type = "keyframes");
|
|
44
|
+
optionsWithDefaults.repeat ?? (optionsWithDefaults.repeat = 0);
|
|
45
|
+
optionsWithDefaults.repeatDelay ?? (optionsWithDefaults.repeatDelay = 0);
|
|
46
|
+
optionsWithDefaults.repeatType ?? (optionsWithDefaults.repeatType = "loop");
|
|
47
47
|
const KeyframeResolver$1 = element?.KeyframeResolver || KeyframeResolver;
|
|
48
48
|
this.keyframeResolver = new KeyframeResolver$1(keyframes, (resolvedKeyframes, finalKeyframe, forced) => this.onKeyframesResolved(resolvedKeyframes, finalKeyframe, optionsWithDefaults, !forced), name, motionValue, element);
|
|
49
49
|
this.keyframeResolver?.scheduleResolve();
|
|
@@ -85,11 +85,22 @@ class AsyncMotionValueAnimation extends WithPromise {
|
|
|
85
85
|
? this.resolvedAt
|
|
86
86
|
: this.createdAt
|
|
87
87
|
: undefined;
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
const { onComplete } = options;
|
|
89
|
+
/**
|
|
90
|
+
* A startTime passed in options (an optimised appear handoff syncing
|
|
91
|
+
* to its WAAPI animation) takes precedence over the derived one.
|
|
92
|
+
*/
|
|
93
|
+
options.startTime ?? (options.startTime = startTime);
|
|
94
|
+
options.finalKeyframe = finalKeyframe;
|
|
95
|
+
options.keyframes = keyframes;
|
|
96
|
+
/**
|
|
97
|
+
* JSAnimation and NativeAnimation call onComplete exactly when
|
|
98
|
+
* their own `finished` resolves, so this replaces a promise chain
|
|
99
|
+
* per value with a callback.
|
|
100
|
+
*/
|
|
101
|
+
options.onComplete = () => {
|
|
102
|
+
onComplete?.();
|
|
103
|
+
this.notifyFinished();
|
|
93
104
|
};
|
|
94
105
|
/**
|
|
95
106
|
* Animate via WAAPI if possible. If this is a handoff animation, the optimised animation will be running via
|
|
@@ -99,28 +110,25 @@ class AsyncMotionValueAnimation extends WithPromise {
|
|
|
99
110
|
* Also skip WAAPI when keyframes aren't animatable, as the resolved
|
|
100
111
|
* values may not be valid CSS and would trigger browser warnings.
|
|
101
112
|
*/
|
|
102
|
-
const useWaapi = canAnimateValue &&
|
|
103
|
-
!isHandoff &&
|
|
104
|
-
supportsBrowserAnimation(resolvedOptions);
|
|
105
|
-
const element = resolvedOptions.motionValue?.owner?.current;
|
|
113
|
+
const useWaapi = canAnimateValue && !isHandoff && supportsBrowserAnimation(options);
|
|
106
114
|
let animation;
|
|
107
115
|
if (useWaapi) {
|
|
116
|
+
/**
|
|
117
|
+
* The resolver needed the VisualElement, WAAPI needs the DOM
|
|
118
|
+
* element. JSAnimation reads neither, so this is safe to
|
|
119
|
+
* leave in place if we fall back to it.
|
|
120
|
+
*/
|
|
121
|
+
options.element = options.motionValue?.owner?.current;
|
|
108
122
|
try {
|
|
109
|
-
animation = new NativeAnimationExtended(
|
|
110
|
-
...resolvedOptions,
|
|
111
|
-
element,
|
|
112
|
-
});
|
|
123
|
+
animation = new NativeAnimationExtended(options);
|
|
113
124
|
}
|
|
114
125
|
catch {
|
|
115
|
-
animation = new JSAnimation(
|
|
126
|
+
animation = new JSAnimation(options);
|
|
116
127
|
}
|
|
117
128
|
}
|
|
118
129
|
else {
|
|
119
|
-
animation = new JSAnimation(
|
|
130
|
+
animation = new JSAnimation(options);
|
|
120
131
|
}
|
|
121
|
-
animation.finished.then(() => {
|
|
122
|
-
this.notifyFinished();
|
|
123
|
-
}).catch(noop);
|
|
124
132
|
if (this.pendingTimeline) {
|
|
125
133
|
this.stopTimeline = animation.attachTimeline(this.pendingTimeline);
|
|
126
134
|
this.pendingTimeline = undefined;
|
|
@@ -128,12 +136,7 @@ class AsyncMotionValueAnimation extends WithPromise {
|
|
|
128
136
|
this._animation = animation;
|
|
129
137
|
}
|
|
130
138
|
get finished() {
|
|
131
|
-
|
|
132
|
-
return this._finished;
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
return this.animation.finished;
|
|
136
|
-
}
|
|
139
|
+
return this._animation ? this._animation.finished : super.finished;
|
|
137
140
|
}
|
|
138
141
|
then(onResolve, _onReject) {
|
|
139
142
|
return this.finished.finally(onResolve).then(() => { });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AsyncMotionValueAnimation.mjs","sources":["../../../src/animation/AsyncMotionValueAnimation.ts"],"sourcesContent":["import { MotionGlobalConfig, noop } 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 { WithPromise } from \"./utils/WithPromise\"\nimport { supportsBrowserAnimation } from \"./waapi/supports/waapi\"\n\n/**\n * Maximum time allowed between an animation being created and it being\n * resolved for us to use the latter as the start time.\n *\n * This is to ensure that while we prefer to \"start\" an animation as soon\n * as it's triggered, we also want to avoid a visual jump if there's a big delay\n * between these two moments.\n */\nconst MAX_RESOLVE_DELAY = 40\n\ntype OptionsWithoutKeyframes<T extends AnyResolvedKeyframe> = Omit<\n ValueAnimationOptions<T>,\n \"keyframes\"\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({\n autoplay = true,\n delay = 0,\n type = \"keyframes\",\n repeat = 0,\n repeatDelay = 0,\n repeatType = \"loop\",\n keyframes,\n name,\n motionValue,\n element,\n ...options\n }: ValueAnimationOptions<T>) {\n super()\n\n this.createdAt = time.now()\n\n const optionsWithDefaults: OptionsWithoutKeyframes<T> = {\n autoplay,\n delay,\n type,\n repeat,\n repeatDelay,\n repeatType,\n name,\n motionValue,\n element,\n ...options,\n }\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: OptionsWithoutKeyframes<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 /**\n * Resolve startTime for the animation.\n *\n * This method uses the createdAt and resolvedAt to calculate the\n * animation startTime. *Ideally*, we would use the createdAt time as t=0\n * as the following frame would then be the first frame of the animation in\n * progress, which would feel snappier.\n *\n * However, if there's a delay (main thread work) between the creation of\n * the animation and the first committed frame, we prefer to use resolvedAt\n * to avoid a sudden jump into the animation.\n */\n const startTime = sync\n ? !this.resolvedAt\n ? this.createdAt\n : this.resolvedAt - this.createdAt > MAX_RESOLVE_DELAY\n ? this.resolvedAt\n : this.createdAt\n : undefined\n\n const resolvedOptions = {\n startTime,\n finalKeyframe,\n ...options,\n keyframes,\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 &&\n !isHandoff &&\n supportsBrowserAnimation(resolvedOptions)\n const element = resolvedOptions.motionValue?.owner?.current\n\n let animation: AnimationPlaybackControls\n if (useWaapi) {\n try {\n animation = new NativeAnimationExtended({\n ...resolvedOptions,\n element,\n } as any)\n } catch {\n animation = new JSAnimation(resolvedOptions)\n }\n } else {\n animation = new JSAnimation(resolvedOptions)\n }\n\n animation.finished.then(() => {\n this.notifyFinished()\n }).catch(noop)\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 if (!this._animation) {\n return this._finished\n } else {\n return this.animation.finished\n }\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":";;;;;;;;;;;AAqBA;;;;;;;AAOG;AACH,MAAM,iBAAiB,GAAG,EAAE;AAOtB,MAAO,yBACT,SAAQ,WAAW,CAAA;AAenB,IAAA,WAAA,CAAY,EACR,QAAQ,GAAG,IAAI,EACf,KAAK,GAAG,CAAC,EACT,IAAI,GAAG,WAAW,EAClB,MAAM,GAAG,CAAC,EACV,WAAW,GAAG,CAAC,EACf,UAAU,GAAG,MAAM,EACnB,SAAS,EACT,IAAI,EACJ,WAAW,EACX,OAAO,EACP,GAAG,OAAO,EACa,EAAA;AACvB,QAAA,KAAK,EAAE;AA2NX;;AAEG;QACH,IAAA,CAAA,IAAI,GAAG,MAAK;AACR,YAAA,IAAI,IAAI,CAAC,UAAU,EAAE;AACjB,gBAAA,IAAI,CAAC,UAAU,CA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AEA,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;;;;"}
|
|
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 { WithPromise } from \"./utils/WithPromise\"\nimport { supportsBrowserAnimation } from \"./waapi/supports/waapi\"\n\n/**\n * Maximum time allowed between an animation being created and it being\n * resolved for us to use the latter as the start time.\n *\n * This is to ensure that while we prefer to \"start\" an animation as soon\n * as it's triggered, we also want to avoid a visual jump if there's a big delay\n * between these two moments.\n */\nconst MAX_RESOLVE_DELAY = 40\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 /**\n * Resolve startTime for the animation.\n *\n * This method uses the createdAt and resolvedAt to calculate the\n * animation startTime. *Ideally*, we would use the createdAt time as t=0\n * as the following frame would then be the first frame of the animation in\n * progress, which would feel snappier.\n *\n * However, if there's a delay (main thread work) between the creation of\n * the animation and the first committed frame, we prefer to use resolvedAt\n * to avoid a sudden jump into the animation.\n */\n const startTime = sync\n ? !this.resolvedAt\n ? this.createdAt\n : this.resolvedAt - this.createdAt > MAX_RESOLVE_DELAY\n ? this.resolvedAt\n : this.createdAt\n : undefined\n\n const { onComplete } = options\n /**\n * A startTime passed in options (an optimised appear handoff syncing\n * to its WAAPI animation) takes precedence over the derived one.\n */\n options.startTime ??= startTime\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 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":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S,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;;;;"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { time } from '../frameloop/sync-time.mjs';
|
|
2
|
+
import { keyframes } from './generators/keyframes.mjs';
|
|
3
|
+
import { calcGeneratorVelocity } from './generators/utils/velocity.mjs';
|
|
4
|
+
import { replaceTransitionType } from './utils/replace-transition-type.mjs';
|
|
5
|
+
import { WithPromise } from './utils/WithPromise.mjs';
|
|
6
|
+
import { frame, cancelFrame } from '../frameloop/frame.mjs';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Every running follower ticks from this one frame callback.
|
|
10
|
+
*/
|
|
11
|
+
const active = new Set();
|
|
12
|
+
const tickAll = ({ timestamp }) => {
|
|
13
|
+
active.forEach((animation) => animation.tick(timestamp));
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* A lean animation for values that follow a target (`followValue`,
|
|
17
|
+
* `springValue`): a single numeric generator, retargeted in place as
|
|
18
|
+
* the target moves and ticked from a frame callback shared by every
|
|
19
|
+
* follower. Only implements the controls `MotionValue.animation`
|
|
20
|
+
* exposes. Repeat options are ignored: a follower only ever heads for
|
|
21
|
+
* its latest target.
|
|
22
|
+
*/
|
|
23
|
+
class FollowAnimation extends WithPromise {
|
|
24
|
+
constructor(options) {
|
|
25
|
+
super();
|
|
26
|
+
this.state = "idle";
|
|
27
|
+
this.startTime = 0;
|
|
28
|
+
this.currentTime = 0;
|
|
29
|
+
/**
|
|
30
|
+
* Whether the first tick has fired `onPlay`.
|
|
31
|
+
*/
|
|
32
|
+
this.started = false;
|
|
33
|
+
/**
|
|
34
|
+
* A target set since the last tick, applied at the next one so any
|
|
35
|
+
* number of sets per frame cost one retarget.
|
|
36
|
+
*/
|
|
37
|
+
this.hasNextTarget = false;
|
|
38
|
+
this.nextTarget = 0;
|
|
39
|
+
/**
|
|
40
|
+
* Bound to the instance so `animation.stop` can be passed around.
|
|
41
|
+
*/
|
|
42
|
+
this.stop = () => {
|
|
43
|
+
if (this.state === "idle")
|
|
44
|
+
return;
|
|
45
|
+
this.teardown();
|
|
46
|
+
this.options.onStop?.();
|
|
47
|
+
};
|
|
48
|
+
this.options = options;
|
|
49
|
+
replaceTransitionType(options);
|
|
50
|
+
this.factory = options.type || keyframes;
|
|
51
|
+
this.generator = this.factory(options);
|
|
52
|
+
const { driver } = options;
|
|
53
|
+
if (driver)
|
|
54
|
+
this.driver = driver((timestamp) => this.tick(timestamp));
|
|
55
|
+
this.startTime = this.now();
|
|
56
|
+
this.state = "running";
|
|
57
|
+
if (this.driver) {
|
|
58
|
+
this.driver.start();
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
active.size || frame.update(tickAll, true);
|
|
62
|
+
active.add(this);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Aim at a new target from wherever the animation has reached by the
|
|
67
|
+
* next tick. `velocity` fixes the velocity to steer with, otherwise
|
|
68
|
+
* the generator's is used.
|
|
69
|
+
*/
|
|
70
|
+
setTarget(target, velocity) {
|
|
71
|
+
this.nextTarget = target;
|
|
72
|
+
this.nextVelocity = velocity;
|
|
73
|
+
this.hasNextTarget = true;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Steer towards new keyframes from the current position and velocity.
|
|
77
|
+
* Springs are updated in place; other generators are recreated.
|
|
78
|
+
*/
|
|
79
|
+
retarget(keyframes, velocity) {
|
|
80
|
+
const { options, generator } = this;
|
|
81
|
+
options.keyframes = keyframes;
|
|
82
|
+
options.velocity = velocity;
|
|
83
|
+
this.startTime = this.now();
|
|
84
|
+
this.currentTime = 0;
|
|
85
|
+
if (generator.retarget) {
|
|
86
|
+
generator.retarget(keyframes, velocity);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
this.generator = this.factory(options);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
tick(timestamp) {
|
|
93
|
+
const { options, hasNextTarget: retargeted } = this;
|
|
94
|
+
const { delay = 0, onUpdate, onPlay } = options;
|
|
95
|
+
const elapsed = Math.round(timestamp - this.startTime) - delay;
|
|
96
|
+
const t = (this.currentTime = Math.max(0, elapsed));
|
|
97
|
+
const state = this.generator.next(t);
|
|
98
|
+
// Hold the origin through the delay, even if the generator is already at rest
|
|
99
|
+
const value = elapsed < 0 ? options.keyframes[0] : state.value;
|
|
100
|
+
if (retargeted) {
|
|
101
|
+
/**
|
|
102
|
+
* Steer from where the current trajectory has reached this
|
|
103
|
+
* frame, then render that position as the new origin.
|
|
104
|
+
*/
|
|
105
|
+
this.hasNextTarget = false;
|
|
106
|
+
const { keyframes } = options;
|
|
107
|
+
keyframes[0] = value;
|
|
108
|
+
keyframes[1] = this.nextTarget;
|
|
109
|
+
this.retarget(keyframes, this.nextVelocity ?? this.getGeneratorVelocity());
|
|
110
|
+
}
|
|
111
|
+
if (retargeted || !this.started) {
|
|
112
|
+
this.started = true;
|
|
113
|
+
onPlay?.();
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Callbacks may stop this animation or set a new target, so check
|
|
117
|
+
* the live state rather than what was read at the top of the tick.
|
|
118
|
+
*/
|
|
119
|
+
if (this.state !== "running")
|
|
120
|
+
return;
|
|
121
|
+
onUpdate?.(value);
|
|
122
|
+
if (state.done &&
|
|
123
|
+
elapsed >= 0 &&
|
|
124
|
+
!retargeted &&
|
|
125
|
+
!this.hasNextTarget &&
|
|
126
|
+
this.state === "running") {
|
|
127
|
+
this.notifyFinished();
|
|
128
|
+
this.teardown();
|
|
129
|
+
this.state = "finished";
|
|
130
|
+
options.onComplete?.();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
getGeneratorVelocity() {
|
|
134
|
+
return calcGeneratorVelocity(this.generator, this.currentTime, this.options.velocity);
|
|
135
|
+
}
|
|
136
|
+
now() {
|
|
137
|
+
return this.driver ? this.driver.now() : time.now();
|
|
138
|
+
}
|
|
139
|
+
teardown() {
|
|
140
|
+
this.state = "idle";
|
|
141
|
+
if (this.driver) {
|
|
142
|
+
this.driver.stop();
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
active.delete(this);
|
|
146
|
+
active.size || cancelFrame(tickAll);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export { FollowAnimation };
|
|
152
|
+
//# sourceMappingURL=FollowAnimation.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FollowAnimation.mjs","sources":["../../../src/animation/FollowAnimation.ts"],"sourcesContent":["import { cancelFrame, frame } from \"../frameloop\"\nimport { time } from \"../frameloop/sync-time\"\nimport { FrameData } from \"../frameloop/types\"\nimport { DriverControls } from \"./drivers/types\"\nimport { keyframes as keyframesGenerator } from \"./generators/keyframes\"\nimport { calcGeneratorVelocity } from \"./generators/utils/velocity\"\nimport {\n GeneratorFactory,\n KeyframeGenerator,\n MotionValueAnimation,\n ValueAnimationOptions,\n} from \"./types\"\nimport { replaceTransitionType } from \"./utils/replace-transition-type\"\nimport { WithPromise } from \"./utils/WithPromise\"\n\n/**\n * Every running follower ticks from this one frame callback.\n */\nconst active = new Set<FollowAnimation>()\n\nconst tickAll = ({ timestamp }: FrameData) => {\n active.forEach((animation) => animation.tick(timestamp))\n}\n\n/**\n * A lean animation for values that follow a target (`followValue`,\n * `springValue`): a single numeric generator, retargeted in place as\n * the target moves and ticked from a frame callback shared by every\n * follower. Only implements the controls `MotionValue.animation`\n * exposes. Repeat options are ignored: a follower only ever heads for\n * its latest target.\n */\nexport class FollowAnimation\n extends WithPromise\n implements MotionValueAnimation\n{\n state: AnimationPlayState = \"idle\"\n\n private generator: KeyframeGenerator<number>\n\n private factory: GeneratorFactory\n\n private options: ValueAnimationOptions<number>\n\n private startTime = 0\n\n private currentTime = 0\n\n /**\n * Whether the first tick has fired `onPlay`.\n */\n private started = false\n\n /**\n * A target set since the last tick, applied at the next one so any\n * number of sets per frame cost one retarget.\n */\n private hasNextTarget = false\n\n private nextTarget = 0\n\n private nextVelocity?: number\n\n /**\n * Only created when a custom driver is passed, otherwise the\n * follower ticks from the shared frame callback.\n */\n private driver?: DriverControls\n\n constructor(options: ValueAnimationOptions<number>) {\n super()\n\n this.options = options\n replaceTransitionType(options)\n this.factory = (options.type as GeneratorFactory) || keyframesGenerator\n this.generator = this.factory(options)\n\n const { driver } = options\n if (driver) this.driver = driver((timestamp) => this.tick(timestamp))\n\n this.startTime = this.now()\n this.state = \"running\"\n\n if (this.driver) {\n this.driver.start()\n } else {\n active.size || frame.update(tickAll, true)\n active.add(this)\n }\n }\n\n /**\n * Aim at a new target from wherever the animation has reached by the\n * next tick. `velocity` fixes the velocity to steer with, otherwise\n * the generator's is used.\n */\n setTarget(target: number, velocity?: number) {\n this.nextTarget = target\n this.nextVelocity = velocity\n this.hasNextTarget = true\n }\n\n /**\n * Steer towards new keyframes from the current position and velocity.\n * Springs are updated in place; other generators are recreated.\n */\n retarget(keyframes: number[], velocity: number) {\n const { options, generator } = this\n options.keyframes = keyframes\n options.velocity = velocity\n this.startTime = this.now()\n this.currentTime = 0\n\n if (generator.retarget) {\n generator.retarget(keyframes, velocity)\n } else {\n this.generator = this.factory(options)\n }\n }\n\n tick(timestamp: number) {\n const { options, hasNextTarget: retargeted } = this\n const { delay = 0, onUpdate, onPlay } = options\n const elapsed = Math.round(timestamp - this.startTime) - delay\n const t = (this.currentTime = Math.max(0, elapsed))\n const state = this.generator.next(t)\n // Hold the origin through the delay, even if the generator is already at rest\n const value = elapsed < 0 ? options.keyframes[0] : state.value\n\n if (retargeted) {\n /**\n * Steer from where the current trajectory has reached this\n * frame, then render that position as the new origin.\n */\n this.hasNextTarget = false\n const { keyframes } = options\n keyframes[0] = value\n keyframes[1] = this.nextTarget\n this.retarget(\n keyframes,\n this.nextVelocity ?? this.getGeneratorVelocity()\n )\n }\n\n if (retargeted || !this.started) {\n this.started = true\n onPlay?.()\n }\n\n /**\n * Callbacks may stop this animation or set a new target, so check\n * the live state rather than what was read at the top of the tick.\n */\n if (this.state !== \"running\") return\n\n onUpdate?.(value)\n\n if (\n state.done &&\n elapsed >= 0 &&\n !retargeted &&\n !this.hasNextTarget &&\n this.state === \"running\"\n ) {\n this.notifyFinished()\n this.teardown()\n this.state = \"finished\"\n options.onComplete?.()\n }\n }\n\n getGeneratorVelocity(): number {\n return calcGeneratorVelocity(\n this.generator,\n this.currentTime,\n this.options.velocity\n )\n }\n\n /**\n * Bound to the instance so `animation.stop` can be passed around.\n */\n stop = () => {\n if (this.state === \"idle\") return\n this.teardown()\n this.options.onStop?.()\n }\n\n private now() {\n return this.driver ? this.driver.now() : time.now()\n }\n\n private teardown() {\n this.state = \"idle\"\n\n if (this.driver) {\n this.driver.stop()\n } else {\n active.delete(this)\n active.size || cancelFrame(tickAll)\n }\n }\n}\n"],"names":["keyframesGenerator"],"mappings":";;;;;;;AAeA;;AAEG;AACH,MAAM,MAAM,GAAG,IAAI,GAAG,EAAmB;AAEzC,MAAM,OAAO,GAAG,CAAC,EAAE,SAAS,EAAa,KAAI;AACzC,IAAA,MAAM,CAAC,OAAO,CAAC,CAAC,SAAS,KAAK,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;AAC5D,CAAC;AAED;;;;;;;AAOG;AACG,MAAO,eACT,SAAQ,WAAW,CAAA;AAoCnB,IAAA,WAAA,CAAY,OAAsC,EAAA;AAC9C,QAAA,KAAK,EAAE;QAlCX,IAAA,CAAA,KAAK,GAAuB,MAAM;QAQ1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E;AACb,YAAA,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE;QACtB;aAAO;AACH,YAAA,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC;AACnB,YAAA,MAAM,CAAC,IAAI,IAAI,WAAW,CAAC,OAAO,CAAC;QACvC;IACJ;AACH;;;;"}
|
|
@@ -5,9 +5,10 @@ import { frameloopDriver } from './drivers/frame.mjs';
|
|
|
5
5
|
import { inertia } from './generators/inertia.mjs';
|
|
6
6
|
import { keyframes } from './generators/keyframes.mjs';
|
|
7
7
|
import { calcGeneratorDuration } from './generators/utils/calc-duration.mjs';
|
|
8
|
-
import {
|
|
8
|
+
import { calcGeneratorVelocity } from './generators/utils/velocity.mjs';
|
|
9
9
|
import { getFinalKeyframe } from './keyframes/get-final.mjs';
|
|
10
10
|
import { replaceTransitionType } from './utils/replace-transition-type.mjs';
|
|
11
|
+
import { notifyAnimationStart } from './utils/notify-inspector.mjs';
|
|
11
12
|
import { WithPromise } from './utils/WithPromise.mjs';
|
|
12
13
|
|
|
13
14
|
const percentToProgress = (percent) => percent / 100;
|
|
@@ -57,6 +58,7 @@ class JSAnimation extends WithPromise {
|
|
|
57
58
|
this.play();
|
|
58
59
|
if (options.autoplay === false)
|
|
59
60
|
this.pause();
|
|
61
|
+
notifyAnimationStart(this, this.options);
|
|
60
62
|
}
|
|
61
63
|
initAnimation() {
|
|
62
64
|
const { options } = this;
|
|
@@ -73,7 +75,9 @@ class JSAnimation extends WithPromise {
|
|
|
73
75
|
this.mixKeyframes = pipe(percentToProgress, mix(keyframes$1[0], keyframes$1[1]));
|
|
74
76
|
keyframes$1 = [0, 100];
|
|
75
77
|
}
|
|
76
|
-
const generator = generatorFactory(
|
|
78
|
+
const generator = generatorFactory(keyframes$1 === options.keyframes
|
|
79
|
+
? options
|
|
80
|
+
: { ...options, keyframes: keyframes$1 });
|
|
77
81
|
/**
|
|
78
82
|
* If we have a mirror repeat type we need to create a second generator that outputs the
|
|
79
83
|
* mirrored (not reversed) animation and later ping pong between the two generators.
|
|
@@ -275,15 +279,7 @@ class JSAnimation extends WithPromise {
|
|
|
275
279
|
* the MotionValue's frame-dependent velocity estimation.
|
|
276
280
|
*/
|
|
277
281
|
getGeneratorVelocity() {
|
|
278
|
-
|
|
279
|
-
if (t <= 0)
|
|
280
|
-
return this.options.velocity || 0;
|
|
281
|
-
if (this.generator.velocity) {
|
|
282
|
-
return this.generator.velocity(t);
|
|
283
|
-
}
|
|
284
|
-
// Fallback: finite difference
|
|
285
|
-
const current = this.generator.next(t).value;
|
|
286
|
-
return getGeneratorVelocity((s) => this.generator.next(s).value, t, current);
|
|
282
|
+
return calcGeneratorVelocity(this.generator, this.currentTime, this.options.velocity);
|
|
287
283
|
}
|
|
288
284
|
get speed() {
|
|
289
285
|
return this.playbackSpeed;
|
|
@@ -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 { getGeneratorVelocity } 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 { 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 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\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({ ...options, keyframes })\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 /**\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 const t = this.currentTime\n if (t <= 0) return this.options.velocity || 0\n\n if (this.generator.velocity) {\n return this.generator.velocity(t)\n }\n\n // Fallback: finite difference\n const current = this.generator.next(t).value as number\n return getGeneratorVelocity(\n (s) => this.generator.next(s).value as number,\n t,\n current\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\n } else if (!this.startTime) {\n this.startTime = startTime ?? 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":";;;;;;;;;;;;AA4BA,MAAM,iBAAiB,GAAG,CAAC,OAAe,KAAK,OAAO,GAAG,GAAG;AAEtD,MAAO,WACT,SAAQ,WAAW,CAAA;AA2DnB,IAAA,WAAA,CAAY,OAAiC,EAAA;AACzC,QAAA,KAAK,EAAE;QAzDX,IAAA,CAAA,KAAK,GAAuB,MAAM;QAElC,IAAA,CAAA,SAAS,GAAkB,IAAI;QAQvB,IAAA,CAAA,SAAS,GAAG,KAAK;AAYzB;;AAEG;QACK,IAAA,CAAA,WAAW,GAAW,CAAC;AAE/B;;AAEG;QACK,IAAA,CAAA,QAAQ,GAAkB,IAAI;AAEtC;;AAEG;QACK,IAAA,CAAA,aAAa,GAAG,CAAC;AAYzB;;;AAGG;AACK,QAAA,IAAA,CAAA,UAAU,GAAsB;AACpC,YAAA,IAAI,EAAE,KAAK;AACX,YAAA,KAAK,EAAE,SAAyB;SACnC;AAyYD;;;AAGG;QACH,IAAA,CAAA,IAAI,GAAG,MAAK;AACR,YAAA,MAAM,EAAE,WAAW,EAAE,GAAG,IAAI,CAAC,OAAO;YACpC,IAAI,WAAW,IAAI,WAAW,CAAC,SAAS,KAAK,IAAI,CAAC,GAAG,EAAE,EAAE;gBACrD,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;YACzB;AAEA,YAAA,IAAI,CAAC,SAAS,GAAG,IAAI;AACrB,YAAA,IAAI,IAAI,CAAC,KAAK,KAAK,MAAM;gBAAE;YAC3B,IAAI,CAAC,QAAQ,EAAE;AACf,YAAA,IAAI,C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AC;QACZ,IAAI,CAAC,QAAQ,EAAE;AACf,QAAA,IAAI,CAAC,OAAO,CAAC,QAAQ,IAAI;IAC7B;IAEQ,QAAQ,GAAA;AACZ,QAAA,IAAI,CAAC,KAAK,GAAG,MAAM;QACnB,IAAI,CAAC,UAAU,EAAE;QACjB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,QAAQ,GAAG,IAAI;IACzC;IAEQ,UAAU,GAAA;QACd,IAAI,CAAC,IAAI,CAAC,MAAM;YAAE;AAClB,QAAA,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE;AAClB,QAAA,IAAI,CAAC,MAAM,GAAG,SAAS;IAC3B;AAEA,IAAA,MAAM,CAAC,UAAkB,EAAA;AACrB,QAAA,IAAI,CAAC,SAAS,GAAG,CAAC;QAClB,OAAO,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC;IACtC;AAEA,IAAA,cAAc,CAAC,QAA8B,EAAA;AACzC,QAAA,IAAI,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE;AAC3B,YAAA,IAAI,CAAC,OAAO,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 { 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 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 /**\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\n } else if (!this.startTime) {\n this.startTime = startTime ?? 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":";;;;;;;;;;;;;AA6BA,MAAM,iBAAiB,GAAG,CAAC,OAAe,KAAK,OAAO,GAAG,GAAG;AAEtD,MAAO,WACT,SAAQ,WAAW,CAAA;AA2DnB,IAAA,WAAA,CAAY,OAAiC,EAAA;AACzC,QAAA,KAAK,EAAE;QAzDX,IAAA,CAAA,KAAK,GAAuB,MAAM;QAElC,IAAA,CAAA,SAAS,GAAkB,IAAI;QAQvB,IAAA,CAAA,SAAS,GAAG,KAAK;AAYzB;;AAEG;QACK,IAAA,CAAA,WAAW,GAAW,CAAC;AAE/B;;AAEG;QACK,IAAA,CAAA,QAAQ,GAAkB,IAAI;AAEtC;;AAEG;QACK,IAAA,CAAA,aAAa,GAAG,CAAC;AAYzB;;;AAGG;AACK,QAAA,IAAA,CAAA,UAAU,G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6 +3,7 @@ import { setStyle } from '../render/dom/style-set.mjs';
|
|
|
3
3
|
import { supportsScrollTimeline } from '../utils/supports/scroll-timeline.mjs';
|
|
4
4
|
import { getFinalKeyframe } from './keyframes/get-final.mjs';
|
|
5
5
|
import { WithPromise } from './utils/WithPromise.mjs';
|
|
6
|
+
import { notifyAnimationStart } from './utils/notify-inspector.mjs';
|
|
6
7
|
import { startWaapiAnimation } from './waapi/start-waapi-animation.mjs';
|
|
7
8
|
import { applyGeneratorOptions } from './waapi/utils/apply-generator.mjs';
|
|
8
9
|
|
|
@@ -52,6 +53,7 @@ class NativeAnimation extends WithPromise {
|
|
|
52
53
|
onComplete?.();
|
|
53
54
|
this.notifyFinished();
|
|
54
55
|
};
|
|
56
|
+
notifyAnimationStart(this, options, transition);
|
|
55
57
|
}
|
|
56
58
|
play() {
|
|
57
59
|
if (this.isStopped)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NativeAnimation.mjs","sources":["../../../src/animation/NativeAnimation.ts"],"sourcesContent":["import {\n invariant,\n millisecondsToSeconds,\n noop,\n secondsToMilliseconds,\n} from \"motion-utils\"\nimport { setStyle } from \"../render/dom/style-set\"\nimport { supportsScrollTimeline } from \"../utils/supports/scroll-timeline\"\nimport { getFinalKeyframe } from \"./keyframes/get-final\"\nimport {\n AnimationPlaybackControlsWithThen,\n AnyResolvedKeyframe,\n DOMValueAnimationOptions,\n TimelineWithFallback,\n} from \"./types\"\nimport { WithPromise } from \"./utils/WithPromise\"\nimport { startWaapiAnimation } from \"./waapi/start-waapi-animation\"\nimport { applyGeneratorOptions } from \"./waapi/utils/apply-generator\"\n\nexport interface NativeAnimationOptions<V extends AnyResolvedKeyframe = number>\n extends DOMValueAnimationOptions<V> {\n pseudoElement?: string\n startTime?: number\n}\n\n/**\n * NativeAnimation implements AnimationPlaybackControls for the browser's Web Animations API.\n */\nexport class NativeAnimation<T extends AnyResolvedKeyframe>\n extends WithPromise\n implements AnimationPlaybackControlsWithThen\n{\n /**\n * The interfaced Web Animation API animation\n */\n protected animation: Animation\n\n protected finishedTime: number | null = null\n\n protected options: NativeAnimationOptions\n\n private allowFlatten: boolean\n\n private isStopped = false\n\n private isPseudoElement: boolean\n\n /**\n * Tracks a manually-set start time that takes precedence over WAAPI's\n * dynamic startTime. This is cleared when play() or time setter is called,\n * allowing WAAPI to take over timing.\n */\n protected manualStartTime: number | null = null\n\n constructor(options?: NativeAnimationOptions) {\n super()\n\n if (!options) return\n\n const {\n element,\n name,\n keyframes,\n pseudoElement,\n allowFlatten = false,\n finalKeyframe,\n onComplete,\n } = options as any\n\n this.isPseudoElement = Boolean(pseudoElement)\n\n this.allowFlatten = allowFlatten\n this.options = options\n\n invariant(\n typeof options.type !== \"string\",\n `Mini animate() doesn't support \"type\" as a string.`,\n \"mini-spring\"\n )\n\n const transition = applyGeneratorOptions(options)\n\n this.animation = startWaapiAnimation(\n element,\n name,\n keyframes,\n transition,\n pseudoElement\n )\n\n if (transition.autoplay === false) {\n this.animation.pause()\n }\n\n this.animation.onfinish = () => {\n this.finishedTime = this.time\n\n if (!pseudoElement) {\n const keyframe = getFinalKeyframe(\n keyframes as any,\n this.options as any,\n finalKeyframe,\n this.speed\n )\n if (this.updateMotionValue) {\n this.updateMotionValue(keyframe)\n }\n\n /**\n * If we can, we want to commit the final style as set by the user,\n * rather than the computed keyframe value supplied by the animation.\n * We always do this, even when a motion value is present, to prevent\n * a visual flash in Firefox where the WAAPI animation's fill is removed\n * during cancel() before the scheduled render can apply the correct value.\n */\n setStyle(element, name, keyframe)\n\n this.animation.cancel()\n }\n\n onComplete?.()\n this.notifyFinished()\n }\n }\n\n updateMotionValue?(value?: T): void\n\n play() {\n if (this.isStopped) return\n\n this.manualStartTime = null\n this.animation.play()\n\n if (this.state === \"finished\") {\n this.updateFinished()\n }\n }\n\n pause() {\n this.animation.pause()\n }\n\n complete() {\n this.animation.finish?.()\n }\n\n cancel() {\n try {\n this.animation.cancel()\n } catch (e) {}\n }\n\n stop() {\n if (this.isStopped) return\n this.isStopped = true\n const { state } = this\n\n if (state === \"idle\" || state === \"finished\") {\n return\n }\n\n if (this.updateMotionValue) {\n this.updateMotionValue()\n } else {\n this.commitStyles()\n }\n\n if (!this.isPseudoElement) this.cancel()\n }\n\n /**\n * WAAPI doesn't natively have any interruption capabilities.\n *\n * In this method, we commit styles back to the DOM before cancelling\n * the animation.\n *\n * This is designed to be overridden by NativeAnimationExtended, which\n * will create a renderless JS animation and sample it twice to calculate\n * its current value, \"previous\" value, and therefore allow\n * Motion to also correctly calculate velocity for any subsequent animation\n * while deferring the commit until the next animation frame.\n */\n protected commitStyles() {\n const element = this.options?.element\n if (!this.isPseudoElement && element?.isConnected) {\n this.animation.commitStyles?.()\n }\n }\n\n get duration() {\n const duration =\n this.animation.effect?.getComputedTiming?.().duration || 0\n\n return millisecondsToSeconds(Number(duration))\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(Number(this.animation.currentTime) || 0)\n }\n\n set time(newTime: number) {\n const wasFinished = this.finishedTime !== null\n this.manualStartTime = null\n this.finishedTime = null\n this.animation.currentTime = secondsToMilliseconds(newTime)\n\n if (wasFinished) {\n this.animation.pause()\n }\n }\n\n /**\n * The playback speed of the animation.\n * 1 = normal speed, 2 = double speed, 0.5 = half speed.\n */\n get speed() {\n return this.animation.playbackRate\n }\n\n set speed(newSpeed: number) {\n // Allow backwards playback after finishing\n if (newSpeed < 0) this.finishedTime = null\n\n this.animation.playbackRate = newSpeed\n }\n\n get state() {\n return this.finishedTime !== null\n ? \"finished\"\n : this.animation.playState\n }\n\n get startTime() {\n return this.manualStartTime ?? Number(this.animation.startTime)\n }\n\n set startTime(newStartTime: number) {\n this.manualStartTime = this.animation.startTime = newStartTime\n }\n\n /**\n * Attaches a timeline to the animation, for instance the `ScrollTimeline`.\n */\n attachTimeline({\n timeline,\n rangeStart,\n rangeEnd,\n observe,\n }: TimelineWithFallback): VoidFunction {\n if (this.allowFlatten) {\n this.animation.effect?.updateTiming({ easing: \"linear\" })\n }\n\n this.animation.onfinish = null\n\n if (timeline && supportsScrollTimeline()) {\n this.animation.timeline = timeline as any\n\n if (rangeStart) (this.animation as any).rangeStart = rangeStart\n if (rangeEnd) (this.animation as any).rangeEnd = rangeEnd\n\n return noop<void>\n } else {\n return observe(this)\n }\n }\n}\n"],"names":[],"mappings":";;;;;;;;AAyBA;;AAEG;AACG,MAAO,eACT,SAAQ,WAAW,CAAA;AAyBnB,IAAA,WAAA,CAAY,OAAgC,EAAA;AACxC,QAAA,KAAK,EAAE;QAlBD,IAAA,CAAA,YAAY,GAAkB,IAAI;QAMpC,IAAA,CAAA,SAAS,GAAG,KAAK;AAIzB;;;;AAIG;QACO,IAAA,CAAA,eAAe,GAAkB,IAAI;AAK3C,QAAA,IAAI,CAAC,OAAO;YAAE;AAEd,QAAA,MAAM,EACF,OAAO,EACP,IAAI,EACJ,SAAS,EACT,aAAa,EACb,YAAY,GAAG,KAAK,EACpB,aAAa,EACb,UAAU,GACb,GAAG,OAAc;AAElB,QAAA,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,aAAa,CAAC;AAE7C,QAAA,IAAI,CAAC,YAAY,GAAG,YAAY;AAChC,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO;AAEtB,QAAA,SAAS,CACL,OAAO,OAAO,CAAC,IAAI,KAAK,QAAQ,EAChC,CAAA,kDAAA,CAAoD,EACpD,aAAa,CAChB;AAED,QAAA,MAAM,UAAU,GAAG,qBAAqB,CAAC,OAAO,CAAC;AAEjD,QAAA,IAAI,CAAC,SAAS,GAAG,mBAAmB,CAChC,OAAO,EACP,IAAI,EACJ,SAAS,EACT,UAAU,EACV,aAAa,CAChB;AAED,QAAA,IAAI,UAAU,CAAC,QAAQ,KAAK,KAAK,EAAE;AAC/B,YAAA,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE;QAC1B;AAEA,QAAA,IAAI,CAAC,SAA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AC/D,YAAA,IAAI,QAAQ;AAAG,gBAAA,IAAI,CAAC,SAAiB,CAAC,QAAQ,GAAG,QAAQ;AAEzD,YAAA,OAAO,IAAU;QACrB;aAAO;AACH,YAAA,OAAO,OAAO,CAAC,IAAI,CAAC;QACxB;IACJ;AACH;;;;"}
|
|
1
|
+
{"version":3,"file":"NativeAnimation.mjs","sources":["../../../src/animation/NativeAnimation.ts"],"sourcesContent":["import {\n invariant,\n millisecondsToSeconds,\n noop,\n secondsToMilliseconds,\n} from \"motion-utils\"\nimport { setStyle } from \"../render/dom/style-set\"\nimport { supportsScrollTimeline } from \"../utils/supports/scroll-timeline\"\nimport { getFinalKeyframe } from \"./keyframes/get-final\"\nimport {\n AnimationPlaybackControlsWithThen,\n AnyResolvedKeyframe,\n DOMValueAnimationOptions,\n TimelineWithFallback,\n} from \"./types\"\nimport { WithPromise } from \"./utils/WithPromise\"\nimport { notifyAnimationStart } from \"./utils/notify-inspector\"\nimport { startWaapiAnimation } from \"./waapi/start-waapi-animation\"\nimport { applyGeneratorOptions } from \"./waapi/utils/apply-generator\"\n\nexport interface NativeAnimationOptions<V extends AnyResolvedKeyframe = number>\n extends DOMValueAnimationOptions<V> {\n pseudoElement?: string\n startTime?: number\n}\n\n/**\n * NativeAnimation implements AnimationPlaybackControls for the browser's Web Animations API.\n */\nexport class NativeAnimation<T extends AnyResolvedKeyframe>\n extends WithPromise\n implements AnimationPlaybackControlsWithThen\n{\n /**\n * The interfaced Web Animation API animation\n */\n protected animation: Animation\n\n protected finishedTime: number | null = null\n\n protected options: NativeAnimationOptions\n\n private allowFlatten: boolean\n\n private isStopped = false\n\n private isPseudoElement: boolean\n\n /**\n * Tracks a manually-set start time that takes precedence over WAAPI's\n * dynamic startTime. This is cleared when play() or time setter is called,\n * allowing WAAPI to take over timing.\n */\n protected manualStartTime: number | null = null\n\n constructor(options?: NativeAnimationOptions) {\n super()\n\n if (!options) return\n\n const {\n element,\n name,\n keyframes,\n pseudoElement,\n allowFlatten = false,\n finalKeyframe,\n onComplete,\n } = options as any\n\n this.isPseudoElement = Boolean(pseudoElement)\n\n this.allowFlatten = allowFlatten\n this.options = options\n\n invariant(\n typeof options.type !== \"string\",\n `Mini animate() doesn't support \"type\" as a string.`,\n \"mini-spring\"\n )\n\n const transition = applyGeneratorOptions(options)\n\n this.animation = startWaapiAnimation(\n element,\n name,\n keyframes,\n transition,\n pseudoElement\n )\n\n if (transition.autoplay === false) {\n this.animation.pause()\n }\n\n this.animation.onfinish = () => {\n this.finishedTime = this.time\n\n if (!pseudoElement) {\n const keyframe = getFinalKeyframe(\n keyframes as any,\n this.options as any,\n finalKeyframe,\n this.speed\n )\n if (this.updateMotionValue) {\n this.updateMotionValue(keyframe)\n }\n\n /**\n * If we can, we want to commit the final style as set by the user,\n * rather than the computed keyframe value supplied by the animation.\n * We always do this, even when a motion value is present, to prevent\n * a visual flash in Firefox where the WAAPI animation's fill is removed\n * during cancel() before the scheduled render can apply the correct value.\n */\n setStyle(element, name, keyframe)\n\n this.animation.cancel()\n }\n\n onComplete?.()\n this.notifyFinished()\n }\n\n notifyAnimationStart(this, options, transition)\n }\n\n updateMotionValue?(value?: T): void\n\n play() {\n if (this.isStopped) return\n\n this.manualStartTime = null\n this.animation.play()\n\n if (this.state === \"finished\") {\n this.updateFinished()\n }\n }\n\n pause() {\n this.animation.pause()\n }\n\n complete() {\n this.animation.finish?.()\n }\n\n cancel() {\n try {\n this.animation.cancel()\n } catch (e) {}\n }\n\n stop() {\n if (this.isStopped) return\n this.isStopped = true\n const { state } = this\n\n if (state === \"idle\" || state === \"finished\") {\n return\n }\n\n if (this.updateMotionValue) {\n this.updateMotionValue()\n } else {\n this.commitStyles()\n }\n\n if (!this.isPseudoElement) this.cancel()\n }\n\n /**\n * WAAPI doesn't natively have any interruption capabilities.\n *\n * In this method, we commit styles back to the DOM before cancelling\n * the animation.\n *\n * This is designed to be overridden by NativeAnimationExtended, which\n * will create a renderless JS animation and sample it twice to calculate\n * its current value, \"previous\" value, and therefore allow\n * Motion to also correctly calculate velocity for any subsequent animation\n * while deferring the commit until the next animation frame.\n */\n protected commitStyles() {\n const element = this.options?.element\n if (!this.isPseudoElement && element?.isConnected) {\n this.animation.commitStyles?.()\n }\n }\n\n get duration() {\n const duration =\n this.animation.effect?.getComputedTiming?.().duration || 0\n\n return millisecondsToSeconds(Number(duration))\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(Number(this.animation.currentTime) || 0)\n }\n\n set time(newTime: number) {\n const wasFinished = this.finishedTime !== null\n this.manualStartTime = null\n this.finishedTime = null\n this.animation.currentTime = secondsToMilliseconds(newTime)\n\n if (wasFinished) {\n this.animation.pause()\n }\n }\n\n /**\n * The playback speed of the animation.\n * 1 = normal speed, 2 = double speed, 0.5 = half speed.\n */\n get speed() {\n return this.animation.playbackRate\n }\n\n set speed(newSpeed: number) {\n // Allow backwards playback after finishing\n if (newSpeed < 0) this.finishedTime = null\n\n this.animation.playbackRate = newSpeed\n }\n\n get state() {\n return this.finishedTime !== null\n ? \"finished\"\n : this.animation.playState\n }\n\n get startTime() {\n return this.manualStartTime ?? Number(this.animation.startTime)\n }\n\n set startTime(newStartTime: number) {\n this.manualStartTime = this.animation.startTime = newStartTime\n }\n\n /**\n * Attaches a timeline to the animation, for instance the `ScrollTimeline`.\n */\n attachTimeline({\n timeline,\n rangeStart,\n rangeEnd,\n observe,\n }: TimelineWithFallback): VoidFunction {\n if (this.allowFlatten) {\n this.animation.effect?.updateTiming({ easing: \"linear\" })\n }\n\n this.animation.onfinish = null\n\n if (timeline && supportsScrollTimeline()) {\n this.animation.timeline = timeline as any\n\n if (rangeStart) (this.animation as any).rangeStart = rangeStart\n if (rangeEnd) (this.animation as any).rangeEnd = rangeEnd\n\n return noop<void>\n } else {\n return observe(this)\n }\n }\n}\n"],"names":[],"mappings":";;;;;;;;;AA0BA;;AAEG;AACG,MAAO,eACT,SAAQ,WA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