Skip to content

Commit

Permalink
fix: Fix bounce ease producing invalid final value
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Feb 7, 2021
1 parent 9794dd8 commit 6d840ab
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media.Animation;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Uno.UI.Tests.Windows_UI_Xaml_Media_Animation
{
[TestClass]
public class Given_BounceEase
{
[TestMethod]
public void When_FinalValueGreaterThanInitial()
{
var sut = new BounceEase();

EaseCore(0.0).Should().BeApproximately(100, .1);
EaseCore(1.0).Should().BeApproximately(200, .1);

double EaseCore(double normalizedTime)
=> sut.Ease(
currentTime: normalizedTime, duration: 1.0,
startValue: 100, finalValue: 200);
}

[TestMethod]
public void When_FinalValueLowerThanInitial()
{
var sut = new BounceEase();

EaseCore(0.0).Should().BeApproximately(200, .1);
EaseCore(1.0).Should().BeApproximately(100, .1);

double EaseCore(double normalizedTime)
=> sut.Ease(
currentTime: normalizedTime, duration: 1.0,
startValue: 200, finalValue: 100);
}
}
}
67 changes: 45 additions & 22 deletions src/Uno.UI/UI/Xaml/Media/Animation/BounceEase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,67 @@ namespace Windows.UI.Xaml.Media.Animation
{
public partial class BounceEase : EasingFunctionBase
{
// Source: https://easings.net/

public override double Ease(double currentTime, double startValue, double finalValue, double duration)
{
//Depending on the mode we have different functions for the return value.
switch (this.EasingMode)
var delta = finalValue - startValue;
var progress = currentTime / duration;

var ratio = EaseCore(progress);

return startValue + ratio * delta;
}

internal double EaseCore(double progress)
{
switch (EasingMode)
{
case EasingMode.EaseIn:
return BounceEaseIn(currentTime, startValue,finalValue, duration);
return BounceEaseIn(progress);

case EasingMode.EaseOut:
return BounceEaseOut(currentTime, startValue, finalValue, duration);
return BounceEaseOut(progress);

case EasingMode.EaseInOut:

if (currentTime < duration / 2)
return BounceEaseIn(currentTime * 2, 0, finalValue, duration) * .5 + startValue;
else
return BounceEaseOut(currentTime * 2 - duration, 0, finalValue, duration) * .5 + finalValue * .5 + startValue;

default:
return finalValue * currentTime / duration + startValue;
return BounceEaseInOut(progress);
}
}

public static double BounceEaseIn(double currentTime, double startValue, double finalValue, double duration)
public static double BounceEaseIn(double progress)
{
return finalValue - BounceEaseOut(duration - currentTime, 0, finalValue, duration) + startValue;
return 1 - BounceEaseOut(1 - progress);
}

public static double BounceEaseOut(double currentTime, double startValue, double finalValue, double duration)
public static double BounceEaseOut(double progress)
{
if ((currentTime /= duration) < (1 / 2.75))
return finalValue * (7.5625 * currentTime * currentTime) + startValue;
else if (currentTime < (2 / 2.75))
return finalValue * (7.5625 * (currentTime -= (1.5 / 2.75)) * currentTime + .75) + startValue;
else if (currentTime < (2.5 / 2.75))
return finalValue * (7.5625 * (currentTime -= (2.25 / 2.75)) * currentTime + .9375) + startValue;
const double n1 = 7.5625;
const double d1 = 2.75;

if (progress < 1 / d1)
{
return n1 * progress * progress;
}
else if (progress < 2 / d1)
{
return n1 * (progress -= 1.5 / d1) * progress + 0.75;
}
else if (progress < 2.5 / d1)
{
return n1 * (progress -= 2.25 / d1) * progress + 0.9375;
}
else
return finalValue * (7.5625 * (currentTime -= (2.625 / 2.75)) * currentTime + .984375) + startValue;
{
return n1 * (progress -= 2.625 / d1) * progress + 0.984375;
}
}
}

public static double BounceEaseInOut(double progress)
{
return progress < 0.5
? (1 - BounceEaseOut(1 - 2 * progress)) / 2
: (1 + BounceEaseOut(2 * progress - 1)) / 2;
}
}
}

0 comments on commit 6d840ab

Please sign in to comment.