Skip to content

Commit

Permalink
feat(easing): Implemented Circle easing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
carldebilly committed Jul 1, 2020
1 parent ab9211a commit 82ab08e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@
#pragma warning disable 114 // new keyword hiding
namespace Windows.UI.Xaml.Media.Animation
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __MACOS__
#if false || false || false || false || false
[global::Uno.NotImplemented]
#endif
public partial class CircleEase : global::Windows.UI.Xaml.Media.Animation.EasingFunctionBase
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __MACOS__
[global::Uno.NotImplemented]
public CircleEase()
{
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.UI.Xaml.Media.Animation.CircleEase", "CircleEase.CircleEase()");
}
#endif
// Skipping already declared method Windows.UI.Xaml.Media.Animation.CircleEase.CircleEase()
// Forced skipping of method Windows.UI.Xaml.Media.Animation.CircleEase.CircleEase()
}
}
41 changes: 41 additions & 0 deletions src/Uno.UI/UI/Xaml/Media/Animation/CircleEase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

using System;

namespace Windows.UI.Xaml.Media.Animation
{
public partial class CircleEase : EasingFunctionBase
{
public override double Ease(double currentTime, double startValue, double finalValue, double duration)
{
var c = finalValue - startValue;

var t = currentTime;
var b = startValue;
var d = duration;


//Depending on the mode we have different functions for the return value.
switch (this.EasingMode)
{
case EasingMode.EaseIn:
// http://gizma.com/easing/#circ1
t /= d;
return -c * (Math.Sqrt(1 - t * t) - 1) + b;
case EasingMode.EaseOut:
// http://gizma.com/easing/#circ2
t /= d;
t--;
return c * Math.Sqrt(1 - t * t) + b;
case EasingMode.EaseInOut:
// http://gizma.com/easing/#circ3
t /= d / 2;
if (t < 1) return -c / 2 * (Math.Sqrt(1 - t * t) - 1) + b;
t -= 2;
return c / 2 * (Math.Sqrt(1 - t * t) + 1) + b;

default:
return finalValue * currentTime / duration + startValue;
}
}
}
}
10 changes: 7 additions & 3 deletions src/Uno.UI/UI/Xaml/Media/Animation/EasingFunctionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ public EasingFunctionBase()

public EasingMode EasingMode
{
get { return (EasingMode)this.GetValue(EasingModeProperty); }
set { this.SetValue(EasingModeProperty, value); }
get => (EasingMode)this.GetValue(EasingModeProperty);
set => this.SetValue(EasingModeProperty, value);
}

public static readonly DependencyProperty EasingModeProperty =
DependencyProperty.Register("EasingMode", typeof(EasingMode), typeof(EasingFunctionBase), new PropertyMetadata(EasingMode.EaseOut));

public virtual double Ease(double currentTime, double startValue, double finalValue, double duration) { throw new NotSupportedException(); }
public virtual double Ease(double currentTime, double startValue, double finalValue, double duration) =>
// Return linear interpolation instead of an exception for unimplemented easing functions.
currentTime == 0 ? startValue : Lerp(startValue, finalValue, currentTime / duration);

private static double Lerp(double first, double last, double by) => first * (1.0 - @by) + last * @by;
}
}
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Media/Animation/SineEase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Windows.UI.Xaml.Media.Animation
{
public partial class SineEase : EasingFunctionBase
public partial class SineEase : EasingFunctionBase
{
public override double Ease(double currentTime, double startValue, double finalValue, double duration)
{
Expand Down

0 comments on commit 82ab08e

Please sign in to comment.