|
| 1 | +using Newtonsoft.Json.Linq; |
| 2 | +using ReactNative.Bridge; |
| 3 | +using System; |
| 4 | + |
| 5 | +namespace ReactNative.Animated |
| 6 | +{ |
| 7 | + class DecayAnimationDriver : AnimationDriver |
| 8 | + { |
| 9 | + private readonly double _velocity; |
| 10 | + private readonly double _deceleration; |
| 11 | + |
| 12 | + private long _startFrameTimeMillis = -1; |
| 13 | + private double _fromValue; |
| 14 | + private double _lastValue; |
| 15 | + |
| 16 | + public DecayAnimationDriver(int id, ValueAnimatedNode animatedValue, ICallback endCallback, JObject config) |
| 17 | + : base(id, animatedValue, endCallback) |
| 18 | + { |
| 19 | + _velocity = config.Value<double>("velocity"); |
| 20 | + _deceleration = config.Value<double>("deceleration"); |
| 21 | + } |
| 22 | + |
| 23 | + public override void RunAnimationStep(TimeSpan renderingTime) |
| 24 | + { |
| 25 | + long frameTimeMillis = renderingTime.Ticks / 10000; |
| 26 | + if (_startFrameTimeMillis == -1) |
| 27 | + { |
| 28 | + // since this is the first animation step, consider the start to be on the previous frame |
| 29 | + _startFrameTimeMillis = frameTimeMillis - 16; |
| 30 | + _fromValue = AnimatedValue.Value; |
| 31 | + _lastValue = AnimatedValue.Value; |
| 32 | + } |
| 33 | + |
| 34 | + var value = _fromValue + |
| 35 | + (_velocity / (1 - _deceleration)) * |
| 36 | + (1 - Math.Exp(-(1 - _deceleration) * (frameTimeMillis - _startFrameTimeMillis))); |
| 37 | + |
| 38 | + if (Math.Abs(_lastValue - value) < 0.1) |
| 39 | + { |
| 40 | + HasFinished = true; |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + _lastValue = value; |
| 45 | + AnimatedValue.Value = value; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments