-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEasingFunction.java
More file actions
165 lines (146 loc) · 5.68 KB
/
Copy pathEasingFunction.java
File metadata and controls
165 lines (146 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.sovdee.shapes.modifiers;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
/**
* Maps a normalised value {@code t ∈ [0, 1]} to a (typically) {@code [0, 1]} output, shaping
* how interpolation progresses along a modifier's input axis.
* <br>
* Built-in constants and factory methods cover the most common curves, like {@link #SINE_IN_OUT}.
* <br>
* Custom implementations must provide a stable {@link #easingHash()} so that modifier cache
* invalidation works correctly.
*/
public interface EasingFunction {
/**
* Given an input between 0 and 1, applies the easing function and returns a mapped output
* @param t Input between 0 and 1.
* @return Output value mapped by the easing function.
*/
double apply(double t);
/**
* @return Stable hash of this easing's configuration, used in modifier cache keys.
* Two easings that produce identical output for all inputs should return the same value.
*/
int easingHash();
/**
* Controls which end of the interpolation range is eased.
* <ul>
* <li>{@link #IN} — slow start, fast end (ease into motion)</li>
* <li>{@link #OUT} — fast start, slow end (ease out of motion)</li>
* <li>{@link #IN_OUT} — slow start and slow end (symmetric ease)</li>
* </ul>
*/
enum Mode {
IN,
OUT,
IN_OUT
}
/**
* Power-curve easing: {@code t^n} (in), {@code 1-(1-t)^n} (out),
* symmetric combination (in-out).
* @param exponent 1 = linear, 2 = quadratic, 3 = cubic, etc.
* @param mode Whether to have the start, end, or both be smooth.
*/
record PowerEasing(double exponent, Mode mode) implements EasingFunction {
/**
* Applies the power-curve easing to {@code t}.
* <ul>
* <li>IN: {@code t^exponent}</li>
* <li>OUT: {@code 1 - (1-t)^exponent}</li>
* <li>IN_OUT: piecewise symmetric combination of the above</li>
* </ul>
*
* @param t normalised input in {@code [0, 1]}
* @return eased output value
*/
@Override
public double apply(double t) {
return switch (mode) {
case IN -> Math.pow(t, exponent);
case OUT -> 1.0 - Math.pow(1.0 - t, exponent);
case IN_OUT -> {
if (t < 0.5)
yield Math.pow(2.0 * t, exponent) / 2.0;
yield 1.0 - Math.pow(-2.0 * t + 2.0, exponent) / 2.0;
}
};
}
/**
* @return a hash combining the exponent and mode ordinal,
* unique among all standard {@code PowerEasing} configurations
*/
@Override
public int easingHash() {
return 31 * Double.hashCode(exponent) + mode.ordinal();
}
}
/**
* Sine-based easing.
* @param mode Whether to have the start, end, or both be smooth.
*/
record SineEasing(Mode mode) implements EasingFunction {
/**
* Applies the sine-based easing to {@code t}.
* <ul>
* <li>IN: {@code 1 - cos(t * π/2)}</li>
* <li>OUT: {@code sin(t * π/2)}</li>
* <li>IN_OUT: {@code -(cos(π*t) - 1) / 2}</li>
* </ul>
*
* @param t normalised input in {@code [0, 1]}
* @return eased output value
*/
@Override
public double apply(double t) {
return switch (mode) {
case IN -> 1.0 - Math.cos(t * Math.PI / 2.0);
case OUT -> Math.sin(t * Math.PI / 2.0);
case IN_OUT -> -(Math.cos(Math.PI * t) - 1.0) / 2.0;
};
}
@Override
public int easingHash() {
return 1000 + mode.ordinal();
}
}
// -------------------------------------------------------------------------
// Named constants
// -------------------------------------------------------------------------
EasingFunction LINEAR = new PowerEasing(1.0, Mode.IN);
EasingFunction QUAD_IN = new PowerEasing(2.0, Mode.IN);
EasingFunction QUAD_OUT = new PowerEasing(2.0, Mode.OUT);
EasingFunction QUAD_IN_OUT = new PowerEasing(2.0, Mode.IN_OUT);
EasingFunction CUBIC_IN = new PowerEasing(3.0, Mode.IN);
EasingFunction CUBIC_OUT = new PowerEasing(3.0, Mode.OUT);
EasingFunction CUBIC_IN_OUT = new PowerEasing(3.0, Mode.IN_OUT);
EasingFunction SINE_IN = new SineEasing(Mode.IN);
EasingFunction SINE_OUT = new SineEasing(Mode.OUT);
EasingFunction SINE_IN_OUT = new SineEasing(Mode.IN_OUT);
// -------------------------------------------------------------------------
// Factory methods
// -------------------------------------------------------------------------
/**
* @param exponent Degree to use for the easing function
* @return A power-based easing function with a gradual start and abrupt end
*/
@Contract("_ -> new")
static @NotNull EasingFunction powerIn(double exponent) {
return new PowerEasing(exponent, Mode.IN);
}
/**
* @param exponent Degree to use for the easing function
* @return A power-based easing function with an abrupt start and gradual end
*/
@Contract("_ -> new")
static @NotNull EasingFunction powerOut(double exponent) {
return new PowerEasing(exponent, Mode.OUT);
}
/**
* @param exponent Degree to use for the easing function
* @return A power-based easing function with a gradual start and end
*/
@Contract("_ -> new")
static @NotNull EasingFunction powerInOut(double exponent) {
return new PowerEasing(exponent, Mode.IN_OUT);
}
}