public static class Ease
{
public static float LinearEase(float start, float changeValue, float currentTime, float fullTime)
{
return start + changeValue * currentTime / fullTime;
}
public static float QuadraticEaseIn(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime;
return start + changeValue * currentTime * currentTime;
}
public static float QuadraticEaseOut(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime;
return start - changeValue * currentTime * (currentTime - 2);
}
public static float QuadraticEaseInOut(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime / 2;
if (currentTime < 1) return start + changeValue / 2 * currentTime * currentTime;
currentTime--;
return start - changeValue / 2 * (currentTime * (currentTime - 2) - 1);
}
public static float CubicEaseIn(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime;
return start + changeValue * currentTime * currentTime * currentTime;
}
public static float CubicEaseOut(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime;
currentTime--;
return start + changeValue * (currentTime * currentTime * currentTime + 1);
}
public static float CubicEaseInOut(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime / 2;
if (currentTime < 1) return start + changeValue / 2 * currentTime * currentTime * currentTime;
currentTime -= 2;
return start + changeValue / 2 * (currentTime * currentTime * currentTime + 2);
}
public static float QuarticEaseIn(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime;
return start + changeValue * currentTime * currentTime * currentTime * currentTime;
}
public static float QuarticEaseOut(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime;
currentTime--;
return start - changeValue * (currentTime * currentTime * currentTime * currentTime - 1);
}
public static float QuarticEaseInOut(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime / 2;
if (currentTime < 1) return start + changeValue / 2 * currentTime * currentTime * currentTime * currentTime;
currentTime -= 2;
return start - changeValue / 2 * (currentTime * currentTime * currentTime * currentTime - 2);
}
public static float QuinticEaseIn(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime;
return start + changeValue * currentTime * currentTime * currentTime * currentTime * currentTime;
}
public static float QuinticEaseOut(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime;
currentTime--;
return start + changeValue * (currentTime * currentTime * currentTime * currentTime * currentTime + 1);
}
public static float QuinticEaseInOut(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime / 2;
if (currentTime < 1) return start + changeValue / 2 * currentTime * currentTime * currentTime * currentTime * currentTime;
currentTime -= 2;
return start + changeValue / 2 * (currentTime * currentTime * currentTime * currentTime * currentTime + 2);
}
public static float ExponentialEaseIn(float start, float changeValue, float currentTime, float fullTime)
{
return start + changeValue * (float)System.Math.Pow(2, 10 * (currentTime / fullTime - 1));
}
public static float ExponentialEaseOut(float start, float changeValue, float currentTime, float fullTime)
{
return start + changeValue * (float)(-System.Math.Pow(2, -10 * currentTime / fullTime) + 1);
}
public static float ExponentialEaseInOut(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime / 2;
if (currentTime < 1) return start + changeValue / 2 * (float)System.Math.Pow(2, 10 * (currentTime - 1));
currentTime--;
return start + changeValue / 2 * (float)(-System.Math.Pow(2, -10 * currentTime) + 2);
}
public static float CircularEaseIn(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime;
return start - changeValue * (float)(System.Math.Sqrt(1 - currentTime * currentTime) - 1);
}
public static float CircularEaseOut(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime;
currentTime--;
return start + changeValue * (float)System.Math.Sqrt(1 - currentTime * currentTime);
}
public static float CircularEaseInOut(float start, float changeValue, float currentTime, float fullTime)
{
currentTime /= fullTime / 2;
if (currentTime < 1) return start - changeValue / 2 * (float)(System.Math.Sqrt(1 - currentTime * currentTime) - 1);
currentTime -= 2;
return start + changeValue / 2 * (float)(System.Math.Sqrt(1 - currentTime * currentTime) + 1);
}
public static float SinusoidalEaseIn(float start, float changeValue, float currentTime, float fullTime)
{
return start + changeValue - changeValue * (float)System.Math.Cos(currentTime / fullTime * (System.Math.PI / 2));
}
public static float SinusoidalEaseOut(float start, float changeValue, float currentTime, float fullTime)
{
return start + changeValue * (float)System.Math.Sin(currentTime / fullTime * (System.Math.PI / 2));
}
public static float SinusoidalEaseInOut(float start, float changeValue, float currentTime, float fullTime)
{
return start - changeValue / 2 * (float)(System.Math.Cos(System.Math.PI * currentTime / fullTime) - 1);
}
public static float DoEase(EaseType type, float start, float changeValue, float currentTime, float fullTime)
{
switch (type)
{
case EaseType.Linear:
return LinearEase(start, changeValue, currentTime, fullTime);
case EaseType.QuadraticIn:
return QuadraticEaseIn(start, changeValue, currentTime, fullTime);
case EaseType.QuadraticOut:
return QuadraticEaseOut(start, changeValue, currentTime, fullTime);
case EaseType.QuadraticInOut:
return QuarticEaseInOut(start, changeValue, currentTime, fullTime);
case EaseType.CubicIn:
return CubicEaseIn(start, changeValue, currentTime, fullTime);
case EaseType.CubicOut:
return CubicEaseOut(start, changeValue, currentTime, fullTime);
case EaseType.CubicInOut:
return CubicEaseInOut(start, changeValue, currentTime, fullTime);
case EaseType.QuarticIn:
return QuarticEaseIn(start, changeValue, currentTime, fullTime);
case EaseType.QuarticOut:
return QuarticEaseOut(start, changeValue, currentTime, fullTime);
case EaseType.QuarticInOut:
return QuarticEaseInOut(start, changeValue, currentTime, fullTime);
case EaseType.QuinticIn:
return QuinticEaseIn(start, changeValue, currentTime, fullTime);
case EaseType.QuinticOut:
return QuinticEaseOut(start, changeValue, currentTime, fullTime);
case EaseType.QuinticInOut:
return QuinticEaseInOut(start, changeValue, currentTime, fullTime);
case EaseType.ExponentialIn:
return ExponentialEaseIn(start, changeValue, currentTime, fullTime);
case EaseType.ExponentialOut:
return ExponentialEaseOut(start, changeValue, currentTime, fullTime);
case EaseType.ExponentialInOut:
return ExponentialEaseInOut(start, changeValue, currentTime, fullTime);
case EaseType.CircularIn:
return CircularEaseIn(start, changeValue, currentTime, fullTime);
case EaseType.CircularOut:
return CircularEaseOut(start, changeValue, currentTime, fullTime);
case EaseType.CircularInOut:
return CircularEaseInOut(start, changeValue, currentTime, fullTime);
case EaseType.SinusoidalIn:
return SinusoidalEaseIn(start, changeValue, currentTime, fullTime);
case EaseType.SinusoidalOut:
return SinusoidalEaseOut(start, changeValue, currentTime, fullTime);
case EaseType.SinusoidalInOut:
return SinusoidalEaseInOut(start, changeValue, currentTime, fullTime);
default:
return LinearEase(start, changeValue, currentTime, fullTime);
}
}
}
public enum EaseType
{
Linear,
QuadraticIn,
QuadraticOut,
QuadraticInOut,
CubicIn,
CubicOut,
CubicInOut,
QuarticIn,
QuarticOut,
QuarticInOut,
QuinticIn,
QuinticOut,
QuinticInOut,
ExponentialIn,
ExponentialOut,
ExponentialInOut,
CircularIn,
CircularOut,
CircularInOut,
SinusoidalIn,
SinusoidalOut,
SinusoidalInOut
}
C# 常用缓动计算
最新推荐文章于 2024-10-26 14:33:34 发布