motion-dom 13.4.4 → 13.5.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 +469 -415
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/animation/FollowAnimation.mjs +1 -4
- package/dist/es/animation/FollowAnimation.mjs.map +1 -1
- package/dist/es/animation/NativeAnimation.mjs +2 -5
- package/dist/es/animation/NativeAnimation.mjs.map +1 -1
- package/dist/es/animation/drivers/frame.mjs +2 -1
- package/dist/es/animation/drivers/frame.mjs.map +1 -1
- package/dist/es/animation/generators/spring.mjs +99 -95
- package/dist/es/animation/generators/spring.mjs.map +1 -1
- package/dist/es/animation/utils/notify-inspector.mjs +1 -1
- package/dist/es/animation/utils/notify-inspector.mjs.map +1 -1
- package/dist/es/frameloop/batcher.mjs +5 -6
- package/dist/es/frameloop/batcher.mjs.map +1 -1
- package/dist/es/frameloop/frame-data.mjs +12 -0
- package/dist/es/frameloop/frame-data.mjs.map +1 -0
- package/dist/es/frameloop/frame.mjs +3 -2
- package/dist/es/frameloop/frame.mjs.map +1 -1
- package/dist/es/frameloop/sync-time.mjs +1 -1
- package/dist/es/frameloop/sync-time.mjs.map +1 -1
- package/dist/es/index.mjs +2 -2
- package/dist/es/layout/LayoutAnimationBuilder.mjs +2 -1
- package/dist/es/layout/LayoutAnimationBuilder.mjs.map +1 -1
- package/dist/es/projection/animation/mix-values.mjs +1 -1
- package/dist/es/projection/animation/mix-values.mjs.map +1 -1
- package/dist/es/projection/node/create-projection-node.mjs +32 -10
- package/dist/es/projection/node/create-projection-node.mjs.map +1 -1
- package/dist/es/projection/styles/default-scale-correctors.mjs +18 -0
- package/dist/es/projection/styles/default-scale-correctors.mjs.map +1 -0
- package/dist/es/projection/styles/scale-correction.mjs +1 -14
- package/dist/es/projection/styles/scale-correction.mjs.map +1 -1
- package/dist/es/projection/utils/each-axis.mjs.map +1 -1
- package/dist/es/render/VisualElement.mjs +12 -2
- package/dist/es/render/VisualElement.mjs.map +1 -1
- package/dist/es/render/svg/SVGVisualElement.mjs +19 -1
- package/dist/es/render/svg/SVGVisualElement.mjs.map +1 -1
- package/dist/es/value/follow-value.mjs +8 -85
- package/dist/es/value/follow-value.mjs.map +1 -1
- package/dist/es/value/spring-value.mjs +8 -3
- package/dist/es/value/spring-value.mjs.map +1 -1
- package/dist/es/value/utils/as-number.mjs +4 -0
- package/dist/es/value/utils/as-number.mjs.map +1 -0
- package/dist/es/value/utils/follow.mjs +94 -0
- package/dist/es/value/utils/follow.mjs.map +1 -0
- package/dist/index.d.ts +31 -22
- package/dist/motion-dom.dev.js +469 -415
- 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 +6 -5
- package/dist/es/scroll/observe.mjs +0 -19
- package/dist/es/scroll/observe.mjs.map +0 -1
package/dist/cjs/index.js
CHANGED
|
@@ -4,170 +4,15 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var motionUtils = require('motion-utils');
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
];
|
|
17
|
-
|
|
18
|
-
function createRenderStep(runNextFrame) {
|
|
19
|
-
/**
|
|
20
|
-
* We create and reuse two queues, one to queue jobs for the current frame
|
|
21
|
-
* and one for the next. We reuse to avoid triggering GC after x frames.
|
|
22
|
-
*/
|
|
23
|
-
let thisFrame = new Set();
|
|
24
|
-
let nextFrame = new Set();
|
|
25
|
-
/**
|
|
26
|
-
* Track whether we're currently processing jobs in this step. This way
|
|
27
|
-
* we can decide whether to schedule new jobs for this frame or next.
|
|
28
|
-
*/
|
|
29
|
-
let isProcessing = false;
|
|
30
|
-
let flushNextFrame = false;
|
|
31
|
-
/**
|
|
32
|
-
* A set of processes which were marked keepAlive when scheduled.
|
|
33
|
-
* A keepAlive process is always also held by a frame queue until
|
|
34
|
-
* it's cancelled, so a Set has the same lifetime semantics as a
|
|
35
|
-
* WeakSet here while being considerably faster to query every frame.
|
|
36
|
-
*/
|
|
37
|
-
const toKeepAlive = new Set();
|
|
38
|
-
let latestFrameData = {
|
|
39
|
-
delta: 0.0,
|
|
40
|
-
timestamp: 0.0,
|
|
41
|
-
isProcessing: false,
|
|
42
|
-
};
|
|
43
|
-
function triggerCallback(callback) {
|
|
44
|
-
if (toKeepAlive.has(callback)) {
|
|
45
|
-
nextFrame.add(callback);
|
|
46
|
-
runNextFrame();
|
|
47
|
-
}
|
|
48
|
-
callback(latestFrameData);
|
|
49
|
-
}
|
|
50
|
-
const step = {
|
|
51
|
-
/**
|
|
52
|
-
* Schedule a process to run on the next frame.
|
|
53
|
-
*/
|
|
54
|
-
schedule: (callback, keepAlive = false, immediate = false) => {
|
|
55
|
-
const addToCurrentFrame = immediate && isProcessing;
|
|
56
|
-
const queue = addToCurrentFrame ? thisFrame : nextFrame;
|
|
57
|
-
if (keepAlive)
|
|
58
|
-
toKeepAlive.add(callback);
|
|
59
|
-
queue.add(callback);
|
|
60
|
-
return callback;
|
|
61
|
-
},
|
|
62
|
-
/**
|
|
63
|
-
* Cancel the provided callback from running on the next frame.
|
|
64
|
-
*/
|
|
65
|
-
cancel: (callback) => {
|
|
66
|
-
nextFrame.delete(callback);
|
|
67
|
-
toKeepAlive.delete(callback);
|
|
68
|
-
},
|
|
69
|
-
/**
|
|
70
|
-
* Execute all schedule callbacks.
|
|
71
|
-
*/
|
|
72
|
-
process: (frameData) => {
|
|
73
|
-
latestFrameData = frameData;
|
|
74
|
-
/**
|
|
75
|
-
* If we're already processing we've probably been triggered by a flushSync
|
|
76
|
-
* inside an existing process. Instead of executing, mark flushNextFrame
|
|
77
|
-
* as true and ensure we flush the following frame at the end of this one.
|
|
78
|
-
*/
|
|
79
|
-
if (isProcessing) {
|
|
80
|
-
flushNextFrame = true;
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
isProcessing = true;
|
|
84
|
-
// Swap this frame and the next to avoid GC
|
|
85
|
-
const prevFrame = thisFrame;
|
|
86
|
-
thisFrame = nextFrame;
|
|
87
|
-
nextFrame = prevFrame;
|
|
88
|
-
// Execute this frame
|
|
89
|
-
thisFrame.forEach(triggerCallback);
|
|
90
|
-
// Clear the frame so no callbacks remain. This is to avoid
|
|
91
|
-
// memory leaks should this render step not run for a while.
|
|
92
|
-
thisFrame.clear();
|
|
93
|
-
isProcessing = false;
|
|
94
|
-
if (flushNextFrame) {
|
|
95
|
-
flushNextFrame = false;
|
|
96
|
-
step.process(frameData);
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
return step;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const maxElapsed = 40;
|
|
104
|
-
function createRenderBatcher(scheduleNextBatch, allowKeepAlive) {
|
|
105
|
-
let runNextFrame = false;
|
|
106
|
-
let useDefaultElapsed = true;
|
|
107
|
-
const state = {
|
|
108
|
-
delta: 0.0,
|
|
109
|
-
timestamp: 0.0,
|
|
110
|
-
isProcessing: false,
|
|
111
|
-
};
|
|
112
|
-
const flagRunNextFrame = () => (runNextFrame = true);
|
|
113
|
-
const steps = stepsOrder.reduce((acc, key) => {
|
|
114
|
-
acc[key] = createRenderStep(flagRunNextFrame);
|
|
115
|
-
return acc;
|
|
116
|
-
}, {});
|
|
117
|
-
const { setup, read, resolveKeyframes, preUpdate, update, preRender, render, postRender, } = steps;
|
|
118
|
-
const processBatch = () => {
|
|
119
|
-
const useManualTiming = motionUtils.MotionGlobalConfig.useManualTiming;
|
|
120
|
-
const timestamp = useManualTiming
|
|
121
|
-
? state.timestamp
|
|
122
|
-
: performance.now();
|
|
123
|
-
runNextFrame = false;
|
|
124
|
-
if (!useManualTiming) {
|
|
125
|
-
state.delta = useDefaultElapsed
|
|
126
|
-
? 1000 / 60
|
|
127
|
-
: Math.max(Math.min(timestamp - state.timestamp, maxElapsed), 1);
|
|
128
|
-
}
|
|
129
|
-
state.timestamp = timestamp;
|
|
130
|
-
state.isProcessing = true;
|
|
131
|
-
// Unrolled render loop for better per-frame performance
|
|
132
|
-
setup.process(state);
|
|
133
|
-
read.process(state);
|
|
134
|
-
resolveKeyframes.process(state);
|
|
135
|
-
preUpdate.process(state);
|
|
136
|
-
update.process(state);
|
|
137
|
-
preRender.process(state);
|
|
138
|
-
render.process(state);
|
|
139
|
-
postRender.process(state);
|
|
140
|
-
state.isProcessing = false;
|
|
141
|
-
if (runNextFrame && allowKeepAlive) {
|
|
142
|
-
useDefaultElapsed = false;
|
|
143
|
-
scheduleNextBatch(processBatch);
|
|
144
|
-
}
|
|
145
|
-
};
|
|
146
|
-
const wake = () => {
|
|
147
|
-
runNextFrame = true;
|
|
148
|
-
useDefaultElapsed = true;
|
|
149
|
-
if (!state.isProcessing) {
|
|
150
|
-
scheduleNextBatch(processBatch);
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
const schedule = stepsOrder.reduce((acc, key) => {
|
|
154
|
-
const step = steps[key];
|
|
155
|
-
acc[key] = (process, keepAlive = false, immediate = false) => {
|
|
156
|
-
if (!runNextFrame)
|
|
157
|
-
wake();
|
|
158
|
-
return step.schedule(process, keepAlive, immediate);
|
|
159
|
-
};
|
|
160
|
-
return acc;
|
|
161
|
-
}, {});
|
|
162
|
-
const cancel = (process) => {
|
|
163
|
-
for (let i = 0; i < stepsOrder.length; i++) {
|
|
164
|
-
steps[stepsOrder[i]].cancel(process);
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
return { schedule, cancel, state, steps };
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const { schedule: frame, cancel: cancelFrame, state: frameData, steps: frameSteps, } = /* @__PURE__ */ createRenderBatcher(typeof requestAnimationFrame !== "undefined" ? requestAnimationFrame : motionUtils.noop, true);
|
|
7
|
+
/**
|
|
8
|
+
* The frameloop's state, kept apart from the frameloop itself so that
|
|
9
|
+
* reading it doesn't bundle the scheduler.
|
|
10
|
+
*/
|
|
11
|
+
const frameData = {
|
|
12
|
+
delta: 0.0,
|
|
13
|
+
timestamp: 0.0,
|
|
14
|
+
isProcessing: false,
|
|
15
|
+
};
|
|
171
16
|
|
|
172
17
|
let now;
|
|
173
18
|
function clearTime() {
|
|
@@ -751,6 +596,170 @@ function mix(from, to, p) {
|
|
|
751
596
|
return mixer(from, to);
|
|
752
597
|
}
|
|
753
598
|
|
|
599
|
+
const stepsOrder = [
|
|
600
|
+
"setup", // Compute
|
|
601
|
+
"read", // Read
|
|
602
|
+
"resolveKeyframes", // Write/Read/Write/Read
|
|
603
|
+
"preUpdate", // Compute
|
|
604
|
+
"update", // Compute
|
|
605
|
+
"preRender", // Compute
|
|
606
|
+
"render", // Write
|
|
607
|
+
"postRender", // Compute
|
|
608
|
+
];
|
|
609
|
+
|
|
610
|
+
function createRenderStep(runNextFrame) {
|
|
611
|
+
/**
|
|
612
|
+
* We create and reuse two queues, one to queue jobs for the current frame
|
|
613
|
+
* and one for the next. We reuse to avoid triggering GC after x frames.
|
|
614
|
+
*/
|
|
615
|
+
let thisFrame = new Set();
|
|
616
|
+
let nextFrame = new Set();
|
|
617
|
+
/**
|
|
618
|
+
* Track whether we're currently processing jobs in this step. This way
|
|
619
|
+
* we can decide whether to schedule new jobs for this frame or next.
|
|
620
|
+
*/
|
|
621
|
+
let isProcessing = false;
|
|
622
|
+
let flushNextFrame = false;
|
|
623
|
+
/**
|
|
624
|
+
* A set of processes which were marked keepAlive when scheduled.
|
|
625
|
+
* A keepAlive process is always also held by a frame queue until
|
|
626
|
+
* it's cancelled, so a Set has the same lifetime semantics as a
|
|
627
|
+
* WeakSet here while being considerably faster to query every frame.
|
|
628
|
+
*/
|
|
629
|
+
const toKeepAlive = new Set();
|
|
630
|
+
let latestFrameData = {
|
|
631
|
+
delta: 0.0,
|
|
632
|
+
timestamp: 0.0,
|
|
633
|
+
isProcessing: false,
|
|
634
|
+
};
|
|
635
|
+
function triggerCallback(callback) {
|
|
636
|
+
if (toKeepAlive.has(callback)) {
|
|
637
|
+
nextFrame.add(callback);
|
|
638
|
+
runNextFrame();
|
|
639
|
+
}
|
|
640
|
+
callback(latestFrameData);
|
|
641
|
+
}
|
|
642
|
+
const step = {
|
|
643
|
+
/**
|
|
644
|
+
* Schedule a process to run on the next frame.
|
|
645
|
+
*/
|
|
646
|
+
schedule: (callback, keepAlive = false, immediate = false) => {
|
|
647
|
+
const addToCurrentFrame = immediate && isProcessing;
|
|
648
|
+
const queue = addToCurrentFrame ? thisFrame : nextFrame;
|
|
649
|
+
if (keepAlive)
|
|
650
|
+
toKeepAlive.add(callback);
|
|
651
|
+
queue.add(callback);
|
|
652
|
+
return callback;
|
|
653
|
+
},
|
|
654
|
+
/**
|
|
655
|
+
* Cancel the provided callback from running on the next frame.
|
|
656
|
+
*/
|
|
657
|
+
cancel: (callback) => {
|
|
658
|
+
nextFrame.delete(callback);
|
|
659
|
+
toKeepAlive.delete(callback);
|
|
660
|
+
},
|
|
661
|
+
/**
|
|
662
|
+
* Execute all schedule callbacks.
|
|
663
|
+
*/
|
|
664
|
+
process: (frameData) => {
|
|
665
|
+
latestFrameData = frameData;
|
|
666
|
+
/**
|
|
667
|
+
* If we're already processing we've probably been triggered by a flushSync
|
|
668
|
+
* inside an existing process. Instead of executing, mark flushNextFrame
|
|
669
|
+
* as true and ensure we flush the following frame at the end of this one.
|
|
670
|
+
*/
|
|
671
|
+
if (isProcessing) {
|
|
672
|
+
flushNextFrame = true;
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
isProcessing = true;
|
|
676
|
+
// Swap this frame and the next to avoid GC
|
|
677
|
+
const prevFrame = thisFrame;
|
|
678
|
+
thisFrame = nextFrame;
|
|
679
|
+
nextFrame = prevFrame;
|
|
680
|
+
// Execute this frame
|
|
681
|
+
thisFrame.forEach(triggerCallback);
|
|
682
|
+
// Clear the frame so no callbacks remain. This is to avoid
|
|
683
|
+
// memory leaks should this render step not run for a while.
|
|
684
|
+
thisFrame.clear();
|
|
685
|
+
isProcessing = false;
|
|
686
|
+
if (flushNextFrame) {
|
|
687
|
+
flushNextFrame = false;
|
|
688
|
+
step.process(frameData);
|
|
689
|
+
}
|
|
690
|
+
},
|
|
691
|
+
};
|
|
692
|
+
return step;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
const maxElapsed = 40;
|
|
696
|
+
function createRenderBatcher(scheduleNextBatch, allowKeepAlive, state = {
|
|
697
|
+
delta: 0.0,
|
|
698
|
+
timestamp: 0.0,
|
|
699
|
+
isProcessing: false,
|
|
700
|
+
}) {
|
|
701
|
+
let runNextFrame = false;
|
|
702
|
+
let useDefaultElapsed = true;
|
|
703
|
+
const flagRunNextFrame = () => (runNextFrame = true);
|
|
704
|
+
const steps = stepsOrder.reduce((acc, key) => {
|
|
705
|
+
acc[key] = createRenderStep(flagRunNextFrame);
|
|
706
|
+
return acc;
|
|
707
|
+
}, {});
|
|
708
|
+
const { setup, read, resolveKeyframes, preUpdate, update, preRender, render, postRender, } = steps;
|
|
709
|
+
const processBatch = () => {
|
|
710
|
+
const useManualTiming = motionUtils.MotionGlobalConfig.useManualTiming;
|
|
711
|
+
const timestamp = useManualTiming
|
|
712
|
+
? state.timestamp
|
|
713
|
+
: performance.now();
|
|
714
|
+
runNextFrame = false;
|
|
715
|
+
if (!useManualTiming) {
|
|
716
|
+
state.delta = useDefaultElapsed
|
|
717
|
+
? 1000 / 60
|
|
718
|
+
: Math.max(Math.min(timestamp - state.timestamp, maxElapsed), 1);
|
|
719
|
+
}
|
|
720
|
+
state.timestamp = timestamp;
|
|
721
|
+
state.isProcessing = true;
|
|
722
|
+
// Unrolled render loop for better per-frame performance
|
|
723
|
+
setup.process(state);
|
|
724
|
+
read.process(state);
|
|
725
|
+
resolveKeyframes.process(state);
|
|
726
|
+
preUpdate.process(state);
|
|
727
|
+
update.process(state);
|
|
728
|
+
preRender.process(state);
|
|
729
|
+
render.process(state);
|
|
730
|
+
postRender.process(state);
|
|
731
|
+
state.isProcessing = false;
|
|
732
|
+
if (runNextFrame && allowKeepAlive) {
|
|
733
|
+
useDefaultElapsed = false;
|
|
734
|
+
scheduleNextBatch(processBatch);
|
|
735
|
+
}
|
|
736
|
+
};
|
|
737
|
+
const wake = () => {
|
|
738
|
+
runNextFrame = true;
|
|
739
|
+
useDefaultElapsed = true;
|
|
740
|
+
if (!state.isProcessing) {
|
|
741
|
+
scheduleNextBatch(processBatch);
|
|
742
|
+
}
|
|
743
|
+
};
|
|
744
|
+
const schedule = stepsOrder.reduce((acc, key) => {
|
|
745
|
+
const step = steps[key];
|
|
746
|
+
acc[key] = (process, keepAlive = false, immediate = false) => {
|
|
747
|
+
if (!runNextFrame)
|
|
748
|
+
wake();
|
|
749
|
+
return step.schedule(process, keepAlive, immediate);
|
|
750
|
+
};
|
|
751
|
+
return acc;
|
|
752
|
+
}, {});
|
|
753
|
+
const cancel = (process) => {
|
|
754
|
+
for (let i = 0; i < stepsOrder.length; i++) {
|
|
755
|
+
steps[stepsOrder[i]].cancel(process);
|
|
756
|
+
}
|
|
757
|
+
};
|
|
758
|
+
return { schedule, cancel, state, steps };
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
const { schedule: frame, cancel: cancelFrame, steps: frameSteps, } = /* @__PURE__ */ createRenderBatcher(typeof requestAnimationFrame !== "undefined" ? requestAnimationFrame : motionUtils.noop, true, frameData);
|
|
762
|
+
|
|
754
763
|
const frameloopDriver = (update) => {
|
|
755
764
|
const passTimestamp = ({ timestamp }) => update(timestamp);
|
|
756
765
|
return {
|
|
@@ -812,7 +821,6 @@ const springDefaults = {
|
|
|
812
821
|
stiffness: 100,
|
|
813
822
|
damping: 10,
|
|
814
823
|
mass: 1.0,
|
|
815
|
-
velocity: 0.0,
|
|
816
824
|
// Default duration/bounce-based options
|
|
817
825
|
duration: 800, // in ms
|
|
818
826
|
bounce: 0.3,
|
|
@@ -830,8 +838,37 @@ const springDefaults = {
|
|
|
830
838
|
minDuration: 0.01, // in seconds
|
|
831
839
|
maxDuration: 10.0, // in seconds
|
|
832
840
|
minDamping: 0.05,
|
|
833
|
-
maxDamping: 1,
|
|
834
841
|
};
|
|
842
|
+
/**
|
|
843
|
+
* Maps bounce to a damping ratio as SwiftUI does: 0 is critically damped, and
|
|
844
|
+
* it approaches undamped towards 1 and infinitely overdamped towards -1. Both
|
|
845
|
+
* ends are limited so the spring still moves and settles.
|
|
846
|
+
*/
|
|
847
|
+
const bounceToDampingRatio = (bounce) => bounce < 0
|
|
848
|
+
? 1 / Math.max(1 + bounce, springDefaults.minDamping)
|
|
849
|
+
: Math.max(1 - bounce, springDefaults.minDamping);
|
|
850
|
+
/**
|
|
851
|
+
* Scales an overdamped spring's undamped frequency so that, at its defining
|
|
852
|
+
* time, it has as far left to go as the critically damped spring that's
|
|
853
|
+
* criticalFreqTime radians in by then. A negative bounce then changes the
|
|
854
|
+
* shape of the curve, not when it arrives. 1 at a damping ratio of 1, so
|
|
855
|
+
* there's no jump at a bounce of 0.
|
|
856
|
+
*
|
|
857
|
+
* Solved by Newton on the remaining distance (times fast - slow), which is
|
|
858
|
+
* convex from criticalFreqTime onwards. criticalFreqTime is always below the
|
|
859
|
+
* answer, so it converges from below without overshooting.
|
|
860
|
+
*/
|
|
861
|
+
function overdampedFreqScale(dampingRatio, criticalFreqTime) {
|
|
862
|
+
if (!(dampingRatio > 1))
|
|
863
|
+
return 1;
|
|
864
|
+
const root = Math.sqrt(dampingRatio * dampingRatio - 1);
|
|
865
|
+
const slow = dampingRatio - root;
|
|
866
|
+
const fast = dampingRatio + root;
|
|
867
|
+
const target = 2 * root * Math.exp(-criticalFreqTime) * (1 + criticalFreqTime);
|
|
868
|
+
return (approximateRoot((freqTime) => fast * Math.exp(-slow * freqTime) -
|
|
869
|
+
slow * Math.exp(-fast * freqTime) -
|
|
870
|
+
target, (freqTime) => Math.exp(-fast * freqTime) - Math.exp(-slow * freqTime), criticalFreqTime) / criticalFreqTime);
|
|
871
|
+
}
|
|
835
872
|
function calcAngularFreq(undampedFreq, dampingRatio) {
|
|
836
873
|
return undampedFreq * Math.sqrt(1 - dampingRatio * dampingRatio);
|
|
837
874
|
}
|
|
@@ -847,15 +884,16 @@ function approximateRoot(envelope, derivative, initialGuess) {
|
|
|
847
884
|
* This is ported from the Framer implementation of duration-based spring resolution.
|
|
848
885
|
*/
|
|
849
886
|
const safeMin = 0.001;
|
|
850
|
-
|
|
887
|
+
/**
|
|
888
|
+
* Assumes zero initial velocity and the default mass: time-defined springs
|
|
889
|
+
* ignore inherited velocity and only resolve without valid physics. Returns
|
|
890
|
+
* NaN physics when the root search doesn't converge.
|
|
891
|
+
*/
|
|
892
|
+
function findSpring({ duration = springDefaults.duration, bounce = springDefaults.bounce, }) {
|
|
851
893
|
let envelope;
|
|
852
894
|
let derivative;
|
|
853
895
|
motionUtils.warning(duration <= motionUtils.secondsToMilliseconds(springDefaults.maxDuration), "Spring duration must be 10 seconds or less", "spring-duration-limit");
|
|
854
|
-
|
|
855
|
-
/**
|
|
856
|
-
* Restrict dampingRatio and duration to within acceptable ranges.
|
|
857
|
-
*/
|
|
858
|
-
dampingRatio = motionUtils.clamp(springDefaults.minDamping, springDefaults.maxDamping, dampingRatio);
|
|
896
|
+
const dampingRatio = bounceToDampingRatio(bounce);
|
|
859
897
|
duration = motionUtils.clamp(springDefaults.minDuration, springDefaults.maxDuration, motionUtils.millisecondsToSeconds(duration));
|
|
860
898
|
if (dampingRatio < 1) {
|
|
861
899
|
/**
|
|
@@ -864,15 +902,13 @@ function findSpring({ duration = springDefaults.duration, bounce = springDefault
|
|
|
864
902
|
envelope = (undampedFreq) => {
|
|
865
903
|
const exponentialDecay = undampedFreq * dampingRatio;
|
|
866
904
|
const delta = exponentialDecay * duration;
|
|
867
|
-
const a = exponentialDecay - velocity;
|
|
868
905
|
const b = calcAngularFreq(undampedFreq, dampingRatio);
|
|
869
906
|
const c = Math.exp(-delta);
|
|
870
|
-
return safeMin - (
|
|
907
|
+
return safeMin - (exponentialDecay / b) * c;
|
|
871
908
|
};
|
|
872
909
|
derivative = (undampedFreq) => {
|
|
873
910
|
const exponentialDecay = undampedFreq * dampingRatio;
|
|
874
911
|
const delta = exponentialDecay * duration;
|
|
875
|
-
const d = delta * velocity + velocity;
|
|
876
912
|
const e = dampingRatio *
|
|
877
913
|
dampingRatio *
|
|
878
914
|
undampedFreq *
|
|
@@ -881,46 +917,35 @@ function findSpring({ duration = springDefaults.duration, bounce = springDefault
|
|
|
881
917
|
const f = Math.exp(-delta);
|
|
882
918
|
const g = calcAngularFreq(undampedFreq * undampedFreq, dampingRatio);
|
|
883
919
|
const factor = -envelope(undampedFreq) + safeMin > 0 ? -1 : 1;
|
|
884
|
-
return (factor *
|
|
920
|
+
return (factor * -e * f) / g;
|
|
885
921
|
};
|
|
886
922
|
}
|
|
887
923
|
else {
|
|
888
924
|
/**
|
|
889
|
-
* Critically-damped spring
|
|
925
|
+
* Critically-damped spring. Overdamped springs reuse this root,
|
|
926
|
+
* scaled below.
|
|
890
927
|
*/
|
|
891
928
|
envelope = (undampedFreq) => {
|
|
892
929
|
const a = Math.exp(-undampedFreq * duration);
|
|
893
|
-
const b =
|
|
930
|
+
const b = undampedFreq * duration + 1;
|
|
894
931
|
return -safeMin + a * b;
|
|
895
932
|
};
|
|
896
933
|
derivative = (undampedFreq) => {
|
|
897
934
|
const a = Math.exp(-undampedFreq * duration);
|
|
898
|
-
const b =
|
|
935
|
+
const b = -undampedFreq * (duration * duration);
|
|
899
936
|
return a * b;
|
|
900
937
|
};
|
|
901
938
|
}
|
|
902
939
|
const initialGuess = 5 / duration;
|
|
903
|
-
const
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
}
|
|
912
|
-
else {
|
|
913
|
-
const stiffness = undampedFreq * undampedFreq * mass;
|
|
914
|
-
return {
|
|
915
|
-
stiffness,
|
|
916
|
-
damping: dampingRatio * 2 * Math.sqrt(mass * stiffness),
|
|
917
|
-
duration,
|
|
918
|
-
};
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
const durationKeys = ["duration", "bounce"];
|
|
922
|
-
function isSpringType(options, keys) {
|
|
923
|
-
return keys.some((key) => options[key] !== undefined);
|
|
940
|
+
const criticalFreq = approximateRoot(envelope, derivative, initialGuess);
|
|
941
|
+
const undampedFreq = criticalFreq *
|
|
942
|
+
overdampedFreqScale(dampingRatio, criticalFreq * duration);
|
|
943
|
+
const stiffness = undampedFreq * undampedFreq;
|
|
944
|
+
return {
|
|
945
|
+
stiffness,
|
|
946
|
+
damping: dampingRatio * 2 * Math.sqrt(stiffness),
|
|
947
|
+
duration: motionUtils.secondsToMilliseconds(duration),
|
|
948
|
+
};
|
|
924
949
|
}
|
|
925
950
|
/**
|
|
926
951
|
* Spring physics must be finite. stiffness and mass are also divisors so must
|
|
@@ -952,7 +977,7 @@ function getSpringOptions(options) {
|
|
|
952
977
|
const validStiffness = resolvePhysics(options.stiffness);
|
|
953
978
|
const validDamping = resolvePhysics(options.damping, true);
|
|
954
979
|
const validMass = resolvePhysics(options.mass);
|
|
955
|
-
|
|
980
|
+
const springOptions = {
|
|
956
981
|
...options,
|
|
957
982
|
stiffness: validStiffness ?? springDefaults.stiffness,
|
|
958
983
|
damping: validDamping ?? springDefaults.damping,
|
|
@@ -960,42 +985,27 @@ function getSpringOptions(options) {
|
|
|
960
985
|
isResolvedFromDuration: false,
|
|
961
986
|
// stiffness/damping/mass overrides duration/bounce
|
|
962
987
|
isTimeDefined: (validStiffness ?? validDamping ?? validMass) === undefined &&
|
|
963
|
-
|
|
988
|
+
(options.duration !== undefined || options.bounce !== undefined),
|
|
964
989
|
};
|
|
965
990
|
if (springOptions.isTimeDefined) {
|
|
966
|
-
// Time-defined springs should ignore inherited velocity.
|
|
967
|
-
// Velocity from interrupted animations can cause findSpring()
|
|
968
|
-
// to compute wildly different spring parameters, leading to
|
|
969
|
-
// massive oscillation on small-range animations.
|
|
970
|
-
springOptions.velocity = 0;
|
|
971
991
|
if (options.visualDuration) {
|
|
972
|
-
const
|
|
973
|
-
const root = (2 * Math.PI) / (visualDuration * 1.2)
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
Math.sqrt(stiffness);
|
|
978
|
-
springOptions = {
|
|
979
|
-
...springOptions,
|
|
980
|
-
mass: springDefaults.mass,
|
|
981
|
-
stiffness,
|
|
982
|
-
damping,
|
|
983
|
-
};
|
|
992
|
+
const dampingRatio = bounceToDampingRatio(options.bounce || 0);
|
|
993
|
+
const root = ((2 * Math.PI) / (options.visualDuration * 1.2)) *
|
|
994
|
+
overdampedFreqScale(dampingRatio, (2 * Math.PI) / 1.2);
|
|
995
|
+
springOptions.stiffness = root * root;
|
|
996
|
+
springOptions.damping =
|
|
997
|
+
2 * dampingRatio * Math.sqrt(springOptions.stiffness);
|
|
984
998
|
}
|
|
985
999
|
else {
|
|
986
|
-
|
|
987
|
-
springOptions = {
|
|
988
|
-
...springOptions,
|
|
989
|
-
...derived,
|
|
990
|
-
mass: springDefaults.mass,
|
|
991
|
-
};
|
|
1000
|
+
Object.assign(springOptions, findSpring(springOptions));
|
|
992
1001
|
springOptions.isResolvedFromDuration = true;
|
|
993
1002
|
}
|
|
994
1003
|
/**
|
|
995
|
-
*
|
|
996
|
-
*
|
|
997
|
-
*
|
|
998
|
-
* between them is never
|
|
1004
|
+
* Time options can degenerate: a NaN bounce gives a NaN damping, an
|
|
1005
|
+
* infinite visualDuration a 0 stiffness, and findSpring NaN for both
|
|
1006
|
+
* when it doesn't converge. Replace the two together, so the
|
|
1007
|
+
* relationship duration resolution establishes between them is never
|
|
1008
|
+
* left half-overwritten.
|
|
999
1009
|
*/
|
|
1000
1010
|
if (!isValidPhysics(springOptions.stiffness) ||
|
|
1001
1011
|
!isValidPhysics(springOptions.damping, true)) {
|
|
@@ -1020,10 +1030,13 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
|
|
|
1020
1030
|
* to reduce GC during animation.
|
|
1021
1031
|
*/
|
|
1022
1032
|
const state = { done: false, value: origin };
|
|
1023
|
-
const { stiffness, damping, mass, duration,
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1033
|
+
const { stiffness, damping, mass, duration, isResolvedFromDuration, isTimeDefined, } = getSpringOptions({ ...options });
|
|
1034
|
+
/**
|
|
1035
|
+
* Time-defined springs ignore inherited velocity. Velocity from
|
|
1036
|
+
* interrupted animations causes massive oscillation on small-range
|
|
1037
|
+
* animations.
|
|
1038
|
+
*/
|
|
1039
|
+
const inheritVelocity = (velocity) => isTimeDefined ? 0 : -motionUtils.millisecondsToSeconds(velocity);
|
|
1027
1040
|
const dampingRatio = damping / (2 * Math.sqrt(stiffness * mass));
|
|
1028
1041
|
const undampedAngularFreq = motionUtils.millisecondsToSeconds(Math.sqrt(stiffness / mass));
|
|
1029
1042
|
const decay = dampingRatio * undampedAngularFreq;
|
|
@@ -1037,7 +1050,7 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
|
|
|
1037
1050
|
const s = {
|
|
1038
1051
|
target,
|
|
1039
1052
|
delta: target - origin,
|
|
1040
|
-
velocity: velocity || 0
|
|
1053
|
+
velocity: inheritVelocity(options.velocity || 0) || 0,
|
|
1041
1054
|
restSpeed: 0,
|
|
1042
1055
|
restDelta: 0,
|
|
1043
1056
|
};
|
|
@@ -1116,31 +1129,32 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
|
|
|
1116
1129
|
(undampedAngularFreq * c.C * t - s.velocity);
|
|
1117
1130
|
}
|
|
1118
1131
|
else {
|
|
1119
|
-
|
|
1132
|
+
/**
|
|
1133
|
+
* Overdamped spring: the sum of a slow and a fast decaying
|
|
1134
|
+
* exponential, so no term can overflow however heavily it's damped.
|
|
1135
|
+
*/
|
|
1120
1136
|
const dampedAngularFreq = undampedAngularFreq * Math.sqrt(dampingRatio * dampingRatio - 1);
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
const
|
|
1137
|
+
const slow = decay - dampedAngularFreq;
|
|
1138
|
+
const fast = decay + dampedAngularFreq;
|
|
1139
|
+
/**
|
|
1140
|
+
* Physics-defined springs keep a non-physical limit: once
|
|
1141
|
+
* dampedAngularFreq * t passes 300, both terms decay at the damping
|
|
1142
|
+
* rate. Heavily overdamped drag springs (dragElastic: 0,
|
|
1143
|
+
* dragTransition) rely on this to settle almost at once rather than
|
|
1144
|
+
* creep at stiffness / damping. Negative bounce needs the exact
|
|
1145
|
+
* curve, so time-defined springs are exempt.
|
|
1146
|
+
*/
|
|
1147
|
+
const limit = isTimeDefined ? Infinity : 300 / dampedAngularFreq;
|
|
1148
|
+
const decayAt = (rate, t) => Math.exp(t > limit ? -rate * limit - decay * (t - limit) : -rate * t);
|
|
1149
|
+
const c = { S: 0, F: 0 };
|
|
1133
1150
|
update = () => {
|
|
1134
|
-
|
|
1135
|
-
c.
|
|
1136
|
-
c.
|
|
1137
|
-
};
|
|
1138
|
-
resolveVelocity = (t) => {
|
|
1139
|
-
const envelope = Math.exp(-decay * t);
|
|
1140
|
-
const freqForT = Math.min(dampedAngularFreq * t, 300);
|
|
1141
|
-
return (envelope *
|
|
1142
|
-
(c.sinh * Math.sinh(freqForT) + c.cosh * Math.cosh(freqForT)));
|
|
1151
|
+
const P = (s.velocity + decay * s.delta) / dampedAngularFreq;
|
|
1152
|
+
c.S = (s.delta + P) / 2;
|
|
1153
|
+
c.F = (s.delta - P) / 2;
|
|
1143
1154
|
};
|
|
1155
|
+
resolveSpring = (t) => s.target - c.S * decayAt(slow, t) - c.F * decayAt(fast, t);
|
|
1156
|
+
// Analytical derivative of overdamped spring (px/ms)
|
|
1157
|
+
resolveVelocity = (t) => slow * c.S * decayAt(slow, t) + fast * c.F * decayAt(fast, t);
|
|
1144
1158
|
}
|
|
1145
1159
|
update();
|
|
1146
1160
|
const calculatedDuration = isResolvedFromDuration ? duration || null : null;
|
|
@@ -1153,8 +1167,7 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
|
|
|
1153
1167
|
retarget: (keyframes, newVelocity) => {
|
|
1154
1168
|
s.target = keyframes[keyframes.length - 1];
|
|
1155
1169
|
s.delta = s.target - keyframes[0];
|
|
1156
|
-
|
|
1157
|
-
s.velocity = isTimeDefined ? 0 : -motionUtils.millisecondsToSeconds(newVelocity);
|
|
1170
|
+
s.velocity = inheritVelocity(newVelocity);
|
|
1158
1171
|
// Default thresholds depend on the scale of the new delta
|
|
1159
1172
|
if (!(options.restSpeed && options.restDelta))
|
|
1160
1173
|
setRestThresholds();
|
|
@@ -2720,17 +2733,14 @@ class NativeAnimation extends WithPromise {
|
|
|
2720
2733
|
/**
|
|
2721
2734
|
* Attaches a timeline to the animation, for instance the `ScrollTimeline`.
|
|
2722
2735
|
*/
|
|
2723
|
-
attachTimeline({ timeline,
|
|
2736
|
+
attachTimeline({ timeline, onAttach, observe, }) {
|
|
2724
2737
|
if (this.allowFlatten) {
|
|
2725
2738
|
this.animation.effect?.updateTiming({ easing: "linear" });
|
|
2726
2739
|
}
|
|
2727
2740
|
this.animation.onfinish = null;
|
|
2728
2741
|
if (timeline && supportsScrollTimeline()) {
|
|
2729
2742
|
this.animation.timeline = timeline;
|
|
2730
|
-
|
|
2731
|
-
this.animation.rangeStart = rangeStart;
|
|
2732
|
-
if (rangeEnd)
|
|
2733
|
-
this.animation.rangeEnd = rangeEnd;
|
|
2743
|
+
onAttach?.(this.animation);
|
|
2734
2744
|
return motionUtils.noop;
|
|
2735
2745
|
}
|
|
2736
2746
|
else {
|
|
@@ -6063,21 +6073,6 @@ function resize(a, b) {
|
|
|
6063
6073
|
return typeof a === "function" ? resizeWindow(a) : resizeElement(a, b);
|
|
6064
6074
|
}
|
|
6065
6075
|
|
|
6066
|
-
function observeTimeline(update, timeline) {
|
|
6067
|
-
let prevProgress;
|
|
6068
|
-
const onFrame = () => {
|
|
6069
|
-
const { currentTime } = timeline;
|
|
6070
|
-
const percentage = currentTime === null ? 0 : currentTime.value;
|
|
6071
|
-
const progress = percentage / 100;
|
|
6072
|
-
if (prevProgress !== progress) {
|
|
6073
|
-
update(progress);
|
|
6074
|
-
}
|
|
6075
|
-
prevProgress = progress;
|
|
6076
|
-
};
|
|
6077
|
-
frame.preUpdate(onFrame, true);
|
|
6078
|
-
return () => cancelFrame(onFrame);
|
|
6079
|
-
}
|
|
6080
|
-
|
|
6081
6076
|
const statsBuffer = {
|
|
6082
6077
|
value: null,
|
|
6083
6078
|
addProjectionMetrics: null,
|
|
@@ -6191,8 +6186,7 @@ class FollowAnimation extends WithPromise {
|
|
|
6191
6186
|
this.options.onStop?.();
|
|
6192
6187
|
};
|
|
6193
6188
|
this.options = options;
|
|
6194
|
-
|
|
6195
|
-
this.factory = options.type || keyframes;
|
|
6189
|
+
this.factory = options.type;
|
|
6196
6190
|
this.generator = this.factory(options);
|
|
6197
6191
|
const { driver } = options;
|
|
6198
6192
|
if (driver)
|
|
@@ -6293,41 +6287,15 @@ class FollowAnimation extends WithPromise {
|
|
|
6293
6287
|
}
|
|
6294
6288
|
}
|
|
6295
6289
|
|
|
6290
|
+
const asNumber = (v) => typeof v === "number" ? v : parseFloat(v);
|
|
6291
|
+
|
|
6296
6292
|
/**
|
|
6297
|
-
*
|
|
6298
|
-
*
|
|
6299
|
-
*
|
|
6300
|
-
*
|
|
6301
|
-
* const x = motionValue(0)
|
|
6302
|
-
* const y = followValue(x, { type: "spring", stiffness: 300 })
|
|
6303
|
-
* // or with tween
|
|
6304
|
-
* const z = followValue(x, { type: "tween", duration: 0.5, ease: "easeOut" })
|
|
6305
|
-
* ```
|
|
6306
|
-
*
|
|
6307
|
-
* @param source - Initial value or MotionValue to track
|
|
6308
|
-
* @param options - Animation transition options
|
|
6309
|
-
* @returns `MotionValue`
|
|
6310
|
-
*
|
|
6311
|
-
* @public
|
|
6312
|
-
*/
|
|
6313
|
-
function followValue(source, options) {
|
|
6314
|
-
const initialValue = isMotionValue(source) ? source.get() : source;
|
|
6315
|
-
const value = motionValue(initialValue);
|
|
6316
|
-
attachFollow(value, source, options);
|
|
6317
|
-
return value;
|
|
6318
|
-
}
|
|
6319
|
-
/**
|
|
6320
|
-
* Attach an animation to a MotionValue that will animate whenever the value changes.
|
|
6321
|
-
* Similar to attachSpring but supports any transition type (spring, tween, inertia, etc.)
|
|
6322
|
-
*
|
|
6323
|
-
* @param value - The MotionValue to animate
|
|
6324
|
-
* @param source - Initial value or MotionValue to track
|
|
6325
|
-
* @param options - Animation transition options
|
|
6326
|
-
* @returns Cleanup function
|
|
6327
|
-
*
|
|
6328
|
-
* @public
|
|
6293
|
+
* Animate `value` to its latest value with the generator in
|
|
6294
|
+
* `options.type`, tracking `source` if it's a `MotionValue`. Takes a
|
|
6295
|
+
* generator rather than a type name, so spring-only callers don't
|
|
6296
|
+
* bundle every generator.
|
|
6329
6297
|
*/
|
|
6330
|
-
function
|
|
6298
|
+
function follow(value, source, options) {
|
|
6331
6299
|
const initialValue = value.get();
|
|
6332
6300
|
let activeAnimation = null;
|
|
6333
6301
|
let set;
|
|
@@ -6345,7 +6313,7 @@ function attachFollow(value, source, options = {}) {
|
|
|
6345
6313
|
};
|
|
6346
6314
|
value.attach((v, safeSet) => {
|
|
6347
6315
|
set = safeSet;
|
|
6348
|
-
const target = asNumber
|
|
6316
|
+
const target = asNumber(v);
|
|
6349
6317
|
if (activeAnimation?.state === "running") {
|
|
6350
6318
|
/**
|
|
6351
6319
|
* Steer the running animation rather than replacing it. This
|
|
@@ -6356,7 +6324,7 @@ function attachFollow(value, source, options = {}) {
|
|
|
6356
6324
|
activeAnimation.setTarget(target, options.velocity);
|
|
6357
6325
|
return;
|
|
6358
6326
|
}
|
|
6359
|
-
const current = asNumber
|
|
6327
|
+
const current = asNumber(value.get());
|
|
6360
6328
|
const velocity = activeAnimation
|
|
6361
6329
|
? activeAnimation.getGeneratorVelocity()
|
|
6362
6330
|
: value.getVelocity();
|
|
@@ -6367,8 +6335,6 @@ function attachFollow(value, source, options = {}) {
|
|
|
6367
6335
|
const animationOptions = {
|
|
6368
6336
|
keyframes: [current, target],
|
|
6369
6337
|
velocity,
|
|
6370
|
-
// Default to spring if no type specified (matches useSpring behavior)
|
|
6371
|
-
type: "spring",
|
|
6372
6338
|
restDelta: 0.001,
|
|
6373
6339
|
restSpeed: 0.01,
|
|
6374
6340
|
...options,
|
|
@@ -6410,8 +6376,47 @@ function attachFollow(value, source, options = {}) {
|
|
|
6410
6376
|
function parseValue(v, unit) {
|
|
6411
6377
|
return unit ? v + unit : v;
|
|
6412
6378
|
}
|
|
6413
|
-
|
|
6414
|
-
|
|
6379
|
+
|
|
6380
|
+
/**
|
|
6381
|
+
* Create a `MotionValue` that animates to its latest value using any transition type.
|
|
6382
|
+
* Can either be a value or track another `MotionValue`.
|
|
6383
|
+
*
|
|
6384
|
+
* ```jsx
|
|
6385
|
+
* const x = motionValue(0)
|
|
6386
|
+
* const y = followValue(x, { type: "spring", stiffness: 300 })
|
|
6387
|
+
* // or with tween
|
|
6388
|
+
* const z = followValue(x, { type: "tween", duration: 0.5, ease: "easeOut" })
|
|
6389
|
+
* ```
|
|
6390
|
+
*
|
|
6391
|
+
* @param source - Initial value or MotionValue to track
|
|
6392
|
+
* @param options - Animation transition options
|
|
6393
|
+
* @returns `MotionValue`
|
|
6394
|
+
*
|
|
6395
|
+
* @public
|
|
6396
|
+
*/
|
|
6397
|
+
function followValue(source, options) {
|
|
6398
|
+
const initialValue = isMotionValue(source) ? source.get() : source;
|
|
6399
|
+
const value = motionValue(initialValue);
|
|
6400
|
+
attachFollow(value, source, options);
|
|
6401
|
+
return value;
|
|
6402
|
+
}
|
|
6403
|
+
/**
|
|
6404
|
+
* Attach an animation to a MotionValue that will animate whenever the value changes.
|
|
6405
|
+
* Similar to attachSpring but supports any transition type (spring, tween, inertia, etc.)
|
|
6406
|
+
*
|
|
6407
|
+
* @param value - The MotionValue to animate
|
|
6408
|
+
* @param source - Initial value or MotionValue to track
|
|
6409
|
+
* @param options - Animation transition options
|
|
6410
|
+
* @returns Cleanup function
|
|
6411
|
+
*
|
|
6412
|
+
* @public
|
|
6413
|
+
*/
|
|
6414
|
+
function attachFollow(value, source, options = {}) {
|
|
6415
|
+
// Default to spring if no type specified (matches useSpring behavior)
|
|
6416
|
+
const transition = { type: "spring", ...options };
|
|
6417
|
+
replaceTransitionType(transition);
|
|
6418
|
+
transition.type = transition.type || keyframes;
|
|
6419
|
+
return follow(value, source, transition);
|
|
6415
6420
|
}
|
|
6416
6421
|
|
|
6417
6422
|
function subscribeValue(inputValues, outputValue, getLatest) {
|
|
@@ -6513,7 +6518,9 @@ function mapValue(inputValue, inputRange, outputRange, options) {
|
|
|
6513
6518
|
* @public
|
|
6514
6519
|
*/
|
|
6515
6520
|
function springValue(source, options) {
|
|
6516
|
-
|
|
6521
|
+
const value = motionValue(isMotionValue(source) ? source.get() : source);
|
|
6522
|
+
attachSpring(value, source, options);
|
|
6523
|
+
return value;
|
|
6517
6524
|
}
|
|
6518
6525
|
/**
|
|
6519
6526
|
* Attach a spring animation to a MotionValue that will animate whenever the value changes.
|
|
@@ -6526,7 +6533,7 @@ function springValue(source, options) {
|
|
|
6526
6533
|
* @public
|
|
6527
6534
|
*/
|
|
6528
6535
|
function attachSpring(value, source, options) {
|
|
6529
|
-
return
|
|
6536
|
+
return follow(value, source, { ...options, type: spring });
|
|
6530
6537
|
}
|
|
6531
6538
|
|
|
6532
6539
|
/**
|
|
@@ -8196,7 +8203,15 @@ class VisualElement {
|
|
|
8196
8203
|
this.removeValue(key);
|
|
8197
8204
|
this.bindToMotionValue(key, value);
|
|
8198
8205
|
this.values.set(key, value);
|
|
8199
|
-
|
|
8206
|
+
/**
|
|
8207
|
+
* An animated value with no base value is undefined until the
|
|
8208
|
+
* keyframe resolver reads its origin. Rendering it before then
|
|
8209
|
+
* writes an invalid placeholder (e.g. points="undefined") that
|
|
8210
|
+
* the resolver can read back from the DOM as the origin.
|
|
8211
|
+
*/
|
|
8212
|
+
const latest = value.get();
|
|
8213
|
+
if (latest !== undefined)
|
|
8214
|
+
this.latestValues[key] = latest;
|
|
8200
8215
|
}
|
|
8201
8216
|
}
|
|
8202
8217
|
/**
|
|
@@ -8224,7 +8239,9 @@ class VisualElement {
|
|
|
8224
8239
|
}
|
|
8225
8240
|
let value = this.values.get(key);
|
|
8226
8241
|
if (value === undefined && defaultValue !== undefined) {
|
|
8227
|
-
value = motionValue(defaultValue
|
|
8242
|
+
value = motionValue(defaultValue ?? this.getDefaultValue?.(key), {
|
|
8243
|
+
owner: this,
|
|
8244
|
+
});
|
|
8228
8245
|
this.addValue(key, value);
|
|
8229
8246
|
}
|
|
8230
8247
|
return value;
|
|
@@ -8386,86 +8403,7 @@ function renderHTML(element, { style, vars }, styleProp, projection) {
|
|
|
8386
8403
|
}
|
|
8387
8404
|
}
|
|
8388
8405
|
|
|
8389
|
-
|
|
8390
|
-
if (axis.max === axis.min)
|
|
8391
|
-
return 0;
|
|
8392
|
-
return (pixels / (axis.max - axis.min)) * 100;
|
|
8393
|
-
}
|
|
8394
|
-
/**
|
|
8395
|
-
* We always correct borderRadius as a percentage rather than pixels to reduce paints.
|
|
8396
|
-
* For example, if you are projecting a box that is 100px wide with a 10px borderRadius
|
|
8397
|
-
* into a box that is 200px wide with a 20px borderRadius, that is actually a 10%
|
|
8398
|
-
* borderRadius in both states. If we animate between the two in pixels that will trigger
|
|
8399
|
-
* a paint each time. If we animate between the two in percentage we'll avoid a paint.
|
|
8400
|
-
*/
|
|
8401
|
-
const correctBorderRadius = {
|
|
8402
|
-
correct: (latest, node) => {
|
|
8403
|
-
if (!node.target)
|
|
8404
|
-
return latest;
|
|
8405
|
-
/**
|
|
8406
|
-
* If latest is a string, if it's a percentage we can return immediately as it's
|
|
8407
|
-
* going to be stretched appropriately. Otherwise, if it's a pixel, convert it to a number.
|
|
8408
|
-
*/
|
|
8409
|
-
if (typeof latest === "string") {
|
|
8410
|
-
if (px.test(latest)) {
|
|
8411
|
-
latest = parseFloat(latest);
|
|
8412
|
-
}
|
|
8413
|
-
else {
|
|
8414
|
-
return latest;
|
|
8415
|
-
}
|
|
8416
|
-
}
|
|
8417
|
-
/**
|
|
8418
|
-
* If latest is a number, it's a pixel value. We use the current viewportBox to calculate that
|
|
8419
|
-
* pixel value as a percentage of each axis
|
|
8420
|
-
*/
|
|
8421
|
-
const x = pixelsToPercent(latest, node.target.x);
|
|
8422
|
-
const y = pixelsToPercent(latest, node.target.y);
|
|
8423
|
-
return `${x}% ${y}%`;
|
|
8424
|
-
},
|
|
8425
|
-
};
|
|
8426
|
-
|
|
8427
|
-
const correctBoxShadow = {
|
|
8428
|
-
correct: (latest, { treeScale, projectionDelta }) => {
|
|
8429
|
-
const original = latest;
|
|
8430
|
-
const shadow = complex.parse(latest);
|
|
8431
|
-
// TODO: Doesn't support multiple shadows
|
|
8432
|
-
if (shadow.length > 5)
|
|
8433
|
-
return original;
|
|
8434
|
-
const template = complex.createTransformer(latest);
|
|
8435
|
-
const offset = typeof shadow[0] !== "number" ? 1 : 0;
|
|
8436
|
-
// Calculate the overall context scale
|
|
8437
|
-
const xScale = projectionDelta.x.scale * treeScale.x;
|
|
8438
|
-
const yScale = projectionDelta.y.scale * treeScale.y;
|
|
8439
|
-
shadow[0 + offset] /= xScale;
|
|
8440
|
-
shadow[1 + offset] /= yScale;
|
|
8441
|
-
/**
|
|
8442
|
-
* Ideally we'd correct x and y scales individually, but because blur and
|
|
8443
|
-
* spread apply to both we have to take a scale average and apply that instead.
|
|
8444
|
-
* We could potentially improve the outcome of this by incorporating the ratio between
|
|
8445
|
-
* the two scales.
|
|
8446
|
-
*/
|
|
8447
|
-
const averageScale = mixNumber$1(xScale, yScale, 0.5);
|
|
8448
|
-
// Blur
|
|
8449
|
-
if (typeof shadow[2 + offset] === "number")
|
|
8450
|
-
shadow[2 + offset] /= averageScale;
|
|
8451
|
-
// Spread
|
|
8452
|
-
if (typeof shadow[3 + offset] === "number")
|
|
8453
|
-
shadow[3 + offset] /= averageScale;
|
|
8454
|
-
return template(shadow);
|
|
8455
|
-
},
|
|
8456
|
-
};
|
|
8457
|
-
|
|
8458
|
-
const scaleCorrectors = {
|
|
8459
|
-
borderRadius: {
|
|
8460
|
-
...correctBorderRadius,
|
|
8461
|
-
applyTo: [...cornerRadiusProps],
|
|
8462
|
-
},
|
|
8463
|
-
borderTopLeftRadius: correctBorderRadius,
|
|
8464
|
-
borderTopRightRadius: correctBorderRadius,
|
|
8465
|
-
borderBottomLeftRadius: correctBorderRadius,
|
|
8466
|
-
borderBottomRightRadius: correctBorderRadius,
|
|
8467
|
-
boxShadow: correctBoxShadow,
|
|
8468
|
-
};
|
|
8406
|
+
const scaleCorrectors = {};
|
|
8469
8407
|
function addScaleCorrector(correctors) {
|
|
8470
8408
|
for (const key in correctors) {
|
|
8471
8409
|
scaleCorrectors[key] = correctors[key];
|
|
@@ -8639,7 +8577,24 @@ class SVGVisualElement extends DOMVisualElement {
|
|
|
8639
8577
|
this.measureInstanceViewportBox = createBox;
|
|
8640
8578
|
}
|
|
8641
8579
|
getBaseTargetFromProps(props, key) {
|
|
8642
|
-
|
|
8580
|
+
/**
|
|
8581
|
+
* An independent transform's base is never an attribute of the same
|
|
8582
|
+
* name, e.g. <rect x> is a position, not a translate. Like HTML it
|
|
8583
|
+
* comes from style, falling back to the transform's default.
|
|
8584
|
+
*/
|
|
8585
|
+
return transformProps.has(key)
|
|
8586
|
+
? super.getBaseTargetFromProps(props, key) ??
|
|
8587
|
+
defaultTransformValue(key)
|
|
8588
|
+
: props[key];
|
|
8589
|
+
}
|
|
8590
|
+
/**
|
|
8591
|
+
* An independent transform's origin is never read from the DOM, so it
|
|
8592
|
+
* starts from its latest or default value and renders on mount.
|
|
8593
|
+
*/
|
|
8594
|
+
getDefaultValue(key) {
|
|
8595
|
+
return transformProps.has(key)
|
|
8596
|
+
? this.latestValues[key] ?? defaultTransformValue(key)
|
|
8597
|
+
: undefined;
|
|
8643
8598
|
}
|
|
8644
8599
|
readValueFromInstance(instance, key) {
|
|
8645
8600
|
if (transformProps.has(key)) {
|
|
@@ -9252,6 +9207,75 @@ function eachAxis(callback) {
|
|
|
9252
9207
|
return [callback("x"), callback("y")];
|
|
9253
9208
|
}
|
|
9254
9209
|
|
|
9210
|
+
function pixelsToPercent(pixels, axis) {
|
|
9211
|
+
if (axis.max === axis.min)
|
|
9212
|
+
return 0;
|
|
9213
|
+
return (pixels / (axis.max - axis.min)) * 100;
|
|
9214
|
+
}
|
|
9215
|
+
/**
|
|
9216
|
+
* We always correct borderRadius as a percentage rather than pixels to reduce paints.
|
|
9217
|
+
* For example, if you are projecting a box that is 100px wide with a 10px borderRadius
|
|
9218
|
+
* into a box that is 200px wide with a 20px borderRadius, that is actually a 10%
|
|
9219
|
+
* borderRadius in both states. If we animate between the two in pixels that will trigger
|
|
9220
|
+
* a paint each time. If we animate between the two in percentage we'll avoid a paint.
|
|
9221
|
+
*/
|
|
9222
|
+
const correctBorderRadius = {
|
|
9223
|
+
correct: (latest, node) => {
|
|
9224
|
+
if (!node.target)
|
|
9225
|
+
return latest;
|
|
9226
|
+
/**
|
|
9227
|
+
* If latest is a string, if it's a percentage we can return immediately as it's
|
|
9228
|
+
* going to be stretched appropriately. Otherwise, if it's a pixel, convert it to a number.
|
|
9229
|
+
*/
|
|
9230
|
+
if (typeof latest === "string") {
|
|
9231
|
+
if (px.test(latest)) {
|
|
9232
|
+
latest = parseFloat(latest);
|
|
9233
|
+
}
|
|
9234
|
+
else {
|
|
9235
|
+
return latest;
|
|
9236
|
+
}
|
|
9237
|
+
}
|
|
9238
|
+
/**
|
|
9239
|
+
* If latest is a number, it's a pixel value. We use the current viewportBox to calculate that
|
|
9240
|
+
* pixel value as a percentage of each axis
|
|
9241
|
+
*/
|
|
9242
|
+
const x = pixelsToPercent(latest, node.target.x);
|
|
9243
|
+
const y = pixelsToPercent(latest, node.target.y);
|
|
9244
|
+
return `${x}% ${y}%`;
|
|
9245
|
+
},
|
|
9246
|
+
};
|
|
9247
|
+
|
|
9248
|
+
const correctBoxShadow = {
|
|
9249
|
+
correct: (latest, { treeScale, projectionDelta }) => {
|
|
9250
|
+
const original = latest;
|
|
9251
|
+
const shadow = complex.parse(latest);
|
|
9252
|
+
// TODO: Doesn't support multiple shadows
|
|
9253
|
+
if (shadow.length > 5)
|
|
9254
|
+
return original;
|
|
9255
|
+
const template = complex.createTransformer(latest);
|
|
9256
|
+
const offset = typeof shadow[0] !== "number" ? 1 : 0;
|
|
9257
|
+
// Calculate the overall context scale
|
|
9258
|
+
const xScale = projectionDelta.x.scale * treeScale.x;
|
|
9259
|
+
const yScale = projectionDelta.y.scale * treeScale.y;
|
|
9260
|
+
shadow[0 + offset] /= xScale;
|
|
9261
|
+
shadow[1 + offset] /= yScale;
|
|
9262
|
+
/**
|
|
9263
|
+
* Ideally we'd correct x and y scales individually, but because blur and
|
|
9264
|
+
* spread apply to both we have to take a scale average and apply that instead.
|
|
9265
|
+
* We could potentially improve the outcome of this by incorporating the ratio between
|
|
9266
|
+
* the two scales.
|
|
9267
|
+
*/
|
|
9268
|
+
const averageScale = mixNumber$1(xScale, yScale, 0.5);
|
|
9269
|
+
// Blur
|
|
9270
|
+
if (typeof shadow[2 + offset] === "number")
|
|
9271
|
+
shadow[2 + offset] /= averageScale;
|
|
9272
|
+
// Spread
|
|
9273
|
+
if (typeof shadow[3 + offset] === "number")
|
|
9274
|
+
shadow[3 + offset] /= averageScale;
|
|
9275
|
+
return template(shadow);
|
|
9276
|
+
},
|
|
9277
|
+
};
|
|
9278
|
+
|
|
9255
9279
|
function buildProjectionTransform(delta, treeScale, latestTransform) {
|
|
9256
9280
|
let transform = "";
|
|
9257
9281
|
/**
|
|
@@ -9304,7 +9328,6 @@ function buildProjectionTransform(delta, treeScale, latestTransform) {
|
|
|
9304
9328
|
}
|
|
9305
9329
|
|
|
9306
9330
|
const numBorders = cornerRadiusProps.length;
|
|
9307
|
-
const asNumber = (value) => typeof value === "string" ? parseFloat(value) : value;
|
|
9308
9331
|
const isPx = (value) => typeof value === "number" || px.test(value);
|
|
9309
9332
|
function mixValues(target, follow, lead, progress, shouldCrossfadeOpacity, isOnlyMember) {
|
|
9310
9333
|
if (shouldCrossfadeOpacity) {
|
|
@@ -9503,6 +9526,18 @@ class NodeStack {
|
|
|
9503
9526
|
}
|
|
9504
9527
|
}
|
|
9505
9528
|
|
|
9529
|
+
const defaultScaleCorrectors = {
|
|
9530
|
+
borderRadius: {
|
|
9531
|
+
...correctBorderRadius,
|
|
9532
|
+
applyTo: [...cornerRadiusProps],
|
|
9533
|
+
},
|
|
9534
|
+
borderTopLeftRadius: correctBorderRadius,
|
|
9535
|
+
borderTopRightRadius: correctBorderRadius,
|
|
9536
|
+
borderBottomLeftRadius: correctBorderRadius,
|
|
9537
|
+
borderBottomRightRadius: correctBorderRadius,
|
|
9538
|
+
boxShadow: correctBoxShadow,
|
|
9539
|
+
};
|
|
9540
|
+
|
|
9506
9541
|
/**
|
|
9507
9542
|
* This should only ever be modified on the client otherwise it'll
|
|
9508
9543
|
* persist through server requests. If we need instanced states we
|
|
@@ -9521,6 +9556,14 @@ const globalProjectionState = {
|
|
|
9521
9556
|
hasEverUpdated: false,
|
|
9522
9557
|
};
|
|
9523
9558
|
|
|
9559
|
+
/**
|
|
9560
|
+
* Correctors registered via addScaleCorrector before projection loaded
|
|
9561
|
+
* (e.g. with an async LazyMotion bundle) take precedence over the defaults.
|
|
9562
|
+
*/
|
|
9563
|
+
Object.assign(scaleCorrectors, {
|
|
9564
|
+
...defaultScaleCorrectors,
|
|
9565
|
+
...scaleCorrectors,
|
|
9566
|
+
});
|
|
9524
9567
|
const metrics = {
|
|
9525
9568
|
nodes: 0,
|
|
9526
9569
|
calculatedTargetDeltas: 0,
|
|
@@ -9658,7 +9701,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
|
|
|
9658
9701
|
*/
|
|
9659
9702
|
this.eventHandlers = new Map();
|
|
9660
9703
|
this.hasTreeAnimated = false;
|
|
9661
|
-
this.layoutVersion = 0;
|
|
9662
9704
|
// Note: Currently only running on root node
|
|
9663
9705
|
this.updateScheduled = false;
|
|
9664
9706
|
this.scheduleUpdate = () => this.update();
|
|
@@ -9698,7 +9740,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
|
|
|
9698
9740
|
* Frame calculations
|
|
9699
9741
|
*/
|
|
9700
9742
|
this.resolvedRelativeTargetAt = 0.0;
|
|
9701
|
-
this.linkedParentVersion = 0;
|
|
9702
9743
|
this.hasProjected = false;
|
|
9703
9744
|
this.isVisible = true;
|
|
9704
9745
|
this.animationProgress = 0;
|
|
@@ -9979,6 +10020,7 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
|
|
|
9979
10020
|
* get measured even when memoized (willUpdate skipped).
|
|
9980
10021
|
*/
|
|
9981
10022
|
this.nodes.forEach(ensureDraggedNodesSnapshotted);
|
|
10023
|
+
this.nodes.forEach(syncRelativeLayout);
|
|
9982
10024
|
/**
|
|
9983
10025
|
* Write
|
|
9984
10026
|
*/
|
|
@@ -10076,7 +10118,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
|
|
|
10076
10118
|
}
|
|
10077
10119
|
const prevLayout = this.layout;
|
|
10078
10120
|
this.layout = this.measure(false);
|
|
10079
|
-
this.layoutVersion++;
|
|
10080
10121
|
if (!this.layoutCorrected)
|
|
10081
10122
|
this.layoutCorrected = createBox();
|
|
10082
10123
|
this.isLayoutDirty = false;
|
|
@@ -10296,11 +10337,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
|
|
|
10296
10337
|
return;
|
|
10297
10338
|
this.resolvedRelativeTargetAt = frameData.timestamp;
|
|
10298
10339
|
const relativeParent = this.getClosestProjectingParent();
|
|
10299
|
-
if (relativeParent &&
|
|
10300
|
-
this.linkedParentVersion !== relativeParent.layoutVersion &&
|
|
10301
|
-
!relativeParent.options.layoutRoot) {
|
|
10302
|
-
this.removeRelativeTarget();
|
|
10303
|
-
}
|
|
10304
10340
|
/**
|
|
10305
10341
|
* If we don't have a targetDelta but do have a layout, we can attempt to resolve
|
|
10306
10342
|
* a relativeParent. This will allow a component to perform scale correction
|
|
@@ -10403,7 +10439,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
|
|
|
10403
10439
|
}
|
|
10404
10440
|
createRelativeTarget(relativeParent, layout, parentLayout) {
|
|
10405
10441
|
this.relativeParent = relativeParent;
|
|
10406
|
-
this.linkedParentVersion = relativeParent.layoutVersion;
|
|
10407
10442
|
this.forceRelativeParentToResolveTarget();
|
|
10408
10443
|
this.relativeTarget = createBox();
|
|
10409
10444
|
this.relativeTargetOrigin = createBox();
|
|
@@ -11113,6 +11148,26 @@ function ensureDraggedNodesSnapshotted(node) {
|
|
|
11113
11148
|
node.isLayoutDirty = true;
|
|
11114
11149
|
}
|
|
11115
11150
|
}
|
|
11151
|
+
/**
|
|
11152
|
+
* If a node's relative parent is about to be re-measured but the node isn't
|
|
11153
|
+
* (e.g. it's in another LayoutGroup), its relative target is out of date.
|
|
11154
|
+
* If it's layout animating, re-measure it too, so its relative target keeps
|
|
11155
|
+
* resolving against current layouts. Otherwise its layout is still valid
|
|
11156
|
+
* relative to its parent, so drop the relative target, and any targetDelta
|
|
11157
|
+
* left over from a finished animation while an ancestor still animates, to be
|
|
11158
|
+
* rebuilt from its layout.
|
|
11159
|
+
*/
|
|
11160
|
+
function syncRelativeLayout(node) {
|
|
11161
|
+
if (node.relativeTarget && node.relativeParent?.isLayoutDirty) {
|
|
11162
|
+
if (node.currentAnimation) {
|
|
11163
|
+
node.isLayoutDirty = true;
|
|
11164
|
+
}
|
|
11165
|
+
else {
|
|
11166
|
+
node.targetDelta = undefined;
|
|
11167
|
+
node.removeRelativeTarget();
|
|
11168
|
+
}
|
|
11169
|
+
}
|
|
11170
|
+
}
|
|
11116
11171
|
function resetTransformStyle(node) {
|
|
11117
11172
|
const { visualElement } = node.options;
|
|
11118
11173
|
if (visualElement && visualElement.getProps().onBeforeLayoutMeasure) {
|
|
@@ -11808,7 +11863,6 @@ exports.motionValue = motionValue;
|
|
|
11808
11863
|
exports.nodeGroup = nodeGroup;
|
|
11809
11864
|
exports.number = number;
|
|
11810
11865
|
exports.numberValueTypes = numberValueTypes;
|
|
11811
|
-
exports.observeTimeline = observeTimeline;
|
|
11812
11866
|
exports.optimizedAppearDataAttribute = optimizedAppearDataAttribute;
|
|
11813
11867
|
exports.optimizedAppearDataId = optimizedAppearDataId;
|
|
11814
11868
|
exports.parseAnimateLayoutArgs = parseAnimateLayoutArgs;
|