-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathAsyncMotionValueAnimation.ts
More file actions
289 lines (242 loc) · 7.8 KB
/
AsyncMotionValueAnimation.ts
File metadata and controls
289 lines (242 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { MotionGlobalConfig, noop } from "motion-utils"
import { time } from "../frameloop/sync-time"
import { JSAnimation } from "./JSAnimation"
import { getFinalKeyframe } from "./keyframes/get-final"
import {
KeyframeResolver as DefaultKeyframeResolver,
flushKeyframeResolvers,
ResolvedKeyframes,
} from "./keyframes/KeyframesResolver"
import { NativeAnimationExtended } from "./NativeAnimationExtended"
import {
AnimationPlaybackControls,
AnyResolvedKeyframe,
TimelineWithFallback,
ValueAnimationOptions,
} from "./types"
import { canAnimate } from "./utils/can-animate"
import { makeAnimationInstant } from "./utils/make-animation-instant"
import { WithPromise } from "./utils/WithPromise"
import { supportsBrowserAnimation } from "./waapi/supports/waapi"
/**
* Maximum time allowed between an animation being created and it being
* resolved for us to use the latter as the start time.
*
* This is to ensure that while we prefer to "start" an animation as soon
* as it's triggered, we also want to avoid a visual jump if there's a big delay
* between these two moments.
*/
const MAX_RESOLVE_DELAY = 40
type OptionsWithoutKeyframes<T extends AnyResolvedKeyframe> = Omit<
ValueAnimationOptions<T>,
"keyframes"
>
export class AsyncMotionValueAnimation<T extends AnyResolvedKeyframe>
extends WithPromise
implements AnimationPlaybackControls
{
private createdAt: number
private resolvedAt: number | undefined
private _animation: AnimationPlaybackControls | undefined
private pendingTimeline: TimelineWithFallback | undefined
private keyframeResolver: DefaultKeyframeResolver | undefined
private stopTimeline: VoidFunction | undefined
constructor({
autoplay = true,
delay = 0,
type = "keyframes",
repeat = 0,
repeatDelay = 0,
repeatType = "loop",
keyframes,
name,
motionValue,
element,
...options
}: ValueAnimationOptions<T>) {
super()
this.createdAt = time.now()
const optionsWithDefaults: OptionsWithoutKeyframes<T> = {
autoplay,
delay,
type,
repeat,
repeatDelay,
repeatType,
name,
motionValue,
element,
...options,
}
const KeyframeResolver =
element?.KeyframeResolver || DefaultKeyframeResolver
this.keyframeResolver = new KeyframeResolver(
keyframes,
(
resolvedKeyframes: ResolvedKeyframes<T>,
finalKeyframe: T,
forced: boolean
) =>
this.onKeyframesResolved(
resolvedKeyframes,
finalKeyframe,
optionsWithDefaults,
!forced
),
name,
motionValue,
element
)
this.keyframeResolver?.scheduleResolve()
}
onKeyframesResolved(
keyframes: ResolvedKeyframes<T>,
finalKeyframe: T,
options: OptionsWithoutKeyframes<T>,
sync: boolean
) {
this.keyframeResolver = undefined
const { name, type, velocity, delay, isHandoff, onUpdate } = options
this.resolvedAt = time.now()
/**
* If we can't animate this value with the resolved keyframes
* then we should complete it immediately.
*/
let canAnimateValue = true
if (!canAnimate(keyframes, name, type, velocity)) {
canAnimateValue = false
if (MotionGlobalConfig.instantAnimations || !delay) {
onUpdate?.(getFinalKeyframe(keyframes, options, finalKeyframe))
}
keyframes[0] = keyframes[keyframes.length - 1]
makeAnimationInstant(options)
options.repeat = 0
}
/**
* Resolve startTime for the animation.
*
* This method uses the createdAt and resolvedAt to calculate the
* animation startTime. *Ideally*, we would use the createdAt time as t=0
* as the following frame would then be the first frame of the animation in
* progress, which would feel snappier.
*
* However, if there's a delay (main thread work) between the creation of
* the animation and the first committed frame, we prefer to use resolvedAt
* to avoid a sudden jump into the animation.
*/
const startTime = sync
? !this.resolvedAt
? this.createdAt
: this.resolvedAt - this.createdAt > MAX_RESOLVE_DELAY
? this.resolvedAt
: this.createdAt
: undefined
const resolvedOptions = {
startTime,
finalKeyframe,
...options,
keyframes,
}
/**
* Animate via WAAPI if possible. If this is a handoff animation, the optimised animation will be running via
* WAAPI. Therefore, this animation must be JS to ensure it runs "under" the
* optimised animation.
*
* Also skip WAAPI when keyframes aren't animatable, as the resolved
* values may not be valid CSS and would trigger browser warnings.
*/
const useWaapi =
canAnimateValue &&
!isHandoff &&
supportsBrowserAnimation(resolvedOptions)
const element = resolvedOptions.motionValue?.owner?.current
const animation = useWaapi
? new NativeAnimationExtended({
...resolvedOptions,
element,
} as any)
: new JSAnimation(resolvedOptions)
animation.finished.then(() => {
this.notifyFinished()
}).catch(noop)
if (this.pendingTimeline) {
this.stopTimeline = animation.attachTimeline(this.pendingTimeline)
this.pendingTimeline = undefined
}
this._animation = animation
}
get finished() {
if (!this._animation) {
return this._finished
} else {
return this.animation.finished
}
}
then(onResolve: VoidFunction, _onReject?: VoidFunction) {
return this.finished.finally(onResolve).then(() => {})
}
get animation(): AnimationPlaybackControls {
if (!this._animation) {
this.keyframeResolver?.resume()
flushKeyframeResolvers()
}
return this._animation!
}
get duration() {
return this.animation.duration
}
get iterationDuration() {
return this.animation.iterationDuration
}
get time() {
return this.animation.time
}
set time(newTime: number) {
this.animation.time = newTime
}
get speed() {
return this.animation.speed
}
get state() {
return this.animation.state
}
set speed(newSpeed: number) {
this.animation.speed = newSpeed
}
get startTime() {
return this.animation.startTime
}
attachTimeline(timeline: TimelineWithFallback) {
if (this._animation) {
this.stopTimeline = this.animation.attachTimeline(timeline)
} else {
this.pendingTimeline = timeline
}
return () => this.stop()
}
play() {
this.animation.play()
}
pause() {
this.animation.pause()
}
complete() {
this.animation.complete()
}
cancel() {
if (this._animation) {
this.animation.cancel()
}
this.keyframeResolver?.cancel()
}
/**
* Bound to support return animation.stop pattern
*/
stop = () => {
if (this._animation) {
this._animation.stop()
this.stopTimeline?.()
}
this.keyframeResolver?.cancel()
}
}