Skip to content

Commit 9edac68

Browse files
authored
feat(DecayAnimation): Adds decay animation driver (microsoft#636)
1 parent edd8428 commit 9edac68

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

ReactWindows/ReactNative/Animated/NativeAnimatedNodesManager.cs

+3
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ public void StartAnimatingNode(int animationId, int animatedNodeTag, JObject ani
155155
var animation = default(AnimationDriver);
156156
switch (type)
157157
{
158+
case "decay":
159+
animation = new DecayAnimationDriver(animationId, valueNode, endCallback, animationConfig);
160+
break;
158161
case "frames":
159162
animation = new FrameBasedAnimationDriver(animationId, valueNode, endCallback, animationConfig);
160163
break;

ReactWindows/ReactNative/ReactNative.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
<Compile Include="Animated\AdditionAnimatedNode.cs" />
119119
<Compile Include="Animated\AnimatedNode.cs" />
120120
<Compile Include="Animated\AnimationDriver.cs" />
121+
<Compile Include="Animated\DecayAnimationDriver.cs" />
121122
<Compile Include="Animated\FrameBasedAnimationDriver.cs" />
122123
<Compile Include="Animated\InterpolationAnimatedNode.cs" />
123124
<Compile Include="Animated\MultiplicationAnimatedNode.cs" />

0 commit comments

Comments
 (0)