/
Animation.re
201 lines (174 loc) · 5.52 KB
/
Animation.re
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
open Revery_Core;
open Revery_Math;
module type AnimationTicker = {
let time: unit => Time.t;
let onTick: Event.t(Time.t);
};
let optCall = (opt, param) =>
switch (opt) {
| Some(f) => f(param)
| None => ()
};
module Make = (AnimationTickerImpl: AnimationTicker) => {
type animationValue = {mutable current: float};
module AnimationId =
UniqueId.Make({});
type animation = {
id: int,
delay: float,
mutable startTime: float,
duration: float,
startValue: float,
toValue: float,
value: animationValue,
repeat: bool,
easing: float => float,
};
type activeAnimation = {
animation,
update: option(float => unit),
complete: option(unit => unit),
};
type playback = {
pause: unit => unit,
stop: unit => unit,
};
let activeAnimations: ref(list(activeAnimation)) = ref([]);
type animationOptions = {
duration: Time.t,
delay: Time.t,
toValue: float,
repeat: bool,
easing: float => float,
};
let linear = (t: float) => t;
let quadratic = (t: float) => t *. t;
let cubic = (t: float) => t *. t *. t;
let cubicBezier = Rebez.make;
// From https://developer.mozilla.org/en-US/docs/Web/CSS/timing-function#Keywords_for_common_cubic-bezier_timing_functions
let ease = cubicBezier(0.25, 0.1, 0.25, 1.0);
let easeIn = cubicBezier(0.42, 0.0, 1.0, 1.0);
let easeOut = cubicBezier(0.0, 0.0, 0.58, 1.0);
let easeInOut = cubicBezier(0.42, 0.0, 0.58, 1.0);
let floatValue = (v: float) => {
let ret = {current: v};
ret;
};
let getLocalTime = (clock: float, anim: animation) => {
let adjustedStart = anim.startTime +. anim.delay;
let endTime = adjustedStart +. anim.duration;
(clock -. adjustedStart) /. (endTime -. adjustedStart);
};
let hasStarted = (clock: float, {animation, _}) => {
let t = getLocalTime(clock, animation);
t > 0.;
};
let isComplete = (clock: float, {animation: anim, _}) => {
let t = getLocalTime(clock, anim);
t > 1. && !anim.repeat;
};
let tickAnimation = (clock: float, {animation: anim, update, complete}) => {
let t = anim.easing(getLocalTime(clock, anim));
if (t >= 1.) {
if (anim.repeat) {
/* If the anim is set to repeat and the time has expired, restart */
anim.startTime = anim.startTime +. anim.delay +. anim.duration;
let newT = getLocalTime(clock, anim);
anim.value.current = interpolate(newT, anim.startValue, anim.toValue);
} else {
optCall(complete, ());
};
} else {
anim.value.current = interpolate(t, anim.startValue, anim.toValue);
optCall(update, anim.value.current);
};
};
let getAnimationCount = () => List.length(activeAnimations^);
let anyActiveAnimations = () => {
let t = Time.to_float_seconds(AnimationTickerImpl.time());
let anims = List.filter(hasStarted(t), activeAnimations^);
List.length(anims) > 0;
};
let tween =
(animationValue: animationValue, animationOptions: animationOptions) => {
let animation = {
id: AnimationId.getUniqueId(),
delay: Time.to_float_seconds(animationOptions.delay),
duration: Time.to_float_seconds(animationOptions.duration),
toValue: animationOptions.toValue,
repeat: animationOptions.repeat,
value: animationValue,
startTime: Time.to_float_seconds(AnimationTickerImpl.time()),
startValue: animationValue.current,
easing: animationOptions.easing,
};
animation;
};
let start = (~update=?, ~complete=?, animation) => {
let activeAnimation = {animation, update, complete};
animation.startTime = Time.to_float_seconds(AnimationTickerImpl.time());
activeAnimations := List.append([activeAnimation], activeAnimations^);
let removeAnimation = l =>
List.filter(({animation: a, _}) => a.id !== animation.id, l);
let playback = {
pause: () => {
activeAnimations := removeAnimation(activeAnimations^);
},
stop: () => {
animation.value.current = animation.startValue;
activeAnimations := removeAnimation(activeAnimations^);
},
};
playback;
};
module Chain = {
type t = {animations: list(animation)};
let make = animation => {animations: [animation]};
let add = (animation, {animations: l}) => {
animations: List.cons(animation, l),
};
let start = (~update=?, ~complete=?, {animations: l}) => {
let currentPlayback = ref(None);
let rec runAnimation = (animations, index) => {
switch (animations) {
| [a, ...xl] =>
let playback =
a |> start(~update?, ~complete=() => runAnimation(xl, index + 1));
currentPlayback := Some(playback);
| [] => optCall(complete, ())
};
};
runAnimation(List.rev(l), 0);
let playback = {
pause: () => {
switch (currentPlayback^) {
| Some(p) => p.pause()
| None => ()
};
},
stop: () => {
switch (currentPlayback^) {
| Some(p) => p.stop()
| None => ()
};
},
};
playback;
};
};
let cancel = (anim: animation) =>
activeAnimations :=
List.filter(
({animation: a, _}) => a.id !== anim.id,
activeAnimations^,
);
let cancelAll = () => activeAnimations := [];
let tick = (t: float) => {
List.iter(tickAnimation(t), activeAnimations^);
activeAnimations :=
List.filter(a => !isComplete(t, a), activeAnimations^);
};
Event.subscribe(AnimationTickerImpl.onTick, t =>
tick(Time.to_float_seconds(t))
);
};