motion-dom 12.7.3 → 12.7.5-alpha.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 +2946 -475
- package/dist/es/animation/AsyncMotionValueAnimation.mjs +178 -0
- package/dist/es/animation/GroupAnimation.mjs +6 -15
- package/dist/es/animation/JSAnimation.mjs +346 -0
- package/dist/es/animation/NativeAnimation.mjs +64 -67
- package/dist/es/animation/NativeAnimationExtended.mjs +65 -0
- package/dist/es/animation/NativeAnimationWrapper.mjs +14 -0
- package/dist/es/animation/drivers/driver-frameloop.mjs +17 -0
- package/dist/es/animation/generators/inertia.mjs +87 -0
- package/dist/es/animation/generators/keyframes.mjs +49 -0
- package/dist/es/animation/generators/spring/defaults.mjs +27 -0
- package/dist/es/animation/generators/spring/find.mjs +84 -0
- package/dist/es/animation/generators/spring/index.mjs +175 -0
- package/dist/es/animation/generators/utils/velocity.mjs +9 -0
- package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs +131 -0
- package/dist/es/animation/keyframes/KeyframesResolver.mjs +156 -0
- package/dist/es/animation/keyframes/get-final.mjs +3 -4
- package/dist/es/animation/keyframes/offsets/default.mjs +9 -0
- package/dist/es/animation/keyframes/offsets/fill.mjs +12 -0
- package/dist/es/animation/keyframes/offsets/time.mjs +5 -0
- package/dist/es/animation/keyframes/utils/apply-px-defaults.mjs +11 -0
- package/dist/es/animation/keyframes/utils/fill-wildcards.mjs +7 -0
- package/dist/es/animation/keyframes/utils/is-none.mjs +15 -0
- package/dist/es/animation/keyframes/utils/make-none-animatable.mjs +30 -0
- package/dist/es/animation/keyframes/utils/unit-conversion.mjs +36 -0
- package/dist/es/animation/utils/WithPromise.mjs +28 -0
- package/dist/es/animation/utils/active-animations.mjs +9 -0
- package/dist/es/animation/utils/can-animate.mjs +42 -0
- package/dist/es/animation/utils/css-variables-conversion.mjs +41 -0
- package/dist/es/animation/utils/is-animatable.mjs +30 -0
- package/dist/es/animation/utils/is-css-variable.mjs +15 -0
- package/dist/es/animation/utils/replace-transition-type.mjs +18 -0
- package/dist/es/animation/waapi/easing/is-supported.mjs +1 -1
- package/dist/es/animation/waapi/easing/map-easing.mjs +5 -3
- package/dist/es/animation/waapi/start-waapi-animation.mjs +6 -4
- package/dist/es/animation/waapi/supports/waapi.mjs +39 -0
- package/dist/es/animation/waapi/utils/accelerated-values.mjs +14 -0
- package/dist/es/animation/waapi/utils/apply-generator.mjs +2 -1
- package/dist/es/animation/waapi/utils/unsupported-easing.mjs +18 -0
- package/dist/es/frameloop/batcher.mjs +2 -1
- package/dist/es/frameloop/order.mjs +1 -0
- package/dist/es/index.mjs +49 -2
- package/dist/es/render/dom/is-css-var.mjs +3 -0
- package/dist/es/render/dom/parse-transform.mjs +83 -0
- package/dist/es/render/dom/style-computed.mjs +10 -0
- package/dist/es/render/dom/style-set.mjs +9 -0
- package/dist/es/render/utils/keys-position.mjs +13 -0
- package/dist/es/render/utils/keys-transform.mjs +28 -0
- package/dist/es/scroll/observe.mjs +18 -0
- package/dist/es/stats/index.mjs +2 -0
- package/dist/es/utils/interpolate.mjs +74 -0
- package/dist/es/utils/mix/color.mjs +47 -0
- package/dist/es/utils/mix/complex.mjs +92 -0
- package/dist/es/utils/mix/immediate.mjs +5 -0
- package/dist/es/utils/mix/index.mjs +14 -0
- package/dist/es/utils/mix/number.mjs +26 -0
- package/dist/es/utils/mix/visibility.mjs +16 -0
- package/dist/es/value/index.mjs +3 -1
- package/dist/es/value/types/auto.mjs +9 -0
- package/dist/es/value/types/color/hex.mjs +40 -0
- package/dist/es/value/types/color/hsla-to-rgba.mjs +42 -0
- package/dist/es/value/types/color/hsla.mjs +22 -0
- package/dist/es/value/types/color/index.mjs +27 -0
- package/dist/es/value/types/color/rgba.mjs +25 -0
- package/dist/es/value/types/color/utils.mjs +29 -0
- package/dist/es/value/types/complex/filter.mjs +30 -0
- package/dist/es/value/types/complex/index.mjs +91 -0
- package/dist/es/value/types/dimensions.mjs +15 -0
- package/dist/es/value/types/int.mjs +8 -0
- package/dist/es/value/types/maps/defaults.mjs +30 -0
- package/dist/es/value/types/maps/number.mjs +51 -0
- package/dist/es/value/types/maps/transform.mjs +31 -0
- package/dist/es/value/types/numbers/index.mjs +17 -0
- package/dist/es/value/types/numbers/units.mjs +18 -0
- package/dist/es/value/types/test.mjs +6 -0
- package/dist/es/value/types/utils/animatable-none.mjs +15 -0
- package/dist/es/value/types/utils/color-regex.mjs +3 -0
- package/dist/es/value/types/utils/find.mjs +15 -0
- package/dist/es/value/types/utils/float-regex.mjs +3 -0
- package/dist/es/value/types/utils/get-as-type.mjs +10 -0
- package/dist/es/value/types/utils/is-nullish.mjs +5 -0
- package/dist/es/value/types/utils/sanitize.mjs +5 -0
- package/dist/es/value/types/utils/single-color-regex.mjs +3 -0
- package/dist/es/view/start.mjs +4 -3
- package/dist/index.d.ts +945 -444
- package/dist/motion-dom.dev.js +2945 -475
- package/dist/motion-dom.js +1 -1
- package/package.json +3 -3
- package/dist/es/animation/keyframes/hydrate.mjs +0 -26
- package/dist/es/animation/waapi/utils/attach-timeline.mjs +0 -6
- package/dist/es/render/dom/style.mjs +0 -15
- package/dist/es/utils/is-bezier-definition.mjs +0 -3
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { MotionGlobalConfig, noop } from 'motion-utils';
|
|
2
|
+
import { time } from '../frameloop/sync-time.mjs';
|
|
3
|
+
import { JSAnimation } from './JSAnimation.mjs';
|
|
4
|
+
import { getFinalKeyframe } from './keyframes/get-final.mjs';
|
|
5
|
+
import { KeyframeResolver, flushKeyframeResolvers } from './keyframes/KeyframesResolver.mjs';
|
|
6
|
+
import { NativeAnimationExtended } from './NativeAnimationExtended.mjs';
|
|
7
|
+
import { canAnimate } from './utils/can-animate.mjs';
|
|
8
|
+
import { WithPromise } from './utils/WithPromise.mjs';
|
|
9
|
+
import { supportsBrowserAnimation } from './waapi/supports/waapi.mjs';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Maximum time allowed between an animation being created and it being
|
|
13
|
+
* resolved for us to use the latter as the start time.
|
|
14
|
+
*
|
|
15
|
+
* This is to ensure that while we prefer to "start" an animation as soon
|
|
16
|
+
* as it's triggered, we also want to avoid a visual jump if there's a big delay
|
|
17
|
+
* between these two moments.
|
|
18
|
+
*/
|
|
19
|
+
const MAX_RESOLVE_DELAY = 40;
|
|
20
|
+
class AsyncMotionValueAnimation extends WithPromise {
|
|
21
|
+
constructor({ autoplay = true, delay = 0, type = "keyframes", repeat = 0, repeatDelay = 0, repeatType = "loop", keyframes, name, motionValue, element, ...options }) {
|
|
22
|
+
super();
|
|
23
|
+
/**
|
|
24
|
+
* Bound to support return animation.stop pattern
|
|
25
|
+
*/
|
|
26
|
+
this.stop = () => {
|
|
27
|
+
if (this._animation) {
|
|
28
|
+
this._animation.stop();
|
|
29
|
+
this.stopTimeline?.();
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
this.keyframeResolver?.cancel();
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
this.createdAt = time.now();
|
|
36
|
+
const optionsWithDefaults = {
|
|
37
|
+
autoplay,
|
|
38
|
+
delay,
|
|
39
|
+
type,
|
|
40
|
+
repeat,
|
|
41
|
+
repeatDelay,
|
|
42
|
+
repeatType,
|
|
43
|
+
name,
|
|
44
|
+
motionValue,
|
|
45
|
+
element,
|
|
46
|
+
...options,
|
|
47
|
+
};
|
|
48
|
+
const KeyframeResolver$1 = element?.KeyframeResolver || KeyframeResolver;
|
|
49
|
+
this.keyframeResolver = new KeyframeResolver$1(keyframes, (resolvedKeyframes, finalKeyframe, forced) => this.onKeyframesResolved(resolvedKeyframes, finalKeyframe, optionsWithDefaults, !forced), name, motionValue, element);
|
|
50
|
+
this.keyframeResolver?.scheduleResolve();
|
|
51
|
+
}
|
|
52
|
+
onKeyframesResolved(keyframes, finalKeyframe, options, sync) {
|
|
53
|
+
this.keyframeResolver = undefined;
|
|
54
|
+
const { name, type, velocity, delay, isHandoff, onUpdate, onComplete } = options;
|
|
55
|
+
this.resolvedAt = time.now();
|
|
56
|
+
/**
|
|
57
|
+
* If we can't animate this value with the resolved keyframes
|
|
58
|
+
* then we should complete it immediately.
|
|
59
|
+
*/
|
|
60
|
+
if (!canAnimate(keyframes, name, type, velocity)) {
|
|
61
|
+
if (MotionGlobalConfig.instantAnimations || !delay) {
|
|
62
|
+
onUpdate?.(getFinalKeyframe(keyframes, options, finalKeyframe));
|
|
63
|
+
}
|
|
64
|
+
keyframes[0] = keyframes[keyframes.length - 1];
|
|
65
|
+
options.duration = 0;
|
|
66
|
+
options.repeat = 0;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Resolve startTime for the animation.
|
|
70
|
+
*
|
|
71
|
+
* This method uses the createdAt and resolvedAt to calculate the
|
|
72
|
+
* animation startTime. *Ideally*, we would use the createdAt time as t=0
|
|
73
|
+
* as the following frame would then be the first frame of the animation in
|
|
74
|
+
* progress, which would feel snappier.
|
|
75
|
+
*
|
|
76
|
+
* However, if there's a delay (main thread work) between the creation of
|
|
77
|
+
* the animation and the first commited frame, we prefer to use resolvedAt
|
|
78
|
+
* to avoid a sudden jump into the animation.
|
|
79
|
+
*/
|
|
80
|
+
const startTime = sync
|
|
81
|
+
? !this.resolvedAt
|
|
82
|
+
? this.createdAt
|
|
83
|
+
: this.resolvedAt - this.createdAt > MAX_RESOLVE_DELAY
|
|
84
|
+
? this.resolvedAt
|
|
85
|
+
: this.createdAt
|
|
86
|
+
: undefined;
|
|
87
|
+
const resolvedOptions = {
|
|
88
|
+
startTime,
|
|
89
|
+
finalKeyframe,
|
|
90
|
+
...options,
|
|
91
|
+
keyframes,
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Animate via WAAPI if possible. If this is a handoff animation, the optimised animation will be running via
|
|
95
|
+
* WAAPI. Therefore, this animation must be JS to ensure it runs "under" the
|
|
96
|
+
* optimised animation.
|
|
97
|
+
*/
|
|
98
|
+
const animation = !isHandoff && supportsBrowserAnimation(resolvedOptions)
|
|
99
|
+
? new NativeAnimationExtended({
|
|
100
|
+
...resolvedOptions,
|
|
101
|
+
element: resolvedOptions.motionValue.owner.current,
|
|
102
|
+
})
|
|
103
|
+
: new JSAnimation(resolvedOptions);
|
|
104
|
+
animation.finished
|
|
105
|
+
.then(() => {
|
|
106
|
+
onComplete?.();
|
|
107
|
+
this.notifyFinished();
|
|
108
|
+
})
|
|
109
|
+
.catch(noop);
|
|
110
|
+
if (this.pendingTimeline) {
|
|
111
|
+
this.stopTimeline = animation.attachTimeline(this.pendingTimeline);
|
|
112
|
+
this.pendingTimeline = undefined;
|
|
113
|
+
}
|
|
114
|
+
this._animation = animation;
|
|
115
|
+
}
|
|
116
|
+
get finished() {
|
|
117
|
+
if (!this._animation) {
|
|
118
|
+
return this._finished;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
return this.animation.finished;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
then(onResolve, _onReject) {
|
|
125
|
+
return this.finished.finally(onResolve).then(() => { });
|
|
126
|
+
}
|
|
127
|
+
get animation() {
|
|
128
|
+
if (!this._animation) {
|
|
129
|
+
flushKeyframeResolvers();
|
|
130
|
+
}
|
|
131
|
+
return this._animation;
|
|
132
|
+
}
|
|
133
|
+
get duration() {
|
|
134
|
+
return this.animation.duration;
|
|
135
|
+
}
|
|
136
|
+
get time() {
|
|
137
|
+
return this.animation.time;
|
|
138
|
+
}
|
|
139
|
+
set time(newTime) {
|
|
140
|
+
this.animation.time = newTime;
|
|
141
|
+
}
|
|
142
|
+
get speed() {
|
|
143
|
+
return this.animation.speed;
|
|
144
|
+
}
|
|
145
|
+
get state() {
|
|
146
|
+
return this.animation.state;
|
|
147
|
+
}
|
|
148
|
+
set speed(newSpeed) {
|
|
149
|
+
this.animation.speed = newSpeed;
|
|
150
|
+
}
|
|
151
|
+
get startTime() {
|
|
152
|
+
return this.animation.startTime;
|
|
153
|
+
}
|
|
154
|
+
attachTimeline(timeline) {
|
|
155
|
+
if (this._animation) {
|
|
156
|
+
this.stopTimeline = this.animation.attachTimeline(timeline);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
this.pendingTimeline = timeline;
|
|
160
|
+
}
|
|
161
|
+
return () => this.stop();
|
|
162
|
+
}
|
|
163
|
+
play() {
|
|
164
|
+
this.animation.play();
|
|
165
|
+
}
|
|
166
|
+
pause() {
|
|
167
|
+
this.animation.pause();
|
|
168
|
+
}
|
|
169
|
+
complete() {
|
|
170
|
+
console.log("async animation complete");
|
|
171
|
+
this.animation.complete();
|
|
172
|
+
}
|
|
173
|
+
cancel() {
|
|
174
|
+
this.animation.cancel();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export { AsyncMotionValueAnimation };
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { supportsScrollTimeline } from '../utils/supports/scroll-timeline.mjs';
|
|
2
|
-
|
|
3
1
|
class GroupAnimation {
|
|
4
2
|
constructor(animations) {
|
|
5
|
-
// Bound to
|
|
3
|
+
// Bound to accomadate common `return animation.stop` pattern
|
|
6
4
|
this.stop = () => this.runAll("stop");
|
|
7
5
|
this.animations = animations.filter(Boolean);
|
|
8
6
|
}
|
|
@@ -20,15 +18,8 @@ class GroupAnimation {
|
|
|
20
18
|
this.animations[i][propName] = newValue;
|
|
21
19
|
}
|
|
22
20
|
}
|
|
23
|
-
attachTimeline(timeline
|
|
24
|
-
const subscriptions = this.animations.map((animation) =>
|
|
25
|
-
if (supportsScrollTimeline() && animation.attachTimeline) {
|
|
26
|
-
return animation.attachTimeline(timeline);
|
|
27
|
-
}
|
|
28
|
-
else if (typeof fallback === "function") {
|
|
29
|
-
return fallback(animation);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
21
|
+
attachTimeline(timeline) {
|
|
22
|
+
const subscriptions = this.animations.map((animation) => animation.attachTimeline(timeline));
|
|
32
23
|
return () => {
|
|
33
24
|
subscriptions.forEach((cancel, i) => {
|
|
34
25
|
cancel && cancel();
|
|
@@ -48,6 +39,9 @@ class GroupAnimation {
|
|
|
48
39
|
set speed(speed) {
|
|
49
40
|
this.setAll("speed", speed);
|
|
50
41
|
}
|
|
42
|
+
get state() {
|
|
43
|
+
return this.getAll("state");
|
|
44
|
+
}
|
|
51
45
|
get startTime() {
|
|
52
46
|
return this.getAll("startTime");
|
|
53
47
|
}
|
|
@@ -61,9 +55,6 @@ class GroupAnimation {
|
|
|
61
55
|
runAll(methodName) {
|
|
62
56
|
this.animations.forEach((controls) => controls[methodName]());
|
|
63
57
|
}
|
|
64
|
-
flatten() {
|
|
65
|
-
this.runAll("flatten");
|
|
66
|
-
}
|
|
67
58
|
play() {
|
|
68
59
|
this.runAll("play");
|
|
69
60
|
}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { invariant, pipe, clamp, millisecondsToSeconds, secondsToMilliseconds } from 'motion-utils';
|
|
2
|
+
import { time } from '../frameloop/sync-time.mjs';
|
|
3
|
+
import { activeAnimations } from '../stats/animation-count.mjs';
|
|
4
|
+
import { mix } from '../utils/mix/index.mjs';
|
|
5
|
+
import { frameloopDriver } from './drivers/driver-frameloop.mjs';
|
|
6
|
+
import { inertia } from './generators/inertia.mjs';
|
|
7
|
+
import { keyframes } from './generators/keyframes.mjs';
|
|
8
|
+
import { calcGeneratorDuration } from './generators/utils/calc-duration.mjs';
|
|
9
|
+
import { getFinalKeyframe } from './keyframes/get-final.mjs';
|
|
10
|
+
import { replaceTransitionType } from './utils/replace-transition-type.mjs';
|
|
11
|
+
import { WithPromise } from './utils/WithPromise.mjs';
|
|
12
|
+
|
|
13
|
+
const percentToProgress = (percent) => percent / 100;
|
|
14
|
+
class JSAnimation extends WithPromise {
|
|
15
|
+
constructor(options) {
|
|
16
|
+
super();
|
|
17
|
+
this.state = "idle";
|
|
18
|
+
this.startTime = null;
|
|
19
|
+
this.isStopped = false;
|
|
20
|
+
/**
|
|
21
|
+
* The current time of the animation.
|
|
22
|
+
*/
|
|
23
|
+
this.currentTime = 0;
|
|
24
|
+
/**
|
|
25
|
+
* The time at which the animation was paused.
|
|
26
|
+
*/
|
|
27
|
+
this.holdTime = null;
|
|
28
|
+
/**
|
|
29
|
+
* Playback speed as a factor. 0 would be stopped, -1 reverse and 2 double speed.
|
|
30
|
+
*/
|
|
31
|
+
this.playbackSpeed = 1;
|
|
32
|
+
/**
|
|
33
|
+
* This method is bound to the instance to fix a pattern where
|
|
34
|
+
* animation.stop is returned as a reference from a useEffect.
|
|
35
|
+
*/
|
|
36
|
+
this.stop = () => {
|
|
37
|
+
const { motionValue } = this.options;
|
|
38
|
+
if (motionValue && motionValue.updatedAt !== time.now()) {
|
|
39
|
+
this.tick(time.now());
|
|
40
|
+
}
|
|
41
|
+
this.isStopped = true;
|
|
42
|
+
if (this.state === "idle")
|
|
43
|
+
return;
|
|
44
|
+
this.teardown();
|
|
45
|
+
const { onStop } = this.options;
|
|
46
|
+
onStop && onStop();
|
|
47
|
+
};
|
|
48
|
+
activeAnimations.mainThread++;
|
|
49
|
+
this.options = options;
|
|
50
|
+
this.initAnimation();
|
|
51
|
+
this.play();
|
|
52
|
+
if (options.autoplay === false)
|
|
53
|
+
this.pause();
|
|
54
|
+
}
|
|
55
|
+
initAnimation() {
|
|
56
|
+
const { options } = this;
|
|
57
|
+
replaceTransitionType(options);
|
|
58
|
+
const { type = keyframes, repeat = 0, repeatDelay = 0, repeatType, velocity = 0, } = options;
|
|
59
|
+
let { keyframes: keyframes$1 } = options;
|
|
60
|
+
const generatorFactory = type || keyframes;
|
|
61
|
+
if (process.env.NODE_ENV !== "production" &&
|
|
62
|
+
generatorFactory !== keyframes) {
|
|
63
|
+
invariant(keyframes$1.length <= 2, `Only two keyframes currently supported with spring and inertia animations. Trying to animate ${keyframes$1}`);
|
|
64
|
+
}
|
|
65
|
+
if (generatorFactory !== keyframes &&
|
|
66
|
+
typeof keyframes$1[0] !== "number") {
|
|
67
|
+
this.mixKeyframes = pipe(percentToProgress, mix(keyframes$1[0], keyframes$1[1]));
|
|
68
|
+
keyframes$1 = [0, 100];
|
|
69
|
+
}
|
|
70
|
+
const generator = generatorFactory({ ...options, keyframes: keyframes$1 });
|
|
71
|
+
/**
|
|
72
|
+
* If we have a mirror repeat type we need to create a second generator that outputs the
|
|
73
|
+
* mirrored (not reversed) animation and later ping pong between the two generators.
|
|
74
|
+
*/
|
|
75
|
+
if (repeatType === "mirror") {
|
|
76
|
+
this.mirroredGenerator = generatorFactory({
|
|
77
|
+
...options,
|
|
78
|
+
keyframes: [...keyframes$1].reverse(),
|
|
79
|
+
velocity: -velocity,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* If duration is undefined and we have repeat options,
|
|
84
|
+
* we need to calculate a duration from the generator.
|
|
85
|
+
*
|
|
86
|
+
* We set it to the generator itself to cache the duration.
|
|
87
|
+
* Any timeline resolver will need to have already precalculated
|
|
88
|
+
* the duration by this step.
|
|
89
|
+
*/
|
|
90
|
+
if (generator.calculatedDuration === null) {
|
|
91
|
+
generator.calculatedDuration = calcGeneratorDuration(generator);
|
|
92
|
+
}
|
|
93
|
+
const { calculatedDuration } = generator;
|
|
94
|
+
this.calculatedDuration = calculatedDuration;
|
|
95
|
+
this.resolvedDuration = calculatedDuration + repeatDelay;
|
|
96
|
+
this.totalDuration = this.resolvedDuration * (repeat + 1) - repeatDelay;
|
|
97
|
+
this.generator = generator;
|
|
98
|
+
}
|
|
99
|
+
updateTime(timestamp) {
|
|
100
|
+
const animationTime = Math.round(timestamp - this.startTime) * this.playbackSpeed;
|
|
101
|
+
// Update currentTime
|
|
102
|
+
if (this.holdTime !== null) {
|
|
103
|
+
this.currentTime = this.holdTime;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
// Rounding the time because floating point arithmetic is not always accurate, e.g. 3000.367 - 1000.367 =
|
|
107
|
+
// 2000.0000000000002. This is a problem when we are comparing the currentTime with the duration, for
|
|
108
|
+
// example.
|
|
109
|
+
this.currentTime = animationTime;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
tick(timestamp, sample = false) {
|
|
113
|
+
const { generator, totalDuration, mixKeyframes, mirroredGenerator, resolvedDuration, calculatedDuration, } = this;
|
|
114
|
+
if (this.startTime === null)
|
|
115
|
+
return generator.next(0);
|
|
116
|
+
const { delay = 0, keyframes, repeat, repeatType, repeatDelay, type, onUpdate, finalKeyframe, } = this.options;
|
|
117
|
+
/**
|
|
118
|
+
* requestAnimationFrame timestamps can come through as lower than
|
|
119
|
+
* the startTime as set by performance.now(). Here we prevent this,
|
|
120
|
+
* though in the future it could be possible to make setting startTime
|
|
121
|
+
* a pending operation that gets resolved here.
|
|
122
|
+
*/
|
|
123
|
+
if (this.speed > 0) {
|
|
124
|
+
this.startTime = Math.min(this.startTime, timestamp);
|
|
125
|
+
}
|
|
126
|
+
else if (this.speed < 0) {
|
|
127
|
+
this.startTime = Math.min(timestamp - totalDuration / this.speed, this.startTime);
|
|
128
|
+
}
|
|
129
|
+
if (sample) {
|
|
130
|
+
this.currentTime = timestamp;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
this.updateTime(timestamp);
|
|
134
|
+
}
|
|
135
|
+
// Rebase on delay
|
|
136
|
+
const timeWithoutDelay = this.currentTime - delay * (this.playbackSpeed >= 0 ? 1 : -1);
|
|
137
|
+
const isInDelayPhase = this.playbackSpeed >= 0
|
|
138
|
+
? timeWithoutDelay < 0
|
|
139
|
+
: timeWithoutDelay > totalDuration;
|
|
140
|
+
this.currentTime = Math.max(timeWithoutDelay, 0);
|
|
141
|
+
// If this animation has finished, set the current time to the total duration.
|
|
142
|
+
if (this.state === "finished" && this.holdTime === null) {
|
|
143
|
+
this.currentTime = totalDuration;
|
|
144
|
+
}
|
|
145
|
+
let elapsed = this.currentTime;
|
|
146
|
+
let frameGenerator = generator;
|
|
147
|
+
if (repeat) {
|
|
148
|
+
/**
|
|
149
|
+
* Get the current progress (0-1) of the animation. If t is >
|
|
150
|
+
* than duration we'll get values like 2.5 (midway through the
|
|
151
|
+
* third iteration)
|
|
152
|
+
*/
|
|
153
|
+
const progress = Math.min(this.currentTime, totalDuration) / resolvedDuration;
|
|
154
|
+
/**
|
|
155
|
+
* Get the current iteration (0 indexed). For instance the floor of
|
|
156
|
+
* 2.5 is 2.
|
|
157
|
+
*/
|
|
158
|
+
let currentIteration = Math.floor(progress);
|
|
159
|
+
/**
|
|
160
|
+
* Get the current progress of the iteration by taking the remainder
|
|
161
|
+
* so 2.5 is 0.5 through iteration 2
|
|
162
|
+
*/
|
|
163
|
+
let iterationProgress = progress % 1.0;
|
|
164
|
+
/**
|
|
165
|
+
* If iteration progress is 1 we count that as the end
|
|
166
|
+
* of the previous iteration.
|
|
167
|
+
*/
|
|
168
|
+
if (!iterationProgress && progress >= 1) {
|
|
169
|
+
iterationProgress = 1;
|
|
170
|
+
}
|
|
171
|
+
iterationProgress === 1 && currentIteration--;
|
|
172
|
+
currentIteration = Math.min(currentIteration, repeat + 1);
|
|
173
|
+
/**
|
|
174
|
+
* Reverse progress if we're not running in "normal" direction
|
|
175
|
+
*/
|
|
176
|
+
const isOddIteration = Boolean(currentIteration % 2);
|
|
177
|
+
if (isOddIteration) {
|
|
178
|
+
if (repeatType === "reverse") {
|
|
179
|
+
iterationProgress = 1 - iterationProgress;
|
|
180
|
+
if (repeatDelay) {
|
|
181
|
+
iterationProgress -= repeatDelay / resolvedDuration;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else if (repeatType === "mirror") {
|
|
185
|
+
frameGenerator = mirroredGenerator;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
elapsed = clamp(0, 1, iterationProgress) * resolvedDuration;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* If we're in negative time, set state as the initial keyframe.
|
|
192
|
+
* This prevents delay: x, duration: 0 animations from finishing
|
|
193
|
+
* instantly.
|
|
194
|
+
*/
|
|
195
|
+
const state = isInDelayPhase
|
|
196
|
+
? { done: false, value: keyframes[0] }
|
|
197
|
+
: frameGenerator.next(elapsed);
|
|
198
|
+
if (mixKeyframes) {
|
|
199
|
+
state.value = mixKeyframes(state.value);
|
|
200
|
+
}
|
|
201
|
+
let { done } = state;
|
|
202
|
+
if (!isInDelayPhase && calculatedDuration !== null) {
|
|
203
|
+
done =
|
|
204
|
+
this.playbackSpeed >= 0
|
|
205
|
+
? this.currentTime >= totalDuration
|
|
206
|
+
: this.currentTime <= 0;
|
|
207
|
+
}
|
|
208
|
+
const isAnimationFinished = this.holdTime === null &&
|
|
209
|
+
(this.state === "finished" || (this.state === "running" && done));
|
|
210
|
+
// TODO: The exception for inertia could be cleaner here
|
|
211
|
+
if (isAnimationFinished && type !== inertia) {
|
|
212
|
+
state.value = getFinalKeyframe(keyframes, this.options, finalKeyframe, this.speed);
|
|
213
|
+
}
|
|
214
|
+
if (onUpdate) {
|
|
215
|
+
onUpdate(state.value);
|
|
216
|
+
}
|
|
217
|
+
if (isAnimationFinished) {
|
|
218
|
+
this.finish();
|
|
219
|
+
}
|
|
220
|
+
return state;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Allows the returned animation to be awaited or promise-chained. Currently
|
|
224
|
+
* resolves when the animation finishes at all but in a future update could/should
|
|
225
|
+
* reject if its cancels.
|
|
226
|
+
*/
|
|
227
|
+
then(resolve, reject) {
|
|
228
|
+
return this.finished.then(resolve, reject);
|
|
229
|
+
}
|
|
230
|
+
get duration() {
|
|
231
|
+
return millisecondsToSeconds(this.calculatedDuration);
|
|
232
|
+
}
|
|
233
|
+
get time() {
|
|
234
|
+
return millisecondsToSeconds(this.currentTime);
|
|
235
|
+
}
|
|
236
|
+
set time(newTime) {
|
|
237
|
+
newTime = secondsToMilliseconds(newTime);
|
|
238
|
+
this.currentTime = newTime;
|
|
239
|
+
if (this.startTime === null ||
|
|
240
|
+
this.holdTime !== null ||
|
|
241
|
+
this.playbackSpeed === 0) {
|
|
242
|
+
this.holdTime = newTime;
|
|
243
|
+
}
|
|
244
|
+
else if (this.driver) {
|
|
245
|
+
this.startTime = this.driver.now() - newTime / this.playbackSpeed;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
get speed() {
|
|
249
|
+
return this.playbackSpeed;
|
|
250
|
+
}
|
|
251
|
+
set speed(newSpeed) {
|
|
252
|
+
this.updateTime(time.now());
|
|
253
|
+
const hasChanged = this.playbackSpeed !== newSpeed;
|
|
254
|
+
this.playbackSpeed = newSpeed;
|
|
255
|
+
if (hasChanged) {
|
|
256
|
+
this.time = millisecondsToSeconds(this.currentTime);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
play() {
|
|
260
|
+
if (this.isStopped)
|
|
261
|
+
return;
|
|
262
|
+
const { driver = frameloopDriver, onPlay, startTime } = this.options;
|
|
263
|
+
if (!this.driver) {
|
|
264
|
+
this.driver = driver((timestamp) => this.tick(timestamp));
|
|
265
|
+
}
|
|
266
|
+
onPlay && onPlay();
|
|
267
|
+
const now = this.driver.now();
|
|
268
|
+
if (this.holdTime !== null) {
|
|
269
|
+
this.startTime = now - this.holdTime;
|
|
270
|
+
}
|
|
271
|
+
else if (this.state === "finished") {
|
|
272
|
+
console.log("JSAnimation updateFinished");
|
|
273
|
+
this.updateFinished();
|
|
274
|
+
this.startTime = now;
|
|
275
|
+
}
|
|
276
|
+
else if (!this.startTime) {
|
|
277
|
+
this.startTime = startTime ?? now;
|
|
278
|
+
}
|
|
279
|
+
if (this.state === "finished" && this.speed < 0) {
|
|
280
|
+
this.startTime += this.calculatedDuration;
|
|
281
|
+
}
|
|
282
|
+
this.holdTime = null;
|
|
283
|
+
/**
|
|
284
|
+
* Set playState to running only after we've used it in
|
|
285
|
+
* the previous logic.
|
|
286
|
+
*/
|
|
287
|
+
this.state = "running";
|
|
288
|
+
this.driver.start();
|
|
289
|
+
}
|
|
290
|
+
pause() {
|
|
291
|
+
this.state = "paused";
|
|
292
|
+
this.updateTime(time.now());
|
|
293
|
+
this.holdTime = this.currentTime;
|
|
294
|
+
}
|
|
295
|
+
complete() {
|
|
296
|
+
if (this.state !== "running") {
|
|
297
|
+
this.play();
|
|
298
|
+
}
|
|
299
|
+
this.state = "finished";
|
|
300
|
+
this.holdTime = null;
|
|
301
|
+
}
|
|
302
|
+
finish() {
|
|
303
|
+
this.notifyFinished();
|
|
304
|
+
this.teardown();
|
|
305
|
+
this.state = "finished";
|
|
306
|
+
const { onComplete } = this.options;
|
|
307
|
+
onComplete && onComplete();
|
|
308
|
+
}
|
|
309
|
+
cancel() {
|
|
310
|
+
this.holdTime = null;
|
|
311
|
+
this.startTime = 0;
|
|
312
|
+
this.tick(0);
|
|
313
|
+
this.teardown();
|
|
314
|
+
}
|
|
315
|
+
teardown() {
|
|
316
|
+
this.state = "idle";
|
|
317
|
+
this.stopDriver();
|
|
318
|
+
this.startTime = this.holdTime = null;
|
|
319
|
+
activeAnimations.mainThread--;
|
|
320
|
+
}
|
|
321
|
+
stopDriver() {
|
|
322
|
+
if (!this.driver)
|
|
323
|
+
return;
|
|
324
|
+
this.driver.stop();
|
|
325
|
+
this.driver = undefined;
|
|
326
|
+
}
|
|
327
|
+
sample(sampleTime) {
|
|
328
|
+
this.startTime = 0;
|
|
329
|
+
return this.tick(sampleTime, true);
|
|
330
|
+
}
|
|
331
|
+
attachTimeline(timeline) {
|
|
332
|
+
this.pause();
|
|
333
|
+
if (this.options.allowFlatten) {
|
|
334
|
+
this.options.type = "keyframes";
|
|
335
|
+
this.options.ease = "linear";
|
|
336
|
+
this.initAnimation();
|
|
337
|
+
}
|
|
338
|
+
return timeline.observe(this);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
// Legacy function support
|
|
342
|
+
function animateValue(options) {
|
|
343
|
+
return new JSAnimation(options);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
export { JSAnimation, animateValue };
|