motion-dom 12.4.11 → 12.6.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 +717 -38
- package/dist/es/frameloop/batcher.mjs +69 -0
- package/dist/es/frameloop/frame.mjs +6 -0
- package/dist/es/frameloop/microtask.mjs +6 -0
- package/dist/es/frameloop/order.mjs +10 -0
- package/dist/es/frameloop/render-step.mjs +92 -0
- package/dist/es/frameloop/sync-time.mjs +31 -0
- package/dist/es/index.mjs +13 -5
- package/dist/es/stats/animation-count.mjs +7 -0
- package/dist/es/stats/buffer.mjs +6 -0
- package/dist/es/stats/index.mjs +113 -0
- package/dist/es/utils/supports/scroll-timeline.mjs +1 -1
- package/dist/es/value/index.mjs +311 -0
- package/dist/es/view/index.mjs +9 -12
- package/dist/es/view/queue.mjs +53 -0
- package/dist/es/view/start.mjs +2 -1
- package/dist/index.d.ts +346 -29
- package/dist/motion-dom.dev.js +717 -38
- package/dist/motion-dom.js +1 -1
- package/package.json +5 -4
package/dist/motion-dom.dev.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.MotionDom = {}, global.MotionUtils));
|
|
5
5
|
})(this, (function (exports, motionUtils) { 'use strict';
|
|
6
6
|
|
|
7
|
-
const supportsScrollTimeline = motionUtils.memo(() => window.ScrollTimeline !== undefined);
|
|
7
|
+
const supportsScrollTimeline = /* @__PURE__ */ motionUtils.memo(() => window.ScrollTimeline !== undefined);
|
|
8
8
|
|
|
9
9
|
class BaseGroupPlaybackControls {
|
|
10
10
|
constructor(animations) {
|
|
@@ -299,6 +299,205 @@
|
|
|
299
299
|
}
|
|
300
300
|
}
|
|
301
301
|
|
|
302
|
+
const stepsOrder = [
|
|
303
|
+
"read", // Read
|
|
304
|
+
"resolveKeyframes", // Write/Read/Write/Read
|
|
305
|
+
"update", // Compute
|
|
306
|
+
"preRender", // Compute
|
|
307
|
+
"render", // Write
|
|
308
|
+
"postRender", // Compute
|
|
309
|
+
];
|
|
310
|
+
|
|
311
|
+
const statsBuffer = {
|
|
312
|
+
value: null,
|
|
313
|
+
addProjectionMetrics: null,
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
function createRenderStep(runNextFrame, stepName) {
|
|
317
|
+
/**
|
|
318
|
+
* We create and reuse two queues, one to queue jobs for the current frame
|
|
319
|
+
* and one for the next. We reuse to avoid triggering GC after x frames.
|
|
320
|
+
*/
|
|
321
|
+
let thisFrame = new Set();
|
|
322
|
+
let nextFrame = new Set();
|
|
323
|
+
/**
|
|
324
|
+
* Track whether we're currently processing jobs in this step. This way
|
|
325
|
+
* we can decide whether to schedule new jobs for this frame or next.
|
|
326
|
+
*/
|
|
327
|
+
let isProcessing = false;
|
|
328
|
+
let flushNextFrame = false;
|
|
329
|
+
/**
|
|
330
|
+
* A set of processes which were marked keepAlive when scheduled.
|
|
331
|
+
*/
|
|
332
|
+
const toKeepAlive = new WeakSet();
|
|
333
|
+
let latestFrameData = {
|
|
334
|
+
delta: 0.0,
|
|
335
|
+
timestamp: 0.0,
|
|
336
|
+
isProcessing: false,
|
|
337
|
+
};
|
|
338
|
+
let numCalls = 0;
|
|
339
|
+
function triggerCallback(callback) {
|
|
340
|
+
if (toKeepAlive.has(callback)) {
|
|
341
|
+
step.schedule(callback);
|
|
342
|
+
runNextFrame();
|
|
343
|
+
}
|
|
344
|
+
numCalls++;
|
|
345
|
+
callback(latestFrameData);
|
|
346
|
+
}
|
|
347
|
+
const step = {
|
|
348
|
+
/**
|
|
349
|
+
* Schedule a process to run on the next frame.
|
|
350
|
+
*/
|
|
351
|
+
schedule: (callback, keepAlive = false, immediate = false) => {
|
|
352
|
+
const addToCurrentFrame = immediate && isProcessing;
|
|
353
|
+
const queue = addToCurrentFrame ? thisFrame : nextFrame;
|
|
354
|
+
if (keepAlive)
|
|
355
|
+
toKeepAlive.add(callback);
|
|
356
|
+
if (!queue.has(callback))
|
|
357
|
+
queue.add(callback);
|
|
358
|
+
return callback;
|
|
359
|
+
},
|
|
360
|
+
/**
|
|
361
|
+
* Cancel the provided callback from running on the next frame.
|
|
362
|
+
*/
|
|
363
|
+
cancel: (callback) => {
|
|
364
|
+
nextFrame.delete(callback);
|
|
365
|
+
toKeepAlive.delete(callback);
|
|
366
|
+
},
|
|
367
|
+
/**
|
|
368
|
+
* Execute all schedule callbacks.
|
|
369
|
+
*/
|
|
370
|
+
process: (frameData) => {
|
|
371
|
+
latestFrameData = frameData;
|
|
372
|
+
/**
|
|
373
|
+
* If we're already processing we've probably been triggered by a flushSync
|
|
374
|
+
* inside an existing process. Instead of executing, mark flushNextFrame
|
|
375
|
+
* as true and ensure we flush the following frame at the end of this one.
|
|
376
|
+
*/
|
|
377
|
+
if (isProcessing) {
|
|
378
|
+
flushNextFrame = true;
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
isProcessing = true;
|
|
382
|
+
[thisFrame, nextFrame] = [nextFrame, thisFrame];
|
|
383
|
+
// Execute this frame
|
|
384
|
+
thisFrame.forEach(triggerCallback);
|
|
385
|
+
/**
|
|
386
|
+
* If we're recording stats then
|
|
387
|
+
*/
|
|
388
|
+
if (stepName && statsBuffer.value) {
|
|
389
|
+
statsBuffer.value.frameloop[stepName].push(numCalls);
|
|
390
|
+
}
|
|
391
|
+
numCalls = 0;
|
|
392
|
+
// Clear the frame so no callbacks remain. This is to avoid
|
|
393
|
+
// memory leaks should this render step not run for a while.
|
|
394
|
+
thisFrame.clear();
|
|
395
|
+
isProcessing = false;
|
|
396
|
+
if (flushNextFrame) {
|
|
397
|
+
flushNextFrame = false;
|
|
398
|
+
step.process(frameData);
|
|
399
|
+
}
|
|
400
|
+
},
|
|
401
|
+
};
|
|
402
|
+
return step;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const maxElapsed = 40;
|
|
406
|
+
function createRenderBatcher(scheduleNextBatch, allowKeepAlive) {
|
|
407
|
+
let runNextFrame = false;
|
|
408
|
+
let useDefaultElapsed = true;
|
|
409
|
+
const state = {
|
|
410
|
+
delta: 0.0,
|
|
411
|
+
timestamp: 0.0,
|
|
412
|
+
isProcessing: false,
|
|
413
|
+
};
|
|
414
|
+
const flagRunNextFrame = () => (runNextFrame = true);
|
|
415
|
+
const steps = stepsOrder.reduce((acc, key) => {
|
|
416
|
+
acc[key] = createRenderStep(flagRunNextFrame, allowKeepAlive ? key : undefined);
|
|
417
|
+
return acc;
|
|
418
|
+
}, {});
|
|
419
|
+
const { read, resolveKeyframes, update, preRender, render, postRender } = steps;
|
|
420
|
+
const processBatch = () => {
|
|
421
|
+
const timestamp = motionUtils.MotionGlobalConfig.useManualTiming
|
|
422
|
+
? state.timestamp
|
|
423
|
+
: performance.now();
|
|
424
|
+
runNextFrame = false;
|
|
425
|
+
if (!motionUtils.MotionGlobalConfig.useManualTiming) {
|
|
426
|
+
state.delta = useDefaultElapsed
|
|
427
|
+
? 1000 / 60
|
|
428
|
+
: Math.max(Math.min(timestamp - state.timestamp, maxElapsed), 1);
|
|
429
|
+
}
|
|
430
|
+
state.timestamp = timestamp;
|
|
431
|
+
state.isProcessing = true;
|
|
432
|
+
// Unrolled render loop for better per-frame performance
|
|
433
|
+
read.process(state);
|
|
434
|
+
resolveKeyframes.process(state);
|
|
435
|
+
update.process(state);
|
|
436
|
+
preRender.process(state);
|
|
437
|
+
render.process(state);
|
|
438
|
+
postRender.process(state);
|
|
439
|
+
state.isProcessing = false;
|
|
440
|
+
if (runNextFrame && allowKeepAlive) {
|
|
441
|
+
useDefaultElapsed = false;
|
|
442
|
+
scheduleNextBatch(processBatch);
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
const wake = () => {
|
|
446
|
+
runNextFrame = true;
|
|
447
|
+
useDefaultElapsed = true;
|
|
448
|
+
if (!state.isProcessing) {
|
|
449
|
+
scheduleNextBatch(processBatch);
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
const schedule = stepsOrder.reduce((acc, key) => {
|
|
453
|
+
const step = steps[key];
|
|
454
|
+
acc[key] = (process, keepAlive = false, immediate = false) => {
|
|
455
|
+
if (!runNextFrame)
|
|
456
|
+
wake();
|
|
457
|
+
return step.schedule(process, keepAlive, immediate);
|
|
458
|
+
};
|
|
459
|
+
return acc;
|
|
460
|
+
}, {});
|
|
461
|
+
const cancel = (process) => {
|
|
462
|
+
for (let i = 0; i < stepsOrder.length; i++) {
|
|
463
|
+
steps[stepsOrder[i]].cancel(process);
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
return { schedule, cancel, state, steps };
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const { schedule: frame, cancel: cancelFrame, state: frameData, steps: frameSteps, } = /* @__PURE__ */ createRenderBatcher(typeof requestAnimationFrame !== "undefined" ? requestAnimationFrame : motionUtils.noop, true);
|
|
470
|
+
|
|
471
|
+
const { schedule: microtask, cancel: cancelMicrotask } =
|
|
472
|
+
/* @__PURE__ */ createRenderBatcher(queueMicrotask, false);
|
|
473
|
+
|
|
474
|
+
let now;
|
|
475
|
+
function clearTime() {
|
|
476
|
+
now = undefined;
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* An eventloop-synchronous alternative to performance.now().
|
|
480
|
+
*
|
|
481
|
+
* Ensures that time measurements remain consistent within a synchronous context.
|
|
482
|
+
* Usually calling performance.now() twice within the same synchronous context
|
|
483
|
+
* will return different values which isn't useful for animations when we're usually
|
|
484
|
+
* trying to sync animations to the same frame.
|
|
485
|
+
*/
|
|
486
|
+
const time = {
|
|
487
|
+
now: () => {
|
|
488
|
+
if (now === undefined) {
|
|
489
|
+
time.set(frameData.isProcessing || motionUtils.MotionGlobalConfig.useManualTiming
|
|
490
|
+
? frameData.timestamp
|
|
491
|
+
: performance.now());
|
|
492
|
+
}
|
|
493
|
+
return now;
|
|
494
|
+
},
|
|
495
|
+
set: (newTime) => {
|
|
496
|
+
now = newTime;
|
|
497
|
+
queueMicrotask(clearTime);
|
|
498
|
+
},
|
|
499
|
+
};
|
|
500
|
+
|
|
302
501
|
const isDragging = {
|
|
303
502
|
x: false,
|
|
304
503
|
y: false,
|
|
@@ -307,6 +506,31 @@
|
|
|
307
506
|
return isDragging.x || isDragging.y;
|
|
308
507
|
}
|
|
309
508
|
|
|
509
|
+
function setDragLock(axis) {
|
|
510
|
+
if (axis === "x" || axis === "y") {
|
|
511
|
+
if (isDragging[axis]) {
|
|
512
|
+
return null;
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
isDragging[axis] = true;
|
|
516
|
+
return () => {
|
|
517
|
+
isDragging[axis] = false;
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
if (isDragging.x || isDragging.y) {
|
|
523
|
+
return null;
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
isDragging.x = isDragging.y = true;
|
|
527
|
+
return () => {
|
|
528
|
+
isDragging.x = isDragging.y = false;
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
310
534
|
function resolveElements(elementOrSelector, scope, selectorCache) {
|
|
311
535
|
var _a;
|
|
312
536
|
if (elementOrSelector instanceof EventTarget) {
|
|
@@ -567,6 +791,426 @@
|
|
|
567
791
|
event.clientY > rect.bottom);
|
|
568
792
|
}
|
|
569
793
|
|
|
794
|
+
const activeAnimations = {
|
|
795
|
+
layout: 0,
|
|
796
|
+
mainThread: 0,
|
|
797
|
+
waapi: 0,
|
|
798
|
+
};
|
|
799
|
+
|
|
800
|
+
function record() {
|
|
801
|
+
const { value } = statsBuffer;
|
|
802
|
+
if (value === null) {
|
|
803
|
+
cancelFrame(record);
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
value.frameloop.rate.push(frameData.delta);
|
|
807
|
+
value.animations.mainThread.push(activeAnimations.mainThread);
|
|
808
|
+
value.animations.waapi.push(activeAnimations.waapi);
|
|
809
|
+
value.animations.layout.push(activeAnimations.layout);
|
|
810
|
+
}
|
|
811
|
+
function mean(values) {
|
|
812
|
+
return values.reduce((acc, value) => acc + value, 0) / values.length;
|
|
813
|
+
}
|
|
814
|
+
function summarise(values, calcAverage = mean) {
|
|
815
|
+
if (values.length === 0) {
|
|
816
|
+
return {
|
|
817
|
+
min: 0,
|
|
818
|
+
max: 0,
|
|
819
|
+
avg: 0,
|
|
820
|
+
};
|
|
821
|
+
}
|
|
822
|
+
return {
|
|
823
|
+
min: Math.min(...values),
|
|
824
|
+
max: Math.max(...values),
|
|
825
|
+
avg: calcAverage(values),
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
const msToFps = (ms) => Math.round(1000 / ms);
|
|
829
|
+
function clearStatsBuffer() {
|
|
830
|
+
statsBuffer.value = null;
|
|
831
|
+
statsBuffer.addProjectionMetrics = null;
|
|
832
|
+
}
|
|
833
|
+
function reportStats() {
|
|
834
|
+
const { value } = statsBuffer;
|
|
835
|
+
if (!value) {
|
|
836
|
+
throw new Error("Stats are not being measured");
|
|
837
|
+
}
|
|
838
|
+
clearStatsBuffer();
|
|
839
|
+
cancelFrame(record);
|
|
840
|
+
const summary = {
|
|
841
|
+
frameloop: {
|
|
842
|
+
rate: summarise(value.frameloop.rate),
|
|
843
|
+
read: summarise(value.frameloop.read),
|
|
844
|
+
resolveKeyframes: summarise(value.frameloop.resolveKeyframes),
|
|
845
|
+
update: summarise(value.frameloop.update),
|
|
846
|
+
preRender: summarise(value.frameloop.preRender),
|
|
847
|
+
render: summarise(value.frameloop.render),
|
|
848
|
+
postRender: summarise(value.frameloop.postRender),
|
|
849
|
+
},
|
|
850
|
+
animations: {
|
|
851
|
+
mainThread: summarise(value.animations.mainThread),
|
|
852
|
+
waapi: summarise(value.animations.waapi),
|
|
853
|
+
layout: summarise(value.animations.layout),
|
|
854
|
+
},
|
|
855
|
+
layoutProjection: {
|
|
856
|
+
nodes: summarise(value.layoutProjection.nodes),
|
|
857
|
+
calculatedTargetDeltas: summarise(value.layoutProjection.calculatedTargetDeltas),
|
|
858
|
+
calculatedProjections: summarise(value.layoutProjection.calculatedProjections),
|
|
859
|
+
},
|
|
860
|
+
};
|
|
861
|
+
/**
|
|
862
|
+
* Convert the rate to FPS
|
|
863
|
+
*/
|
|
864
|
+
const { rate } = summary.frameloop;
|
|
865
|
+
rate.min = msToFps(rate.min);
|
|
866
|
+
rate.max = msToFps(rate.max);
|
|
867
|
+
rate.avg = msToFps(rate.avg);
|
|
868
|
+
[rate.min, rate.max] = [rate.max, rate.min];
|
|
869
|
+
return summary;
|
|
870
|
+
}
|
|
871
|
+
function recordStats() {
|
|
872
|
+
if (statsBuffer.value) {
|
|
873
|
+
clearStatsBuffer();
|
|
874
|
+
throw new Error("Stats are already being measured");
|
|
875
|
+
}
|
|
876
|
+
const newStatsBuffer = statsBuffer;
|
|
877
|
+
newStatsBuffer.value = {
|
|
878
|
+
frameloop: {
|
|
879
|
+
rate: [],
|
|
880
|
+
read: [],
|
|
881
|
+
resolveKeyframes: [],
|
|
882
|
+
update: [],
|
|
883
|
+
preRender: [],
|
|
884
|
+
render: [],
|
|
885
|
+
postRender: [],
|
|
886
|
+
},
|
|
887
|
+
animations: {
|
|
888
|
+
mainThread: [],
|
|
889
|
+
waapi: [],
|
|
890
|
+
layout: [],
|
|
891
|
+
},
|
|
892
|
+
layoutProjection: {
|
|
893
|
+
nodes: [],
|
|
894
|
+
calculatedTargetDeltas: [],
|
|
895
|
+
calculatedProjections: [],
|
|
896
|
+
},
|
|
897
|
+
};
|
|
898
|
+
newStatsBuffer.addProjectionMetrics = (metrics) => {
|
|
899
|
+
const { layoutProjection } = newStatsBuffer.value;
|
|
900
|
+
layoutProjection.nodes.push(metrics.nodes);
|
|
901
|
+
layoutProjection.calculatedTargetDeltas.push(metrics.calculatedTargetDeltas);
|
|
902
|
+
layoutProjection.calculatedProjections.push(metrics.calculatedProjections);
|
|
903
|
+
};
|
|
904
|
+
frame.postRender(record, true);
|
|
905
|
+
return reportStats;
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
/**
|
|
909
|
+
* Maximum time between the value of two frames, beyond which we
|
|
910
|
+
* assume the velocity has since been 0.
|
|
911
|
+
*/
|
|
912
|
+
const MAX_VELOCITY_DELTA = 30;
|
|
913
|
+
const isFloat = (value) => {
|
|
914
|
+
return !isNaN(parseFloat(value));
|
|
915
|
+
};
|
|
916
|
+
const collectMotionValues = {
|
|
917
|
+
current: undefined,
|
|
918
|
+
};
|
|
919
|
+
/**
|
|
920
|
+
* `MotionValue` is used to track the state and velocity of motion values.
|
|
921
|
+
*
|
|
922
|
+
* @public
|
|
923
|
+
*/
|
|
924
|
+
class MotionValue {
|
|
925
|
+
/**
|
|
926
|
+
* @param init - The initiating value
|
|
927
|
+
* @param config - Optional configuration options
|
|
928
|
+
*
|
|
929
|
+
* - `transformer`: A function to transform incoming values with.
|
|
930
|
+
*/
|
|
931
|
+
constructor(init, options = {}) {
|
|
932
|
+
/**
|
|
933
|
+
* This will be replaced by the build step with the latest version number.
|
|
934
|
+
* When MotionValues are provided to motion components, warn if versions are mixed.
|
|
935
|
+
*/
|
|
936
|
+
this.version = "12.6.0";
|
|
937
|
+
/**
|
|
938
|
+
* Tracks whether this value can output a velocity. Currently this is only true
|
|
939
|
+
* if the value is numerical, but we might be able to widen the scope here and support
|
|
940
|
+
* other value types.
|
|
941
|
+
*
|
|
942
|
+
* @internal
|
|
943
|
+
*/
|
|
944
|
+
this.canTrackVelocity = null;
|
|
945
|
+
/**
|
|
946
|
+
* An object containing a SubscriptionManager for each active event.
|
|
947
|
+
*/
|
|
948
|
+
this.events = {};
|
|
949
|
+
this.updateAndNotify = (v, render = true) => {
|
|
950
|
+
const currentTime = time.now();
|
|
951
|
+
/**
|
|
952
|
+
* If we're updating the value during another frame or eventloop
|
|
953
|
+
* than the previous frame, then the we set the previous frame value
|
|
954
|
+
* to current.
|
|
955
|
+
*/
|
|
956
|
+
if (this.updatedAt !== currentTime) {
|
|
957
|
+
this.setPrevFrameValue();
|
|
958
|
+
}
|
|
959
|
+
this.prev = this.current;
|
|
960
|
+
this.setCurrent(v);
|
|
961
|
+
// Update update subscribers
|
|
962
|
+
if (this.current !== this.prev && this.events.change) {
|
|
963
|
+
this.events.change.notify(this.current);
|
|
964
|
+
}
|
|
965
|
+
// Update render subscribers
|
|
966
|
+
if (render && this.events.renderRequest) {
|
|
967
|
+
this.events.renderRequest.notify(this.current);
|
|
968
|
+
}
|
|
969
|
+
};
|
|
970
|
+
this.hasAnimated = false;
|
|
971
|
+
this.setCurrent(init);
|
|
972
|
+
this.owner = options.owner;
|
|
973
|
+
}
|
|
974
|
+
setCurrent(current) {
|
|
975
|
+
this.current = current;
|
|
976
|
+
this.updatedAt = time.now();
|
|
977
|
+
if (this.canTrackVelocity === null && current !== undefined) {
|
|
978
|
+
this.canTrackVelocity = isFloat(this.current);
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
setPrevFrameValue(prevFrameValue = this.current) {
|
|
982
|
+
this.prevFrameValue = prevFrameValue;
|
|
983
|
+
this.prevUpdatedAt = this.updatedAt;
|
|
984
|
+
}
|
|
985
|
+
/**
|
|
986
|
+
* Adds a function that will be notified when the `MotionValue` is updated.
|
|
987
|
+
*
|
|
988
|
+
* It returns a function that, when called, will cancel the subscription.
|
|
989
|
+
*
|
|
990
|
+
* When calling `onChange` inside a React component, it should be wrapped with the
|
|
991
|
+
* `useEffect` hook. As it returns an unsubscribe function, this should be returned
|
|
992
|
+
* from the `useEffect` function to ensure you don't add duplicate subscribers..
|
|
993
|
+
*
|
|
994
|
+
* ```jsx
|
|
995
|
+
* export const MyComponent = () => {
|
|
996
|
+
* const x = useMotionValue(0)
|
|
997
|
+
* const y = useMotionValue(0)
|
|
998
|
+
* const opacity = useMotionValue(1)
|
|
999
|
+
*
|
|
1000
|
+
* useEffect(() => {
|
|
1001
|
+
* function updateOpacity() {
|
|
1002
|
+
* const maxXY = Math.max(x.get(), y.get())
|
|
1003
|
+
* const newOpacity = transform(maxXY, [0, 100], [1, 0])
|
|
1004
|
+
* opacity.set(newOpacity)
|
|
1005
|
+
* }
|
|
1006
|
+
*
|
|
1007
|
+
* const unsubscribeX = x.on("change", updateOpacity)
|
|
1008
|
+
* const unsubscribeY = y.on("change", updateOpacity)
|
|
1009
|
+
*
|
|
1010
|
+
* return () => {
|
|
1011
|
+
* unsubscribeX()
|
|
1012
|
+
* unsubscribeY()
|
|
1013
|
+
* }
|
|
1014
|
+
* }, [])
|
|
1015
|
+
*
|
|
1016
|
+
* return <motion.div style={{ x }} />
|
|
1017
|
+
* }
|
|
1018
|
+
* ```
|
|
1019
|
+
*
|
|
1020
|
+
* @param subscriber - A function that receives the latest value.
|
|
1021
|
+
* @returns A function that, when called, will cancel this subscription.
|
|
1022
|
+
*
|
|
1023
|
+
* @deprecated
|
|
1024
|
+
*/
|
|
1025
|
+
onChange(subscription) {
|
|
1026
|
+
{
|
|
1027
|
+
motionUtils.warnOnce(false, `value.onChange(callback) is deprecated. Switch to value.on("change", callback).`);
|
|
1028
|
+
}
|
|
1029
|
+
return this.on("change", subscription);
|
|
1030
|
+
}
|
|
1031
|
+
on(eventName, callback) {
|
|
1032
|
+
if (!this.events[eventName]) {
|
|
1033
|
+
this.events[eventName] = new motionUtils.SubscriptionManager();
|
|
1034
|
+
}
|
|
1035
|
+
const unsubscribe = this.events[eventName].add(callback);
|
|
1036
|
+
if (eventName === "change") {
|
|
1037
|
+
return () => {
|
|
1038
|
+
unsubscribe();
|
|
1039
|
+
/**
|
|
1040
|
+
* If we have no more change listeners by the start
|
|
1041
|
+
* of the next frame, stop active animations.
|
|
1042
|
+
*/
|
|
1043
|
+
frame.read(() => {
|
|
1044
|
+
if (!this.events.change.getSize()) {
|
|
1045
|
+
this.stop();
|
|
1046
|
+
}
|
|
1047
|
+
});
|
|
1048
|
+
};
|
|
1049
|
+
}
|
|
1050
|
+
return unsubscribe;
|
|
1051
|
+
}
|
|
1052
|
+
clearListeners() {
|
|
1053
|
+
for (const eventManagers in this.events) {
|
|
1054
|
+
this.events[eventManagers].clear();
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
/**
|
|
1058
|
+
* Attaches a passive effect to the `MotionValue`.
|
|
1059
|
+
*/
|
|
1060
|
+
attach(passiveEffect, stopPassiveEffect) {
|
|
1061
|
+
this.passiveEffect = passiveEffect;
|
|
1062
|
+
this.stopPassiveEffect = stopPassiveEffect;
|
|
1063
|
+
}
|
|
1064
|
+
/**
|
|
1065
|
+
* Sets the state of the `MotionValue`.
|
|
1066
|
+
*
|
|
1067
|
+
* @remarks
|
|
1068
|
+
*
|
|
1069
|
+
* ```jsx
|
|
1070
|
+
* const x = useMotionValue(0)
|
|
1071
|
+
* x.set(10)
|
|
1072
|
+
* ```
|
|
1073
|
+
*
|
|
1074
|
+
* @param latest - Latest value to set.
|
|
1075
|
+
* @param render - Whether to notify render subscribers. Defaults to `true`
|
|
1076
|
+
*
|
|
1077
|
+
* @public
|
|
1078
|
+
*/
|
|
1079
|
+
set(v, render = true) {
|
|
1080
|
+
if (!render || !this.passiveEffect) {
|
|
1081
|
+
this.updateAndNotify(v, render);
|
|
1082
|
+
}
|
|
1083
|
+
else {
|
|
1084
|
+
this.passiveEffect(v, this.updateAndNotify);
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
setWithVelocity(prev, current, delta) {
|
|
1088
|
+
this.set(current);
|
|
1089
|
+
this.prev = undefined;
|
|
1090
|
+
this.prevFrameValue = prev;
|
|
1091
|
+
this.prevUpdatedAt = this.updatedAt - delta;
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* Set the state of the `MotionValue`, stopping any active animations,
|
|
1095
|
+
* effects, and resets velocity to `0`.
|
|
1096
|
+
*/
|
|
1097
|
+
jump(v, endAnimation = true) {
|
|
1098
|
+
this.updateAndNotify(v);
|
|
1099
|
+
this.prev = v;
|
|
1100
|
+
this.prevUpdatedAt = this.prevFrameValue = undefined;
|
|
1101
|
+
endAnimation && this.stop();
|
|
1102
|
+
if (this.stopPassiveEffect)
|
|
1103
|
+
this.stopPassiveEffect();
|
|
1104
|
+
}
|
|
1105
|
+
/**
|
|
1106
|
+
* Returns the latest state of `MotionValue`
|
|
1107
|
+
*
|
|
1108
|
+
* @returns - The latest state of `MotionValue`
|
|
1109
|
+
*
|
|
1110
|
+
* @public
|
|
1111
|
+
*/
|
|
1112
|
+
get() {
|
|
1113
|
+
if (collectMotionValues.current) {
|
|
1114
|
+
collectMotionValues.current.push(this);
|
|
1115
|
+
}
|
|
1116
|
+
return this.current;
|
|
1117
|
+
}
|
|
1118
|
+
/**
|
|
1119
|
+
* @public
|
|
1120
|
+
*/
|
|
1121
|
+
getPrevious() {
|
|
1122
|
+
return this.prev;
|
|
1123
|
+
}
|
|
1124
|
+
/**
|
|
1125
|
+
* Returns the latest velocity of `MotionValue`
|
|
1126
|
+
*
|
|
1127
|
+
* @returns - The latest velocity of `MotionValue`. Returns `0` if the state is non-numerical.
|
|
1128
|
+
*
|
|
1129
|
+
* @public
|
|
1130
|
+
*/
|
|
1131
|
+
getVelocity() {
|
|
1132
|
+
const currentTime = time.now();
|
|
1133
|
+
if (!this.canTrackVelocity ||
|
|
1134
|
+
this.prevFrameValue === undefined ||
|
|
1135
|
+
currentTime - this.updatedAt > MAX_VELOCITY_DELTA) {
|
|
1136
|
+
return 0;
|
|
1137
|
+
}
|
|
1138
|
+
const delta = Math.min(this.updatedAt - this.prevUpdatedAt, MAX_VELOCITY_DELTA);
|
|
1139
|
+
// Casts because of parseFloat's poor typing
|
|
1140
|
+
return motionUtils.velocityPerSecond(parseFloat(this.current) -
|
|
1141
|
+
parseFloat(this.prevFrameValue), delta);
|
|
1142
|
+
}
|
|
1143
|
+
/**
|
|
1144
|
+
* Registers a new animation to control this `MotionValue`. Only one
|
|
1145
|
+
* animation can drive a `MotionValue` at one time.
|
|
1146
|
+
*
|
|
1147
|
+
* ```jsx
|
|
1148
|
+
* value.start()
|
|
1149
|
+
* ```
|
|
1150
|
+
*
|
|
1151
|
+
* @param animation - A function that starts the provided animation
|
|
1152
|
+
*/
|
|
1153
|
+
start(startAnimation) {
|
|
1154
|
+
this.stop();
|
|
1155
|
+
return new Promise((resolve) => {
|
|
1156
|
+
this.hasAnimated = true;
|
|
1157
|
+
this.animation = startAnimation(resolve);
|
|
1158
|
+
if (this.events.animationStart) {
|
|
1159
|
+
this.events.animationStart.notify();
|
|
1160
|
+
}
|
|
1161
|
+
}).then(() => {
|
|
1162
|
+
if (this.events.animationComplete) {
|
|
1163
|
+
this.events.animationComplete.notify();
|
|
1164
|
+
}
|
|
1165
|
+
this.clearAnimation();
|
|
1166
|
+
});
|
|
1167
|
+
}
|
|
1168
|
+
/**
|
|
1169
|
+
* Stop the currently active animation.
|
|
1170
|
+
*
|
|
1171
|
+
* @public
|
|
1172
|
+
*/
|
|
1173
|
+
stop() {
|
|
1174
|
+
if (this.animation) {
|
|
1175
|
+
this.animation.stop();
|
|
1176
|
+
if (this.events.animationCancel) {
|
|
1177
|
+
this.events.animationCancel.notify();
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
this.clearAnimation();
|
|
1181
|
+
}
|
|
1182
|
+
/**
|
|
1183
|
+
* Returns `true` if this value is currently animating.
|
|
1184
|
+
*
|
|
1185
|
+
* @public
|
|
1186
|
+
*/
|
|
1187
|
+
isAnimating() {
|
|
1188
|
+
return !!this.animation;
|
|
1189
|
+
}
|
|
1190
|
+
clearAnimation() {
|
|
1191
|
+
delete this.animation;
|
|
1192
|
+
}
|
|
1193
|
+
/**
|
|
1194
|
+
* Destroy and clean up subscribers to this `MotionValue`.
|
|
1195
|
+
*
|
|
1196
|
+
* The `MotionValue` hooks like `useMotionValue` and `useTransform` automatically
|
|
1197
|
+
* handle the lifecycle of the returned `MotionValue`, so this method is only necessary if you've manually
|
|
1198
|
+
* created a `MotionValue` via the `motionValue` function.
|
|
1199
|
+
*
|
|
1200
|
+
* @public
|
|
1201
|
+
*/
|
|
1202
|
+
destroy() {
|
|
1203
|
+
this.clearListeners();
|
|
1204
|
+
this.stop();
|
|
1205
|
+
if (this.stopPassiveEffect) {
|
|
1206
|
+
this.stopPassiveEffect();
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
function motionValue(init, options) {
|
|
1211
|
+
return new MotionValue(init, options);
|
|
1212
|
+
}
|
|
1213
|
+
|
|
570
1214
|
const defaultEasing = "easeOut";
|
|
571
1215
|
function applyGeneratorOptions(options) {
|
|
572
1216
|
var _a;
|
|
@@ -691,7 +1335,8 @@
|
|
|
691
1335
|
}
|
|
692
1336
|
|
|
693
1337
|
const definitionNames = ["layout", "enter", "exit", "new", "old"];
|
|
694
|
-
function startViewAnimation(
|
|
1338
|
+
function startViewAnimation(builder) {
|
|
1339
|
+
const { update, targets, options: defaultOptions } = builder;
|
|
695
1340
|
if (!document.startViewTransition) {
|
|
696
1341
|
return new Promise(async (resolve) => {
|
|
697
1342
|
await update();
|
|
@@ -822,12 +1467,54 @@
|
|
|
822
1467
|
return (_a = target === null || target === void 0 ? void 0 : target[key]) === null || _a === void 0 ? void 0 : _a.keyframes.opacity;
|
|
823
1468
|
}
|
|
824
1469
|
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
1470
|
+
let builders = [];
|
|
1471
|
+
let current = null;
|
|
1472
|
+
function next() {
|
|
1473
|
+
current = null;
|
|
1474
|
+
const [nextBuilder] = builders;
|
|
1475
|
+
if (nextBuilder)
|
|
1476
|
+
start(nextBuilder);
|
|
1477
|
+
}
|
|
1478
|
+
function start(builder) {
|
|
1479
|
+
motionUtils.removeItem(builders, builder);
|
|
1480
|
+
current = builder;
|
|
1481
|
+
startViewAnimation(builder).then((animation) => {
|
|
1482
|
+
builder.notifyReady(animation);
|
|
1483
|
+
animation.finished.finally(next);
|
|
1484
|
+
});
|
|
1485
|
+
}
|
|
1486
|
+
function processQueue() {
|
|
1487
|
+
var _a;
|
|
1488
|
+
/**
|
|
1489
|
+
* Iterate backwards over the builders array. We can ignore the
|
|
1490
|
+
* "wait" animations. If we have an interrupting animation in the
|
|
1491
|
+
* queue then we need to batch all preceeding animations into it.
|
|
1492
|
+
* Currently this only batches the update functions but will also
|
|
1493
|
+
* need to batch the targets.
|
|
1494
|
+
*/
|
|
1495
|
+
for (let i = builders.length - 1; i >= 0; i--) {
|
|
1496
|
+
const builder = builders[i];
|
|
1497
|
+
const { interrupt } = builder.options;
|
|
1498
|
+
if (interrupt === "immediate") {
|
|
1499
|
+
const batchedUpdates = builders.slice(0, i + 1).map((b) => b.update);
|
|
1500
|
+
const remaining = builders.slice(i + 1);
|
|
1501
|
+
builder.update = () => {
|
|
1502
|
+
batchedUpdates.forEach((update) => update());
|
|
1503
|
+
};
|
|
1504
|
+
// Put the current builder at the front, followed by any "wait" builders
|
|
1505
|
+
builders = [builder, ...remaining];
|
|
1506
|
+
break;
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
if (!current || ((_a = builders[0]) === null || _a === void 0 ? void 0 : _a.options.interrupt) === "immediate") {
|
|
1510
|
+
next();
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
function addToQueue(builder) {
|
|
1514
|
+
builders.push(builder);
|
|
1515
|
+
microtask.render(processQueue);
|
|
1516
|
+
}
|
|
1517
|
+
|
|
831
1518
|
class ViewTransitionBuilder {
|
|
832
1519
|
constructor(update, options = {}) {
|
|
833
1520
|
this.currentTarget = "root";
|
|
@@ -836,9 +1523,12 @@
|
|
|
836
1523
|
this.readyPromise = new Promise((resolve) => {
|
|
837
1524
|
this.notifyReady = resolve;
|
|
838
1525
|
});
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
1526
|
+
this.update = update;
|
|
1527
|
+
this.options = {
|
|
1528
|
+
interrupt: "wait",
|
|
1529
|
+
...options,
|
|
1530
|
+
};
|
|
1531
|
+
addToQueue(this);
|
|
842
1532
|
}
|
|
843
1533
|
get(selector) {
|
|
844
1534
|
this.currentTarget = selector;
|
|
@@ -881,43 +1571,28 @@
|
|
|
881
1571
|
return this.readyPromise.then(resolve, reject);
|
|
882
1572
|
}
|
|
883
1573
|
}
|
|
884
|
-
function
|
|
1574
|
+
function animateView(update, defaultOptions = {}) {
|
|
885
1575
|
return new ViewTransitionBuilder(update, defaultOptions);
|
|
886
1576
|
}
|
|
887
1577
|
|
|
888
|
-
function setDragLock(axis) {
|
|
889
|
-
if (axis === "x" || axis === "y") {
|
|
890
|
-
if (isDragging[axis]) {
|
|
891
|
-
return null;
|
|
892
|
-
}
|
|
893
|
-
else {
|
|
894
|
-
isDragging[axis] = true;
|
|
895
|
-
return () => {
|
|
896
|
-
isDragging[axis] = false;
|
|
897
|
-
};
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
else {
|
|
901
|
-
if (isDragging.x || isDragging.y) {
|
|
902
|
-
return null;
|
|
903
|
-
}
|
|
904
|
-
else {
|
|
905
|
-
isDragging.x = isDragging.y = true;
|
|
906
|
-
return () => {
|
|
907
|
-
isDragging.x = isDragging.y = false;
|
|
908
|
-
};
|
|
909
|
-
}
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
|
|
913
1578
|
exports.GroupPlaybackControls = GroupPlaybackControls;
|
|
1579
|
+
exports.MotionValue = MotionValue;
|
|
914
1580
|
exports.NativeAnimationControls = NativeAnimationControls;
|
|
915
1581
|
exports.ViewTransitionBuilder = ViewTransitionBuilder;
|
|
1582
|
+
exports.activeAnimations = activeAnimations;
|
|
1583
|
+
exports.animateView = animateView;
|
|
916
1584
|
exports.attachTimeline = attachTimeline;
|
|
917
1585
|
exports.calcGeneratorDuration = calcGeneratorDuration;
|
|
1586
|
+
exports.cancelFrame = cancelFrame;
|
|
1587
|
+
exports.cancelMicrotask = cancelMicrotask;
|
|
918
1588
|
exports.capturePointer = capturePointer;
|
|
1589
|
+
exports.collectMotionValues = collectMotionValues;
|
|
919
1590
|
exports.createGeneratorEasing = createGeneratorEasing;
|
|
1591
|
+
exports.createRenderBatcher = createRenderBatcher;
|
|
920
1592
|
exports.cubicBezierAsString = cubicBezierAsString;
|
|
1593
|
+
exports.frame = frame;
|
|
1594
|
+
exports.frameData = frameData;
|
|
1595
|
+
exports.frameSteps = frameSteps;
|
|
921
1596
|
exports.generateLinearEasing = generateLinearEasing;
|
|
922
1597
|
exports.getValueTransition = getValueTransition;
|
|
923
1598
|
exports.hover = hover;
|
|
@@ -930,13 +1605,17 @@
|
|
|
930
1605
|
exports.isWaapiSupportedEasing = isWaapiSupportedEasing;
|
|
931
1606
|
exports.mapEasingToNativeEasing = mapEasingToNativeEasing;
|
|
932
1607
|
exports.maxGeneratorDuration = maxGeneratorDuration;
|
|
1608
|
+
exports.microtask = microtask;
|
|
1609
|
+
exports.motionValue = motionValue;
|
|
933
1610
|
exports.press = press;
|
|
1611
|
+
exports.recordStats = recordStats;
|
|
934
1612
|
exports.resolveElements = resolveElements;
|
|
935
1613
|
exports.setDragLock = setDragLock;
|
|
1614
|
+
exports.statsBuffer = statsBuffer;
|
|
936
1615
|
exports.supportedWaapiEasing = supportedWaapiEasing;
|
|
937
1616
|
exports.supportsFlags = supportsFlags;
|
|
938
1617
|
exports.supportsLinearEasing = supportsLinearEasing;
|
|
939
1618
|
exports.supportsScrollTimeline = supportsScrollTimeline;
|
|
940
|
-
exports.
|
|
1619
|
+
exports.time = time;
|
|
941
1620
|
|
|
942
1621
|
}));
|