UNPKG

@tamagui/react-native-web-lite

Version:
134 lines 4.23 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var TimingAnimation_exports = {}; __export(TimingAnimation_exports, { TimingAnimation: () => TimingAnimation, default: () => TimingAnimation_default }); module.exports = __toCommonJS(TimingAnimation_exports); var import_Easing = require("../Easing.cjs"); var import_Animation = require("./Animation.cjs"); var import_NativeAnimatedHelper = require("../NativeAnimatedHelper.cjs"); let _easeInOut; function easeInOut() { if (!_easeInOut) { _easeInOut = import_Easing.Easing.inOut(import_Easing.Easing.ease); } return _easeInOut; } class TimingAnimation extends import_Animation.Animation { _startTime; _fromValue; _toValue; _duration; _delay; _easing; _onUpdate; _animationFrame; _timeout; _useNativeDriver; _platformConfig; constructor(config) { super(); this._toValue = config.toValue; this._easing = config.easing ?? easeInOut(); this._duration = config.duration ?? 500; this._delay = config.delay ?? 0; this.__iterations = config.iterations ?? 1; this._useNativeDriver = (0, import_NativeAnimatedHelper.shouldUseNativeDriver)(config); this._platformConfig = config.platformConfig; this.__isInteraction = config.isInteraction ?? !this._useNativeDriver; } __getNativeAnimationConfig() { const frameDuration = 1e3 / 60; const frames = []; const numFrames = Math.round(this._duration / frameDuration); for (let frame = 0; frame < numFrames; frame++) { frames.push(this._easing(frame / numFrames)); } frames.push(this._easing(1)); return { type: "frames", frames, toValue: this._toValue, iterations: this.__iterations, platformConfig: this._platformConfig }; } start(fromValue, onUpdate, onEnd, previousAnimation, animatedValue) { this.__active = true; this._fromValue = fromValue; this._onUpdate = onUpdate; this.__onEnd = onEnd; const start = () => { if (this._duration === 0 && !this._useNativeDriver) { this._onUpdate(this._toValue); this.__debouncedOnEnd({ finished: true }); } else { this._startTime = Date.now(); if (this._useNativeDriver) { this.__startNativeAnimation(animatedValue); } else { this._animationFrame = requestAnimationFrame( // $FlowFixMe[method-unbinding] added when improving typing for this parameters this.onUpdate.bind(this)); } } }; if (this._delay) { this._timeout = setTimeout(start, this._delay); } else { start(); } } onUpdate() { const now = Date.now(); if (now >= this._startTime + this._duration) { if (this._duration === 0) { this._onUpdate(this._toValue); } else { this._onUpdate(this._fromValue + this._easing(1) * (this._toValue - this._fromValue)); } this.__debouncedOnEnd({ finished: true }); return; } this._onUpdate(this._fromValue + this._easing((now - this._startTime) / this._duration) * (this._toValue - this._fromValue)); if (this.__active) { this._animationFrame = requestAnimationFrame(this.onUpdate.bind(this)); } } stop() { super.stop(); this.__active = false; clearTimeout(this._timeout); global.cancelAnimationFrame(this._animationFrame); this.__debouncedOnEnd({ finished: false }); } } var TimingAnimation_default = TimingAnimation;