GA::kit v0.3
G&A's in-house C++ application framework
Loading...
Searching...
No Matches
easing.h
Go to the documentation of this file.
1#pragma once
2#include "ga/math.h"
3#include <functional>
4#include <vector>
5
6namespace ga {
7
8using EasingFn = std::function<float( float )>;
9
10namespace ease {
11
12 //
13 // standardized easing / tweening functions -
14 // p = f(t)
15 //
16 // convert 't' time (percent 0-1) to 'p' position (percent 0-1)
17 // based on an easing curve - see http://robertpenner.com/easing/penner_chapter7_tweening.pdf
18 //
19
20 // exponential curves
21 // ------------------
22 // https://github.com/jesusgollonet/ofpennereasing/blob/master/PennerEasing/Expo.cpp
23
24 inline float expoIn( float t )
25 { // t is percent (0.-1.) of transition
26 return ( t == 0.0f ) ? 0.0f : pow( 2.0f, 10.0f * ( t - 1.0f ) );
27 }
28
29 inline float expoOut( float t )
30 {
31 return ( t == 1.0f ) ? 1.0f : 1.0f - pow( 2.0f, -10.0f * t );
32 }
33
34 inline float expoInOut( float t )
35 {
36 if ( t == 0.0f ) {
37 return 0.0f;
38 }
39
40 if ( t == 1.0f ) {
41 return 1.0f;
42 }
43
44 if ( ( t /= 0.5f ) < 1.0f ) {
45 return 0.5f * pow( 2.0f, 10.0f * ( t - 1.0f ) );
46 }
47
48 return 0.5f * ( -pow( 2.0f, -10.0f * --t ) + 2.0f );
49 }
50
51 // cubic / quadratic curves
52 // ------------------------
53
54 inline float cubeInQuadOut( float t )
55 {
56 if ( t < 0.5f ) {
57 return 4.0f * t * t * t;
58 } // cubic in
59
60 return ( -2.0f * t * t ) + ( 4.0f * t ) - 1.0f; // quadratic out
61 }
62
63 // material design curves
64 // ----------------------
65 // https://material.io/design/motion/speed.html#easing
66
67 // Material "standard ease" (decelerate in, accelerate out)
68 inline float material( float t )
69 {
70 return cubicBezier( t, 0.4f, 0.0f, 0.2f, 1.0f );
71 }
72
73 // Material "accelerate" (e.g. exit screen)
74 inline float materialEnter( float t )
75 {
76 return cubicBezier( t, 0.0f, 0.0f, 0.2f, 1.0f );
77 }
78
79 // Material "decelerate" (e.g. enter screen)
80 inline float materialExit( float t )
81 {
82 return cubicBezier( t, 0.4f, 0.0f, 1.0f, 1.0f );
83 }
84} // namespace ease
85
86enum class EaseType
87{
88 LINEAR,
89 EXPO_IN,
96 CUSTOM,
97 // for loops:
100};
101
102inline std::function<float( float )> easeFn( const EaseType& type )
103{
104 using namespace ease;
105 switch ( type ) {
107 return expoIn;
109 return expoOut;
111 return expoInOut;
113 return cubeInQuadOut;
115 return material;
117 return materialEnter;
119 return materialExit;
120 case EaseType::LINEAR:
121 default:
122 return []( float t ) { return t; };
123 }
124}
125
126} // namespace ga
float cubeInQuadOut(float t)
Definition: easing.h:54
float material(float t)
Definition: easing.h:68
float expoInOut(float t)
Definition: easing.h:34
float materialEnter(float t)
Definition: easing.h:74
float expoIn(float t)
Definition: easing.h:24
float expoOut(float t)
Definition: easing.h:29
float materialExit(float t)
Definition: easing.h:80
Definition: color.h:9
std::function< float(float)> easeFn(const EaseType &type)
Definition: easing.h:102
float cubicBezier(float x, float x1, float y1, float x2, float y2)
Definition: math.h:208
std::function< float(float)> EasingFn
Definition: easing.h:8
EaseType
Definition: easing.h:87