{"version":3,"file":"mix-values.mjs","sources":["../../../../src/projection/animation/mix-values.ts"],"sourcesContent":["import { type AnyResolvedKeyframe, mixNumber, percent, px } from \"motion-dom\"\nimport {\n    progress as calcProgress,\n    circOut,\n    EasingFunction,\n    noop,\n} from \"motion-utils\"\nimport { ResolvedValues } from \"../../render/types\"\n\nconst borders = [\"TopLeft\", \"TopRight\", \"BottomLeft\", \"BottomRight\"]\nconst numBorders = borders.length\n\nconst asNumber = (value: AnyResolvedKeyframe) =>\n    typeof value === \"string\" ? parseFloat(value) : value\n\nconst isPx = (value: AnyResolvedKeyframe) =>\n    typeof value === \"number\" || px.test(value)\n\nexport function mixValues(\n    target: ResolvedValues,\n    follow: ResolvedValues,\n    lead: ResolvedValues,\n    progress: number,\n    shouldCrossfadeOpacity: boolean,\n    isOnlyMember: boolean\n) {\n    if (shouldCrossfadeOpacity) {\n        target.opacity = mixNumber(\n            0,\n            (lead.opacity as number) ?? 1,\n            easeCrossfadeIn(progress)\n        )\n        target.opacityExit = mixNumber(\n            (follow.opacity as number) ?? 1,\n            0,\n            easeCrossfadeOut(progress)\n        )\n    } else if (isOnlyMember) {\n        target.opacity = mixNumber(\n            (follow.opacity as number) ?? 1,\n            (lead.opacity as number) ?? 1,\n            progress\n        )\n    }\n\n    /**\n     * Mix border radius\n     */\n    for (let i = 0; i < numBorders; i++) {\n        const borderLabel = `border${borders[i]}Radius`\n        let followRadius = getRadius(follow, borderLabel)\n        let leadRadius = getRadius(lead, borderLabel)\n\n        if (followRadius === undefined && leadRadius === undefined) continue\n\n        followRadius ||= 0\n        leadRadius ||= 0\n\n        const canMix =\n            followRadius === 0 ||\n            leadRadius === 0 ||\n            isPx(followRadius) === isPx(leadRadius)\n\n        if (canMix) {\n            target[borderLabel] = Math.max(\n                mixNumber(\n                    asNumber(followRadius),\n                    asNumber(leadRadius),\n                    progress\n                ),\n                0\n            )\n\n            if (percent.test(leadRadius) || percent.test(followRadius)) {\n                target[borderLabel] += \"%\"\n            }\n        } else {\n            target[borderLabel] = leadRadius\n        }\n    }\n\n    /**\n     * Mix rotation\n     */\n    if (follow.rotate || lead.rotate) {\n        target.rotate = mixNumber(\n            (follow.rotate as number) || 0,\n            (lead.rotate as number) || 0,\n            progress\n        )\n    }\n}\n\nfunction getRadius(values: ResolvedValues, radiusName: string) {\n    return values[radiusName] !== undefined\n        ? values[radiusName]\n        : values.borderRadius\n}\n\n// /**\n//  * We only want to mix the background color if there's a follow element\n//  * that we're not crossfading opacity between. For instance with switch\n//  * AnimateSharedLayout animations, this helps the illusion of a continuous\n//  * element being animated but also cuts down on the number of paints triggered\n//  * for elements where opacity is doing that work for us.\n//  */\n// if (\n//     !hasFollowElement &&\n//     latestLeadValues.backgroundColor &&\n//     latestFollowValues.backgroundColor\n// ) {\n//     /**\n//      * This isn't ideal performance-wise as mixColor is creating a new function every frame.\n//      * We could probably create a mixer that runs at the start of the animation but\n//      * the idea behind the crossfader is that it runs dynamically between two potentially\n//      * changing targets (ie opacity or borderRadius may be animating independently via variants)\n//      */\n//     leadState.backgroundColor = followState.backgroundColor = mixColor(\n//         latestFollowValues.backgroundColor as string,\n//         latestLeadValues.backgroundColor as string\n//     )(p)\n// }\n\nconst easeCrossfadeIn = /*@__PURE__*/ compress(0, 0.5, circOut)\nconst easeCrossfadeOut = /*@__PURE__*/ compress(0.5, 0.95, noop)\n\nfunction compress(\n    min: number,\n    max: number,\n    easing: EasingFunction\n): EasingFunction {\n    return (p: number) => {\n        // Could replace ifs with clamp\n        if (p < min) return 0\n        if (p > max) return 1\n        return easing(calcProgress(min, max, p))\n    }\n}\n"],"names":["calcProgress"],"mappings":";;;AASA,MAAM,OAAO,GAAG,CAAC,SAAS,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,CAAC,CAAA;AACpE,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,CAAA;AAEjC,MAAM,QAAQ,GAAG,CAAC,KAA0B,KACxC,OAAO,KAAK,KAAK,QAAQ,GAAG,UAAU,CAAC,KAAK,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