• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

excaliburjs / Excalibur / 14804036802

02 May 2025 09:58PM UTC coverage: 5.927% (-83.4%) from 89.28%
14804036802

Pull #3404

github

web-flow
Merge 5c103d7f8 into 0f2ccaeb2
Pull Request #3404: feat: added Graph module to Math

234 of 8383 branches covered (2.79%)

229 of 246 new or added lines in 1 file covered. (93.09%)

13145 existing lines in 208 files now uncovered.

934 of 15759 relevant lines covered (5.93%)

4.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.0
/src/engine/Util/EasingFunctions.ts
1
import { Vector } from '../Math/vector';
2

3
/**
4
 * A definition of an EasingFunction. See {@apilink EasingFunctions}.
5
 */
6
// tslint:disable-next-line
7
export interface EasingFunction<TValueToEase = number> {
8
  (currentTime: number, startValue: TValueToEase, endValue: TValueToEase, duration: number): TValueToEase;
9
}
10

11
/**
12
 * Standard easing functions for motion in Excalibur, defined on a domain of [0, duration] and a range from [+startValue,+endValue]
13
 * Given a time, the function will return a value from positive startValue to positive endValue.
14
 *
15
 * ```js
16
 * function Linear (t) {
17
 *    return t * t;
18
 * }
19
 *
20
 * // accelerating from zero velocity
21
 * function EaseInQuad (t) {
22
 *    return t * t;
23
 * }
24
 *
25
 * // decelerating to zero velocity
26
 * function EaseOutQuad (t) {
27
 *    return t * (2 - t);
28
 * }
29
 *
30
 * // acceleration until halfway, then deceleration
31
 * function EaseInOutQuad (t) {
32
 *    return t < .5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
33
 * }
34
 *
35
 * // accelerating from zero velocity
36
 * function EaseInCubic (t) {
37
 *    return t * t * t;
38
 * }
39
 *
40
 * // decelerating to zero velocity
41
 * function EaseOutCubic (t) {
42
 *    return (--t) * t * t + 1;
43
 * }
44
 *
45
 * // acceleration until halfway, then deceleration
46
 * function EaseInOutCubic (t) {
47
 *    return t < .5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
48
 * }
49
 * ```
50
 */
51
export class EasingFunctions {
52
  public static CreateReversibleEasingFunction(easing: EasingFunction): EasingFunction {
53
    return (time: number, start: number, end: number, duration: number) => {
7✔
UNCOV
54
      if (end < start) {
×
UNCOV
55
        return start - (easing(time, end, start, duration) - end);
×
56
      } else {
UNCOV
57
        return easing(time, start, end, duration);
×
58
      }
59
    };
60
  }
61

62
  public static CreateVectorEasingFunction(easing: EasingFunction<number>): EasingFunction<Vector> {
UNCOV
63
    return (time: number, start: Vector, end: Vector, duration: number) => {
×
UNCOV
64
      return new Vector(easing(time, start.x, end.x, duration), easing(time, start.y, end.y, duration));
×
65
    };
66
  }
67

68
  public static Linear: EasingFunction = EasingFunctions.CreateReversibleEasingFunction(
1✔
69
    (currentTime: number, startValue: number, endValue: number, duration: number) => {
UNCOV
70
      endValue = endValue - startValue;
×
UNCOV
71
      return (endValue * currentTime) / duration + startValue;
×
72
    }
73
  );
74

75
  public static EaseInQuad = EasingFunctions.CreateReversibleEasingFunction(
1✔
76
    (currentTime: number, startValue: number, endValue: number, duration: number) => {
UNCOV
77
      endValue = endValue - startValue;
×
UNCOV
78
      currentTime /= duration;
×
79

UNCOV
80
      return endValue * currentTime * currentTime + startValue;
×
81
    }
82
  );
83

84
  public static EaseOutQuad: EasingFunction = EasingFunctions.CreateReversibleEasingFunction(
1✔
85
    (currentTime: number, startValue: number, endValue: number, duration: number) => {
UNCOV
86
      endValue = endValue - startValue;
×
UNCOV
87
      currentTime /= duration;
×
UNCOV
88
      return -endValue * currentTime * (currentTime - 2) + startValue;
×
89
    }
90
  );
91

92
  public static EaseInOutQuad: EasingFunction = EasingFunctions.CreateReversibleEasingFunction(
1✔
93
    (currentTime: number, startValue: number, endValue: number, duration: number) => {
UNCOV
94
      endValue = endValue - startValue;
×
UNCOV
95
      currentTime /= duration / 2;
×
96

UNCOV
97
      if (currentTime < 1) {
×
UNCOV
98
        return (endValue / 2) * currentTime * currentTime + startValue;
×
99
      }
UNCOV
100
      currentTime--;
×
101

UNCOV
102
      return (-endValue / 2) * (currentTime * (currentTime - 2) - 1) + startValue;
×
103
    }
104
  );
105

106
  public static EaseInCubic: EasingFunction = EasingFunctions.CreateReversibleEasingFunction(
1✔
107
    (currentTime: number, startValue: number, endValue: number, duration: number) => {
UNCOV
108
      endValue = endValue - startValue;
×
UNCOV
109
      currentTime /= duration;
×
UNCOV
110
      return endValue * currentTime * currentTime * currentTime + startValue;
×
111
    }
112
  );
113

114
  public static EaseOutCubic: EasingFunction = EasingFunctions.CreateReversibleEasingFunction(
1✔
115
    (currentTime: number, startValue: number, endValue: number, duration: number) => {
UNCOV
116
      endValue = endValue - startValue;
×
UNCOV
117
      currentTime /= duration;
×
UNCOV
118
      currentTime--;
×
UNCOV
119
      return endValue * (currentTime * currentTime * currentTime + 1) + startValue;
×
120
    }
121
  );
122

123
  public static EaseInOutCubic: EasingFunction = EasingFunctions.CreateReversibleEasingFunction(
1✔
124
    (currentTime: number, startValue: number, endValue: number, duration: number) => {
UNCOV
125
      endValue = endValue - startValue;
×
UNCOV
126
      currentTime /= duration / 2;
×
UNCOV
127
      if (currentTime < 1) {
×
UNCOV
128
        return (endValue / 2) * currentTime * currentTime * currentTime + startValue;
×
129
      }
UNCOV
130
      currentTime -= 2;
×
UNCOV
131
      return (endValue / 2) * (currentTime * currentTime * currentTime + 2) + startValue;
×
132
    }
133
  );
134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc