motion-dom 14.1.0 → 14.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +1165 -187
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/animation/AsyncMotionValueAnimation.mjs +19 -1
- package/dist/es/animation/AsyncMotionValueAnimation.mjs.map +1 -1
- package/dist/es/animation/JSAnimation.mjs +54 -24
- package/dist/es/animation/JSAnimation.mjs.map +1 -1
- package/dist/es/animation/NativeAnimationExtended.mjs +47 -20
- package/dist/es/animation/NativeAnimationExtended.mjs.map +1 -1
- package/dist/es/animation/generators/utils/create-generator-easing.mjs +14 -1
- package/dist/es/animation/generators/utils/create-generator-easing.mjs.map +1 -1
- package/dist/es/animation/interfaces/motion-value.mjs +1 -0
- package/dist/es/animation/interfaces/motion-value.mjs.map +1 -1
- package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs +25 -1
- package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs.map +1 -1
- package/dist/es/animation/waapi/supports/waapi.mjs +1 -1
- package/dist/es/animation/waapi/supports/waapi.mjs.map +1 -1
- package/dist/es/animation/waapi/transforms/TransformAnimation.mjs +854 -0
- package/dist/es/animation/waapi/transforms/TransformAnimation.mjs.map +1 -0
- package/dist/es/animation/waapi/transforms/groups.mjs +4 -0
- package/dist/es/animation/waapi/transforms/groups.mjs.map +1 -0
- package/dist/es/effects/style/index.mjs +6 -3
- package/dist/es/effects/style/index.mjs.map +1 -1
- package/dist/es/effects/style/transform.mjs +2 -7
- package/dist/es/effects/style/transform.mjs.map +1 -1
- package/dist/es/index.mjs +3 -3
- package/dist/es/projection/node/create-projection-node.mjs +10 -0
- package/dist/es/projection/node/create-projection-node.mjs.map +1 -1
- package/dist/es/render/VisualElement.mjs +16 -3
- package/dist/es/render/VisualElement.mjs.map +1 -1
- package/dist/es/render/html/utils/build-transform.mjs +1 -1
- package/dist/es/render/html/utils/build-transform.mjs.map +1 -1
- package/dist/es/value/index.mjs +12 -0
- package/dist/es/value/index.mjs.map +1 -1
- package/dist/index.d.ts +77 -12
- package/dist/motion-dom.dev.js +1165 -187
- package/dist/motion-dom.js +1 -1
- package/dist/size-rollup-motion-value.js +1 -1
- package/dist/size-rollup-motion-value.js.map +1 -1
- package/dist/size-rollup-style-effect.js +1 -1
- package/dist/size-rollup-style-effect.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,854 @@
|
|
|
1
|
+
import { isBezierDefinition, millisecondsToSeconds, clamp, progress } from 'motion-utils';
|
|
2
|
+
import { frame } from '../../../frameloop/frame.mjs';
|
|
3
|
+
import { frameData } from '../../../frameloop/frame-data.mjs';
|
|
4
|
+
import { time } from '../../../frameloop/sync-time.mjs';
|
|
5
|
+
import { buildTransform, translateAlias } from '../../../render/html/utils/build-transform.mjs';
|
|
6
|
+
import { transformPropOrder } from '../../../render/utils/keys-transform.mjs';
|
|
7
|
+
import { mixNumber } from '../../../utils/mix/number.mjs';
|
|
8
|
+
import { numberValueTypes } from '../../../value/types/maps/number.mjs';
|
|
9
|
+
import { getValueAsType } from '../../../value/types/utils/get-as-type.mjs';
|
|
10
|
+
import { frameloopDriver } from '../../drivers/frame.mjs';
|
|
11
|
+
import { keyframes } from '../../generators/keyframes.mjs';
|
|
12
|
+
import { defaultOffset } from '../../keyframes/offsets/default.mjs';
|
|
13
|
+
import { JSAnimation, round } from '../../JSAnimation.mjs';
|
|
14
|
+
import { sampleDelta } from '../../NativeAnimationExtended.mjs';
|
|
15
|
+
import { cubicBezierAsString } from '../easing/cubic-bezier.mjs';
|
|
16
|
+
import { supportedWaapiEasing } from '../easing/supported.mjs';
|
|
17
|
+
import { supportsWaapi } from '../supports/waapi.mjs';
|
|
18
|
+
import { supportsLinearEasing } from '../../../utils/supports/linear-easing.mjs';
|
|
19
|
+
import { transformGroups } from './groups.mjs';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Sample at the interruption sampling rate, up to a cap for long
|
|
23
|
+
* animations.
|
|
24
|
+
*/
|
|
25
|
+
const maxSamples = 200;
|
|
26
|
+
/**
|
|
27
|
+
* How far values may stray from the WAAPI animation: as a fraction of
|
|
28
|
+
* each value's change when they share an easing, otherwise in px or deg,
|
|
29
|
+
* or for scale.
|
|
30
|
+
*/
|
|
31
|
+
const easingTolerance = 0.001;
|
|
32
|
+
const sampleTolerance = 0.1;
|
|
33
|
+
const scaleTolerance = 0.001;
|
|
34
|
+
/**
|
|
35
|
+
* If an outside write, or a seek the WAAPI animation can't follow,
|
|
36
|
+
* happens again within this window, the group is being driven every
|
|
37
|
+
* frame (scrubbing, drag), which is cheaper on the main thread than
|
|
38
|
+
* rebuilding the WAAPI animation every frame.
|
|
39
|
+
*/
|
|
40
|
+
const churnWindow = 100;
|
|
41
|
+
/**
|
|
42
|
+
* Named easings that are the same cubic-bezier in JS and CSS.
|
|
43
|
+
*/
|
|
44
|
+
const isNativeEasing = (ease) => /^(linear|ease(In|Out|InOut))$/.test(ease);
|
|
45
|
+
const byTime = (a, b) => a - b;
|
|
46
|
+
/**
|
|
47
|
+
* n + 1 evenly spaced timestamps, from one timestamp to another.
|
|
48
|
+
*/
|
|
49
|
+
const spread = (from, to, n) => {
|
|
50
|
+
const result = [];
|
|
51
|
+
for (let i = 0; i <= n; i++)
|
|
52
|
+
result.push(mixNumber(from, to, i / n));
|
|
53
|
+
return result;
|
|
54
|
+
};
|
|
55
|
+
const getOwner = (options) => options.motionValue.owner;
|
|
56
|
+
const getGroup = (element) => transformGroups.get(element);
|
|
57
|
+
const read = (owner, key) => owner.latestValues ? owner.latestValues[key] : owner.getValue(key)?.get();
|
|
58
|
+
/**
|
|
59
|
+
* Whether this animation should join its element's transform group.
|
|
60
|
+
* Animations that can't be accelerated still join, so the group knows
|
|
61
|
+
* to stay on the main thread while they run. Browsers without linear()
|
|
62
|
+
* easing stay on the main thread.
|
|
63
|
+
*/
|
|
64
|
+
const canGroupTransform = ({ name, motionValue, }) => name &&
|
|
65
|
+
transformPropOrder.includes(name) &&
|
|
66
|
+
motionValue?.owner?.current instanceof HTMLElement &&
|
|
67
|
+
supportsWaapi() &&
|
|
68
|
+
supportsLinearEasing();
|
|
69
|
+
function canAccelerateTransform(options) {
|
|
70
|
+
const owner = getOwner(options);
|
|
71
|
+
const { onUpdate, transformTemplate } = owner.getProps();
|
|
72
|
+
return (
|
|
73
|
+
/**
|
|
74
|
+
* A handoff from an optimised appear animation is already running
|
|
75
|
+
* on its own WAAPI animation. onUpdate needs every value, every
|
|
76
|
+
* frame.
|
|
77
|
+
*/
|
|
78
|
+
!options.isHandoff &&
|
|
79
|
+
!onUpdate &&
|
|
80
|
+
!options.hasOnUpdate &&
|
|
81
|
+
/**
|
|
82
|
+
* transformTemplate could output anything.
|
|
83
|
+
*/
|
|
84
|
+
!transformTemplate &&
|
|
85
|
+
options.type !== "inertia" &&
|
|
86
|
+
/**
|
|
87
|
+
* A literal transform, or pathRotation, replaces or adds to the
|
|
88
|
+
* transform we'd build. styleEffect builds its own as "none".
|
|
89
|
+
*/
|
|
90
|
+
(read(owner, "transform") || "none") === "none" &&
|
|
91
|
+
!read(owner, "pathRotation") &&
|
|
92
|
+
/**
|
|
93
|
+
* Layout animations write transform every frame.
|
|
94
|
+
*/
|
|
95
|
+
!owner.projection?.isProjecting());
|
|
96
|
+
}
|
|
97
|
+
class TransformAnimation extends JSAnimation {
|
|
98
|
+
constructor(options) {
|
|
99
|
+
const element = getOwner(options).current;
|
|
100
|
+
const { driver = frameloopDriver } = options;
|
|
101
|
+
/**
|
|
102
|
+
* Before JSAnimation replaces the type with its generator.
|
|
103
|
+
*/
|
|
104
|
+
const canAccelerate = canAccelerateTransform(options);
|
|
105
|
+
/**
|
|
106
|
+
* The driver ticks on the main thread only once the group has
|
|
107
|
+
* moved there. Until then, starting or stopping tells the group to
|
|
108
|
+
* update its WAAPI animation.
|
|
109
|
+
*/
|
|
110
|
+
options.driver = (update) => {
|
|
111
|
+
const js = driver(update);
|
|
112
|
+
return {
|
|
113
|
+
js,
|
|
114
|
+
now: js.now,
|
|
115
|
+
start: (keepAlive = true) => {
|
|
116
|
+
const group = getGroup(element);
|
|
117
|
+
const isJS = !group || group.isJS;
|
|
118
|
+
/**
|
|
119
|
+
* keepAlive is false when seeking. Seeking the WAAPI
|
|
120
|
+
* animation is free, but if a seek needs a rebuild and
|
|
121
|
+
* keeps happening, the group moves to the main thread.
|
|
122
|
+
*/
|
|
123
|
+
isJS
|
|
124
|
+
? js.start(keepAlive)
|
|
125
|
+
: group.schedule(false, !keepAlive);
|
|
126
|
+
},
|
|
127
|
+
stop: () => {
|
|
128
|
+
js.stop();
|
|
129
|
+
const group = getGroup(element);
|
|
130
|
+
group?.schedule(!group.isFinishing);
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
super(options);
|
|
135
|
+
this.canAccelerate = canAccelerate;
|
|
136
|
+
/**
|
|
137
|
+
* stop is bound in JSAnimation, so wrap it rather than override.
|
|
138
|
+
* Before stopping, set the motion value to where the WAAPI
|
|
139
|
+
* animation is now, with velocity, for the next animation to
|
|
140
|
+
* start from.
|
|
141
|
+
*/
|
|
142
|
+
const { stop } = this;
|
|
143
|
+
this.stop = () => {
|
|
144
|
+
this.isAccelerated() && this.sync();
|
|
145
|
+
stop();
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
get group() {
|
|
149
|
+
return getGroup(getOwner(this.options).current);
|
|
150
|
+
}
|
|
151
|
+
get js() {
|
|
152
|
+
return this.driver?.js;
|
|
153
|
+
}
|
|
154
|
+
get name() {
|
|
155
|
+
return this.options.name;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* When this stops moving, or, if it repeats forever, when it starts
|
|
159
|
+
* repeating, and how long each repeat takes. Held (paused, or at
|
|
160
|
+
* speed 0) animations never stop moving.
|
|
161
|
+
*/
|
|
162
|
+
timing() {
|
|
163
|
+
const { startTime, speed, totalDuration } = this;
|
|
164
|
+
const { delay = 0, repeatType } = this.options;
|
|
165
|
+
return this.holdTime !== null
|
|
166
|
+
? [Infinity, 0]
|
|
167
|
+
: totalDuration === Infinity
|
|
168
|
+
? [
|
|
169
|
+
startTime + delay / speed,
|
|
170
|
+
(this.resolvedDuration * (repeatType === "loop" ? 1 : 2)) /
|
|
171
|
+
speed || 1,
|
|
172
|
+
]
|
|
173
|
+
: [startTime + (delay + totalDuration) / speed, 0];
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Whether this is running, or paused, on the compositor.
|
|
177
|
+
*/
|
|
178
|
+
isAccelerated() {
|
|
179
|
+
const { group, state } = this;
|
|
180
|
+
return (group?.tracks.has(this) &&
|
|
181
|
+
!group.isJS &&
|
|
182
|
+
this.canAccelerate &&
|
|
183
|
+
(state === "running" || state === "paused"));
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* The value now, while this runs on the compositor and so doesn't
|
|
187
|
+
* set its motion value every frame.
|
|
188
|
+
*/
|
|
189
|
+
liveValue() {
|
|
190
|
+
return this.isAccelerated() ? this.sampleAt(time.now()) : undefined;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* If this is a two-keyframe tween whose easing WAAPI runs exactly,
|
|
194
|
+
* when it starts and stops moving, and that easing.
|
|
195
|
+
*/
|
|
196
|
+
tween() {
|
|
197
|
+
const { keyframes: keyframes$1, type, ease, times, repeat, delay = 0 } = this.options;
|
|
198
|
+
const easing = isBezierDefinition(ease)
|
|
199
|
+
? cubicBezierAsString(ease)
|
|
200
|
+
: isNativeEasing(ease) && supportedWaapiEasing[ease];
|
|
201
|
+
if (easing &&
|
|
202
|
+
!repeat &&
|
|
203
|
+
!times &&
|
|
204
|
+
keyframes$1.length === 2 &&
|
|
205
|
+
type === keyframes &&
|
|
206
|
+
this.holdTime === null) {
|
|
207
|
+
const start = this.startTime + delay / this.speed;
|
|
208
|
+
return [start, start + this.resolvedDuration / this.speed, easing];
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* The timestamps between two timestamps where this reaches a
|
|
213
|
+
* keyframe or starts a repeat, where it can turn or jump.
|
|
214
|
+
*/
|
|
215
|
+
boundaries(from, to) {
|
|
216
|
+
const { startTime, speed, resolvedDuration, options } = this;
|
|
217
|
+
const { delay = 0, repeat = 0, repeatDelay = 0, repeatType, times, keyframes: keyframes$1, type, } = options;
|
|
218
|
+
const offsets = (type || keyframes) === keyframes
|
|
219
|
+
? times || defaultOffset(keyframes$1)
|
|
220
|
+
: [0, 1];
|
|
221
|
+
const start = startTime + delay / speed;
|
|
222
|
+
const result = [];
|
|
223
|
+
if (this.holdTime !== null || !(resolvedDuration > 0))
|
|
224
|
+
return result;
|
|
225
|
+
for (let i = Math.max(0, Math.floor(((from - start) * speed) / resolvedDuration)); i <= repeat; i++) {
|
|
226
|
+
const iterationStart = start + (i * resolvedDuration) / speed;
|
|
227
|
+
if (iterationStart > to)
|
|
228
|
+
break;
|
|
229
|
+
for (const offset of offsets) {
|
|
230
|
+
const t = iterationStart +
|
|
231
|
+
((i % 2 && repeatType && repeatType !== "loop"
|
|
232
|
+
? 1 - offset
|
|
233
|
+
: offset) *
|
|
234
|
+
(resolvedDuration - repeatDelay)) /
|
|
235
|
+
speed;
|
|
236
|
+
t > from && t < to && result.push(t);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return result;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* The value at a timestamp, as a number.
|
|
243
|
+
*/
|
|
244
|
+
numberAt(timestamp) {
|
|
245
|
+
return parseFloat(this.sampleAt(timestamp, true));
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Write the current value, and velocity, to the motion value.
|
|
249
|
+
*/
|
|
250
|
+
sync() {
|
|
251
|
+
const now = time.now();
|
|
252
|
+
this.options.motionValue.setWithVelocity(this.sampleAt(now - sampleDelta, true), this.sampleAt(now, true), sampleDelta);
|
|
253
|
+
}
|
|
254
|
+
play() {
|
|
255
|
+
/**
|
|
256
|
+
* A stopped animation (one that was interrupted) can't play again,
|
|
257
|
+
* so it mustn't join the group.
|
|
258
|
+
*/
|
|
259
|
+
if (this.isStopped)
|
|
260
|
+
return;
|
|
261
|
+
(this.group || createGroup(getOwner(this.options))).tracks.add(this);
|
|
262
|
+
super.play();
|
|
263
|
+
}
|
|
264
|
+
pause() {
|
|
265
|
+
super.pause();
|
|
266
|
+
if (this.isAccelerated()) {
|
|
267
|
+
this.sync();
|
|
268
|
+
this.group.schedule(false);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* JSAnimation finishes on its next tick, which only happens on the
|
|
273
|
+
* main thread, so finish now.
|
|
274
|
+
*/
|
|
275
|
+
complete() {
|
|
276
|
+
super.complete();
|
|
277
|
+
const { group } = this;
|
|
278
|
+
group && !group.isJS && this.canAccelerate && this.tick(time.now());
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Without a tick to rebase it, a held time still includes the delay,
|
|
282
|
+
* so take it off here, as a JS animation does on its next tick.
|
|
283
|
+
*/
|
|
284
|
+
get time() {
|
|
285
|
+
if (this.isAccelerated()) {
|
|
286
|
+
return millisecondsToSeconds(clamp(0, this.totalDuration, this.at(time.now()) - (this.options.delay || 0)));
|
|
287
|
+
}
|
|
288
|
+
return super.time;
|
|
289
|
+
}
|
|
290
|
+
set time(newTime) {
|
|
291
|
+
super.time = newTime;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Scroll timelines set time every frame, so run these on the main
|
|
295
|
+
* thread.
|
|
296
|
+
*/
|
|
297
|
+
attachTimeline(timeline) {
|
|
298
|
+
this.canAccelerate = false;
|
|
299
|
+
this.group?.demote(true);
|
|
300
|
+
return super.attachTimeline(timeline);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
function createGroup(owner) {
|
|
304
|
+
const element = owner.current;
|
|
305
|
+
const tracks = new Set();
|
|
306
|
+
const byKey = new Map();
|
|
307
|
+
/**
|
|
308
|
+
* Transforms that aren't animating, as built into the WAAPI animation.
|
|
309
|
+
*/
|
|
310
|
+
let statics = {};
|
|
311
|
+
/**
|
|
312
|
+
* The transforms in the WAAPI animation, in order.
|
|
313
|
+
*/
|
|
314
|
+
let keys = [];
|
|
315
|
+
let built = new Map();
|
|
316
|
+
let segments = [];
|
|
317
|
+
let needsBuild = false;
|
|
318
|
+
let isScheduled = false;
|
|
319
|
+
let hasChurned = false;
|
|
320
|
+
let lastChurn = -Infinity;
|
|
321
|
+
const has = (key) => {
|
|
322
|
+
for (const track of tracks)
|
|
323
|
+
if (track.name === key)
|
|
324
|
+
return true;
|
|
325
|
+
};
|
|
326
|
+
/**
|
|
327
|
+
* The transform values at a timestamp or, without one, from the
|
|
328
|
+
* motion values. Animations that don't repeat forever are sampled no
|
|
329
|
+
* earlier than settled, so a loop that starts from an earlier cycle
|
|
330
|
+
* boundary holds them at their end.
|
|
331
|
+
*/
|
|
332
|
+
const valuesAt = (timestamp, settled) => {
|
|
333
|
+
const values = {};
|
|
334
|
+
for (const key of timestamp === undefined ? transformPropOrder : keys) {
|
|
335
|
+
const track = byKey.get(key);
|
|
336
|
+
const value = timestamp === undefined
|
|
337
|
+
? read(owner, key)
|
|
338
|
+
: track
|
|
339
|
+
? track.sampleAt(settled > timestamp && !track.timing()[1]
|
|
340
|
+
? settled
|
|
341
|
+
: timestamp)
|
|
342
|
+
: statics[key];
|
|
343
|
+
if (value !== undefined)
|
|
344
|
+
values[key] = value;
|
|
345
|
+
}
|
|
346
|
+
return values;
|
|
347
|
+
};
|
|
348
|
+
/**
|
|
349
|
+
* Build the transform at a timestamp. Unlike buildTransform, every key
|
|
350
|
+
* is written, even at its default, so each keyframe has the same list
|
|
351
|
+
* of functions and the browser interpolates them one by one.
|
|
352
|
+
*/
|
|
353
|
+
const compose = (timestamp, settled) => {
|
|
354
|
+
let transform = "";
|
|
355
|
+
const values = valuesAt(timestamp, settled);
|
|
356
|
+
for (const key in values) {
|
|
357
|
+
transform +=
|
|
358
|
+
(translateAlias[key] || key) +
|
|
359
|
+
"(" +
|
|
360
|
+
getValueAsType(values[key], numberValueTypes[key]) +
|
|
361
|
+
") ";
|
|
362
|
+
}
|
|
363
|
+
return transform || "none";
|
|
364
|
+
};
|
|
365
|
+
/**
|
|
366
|
+
* Write the transform as an inline style, as the renderer would.
|
|
367
|
+
*/
|
|
368
|
+
const commit = (values = valuesAt()) => {
|
|
369
|
+
element.style.transform = buildTransform(values, {});
|
|
370
|
+
};
|
|
371
|
+
/**
|
|
372
|
+
* Cancel the WAAPI animations, by default first committing the
|
|
373
|
+
* transform. The renderer writes its own version on its next render.
|
|
374
|
+
*/
|
|
375
|
+
const stop = (shouldCommit = true) => {
|
|
376
|
+
shouldCommit && commit();
|
|
377
|
+
segments.forEach(({ animation }) => animation.cancel());
|
|
378
|
+
segments = [];
|
|
379
|
+
};
|
|
380
|
+
const add = (animation, offset, end = Infinity) => {
|
|
381
|
+
const segment = { animation, offset, end };
|
|
382
|
+
animation.startTime = time.now() + offset;
|
|
383
|
+
/**
|
|
384
|
+
* Animations finish when the WAAPI animation reaches their end.
|
|
385
|
+
* Once a loop has taken over, the finished animation isn't needed.
|
|
386
|
+
*/
|
|
387
|
+
if (end < Infinity) {
|
|
388
|
+
animation.onfinish = () => {
|
|
389
|
+
segment.isDone = true;
|
|
390
|
+
if (segments.some((s) => s.end === Infinity)) {
|
|
391
|
+
animation.cancel();
|
|
392
|
+
segments = segments.filter((s) => s !== segment);
|
|
393
|
+
}
|
|
394
|
+
group.schedule(false);
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Something else cancelled it, so carry on on the main thread.
|
|
399
|
+
*/
|
|
400
|
+
animation.oncancel = () => segments.includes(segment) && group.demote();
|
|
401
|
+
segments.push(segment);
|
|
402
|
+
};
|
|
403
|
+
/**
|
|
404
|
+
* Where a value jumps between two timestamps, as when a repeat starts
|
|
405
|
+
* again: the timestamps either side of the jump, or none if it moves
|
|
406
|
+
* there continuously.
|
|
407
|
+
*/
|
|
408
|
+
const jumpAt = (value, a, b) => {
|
|
409
|
+
const change = (x, y) => Math.abs(value(y) - value(x));
|
|
410
|
+
const size = change(a, b);
|
|
411
|
+
while (b - a > 0.01) {
|
|
412
|
+
const mid = (a + b) / 2;
|
|
413
|
+
change(a, mid) > change(mid, b) ? (b = mid) : (a = mid);
|
|
414
|
+
}
|
|
415
|
+
return change(a, b) > size / 2 ? [a, b] : [];
|
|
416
|
+
};
|
|
417
|
+
/**
|
|
418
|
+
* If every moving animation follows the same eased progress between
|
|
419
|
+
* two timestamps, the easing that describes it.
|
|
420
|
+
*/
|
|
421
|
+
const getEasing = (moving, from, to, bounds) => {
|
|
422
|
+
const samples = Math.min(maxSamples, Math.max(2, Math.ceil((to - from) / sampleDelta)));
|
|
423
|
+
/**
|
|
424
|
+
* Too many keyframes or repeats to follow with this many samples.
|
|
425
|
+
*/
|
|
426
|
+
if (bounds.length * 4 > samples)
|
|
427
|
+
return;
|
|
428
|
+
const at = spread(from, to, samples).concat(bounds).sort(byTime);
|
|
429
|
+
const position = (t) => progress(from, to, t);
|
|
430
|
+
let shared;
|
|
431
|
+
let progressAt;
|
|
432
|
+
for (const track of moving) {
|
|
433
|
+
const values = at.map((t) => track.numberAt(t));
|
|
434
|
+
const [first] = values;
|
|
435
|
+
const range = values[values.length - 1] - first;
|
|
436
|
+
const progress = values.map((value) => range ? (value - first) / range : value - first);
|
|
437
|
+
/**
|
|
438
|
+
* A value that doesn't change, like one still in its delay,
|
|
439
|
+
* fits any easing. One that changes but ends where it started
|
|
440
|
+
* fits none.
|
|
441
|
+
*/
|
|
442
|
+
if (!range) {
|
|
443
|
+
if (progress.some((p) => !(Math.abs(p) <= easingTolerance))) {
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
else if (!shared) {
|
|
448
|
+
shared = progress;
|
|
449
|
+
progressAt = (t) => (track.numberAt(t) - first) / range;
|
|
450
|
+
}
|
|
451
|
+
else if (progress.some((p, i) => !(Math.abs(p - shared[i]) <= easingTolerance))) {
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
if (!shared ||
|
|
456
|
+
shared.every((p, i) => Math.abs(p - position(at[i])) <= easingTolerance)) {
|
|
457
|
+
return "linear";
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* A repeat that starts again jumps back, which takes two points at
|
|
461
|
+
* the same position.
|
|
462
|
+
*/
|
|
463
|
+
const points = [];
|
|
464
|
+
const point = (t, p = progressAt(t)) => points.push(round(p) + " " + round(position(t) * 100) + "%");
|
|
465
|
+
shared.forEach((p, i) => {
|
|
466
|
+
i &&
|
|
467
|
+
Math.abs(p - shared[i - 1]) > 0.2 &&
|
|
468
|
+
jumpAt(progressAt, at[i - 1], at[i]).forEach((t) => point(t));
|
|
469
|
+
point(at[i], p);
|
|
470
|
+
});
|
|
471
|
+
return "linear(" + points + ")";
|
|
472
|
+
};
|
|
473
|
+
/**
|
|
474
|
+
* Sample timestamps between two timestamps, closer together where the
|
|
475
|
+
* moving animations curve.
|
|
476
|
+
*/
|
|
477
|
+
const sample = (moving, from, to, bounds) => {
|
|
478
|
+
const times = [from];
|
|
479
|
+
/**
|
|
480
|
+
* Values that aren't numbers, like calc(), are left to the
|
|
481
|
+
* browser to interpolate.
|
|
482
|
+
*/
|
|
483
|
+
const isStraight = (a, b) => moving.every((track) => !(Math.abs(track.numberAt((a + b) / 2) -
|
|
484
|
+
(track.numberAt(a) + track.numberAt(b)) / 2) >
|
|
485
|
+
(track.name.startsWith("scale")
|
|
486
|
+
? scaleTolerance
|
|
487
|
+
: sampleTolerance)));
|
|
488
|
+
const split = (a, b, depth) => {
|
|
489
|
+
if (b - a < 1 || isStraight(a, b)) {
|
|
490
|
+
times.push(b);
|
|
491
|
+
}
|
|
492
|
+
else if (depth < 4) {
|
|
493
|
+
split(a, (a + b) / 2, depth + 1);
|
|
494
|
+
split((a + b) / 2, b, depth + 1);
|
|
495
|
+
}
|
|
496
|
+
else {
|
|
497
|
+
/**
|
|
498
|
+
* Keyframes either side of a jump, like a repeat starting
|
|
499
|
+
* again, so it doesn't play as a quick sweep.
|
|
500
|
+
*/
|
|
501
|
+
for (const track of moving) {
|
|
502
|
+
times.push(...jumpAt((t) => track.numberAt(t), a, b));
|
|
503
|
+
}
|
|
504
|
+
times.push(b);
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
/**
|
|
508
|
+
* Split at least every 100ms, and at every keyframe and repeat,
|
|
509
|
+
* either side of where a value can jump.
|
|
510
|
+
*/
|
|
511
|
+
const ends = spread(from, to, Math.ceil((to - from) / 100));
|
|
512
|
+
for (const t of bounds)
|
|
513
|
+
ends.push(t, Math.min(t + 0.01, to));
|
|
514
|
+
let start = from;
|
|
515
|
+
for (const end of ends.sort(byTime)) {
|
|
516
|
+
end > start && split(start, end, 0);
|
|
517
|
+
start = end;
|
|
518
|
+
}
|
|
519
|
+
return times.sort(byTime);
|
|
520
|
+
};
|
|
521
|
+
/**
|
|
522
|
+
* If every moving animation is the same tween, started together, as
|
|
523
|
+
* happens for the values of one animate() call or transition, when
|
|
524
|
+
* they move and their native easing.
|
|
525
|
+
*/
|
|
526
|
+
const getTween = (moving) => {
|
|
527
|
+
let shared;
|
|
528
|
+
for (const track of moving) {
|
|
529
|
+
const tween = track.tween();
|
|
530
|
+
if (!tween ||
|
|
531
|
+
shared?.some((v, i) => i < 2
|
|
532
|
+
? Math.abs(tween[i] - v) > 1
|
|
533
|
+
: tween[i] !== v)) {
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
shared = tween;
|
|
537
|
+
}
|
|
538
|
+
return shared;
|
|
539
|
+
};
|
|
540
|
+
/**
|
|
541
|
+
* A WAAPI animation of the composed transform between two timestamps.
|
|
542
|
+
*/
|
|
543
|
+
const run = (moving, from, to, options = { fill: "both" }, settled, nativeEasing) => {
|
|
544
|
+
const bounds = moving.flatMap((track) => track.boundaries(from, to));
|
|
545
|
+
const easing = nativeEasing || getEasing(moving, from, to, bounds);
|
|
546
|
+
const times = easing ? [from, to] : sample(moving, from, to, bounds);
|
|
547
|
+
const transform = times.map((t) => compose(t, settled));
|
|
548
|
+
let keyframes = { transform };
|
|
549
|
+
if (!easing) {
|
|
550
|
+
keyframes.offset = times.map((t) => progress(from, to, t));
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* If nothing moves, don't hold a transform, which, even at its
|
|
554
|
+
* default, would make the element a containing block. The empty
|
|
555
|
+
* animation still finishes the animations.
|
|
556
|
+
*/
|
|
557
|
+
if (transform.every((t) => t === transform[0])) {
|
|
558
|
+
commit(valuesAt(from, settled));
|
|
559
|
+
keyframes = null;
|
|
560
|
+
}
|
|
561
|
+
return element.animate(keyframes, {
|
|
562
|
+
...options,
|
|
563
|
+
duration: to - from,
|
|
564
|
+
easing: easing || "linear",
|
|
565
|
+
});
|
|
566
|
+
};
|
|
567
|
+
/**
|
|
568
|
+
* Replace the WAAPI animations with ones built from now. Returns
|
|
569
|
+
* undefined if the animations can't be composed.
|
|
570
|
+
*/
|
|
571
|
+
const animate = (now) => {
|
|
572
|
+
/**
|
|
573
|
+
* The finite part runs until the last finite animation (or the
|
|
574
|
+
* last delay) ends. Infinitely repeating animations then continue
|
|
575
|
+
* as one looping WAAPI animation, which needs them to share a
|
|
576
|
+
* cycle.
|
|
577
|
+
*/
|
|
578
|
+
let end = now;
|
|
579
|
+
let cycle = 0;
|
|
580
|
+
let cycleStart = 0;
|
|
581
|
+
const moving = [];
|
|
582
|
+
const looping = [];
|
|
583
|
+
const ends = new Set();
|
|
584
|
+
byKey.clear();
|
|
585
|
+
built = new Map();
|
|
586
|
+
for (const track of tracks) {
|
|
587
|
+
byKey.set(track.name, track);
|
|
588
|
+
const [trackEnd, trackCycle] = track.timing();
|
|
589
|
+
const isHeld = trackEnd === Infinity;
|
|
590
|
+
built.set(track, [track.at(now), track.speed, isHeld]);
|
|
591
|
+
/**
|
|
592
|
+
* Held animations don't move, so keep their motion values up
|
|
593
|
+
* to date, as a JSAnimation would on a seek.
|
|
594
|
+
*/
|
|
595
|
+
if (isHeld) {
|
|
596
|
+
track.sync();
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
if (track.speed < 0)
|
|
600
|
+
return;
|
|
601
|
+
if (trackCycle) {
|
|
602
|
+
if (cycle) {
|
|
603
|
+
const phase = (trackEnd - cycleStart) / trackCycle;
|
|
604
|
+
if (Math.abs(trackCycle - cycle) > 1 ||
|
|
605
|
+
Math.abs(phase - Math.round(phase)) * cycle > 1) {
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
else {
|
|
610
|
+
cycle = trackCycle;
|
|
611
|
+
cycleStart = trackEnd;
|
|
612
|
+
}
|
|
613
|
+
looping.push(track);
|
|
614
|
+
}
|
|
615
|
+
else {
|
|
616
|
+
ends.add(trackEnd);
|
|
617
|
+
}
|
|
618
|
+
moving.push(track);
|
|
619
|
+
end = Math.max(end, trackEnd);
|
|
620
|
+
}
|
|
621
|
+
statics = {};
|
|
622
|
+
/**
|
|
623
|
+
* Transforms that aren't animating are left out at their default.
|
|
624
|
+
*/
|
|
625
|
+
keys = transformPropOrder.filter((key) => {
|
|
626
|
+
if (byKey.has(key))
|
|
627
|
+
return true;
|
|
628
|
+
const value = (statics[key] = read(owner, key));
|
|
629
|
+
return (value !== undefined &&
|
|
630
|
+
parseFloat(value) !==
|
|
631
|
+
(key.startsWith("scale") ? 1 : 0));
|
|
632
|
+
});
|
|
633
|
+
stop(false);
|
|
634
|
+
/**
|
|
635
|
+
* Values that share one tween are one WAAPI animation of it, with
|
|
636
|
+
* two keyframes and its own easing, from when they started.
|
|
637
|
+
*/
|
|
638
|
+
const tween = !cycle && getTween(moving);
|
|
639
|
+
try {
|
|
640
|
+
if (tween && tween[1] > now) {
|
|
641
|
+
const [from, to, easing] = tween;
|
|
642
|
+
add(run(moving, from, to, undefined, undefined, easing), from - now, to - from);
|
|
643
|
+
}
|
|
644
|
+
else if (end > now) {
|
|
645
|
+
add(run(moving, now, end), 0, end - now);
|
|
646
|
+
/**
|
|
647
|
+
* Animations that end sooner finish on the events of
|
|
648
|
+
* empty animations that end with them.
|
|
649
|
+
*/
|
|
650
|
+
ends.forEach((trackEnd) => trackEnd < end - 1 &&
|
|
651
|
+
add(element.animate(null, {
|
|
652
|
+
duration: trackEnd - now,
|
|
653
|
+
}), 0, trackEnd - now));
|
|
654
|
+
}
|
|
655
|
+
if (cycle) {
|
|
656
|
+
/**
|
|
657
|
+
* Build one cycle from a cycle boundary, so it repeats
|
|
658
|
+
* seamlessly, and start it part way through.
|
|
659
|
+
*/
|
|
660
|
+
const from = cycleStart + Math.floor((end - cycleStart) / cycle) * cycle;
|
|
661
|
+
add(run(looping, from, from + cycle, {
|
|
662
|
+
iterations: Infinity,
|
|
663
|
+
iterationStart: (end - from) / cycle,
|
|
664
|
+
/**
|
|
665
|
+
* It must not fill backwards over the finite part
|
|
666
|
+
* that runs before it.
|
|
667
|
+
*/
|
|
668
|
+
fill: "forwards",
|
|
669
|
+
}, end), end - now);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
catch {
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* Everything is held, so hold it with an inline style instead.
|
|
677
|
+
*/
|
|
678
|
+
segments.length || stop();
|
|
679
|
+
return true;
|
|
680
|
+
};
|
|
681
|
+
/**
|
|
682
|
+
* If every animation has been paused, played, seeked or sped up
|
|
683
|
+
* together since the WAAPI animation was built, do the same to it.
|
|
684
|
+
* Returns false if it needs rebuilding instead.
|
|
685
|
+
*/
|
|
686
|
+
const retime = (now) => {
|
|
687
|
+
let elapsed;
|
|
688
|
+
let rate = 0;
|
|
689
|
+
for (const track of tracks) {
|
|
690
|
+
const state = built.get(track);
|
|
691
|
+
if (!state)
|
|
692
|
+
return false;
|
|
693
|
+
const [builtTime, builtSpeed, wasHeld] = state;
|
|
694
|
+
const trackTime = track.at(now);
|
|
695
|
+
const isHeld = track.timing()[0] === Infinity;
|
|
696
|
+
if (wasHeld) {
|
|
697
|
+
if (isHeld && trackTime === builtTime)
|
|
698
|
+
continue;
|
|
699
|
+
return false;
|
|
700
|
+
}
|
|
701
|
+
const trackElapsed = (trackTime - builtTime) / builtSpeed;
|
|
702
|
+
const trackRate = isHeld ? 0 : track.speed / builtSpeed;
|
|
703
|
+
if (elapsed === undefined) {
|
|
704
|
+
elapsed = trackElapsed;
|
|
705
|
+
rate = trackRate;
|
|
706
|
+
}
|
|
707
|
+
else if (Math.abs(trackElapsed - elapsed) > 1 ||
|
|
708
|
+
trackRate !== rate) {
|
|
709
|
+
return false;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
if (elapsed === undefined)
|
|
713
|
+
return true;
|
|
714
|
+
if (!segments.length)
|
|
715
|
+
return false;
|
|
716
|
+
for (const { offset, end, isDone } of segments) {
|
|
717
|
+
const local = elapsed - offset;
|
|
718
|
+
if (end < Infinity &&
|
|
719
|
+
(isDone ? local < end - 1 : local < 0 || local >= end)) {
|
|
720
|
+
return false;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
for (const { animation, offset, isDone } of segments) {
|
|
724
|
+
if (isDone)
|
|
725
|
+
continue;
|
|
726
|
+
const local = elapsed - offset;
|
|
727
|
+
if (rate) {
|
|
728
|
+
animation.playbackRate = rate;
|
|
729
|
+
animation.startTime = now - local / rate;
|
|
730
|
+
}
|
|
731
|
+
else {
|
|
732
|
+
animation.pause();
|
|
733
|
+
animation.currentTime = local;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
return true;
|
|
737
|
+
};
|
|
738
|
+
const build = () => {
|
|
739
|
+
const now = time.now();
|
|
740
|
+
/**
|
|
741
|
+
* Finish the animations that have reached their end, which the
|
|
742
|
+
* WAAPI animation has already rendered.
|
|
743
|
+
*/
|
|
744
|
+
if (!group.isJS) {
|
|
745
|
+
group.isFinishing = true;
|
|
746
|
+
tracks.forEach((track) => {
|
|
747
|
+
const [trackEnd, trackCycle] = track.timing();
|
|
748
|
+
if (track.state === "running" &&
|
|
749
|
+
track.speed > 0 &&
|
|
750
|
+
!trackCycle &&
|
|
751
|
+
trackEnd <= now + 1) {
|
|
752
|
+
track.complete();
|
|
753
|
+
/**
|
|
754
|
+
* From now it's checked for outside writes like any
|
|
755
|
+
* other static transform. If the WAAPI animation never
|
|
756
|
+
* rendered it (it was instant), rebuild with its value.
|
|
757
|
+
*/
|
|
758
|
+
statics[track.name] = read(owner, track.name);
|
|
759
|
+
built.has(track) || (needsBuild = true);
|
|
760
|
+
}
|
|
761
|
+
});
|
|
762
|
+
group.isFinishing = false;
|
|
763
|
+
}
|
|
764
|
+
isScheduled = false;
|
|
765
|
+
tracks.forEach(({ state }, track) => {
|
|
766
|
+
if (state === "idle" || state === "finished")
|
|
767
|
+
tracks.delete(track);
|
|
768
|
+
});
|
|
769
|
+
if (!tracks.size) {
|
|
770
|
+
transformGroups.delete(element);
|
|
771
|
+
group.isJS || stop();
|
|
772
|
+
return;
|
|
773
|
+
}
|
|
774
|
+
for (const track of tracks) {
|
|
775
|
+
if (!track.canAccelerate)
|
|
776
|
+
return group.demote(true);
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Everything that kept the group on the main thread has finished,
|
|
780
|
+
* so move the remaining animations back to the compositor.
|
|
781
|
+
*/
|
|
782
|
+
if (group.isJS) {
|
|
783
|
+
group.isJS = false;
|
|
784
|
+
needsBuild = true;
|
|
785
|
+
tracks.forEach((track) => track.js?.stop());
|
|
786
|
+
}
|
|
787
|
+
const shouldBuild = needsBuild;
|
|
788
|
+
const churned = hasChurned;
|
|
789
|
+
needsBuild = hasChurned = false;
|
|
790
|
+
if (shouldBuild || !retime(now)) {
|
|
791
|
+
if (churned) {
|
|
792
|
+
if (now - lastChurn < churnWindow)
|
|
793
|
+
return group.demote();
|
|
794
|
+
lastChurn = now;
|
|
795
|
+
}
|
|
796
|
+
if (!animate(now))
|
|
797
|
+
return group.demote();
|
|
798
|
+
}
|
|
799
|
+
};
|
|
800
|
+
const group = {
|
|
801
|
+
tracks,
|
|
802
|
+
isJS: false,
|
|
803
|
+
isFinishing: false,
|
|
804
|
+
schedule(build_, isChurn) {
|
|
805
|
+
needsBuild || (needsBuild = build_);
|
|
806
|
+
hasChurned || (hasChurned = !!isChurn);
|
|
807
|
+
/**
|
|
808
|
+
* Build in this frame's render step, so it's in place before
|
|
809
|
+
* anything reads the DOM in postRender, or straight after the
|
|
810
|
+
* current task outside of a frame.
|
|
811
|
+
*/
|
|
812
|
+
if (!isScheduled) {
|
|
813
|
+
isScheduled = true;
|
|
814
|
+
frameData.isProcessing
|
|
815
|
+
? frame.render(build, false, true)
|
|
816
|
+
: queueMicrotask(build);
|
|
817
|
+
}
|
|
818
|
+
},
|
|
819
|
+
/**
|
|
820
|
+
* An outside write. Rebuild once, but if it keeps happening, move
|
|
821
|
+
* to the main thread.
|
|
822
|
+
*/
|
|
823
|
+
check() {
|
|
824
|
+
if (group.isJS)
|
|
825
|
+
return;
|
|
826
|
+
for (const key in statics) {
|
|
827
|
+
if (read(owner, key) !== statics[key] && !has(key)) {
|
|
828
|
+
return group.schedule(true, true);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
},
|
|
832
|
+
demote(canReturn) {
|
|
833
|
+
if (group.isJS)
|
|
834
|
+
return;
|
|
835
|
+
/**
|
|
836
|
+
* Unless only another animation is holding the group on the
|
|
837
|
+
* main thread, these animations stay there until they end.
|
|
838
|
+
*/
|
|
839
|
+
tracks.forEach((track) => {
|
|
840
|
+
track.isAccelerated() && track.sync();
|
|
841
|
+
canReturn || (track.canAccelerate = false);
|
|
842
|
+
});
|
|
843
|
+
group.isJS = true;
|
|
844
|
+
built = new Map();
|
|
845
|
+
stop();
|
|
846
|
+
tracks.forEach((track) => track.state === "running" && track.js?.start());
|
|
847
|
+
},
|
|
848
|
+
};
|
|
849
|
+
transformGroups.set(element, group);
|
|
850
|
+
return group;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
export { TransformAnimation, canGroupTransform };
|
|
854
|
+
//# sourceMappingURL=TransformAnimation.mjs.map
|