add ease functions

This commit is contained in:
Haibo 2018-11-21 01:04:34 +08:00 committed by Nomango
parent d88fc5afbe
commit bb03e6df5f
3 changed files with 234 additions and 33 deletions

View File

@ -56,51 +56,96 @@ namespace easy2d
case EaseFunc::Linear: case EaseFunc::Linear:
ease_func_ = math::Linear; ease_func_ = math::Linear;
break; break;
case EaseFunc::EaseIn: case EaseFunc::In:
ease_func_ = MakeEaseIn(2.f); ease_func_ = MakeEaseIn(2.f);
break; break;
case EaseFunc::EaseOut: case EaseFunc::Out:
ease_func_ = MakeEaseOut(2.f); ease_func_ = MakeEaseOut(2.f);
break; break;
case EaseFunc::EaseInOut: case EaseFunc::InOut:
ease_func_ = MakeEaseInOut(2.f); ease_func_ = MakeEaseInOut(2.f);
break; break;
case EaseFunc::EaseExponentialIn: case EaseFunc::ExpoIn:
ease_func_ = math::EaseExponentialIn; ease_func_ = math::EaseExponentialIn;
break; break;
case EaseFunc::EaseExponentialOut: case EaseFunc::ExpoOut:
ease_func_ = math::EaseExponentialOut; ease_func_ = math::EaseExponentialOut;
break; break;
case EaseFunc::EaseExponentialInOut: case EaseFunc::ExpoInOut:
ease_func_ = math::EaseExponentialInOut; ease_func_ = math::EaseExponentialInOut;
break; break;
case EaseFunc::EaseBounceIn: case EaseFunc::BounceIn:
ease_func_ = math::EaseBounceIn; ease_func_ = math::EaseBounceIn;
break; break;
case EaseFunc::EaseBounceOut: case EaseFunc::BounceOut:
ease_func_ = math::EaseBounceOut; ease_func_ = math::EaseBounceOut;
break; break;
case EaseFunc::EaseBounceInOut: case EaseFunc::BounceInOut:
ease_func_ = math::EaseBounceInOut; ease_func_ = math::EaseBounceInOut;
break; break;
case EaseFunc::EaseElasticIn: case EaseFunc::ElasticIn:
ease_func_ = MakeEaseElasticIn(0.3f); ease_func_ = MakeEaseElasticIn(0.3f);
break; break;
case EaseFunc::EaseElasticOut: case EaseFunc::ElasticOut:
ease_func_ = MakeEaseElasticOut(0.3f); ease_func_ = MakeEaseElasticOut(0.3f);
break; break;
case EaseFunc::EaseElasticInOut: case EaseFunc::ElasticInOut:
ease_func_ = MakeEaseElasticInOut(0.3f); ease_func_ = MakeEaseElasticInOut(0.3f);
break; break;
case EaseFunc::EaseSineIn: case EaseFunc::SineIn:
ease_func_ = math::EaseSineIn; ease_func_ = math::EaseSineIn;
break; break;
case EaseFunc::EaseSineOut: case EaseFunc::SineOut:
ease_func_ = math::EaseSineOut; ease_func_ = math::EaseSineOut;
break; break;
case EaseFunc::EaseSineInOut: case EaseFunc::SineInOut:
ease_func_ = math::EaseSineInOut; ease_func_ = math::EaseSineInOut;
break; break;
case EaseFunc::BackIn:
ease_func_ = math::EaseBackIn;
break;
case EaseFunc::BackOut:
ease_func_ = math::EaseBackOut;
break;
case EaseFunc::BackInOut:
ease_func_ = math::EaseBackInOut;
break;
case EaseFunc::QuadIn:
ease_func_ = math::EaseQuadIn;
break;
case EaseFunc::QuadOut:
ease_func_ = math::EaseQuadOut;
break;
case EaseFunc::QuadInOut:
ease_func_ = math::EaseQuadInOut;
break;
case EaseFunc::CubicIn:
ease_func_ = math::EaseCubicIn;
break;
case EaseFunc::CubicOut:
ease_func_ = math::EaseCubicOut;
break;
case EaseFunc::CubicInOut:
ease_func_ = math::EaseCubicInOut;
break;
case EaseFunc::QuartIn:
ease_func_ = math::EaseQuartIn;
break;
case EaseFunc::QuartOut:
ease_func_ = math::EaseQuartOut;
break;
case EaseFunc::QuartInOut:
ease_func_ = math::EaseQuartInOut;
break;
case EaseFunc::QuintIn:
ease_func_ = math::EaseQuintIn;
break;
case EaseFunc::QuintOut:
ease_func_ = math::EaseQuintOut;
break;
case EaseFunc::QuintInOut:
ease_func_ = math::EaseQuintInOut;
break;
default: default:
break; break;
} }

View File

@ -25,26 +25,44 @@
namespace easy2d namespace easy2d
{ {
// 缓动函数枚举
// More infomation about ease functions, see https://easings.net
enum class EaseFunc enum class EaseFunc
{ {
Linear, // 线性 Linear, // 线性
EaseIn, // 由慢变快 In, // 由慢变快
EaseOut, // 由快变慢 Out, // 由快变慢
EaseInOut, // 由慢变快, 再由快变慢 InOut, // 由慢变快, 再由快变慢
EaseExponentialIn, // 由慢变极快 ExpoIn, // 由慢变极快
EaseExponentialOut, // 由极快变慢 ExpoOut, // 由极快变慢
EaseExponentialInOut, // 由慢至极快, 再由极快边慢 ExpoInOut, // 由慢至极快, 再由极快边慢
EaseBounceIn, // 自起点赋予反弹力 ElasticIn, // 自起点赋予弹性
EaseBounceOut, // 自终点赋予反弹力 ElasticOut, // 自终点赋予弹性
EaseBounceInOut, // 在起点和终点赋予反弹力 ElasticInOut, // 再起点和终点赋予弹性
EaseElasticIn, // 自起点赋予弹性 BounceIn, // 自起点赋予反弹力
EaseElasticOut, // 自终点赋予弹性 BounceOut, // 自终点赋予反弹力
EaseElasticInOut, // 再起点和终点赋予弹性 BounceInOut, // 在起点和终点赋予反弹力
EaseSineIn, // 由快变慢, 采用正弦变换速度 BackIn,
EaseSineOut, // 由慢变快, 采用正弦变换速度 BackOut,
EaseSineInOut, // 由慢至快, 再由快至慢, 采用正弦变换速度 BackInOut,
QuadIn,
QuadOut,
QuadInOut,
CubicIn,
CubicOut,
CubicInOut,
QuartIn,
QuartOut,
QuartInOut,
QuintIn,
QuintOut,
QuintInOut,
SineIn,
SineOut,
SineInOut,
}; };
// 缓动函数
using EaseFunction = std::function<float(float)>; using EaseFunction = std::function<float(float)>;
inline EaseFunction MakeEaseIn(float rate) { return std::bind(math::EaseIn, std::placeholders::_1, rate); } inline EaseFunction MakeEaseIn(float rate) { return std::bind(math::EaseIn, std::placeholders::_1, rate); }
@ -54,6 +72,8 @@ namespace easy2d
inline EaseFunction MakeEaseElasticOut(float period) { return std::bind(math::EaseElasticOut, std::placeholders::_1, period); } inline EaseFunction MakeEaseElasticOut(float period) { return std::bind(math::EaseElasticOut, std::placeholders::_1, period); }
inline EaseFunction MakeEaseElasticInOut(float period) { return std::bind(math::EaseElasticInOut, std::placeholders::_1, period); } inline EaseFunction MakeEaseElasticInOut(float period) { return std::bind(math::EaseElasticInOut, std::placeholders::_1, period); }
// 补间动画
class Tween class Tween
: public Action : public Action
{ {
@ -65,12 +85,12 @@ namespace easy2d
EaseFunc func EaseFunc func
); );
// 设置速度变化曲线 // 设置缓动函数
void SetEaseFunction( void SetEaseFunction(
EaseFunc func EaseFunc func
); );
// 自定义速度变化曲线 // 自定义缓动函数
void SetEaseFunction( void SetEaseFunction(
EaseFunction func EaseFunction func
); );

View File

@ -30,6 +30,9 @@ namespace easy2d
return step; return step;
} }
// Ease
inline float EaseIn(float step, float rate) inline float EaseIn(float step, float rate)
{ {
return math::Pow(step, rate); return math::Pow(step, rate);
@ -47,6 +50,9 @@ namespace easy2d
return 1.f - .5f * math::Pow(2.f - 2 * step, rate); return 1.f - .5f * math::Pow(2.f - 2 * step, rate);
} }
// Exponential Ease
inline float EaseExponentialIn(float step) inline float EaseExponentialIn(float step)
{ {
return math::Pow(2.f, 10 * (step - 1)); return math::Pow(2.f, 10 * (step - 1));
@ -64,6 +70,9 @@ namespace easy2d
return 0.5f * (2 - math::Pow(2, -10 * (step * 2 - 1))); return 0.5f * (2 - math::Pow(2, -10 * (step * 2 - 1)));
} }
// Bounce Ease
inline float EaseBounceOut(float step) inline float EaseBounceOut(float step)
{ {
if (step < 1 / 2.75f) if (step < 1 / 2.75f)
@ -102,6 +111,9 @@ namespace easy2d
} }
} }
// Elastic Ease
inline float EaseElasticIn(float step, float period) inline float EaseElasticIn(float step, float period)
{ {
if (step == 0 || step == 1) if (step == 0 || step == 1)
@ -132,6 +144,39 @@ namespace easy2d
return math::Pow(2, -10 * step) * math::Sin((step - period / 4) * 360.f / period) * 0.5f + 1; return math::Pow(2, -10 * step) * math::Sin((step - period / 4) * 360.f / period) * 0.5f + 1;
} }
// Back Ease
inline float EaseBackIn(float step)
{
const float overshoot = 1.70158f;
return step * step * ((overshoot + 1) * step - overshoot);
}
inline float EaseBackOut(float step)
{
const float overshoot = 1.70158f;
step = step - 1;
return step * step * ((overshoot + 1) * step + overshoot) + 1;
}
inline float EaseBackInOut(float step)
{
const float overshoot = 1.70158f * 1.525f;
step = step * 2;
if (step < 1)
{
return (step * step * ((overshoot + 1) * step - overshoot)) / 2;
}
step = step - 2;
return (step * step * ((overshoot + 1) * step + overshoot)) / 2 + 1;
}
// Sine Ease
inline float EaseSineIn(float step) inline float EaseSineIn(float step)
{ {
return 1.f - math::Cos(step * 90); return 1.f - math::Cos(step * 90);
@ -146,5 +191,96 @@ namespace easy2d
{ {
return -0.5f * (math::Cos(step * 180) - 1); return -0.5f * (math::Cos(step * 180) - 1);
} }
// Quad Ease
inline float EaseQuadIn(float step)
{
return step * step;
}
inline float EaseQuadOut(float step)
{
return -1 * step * (step - 2);
}
inline float EaseQuadInOut(float step)
{
step = step * 2;
if (step < 1)
return 0.5f * step * step;
--step;
return -0.5f * (step * (step - 2) - 1);
}
// Cubic Ease
inline float EaseCubicIn(float step)
{
return step * step * step;
}
inline float EaseCubicOut(float step)
{
step -= 1;
return (step * step * step + 1);
}
inline float EaseCubicInOut(float step)
{
step = step * 2;
if (step < 1)
return 0.5f * step * step * step;
step -= 2;
return 0.5f * (step * step * step + 2);
}
// Quart Ease
inline float EaseQuartIn(float step)
{
return step * step * step * step;
}
inline float EaseQuartOut(float step)
{
step -= 1;
return -(step * step * step * step - 1);
}
inline float EaseQuartInOut(float step)
{
step = step * 2;
if (step < 1)
return 0.5f * step * step * step * step;
step -= 2;
return -0.5f * (step * step * step * step - 2);
}
// Quint Ease
inline float EaseQuintIn(float step)
{
return step * step * step * step * step;
}
inline float EaseQuintOut(float step)
{
step -= 1;
return (step * step * step * step * step + 1);
}
inline float EaseQuintInOut(float step)
{
step = step * 2;
if (step < 1)
return 0.5f * step * step * step * step * step;
step -= 2;
return 0.5f * (step * step * step * step * step + 2);
}
} }
} }