motion-dom 12.35.1 → 12.35.2
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 +177 -76
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/animation/JSAnimation.mjs +17 -0
- package/dist/es/animation/JSAnimation.mjs.map +1 -1
- package/dist/es/animation/NativeAnimationExtended.mjs +6 -1
- package/dist/es/animation/NativeAnimationExtended.mjs.map +1 -1
- package/dist/es/animation/generators/inertia.mjs +3 -3
- package/dist/es/animation/generators/inertia.mjs.map +1 -1
- package/dist/es/animation/generators/spring.mjs +329 -0
- package/dist/es/animation/generators/spring.mjs.map +1 -0
- package/dist/es/animation/generators/utils/velocity.mjs +2 -2
- package/dist/es/animation/generators/utils/velocity.mjs.map +1 -1
- package/dist/es/animation/utils/replace-transition-type.mjs +1 -1
- package/dist/es/effects/MotionValueState.mjs +0 -5
- package/dist/es/effects/MotionValueState.mjs.map +1 -1
- package/dist/es/effects/style/transform.mjs +1 -2
- package/dist/es/effects/style/transform.mjs.map +1 -1
- package/dist/es/effects/utils/create-effect.mjs +1 -1
- package/dist/es/effects/utils/create-effect.mjs.map +1 -1
- package/dist/es/index.mjs +1 -1
- package/dist/es/projection/geometry/delta-apply.mjs +5 -4
- package/dist/es/projection/geometry/delta-apply.mjs.map +1 -1
- package/dist/es/projection/node/create-projection-node.mjs +17 -9
- package/dist/es/projection/node/create-projection-node.mjs.map +1 -1
- package/dist/es/value/follow-value.mjs +19 -9
- package/dist/es/value/follow-value.mjs.map +1 -1
- package/dist/es/value/types/complex/index.mjs +27 -5
- package/dist/es/value/types/complex/index.mjs.map +1 -1
- package/dist/index.d.ts +9 -3
- package/dist/motion-dom.dev.js +177 -76
- package/dist/motion-dom.js +1 -1
- package/dist/size-rollup-style-effect.js +1 -1
- package/dist/size-rollup-style-effect.js.map +1 -1
- package/package.json +2 -2
- package/dist/es/animation/generators/spring/defaults.mjs +0 -28
- package/dist/es/animation/generators/spring/defaults.mjs.map +0 -1
- package/dist/es/animation/generators/spring/find.mjs +0 -85
- package/dist/es/animation/generators/spring/find.mjs.map +0 -1
- package/dist/es/animation/generators/spring/index.mjs +0 -181
- package/dist/es/animation/generators/spring/index.mjs.map +0 -1
|
@@ -52,16 +52,23 @@ function attachFollow(value, source, options = {}) {
|
|
|
52
52
|
}
|
|
53
53
|
};
|
|
54
54
|
const startAnimation = () => {
|
|
55
|
-
stopAnimation();
|
|
56
55
|
const currentValue = asNumber(value.get());
|
|
57
56
|
const targetValue = asNumber(latestValue);
|
|
58
57
|
// Don't animate if we're already at the target
|
|
59
58
|
if (currentValue === targetValue) {
|
|
59
|
+
stopAnimation();
|
|
60
60
|
return;
|
|
61
61
|
}
|
|
62
|
+
// Use the running animation's analytical velocity for accuracy,
|
|
63
|
+
// falling back to the MotionValue's velocity for the initial animation.
|
|
64
|
+
// This prevents systematic velocity loss at high frame rates (240hz+).
|
|
65
|
+
const velocity = activeAnimation
|
|
66
|
+
? activeAnimation.getGeneratorVelocity()
|
|
67
|
+
: value.getVelocity();
|
|
68
|
+
stopAnimation();
|
|
62
69
|
activeAnimation = new JSAnimation({
|
|
63
70
|
keyframes: [currentValue, targetValue],
|
|
64
|
-
velocity
|
|
71
|
+
velocity,
|
|
65
72
|
// Default to spring if no type specified (matches useSpring behavior)
|
|
66
73
|
type: "spring",
|
|
67
74
|
restDelta: 0.001,
|
|
@@ -70,16 +77,19 @@ function attachFollow(value, source, options = {}) {
|
|
|
70
77
|
onUpdate: latestSetter,
|
|
71
78
|
});
|
|
72
79
|
};
|
|
80
|
+
// Use a stable function reference so the frame loop Set deduplicates
|
|
81
|
+
// multiple calls within the same frame (e.g. rapid mouse events)
|
|
82
|
+
const scheduleAnimation = () => {
|
|
83
|
+
startAnimation();
|
|
84
|
+
value["events"].animationStart?.notify();
|
|
85
|
+
activeAnimation?.then(() => {
|
|
86
|
+
value["events"].animationComplete?.notify();
|
|
87
|
+
});
|
|
88
|
+
};
|
|
73
89
|
value.attach((v, set) => {
|
|
74
90
|
latestValue = v;
|
|
75
91
|
latestSetter = (latest) => set(parseValue(latest, unit));
|
|
76
|
-
frame.postRender(
|
|
77
|
-
startAnimation();
|
|
78
|
-
value["events"].animationStart?.notify();
|
|
79
|
-
activeAnimation?.then(() => {
|
|
80
|
-
value["events"].animationComplete?.notify();
|
|
81
|
-
});
|
|
82
|
-
});
|
|
92
|
+
frame.postRender(scheduleAnimation);
|
|
83
93
|
}, stopAnimation);
|
|
84
94
|
if (isMotionValue(source)) {
|
|
85
95
|
const removeSourceOnChange = source.on("change", (v) => value.set(parseValue(v, unit)));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"follow-value.mjs","sources":["../../../src/value/follow-value.ts"],"sourcesContent":["import { MotionValue, motionValue } from \".\"\nimport { JSAnimation } from \"../animation/JSAnimation\"\nimport { AnyResolvedKeyframe, ValueAnimationTransition } from \"../animation/types\"\nimport { frame } from \"../frameloop\"\nimport { isMotionValue } from \"./utils/is-motion-value\"\n\n/**\n * Options for useFollowValue hook, extending ValueAnimationTransition\n * but excluding lifecycle callbacks that don't make sense for the hook pattern.\n */\nexport type FollowValueOptions = Omit<\n ValueAnimationTransition,\n \"onUpdate\" | \"onComplete\" | \"onPlay\" | \"onRepeat\" | \"onStop\"\n>\n\n/**\n * Create a `MotionValue` that animates to its latest value using any transition type.\n * Can either be a value or track another `MotionValue`.\n *\n * ```jsx\n * const x = motionValue(0)\n * const y = followValue(x, { type: \"spring\", stiffness: 300 })\n * // or with tween\n * const z = followValue(x, { type: \"tween\", duration: 0.5, ease: \"easeOut\" })\n * ```\n *\n * @param source - Initial value or MotionValue to track\n * @param options - Animation transition options\n * @returns `MotionValue`\n *\n * @public\n */\nexport function followValue<T extends AnyResolvedKeyframe>(\n source: T | MotionValue<T>,\n options?: FollowValueOptions\n) {\n const initialValue = isMotionValue(source) ? source.get() : source\n const value = motionValue(initialValue)\n\n attachFollow(value, source, options)\n\n return value\n}\n\n/**\n * Attach an animation to a MotionValue that will animate whenever the value changes.\n * Similar to attachSpring but supports any transition type (spring, tween, inertia, etc.)\n *\n * @param value - The MotionValue to animate\n * @param source - Initial value or MotionValue to track\n * @param options - Animation transition options\n * @returns Cleanup function\n *\n * @public\n */\nexport function attachFollow<T extends AnyResolvedKeyframe>(\n value: MotionValue<T>,\n source: T | MotionValue<T>,\n options: FollowValueOptions = {}\n): VoidFunction {\n const initialValue = value.get()\n\n let activeAnimation: JSAnimation<number> | null = null\n let latestValue = initialValue\n let latestSetter: (v: T) => void\n\n const unit =\n typeof initialValue === \"string\"\n ? initialValue.replace(/[\\d.-]/g, \"\")\n : undefined\n\n const stopAnimation = () => {\n if (activeAnimation) {\n activeAnimation.stop()\n activeAnimation = null\n }\n }\n\n const startAnimation = () => {\n
|
|
1
|
+
{"version":3,"file":"follow-value.mjs","sources":["../../../src/value/follow-value.ts"],"sourcesContent":["import { MotionValue, motionValue } from \".\"\nimport { JSAnimation } from \"../animation/JSAnimation\"\nimport { AnyResolvedKeyframe, ValueAnimationTransition } from \"../animation/types\"\nimport { frame } from \"../frameloop\"\nimport { isMotionValue } from \"./utils/is-motion-value\"\n\n/**\n * Options for useFollowValue hook, extending ValueAnimationTransition\n * but excluding lifecycle callbacks that don't make sense for the hook pattern.\n */\nexport type FollowValueOptions = Omit<\n ValueAnimationTransition,\n \"onUpdate\" | \"onComplete\" | \"onPlay\" | \"onRepeat\" | \"onStop\"\n>\n\n/**\n * Create a `MotionValue` that animates to its latest value using any transition type.\n * Can either be a value or track another `MotionValue`.\n *\n * ```jsx\n * const x = motionValue(0)\n * const y = followValue(x, { type: \"spring\", stiffness: 300 })\n * // or with tween\n * const z = followValue(x, { type: \"tween\", duration: 0.5, ease: \"easeOut\" })\n * ```\n *\n * @param source - Initial value or MotionValue to track\n * @param options - Animation transition options\n * @returns `MotionValue`\n *\n * @public\n */\nexport function followValue<T extends AnyResolvedKeyframe>(\n source: T | MotionValue<T>,\n options?: FollowValueOptions\n) {\n const initialValue = isMotionValue(source) ? source.get() : source\n const value = motionValue(initialValue)\n\n attachFollow(value, source, options)\n\n return value\n}\n\n/**\n * Attach an animation to a MotionValue that will animate whenever the value changes.\n * Similar to attachSpring but supports any transition type (spring, tween, inertia, etc.)\n *\n * @param value - The MotionValue to animate\n * @param source - Initial value or MotionValue to track\n * @param options - Animation transition options\n * @returns Cleanup function\n *\n * @public\n */\nexport function attachFollow<T extends AnyResolvedKeyframe>(\n value: MotionValue<T>,\n source: T | MotionValue<T>,\n options: FollowValueOptions = {}\n): VoidFunction {\n const initialValue = value.get()\n\n let activeAnimation: JSAnimation<number> | null = null\n let latestValue = initialValue\n let latestSetter: (v: T) => void\n\n const unit =\n typeof initialValue === \"string\"\n ? initialValue.replace(/[\\d.-]/g, \"\")\n : undefined\n\n const stopAnimation = () => {\n if (activeAnimation) {\n activeAnimation.stop()\n activeAnimation = null\n }\n }\n\n const startAnimation = () => {\n const currentValue = asNumber(value.get())\n const targetValue = asNumber(latestValue)\n\n // Don't animate if we're already at the target\n if (currentValue === targetValue) {\n stopAnimation()\n return\n }\n\n // Use the running animation's analytical velocity for accuracy,\n // falling back to the MotionValue's velocity for the initial animation.\n // This prevents systematic velocity loss at high frame rates (240hz+).\n const velocity = activeAnimation\n ? activeAnimation.getGeneratorVelocity()\n : value.getVelocity()\n\n stopAnimation()\n\n activeAnimation = new JSAnimation({\n keyframes: [currentValue, targetValue],\n velocity,\n // Default to spring if no type specified (matches useSpring behavior)\n type: \"spring\",\n restDelta: 0.001,\n restSpeed: 0.01,\n ...options,\n onUpdate: latestSetter,\n })\n }\n\n // Use a stable function reference so the frame loop Set deduplicates\n // multiple calls within the same frame (e.g. rapid mouse events)\n const scheduleAnimation = () => {\n startAnimation()\n value[\"events\"].animationStart?.notify()\n activeAnimation?.then(() => {\n value[\"events\"].animationComplete?.notify()\n })\n }\n\n value.attach((v, set) => {\n latestValue = v\n latestSetter = (latest) => set(parseValue(latest, unit) as T)\n frame.postRender(scheduleAnimation)\n }, stopAnimation)\n\n if (isMotionValue(source)) {\n const removeSourceOnChange = source.on(\"change\", (v) =>\n value.set(parseValue(v, unit) as T)\n )\n\n const removeValueOnDestroy = value.on(\"destroy\", removeSourceOnChange)\n\n return () => {\n removeSourceOnChange()\n removeValueOnDestroy()\n }\n }\n\n return stopAnimation\n}\n\nfunction parseValue(v: AnyResolvedKeyframe, unit?: string) {\n return unit ? v + unit : v\n}\n\nfunction asNumber(v: AnyResolvedKeyframe) {\n return typeof v === \"number\" ? v : parseFloat(v)\n}\n"],"names":[],"mappings":";;;;;AAeA;;;;;;;;;;;;;;;;AAgBG;AACG,SAAU,WAAW,CACvB,MAA0B,EAC1B,OAA4B,EAAA;AAE5B,IAAA,MAAM,YAAY,GAAG,aAAa,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,EAAE,GAAG,MAAM;AAClE,IAAA,MAAM,KAAK,GAAG,WAAW,CAAC,YAAY,CAAC;AAEvC,IAAA,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC;AAEpC,IAAA,OAAO,KAAK;AAChB;AAEA;;;;;;;;;;AAUG;AACG,SAAU,YAAY,CACxB,KAAqB,EACrB,MAA0B,EAC1B,UAA8B,EAAE,EAAA;AAEhC,IAAA,MAAM,YAAY,GAAG,KAAK,CAAC,GAAG,EAAE;IAEhC,IAAI,eAAe,GAA+B,IAAI;IACtD,IAAI,WAAW,GAAG,YAAY;AAC9B,IAAA,IAAI,YAA4B;AAEhC,IAAA,MAAM,IAAI,GACN,OAAO,YAAY,KAAK;UAClB,YAAY,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE;UAClC,SAAS;IAEnB,MAAM,aAAa,GAAG,MAAK;QACvB,IAAI,eAAe,EAAE;YACjB,eAAe,CAAC,IAAI,EAAE;YACtB,eAAe,GAAG,IAAI;QAC1B;AACJ,IAAA,CAAC;IAED,MAAM,cAAc,GAAG,MAAK;QACxB,MAAM,YAAY,GAAG,QAAQ,CAAC,KAAK,CA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AC,GAAG,CAAC,UAAU,CAAC,CAAC,EAAE,IAAI,CAAM,CAAC,CACtC;QAED,MAAM,oBAAoB,GAAG,KAAK,CAAC,EAAE,CAAC,SAAS,EAAE,oBAAoB,CAAC;AAEtE,QAAA,OAAO,MAAK;AACR,YAAA,oBAAoB,EAAE;AACtB,YAAA,oBAAoB,EAAE;AAC1B,QAAA,CAAC;IACL;AAEA,IAAA,OAAO,aAAa;AACxB;AAEA,SAAS,UAAU,CAAC,CAAsB,EAAE,IAAa,EAAA;IACrD,OAAO,IAAI,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC;AAC9B;AAEA,SAAS,QAAQ,CAAC,CAAsB,EAAA;AACpC,IAAA,OAAO,OAAO,CAAC,KAAK,QAAQ,GAAG,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;AACpD;;;;"}
|
|
@@ -52,8 +52,7 @@ function analyseComplexValue(value) {
|
|
|
52
52
|
function parseComplexValue(v) {
|
|
53
53
|
return analyseComplexValue(v).values;
|
|
54
54
|
}
|
|
55
|
-
function
|
|
56
|
-
const { split, types } = analyseComplexValue(source);
|
|
55
|
+
function buildTransformer({ split, types }) {
|
|
57
56
|
const numSections = split.length;
|
|
58
57
|
return (v) => {
|
|
59
58
|
let output = "";
|
|
@@ -75,11 +74,34 @@ function createTransformer(source) {
|
|
|
75
74
|
return output;
|
|
76
75
|
};
|
|
77
76
|
}
|
|
77
|
+
function createTransformer(source) {
|
|
78
|
+
return buildTransformer(analyseComplexValue(source));
|
|
79
|
+
}
|
|
78
80
|
const convertNumbersToZero = (v) => typeof v === "number" ? 0 : color.test(v) ? color.getAnimatableNone(v) : v;
|
|
81
|
+
/**
|
|
82
|
+
* Convert a parsed value to its zero equivalent, but preserve numbers
|
|
83
|
+
* that act as divisors in CSS calc() expressions.
|
|
84
|
+
*
|
|
85
|
+
* analyseComplexValue extracts numbers from CSS strings and puts the
|
|
86
|
+
* surrounding text into a `split` template array. For example:
|
|
87
|
+
* "calc(var(--gap) / 5)" → values: [var(--gap), 5]
|
|
88
|
+
* split: ["calc(", " / ", ")"]
|
|
89
|
+
*
|
|
90
|
+
* When building a zero-equivalent for animation, naively zeroing all
|
|
91
|
+
* numbers turns the divisor into 0 → "calc(var(--gap) / 0)" → NaN.
|
|
92
|
+
* We detect this by checking whether the text preceding a number
|
|
93
|
+
* (split[i]) ends with "/" — the CSS calc division operator.
|
|
94
|
+
*/
|
|
95
|
+
const convertToZero = (value, splitBefore) => {
|
|
96
|
+
if (typeof value === "number") {
|
|
97
|
+
return splitBefore?.trim().endsWith("/") ? value : 0;
|
|
98
|
+
}
|
|
99
|
+
return convertNumbersToZero(value);
|
|
100
|
+
};
|
|
79
101
|
function getAnimatableNone(v) {
|
|
80
|
-
const
|
|
81
|
-
const transformer =
|
|
82
|
-
return transformer(
|
|
102
|
+
const info = analyseComplexValue(v);
|
|
103
|
+
const transformer = buildTransformer(info);
|
|
104
|
+
return transformer(info.values.map((value, i) => convertToZero(value, info.split[i])));
|
|
83
105
|
}
|
|
84
106
|
const complex = {
|
|
85
107
|
test,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../../../../src/value/types/complex/index.ts"],"sourcesContent":["import { AnyResolvedKeyframe } from \"../../../animation/types\"\nimport { CSSVariableToken } from \"../../../animation/utils/is-css-variable\"\nimport { color } from \"../color\"\nimport { Color } from \"../types\"\nimport { colorRegex } from \"../utils/color-regex\"\nimport { floatRegex } from \"../utils/float-regex\"\nimport { sanitize } from \"../utils/sanitize\"\n\nfunction test(v: any) {\n return (\n isNaN(v) &&\n typeof v === \"string\" &&\n (v.match(floatRegex)?.length || 0) +\n (v.match(colorRegex)?.length || 0) >\n 0\n )\n}\n\nconst NUMBER_TOKEN = \"number\"\nconst COLOR_TOKEN = \"color\"\nconst VAR_TOKEN = \"var\"\nconst VAR_FUNCTION_TOKEN = \"var(\"\nconst SPLIT_TOKEN = \"${}\"\n\nexport type ComplexValues = Array<\n CSSVariableToken | AnyResolvedKeyframe | Color\n>\n\nexport interface ValueIndexes {\n color: number[]\n number: number[]\n var: number[]\n}\n\nexport interface ComplexValueInfo {\n values: ComplexValues\n split: string[]\n indexes: ValueIndexes\n types: Array<keyof ValueIndexes>\n}\n\n// this regex consists of the `singleCssVariableRegex|rgbHSLValueRegex|digitRegex`\nconst complexRegex =\n /var\\s*\\(\\s*--(?:[\\w-]+\\s*|[\\w-]+\\s*,(?:\\s*[^)(\\s]|\\s*\\((?:[^)(]|\\([^)(]*\\))*\\))+\\s*)\\)|#[\\da-f]{3,8}|(?:rgb|hsl)a?\\((?:-?[\\d.]+%?[,\\s]+){2}-?[\\d.]+%?\\s*(?:[,/]\\s*)?(?:\\b\\d+(?:\\.\\d+)?|\\.\\d+)?%?\\)|-?(?:\\d+(?:\\.\\d+)?|\\.\\d+)/giu\n\nexport function analyseComplexValue(\n value: AnyResolvedKeyframe\n): ComplexValueInfo {\n const originalValue = value.toString()\n\n const values: ComplexValues = []\n const indexes: ValueIndexes = {\n color: [],\n number: [],\n var: [],\n }\n const types: Array<keyof ValueIndexes> = []\n\n let i = 0\n const tokenised = originalValue.replace(complexRegex, (parsedValue) => {\n if (color.test(parsedValue)) {\n indexes.color.push(i)\n types.push(COLOR_TOKEN)\n values.push(color.parse(parsedValue))\n } else if (parsedValue.startsWith(VAR_FUNCTION_TOKEN)) {\n indexes.var.push(i)\n types.push(VAR_TOKEN)\n values.push(parsedValue)\n } else {\n indexes.number.push(i)\n types.push(NUMBER_TOKEN)\n values.push(parseFloat(parsedValue))\n }\n ++i\n return SPLIT_TOKEN\n })\n const split = tokenised.split(SPLIT_TOKEN)\n\n return { values, split, indexes, types }\n}\n\nfunction parseComplexValue(v: AnyResolvedKeyframe) {\n return analyseComplexValue(v).values\n}\n\nfunction
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../../../../src/value/types/complex/index.ts"],"sourcesContent":["import { AnyResolvedKeyframe } from \"../../../animation/types\"\nimport { CSSVariableToken } from \"../../../animation/utils/is-css-variable\"\nimport { color } from \"../color\"\nimport { Color } from \"../types\"\nimport { colorRegex } from \"../utils/color-regex\"\nimport { floatRegex } from \"../utils/float-regex\"\nimport { sanitize } from \"../utils/sanitize\"\n\nfunction test(v: any) {\n return (\n isNaN(v) &&\n typeof v === \"string\" &&\n (v.match(floatRegex)?.length || 0) +\n (v.match(colorRegex)?.length || 0) >\n 0\n )\n}\n\nconst NUMBER_TOKEN = \"number\"\nconst COLOR_TOKEN = \"color\"\nconst VAR_TOKEN = \"var\"\nconst VAR_FUNCTION_TOKEN = \"var(\"\nconst SPLIT_TOKEN = \"${}\"\n\nexport type ComplexValues = Array<\n CSSVariableToken | AnyResolvedKeyframe | Color\n>\n\nexport interface ValueIndexes {\n color: number[]\n number: number[]\n var: number[]\n}\n\nexport interface ComplexValueInfo {\n values: ComplexValues\n split: string[]\n indexes: ValueIndexes\n types: Array<keyof ValueIndexes>\n}\n\n// this regex consists of the `singleCssVariableRegex|rgbHSLValueRegex|digitRegex`\nconst complexRegex =\n /var\\s*\\(\\s*--(?:[\\w-]+\\s*|[\\w-]+\\s*,(?:\\s*[^)(\\s]|\\s*\\((?:[^)(]|\\([^)(]*\\))*\\))+\\s*)\\)|#[\\da-f]{3,8}|(?:rgb|hsl)a?\\((?:-?[\\d.]+%?[,\\s]+){2}-?[\\d.]+%?\\s*(?:[,/]\\s*)?(?:\\b\\d+(?:\\.\\d+)?|\\.\\d+)?%?\\)|-?(?:\\d+(?:\\.\\d+)?|\\.\\d+)/giu\n\nexport function analyseComplexValue(\n value: AnyResolvedKeyframe\n): ComplexValueInfo {\n const originalValue = value.toString()\n\n const values: ComplexValues = []\n const indexes: ValueIndexes = {\n color: [],\n number: [],\n var: [],\n }\n const types: Array<keyof ValueIndexes> = []\n\n let i = 0\n const tokenised = originalValue.replace(complexRegex, (parsedValue) => {\n if (color.test(parsedValue)) {\n indexes.color.push(i)\n types.push(COLOR_TOKEN)\n values.push(color.parse(parsedValue))\n } else if (parsedValue.startsWith(VAR_FUNCTION_TOKEN)) {\n indexes.var.push(i)\n types.push(VAR_TOKEN)\n values.push(parsedValue)\n } else {\n indexes.number.push(i)\n types.push(NUMBER_TOKEN)\n values.push(parseFloat(parsedValue))\n }\n ++i\n return SPLIT_TOKEN\n })\n const split = tokenised.split(SPLIT_TOKEN)\n\n return { values, split, indexes, types }\n}\n\nfunction parseComplexValue(v: AnyResolvedKeyframe) {\n return analyseComplexValue(v).values\n}\n\nfunction buildTransformer({ split, types }: ComplexValueInfo) {\n const numSections = split.length\n return (v: Array<CSSVariableToken | Color | number | string>) => {\n let output = \"\"\n for (let i = 0; i < numSections; i++) {\n output += split[i]\n if (v[i] !== undefined) {\n const type = types[i]\n if (type === NUMBER_TOKEN) {\n output += sanitize(v[i] as number)\n } else if (type === COLOR_TOKEN) {\n output += color.transform(v[i] as Color)\n } else {\n output += v[i]\n }\n }\n }\n\n return output\n }\n}\n\nfunction createTransformer(source: AnyResolvedKeyframe) {\n return buildTransformer(analyseComplexValue(source))\n}\n\nconst convertNumbersToZero = (v: number | string) =>\n typeof v === \"number\" ? 0 : color.test(v) ? color.getAnimatableNone(v) : v\n\n/**\n * Convert a parsed value to its zero equivalent, but preserve numbers\n * that act as divisors in CSS calc() expressions.\n *\n * analyseComplexValue extracts numbers from CSS strings and puts the\n * surrounding text into a `split` template array. For example:\n * \"calc(var(--gap) / 5)\" → values: [var(--gap), 5]\n * split: [\"calc(\", \" / \", \")\"]\n *\n * When building a zero-equivalent for animation, naively zeroing all\n * numbers turns the divisor into 0 → \"calc(var(--gap) / 0)\" → NaN.\n * We detect this by checking whether the text preceding a number\n * (split[i]) ends with \"/\" — the CSS calc division operator.\n */\nconst convertToZero = (\n value: ComplexValues[number],\n splitBefore: string\n): ComplexValues[number] => {\n if (typeof value === \"number\") {\n return splitBefore?.trim().endsWith(\"/\") ? value : 0\n }\n return convertNumbersToZero(value as string)\n}\n\nfunction getAnimatableNone(v: AnyResolvedKeyframe) {\n const info = analyseComplexValue(v)\n const transformer = buildTransformer(info)\n return transformer(\n info.values.map((value, i) => convertToZero(value, info.split[i]))\n )\n}\n\nexport const complex = {\n test,\n parse: parseComplexValue,\n createTransformer,\n getAnimatableNone,\n}\n"],"names":[],"mappings":";;;;;AAQA,SAAS,IAAI,CAAC,CAAM,EAAA;AAChB,IAAA,QACI,KAAK,CAAC,CAAC,CAAC;QACR,OAAO,CAAC,KAAK,QAAQ;QACrB,CAAC,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE,MAAM,IAAI,CAAC;aAC5B,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE,MAAM,IAAI,CAAC,CAAC;AAClC,YAAA,CAAC;AAEb;AAEA,MAAM,YAAY,GAAG,QAAQ;AAC7B,MAAM,WAAW,GAAG,OAAO;AAC3B,MAAM,SAAS,GAAG,KAAK;AACvB,MAAM,kBAAkB,GAAG,MAAM;AACjC,MAAM,WAAW,GAAG,KAAK;AAmBzB;AACA,MAAM,YAAY,GACd,iOAAiO;AAE/N,SAAU,mBAAmB,CAC/B,KAA0B,EAAA;AAE1B,IAAA,MAAM,aAAa,GAAG,KAAK,CAAC,QAAQ,EAAE;IAEtC,MAAM,MAAM,GAAkB,EAAE;AAChC,IAAA,MAAM,OAAO,GAAiB;AAC1B,QAAA,KAAK,EAAE,EAAE;AACT,QAAA,MAAM,EAAE,EAAE;AACV,QAAA,GAAG,EAAE,EAAE;KACV;IACD,MAAM,KAAK,GAA8B,EAAE;IAE3C,IAAI,CAAC,GAAG,CAAC;IACT,MAAM,SAAS,GAAG,aAAa,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,WAAW,KAAI;AAClE,QAAA,IAAI,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE;AACzB,YAAA,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;AACrB,YAAA,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC;YACvB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,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G;IACnB,IAAI;AACJ,IAAA,KAAK,EAAE,iBAAiB;IACxB,iBAAiB;IACjB,iBAAiB;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1558,6 +1558,7 @@ interface AnimationState$1<V> {
|
|
|
1558
1558
|
interface KeyframeGenerator<V> {
|
|
1559
1559
|
calculatedDuration: null | number;
|
|
1560
1560
|
next: (t: number) => AnimationState$1<V>;
|
|
1561
|
+
velocity?: (t: number) => number;
|
|
1561
1562
|
toString: () => string;
|
|
1562
1563
|
}
|
|
1563
1564
|
interface DOMValueAnimationOptions<V extends AnyResolvedKeyframe = number> extends ValueAnimationTransition<V> {
|
|
@@ -2372,6 +2373,12 @@ declare class JSAnimation<T extends number | string> extends WithPromise impleme
|
|
|
2372
2373
|
get iterationDuration(): number;
|
|
2373
2374
|
get time(): number;
|
|
2374
2375
|
set time(newTime: number);
|
|
2376
|
+
/**
|
|
2377
|
+
* Returns the generator's velocity at the current time in units/second.
|
|
2378
|
+
* Uses the analytical derivative when available (springs), avoiding
|
|
2379
|
+
* the MotionValue's frame-dependent velocity estimation.
|
|
2380
|
+
*/
|
|
2381
|
+
getGeneratorVelocity(): number;
|
|
2375
2382
|
get speed(): number;
|
|
2376
2383
|
set speed(newSpeed: number);
|
|
2377
2384
|
play(): void;
|
|
@@ -3020,7 +3027,6 @@ declare class MotionValueState {
|
|
|
3020
3027
|
private values;
|
|
3021
3028
|
set(name: string, value: MotionValue, render?: VoidFunction, computed?: MotionValue, useDefaultValueType?: boolean): () => void;
|
|
3022
3029
|
get(name: string): MotionValue | undefined;
|
|
3023
|
-
destroy(): void;
|
|
3024
3030
|
}
|
|
3025
3031
|
|
|
3026
3032
|
declare const addAttrValue: (element: HTMLElement | SVGElement, state: MotionValueState, key: string, value: MotionValue) => () => void;
|
|
@@ -3682,7 +3688,7 @@ interface ComplexValueInfo {
|
|
|
3682
3688
|
}
|
|
3683
3689
|
declare function analyseComplexValue(value: AnyResolvedKeyframe): ComplexValueInfo;
|
|
3684
3690
|
declare function parseComplexValue(v: AnyResolvedKeyframe): ComplexValues;
|
|
3685
|
-
declare function createTransformer(source: AnyResolvedKeyframe): (v:
|
|
3691
|
+
declare function createTransformer(source: AnyResolvedKeyframe): (v: (string | number | Color)[]) => string;
|
|
3686
3692
|
declare function getAnimatableNone$1(v: AnyResolvedKeyframe): string;
|
|
3687
3693
|
declare const complex: {
|
|
3688
3694
|
test: typeof test;
|
|
@@ -4100,7 +4106,7 @@ declare function transformAxis(axis: Axis, axisTranslate?: number, axisScale?: n
|
|
|
4100
4106
|
/**
|
|
4101
4107
|
* Apply a transform to a box from the latest resolved motion values.
|
|
4102
4108
|
*/
|
|
4103
|
-
declare function transformBox(box: Box, transform: ResolvedValues$1): void;
|
|
4109
|
+
declare function transformBox(box: Box, transform: ResolvedValues$1, sourceBox?: Box): void;
|
|
4104
4110
|
|
|
4105
4111
|
declare function calcLength(axis: Axis): number;
|
|
4106
4112
|
declare function isNear(value: number, target: number, maxDistance: number): boolean;
|
package/dist/motion-dom.dev.js
CHANGED
|
@@ -451,8 +451,7 @@
|
|
|
451
451
|
function parseComplexValue(v) {
|
|
452
452
|
return analyseComplexValue(v).values;
|
|
453
453
|
}
|
|
454
|
-
function
|
|
455
|
-
const { split, types } = analyseComplexValue(source);
|
|
454
|
+
function buildTransformer({ split, types }) {
|
|
456
455
|
const numSections = split.length;
|
|
457
456
|
return (v) => {
|
|
458
457
|
let output = "";
|
|
@@ -474,11 +473,34 @@
|
|
|
474
473
|
return output;
|
|
475
474
|
};
|
|
476
475
|
}
|
|
476
|
+
function createTransformer(source) {
|
|
477
|
+
return buildTransformer(analyseComplexValue(source));
|
|
478
|
+
}
|
|
477
479
|
const convertNumbersToZero = (v) => typeof v === "number" ? 0 : color.test(v) ? color.getAnimatableNone(v) : v;
|
|
480
|
+
/**
|
|
481
|
+
* Convert a parsed value to its zero equivalent, but preserve numbers
|
|
482
|
+
* that act as divisors in CSS calc() expressions.
|
|
483
|
+
*
|
|
484
|
+
* analyseComplexValue extracts numbers from CSS strings and puts the
|
|
485
|
+
* surrounding text into a `split` template array. For example:
|
|
486
|
+
* "calc(var(--gap) / 5)" → values: [var(--gap), 5]
|
|
487
|
+
* split: ["calc(", " / ", ")"]
|
|
488
|
+
*
|
|
489
|
+
* When building a zero-equivalent for animation, naively zeroing all
|
|
490
|
+
* numbers turns the divisor into 0 → "calc(var(--gap) / 0)" → NaN.
|
|
491
|
+
* We detect this by checking whether the text preceding a number
|
|
492
|
+
* (split[i]) ends with "/" — the CSS calc division operator.
|
|
493
|
+
*/
|
|
494
|
+
const convertToZero = (value, splitBefore) => {
|
|
495
|
+
if (typeof value === "number") {
|
|
496
|
+
return splitBefore?.trim().endsWith("/") ? value : 0;
|
|
497
|
+
}
|
|
498
|
+
return convertNumbersToZero(value);
|
|
499
|
+
};
|
|
478
500
|
function getAnimatableNone$1(v) {
|
|
479
|
-
const
|
|
480
|
-
const transformer =
|
|
481
|
-
return transformer(
|
|
501
|
+
const info = analyseComplexValue(v);
|
|
502
|
+
const transformer = buildTransformer(info);
|
|
503
|
+
return transformer(info.values.map((value, i) => convertToZero(value, info.split[i])));
|
|
482
504
|
}
|
|
483
505
|
const complex = {
|
|
484
506
|
test,
|
|
@@ -757,12 +779,6 @@
|
|
|
757
779
|
};
|
|
758
780
|
}
|
|
759
781
|
|
|
760
|
-
const velocitySampleDuration = 5; // ms
|
|
761
|
-
function calcGeneratorVelocity(resolveValue, t, current) {
|
|
762
|
-
const prevT = Math.max(t - velocitySampleDuration, 0);
|
|
763
|
-
return motionUtils.velocityPerSecond(current - resolveValue(prevT), t - prevT);
|
|
764
|
-
}
|
|
765
|
-
|
|
766
782
|
const springDefaults = {
|
|
767
783
|
// Default spring physics
|
|
768
784
|
stiffness: 100,
|
|
@@ -788,7 +804,20 @@
|
|
|
788
804
|
minDamping: 0.05,
|
|
789
805
|
maxDamping: 1,
|
|
790
806
|
};
|
|
791
|
-
|
|
807
|
+
function calcAngularFreq(undampedFreq, dampingRatio) {
|
|
808
|
+
return undampedFreq * Math.sqrt(1 - dampingRatio * dampingRatio);
|
|
809
|
+
}
|
|
810
|
+
const rootIterations = 12;
|
|
811
|
+
function approximateRoot(envelope, derivative, initialGuess) {
|
|
812
|
+
let result = initialGuess;
|
|
813
|
+
for (let i = 1; i < rootIterations; i++) {
|
|
814
|
+
result = result - envelope(result) / derivative(result);
|
|
815
|
+
}
|
|
816
|
+
return result;
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* This is ported from the Framer implementation of duration-based spring resolution.
|
|
820
|
+
*/
|
|
792
821
|
const safeMin = 0.001;
|
|
793
822
|
function findSpring({ duration = springDefaults.duration, bounce = springDefaults.bounce, velocity = springDefaults.velocity, mass = springDefaults.mass, }) {
|
|
794
823
|
let envelope;
|
|
@@ -857,18 +886,6 @@
|
|
|
857
886
|
};
|
|
858
887
|
}
|
|
859
888
|
}
|
|
860
|
-
const rootIterations = 12;
|
|
861
|
-
function approximateRoot(envelope, derivative, initialGuess) {
|
|
862
|
-
let result = initialGuess;
|
|
863
|
-
for (let i = 1; i < rootIterations; i++) {
|
|
864
|
-
result = result - envelope(result) / derivative(result);
|
|
865
|
-
}
|
|
866
|
-
return result;
|
|
867
|
-
}
|
|
868
|
-
function calcAngularFreq(undampedFreq, dampingRatio) {
|
|
869
|
-
return undampedFreq * Math.sqrt(1 - dampingRatio * dampingRatio);
|
|
870
|
-
}
|
|
871
|
-
|
|
872
889
|
const durationKeys = ["duration", "bounce"];
|
|
873
890
|
const physicsKeys = ["stiffness", "damping", "mass"];
|
|
874
891
|
function isSpringType(options, keys) {
|
|
@@ -956,19 +973,37 @@
|
|
|
956
973
|
? springDefaults.restDelta.granular
|
|
957
974
|
: springDefaults.restDelta.default);
|
|
958
975
|
let resolveSpring;
|
|
976
|
+
let resolveVelocity;
|
|
977
|
+
// Underdamped coefficients, hoisted for use in the inlined next() hot path
|
|
978
|
+
let angularFreq;
|
|
979
|
+
let A;
|
|
980
|
+
let sinCoeff;
|
|
981
|
+
let cosCoeff;
|
|
959
982
|
if (dampingRatio < 1) {
|
|
960
|
-
|
|
983
|
+
angularFreq = calcAngularFreq(undampedAngularFreq, dampingRatio);
|
|
984
|
+
A =
|
|
985
|
+
(initialVelocity +
|
|
986
|
+
dampingRatio * undampedAngularFreq * initialDelta) /
|
|
987
|
+
angularFreq;
|
|
961
988
|
// Underdamped spring
|
|
962
989
|
resolveSpring = (t) => {
|
|
963
990
|
const envelope = Math.exp(-dampingRatio * undampedAngularFreq * t);
|
|
964
991
|
return (target -
|
|
965
992
|
envelope *
|
|
966
|
-
((
|
|
967
|
-
dampingRatio * undampedAngularFreq * initialDelta) /
|
|
968
|
-
angularFreq) *
|
|
969
|
-
Math.sin(angularFreq * t) +
|
|
993
|
+
(A * Math.sin(angularFreq * t) +
|
|
970
994
|
initialDelta * Math.cos(angularFreq * t)));
|
|
971
995
|
};
|
|
996
|
+
// Analytical derivative of underdamped spring (px/ms)
|
|
997
|
+
sinCoeff =
|
|
998
|
+
dampingRatio * undampedAngularFreq * A + initialDelta * angularFreq;
|
|
999
|
+
cosCoeff =
|
|
1000
|
+
dampingRatio * undampedAngularFreq * initialDelta - A * angularFreq;
|
|
1001
|
+
resolveVelocity = (t) => {
|
|
1002
|
+
const envelope = Math.exp(-dampingRatio * undampedAngularFreq * t);
|
|
1003
|
+
return envelope *
|
|
1004
|
+
(sinCoeff * Math.sin(angularFreq * t) +
|
|
1005
|
+
cosCoeff * Math.cos(angularFreq * t));
|
|
1006
|
+
};
|
|
972
1007
|
}
|
|
973
1008
|
else if (dampingRatio === 1) {
|
|
974
1009
|
// Critically damped spring
|
|
@@ -976,6 +1011,10 @@
|
|
|
976
1011
|
Math.exp(-undampedAngularFreq * t) *
|
|
977
1012
|
(initialDelta +
|
|
978
1013
|
(initialVelocity + undampedAngularFreq * initialDelta) * t);
|
|
1014
|
+
// Analytical derivative of critically damped spring (px/ms)
|
|
1015
|
+
const C = initialVelocity + undampedAngularFreq * initialDelta;
|
|
1016
|
+
resolveVelocity = (t) => Math.exp(-undampedAngularFreq * t) *
|
|
1017
|
+
(undampedAngularFreq * C * t - initialVelocity);
|
|
979
1018
|
}
|
|
980
1019
|
else {
|
|
981
1020
|
// Overdamped spring
|
|
@@ -994,28 +1033,50 @@
|
|
|
994
1033
|
Math.cosh(freqForT))) /
|
|
995
1034
|
dampedAngularFreq);
|
|
996
1035
|
};
|
|
1036
|
+
// Analytical derivative of overdamped spring (px/ms)
|
|
1037
|
+
const P = (initialVelocity +
|
|
1038
|
+
dampingRatio * undampedAngularFreq * initialDelta) /
|
|
1039
|
+
dampedAngularFreq;
|
|
1040
|
+
const sinhCoeff = dampingRatio * undampedAngularFreq * P - initialDelta * dampedAngularFreq;
|
|
1041
|
+
const coshCoeff = dampingRatio * undampedAngularFreq * initialDelta - P * dampedAngularFreq;
|
|
1042
|
+
resolveVelocity = (t) => {
|
|
1043
|
+
const envelope = Math.exp(-dampingRatio * undampedAngularFreq * t);
|
|
1044
|
+
const freqForT = Math.min(dampedAngularFreq * t, 300);
|
|
1045
|
+
return envelope *
|
|
1046
|
+
(sinhCoeff * Math.sinh(freqForT) +
|
|
1047
|
+
coshCoeff * Math.cosh(freqForT));
|
|
1048
|
+
};
|
|
997
1049
|
}
|
|
998
1050
|
const generator = {
|
|
999
1051
|
calculatedDuration: isResolvedFromDuration ? duration || null : null,
|
|
1052
|
+
velocity: (t) => motionUtils.secondsToMilliseconds(resolveVelocity(t)),
|
|
1000
1053
|
next: (t) => {
|
|
1054
|
+
/**
|
|
1055
|
+
* For underdamped physics springs we need both position and
|
|
1056
|
+
* velocity each tick. Compute shared trig values once to avoid
|
|
1057
|
+
* duplicate Math.exp/sin/cos calls on the hot path.
|
|
1058
|
+
*/
|
|
1059
|
+
if (!isResolvedFromDuration && dampingRatio < 1) {
|
|
1060
|
+
const envelope = Math.exp(-dampingRatio * undampedAngularFreq * t);
|
|
1061
|
+
const sin = Math.sin(angularFreq * t);
|
|
1062
|
+
const cos = Math.cos(angularFreq * t);
|
|
1063
|
+
const current = target -
|
|
1064
|
+
envelope *
|
|
1065
|
+
(A * sin + initialDelta * cos);
|
|
1066
|
+
const currentVelocity = motionUtils.secondsToMilliseconds(envelope *
|
|
1067
|
+
(sinCoeff * sin + cosCoeff * cos));
|
|
1068
|
+
state.done =
|
|
1069
|
+
Math.abs(currentVelocity) <= restSpeed &&
|
|
1070
|
+
Math.abs(target - current) <= restDelta;
|
|
1071
|
+
state.value = state.done ? target : current;
|
|
1072
|
+
return state;
|
|
1073
|
+
}
|
|
1001
1074
|
const current = resolveSpring(t);
|
|
1002
1075
|
if (!isResolvedFromDuration) {
|
|
1003
|
-
|
|
1004
|
-
/**
|
|
1005
|
-
* We only need to calculate velocity for under-damped springs
|
|
1006
|
-
* as over- and critically-damped springs can't overshoot, so
|
|
1007
|
-
* checking only for displacement is enough.
|
|
1008
|
-
*/
|
|
1009
|
-
if (dampingRatio < 1) {
|
|
1010
|
-
currentVelocity =
|
|
1011
|
-
t === 0
|
|
1012
|
-
? motionUtils.secondsToMilliseconds(initialVelocity)
|
|
1013
|
-
: calcGeneratorVelocity(resolveSpring, t, current);
|
|
1014
|
-
}
|
|
1015
|
-
const isBelowVelocityThreshold = Math.abs(currentVelocity) <= restSpeed;
|
|
1016
|
-
const isBelowDisplacementThreshold = Math.abs(target - current) <= restDelta;
|
|
1076
|
+
const currentVelocity = motionUtils.secondsToMilliseconds(resolveVelocity(t));
|
|
1017
1077
|
state.done =
|
|
1018
|
-
|
|
1078
|
+
Math.abs(currentVelocity) <= restSpeed &&
|
|
1079
|
+
Math.abs(target - current) <= restDelta;
|
|
1019
1080
|
}
|
|
1020
1081
|
else {
|
|
1021
1082
|
state.done = t >= duration;
|
|
@@ -1040,6 +1101,12 @@
|
|
|
1040
1101
|
return options;
|
|
1041
1102
|
};
|
|
1042
1103
|
|
|
1104
|
+
const velocitySampleDuration = 5; // ms
|
|
1105
|
+
function getGeneratorVelocity(resolveValue, t, current) {
|
|
1106
|
+
const prevT = Math.max(t - velocitySampleDuration, 0);
|
|
1107
|
+
return motionUtils.velocityPerSecond(current - resolveValue(prevT), t - prevT);
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1043
1110
|
function inertia({ keyframes, velocity = 0.0, power = 0.8, timeConstant = 325, bounceDamping = 10, bounceStiffness = 500, modifyTarget, min, max, restDelta = 0.5, restSpeed, }) {
|
|
1044
1111
|
const origin = keyframes[0];
|
|
1045
1112
|
const state = {
|
|
@@ -1085,7 +1152,7 @@
|
|
|
1085
1152
|
timeReachedBoundary = t;
|
|
1086
1153
|
spring$1 = spring({
|
|
1087
1154
|
keyframes: [state.value, nearestBoundary(state.value)],
|
|
1088
|
-
velocity:
|
|
1155
|
+
velocity: getGeneratorVelocity(calcLatest, t, state.value), // TODO: This should be passing * 1000
|
|
1089
1156
|
damping: bounceDamping,
|
|
1090
1157
|
stiffness: bounceStiffness,
|
|
1091
1158
|
restDelta,
|
|
@@ -1548,6 +1615,22 @@
|
|
|
1548
1615
|
this.tick(newTime);
|
|
1549
1616
|
}
|
|
1550
1617
|
}
|
|
1618
|
+
/**
|
|
1619
|
+
* Returns the generator's velocity at the current time in units/second.
|
|
1620
|
+
* Uses the analytical derivative when available (springs), avoiding
|
|
1621
|
+
* the MotionValue's frame-dependent velocity estimation.
|
|
1622
|
+
*/
|
|
1623
|
+
getGeneratorVelocity() {
|
|
1624
|
+
const t = this.currentTime;
|
|
1625
|
+
if (t <= 0)
|
|
1626
|
+
return this.options.velocity || 0;
|
|
1627
|
+
if (this.generator.velocity) {
|
|
1628
|
+
return this.generator.velocity(t);
|
|
1629
|
+
}
|
|
1630
|
+
// Fallback: finite difference
|
|
1631
|
+
const current = this.generator.next(t).value;
|
|
1632
|
+
return getGeneratorVelocity((s) => this.generator.next(s).value, t, current);
|
|
1633
|
+
}
|
|
1551
1634
|
get speed() {
|
|
1552
1635
|
return this.playbackSpeed;
|
|
1553
1636
|
}
|
|
@@ -2263,7 +2346,12 @@
|
|
|
2263
2346
|
*/
|
|
2264
2347
|
replaceTransitionType(options);
|
|
2265
2348
|
super(options);
|
|
2266
|
-
|
|
2349
|
+
/**
|
|
2350
|
+
* Only set startTime when the animation should autoplay.
|
|
2351
|
+
* Setting startTime on a paused WAAPI animation unpauses it
|
|
2352
|
+
* (per the WAAPI spec), which breaks autoplay: false.
|
|
2353
|
+
*/
|
|
2354
|
+
if (options.startTime !== undefined && options.autoplay !== false) {
|
|
2267
2355
|
this.startTime = options.startTime;
|
|
2268
2356
|
}
|
|
2269
2357
|
this.options = options;
|
|
@@ -4045,19 +4133,14 @@
|
|
|
4045
4133
|
get(name) {
|
|
4046
4134
|
return this.values.get(name)?.value;
|
|
4047
4135
|
}
|
|
4048
|
-
destroy() {
|
|
4049
|
-
for (const value of this.values.values()) {
|
|
4050
|
-
value.onRemove();
|
|
4051
|
-
}
|
|
4052
|
-
}
|
|
4053
4136
|
}
|
|
4054
4137
|
|
|
4055
4138
|
function createEffect(addValue) {
|
|
4056
4139
|
const stateCache = new WeakMap();
|
|
4057
|
-
const subscriptions = [];
|
|
4058
4140
|
return (subject, values) => {
|
|
4059
4141
|
const state = stateCache.get(subject) ?? new MotionValueState();
|
|
4060
4142
|
stateCache.set(subject, state);
|
|
4143
|
+
const subscriptions = [];
|
|
4061
4144
|
for (const key in values) {
|
|
4062
4145
|
const value = values[key];
|
|
4063
4146
|
const remove = addValue(subject, state, key, value);
|
|
@@ -4149,8 +4232,7 @@
|
|
|
4149
4232
|
if (!valueIsDefault) {
|
|
4150
4233
|
transformIsDefault = false;
|
|
4151
4234
|
const transformName = translateAlias$1[key] || key;
|
|
4152
|
-
|
|
4153
|
-
transform += `${transformName}(${valueToRender}) `;
|
|
4235
|
+
transform += `${transformName}(${value}) `;
|
|
4154
4236
|
}
|
|
4155
4237
|
}
|
|
4156
4238
|
return transformIsDefault ? "none" : transform.trim();
|
|
@@ -4858,16 +4940,23 @@
|
|
|
4858
4940
|
}
|
|
4859
4941
|
};
|
|
4860
4942
|
const startAnimation = () => {
|
|
4861
|
-
stopAnimation();
|
|
4862
4943
|
const currentValue = asNumber$1(value.get());
|
|
4863
4944
|
const targetValue = asNumber$1(latestValue);
|
|
4864
4945
|
// Don't animate if we're already at the target
|
|
4865
4946
|
if (currentValue === targetValue) {
|
|
4947
|
+
stopAnimation();
|
|
4866
4948
|
return;
|
|
4867
4949
|
}
|
|
4950
|
+
// Use the running animation's analytical velocity for accuracy,
|
|
4951
|
+
// falling back to the MotionValue's velocity for the initial animation.
|
|
4952
|
+
// This prevents systematic velocity loss at high frame rates (240hz+).
|
|
4953
|
+
const velocity = activeAnimation
|
|
4954
|
+
? activeAnimation.getGeneratorVelocity()
|
|
4955
|
+
: value.getVelocity();
|
|
4956
|
+
stopAnimation();
|
|
4868
4957
|
activeAnimation = new JSAnimation({
|
|
4869
4958
|
keyframes: [currentValue, targetValue],
|
|
4870
|
-
velocity
|
|
4959
|
+
velocity,
|
|
4871
4960
|
// Default to spring if no type specified (matches useSpring behavior)
|
|
4872
4961
|
type: "spring",
|
|
4873
4962
|
restDelta: 0.001,
|
|
@@ -4876,16 +4965,19 @@
|
|
|
4876
4965
|
onUpdate: latestSetter,
|
|
4877
4966
|
});
|
|
4878
4967
|
};
|
|
4968
|
+
// Use a stable function reference so the frame loop Set deduplicates
|
|
4969
|
+
// multiple calls within the same frame (e.g. rapid mouse events)
|
|
4970
|
+
const scheduleAnimation = () => {
|
|
4971
|
+
startAnimation();
|
|
4972
|
+
value["events"].animationStart?.notify();
|
|
4973
|
+
activeAnimation?.then(() => {
|
|
4974
|
+
value["events"].animationComplete?.notify();
|
|
4975
|
+
});
|
|
4976
|
+
};
|
|
4879
4977
|
value.attach((v, set) => {
|
|
4880
4978
|
latestValue = v;
|
|
4881
4979
|
latestSetter = (latest) => set(parseValue(latest, unit));
|
|
4882
|
-
frame.postRender(
|
|
4883
|
-
startAnimation();
|
|
4884
|
-
value["events"].animationStart?.notify();
|
|
4885
|
-
activeAnimation?.then(() => {
|
|
4886
|
-
value["events"].animationComplete?.notify();
|
|
4887
|
-
});
|
|
4888
|
-
});
|
|
4980
|
+
frame.postRender(scheduleAnimation);
|
|
4889
4981
|
}, stopAnimation);
|
|
4890
4982
|
if (isMotionValue(source)) {
|
|
4891
4983
|
const removeSourceOnChange = source.on("change", (v) => value.set(parseValue(v, unit)));
|
|
@@ -6181,7 +6273,7 @@
|
|
|
6181
6273
|
applyBoxDelta(box, delta);
|
|
6182
6274
|
}
|
|
6183
6275
|
if (isSharedTransition && hasTransform(node.latestValues)) {
|
|
6184
|
-
transformBox(box, node.latestValues);
|
|
6276
|
+
transformBox(box, node.latestValues, node.layout?.layoutBox);
|
|
6185
6277
|
}
|
|
6186
6278
|
}
|
|
6187
6279
|
/**
|
|
@@ -6220,9 +6312,10 @@
|
|
|
6220
6312
|
/**
|
|
6221
6313
|
* Apply a transform to a box from the latest resolved motion values.
|
|
6222
6314
|
*/
|
|
6223
|
-
function transformBox(box, transform) {
|
|
6224
|
-
|
|
6225
|
-
transformAxis(box.
|
|
6315
|
+
function transformBox(box, transform, sourceBox) {
|
|
6316
|
+
const resolveBox = sourceBox ?? box;
|
|
6317
|
+
transformAxis(box.x, resolveAxisTranslate(transform.x, resolveBox.x), transform.scaleX, transform.scale, transform.originX);
|
|
6318
|
+
transformAxis(box.y, resolveAxisTranslate(transform.y, resolveBox.y), transform.scaleY, transform.scale, transform.originY);
|
|
6226
6319
|
}
|
|
6227
6320
|
|
|
6228
6321
|
function measureViewportBox(instance, transformPoint) {
|
|
@@ -7978,6 +8071,14 @@
|
|
|
7978
8071
|
for (let i = 0; i < this.path.length; i++) {
|
|
7979
8072
|
const node = this.path[i];
|
|
7980
8073
|
node.shouldResetTransform = true;
|
|
8074
|
+
/**
|
|
8075
|
+
* Percentage translates resolve against layoutBox dimensions,
|
|
8076
|
+
* so ancestors with them must be re-measured after transform reset.
|
|
8077
|
+
*/
|
|
8078
|
+
if (typeof node.latestValues.x === "string" ||
|
|
8079
|
+
typeof node.latestValues.y === "string") {
|
|
8080
|
+
node.isLayoutDirty = true;
|
|
8081
|
+
}
|
|
7981
8082
|
node.updateScroll("snapshot");
|
|
7982
8083
|
if (node.options.layoutRoot) {
|
|
7983
8084
|
node.willUpdate(false);
|
|
@@ -8243,10 +8344,10 @@
|
|
|
8243
8344
|
}
|
|
8244
8345
|
if (!hasTransform(node.latestValues))
|
|
8245
8346
|
continue;
|
|
8246
|
-
transformBox(withTransforms, node.latestValues);
|
|
8347
|
+
transformBox(withTransforms, node.latestValues, node.layout?.layoutBox);
|
|
8247
8348
|
}
|
|
8248
8349
|
if (hasTransform(this.latestValues)) {
|
|
8249
|
-
transformBox(withTransforms, this.latestValues);
|
|
8350
|
+
transformBox(withTransforms, this.latestValues, this.layout?.layoutBox);
|
|
8250
8351
|
}
|
|
8251
8352
|
return withTransforms;
|
|
8252
8353
|
}
|
|
@@ -8255,15 +8356,15 @@
|
|
|
8255
8356
|
copyBoxInto(boxWithoutTransform, box);
|
|
8256
8357
|
for (let i = 0; i < this.path.length; i++) {
|
|
8257
8358
|
const node = this.path[i];
|
|
8258
|
-
if (!node.instance)
|
|
8259
|
-
continue;
|
|
8260
8359
|
if (!hasTransform(node.latestValues))
|
|
8261
8360
|
continue;
|
|
8262
|
-
|
|
8263
|
-
|
|
8264
|
-
|
|
8265
|
-
|
|
8266
|
-
|
|
8361
|
+
let sourceBox;
|
|
8362
|
+
if (node.instance) {
|
|
8363
|
+
hasScale(node.latestValues) && node.updateSnapshot();
|
|
8364
|
+
sourceBox = createBox();
|
|
8365
|
+
copyBoxInto(sourceBox, node.measurePageBox());
|
|
8366
|
+
}
|
|
8367
|
+
removeBoxTransforms(boxWithoutTransform, node.latestValues, node.snapshot?.layoutBox, sourceBox);
|
|
8267
8368
|
}
|
|
8268
8369
|
if (hasTransform(this.latestValues)) {
|
|
8269
8370
|
removeBoxTransforms(boxWithoutTransform, this.latestValues);
|