mirror of
https://gitee.com/csharpui/CPF.git
synced 2026-04-18 03:28:02 +08:00
初始化
This commit is contained in:
80
ConsoleApp1/EasingSharp/Easing.cs
Normal file
80
ConsoleApp1/EasingSharp/Easing.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ConsoleApp1.EasingSharp
|
||||
{
|
||||
public static class Easing
|
||||
{
|
||||
/// <summary>
|
||||
/// Asynchronously ease between two values and run a callback on each step.
|
||||
/// </summary>
|
||||
/// <param name="callback">The callback to run on each tick</param>
|
||||
/// <param name="easing">缓动类型.</param>
|
||||
/// <param name="start">起始值</param>
|
||||
/// <param name="end">结束值</param>
|
||||
/// <param name="time">执行时间毫秒.</param>
|
||||
public static void Ease(Action<double> callback, EasingTypes easing, double start, double end, int time)
|
||||
{
|
||||
var timeThread = new Thread(() =>
|
||||
{
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var nextFrame = (double)stopwatch.ElapsedMilliseconds;
|
||||
var wait = 1000 / 60;
|
||||
var startTick = (int)stopwatch.ElapsedMilliseconds;
|
||||
while (stopwatch.ElapsedMilliseconds - startTick <= time)
|
||||
if (stopwatch.ElapsedMilliseconds >= nextFrame)
|
||||
{
|
||||
var x = ((float)stopwatch.ElapsedMilliseconds - startTick) / time;
|
||||
if (x > 1) x = 1;
|
||||
double eas = 0;
|
||||
switch (easing)
|
||||
{
|
||||
case EasingTypes.Linear: eas = EasingActions.Linear(x); break;
|
||||
case EasingTypes.EaseInSine: eas = EasingActions.EaseInSine(x); break;
|
||||
case EasingTypes.EaseOutSine: eas = EasingActions.EaseOutSine(x); break;
|
||||
case EasingTypes.EaseInOutSine: eas = EasingActions.EaseInOutSine(x); break;
|
||||
case EasingTypes.EaseInQuad: eas = EasingActions.EaseInQuad(x); break;
|
||||
case EasingTypes.EaseOutQuad: eas = EasingActions.EaseOutQuad(x); break;
|
||||
case EasingTypes.EaseInOutQuad: eas = EasingActions.EaseInOutQuad(x); break;
|
||||
case EasingTypes.EaseInCubic: eas = EasingActions.EaseInCubic(x); break;
|
||||
case EasingTypes.EaseOutCubic: eas = EasingActions.EaseOutCubic(x); break;
|
||||
case EasingTypes.EaseInOutCubic: eas = EasingActions.EaseInOutCubic(x); break;
|
||||
case EasingTypes.EaseInQuart: eas = EasingActions.EaseInQuart(x); break;
|
||||
case EasingTypes.EaseOutQuart: eas = EasingActions.EaseOutQuart(x); break;
|
||||
case EasingTypes.EaseInOutQuart: eas = EasingActions.EaseInOutQuart(x); break;
|
||||
case EasingTypes.EaseInQuint: eas = EasingActions.EaseInQuint(x); break;
|
||||
case EasingTypes.EaseOutQuint: eas = EasingActions.EaseOutQuint(x); break;
|
||||
case EasingTypes.EaseInOutQuint: eas = EasingActions.EaseInOutQuint(x); break;
|
||||
case EasingTypes.EaseInExpo: eas = EasingActions.EaseInExpo(x); break;
|
||||
case EasingTypes.EaseOutExpo: eas = EasingActions.EaseOutExpo(x); break;
|
||||
case EasingTypes.EaseInOutExpo: eas = EasingActions.EaseInOutExpo(x); break;
|
||||
case EasingTypes.EaseInCirc: eas = EasingActions.EaseInCirc(x); break;
|
||||
case EasingTypes.EaseOutCirc: eas = EasingActions.EaseOutCirc(x); break;
|
||||
case EasingTypes.EaseInOutCirc: eas = EasingActions.EaseInOutCirc(x); break;
|
||||
case EasingTypes.EaseInBack: eas = EasingActions.EaseInBack(x); break;
|
||||
case EasingTypes.EaseOutBack: eas = EasingActions.EaseOutBack(x); break;
|
||||
case EasingTypes.EaseInOutBack: eas = EasingActions.EaseInOutBack(x); break;
|
||||
case EasingTypes.EaseInElastic: eas = EasingActions.EaseInElastic(x); break;
|
||||
case EasingTypes.EaseOutElastic: eas = EasingActions.EaseOutElastic(x); break;
|
||||
case EasingTypes.EaseInOutElastic: eas = EasingActions.EaseInOutElastic(x); break;
|
||||
case EasingTypes.EaseInBounce: eas = EasingActions.EaseInBounce(x); break;
|
||||
case EasingTypes.EaseOutBounce: eas = EasingActions.EaseOutBounce(x); break;
|
||||
case EasingTypes.EaseInOutBounce: eas = EasingActions.EaseInOutBounce(x); break;
|
||||
};
|
||||
|
||||
callback(start + eas * (end - start));
|
||||
nextFrame += wait;
|
||||
}
|
||||
|
||||
callback(end);
|
||||
})
|
||||
{ IsBackground = true, Name = "测试" };
|
||||
timeThread.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
129
ConsoleApp1/EasingSharp/EasingActions.cs
Normal file
129
ConsoleApp1/EasingSharp/EasingActions.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace ConsoleApp1.EasingSharp
|
||||
{
|
||||
public static class EasingActions
|
||||
{
|
||||
public const double DEFAULT_EASE = 1.70158;
|
||||
public const double ELASTIC_EASE = 2 * Math.PI / 3;
|
||||
public const double ELASTIC_INOUT_EASE = 2 * Math.PI / 4.5;
|
||||
|
||||
public static double EaseInSine(float x) => 1 - Math.Cos(x * Math.PI / 2);
|
||||
|
||||
public static double EaseOutSine(float x) => Math.Sin(x * Math.PI / 2);
|
||||
|
||||
public static double EaseInOutSine(float x) => -(Math.Cos(Math.PI * x) - 1) / 2;
|
||||
|
||||
public static double EaseInQuad(float x) => x * x;
|
||||
|
||||
public static double EaseOutQuad(float x) => 1 - (1 - x) * (1 - x);
|
||||
|
||||
public static double EaseInOutQuad(float x) => x < 0.5 ? 2 * x * x : 1 - Math.Pow(-2 * x + 2, 2) / 2;
|
||||
|
||||
public static double EaseInCubic(float x) => x * x * x;
|
||||
|
||||
public static double EaseOutCubic(float x) => 1 - Math.Pow(1 - x, 3);
|
||||
|
||||
public static double EaseInOutCubic(float x) =>
|
||||
x < 0.5 ? 4 * x * x * x : 1 - Math.Pow(-2 * x + 2, 3) / 2;
|
||||
|
||||
public static double EaseInQuart(float x) => x * x * x * x;
|
||||
|
||||
public static double EaseOutQuart(float x) => 1 - Math.Pow(1 - x, 4);
|
||||
|
||||
public static double EaseInOutQuart(float x) =>
|
||||
x < 0.5 ? 8 * x * x * x * x : 1 - Math.Pow(-2 * x + 2, 4) / 2;
|
||||
|
||||
public static double EaseInQuint(float x) => x * x * x * x * x;
|
||||
|
||||
public static double EaseOutQuint(float x) => 1 - Math.Pow(1 - x, 5);
|
||||
|
||||
public static double EaseInOutQuint(float x) =>
|
||||
x < 0.5 ? 16 * x * x * x * x * x : 1 - Math.Pow(-2 * x + 2, 5) / 2;
|
||||
|
||||
public static double EaseInExpo(float x) => x <= 0 ? 0 : Math.Pow(2, 10 * x - 10);
|
||||
|
||||
public static double EaseOutExpo(float x) => x >= 1 ? 1 : 1 - Math.Pow(2, -10 * x);
|
||||
|
||||
public static double EaseInOutExpo(float x) =>
|
||||
x <= 0
|
||||
? 0
|
||||
: x >= 1
|
||||
? 1
|
||||
: x < 0.5
|
||||
? Math.Pow(2, 20 * x - 10) / 2
|
||||
: (2 - Math.Pow(2, -20 * x + 10)) / 2;
|
||||
|
||||
public static double EaseInCirc(float x) => 1 - Math.Sqrt(1 - Math.Pow(x, 2));
|
||||
|
||||
public static double EaseOutCirc(float x) => Math.Sqrt(1 - Math.Pow(x - 1, 2));
|
||||
|
||||
public static double EaseInOutCirc(float x) =>
|
||||
x < 0.5
|
||||
? (1 - Math.Sqrt(1 - Math.Pow(2 * x, 2))) / 2
|
||||
: (Math.Sqrt(1 - Math.Pow(-2 * x + 2, 2)) + 1) / 2;
|
||||
|
||||
public static double EaseInBack(float x) => (DEFAULT_EASE + 1) * x * x * x - DEFAULT_EASE * x * x;
|
||||
|
||||
public static double EaseOutBack(float x) =>
|
||||
1 + (DEFAULT_EASE + 1) * Math.Pow(x - 1, 3) + DEFAULT_EASE * Math.Pow(x - 1, 2);
|
||||
|
||||
public static double EaseInOutBack(float x)
|
||||
{
|
||||
var c2 = DEFAULT_EASE * 1.525;
|
||||
|
||||
return x < 0.5
|
||||
? Math.Pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2) / 2
|
||||
: (Math.Pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2;
|
||||
}
|
||||
|
||||
public static double EaseInElastic(float x) =>
|
||||
x <= 0
|
||||
? 0
|
||||
: x >= 1
|
||||
? 1
|
||||
: -Math.Pow(2, 10 * x - 10) * Math.Sin((x * 10 - 10.75) * ELASTIC_EASE);
|
||||
|
||||
public static double EaseOutElastic(float x) =>
|
||||
x <= 0
|
||||
? 0
|
||||
: x >= 1
|
||||
? 1
|
||||
: Math.Pow(2, -10 * x) * Math.Sin((x * 10 - 0.75) * ELASTIC_EASE) + 1;
|
||||
|
||||
public static double EaseInOutElastic(float x) =>
|
||||
x <= 0
|
||||
? 0
|
||||
: x >= 1
|
||||
? 1
|
||||
: x < 0.5
|
||||
? -(Math.Pow(2, 20 * x - 10) * Math.Sin((20 * x - 11.125) * ELASTIC_INOUT_EASE)) / 2
|
||||
: Math.Pow(2, -20 * x + 10) * Math.Sin((20 * x - 11.125) * ELASTIC_INOUT_EASE) / 2 + 1;
|
||||
|
||||
public static double EaseInBounce(float x) => 1 - EaseOutBounce(1 - x);
|
||||
|
||||
public static double EaseOutBounce(float x)
|
||||
{
|
||||
var n1 = 7.5625;
|
||||
var d1 = 2.75;
|
||||
|
||||
if (x < 1 / d1)
|
||||
return n1 * x * x;
|
||||
else if (x < 2 / d1)
|
||||
return n1 * (x -= (float)1.5 / (float)d1) * x + 0.75;
|
||||
else if (x < 2.5 / d1)
|
||||
return n1 * (x -= (float)2.25 / (float)d1) * x + 0.9375;
|
||||
else
|
||||
return n1 * (x -= (float)2.625 / (float)d1) * x + 0.984375;
|
||||
}
|
||||
|
||||
public static double EaseInOutBounce(float x) =>
|
||||
x < 0.5
|
||||
? (1 - EaseOutBounce(1 - 2 * x)) / 2
|
||||
: (1 + EaseOutBounce(2 * x - 1)) / 2;
|
||||
|
||||
public static double Linear(float x) => x;
|
||||
}
|
||||
}
|
||||
41
ConsoleApp1/EasingSharp/EasingTypes.cs
Normal file
41
ConsoleApp1/EasingSharp/EasingTypes.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace ConsoleApp1.EasingSharp
|
||||
{
|
||||
public enum EasingTypes
|
||||
{
|
||||
Linear,
|
||||
EaseInSine,
|
||||
EaseOutSine,
|
||||
EaseInOutSine,
|
||||
EaseInQuad,
|
||||
EaseOutQuad,
|
||||
EaseInOutQuad,
|
||||
EaseInCubic,
|
||||
EaseOutCubic,
|
||||
EaseInOutCubic,
|
||||
EaseInQuart,
|
||||
EaseOutQuart,
|
||||
EaseInOutQuart,
|
||||
EaseInQuint,
|
||||
EaseOutQuint,
|
||||
EaseInOutQuint,
|
||||
EaseInExpo,
|
||||
EaseOutExpo,
|
||||
EaseInOutExpo,
|
||||
EaseInCirc,
|
||||
EaseOutCirc,
|
||||
EaseInOutCirc,
|
||||
EaseInBack,
|
||||
EaseOutBack,
|
||||
EaseInOutBack,
|
||||
EaseInElastic,
|
||||
EaseOutElastic,
|
||||
EaseInOutElastic,
|
||||
EaseInBounce,
|
||||
EaseOutBounce,
|
||||
EaseInOutBounce
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user