UNPKG

@tamagui/react-native-web-lite

Version:
271 lines (269 loc) 15.7 kB
import Animation from "./Animation.native.js"; import SpringConfig from "../SpringConfig.native.js"; import { invariant } from "@tamagui/react-native-web-internals"; import { shouldUseNativeDriver } from "../NativeAnimatedHelper.native.js"; function _assert_this_initialized(self) { if (self === void 0) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return self; } function _call_super(_this, derived, args) { return derived = _get_prototype_of(derived), _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args)); } function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } function _construct(Parent, args, Class) { return _is_native_reflect_construct() ? _construct = Reflect.construct : _construct = function (Parent2, args2, Class2) { var a = [null]; a.push.apply(a, args2); var Constructor = Function.bind.apply(Parent2, a), instance = new Constructor(); return Class2 && _set_prototype_of(instance, Class2.prototype), instance; }, _construct.apply(null, arguments); } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } function _create_class(Constructor, protoProps, staticProps) { return protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Constructor; } function _define_property(obj, key, value) { return key in obj ? Object.defineProperty(obj, key, { value, enumerable: !0, configurable: !0, writable: !0 }) : obj[key] = value, obj; } function _get(target, property, receiver) { return typeof Reflect < "u" && Reflect.get ? _get = Reflect.get : _get = function (target2, property2, receiver2) { var base = _super_prop_base(target2, property2); if (base) { var desc = Object.getOwnPropertyDescriptor(base, property2); return desc.get ? desc.get.call(receiver2 || target2) : desc.value; } }, _get(target, property, receiver || target); } function _get_prototype_of(o) { return _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function (o2) { return o2.__proto__ || Object.getPrototypeOf(o2); }, _get_prototype_of(o); } function _inherits(subClass, superClass) { if (typeof superClass != "function" && superClass !== null) throw new TypeError("Super expression must either be null or a function"); subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: !0, configurable: !0 } }), superClass && _set_prototype_of(subClass, superClass); } function _is_native_function(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; } function _possible_constructor_return(self, call) { return call && (_type_of(call) === "object" || typeof call == "function") ? call : _assert_this_initialized(self); } function _set_prototype_of(o, p) { return _set_prototype_of = Object.setPrototypeOf || function (o2, p2) { return o2.__proto__ = p2, o2; }, _set_prototype_of(o, p); } function _super_prop_base(object, property) { for (; !Object.prototype.hasOwnProperty.call(object, property) && (object = _get_prototype_of(object), object !== null);); return object; } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol < "u" && obj.constructor === Symbol ? "symbol" : typeof obj; } function _wrap_native_super(Class) { var _cache = typeof Map == "function" ? /* @__PURE__ */new Map() : void 0; return _wrap_native_super = function (Class2) { if (Class2 === null || !_is_native_function(Class2)) return Class2; if (typeof Class2 != "function") throw new TypeError("Super expression must either be null or a function"); if (typeof _cache < "u") { if (_cache.has(Class2)) return _cache.get(Class2); _cache.set(Class2, Wrapper); } function Wrapper() { return _construct(Class2, arguments, _get_prototype_of(this).constructor); } return Wrapper.prototype = Object.create(Class2.prototype, { constructor: { value: Wrapper, enumerable: !1, writable: !0, configurable: !0 } }), _set_prototype_of(Wrapper, Class2); }, _wrap_native_super(Class); } function _is_native_reflect_construct() { try { var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch {} return (_is_native_reflect_construct = function () { return !!result; })(); } var SpringAnimation = /* @__PURE__ */function (Animation2) { _inherits(SpringAnimation2, Animation2); function SpringAnimation2(config) { _class_call_check(this, SpringAnimation2); var _this; _this = _call_super(this, SpringAnimation2), _define_property(_this, "_overshootClamping", void 0), _define_property(_this, "_restDisplacementThreshold", void 0), _define_property(_this, "_restSpeedThreshold", void 0), _define_property(_this, "_lastVelocity", void 0), _define_property(_this, "_startPosition", void 0), _define_property(_this, "_lastPosition", void 0), _define_property(_this, "_fromValue", void 0), _define_property(_this, "_toValue", void 0), _define_property(_this, "_stiffness", void 0), _define_property(_this, "_damping", void 0), _define_property(_this, "_mass", void 0), _define_property(_this, "_initialVelocity", void 0), _define_property(_this, "_delay", void 0), _define_property(_this, "_timeout", void 0), _define_property(_this, "_startTime", void 0), _define_property(_this, "_lastTime", void 0), _define_property(_this, "_frameTime", void 0), _define_property(_this, "_onUpdate", void 0), _define_property(_this, "_animationFrame", void 0), _define_property(_this, "_useNativeDriver", void 0), _define_property(_this, "_platformConfig", void 0); var _config_overshootClamping; _this._overshootClamping = (_config_overshootClamping = config.overshootClamping) !== null && _config_overshootClamping !== void 0 ? _config_overshootClamping : !1; var _config_restDisplacementThreshold; _this._restDisplacementThreshold = (_config_restDisplacementThreshold = config.restDisplacementThreshold) !== null && _config_restDisplacementThreshold !== void 0 ? _config_restDisplacementThreshold : 1e-3; var _config_restSpeedThreshold; _this._restSpeedThreshold = (_config_restSpeedThreshold = config.restSpeedThreshold) !== null && _config_restSpeedThreshold !== void 0 ? _config_restSpeedThreshold : 1e-3; var _config_velocity; _this._initialVelocity = (_config_velocity = config.velocity) !== null && _config_velocity !== void 0 ? _config_velocity : 0; var _config_velocity1; _this._lastVelocity = (_config_velocity1 = config.velocity) !== null && _config_velocity1 !== void 0 ? _config_velocity1 : 0, _this._toValue = config.toValue; var _config_delay; _this._delay = (_config_delay = config.delay) !== null && _config_delay !== void 0 ? _config_delay : 0, _this._useNativeDriver = shouldUseNativeDriver(config), _this._platformConfig = config.platformConfig; var _config_isInteraction; _this.__isInteraction = (_config_isInteraction = config.isInteraction) !== null && _config_isInteraction !== void 0 ? _config_isInteraction : !_this._useNativeDriver; var _config_iterations; if (_this.__iterations = (_config_iterations = config.iterations) !== null && _config_iterations !== void 0 ? _config_iterations : 1, config.stiffness !== void 0 || config.damping !== void 0 || config.mass !== void 0) { invariant(config.bounciness === void 0 && config.speed === void 0 && config.tension === void 0 && config.friction === void 0, "You can define one of bounciness/speed, tension/friction, or stiffness/damping/mass, but not more than one"); var _config_stiffness; _this._stiffness = (_config_stiffness = config.stiffness) !== null && _config_stiffness !== void 0 ? _config_stiffness : 100; var _config_damping; _this._damping = (_config_damping = config.damping) !== null && _config_damping !== void 0 ? _config_damping : 10; var _config_mass; _this._mass = (_config_mass = config.mass) !== null && _config_mass !== void 0 ? _config_mass : 1; } else if (config.bounciness !== void 0 || config.speed !== void 0) { invariant(config.tension === void 0 && config.friction === void 0 && config.stiffness === void 0 && config.damping === void 0 && config.mass === void 0, "You can define one of bounciness/speed, tension/friction, or stiffness/damping/mass, but not more than one"); var _config_bounciness, _config_speed, springConfig = SpringConfig.fromBouncinessAndSpeed((_config_bounciness = config.bounciness) !== null && _config_bounciness !== void 0 ? _config_bounciness : 8, (_config_speed = config.speed) !== null && _config_speed !== void 0 ? _config_speed : 12); _this._stiffness = springConfig.stiffness, _this._damping = springConfig.damping, _this._mass = 1; } else { var _config_tension, _config_friction, springConfig1 = SpringConfig.fromOrigamiTensionAndFriction((_config_tension = config.tension) !== null && _config_tension !== void 0 ? _config_tension : 40, (_config_friction = config.friction) !== null && _config_friction !== void 0 ? _config_friction : 7); _this._stiffness = springConfig1.stiffness, _this._damping = springConfig1.damping, _this._mass = 1; } return invariant(_this._stiffness > 0, "Stiffness value must be greater than 0"), invariant(_this._damping > 0, "Damping value must be greater than 0"), invariant(_this._mass > 0, "Mass value must be greater than 0"), _this; } return _create_class(SpringAnimation2, [{ key: "__getNativeAnimationConfig", value: function () { var _this__initialVelocity; return { type: "spring", overshootClamping: this._overshootClamping, restDisplacementThreshold: this._restDisplacementThreshold, restSpeedThreshold: this._restSpeedThreshold, stiffness: this._stiffness, damping: this._damping, mass: this._mass, initialVelocity: (_this__initialVelocity = this._initialVelocity) !== null && _this__initialVelocity !== void 0 ? _this__initialVelocity : this._lastVelocity, toValue: this._toValue, iterations: this.__iterations, platformConfig: this._platformConfig }; } }, { key: "start", value: function (fromValue, onUpdate, onEnd, previousAnimation, animatedValue) { var _this = this; if (this.__active = !0, this._startPosition = fromValue, this._lastPosition = this._startPosition, this._onUpdate = onUpdate, this.__onEnd = onEnd, this._lastTime = Date.now(), this._frameTime = 0, previousAnimation instanceof SpringAnimation2) { var internalState = previousAnimation.getInternalState(); this._lastPosition = internalState.lastPosition, this._lastVelocity = internalState.lastVelocity, this._initialVelocity = this._lastVelocity, this._lastTime = internalState.lastTime; } var start2 = function () { _this._useNativeDriver ? _this.__startNativeAnimation(animatedValue) : _this.onUpdate(); }; this._delay ? this._timeout = setTimeout(start2, this._delay) : start2(); } }, { key: "getInternalState", value: function () { return { lastPosition: this._lastPosition, lastVelocity: this._lastVelocity, lastTime: this._lastTime }; } }, { /** * This spring model is based off of a damped harmonic oscillator * (https://en.wikipedia.org/wiki/Harmonic_oscillator#Damped_harmonic_oscillator). * * We use the closed form of the second order differential equation: * * x'' + (2ζ⍵_0)x' + ⍵^2x = 0 * * where * ⍵_0 = √(k / m) (undamped angular frequency of the oscillator), * ζ = c / 2√mk (damping ratio), * c = damping constant * k = stiffness * m = mass * * The derivation of the closed form is described in detail here: * http://planetmath.org/sites/default/files/texpdf/39745.pdf * * This algorithm happens to match the algorithm used by CASpringAnimation, * a QuartzCore (iOS) API that creates spring animations. */ key: "onUpdate", value: function () { var MAX_STEPS = 64, now = Date.now(); now > this._lastTime + MAX_STEPS && (now = this._lastTime + MAX_STEPS); var deltaTime = (now - this._lastTime) / 1e3; this._frameTime += deltaTime; var c = this._damping, m = this._mass, k = this._stiffness, v0 = -this._initialVelocity, zeta = c / (2 * Math.sqrt(k * m)), omega0 = Math.sqrt(k / m), omega1 = omega0 * Math.sqrt(1 - zeta * zeta), x0 = this._toValue - this._startPosition, position = 0, velocity = 0, t = this._frameTime; if (zeta < 1) { var envelope = Math.exp(-zeta * omega0 * t); position = this._toValue - envelope * ((v0 + zeta * omega0 * x0) / omega1 * Math.sin(omega1 * t) + x0 * Math.cos(omega1 * t)), velocity = zeta * omega0 * envelope * (Math.sin(omega1 * t) * (v0 + zeta * omega0 * x0) / omega1 + x0 * Math.cos(omega1 * t)) - envelope * (Math.cos(omega1 * t) * (v0 + zeta * omega0 * x0) - omega1 * x0 * Math.sin(omega1 * t)); } else { var envelope1 = Math.exp(-omega0 * t); position = this._toValue - envelope1 * (x0 + (v0 + omega0 * x0) * t), velocity = envelope1 * (v0 * (t * omega0 - 1) + t * x0 * (omega0 * omega0)); } if (this._lastTime = now, this._lastPosition = position, this._lastVelocity = velocity, this._onUpdate(position), !!this.__active) { var isOvershooting = !1; this._overshootClamping && this._stiffness !== 0 && (this._startPosition < this._toValue ? isOvershooting = position > this._toValue : isOvershooting = position < this._toValue); var isVelocity = Math.abs(velocity) <= this._restSpeedThreshold, isDisplacement = !0; if (this._stiffness !== 0 && (isDisplacement = Math.abs(this._toValue - position) <= this._restDisplacementThreshold), isOvershooting || isVelocity && isDisplacement) { this._stiffness !== 0 && (this._lastPosition = this._toValue, this._lastVelocity = 0, this._onUpdate(this._toValue)), this.__debouncedOnEnd({ finished: !0 }); return; } this._animationFrame = requestAnimationFrame(this.onUpdate.bind(this)); } } }, { key: "stop", value: function () { _get(_get_prototype_of(SpringAnimation2.prototype), "stop", this).call(this), this.__active = !1, clearTimeout(this._timeout), global.cancelAnimationFrame(this._animationFrame), this.__debouncedOnEnd({ finished: !1 }); } }]), SpringAnimation2; }(_wrap_native_super(Animation)), SpringAnimation_default = SpringAnimation; export { SpringAnimation_default as default }; //# sourceMappingURL=SpringAnimation.native.js.map