{"version":3,"file":"delta-apply.mjs","sources":["../../../../src/projection/geometry/delta-apply.ts"],"sourcesContent":["import { Axis, Box, Delta, Point } from \"motion-utils\"\nimport { mixNumber } from \"../../utils/mix/number\"\nimport { ResolvedValues } from \"../../render/types\"\nimport { hasTransform } from \"../utils/has-transform\"\n\n/**\n * Scales a point based on a factor and an originPoint\n */\nexport function scalePoint(point: number, scale: number, originPoint: number) {\n    const distanceFromOrigin = point - originPoint\n    const scaled = scale * distanceFromOrigin\n    return originPoint + scaled\n}\n\n/**\n * Applies a translate/scale delta to a point\n */\nexport function applyPointDelta(\n    point: number,\n    translate: number,\n    scale: number,\n    originPoint: number,\n    boxScale?: number\n): number {\n    if (boxScale !== undefined) {\n        point = scalePoint(point, boxScale, originPoint)\n    }\n\n    return scalePoint(point, scale, originPoint) + translate\n}\n\n/**\n * Applies a translate/scale delta to an axis\n */\nexport function applyAxisDelta(\n    axis: Axis,\n    translate: number = 0,\n    scale: number = 1,\n    originPoint: number,\n    boxScale?: number\n): void {\n    axis.min = applyPointDelta(\n        axis.min,\n        translate,\n        scale,\n        originPoint,\n        boxScale\n    )\n\n    axis.max = applyPointDelta(\n        axis.max,\n        translate,\n        scale,\n        originPoint,\n        boxScale\n    )\n}\n\n/**\n * Applies a translate/scale delta to a box\n */\nexport function applyBoxDelta(box: Box, { x, y }: Delta): void {\n    applyAxisDelta(box.x, x.translate, x.scale, x.originPoint)\n    applyAxisDelta(box.y, y.translate, y.scale, y.originPoint)\n}\n\nconst TREE_SCALE_SNAP_MIN = 0.999999999999\nconst TREE_SCALE_SNAP_MAX = 1.0000000000001\n\n/**\n * Apply a tree of deltas to a box. We do this to calculate the effect of all the transforms\n * in a tree upon our box before then calculating how to project it into our desired viewport-relative box\n *\n * This is the final nested loop within updateLayoutDelta for future refactoring\n */\nexport function applyTreeDeltas(\n    box: Box,\n    treeScale: Point,\n    treePath: any[],\n    isSharedTransition: boolean = false\n) {\n    const treeLength = treePath.length\n    if (!treeLength) return\n\n    // Reset the treeScale\n    treeScale.x = treeScale.y = 1\n\n    let node: any\n    let delta: Delta | undefined\n\n    for (let i = 0; i < treeLength; i++) {\n        node = treePath[i]\n        delta = node.projectionDelta\n\n        /**\n         * TODO: Prefer to remove this, but currently we have motion components with\n         * display: contents in Framer.\n         */\n        const { visualElement } = node.options\n        if (\n            visualElement &&\n            visualElement.props.style &&\n            visualElement.props.style.display === \"contents\"\n        ) {\n            continue\n        }\n\n        if (\n            isSharedTransition &&\n            node.options.layoutScroll &&\n            node.scroll &&\n            node !== node.root\n        ) {\n            translateAxis(box.x, -node.scroll.offset.x)\n            translateAxis(box.y, -node.scroll.offset.y)\n        }\n\n        if (delta) {\n            // Incoporate each ancestor's scale into a cumulative treeScale for this component\n            treeScale.x *= delta.x.scale\n            treeScale.y *= delta.y.scale\n\n            // Apply each ancestor's calculated delta into this component's recorded layout box\n            applyBoxDelta(box, delta)\n        }\n\n        if (isSharedTransition && hasTransform(node.latestValues)) {\n            transformBox(box, node.latestValues, node.layout?.layoutBox)\n        }\n    }\n\n    /**\n     * Snap tree scale back to 1 if it's within a non-perceivable threshold.\n     * This will help reduce useless scales getting rendered.\n     */\n    if (\n        treeScale.x < TREE_SCALE_SNAP_MAX &&\n        treeScale.x > TREE_SCALE_SNAP_MIN\n    ) {\n        treeScale.x = 1.0\n    }\n    if (\n        treeScale.y < TREE_SCALE_SNAP_MAX &&\n        treeScale.y > TREE_SCALE_SNAP_MIN\n    ) {\n        treeScale.y = 1.0\n    }\n}\n\nexport function translateAxis(axis: Axis, distance: number) {\n    axis.min += distance\n    axis.max += distance\n}\n\n/**\n * Apply a transform to an axis from the latest resolved motion values.\n * This function basically acts as a bridge between a flat motion value map\n * and applyAxisDelta\n */\nexport function transformAxis(\n    axis: Axis,\n    axisTranslate?: number,\n    axisScale?: number,\n    boxScale?: number,\n    axisOrigin: number = 0.5\n): void {\n    const originPoint = mixNumber(axis.min, axis.max, axisOrigin)\n\n    // Apply the axis delta to the final axis\n    applyAxisDelta(axis, axisTranslate, axisScale, originPoint, boxScale)\n}\n\nfunction resolveAxisTranslate(\n    value: number | string | undefined,\n    axis: Axis\n): number | undefined {\n    if (typeof value === \"string\") {\n        return (parseFloat(value) / 100) * (axis.max - axis.min)\n    }\n    return value as number | undefined\n}\n\n/**\n * Apply a transform to a box from the latest resolved motion values.\n */\nexport function transformBox(\n    box: Box,\n    transform: ResolvedValues,\n    sourceBox?: Box\n) {\n    const resolveBox = sourceBox ?? box\n    transformAxis(\n        box.x,\n        resolveAxisTranslate(transform.x, resolveBox.x),\n        transform.scaleX as number,\n        transform.scale as number,\n        transform.originX as number\n    )\n    transformAxis(\n        box.y,\n        resolveAxisTranslate(transform.y, resolveBox.y),\n        transform.scaleY as number,\n        transform.scale as number,\n        transform.originY as number\n    )\n}\n"],"names":[],"mappings":";;;AAKA;;AAEG;SACa,UAAU,CAAC,KAAa,EAAE,KAAa,EAAE,WAAmB,EAAA;AACxE,IAAA,MAAM,kBAAkB,GAAG,KAAK,GAAG,WAAW;AAC9C,IAAA,MAAM,MAAM,GAAG,KAAK,GAAG,kBAAkB;IACzC,OAAO,WAAW,GAAG,MAAM;AAC/B;AAEA;;AAEG;AACG,SAAU,eAAe,CAC3B,KAAa,EACb,SAAiB,EACjB,KAAa,EACb,WAAmB,EACnB,QAAiB,EAAA;AAEjB,IAAA,IAAI,QAAQ,KAAK,SAAS,EAAE;QACxB,KAAK,GAAG,UAAU,CAAC,KAAK,EAAE,QAAQ,EAAE,WAAW,CAAC;IACpD;IAEA,OAAO,UAAU,CAAC,KAAK,EAAE,KAAK,EAAE,WAAW,CAAC,GAAG,SAAS;AAC5D;AAEA;;AAEG;AACG,SAAU,cAAc,CAC1B,IAAU,EACV,SAAA,GAAoB,CAAC,EACrB,KAAA,GAAgB,CAAC,EACjB,WAAmB,EACnB,QAAiB,EAAA;AAEjB,IAAA,IAAI,CAAC,GAAG,GAAG,eAAe,CACtB,IAAI,CAAC,GAAG,EACR,SAAS,EACT,KAAK,EACL,WAAW,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