{"version":3,"file":"NativeAnimationExtended.mjs","sources":["../../../src/animation/NativeAnimationExtended.ts"],"sourcesContent":["import { clamp } from \"motion-utils\"\nimport { time } from \"../frameloop/sync-time\"\nimport { JSAnimation } from \"./JSAnimation\"\nimport { NativeAnimation, NativeAnimationOptions } from \"./NativeAnimation\"\nimport { AnyResolvedKeyframe, ValueAnimationOptions } from \"./types\"\nimport { replaceTransitionType } from \"./utils/replace-transition-type\"\nimport { replaceStringEasing } from \"./waapi/utils/unsupported-easing\"\n\nexport type NativeAnimationOptionsExtended<T extends AnyResolvedKeyframe> =\n    NativeAnimationOptions & ValueAnimationOptions<T> & NativeAnimationOptions\n\n/**\n * 10ms is chosen here as it strikes a balance between smooth\n * results (more than one keyframe per frame at 60fps) and\n * keyframe quantity.\n */\nconst sampleDelta = 10 //ms\n\nexport class NativeAnimationExtended<\n    T extends AnyResolvedKeyframe\n> extends NativeAnimation<T> {\n    options: NativeAnimationOptionsExtended<T>\n\n    constructor(options: NativeAnimationOptionsExtended<T>) {\n        /**\n         * The base NativeAnimation function only supports a subset\n         * of Motion easings, and WAAPI also only supports some\n         * easing functions via string/cubic-bezier definitions.\n         *\n         * This function replaces those unsupported easing functions\n         * with a JS easing function. This will later get compiled\n         * to a linear() easing function.\n         */\n        replaceStringEasing(options)\n\n        /**\n         * Ensure we replace the transition type with a generator function\n         * before passing to WAAPI.\n         *\n         * TODO: Does this have a better home? It could be shared with\n         * JSAnimation.\n         */\n        replaceTransitionType(options)\n\n        super(options)\n\n        if (options.startTime !== undefined) {\n            this.startTime = options.startTime\n        }\n\n        this.options = options\n    }\n\n    /**\n     * WAAPI doesn't natively have any interruption capabilities.\n     *\n     * Rather than read committed styles back out of the DOM, we can\n     * create a renderless JS animation and sample it twice to calculate\n     * its current value, \"previous\" value, and therefore allow\n     * Motion to calculate velocity for any subsequent animation.\n     */\n    updateMotionValue(value?: T) {\n        const { motionValue, onUpdate, onComplete, element, ...options } =\n            this.options\n\n        if (!motionValue) return\n\n        if (value !== undefined) {\n            motionValue.set(value)\n            return\n        }\n\n        const sampleAnimation = new JSAnimation({\n            ...options,\n            autoplay: false,\n        })\n\n        /**\n         * Use wall-clock elapsed time for sampling.\n         * Under CPU load, WAAPI's currentTime may not reflect actual\n         * elapsed time, causing incorrect sampling and visual jumps.\n         */\n        const sampleTime = Math.max(sampleDelta, time.now() - this.startTime)\n        const delta = clamp(0, sampleDelta, sampleTime - sampleDelta)\n\n        motionValue.setWithVelocity(\n            sampleAnimation.sample(Math.max(0, sampleTime - delta)).value,\n            sampleAnimation.sample(sampleTime).value,\n            delta\n        )\n\n        sampleAnimation.stop()\n    }\n}\n"],"names":[],"mappings":";;;;;;;AAWA;;;;AAIG;AACH,MAAM,WAAW,GAAG,EAAE,CAAA;AAEhB,MAAO,uBAEX,SAAQ,eAAkB,CAAA;AAGxB,IAAA,WAAA,CAAY,OAA0C,EAAA;AAClD;;;;;;;;AAQG;QACH,mBAAmB,CAAC,OAAO,CAAC,CAAA;AAE5B;;;;;;AAMG;QACH,qBAAqB,CAAC,OAAO,CAAC,CAAA;QAE9B,KAAK,CAAC,OAAO,CAAC,CAAA;AAEd,QAAA,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS,EAAE;AACjC,YAAA,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAA;SACrC;AAED,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;KACzB;AAED;;;;;;;AAOG;AACH,IAAA,iBAAiB,CAAC,KAAS,EAAA;AACvB,QAAA,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,OAAO,EAAE,GAC5D,IAAI,CAAC,OAAO,CAAA;AAEhB,QAAA,IAAI,CAAC,WAAW;YAAE,OAAM;AAExB,QAAA,IAAI,KAAK,KAAK,SAAS,EAAE;AACrB,YAAA,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;YACtB,OAAM;SACT;AAED,QAAA,MAAM,eAAe,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