diff --git a/Easing.h b/Easing.h new file mode 100644 index 0000000..7e6b84e --- /dev/null +++ b/Easing.h @@ -0,0 +1,30 @@ +/* + * Easing Functions: Copyright (c) 2010 Andy Brown + * http://www.andybrown.me.uk + * + * This work is licensed under a Creative Commons + * Attribution_ShareAlike 3.0 Unported License. + * http://creativecommons.org/licenses/by_sa/3.0/ + */ + +#pragma once +#ifndef HT_TWEEN_EASING_LIBRARY_H +#define HT_TWEEN_EASING_LIBRARY_H + +#include "Easing/BackEase.h" +#include "Easing/BounceEase.h" +#include "Easing/CircularEase.h" +#include "Easing/CubicEase.h" +#include "Easing/ElasticEase.h" +#include "Easing/ExponentialEase.h" +#include "Easing/LinearEase.h" +#include "Easing/QuadraticEase.h" +#include "Easing/QuarticEase.h" +#include "Easing/QuinticEase.h" +#include "Easing/SineEase.h" + +namespace Ease = ht::ease; +template +using EasingFunc = Ease::Easing; + +#endif // HT_TWEEN_EASING_LIBRARY_H diff --git a/Easing/BackEase.h b/Easing/BackEase.h new file mode 100644 index 0000000..a6e90f2 --- /dev/null +++ b/Easing/BackEase.h @@ -0,0 +1,75 @@ +/* + * Easing Functions: Copyright (c) 2010 Andy Brown + * http://www.andybrown.me.uk + * + * This work is licensed under a Creative Commons + * Attribution_ShareAlike 3.0 Unported License. + * http://creativecommons.org/licenses/by_sa/3.0/ + */ + +#pragma once +#ifndef HT_TWEEN_EASING_BACKEASE_H +#define HT_TWEEN_EASING_BACKEASE_H + +#include "EasingBase.h" +#include "Types.h" + +namespace ht { +namespace ease { + +class BackEase : public EasingBase +{ + double v_overshoot {1.70158}; + +public: + + virtual ~BackEase() {} + + virtual double easeIn(double t) const + { + t /= t_scale; + return v_scale * t * t * ((v_overshoot + 1.) * t - v_overshoot); + } + + virtual double easeOut(double t) const + { + t = t / t_scale - 1.; + return v_scale * (t * t * ((v_overshoot + 1.) * t + v_overshoot) + 1.); + } + + virtual double easeInOut(double t) const + { + double overshoot = v_overshoot * 1.525; + t /= t_scale / 2.; + if(t < 1.) + return v_scale / 2. * (t * t * ((overshoot + 1.) * t - overshoot)); + t -= 2.; + return v_scale / 2. * (t * t * ((overshoot + 1.) * t + overshoot) + 2.); + } + + void overshoot(const double ovs) { v_overshoot = ovs; } + double overshoot() const { return v_overshoot; } +}; + +template <> +struct Easing : public BackEase +{ + double get(const double t) const { return BackEase::easeIn(t); } +}; + +template <> +struct Easing : public BackEase +{ + double get(const double t) const { return BackEase::easeOut(t); } +}; + +template <> +struct Easing : public BackEase +{ + double get(const double t) const { return BackEase::easeInOut(t); } +}; + +} // ease +} // ht + +#endif // HT_TWEEN_EASING_BACKEASE_H diff --git a/Easing/BounceEase.h b/Easing/BounceEase.h new file mode 100644 index 0000000..f93b02c --- /dev/null +++ b/Easing/BounceEase.h @@ -0,0 +1,79 @@ +/* + * Easing Functions: Copyright (c) 2010 Andy Brown + * http://www.andybrown.me.uk + * + * This work is licensed under a Creative Commons + * Attribution_ShareAlike 3.0 Unported License. + * http://creativecommons.org/licenses/by_sa/3.0/ + */ + +#pragma once +#ifndef HT_TWEEN_EASING_BOUNCEEASE_H +#define HT_TWEEN_EASING_BOUNCEEASE_H + +#include "EasingBase.h" + +namespace ht { +namespace ease { + +class BounceEase : public EasingBase +{ +public: + + virtual ~BounceEase() {} + + virtual double easeIn(double t) const + { + return v_scale - easeOut(t_scale - t); + } + + virtual double easeOut(double t) const + { + t /= t_scale; + if (t < (1. / 2.75)) + return v_scale * (7.5625 * t * t); + else if (t < (2. / 2.75)) + { + t -= 1.5 / 2.75; + return v_scale * (7.5625 * t * t + 0.75); + } + else if (t < (2.5 / 2.75)) + { + t -= 2.25 / 2.75; + return v_scale * (7.5625 * t * t + 0.9375); + } + t -= 2.625 / 2.75; + return v_scale * (7.5625 * t * t + 0.984375); + } + + virtual double easeInOut(double t) const + { + if(t < t_scale / 2.) + return easeIn(t * 2.) * 0.5; + else + return easeOut(t * 2. - t_scale) * 0.5 + v_scale * 0.5; + } +}; + +template <> +struct Easing : public BounceEase +{ + double get(const double t) const { return BounceEase::easeIn(t); } +}; + +template <> +struct Easing : public BounceEase +{ + double get(const double t) const { return BounceEase::easeOut(t); } +}; + +template <> +struct Easing : public BounceEase +{ + double get(const double t) const { return BounceEase::easeInOut(t); } +}; + +} // ease +} // ht + +#endif diff --git a/Easing/CircularEase.h b/Easing/CircularEase.h new file mode 100644 index 0000000..fb4447f --- /dev/null +++ b/Easing/CircularEase.h @@ -0,0 +1,67 @@ +/* + * Easing Functions: Copyright (c) 2010 Andy Brown + * http://www.andybrown.me.uk + * + * This work is licensed under a Creative Commons + * Attribution_ShareAlike 3.0 Unported License. + * http://creativecommons.org/licenses/by_sa/3.0/ + */ + +#pragma once +#ifndef HT_TWEEN_EASING_CIRCULAREASE_H +#define HT_TWEEN_EASING_CIRCULAREASE_H + +#include "EasingBase.h" + +namespace ht { +namespace ease { + +class CircularEase : public EasingBase +{ +public: + + virtual ~CircularEase() {} + + virtual double easeIn(double t) const + { + t /= t_scale; + return -v_scale * (sqrt(1. - t * t) - 1.); + } + + virtual double easeOut(double t) const + { + t = t / t_scale - 1.; + return v_scale * sqrt(1. - t * t); + } + + virtual double easeInOut(double t) const + { + t /= t_scale / 2.; + if(t < 1.) return -v_scale / 2. * (sqrt(1. - t * t) - 1.); + t -= 2.; + return v_scale / 2. * (sqrt(1. - t * t) + 1.); + } +}; + +template <> +struct Easing : public CircularEase +{ + double get(const double t) const { return CircularEase::easeIn(t); } +}; + +template <> +struct Easing : public CircularEase +{ + double get(const double t) const { return CircularEase::easeOut(t); } +}; + +template <> +struct Easing : public CircularEase +{ + double get(const double t) const { return CircularEase::easeInOut(t); } +}; + +} // ease +} // ht + +#endif diff --git a/Easing/CubicEase.h b/Easing/CubicEase.h new file mode 100644 index 0000000..b85b92b --- /dev/null +++ b/Easing/CubicEase.h @@ -0,0 +1,67 @@ +/* + * Easing Functions: Copyright (c) 2010 Andy Brown + * http://www.andybrown.me.uk + * + * This work is licensed under a Creative Commons + * Attribution-ShareAlike 3.0 Unported License. + * http://creativecommons.org/licenses/by-sa/3.0/ + */ + +#pragma once +#ifndef HT_TWEEN_EASING_CUBICEASE_H +#define HT_TWEEN_EASING_CUBICEASE_H + +#include "EasingBase.h" + +namespace ht { +namespace ease { + +class CubicEase : public EasingBase +{ +public: + + virtual ~CubicEase() {} + + virtual double easeIn(double t) const + { + t /= t_scale; + return v_scale * t * t * t; + } + + virtual double easeOut(double t) const + { + t = t / t_scale - 1.; + return v_scale * (t * t * t + 1.); + } + + virtual double easeInOut(double t) const + { + t /= t_scale / 2.; + if (t < 1.) return v_scale / 2. * t * t * t; + t -= 2.; + return v_scale / 2. * (t * t * t + 2.); + } +}; + +template <> +struct Easing : public CubicEase +{ + double get(const double t) const { return CubicEase::easeIn(t); } +}; + +template <> +struct Easing : public CubicEase +{ + double get(const double t) const { return CubicEase::easeOut(t); } +}; + +template <> +struct Easing : public CubicEase +{ + double get(const double t) const { return CubicEase::easeInOut(t); } +}; + +} // ease +} // ht + +#endif diff --git a/Easing/EasingBase.h b/Easing/EasingBase.h new file mode 100644 index 0000000..d008601 --- /dev/null +++ b/Easing/EasingBase.h @@ -0,0 +1,47 @@ +/* + * Easing Functions: Copyright (c) 2010 Andy Brown + * http://www.andybrown.me.uk + * + * This work is licensed under a Creative Commons + * Attribution_ShareAlike 3.0 Unported License. + * http://creativecommons.org/licenses/by_sa/3.0/ + */ + +#pragma once +#ifndef HT_TWEEN_EASING_EASINGBASE_H +#define HT_TWEEN_EASING_EASINGBASE_H + +namespace ht { +namespace ease { + +class EasingBase +{ +protected: + + double v_scale {1.}; + double t_scale {1.}; + +public: + + // easing API methods + virtual double easeIn(double t) const = 0; + virtual double easeOut(double t) const = 0; + virtual double easeInOut(double t) const = 0; + + // common properties + void duration(const double t) { t_scale = t; } + void scale(const double s) { v_scale = s; } + double duration() const { return t_scale; } + double scale() const { return v_scale; } +}; + +template +struct Easing +{ + double get(const double t); +}; + +} // ease +} // ht + +#endif diff --git a/Easing/ElasticEase.h b/Easing/ElasticEase.h new file mode 100644 index 0000000..2cd4c72 --- /dev/null +++ b/Easing/ElasticEase.h @@ -0,0 +1,132 @@ +/* + * Easing Functions: Copyright (c) 2010 Andy Brown + * http://www.andybrown.me.uk + * + * This work is licensed under a Creative Commons + * Attribution-ShareAlike 3.0 Unported License. + * http://creativecommons.org/licenses/by-sa/3.0/ + */ + +#pragma once +#ifndef HT_TWEEN_EASING_ELASTICEASE_H +#define HT_TWEEN_EASING_ELASTICEASE_H + +#include "EasingBase.h" + +namespace ht { +namespace ease { + +class ElasticEase : public EasingBase +{ + double v_period {0}; + double v_amplitude {0}; + +public: + + virtual ~ElasticEase() {} + + virtual double easeIn(double t) const + { + if (t == 0) return 0; + t /= t_scale; + if (t == 1) return v_scale; + + double p, a, s; + + if (v_period == 0.) p = t_scale * 0.3; + else p = v_period; + + a = v_amplitude; + if (a == 0. || a < fabs(v_scale)) + { + a = v_scale; + s = p / 4.; + } + else + s = p / (2. * M_PI) * asin(v_scale / a); + + t -= 1.; + return -(a * pow(2., 10. * t) * sin((t * t_scale - s) * (2. * M_PI) / p)); + } + + virtual double easeOut(double t) const + { + if (t == 0.) return 0; + t /= t_scale; + if (t == 1.) return v_scale; + + double p, a, s; + + if (v_period == 0.) p = t_scale * 0.3; + else p = v_period; + + a = v_amplitude; + if (a == 0 || a < fabs(v_scale)) + { + a = v_scale; + s = p / 4.; + } + else + s = p / (2. * M_PI) * asin(v_scale / a); + + return a * pow(2., -10. * t) * sin((t * t_scale - s) * (2. * M_PI) / p) + v_scale; + } + + virtual double easeInOut(double t) const + { + if (t == 0.) return 0; + t /= t_scale / 2.; + if (t == 2.) return v_scale; + + double p, a, s; + + if (v_period == 0.) p = t_scale * (0.3 * 1.5); + else p = v_period; + + a = v_amplitude; + if (a == 0 || a < fabs(v_scale)) + { + a = v_scale; + s = p / 4.; + } + else + s = p / (2. * M_PI) * asin(v_scale / a); + + if (t < 1.) + { + t -= 1.; + return -0.5 * (a * pow(2., 10. * t) * sin((t * t_scale - s) * (2. * M_PI) / p)); + } + + t -= 1.; + return a * pow(2., -10. * t) * sin((t * t_scale - s) * (2. * M_PI) / p) * 0.5 + v_scale; + } + + void period(const double p) { v_period = p; } + void amplitude(const double a) { v_amplitude = a; } + double period() const { return v_period; } + double amplitude() const { return v_amplitude; } +}; + +template <> +struct Easing : public ElasticEase +{ + double get(const double t) const { return ElasticEase::easeIn(t); } +}; + +template <> +struct Easing : public ElasticEase +{ + double get(const double t) const { return ElasticEase::easeOut(t); } +}; + +template <> +struct Easing : public ElasticEase +{ + double get(const double t) const { return ElasticEase::easeInOut(t); } +}; + +} // ease +} // ht + +#endif diff --git a/Easing/ExponentialEase.h b/Easing/ExponentialEase.h new file mode 100644 index 0000000..dacc64a --- /dev/null +++ b/Easing/ExponentialEase.h @@ -0,0 +1,67 @@ +/* + * Easing Functions: Copyright (c) 2010 Andy Brown + * http://www.andybrown.me.uk + * + * This work is licensed under a Creative Commons + * Attribution-ShareAlike 3.0 Unported License. + * http://creativecommons.org/licenses/by-sa/3.0/ + */ + +#pragma once +#ifndef HT_TWEEN_EASING_EXPONENTIALEASE_H +#define HT_TWEEN_EASING_EXPONENTIALEASE_H + +#include "EasingBase.h" + +namespace ht { +namespace ease { + +class ExponentialEase : public EasingBase +{ +public: + + virtual ~ExponentialEase() {} + + virtual double easeIn(double t) const + { + return (t == 0.) ? 0. : v_scale * pow(2., 10. * (t / t_scale - 1.)); + } + + virtual double easeOut(double t) const + { + return (t == t_scale) ? v_scale : v_scale * (-pow(2., -10. * t / t_scale) + 1.); + } + + virtual double easeInOut(double t) const + { + if(t == 0.) return 0; + if(t == t_scale) return v_scale; + t /= t_scale / 2.; + if(t < 1.) return v_scale / 2. * pow(2., 10. * (t - 1.)); + t -= 1.; + return v_scale / 2. * (-pow(2., -10. * t) + 2.); + } +}; + +template <> +struct Easing : public ExponentialEase +{ + double get(const double t) const { return ExponentialEase::easeIn(t); } +}; + +template <> +struct Easing : public ExponentialEase +{ + double get(const double t) const { return ExponentialEase::easeOut(t); } +}; + +template <> +struct Easing : public ExponentialEase +{ + double get(const double t) const { return ExponentialEase::easeInOut(t); } +}; + +} // ease +} // ht + +#endif diff --git a/Easing/LICENSE.txt b/Easing/LICENSE.txt new file mode 100644 index 0000000..f14190a --- /dev/null +++ b/Easing/LICENSE.txt @@ -0,0 +1,8 @@ +/* + * Easing Functions: Copyright (c) 2010 Andy Brown + * http://www.andybrown.me.uk + * + * This work is licensed under a Creative Commons + * Attribution_ShareAlike 3.0 Unported License. + * http://creativecommons.org/licenses/by_sa/3.0/ + */ diff --git a/Easing/LinearEase.h b/Easing/LinearEase.h new file mode 100644 index 0000000..97168b5 --- /dev/null +++ b/Easing/LinearEase.h @@ -0,0 +1,62 @@ +/* + * Easing Functions: Copyright (c) 2010 Andy Brown + * http://www.andybrown.me.uk + * + * This work is licensed under a Creative Commons + * Attribution-ShareAlike 3.0 Unported License. + * http://creativecommons.org/licenses/by-sa/3.0/ + */ + +#pragma once +#ifndef HT_TWEEN_EASING_LINEAREASE_H +#define HT_TWEEN_EASING_LINEAREASE_H + +#include "EasingBase.h" + +namespace ht { +namespace ease { + +class LinearEase : public EasingBase +{ +public: + + virtual ~LinearEase() {} + + virtual double easeIn(double t) const + { + return v_scale * t / t_scale; + } + + virtual double easeOut(double t) const + { + return easeIn(t); + } + + virtual double easeInOut(double t) const + { + return easeIn(t); + } +}; + +template <> +struct Easing : public LinearEase +{ + double get(const double t) const { return LinearEase::easeIn(t); } +}; + +template <> +struct Easing : public LinearEase +{ + double get(const double t) const { return LinearEase::easeOut(t); } +}; + +template <> +struct Easing : public LinearEase +{ + double get(const double t) const { return LinearEase::easeInOut(t); } +}; + +} // ease +} // ht + +#endif diff --git a/Easing/QuadraticEase.h b/Easing/QuadraticEase.h new file mode 100644 index 0000000..d86853e --- /dev/null +++ b/Easing/QuadraticEase.h @@ -0,0 +1,67 @@ +/* + * Easing Functions: Copyright (c) 2010 Andy Brown + * http://www.andybrown.me.uk + * + * This work is licensed under a Creative Commons + * Attribution-ShareAlike 3.0 Unported License. + * http://creativecommons.org/licenses/by-sa/3.0/ + */ + +#pragma once +#ifndef HT_TWEEN_EASING_QUADRATICEASE_H +#define HT_TWEEN_EASING_QUADRATICEASE_H + +#include "EasingBase.h" + +namespace ht { +namespace ease { + +class QuadraticEase : public EasingBase +{ +public: + + virtual ~QuadraticEase() {} + + virtual double easeIn(double t) const + { + t /= t_scale; + return v_scale * t * t; + } + + virtual double easeOut(double t) const + { + t /= t_scale; + return -v_scale * t * (t - 2.); + } + + virtual double easeInOut(double t) const + { + t /= t_scale / 2.; + if (t < 1.) return v_scale / 2. * t * t; + t -= 1.; + return -v_scale / 2. * (t * (t - 2.) - 1.); + } +}; + +template <> +struct Easing : public QuadraticEase +{ + double get(const double t) const { return QuadraticEase::easeIn(t); } +}; + +template <> +struct Easing : public QuadraticEase +{ + double get(const double t) const { return QuadraticEase::easeOut(t); } +}; + +template <> +struct Easing : public QuadraticEase +{ + double get(const double t) const { return QuadraticEase::easeInOut(t); } +}; + +} // ease +} // ht + +#endif diff --git a/Easing/QuarticEase.h b/Easing/QuarticEase.h new file mode 100644 index 0000000..7315c3d --- /dev/null +++ b/Easing/QuarticEase.h @@ -0,0 +1,67 @@ +/* + * Easing Functions: Copyright (c) 2010 Andy Brown + * http://www.andybrown.me.uk + * + * This work is licensed under a Creative Commons + * Attribution_ShareAlike 3.0 Unported License. + * http://creativecommons.org/licenses/by_sa/3.0/ + */ + +#pragma once +#ifndef HT_TWEEN_EASING_QUARTICEASE_H +#define HT_TWEEN_EASING_QUARTICEASE_H + +#include "EasingBase.h" + +namespace ht { +namespace ease { + +class QuarticEase : public EasingBase +{ +public: + + virtual ~QuarticEase() {} + + virtual double easeIn(double t) const + { + t /= t_scale; + return v_scale * t * t * t * t; + } + + virtual double easeOut(double t) const + { + t = t / t_scale - 1.; + return -v_scale * (t * t * t * t - 1.); + } + + virtual double easeInOut(double t) const + { + t /= t_scale / 2.; + if(t < 1.) return v_scale / 2. * t * t * t * t; + t -= 2.; + return -v_scale / 2. * (t * t * t * t - 2.); + } +}; + +template <> +struct Easing : public QuarticEase +{ + double get(const double t) const { return QuarticEase::easeIn(t); } +}; + +template <> +struct Easing : public QuarticEase +{ + double get(const double t) const { return QuarticEase::easeOut(t); } +}; + +template <> +struct Easing : public QuarticEase +{ + double get(const double t) const { return QuarticEase::easeInOut(t); } +}; + +} // ease +} // ht + +#endif diff --git a/Easing/QuinticEase.h b/Easing/QuinticEase.h new file mode 100644 index 0000000..82fdd64 --- /dev/null +++ b/Easing/QuinticEase.h @@ -0,0 +1,67 @@ +/* + * Easing Functions: Copyright (c) 2010 Andy Brown + * http://www.andybrown.me.uk + * + * This work is licensed under a Creative Commons + * Attribution_ShareAlike 3.0 Unported License. + * http://creativecommons.org/licenses/by_sa/3.0/ + */ + +#pragma once +#ifndef HT_TWEEN_EASING_QUINTICEASE_H +#define HT_TWEEN_EASING_QUINTICEASE_H + +#include "EasingBase.h" + +namespace ht { +namespace ease { + +class QuinticEase : public EasingBase +{ +public: + + virtual ~QuinticEase() {} + + virtual double easeIn(double t) const + { + t /= t_scale; + return v_scale * t * t * t * t * t; + } + + virtual double easeOut(double t) const + { + t = t / t_scale - 1.; + return v_scale * (t * t * t * t * t + 1.); + } + + virtual double easeInOut(double t) const + { + t /= t_scale / 2.; + if (t < 1.) return v_scale / 2. * t * t * t * t * t; + t -= 2.; + return v_scale / 2. * (t * t * t * t * t + 2.); + } +}; + +template <> +struct Easing : public QuinticEase +{ + double get(const double t) const { return QuinticEase::easeIn(t); } +}; + +template <> +struct Easing : public QuinticEase +{ + double get(const double t) const { return QuinticEase::easeOut(t); } +}; + +template <> +struct Easing : public QuinticEase +{ + double get(const double t) const { return QuinticEase::easeInOut(t); } +}; + +} // ease +} // ht + +#endif diff --git a/Easing/SineEase.h b/Easing/SineEase.h new file mode 100644 index 0000000..84af8c6 --- /dev/null +++ b/Easing/SineEase.h @@ -0,0 +1,62 @@ +/* + * Easing Functions: Copyright (c) 2010 Andy Brown + * http://www.andybrown.me.uk + * + * This work is licensed under a Creative Commons + * Attribution_ShareAlike 3.0 Unported License. + * http://creativecommons.org/licenses/by_sa/3.0/ + */ + +#pragma once +#ifndef HT_TWEEN_EASING_SINEEASE_H +#define HT_TWEEN_EASING_SINEEASE_H + +#include "EasingBase.h" + +namespace ht { +namespace ease { + +class SineEase : public EasingBase +{ +public: + + virtual ~SineEase() {} + + virtual double easeIn(double t) const + { + return -v_scale * cos(t / t_scale * M_PI_2) + v_scale; + } + + virtual double easeOut(double t) const + { + return v_scale * sin(t / t_scale * M_PI_2); + } + + virtual double easeInOut(double t) const + { + return -v_scale / 2. * (cos(M_PI * t / t_scale) - 1.); + } +}; + +template <> +struct Easing : public SineEase +{ + double get(const double t) const { return SineEase::easeIn(t); } +}; + +template <> +struct Easing : public SineEase +{ + double get(const double t) const { return SineEase::easeOut(t); } +}; + +template <> +struct Easing : public SineEase +{ + double get(const double t) const { return SineEase::easeInOut(t); } +}; + +} // ease +} // ht + +#endif diff --git a/Easing/Types.h b/Easing/Types.h new file mode 100644 index 0000000..c4926f5 --- /dev/null +++ b/Easing/Types.h @@ -0,0 +1,68 @@ +#pragma once +#ifndef HT_TWEEN_EASING_TYPES_H +#define HT_TWEEN_EASING_TYPES_H + +#include "EasingBase.h" + +namespace ht { +namespace ease { + + struct BackIn {}; + struct BackOut {}; + struct BackInOut {}; + using Back = BackInOut; + + struct BounceIn {}; + struct BounceOut {}; + struct BounceInOut {}; + using Bounce = BounceInOut; + + struct CircIn {}; + struct CircOut {}; + struct CircInOut {}; + using Circ = CircInOut; + + struct CubicIn {}; + struct CubicOut {}; + struct CubicInOut {}; + using Cubic = CubicInOut; + + struct ElasticIn {}; + struct ElasticOut {}; + struct ElasticInOut {}; + using Elastic = ElasticInOut; + + struct ExpoIn {}; + struct ExpoOut {}; + struct ExpoInOut {}; + using Expo = ExpoInOut; + + struct LinearIn {}; + struct LinearOut {}; + struct LinearInOut {}; + using Linear = LinearInOut; + + struct QuadIn {}; + struct QuadOut {}; + struct QuadInOut {}; + using Quad = QuadInOut; + + struct QuartIn {}; + struct QuartOut {}; + struct QuartInOut {}; + using Quart = QuartInOut; + + struct QuintIn {}; + struct QuintOut {}; + struct QuintInOut {}; + using Quint = QuintInOut; + + struct SineIn {}; + struct SineOut {}; + struct SineInOut {}; + using Sine = SineInOut; + +} // ease +} // ht + +#endif // HT_TWEEN_EASING_TYPES_H diff --git a/README.md b/README.md index a1cea52..af943ab 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,100 @@ # Easing -Easing function library for Arduino + +Easing function library for Arduino. + +This library is based on the [Andy Brown's Easing Function Library](https://andybrown.me.uk/2010/12/05/animation-on-the-arduino-with-easing-functions/). +For animating example of easing functions, see website like [this](https://easings.net/en). + +[Tween](https://github.com/hideakitai/Tween) library is more flexible and useful tweening library based on this easing library. Please check it for more practical use. + +## Usage + +```C++ +EasingFunc e; +float start; + +void setup() +{ + e.duration(duration); // default duration is 1.0 + e.scale(scale); // default scale is 1.0 + + start = millis() / 1000.; +} + +void loop() +{ + // value moves from 0.0 to scale in duration [sec] + float now = millis() / 1000.; + float value = e.get(now - start); + + Serial.println(value); +} +``` + +## APIs + +```C++ +// get automatically in/out/inout value based on easing types +double get(const double t); + +void duration(const double t); +void scale(const double s); +double duration() const; +double scale() const; + +// you can also get in/out/inout values regardless of easing type +// (not recommended) +double easeIn(double t) const; +double easeOut(double t) const; +double easeInOut(double t) const; +``` + +#### `Ease::Back` family only + +```C++ +void overshoot(const double ovs) { v_overshoot = ovs; } +double overshoot() const { return v_overshoot; } +``` + +#### `Ease::Elastic` family only + +```C++ +void period(const double p) { v_period = p; } +void amplitude(const double a) { v_amplitude = a; } +double period() const { return v_period; } +double amplitude() const { return v_amplitude; } +``` + +## Easing Types + +- BackIn, BackOut, BackInOut +- BounceIn, BounceOut, BounceInOut +- CircIn, CircOut, CircInOut +- CubicIn, CubicOut, CubicInOut +- ElasticIn, ElasticOut, ElasticInOut +- ExpoIn, ExpoOut, ExpoInOut +- LinearIn, LinearOut, LinearInOut +- QuadIn, QuadOut, QuadInOut +- QuartIn, QuartOut, QuartInOut +- QuintIn, QuintOut, QuintInOut +- SineIn, SineOut, SineInOut + +```C++ +// alias +using Back = BackInOut; +using Bounce = BounceInOut; +using Circ = CircInOut; +using Cubic = CubicInOut; +using Elastic = ElasticInOut; +using Expo = ExpoInOut; +using Linear = LinearInOut; +using Quad = QuadInOut; +using Quart = QuartInOut; +using Quint = QuintInOut; +using Sine = SineInOut; +``` + + +## License + +MIT diff --git a/examples/simple/simple.ino b/examples/simple/simple.ino new file mode 100644 index 0000000..8686952 --- /dev/null +++ b/examples/simple/simple.ino @@ -0,0 +1,77 @@ +#include + +EasingFunc ba; +EasingFunc bo; +EasingFunc ci; +EasingFunc cu; +EasingFunc el; +EasingFunc ex; +EasingFunc ln; +EasingFunc qd; +EasingFunc qa; +EasingFunc qi; +EasingFunc sn; + +float duration = 20.; +float scale = 5.; +float start_sec; + +void setup() +{ + Serial.begin(115200); + delay(5000); + + // default duration is 1.0 + ba.duration(duration); + bo.duration(duration); + ci.duration(duration); + cu.duration(duration); + el.duration(duration); + ex.duration(duration); + ln.duration(duration); + qd.duration(duration); + qa.duration(duration); + qi.duration(duration); + sn.duration(duration); + + // default scale is 1.0 + ba.scale(scale); + bo.scale(scale); + ci.scale(scale); + cu.scale(scale); + el.scale(scale); + ex.scale(scale); + ln.scale(scale); + qd.scale(scale); + qa.scale(scale); + qi.scale(scale); + sn.scale(scale); + + // serial plotter label + Serial.println("back, bounce, circ, cubic, elastic, expo, linear, quad, quart, quint, sine"); + + start_sec = millis() / 1000.; +} + +void loop() +{ + float now = millis() / 1000.; + + if (now <= start_sec + duration) + { + Serial.print(ba.get(now - start_sec)); Serial.print(", "); + Serial.print(bo.get(now - start_sec)); Serial.print(", "); + Serial.print(ci.get(now - start_sec)); Serial.print(", "); + Serial.print(cu.get(now - start_sec)); Serial.print(", "); + Serial.print(el.get(now - start_sec)); Serial.print(", "); + Serial.print(ex.get(now - start_sec)); Serial.print(", "); + Serial.print(ln.get(now - start_sec)); Serial.print(", "); + Serial.print(qd.get(now - start_sec)); Serial.print(", "); + Serial.print(qa.get(now - start_sec)); Serial.print(", "); + Serial.print(qi.get(now - start_sec)); Serial.print(", "); + Serial.print(sn.get(now - start_sec)); Serial.println(); + } + + delay(50); +} + diff --git a/library.json b/library.json new file mode 100644 index 0000000..23921c1 --- /dev/null +++ b/library.json @@ -0,0 +1,20 @@ +{ + "name": "Easing", + "keywords": "easing, tween", + "description": "Easing function library for Arduino", + "repository": + { + "type": "git", + "url": "https://github.com/hideakitai/Easing.git" + }, + "authors": + { + "name": "Hideaki Tai", + "url": "https://github.com/hideakitai", + "maintainer": true + }, + "version": "0.1.0", + "license": "MIT", + "frameworks": "arduino", + "platforms": "*" +} diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..87483dc --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=Easing +version=0.1.0 +author=hideakitai +maintainer=hideakitai +sentence=Easing function library for Arduino +paragraph=Easing function library for Arduino +category=Data Processing +url=https://github.com/hideakitai +architectures=*