easing.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import { BezierCurve } from "../Maths/math.path";
  2. /**
  3. * This represents the main contract an easing function should follow.
  4. * Easing functions are used throughout the animation system.
  5. * @see http://doc.babylonjs.com/babylon101/animations#easing-functions
  6. */
  7. export interface IEasingFunction {
  8. /**
  9. * Given an input gradient between 0 and 1, this returns the corrseponding value
  10. * of the easing function.
  11. * The link below provides some of the most common examples of easing functions.
  12. * @see https://easings.net/
  13. * @param gradient Defines the value between 0 and 1 we want the easing value for
  14. * @returns the corresponding value on the curve defined by the easing function
  15. */
  16. ease(gradient: number): number;
  17. }
  18. /**
  19. * Base class used for every default easing function.
  20. * @see http://doc.babylonjs.com/babylon101/animations#easing-functions
  21. */
  22. export class EasingFunction implements IEasingFunction {
  23. /**
  24. * Interpolation follows the mathematical formula associated with the easing function.
  25. */
  26. public static readonly EASINGMODE_EASEIN = 0;
  27. /**
  28. * Interpolation follows 100% interpolation minus the output of the formula associated with the easing function.
  29. */
  30. public static readonly EASINGMODE_EASEOUT = 1;
  31. /**
  32. * Interpolation uses EaseIn for the first half of the animation and EaseOut for the second half.
  33. */
  34. public static readonly EASINGMODE_EASEINOUT = 2;
  35. private _easingMode = EasingFunction.EASINGMODE_EASEIN;
  36. /**
  37. * Sets the easing mode of the current function.
  38. * @param easingMode Defines the willing mode (EASINGMODE_EASEIN, EASINGMODE_EASEOUT or EASINGMODE_EASEINOUT)
  39. */
  40. public setEasingMode(easingMode: number) {
  41. var n = Math.min(Math.max(easingMode, 0), 2);
  42. this._easingMode = n;
  43. }
  44. /**
  45. * Gets the current easing mode.
  46. * @returns the easing mode
  47. */
  48. public getEasingMode(): number {
  49. return this._easingMode;
  50. }
  51. /**
  52. * @hidden
  53. */
  54. public easeInCore(gradient: number): number {
  55. throw new Error('You must implement this method');
  56. }
  57. /**
  58. * Given an input gradient between 0 and 1, this returns the corresponding value
  59. * of the easing function.
  60. * @param gradient Defines the value between 0 and 1 we want the easing value for
  61. * @returns the corresponding value on the curve defined by the easing function
  62. */
  63. public ease(gradient: number): number {
  64. switch (this._easingMode) {
  65. case EasingFunction.EASINGMODE_EASEIN:
  66. return this.easeInCore(gradient);
  67. case EasingFunction.EASINGMODE_EASEOUT:
  68. return (1 - this.easeInCore(1 - gradient));
  69. }
  70. if (gradient >= 0.5) {
  71. return (((1 - this.easeInCore((1 - gradient) * 2)) * 0.5) + 0.5);
  72. }
  73. return (this.easeInCore(gradient * 2) * 0.5);
  74. }
  75. }
  76. /**
  77. * Easing function with a circle shape (see link below).
  78. * @see https://easings.net/#easeInCirc
  79. * @see http://doc.babylonjs.com/babylon101/animations#easing-functions
  80. */
  81. export class CircleEase extends EasingFunction implements IEasingFunction {
  82. /** @hidden */
  83. public easeInCore(gradient: number): number {
  84. gradient = Math.max(0, Math.min(1, gradient));
  85. return (1.0 - Math.sqrt(1.0 - (gradient * gradient)));
  86. }
  87. }
  88. /**
  89. * Easing function with a ease back shape (see link below).
  90. * @see https://easings.net/#easeInBack
  91. * @see http://doc.babylonjs.com/babylon101/animations#easing-functions
  92. */
  93. export class BackEase extends EasingFunction implements IEasingFunction {
  94. /**
  95. * Instantiates a back ease easing
  96. * @see https://easings.net/#easeInBack
  97. * @param amplitude Defines the amplitude of the function
  98. */
  99. constructor(
  100. /** Defines the amplitude of the function */
  101. public amplitude: number = 1) {
  102. super();
  103. }
  104. /** @hidden */
  105. public easeInCore(gradient: number): number {
  106. var num = Math.max(0, this.amplitude);
  107. return (Math.pow(gradient, 3.0) - ((gradient * num) * Math.sin(3.1415926535897931 * gradient)));
  108. }
  109. }
  110. /**
  111. * Easing function with a bouncing shape (see link below).
  112. * @see https://easings.net/#easeInBounce
  113. * @see http://doc.babylonjs.com/babylon101/animations#easing-functions
  114. */
  115. export class BounceEase extends EasingFunction implements IEasingFunction {
  116. /**
  117. * Instantiates a bounce easing
  118. * @see https://easings.net/#easeInBounce
  119. * @param bounces Defines the number of bounces
  120. * @param bounciness Defines the amplitude of the bounce
  121. */
  122. constructor(
  123. /** Defines the number of bounces */
  124. public bounces: number = 3,
  125. /** Defines the amplitude of the bounce */
  126. public bounciness: number = 2) {
  127. super();
  128. }
  129. /** @hidden */
  130. public easeInCore(gradient: number): number {
  131. var y = Math.max(0.0, this.bounces);
  132. var bounciness = this.bounciness;
  133. if (bounciness <= 1.0) {
  134. bounciness = 1.001;
  135. }
  136. var num9 = Math.pow(bounciness, y);
  137. var num5 = 1.0 - bounciness;
  138. var num4 = ((1.0 - num9) / num5) + (num9 * 0.5);
  139. var num15 = gradient * num4;
  140. var num65 = Math.log((-num15 * (1.0 - bounciness)) + 1.0) / Math.log(bounciness);
  141. var num3 = Math.floor(num65);
  142. var num13 = num3 + 1.0;
  143. var num8 = (1.0 - Math.pow(bounciness, num3)) / (num5 * num4);
  144. var num12 = (1.0 - Math.pow(bounciness, num13)) / (num5 * num4);
  145. var num7 = (num8 + num12) * 0.5;
  146. var num6 = gradient - num7;
  147. var num2 = num7 - num8;
  148. return (((-Math.pow(1.0 / bounciness, y - num3) / (num2 * num2)) * (num6 - num2)) * (num6 + num2));
  149. }
  150. }
  151. /**
  152. * Easing function with a power of 3 shape (see link below).
  153. * @see https://easings.net/#easeInCubic
  154. * @see http://doc.babylonjs.com/babylon101/animations#easing-functions
  155. */
  156. export class CubicEase extends EasingFunction implements IEasingFunction {
  157. /** @hidden */
  158. public easeInCore(gradient: number): number {
  159. return (gradient * gradient * gradient);
  160. }
  161. }
  162. /**
  163. * Easing function with an elastic shape (see link below).
  164. * @see https://easings.net/#easeInElastic
  165. * @see http://doc.babylonjs.com/babylon101/animations#easing-functions
  166. */
  167. export class ElasticEase extends EasingFunction implements IEasingFunction {
  168. /**
  169. * Instantiates an elastic easing function
  170. * @see https://easings.net/#easeInElastic
  171. * @param oscillations Defines the number of oscillations
  172. * @param springiness Defines the amplitude of the oscillations
  173. */
  174. constructor(
  175. /** Defines the number of oscillations*/
  176. public oscillations: number = 3,
  177. /** Defines the amplitude of the oscillations*/
  178. public springiness: number = 3) {
  179. super();
  180. }
  181. /** @hidden */
  182. public easeInCore(gradient: number): number {
  183. var num2;
  184. var num3 = Math.max(0.0, this.oscillations);
  185. var num = Math.max(0.0, this.springiness);
  186. if (num == 0) {
  187. num2 = gradient;
  188. } else {
  189. num2 = (Math.exp(num * gradient) - 1.0) / (Math.exp(num) - 1.0);
  190. }
  191. return (num2 * Math.sin(((6.2831853071795862 * num3) + 1.5707963267948966) * gradient));
  192. }
  193. }
  194. /**
  195. * Easing function with an exponential shape (see link below).
  196. * @see https://easings.net/#easeInExpo
  197. * @see http://doc.babylonjs.com/babylon101/animations#easing-functions
  198. */
  199. export class ExponentialEase extends EasingFunction implements IEasingFunction {
  200. /**
  201. * Instantiates an exponential easing function
  202. * @see https://easings.net/#easeInExpo
  203. * @param exponent Defines the exponent of the function
  204. */
  205. constructor(
  206. /** Defines the exponent of the function */
  207. public exponent: number = 2) {
  208. super();
  209. }
  210. /** @hidden */
  211. public easeInCore(gradient: number): number {
  212. if (this.exponent <= 0) {
  213. return gradient;
  214. }
  215. return ((Math.exp(this.exponent * gradient) - 1.0) / (Math.exp(this.exponent) - 1.0));
  216. }
  217. }
  218. /**
  219. * Easing function with a power shape (see link below).
  220. * @see https://easings.net/#easeInQuad
  221. * @see http://doc.babylonjs.com/babylon101/animations#easing-functions
  222. */
  223. export class PowerEase extends EasingFunction implements IEasingFunction {
  224. /**
  225. * Instantiates an power base easing function
  226. * @see https://easings.net/#easeInQuad
  227. * @param power Defines the power of the function
  228. */
  229. constructor(
  230. /** Defines the power of the function */
  231. public power: number = 2) {
  232. super();
  233. }
  234. /** @hidden */
  235. public easeInCore(gradient: number): number {
  236. var y = Math.max(0.0, this.power);
  237. return Math.pow(gradient, y);
  238. }
  239. }
  240. /**
  241. * Easing function with a power of 2 shape (see link below).
  242. * @see https://easings.net/#easeInQuad
  243. * @see http://doc.babylonjs.com/babylon101/animations#easing-functions
  244. */
  245. export class QuadraticEase extends EasingFunction implements IEasingFunction {
  246. /** @hidden */
  247. public easeInCore(gradient: number): number {
  248. return (gradient * gradient);
  249. }
  250. }
  251. /**
  252. * Easing function with a power of 4 shape (see link below).
  253. * @see https://easings.net/#easeInQuart
  254. * @see http://doc.babylonjs.com/babylon101/animations#easing-functions
  255. */
  256. export class QuarticEase extends EasingFunction implements IEasingFunction {
  257. /** @hidden */
  258. public easeInCore(gradient: number): number {
  259. return (gradient * gradient * gradient * gradient);
  260. }
  261. }
  262. /**
  263. * Easing function with a power of 5 shape (see link below).
  264. * @see https://easings.net/#easeInQuint
  265. * @see http://doc.babylonjs.com/babylon101/animations#easing-functions
  266. */
  267. export class QuinticEase extends EasingFunction implements IEasingFunction {
  268. /** @hidden */
  269. public easeInCore(gradient: number): number {
  270. return (gradient * gradient * gradient * gradient * gradient);
  271. }
  272. }
  273. /**
  274. * Easing function with a sin shape (see link below).
  275. * @see https://easings.net/#easeInSine
  276. * @see http://doc.babylonjs.com/babylon101/animations#easing-functions
  277. */
  278. export class SineEase extends EasingFunction implements IEasingFunction {
  279. /** @hidden */
  280. public easeInCore(gradient: number): number {
  281. return (1.0 - Math.sin(1.5707963267948966 * (1.0 - gradient)));
  282. }
  283. }
  284. /**
  285. * Easing function with a bezier shape (see link below).
  286. * @see http://cubic-bezier.com/#.17,.67,.83,.67
  287. * @see http://doc.babylonjs.com/babylon101/animations#easing-functions
  288. */
  289. export class BezierCurveEase extends EasingFunction implements IEasingFunction {
  290. /**
  291. * Instantiates a bezier function
  292. * @see http://cubic-bezier.com/#.17,.67,.83,.67
  293. * @param x1 Defines the x component of the start tangent in the bezier curve
  294. * @param y1 Defines the y component of the start tangent in the bezier curve
  295. * @param x2 Defines the x component of the end tangent in the bezier curve
  296. * @param y2 Defines the y component of the end tangent in the bezier curve
  297. */
  298. constructor(
  299. /** Defines the x component of the start tangent in the bezier curve */
  300. public x1: number = 0,
  301. /** Defines the y component of the start tangent in the bezier curve */
  302. public y1: number = 0,
  303. /** Defines the x component of the end tangent in the bezier curve */
  304. public x2: number = 1,
  305. /** Defines the y component of the end tangent in the bezier curve */
  306. public y2: number = 1) {
  307. super();
  308. }
  309. /** @hidden */
  310. public easeInCore(gradient: number): number {
  311. return BezierCurve.Interpolate(gradient, this.x1, this.y1, this.x2, this.y2);
  312. }
  313. }