motion-dom 12.35.0 → 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 +205 -81
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/animation/JSAnimation.mjs +31 -4
- package/dist/es/animation/JSAnimation.mjs.map +1 -1
- package/dist/es/animation/NativeAnimation.mjs +4 -0
- package/dist/es/animation/NativeAnimation.mjs.map +1 -1
- package/dist/es/animation/NativeAnimationExtended.mjs +17 -2
- 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 +205 -81
- 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,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AAG,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,KAC/C,KAAK,CAAC,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,CA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,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CACrE;AACL;AAEO,MAAM,OAAO,GAAG;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;
|