Browse Source

Optimized Easing Functions courtesy of @KvanTTT

Jumar Macato 7 years ago
parent
commit
e05cacd35f

+ 2 - 3
src/Avalonia.Animation/Easing/BackEaseIn.cs

@@ -12,10 +12,9 @@ namespace Avalonia.Animation.Easings
     public class BackEaseIn : Easing
     {
         /// <inheritdoc/>
-        public override double Ease(double progress)
+        public override double Ease(double p)
         {
-            double p = progress;
-            return p * p * p - p * Math.Sin(p * Math.PI); 
+            return p * (p * p - Math.Sin(p * Math.PI)); 
         }
     }
 }

+ 2 - 2
src/Avalonia.Animation/Easing/BackEaseInOut.cs

@@ -19,12 +19,12 @@ namespace Avalonia.Animation.Easings
             if (p < 0.5d)
             {
                 double f = 2d * p;
-                return 0.5d * (f * f * f - f * Math.Sin(f * Math.PI));
+                return 0.5d * f * (f * f - Math.Sin(f * Math.PI));
             }
             else
             {
                 double f = (1d - (2d * p - 1d));
-                return 0.5d * (1d - (f * f * f - f * Math.Sin(f * Math.PI))) + 0.5d;
+                return 0.5d * (1d - f * (f * f - Math.Sin(f * Math.PI))) + 0.5d;
             }
         }
 

+ 1 - 1
src/Avalonia.Animation/Easing/BackEaseOut.cs

@@ -15,7 +15,7 @@ namespace Avalonia.Animation.Easings
         public override double Ease(double progress)
         {
             double p = 1d - progress;
-            return 1 - (p * p * p - p * Math.Sin(p * Math.PI));
+            return 1 - p * (p * p - Math.Sin(p * Math.PI));
         }
     }
 }

+ 2 - 3
src/Avalonia.Animation/Easing/CircularEaseIn.cs

@@ -13,10 +13,9 @@ namespace Avalonia.Animation.Easings
     public class CircularEaseIn : Easing
     {
         /// <inheritdoc/>
-        public override double Ease(double progress)
+        public override double Ease(double p)
         {
-            double p = progress;
-            return 1d - Math.Sqrt(1d - (p * p));
+            return 1d - Math.Sqrt(1d - p * p);
         }
 
     }

+ 3 - 2
src/Avalonia.Animation/Easing/CircularEaseInOut.cs

@@ -17,11 +17,12 @@ namespace Avalonia.Animation.Easings
             double p = progress;
             if (p < 0.5d)
             {
-                return 0.5d * (1d - Math.Sqrt(1d - 4d * (p * p)));
+                return 0.5d * (1d - Math.Sqrt(1d - 4d * p * p));
             }
             else
             {
-                return 0.5d * (Math.Sqrt(-((2d * p) - 3d) * ((2d * p) - 1d)) + 1d);
+                double t = 2d * p;
+                return 0.5d * (Math.Sqrt((3d - t) * (t - 1d)) + 1d);
             }
         }
 

+ 1 - 1
src/Avalonia.Animation/Easing/CubicEaseInOut.cs

@@ -20,7 +20,7 @@ namespace Avalonia.Animation.Easings
             }
             else
             {
-                double f = ((2d * p) - 2d);
+                double f = 2d * (p - 1);
                 return 0.5d * f * f * f + 1d;
             }
         }

+ 3 - 2
src/Avalonia.Animation/Easing/ElasticEaseInOut.cs

@@ -19,12 +19,13 @@ namespace Avalonia.Animation.Easings
 
             if (p < 0.5d)
             {
-                return 0.5d * Math.Sin(13d * EasingUtils.HALFPI * (2d * p)) * Math.Pow(2d, 10d * ((2d * p) - 1d));
+                double t = 2d * p;
+                return 0.5d * Math.Sin(13d * EasingUtils.HALFPI * t) * Math.Pow(2d, 10d * (t - 1d));
             }
             else
             {
                 return 0.5d * (Math.Sin(-13d * EasingUtils.HALFPI * ((2d * p - 1d) + 1d)) * Math.Pow(2d, -10d * (2d * p - 1d)) + 2d);
-            }            
+            }
         }
 
     }

+ 2 - 2
src/Avalonia.Animation/Easing/ExponentialEaseInOut.cs

@@ -18,11 +18,11 @@ namespace Avalonia.Animation.Easings
 
             if (p < 0.5d)
             {
-                return 0.5d * Math.Pow(2d, (20d * p) - 10d);
+                return 0.5d * Math.Pow(2d, 20d * p - 10d);
             }
             else
             {
-                return -0.5d * Math.Pow(2d, (-20d * p) + 10d) + 1d;
+                return -0.5d * Math.Pow(2d, -20d * p + 10d) + 1d;
             }
         }
 

+ 1 - 1
src/Avalonia.Animation/Easing/QuadraticEaseInOut.cs

@@ -20,7 +20,7 @@ namespace Avalonia.Animation.Easings
             }
             else
             {
-                return (-2d * p * p) + (4d * p) - 1d;
+                return p * (-2d * p + 4d) - 1d;
             }           
         }
 

+ 2 - 2
src/Avalonia.Animation/Easing/QuarticEaseIn.cs

@@ -12,8 +12,8 @@ namespace Avalonia.Animation.Easings
         /// <inheritdoc/>
         public override double Ease(double progress)
         {
-            return progress * progress * progress * progress;
-            
+            double p2 = progress * progress;
+            return p2 * p2;
         }
 
     }

+ 6 - 4
src/Avalonia.Animation/Easing/QuarticEaseInOut.cs

@@ -16,13 +16,15 @@ namespace Avalonia.Animation.Easings
 
             if (p < 0.5d)
             {
-                return 8d * p * p * p * p;
+                double p2 = p * p;
+                return 8d * p2 * p2;
             }
             else
             {
-                double f = (p - 1d);
-                return -8d * f * f * f * f + 1d;
-            }           
+                double f = p - 1d;
+                double f2 = f * f;
+                return -8d * f2 * f2 + 1d;
+            }
         }
 
     }

+ 3 - 2
src/Avalonia.Animation/Easing/QuarticEaseOut.cs

@@ -12,8 +12,9 @@ namespace Avalonia.Animation.Easings
         /// <inheritdoc/>
         public override double Ease(double progress)
         {
-            double f = (progress - 1d);
-            return f * f * f * (1d - progress) + 1d;
+            double f = progress - 1d;
+            double f2 = f * f;
+            return -f2 * f2 + 1d;
         }
 
     }

+ 2 - 1
src/Avalonia.Animation/Easing/QuinticEaseIn.cs

@@ -12,7 +12,8 @@ namespace Avalonia.Animation.Easings
         /// <inheritdoc/>
         public override double Ease(double progress)
         {
-            return progress * progress * progress * progress * progress;
+            double p2 = progress * progress;
+            return p2 * p2 * progress;
         }
     }
 }

+ 6 - 5
src/Avalonia.Animation/Easing/QuinticEaseInOut.cs

@@ -13,15 +13,16 @@ namespace Avalonia.Animation.Easings
         public override double Ease(double progress)
         {
             double p = progress;
-
-            if (progress < 0.5d)
+            if (p < 0.5d)
             {
-                return 16d * p * p * p * p * p;
+                double p2 = p * p;
+                return 16d * p2 * p2 * p;
             }
             else
             {
-                double f = ((2d * p) - 2d);
-                return 0.5d * f * f * f * f * f + 1d;
+                double f = 2d * p - 2d;
+                double f2 = f * f;
+                return 0.5d * f2 * f2 * f + 1d;
             }
         }
     }

+ 2 - 1
src/Avalonia.Animation/Easing/QuinticEaseOut.cs

@@ -13,7 +13,8 @@ namespace Avalonia.Animation.Easings
         public override double Ease(double progress)
         {
             double f = (progress - 1d);
-            return f * f * f * f * f + 1d;
+            double f2 = f * f;
+            return f2 * f2 * f + 1d;
         }
     }
 }