Files

149 lines
4.1 KiB
C#
Raw Permalink Normal View History

2021-12-26 21:30:02 +08:00
// LEase.AnimateBase
using System;
using LEase;
public class AnimateBase
{
public static double Back(double x, LEaseMode easeMode)
{
switch (easeMode)
{
case LEaseMode.EaseIn:
return 2.70158 * x * x * x - 1.70158 * x * x;
case LEaseMode.EaseOut:
return 1.0 + 2.70158 * Math.Pow(x - 1.0, 3.0) + 1.70158 * Math.Pow(x - 1.0, 2.0);
case LEaseMode.EaseInOut:
return (x < 0.5) ? (Math.Pow(2.0 * x, 2.0) * (7.189819 * x - 2.5949095) / 2.0) : ((Math.Pow(2.0 * x - 2.0, 2.0) * (7.189819 * x - 4.5949095) + 2.0) / 2.0);
default:
return 0.0;
}
}
private static double BounceOut(double x)
{
if (x < 0.36363636363636365)
{
return 7.5625 * x * x;
}
if (x < 0.72727272727272729)
{
return 7.5625 * (x -= 0.54545454545454541) * x + 0.75;
}
if (x < 0.90909090909090906)
{
return 7.5625 * (x -= 0.81818181818181823) * x + 0.9375;
}
return 7.5625 * (x -= 21.0 / 22.0) * x + 63.0 / 64.0;
}
public static double Bounce(double x, LEaseMode easeMode)
{
switch (easeMode)
{
case LEaseMode.EaseIn:
return 1.0 - BounceOut(1.0 - x);
case LEaseMode.EaseOut:
return BounceOut(x);
case LEaseMode.EaseInOut:
return (x < 0.5) ? ((1.0 - BounceOut(1.0 - 2.0 * x)) / 2.0) : ((1.0 + BounceOut(2.0 * x - 1.0)) / 2.0);
default:
return 0.0;
}
}
public static double Circle(double x, LEaseMode easeMode)
{
switch (easeMode)
{
case LEaseMode.EaseIn:
return 1.0 - Math.Sqrt(1.0 - Math.Pow(x, 2.0));
case LEaseMode.EaseOut:
return Math.Sqrt(1.0 - Math.Pow(x - 1.0, 2.0));
case LEaseMode.EaseInOut:
return (x < 0.5) ? ((1.0 - Math.Sqrt(1.0 - Math.Pow(2.0 * x, 2.0))) / 2.0) : ((Math.Sqrt(1.0 - Math.Pow(-2.0 * x + 2.0, 2.0)) + 1.0) / 2.0);
default:
return 0.0;
}
}
public static double Power(double x, LEaseMode easeMode, int power)
{
switch (easeMode)
{
case LEaseMode.EaseIn:
return Math.Pow(x, power);
case LEaseMode.EaseOut:
return 1.0 - Math.Pow(1.0 - x, power);
case LEaseMode.EaseInOut:
return (x < 0.5) ? (Math.Pow(2.0, power - 1) * Math.Pow(x, power)) : (1.0 - Math.Pow(-2.0 * x + 2.0, power) / 2.0);
default:
return 0.0;
}
}
public static double Quadratic(double x, LEaseMode easeMode)
{
return Power(x, easeMode, 2);
}
public static double Cubic(double x, LEaseMode easeMode)
{
return Power(x, easeMode, 3);
}
public static double Quartic(double x, LEaseMode easeMode)
{
return Power(x, easeMode, 4);
}
public static double Quintic(double x, LEaseMode easeMode)
{
return Power(x, easeMode, 5);
}
public static double Elastic(double x, LEaseMode easeMode)
{
switch (easeMode)
{
case LEaseMode.EaseIn:
return (x == 0.0) ? 0.0 : ((x == 1.0) ? 1.0 : ((0.0 - Math.Pow(2.0, 10.0 * x - 10.0)) * Math.Sin((x * 10.0 - 10.75) * 2.0 * Math.PI / 3.0)));
case LEaseMode.EaseOut:
return (x == 0.0) ? 0.0 : ((x == 1.0) ? 1.0 : (Math.Pow(2.0, -10.0 * x) * Math.Sin((x * 10.0 - 0.75) * 2.0 * Math.PI / 3.0) + 1.0));
case LEaseMode.EaseInOut:
return (x == 0.0) ? 0.0 : ((x == 1.0) ? 1.0 : ((x < 0.5) ? ((0.0 - Math.Pow(2.0, 20.0 * x - 10.0) * Math.Sin((x * 20.0 - 11.125) * 2.0 * Math.PI / 4.5)) / 2.0) : (Math.Pow(2.0, -20.0 * x + 10.0) * Math.Sin((x * 20.0 - 11.125) * 2.0 * Math.PI / 4.5) / 2.0 + 1.0)));
default:
return 0.0;
}
}
public static double Exponential(double x, LEaseMode easeMode)
{
switch (easeMode)
{
case LEaseMode.EaseIn:
return (x == 0.0) ? 0.0 : Math.Pow(2.0, 10.0 * x - 10.0);
case LEaseMode.EaseOut:
return (x == 1.0) ? 1.0 : (1.0 - Math.Pow(2.0, -10.0 * x));
case LEaseMode.EaseInOut:
return (x == 0.0) ? 0.0 : ((x == 1.0) ? 1.0 : ((x < 0.5) ? (Math.Pow(2.0, 20.0 * x - 10.0) / 2.0) : ((2.0 - Math.Pow(2.0, -20.0 * x + 10.0)) / 2.0)));
default:
return 0.0;
}
}
public static double Sine(double x, LEaseMode easeMode)
{
switch (easeMode)
{
case LEaseMode.EaseIn:
return 1.0 - Math.Cos(x * Math.PI / 2.0);
case LEaseMode.EaseOut:
return Math.Sin(x * Math.PI / 2.0);
case LEaseMode.EaseInOut:
return (0.0 - (Math.Cos(Math.PI * x) - 1.0)) / 2.0;
default:
return 0.0;
}
}
}