easing.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #pragma once
  2. #include "cffi.h"
  3. namespace pkpy{
  4. // https://easings.net/
  5. static const double PI = 3.1415926545;
  6. inline static double easeLinear( double x ) {
  7. return x;
  8. }
  9. inline static double easeInSine( double x ) {
  10. return 1.0 - std::cos( x * PI / 2 );
  11. }
  12. inline static double easeOutSine( double x ) {
  13. return std::sin( x * PI / 2 );
  14. }
  15. inline static double easeInOutSine( double x ) {
  16. return -( std::cos( PI * x ) - 1 ) / 2;
  17. }
  18. inline static double easeInQuad( double x ) {
  19. return x * x;
  20. }
  21. inline static double easeOutQuad( double x ) {
  22. return 1 - std::pow( 1 - x, 2 );
  23. }
  24. inline static double easeInOutQuad( double x ) {
  25. if( x < 0.5 ) {
  26. return 2 * x * x;
  27. } else {
  28. return 1 - std::pow( -2 * x + 2, 2 ) / 2;
  29. }
  30. }
  31. inline static double easeInCubic( double x ) {
  32. return x * x * x;
  33. }
  34. inline static double easeOutCubic( double x ) {
  35. return 1 - std::pow( 1 - x, 3 );
  36. }
  37. inline static double easeInOutCubic( double x ) {
  38. if( x < 0.5 ) {
  39. return 4 * x * x * x;
  40. } else {
  41. return 1 - std::pow( -2 * x + 2, 3 ) / 2;
  42. }
  43. }
  44. inline static double easeInQuart( double x ) {
  45. return std::pow( x, 4 );
  46. }
  47. inline static double easeOutQuart( double x ) {
  48. return 1 - std::pow( 1 - x, 4 );
  49. }
  50. inline static double easeInOutQuart( double x ) {
  51. if( x < 0.5 ) {
  52. return 8 * std::pow( x, 4 );
  53. } else {
  54. return 1 - std::pow( -2 * x + 2, 4 ) / 2;
  55. }
  56. }
  57. inline static double easeInQuint( double x ) {
  58. return std::pow( x, 5 );
  59. }
  60. inline static double easeOutQuint( double x ) {
  61. return 1 - std::pow( 1 - x, 5 );
  62. }
  63. inline static double easeInOutQuint( double x ) {
  64. if( x < 0.5 ) {
  65. return 16 * std::pow( x, 5 );
  66. } else {
  67. return 1 - std::pow( -2 * x + 2, 5 ) / 2;
  68. }
  69. }
  70. inline static double easeInExpo( double x ) {
  71. return x == 0 ? 0 : std::pow( 2, 10 * x - 10 );
  72. }
  73. inline static double easeOutExpo( double x ) {
  74. return x == 1 ? 1 : 1 - std::pow( 2, -10 * x );
  75. }
  76. inline double easeInOutExpo( double x ) {
  77. if( x == 0 ) {
  78. return 0;
  79. } else if( x == 1 ) {
  80. return 1;
  81. } else if( x < 0.5 ) {
  82. return std::pow( 2, 20 * x - 10 ) / 2;
  83. } else {
  84. return (2 - std::pow( 2, -20 * x + 10 )) / 2;
  85. }
  86. }
  87. inline static double easeInCirc( double x ) {
  88. return 1 - std::sqrt( 1 - std::pow( x, 2 ) );
  89. }
  90. inline static double easeOutCirc( double x ) {
  91. return std::sqrt( 1 - std::pow( x - 1, 2 ) );
  92. }
  93. inline static double easeInOutCirc( double x ) {
  94. if( x < 0.5 ) {
  95. return (1 - std::sqrt( 1 - std::pow( 2 * x, 2 ) )) / 2;
  96. } else {
  97. return (std::sqrt( 1 - std::pow( -2 * x + 2, 2 ) ) + 1) / 2;
  98. }
  99. }
  100. inline static double easeInBack( double x ) {
  101. const double c1 = 1.70158;
  102. const double c3 = c1 + 1;
  103. return c3 * x * x * x - c1 * x * x;
  104. }
  105. inline static double easeOutBack( double x ) {
  106. const double c1 = 1.70158;
  107. const double c3 = c1 + 1;
  108. return 1 + c3 * std::pow( x - 1, 3 ) + c1 * std::pow( x - 1, 2 );
  109. }
  110. inline static double easeInOutBack( double x ) {
  111. const double c1 = 1.70158;
  112. const double c2 = c1 * 1.525;
  113. if( x < 0.5 ) {
  114. return (std::pow( 2 * x, 2 ) * ((c2 + 1) * 2 * x - c2)) / 2;
  115. } else {
  116. return (std::pow( 2 * x - 2, 2 ) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2;
  117. }
  118. }
  119. inline static double easeInElastic( double x ) {
  120. const double c4 = (2 * PI) / 3;
  121. if( x == 0 ) {
  122. return 0;
  123. } else if( x == 1 ) {
  124. return 1;
  125. } else {
  126. return -std::pow( 2, 10 * x - 10 ) * std::sin( (x * 10 - 10.75) * c4 );
  127. }
  128. }
  129. inline static double easeOutElastic( double x ) {
  130. const double c4 = (2 * PI) / 3;
  131. if( x == 0 ) {
  132. return 0;
  133. } else if( x == 1 ) {
  134. return 1;
  135. } else {
  136. return std::pow( 2, -10 * x ) * std::sin( (x * 10 - 0.75) * c4 ) + 1;
  137. }
  138. }
  139. inline double easeInOutElastic( double x ) {
  140. const double c5 = (2 * PI) / 4.5;
  141. if( x == 0 ) {
  142. return 0;
  143. } else if( x == 1 ) {
  144. return 1;
  145. } else if( x < 0.5 ) {
  146. return -(std::pow( 2, 20 * x - 10 ) * std::sin( (20 * x - 11.125) * c5 )) / 2;
  147. } else {
  148. return (std::pow( 2, -20 * x + 10 ) * std::sin( (20 * x - 11.125) * c5 )) / 2 + 1;
  149. }
  150. }
  151. inline static double easeOutBounce( double x ) {
  152. const double n1 = 7.5625;
  153. const double d1 = 2.75;
  154. if( x < 1 / d1 ) {
  155. return n1 * x * x;
  156. } else if( x < 2 / d1 ) {
  157. x -= 1.5 / d1;
  158. return n1 * x * x + 0.75;
  159. } else if( x < 2.5 / d1 ) {
  160. x -= 2.25 / d1;
  161. return n1 * x * x + 0.9375;
  162. } else {
  163. x -= 2.625 / d1;
  164. return n1 * x * x + 0.984375;
  165. }
  166. }
  167. inline double easeInBounce( double x ) {
  168. return 1 - easeOutBounce(1 - x);
  169. }
  170. inline static double easeInOutBounce( double x ) {
  171. return x < 0.5
  172. ? (1 - easeOutBounce(1 - 2 * x)) / 2
  173. : (1 + easeOutBounce(2 * x - 1)) / 2;
  174. }
  175. inline void add_module_easing(VM* vm){
  176. PyObject* mod = vm->new_module("easing");
  177. #define EASE(name) \
  178. vm->bind_func<1>(mod, "Ease"#name, [](VM* vm, ArgsView args){ \
  179. f64 t = CAST(f64, args[0]); \
  180. return VAR(ease##name(t)); \
  181. });
  182. EASE(Linear)
  183. EASE(InSine)
  184. EASE(OutSine)
  185. EASE(InOutSine)
  186. EASE(InQuad)
  187. EASE(OutQuad)
  188. EASE(InOutQuad)
  189. EASE(InCubic)
  190. EASE(OutCubic)
  191. EASE(InOutCubic)
  192. EASE(InQuart)
  193. EASE(OutQuart)
  194. EASE(InOutQuart)
  195. EASE(InQuint)
  196. EASE(OutQuint)
  197. EASE(InOutQuint)
  198. EASE(InExpo)
  199. EASE(OutExpo)
  200. EASE(InOutExpo)
  201. EASE(InCirc)
  202. EASE(OutCirc)
  203. EASE(InOutCirc)
  204. EASE(InBack)
  205. EASE(OutBack)
  206. EASE(InOutBack)
  207. EASE(InElastic)
  208. EASE(OutElastic)
  209. EASE(InOutElastic)
  210. EASE(InBounce)
  211. EASE(OutBounce)
  212. EASE(InOutBounce)
  213. #undef EASE
  214. }
  215. } // namespace pkpy