motion-dom 13.4.4 → 13.4.5
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 +60 -73
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/animation/generators/spring.mjs +39 -64
- package/dist/es/animation/generators/spring.mjs.map +1 -1
- package/dist/es/projection/node/create-projection-node.mjs +21 -9
- package/dist/es/projection/node/create-projection-node.mjs.map +1 -1
- package/dist/index.d.ts +1 -8
- package/dist/motion-dom.dev.js +60 -73
- package/dist/motion-dom.js +1 -1
- package/package.json +2 -2
package/dist/cjs/index.js
CHANGED
|
@@ -812,7 +812,6 @@ const springDefaults = {
|
|
|
812
812
|
stiffness: 100,
|
|
813
813
|
damping: 10,
|
|
814
814
|
mass: 1.0,
|
|
815
|
-
velocity: 0.0,
|
|
816
815
|
// Default duration/bounce-based options
|
|
817
816
|
duration: 800, // in ms
|
|
818
817
|
bounce: 0.3,
|
|
@@ -847,7 +846,12 @@ function approximateRoot(envelope, derivative, initialGuess) {
|
|
|
847
846
|
* This is ported from the Framer implementation of duration-based spring resolution.
|
|
848
847
|
*/
|
|
849
848
|
const safeMin = 0.001;
|
|
850
|
-
|
|
849
|
+
/**
|
|
850
|
+
* Assumes zero initial velocity and the default mass: time-defined springs
|
|
851
|
+
* ignore inherited velocity and only resolve without valid physics. Returns
|
|
852
|
+
* NaN physics when the root search doesn't converge.
|
|
853
|
+
*/
|
|
854
|
+
function findSpring({ duration = springDefaults.duration, bounce = springDefaults.bounce, }) {
|
|
851
855
|
let envelope;
|
|
852
856
|
let derivative;
|
|
853
857
|
motionUtils.warning(duration <= motionUtils.secondsToMilliseconds(springDefaults.maxDuration), "Spring duration must be 10 seconds or less", "spring-duration-limit");
|
|
@@ -864,15 +868,13 @@ function findSpring({ duration = springDefaults.duration, bounce = springDefault
|
|
|
864
868
|
envelope = (undampedFreq) => {
|
|
865
869
|
const exponentialDecay = undampedFreq * dampingRatio;
|
|
866
870
|
const delta = exponentialDecay * duration;
|
|
867
|
-
const a = exponentialDecay - velocity;
|
|
868
871
|
const b = calcAngularFreq(undampedFreq, dampingRatio);
|
|
869
872
|
const c = Math.exp(-delta);
|
|
870
|
-
return safeMin - (
|
|
873
|
+
return safeMin - (exponentialDecay / b) * c;
|
|
871
874
|
};
|
|
872
875
|
derivative = (undampedFreq) => {
|
|
873
876
|
const exponentialDecay = undampedFreq * dampingRatio;
|
|
874
877
|
const delta = exponentialDecay * duration;
|
|
875
|
-
const d = delta * velocity + velocity;
|
|
876
878
|
const e = dampingRatio *
|
|
877
879
|
dampingRatio *
|
|
878
880
|
undampedFreq *
|
|
@@ -881,7 +883,7 @@ function findSpring({ duration = springDefaults.duration, bounce = springDefault
|
|
|
881
883
|
const f = Math.exp(-delta);
|
|
882
884
|
const g = calcAngularFreq(undampedFreq * undampedFreq, dampingRatio);
|
|
883
885
|
const factor = -envelope(undampedFreq) + safeMin > 0 ? -1 : 1;
|
|
884
|
-
return (factor *
|
|
886
|
+
return (factor * -e * f) / g;
|
|
885
887
|
};
|
|
886
888
|
}
|
|
887
889
|
else {
|
|
@@ -890,37 +892,23 @@ function findSpring({ duration = springDefaults.duration, bounce = springDefault
|
|
|
890
892
|
*/
|
|
891
893
|
envelope = (undampedFreq) => {
|
|
892
894
|
const a = Math.exp(-undampedFreq * duration);
|
|
893
|
-
const b =
|
|
895
|
+
const b = undampedFreq * duration + 1;
|
|
894
896
|
return -safeMin + a * b;
|
|
895
897
|
};
|
|
896
898
|
derivative = (undampedFreq) => {
|
|
897
899
|
const a = Math.exp(-undampedFreq * duration);
|
|
898
|
-
const b =
|
|
900
|
+
const b = -undampedFreq * (duration * duration);
|
|
899
901
|
return a * b;
|
|
900
902
|
};
|
|
901
903
|
}
|
|
902
904
|
const initialGuess = 5 / duration;
|
|
903
905
|
const undampedFreq = approximateRoot(envelope, derivative, initialGuess);
|
|
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);
|
|
906
|
+
const stiffness = undampedFreq * undampedFreq;
|
|
907
|
+
return {
|
|
908
|
+
stiffness,
|
|
909
|
+
damping: dampingRatio * 2 * Math.sqrt(stiffness),
|
|
910
|
+
duration: motionUtils.secondsToMilliseconds(duration),
|
|
911
|
+
};
|
|
924
912
|
}
|
|
925
913
|
/**
|
|
926
914
|
* Spring physics must be finite. stiffness and mass are also divisors so must
|
|
@@ -952,7 +940,7 @@ function getSpringOptions(options) {
|
|
|
952
940
|
const validStiffness = resolvePhysics(options.stiffness);
|
|
953
941
|
const validDamping = resolvePhysics(options.damping, true);
|
|
954
942
|
const validMass = resolvePhysics(options.mass);
|
|
955
|
-
|
|
943
|
+
const springOptions = {
|
|
956
944
|
...options,
|
|
957
945
|
stiffness: validStiffness ?? springDefaults.stiffness,
|
|
958
946
|
damping: validDamping ?? springDefaults.damping,
|
|
@@ -960,42 +948,27 @@ function getSpringOptions(options) {
|
|
|
960
948
|
isResolvedFromDuration: false,
|
|
961
949
|
// stiffness/damping/mass overrides duration/bounce
|
|
962
950
|
isTimeDefined: (validStiffness ?? validDamping ?? validMass) === undefined &&
|
|
963
|
-
|
|
951
|
+
(options.duration !== undefined || options.bounce !== undefined),
|
|
964
952
|
};
|
|
965
953
|
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
954
|
if (options.visualDuration) {
|
|
972
|
-
const
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
springOptions = {
|
|
979
|
-
...springOptions,
|
|
980
|
-
mass: springDefaults.mass,
|
|
981
|
-
stiffness,
|
|
982
|
-
damping,
|
|
983
|
-
};
|
|
955
|
+
const root = (2 * Math.PI) / (options.visualDuration * 1.2);
|
|
956
|
+
springOptions.stiffness = root * root;
|
|
957
|
+
springOptions.damping =
|
|
958
|
+
2 *
|
|
959
|
+
motionUtils.clamp(0.05, 1, 1 - (options.bounce || 0)) *
|
|
960
|
+
Math.sqrt(springOptions.stiffness);
|
|
984
961
|
}
|
|
985
962
|
else {
|
|
986
|
-
|
|
987
|
-
springOptions = {
|
|
988
|
-
...springOptions,
|
|
989
|
-
...derived,
|
|
990
|
-
mass: springDefaults.mass,
|
|
991
|
-
};
|
|
963
|
+
Object.assign(springOptions, findSpring(springOptions));
|
|
992
964
|
springOptions.isResolvedFromDuration = true;
|
|
993
965
|
}
|
|
994
966
|
/**
|
|
995
|
-
*
|
|
996
|
-
*
|
|
997
|
-
*
|
|
998
|
-
* between them is never
|
|
967
|
+
* Time options can degenerate: a NaN bounce gives a NaN damping, an
|
|
968
|
+
* infinite visualDuration a 0 stiffness, and findSpring NaN for both
|
|
969
|
+
* when it doesn't converge. Replace the two together, so the
|
|
970
|
+
* relationship duration resolution establishes between them is never
|
|
971
|
+
* left half-overwritten.
|
|
999
972
|
*/
|
|
1000
973
|
if (!isValidPhysics(springOptions.stiffness) ||
|
|
1001
974
|
!isValidPhysics(springOptions.damping, true)) {
|
|
@@ -1020,10 +993,13 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
|
|
|
1020
993
|
* to reduce GC during animation.
|
|
1021
994
|
*/
|
|
1022
995
|
const state = { done: false, value: origin };
|
|
1023
|
-
const { stiffness, damping, mass, duration,
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
996
|
+
const { stiffness, damping, mass, duration, isResolvedFromDuration, isTimeDefined, } = getSpringOptions({ ...options });
|
|
997
|
+
/**
|
|
998
|
+
* Time-defined springs ignore inherited velocity. Velocity from
|
|
999
|
+
* interrupted animations causes massive oscillation on small-range
|
|
1000
|
+
* animations.
|
|
1001
|
+
*/
|
|
1002
|
+
const inheritVelocity = (velocity) => isTimeDefined ? 0 : -motionUtils.millisecondsToSeconds(velocity);
|
|
1027
1003
|
const dampingRatio = damping / (2 * Math.sqrt(stiffness * mass));
|
|
1028
1004
|
const undampedAngularFreq = motionUtils.millisecondsToSeconds(Math.sqrt(stiffness / mass));
|
|
1029
1005
|
const decay = dampingRatio * undampedAngularFreq;
|
|
@@ -1037,7 +1013,7 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
|
|
|
1037
1013
|
const s = {
|
|
1038
1014
|
target,
|
|
1039
1015
|
delta: target - origin,
|
|
1040
|
-
velocity: velocity || 0
|
|
1016
|
+
velocity: inheritVelocity(options.velocity || 0) || 0,
|
|
1041
1017
|
restSpeed: 0,
|
|
1042
1018
|
restDelta: 0,
|
|
1043
1019
|
};
|
|
@@ -1153,8 +1129,7 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
|
|
|
1153
1129
|
retarget: (keyframes, newVelocity) => {
|
|
1154
1130
|
s.target = keyframes[keyframes.length - 1];
|
|
1155
1131
|
s.delta = s.target - keyframes[0];
|
|
1156
|
-
|
|
1157
|
-
s.velocity = isTimeDefined ? 0 : -motionUtils.millisecondsToSeconds(newVelocity);
|
|
1132
|
+
s.velocity = inheritVelocity(newVelocity);
|
|
1158
1133
|
// Default thresholds depend on the scale of the new delta
|
|
1159
1134
|
if (!(options.restSpeed && options.restDelta))
|
|
1160
1135
|
setRestThresholds();
|
|
@@ -9658,7 +9633,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
|
|
|
9658
9633
|
*/
|
|
9659
9634
|
this.eventHandlers = new Map();
|
|
9660
9635
|
this.hasTreeAnimated = false;
|
|
9661
|
-
this.layoutVersion = 0;
|
|
9662
9636
|
// Note: Currently only running on root node
|
|
9663
9637
|
this.updateScheduled = false;
|
|
9664
9638
|
this.scheduleUpdate = () => this.update();
|
|
@@ -9698,7 +9672,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
|
|
|
9698
9672
|
* Frame calculations
|
|
9699
9673
|
*/
|
|
9700
9674
|
this.resolvedRelativeTargetAt = 0.0;
|
|
9701
|
-
this.linkedParentVersion = 0;
|
|
9702
9675
|
this.hasProjected = false;
|
|
9703
9676
|
this.isVisible = true;
|
|
9704
9677
|
this.animationProgress = 0;
|
|
@@ -9979,6 +9952,7 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
|
|
|
9979
9952
|
* get measured even when memoized (willUpdate skipped).
|
|
9980
9953
|
*/
|
|
9981
9954
|
this.nodes.forEach(ensureDraggedNodesSnapshotted);
|
|
9955
|
+
this.nodes.forEach(syncRelativeLayout);
|
|
9982
9956
|
/**
|
|
9983
9957
|
* Write
|
|
9984
9958
|
*/
|
|
@@ -10076,7 +10050,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
|
|
|
10076
10050
|
}
|
|
10077
10051
|
const prevLayout = this.layout;
|
|
10078
10052
|
this.layout = this.measure(false);
|
|
10079
|
-
this.layoutVersion++;
|
|
10080
10053
|
if (!this.layoutCorrected)
|
|
10081
10054
|
this.layoutCorrected = createBox();
|
|
10082
10055
|
this.isLayoutDirty = false;
|
|
@@ -10296,11 +10269,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
|
|
|
10296
10269
|
return;
|
|
10297
10270
|
this.resolvedRelativeTargetAt = frameData.timestamp;
|
|
10298
10271
|
const relativeParent = this.getClosestProjectingParent();
|
|
10299
|
-
if (relativeParent &&
|
|
10300
|
-
this.linkedParentVersion !== relativeParent.layoutVersion &&
|
|
10301
|
-
!relativeParent.options.layoutRoot) {
|
|
10302
|
-
this.removeRelativeTarget();
|
|
10303
|
-
}
|
|
10304
10272
|
/**
|
|
10305
10273
|
* If we don't have a targetDelta but do have a layout, we can attempt to resolve
|
|
10306
10274
|
* a relativeParent. This will allow a component to perform scale correction
|
|
@@ -10403,7 +10371,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
|
|
|
10403
10371
|
}
|
|
10404
10372
|
createRelativeTarget(relativeParent, layout, parentLayout) {
|
|
10405
10373
|
this.relativeParent = relativeParent;
|
|
10406
|
-
this.linkedParentVersion = relativeParent.layoutVersion;
|
|
10407
10374
|
this.forceRelativeParentToResolveTarget();
|
|
10408
10375
|
this.relativeTarget = createBox();
|
|
10409
10376
|
this.relativeTargetOrigin = createBox();
|
|
@@ -11113,6 +11080,26 @@ function ensureDraggedNodesSnapshotted(node) {
|
|
|
11113
11080
|
node.isLayoutDirty = true;
|
|
11114
11081
|
}
|
|
11115
11082
|
}
|
|
11083
|
+
/**
|
|
11084
|
+
* If a node's relative parent is about to be re-measured but the node isn't
|
|
11085
|
+
* (e.g. it's in another LayoutGroup), its relative target is out of date.
|
|
11086
|
+
* If it's layout animating, re-measure it too, so its relative target keeps
|
|
11087
|
+
* resolving against current layouts. Otherwise its layout is still valid
|
|
11088
|
+
* relative to its parent, so drop the relative target, and any targetDelta
|
|
11089
|
+
* left over from a finished animation while an ancestor still animates, to be
|
|
11090
|
+
* rebuilt from its layout.
|
|
11091
|
+
*/
|
|
11092
|
+
function syncRelativeLayout(node) {
|
|
11093
|
+
if (node.relativeTarget && node.relativeParent?.isLayoutDirty) {
|
|
11094
|
+
if (node.currentAnimation) {
|
|
11095
|
+
node.isLayoutDirty = true;
|
|
11096
|
+
}
|
|
11097
|
+
else {
|
|
11098
|
+
node.targetDelta = undefined;
|
|
11099
|
+
node.removeRelativeTarget();
|
|
11100
|
+
}
|
|
11101
|
+
}
|
|
11102
|
+
}
|
|
11116
11103
|
function resetTransformStyle(node) {
|
|
11117
11104
|
const { visualElement } = node.options;
|
|
11118
11105
|
if (visualElement && visualElement.getProps().onBeforeLayoutMeasure) {
|