FTXUI  4.1.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
animation.cpp
Go to the documentation of this file.
1#include <cmath> // for sin, pow, sqrt, cos
2#include <ratio> // for ratio
3#include <utility> // for move
4
6
7namespace ftxui::animation {
8
9namespace easing {
10
11namespace {
12constexpr float kPi = 3.14159265358979323846f;
13constexpr float kPi2 = kPi / 2.f;
14} // namespace
15
16// Easing function have been taken out of:
17// https://github.com/warrenm/AHEasing/blob/master/AHEasing/easing.c
18//
19// Corresponding license:
20// Copyright (c) 2011, Auerhaus Development, LLC
21//
22// This program is free software. It comes without any warranty, to
23// the extent permitted by applicable law. You can redistribute it
24// and/or modify it under the terms of the Do What The Fuck You Want
25// To Public License, Version 2, as published by Sam Hocevar. See
26// http://sam.zoy.org/wtfpl/COPYING for more details.
27
28// Modeled after the line y = x
29float Linear(float p) {
30 return p;
31}
32
33// Modeled after the parabola y = x^2
34float QuadraticIn(float p) {
35 return p * p;
36}
37
38// Modeled after the parabola y = -x^2 + 2x
39float QuadraticOut(float p) {
40 return -(p * (p - 2.f));
41}
42
43// Modeled after the piecewise quadratic
44// y = (1/2)((2x)^2) ; [0, 0.5)
45// y = -(1/2)((2x-1)*(2x-3) - 1) ; [0.5, 1]
46float QuadraticInOut(float p) {
47 return p < 0.5f // NOLINT
48 ? 2.f * p * p // NOLINT
49 : (-2.f * p * p) + (4.f * p) - 1.f; // NOLINT
50}
51
52// Modeled after the cubic y = x^3
53float CubicIn(float p) {
54 return p * p * p;
55}
56
57// Modeled after the cubic y = (x - 1)^3 + 1
58float CubicOut(float p) {
59 const float f = (p - 1.f);
60 return f * f * f + 1.f;
61}
62
63// Modeled after the piecewise cubic
64// y = (1/2)((2x)^3) ; [0, 0.5)
65// y = (1/2)((2x-2)^3 + 2) ; [0.5, 1]
66float CubicInOut(float p) {
67 if (p < 0.5f) { // NOLINT
68 return 4.f * p * p * p;
69 }
70 const float f = ((2.f * p) - 2.f);
71 return 0.5f * f * f * f + 1.f; // NOLINT
72}
73
74// Modeled after the quartic x^4
75float QuarticIn(float p) {
76 return p * p * p * p;
77}
78
79// Modeled after the quartic y = 1 - (x - 1)^4
80float QuarticOut(float p) {
81 const float f = (p - 1.f);
82 return f * f * f * (1.f - p) + 1.f;
83}
84
85// Modeled after the piecewise quartic
86// y = (1/2)((2x)^4) ; [0, 0.5)
87// y = -(1/2)((2x-2)^4 - 2) ; [0.5, 1]
88float QuarticInOut(float p) {
89 if (p < 0.5f) { // NOLINT
90 return 8.f * p * p * p * p; // NOLINT
91 }
92 const float f = (p - 1.f);
93 return -8.f * f * f * f * f + 1.f; // NOLINT
94}
95
96// Modeled after the quintic y = x^5
97float QuinticIn(float p) {
98 return p * p * p * p * p;
99}
100
101// Modeled after the quintic y = (x - 1)^5 + 1
102float QuinticOut(float p) {
103 const float f = (p - 1.f);
104 return f * f * f * f * f + 1.f;
105}
106
107// Modeled after the piecewise quintic
108// y = (1/2)((2x)^5) ; [0, 0.5)
109// y = (1/2)((2x-2)^5 + 2) ; [0.5, 1]
110float QuinticInOut(float p) {
111 if (p < 0.5f) { // NOLINT
112 return 16.f * p * p * p * p * p; // NOLINT
113 }
114 float f = ((2.f * p) - 2.f); // NOLINT
115 return 0.5f * f * f * f * f * f + 1.f; // NOLINT
116}
117
118// Modeled after quarter-cycle of sine wave
119float SineIn(float p) {
120 return std::sin((p - 1.f) * kPi2) + 1.f;
121}
122
123// Modeled after quarter-cycle of sine wave (different phase)
124float SineOut(float p) {
125 return std::sin(p * kPi2);
126}
127
128// Modeled after half sine wave
129float SineInOut(float p) {
130 return 0.5f * (1.f - std::cos(p * kPi)); // NOLINT
131}
132
133// Modeled after shifted quadrant IV of unit circle
134float CircularIn(float p) {
135 return 1.f - std::sqrt(1.f - (p * p));
136}
137
138// Modeled after shifted quadrant II of unit circle
139float CircularOut(float p) {
140 return std::sqrt((2.f - p) * p);
141}
142
143// Modeled after the piecewise circular function
144// y = (1/2)(1 - sqrt(1 - 4x^2)) ; [0, 0.5)
145// y = (1/2)(sqrt(-(2x - 3)*(2x - 1)) + 1) ; [0.5, 1]
146float CircularInOut(float p) {
147 if (p < 0.5f) { // NOLINT
148 return 0.5f * (1.f - std::sqrt(1.f - 4.f * (p * p))); // NOLINT
149 }
150 // NOLINTNEXTLINE
151 return 0.5f * (std::sqrt(-((2.f * p) - 3.f) * ((2.f * p) - 1.f)) + 1.f);
152}
153
154// Modeled after the exponential function y = 2^(10(x - 1))
155float ExponentialIn(float p) {
156 return (p == 0.f) ? p : std::pow(2.f, 10.f * (p - 1.f)); // NOLINT
157}
158
159// Modeled after the exponential function y = -2^(-10x) + 1
160float ExponentialOut(float p) {
161 return (p == 1.f) ? p : 1.f - std::pow(2.f, -10.f * p); // NOLINT
162}
163
164// Modeled after the piecewise exponential
165// y = (1/2)2^(10(2x - 1)) ; [0,0.5)
166// y = -(1/2)*2^(-10(2x - 1))) + 1 ; [0.5,1]
167float ExponentialInOut(float p) {
168 if (p == 0.f || p == 1.f) {
169 return p;
170 }
171
172 if (p < 0.5f) { // NOLINT
173 return 0.5f * std::pow(2.f, (20.f * p) - 10.f); // NOLINT
174 }
175 return -0.5f * std::pow(2.f, (-20.f * p) + 10.f) + 1.f; // NOLINT
176}
177
178// Modeled after the damped sine wave y = sin(13pi/2*x)*pow(2, 10 * (x - 1))
179float ElasticIn(float p) {
180 return std::sin(13.f * kPi2 * p) * std::pow(2.f, 10.f * (p - 1.f)); // NOLINT
181}
182
183// Modeled after the damped sine wave y = sin(-13pi/2*(x + 1))*pow(2, -10x) +
184// 1
185float ElasticOut(float p) {
186 // NOLINTNEXTLINE
187 return std::sin(-13.f * kPi2 * (p + 1.f)) * std::pow(2.f, -10.f * p) + 1.f;
188}
189
190// Modeled after the piecewise exponentially-damped sine wave:
191// y = (1/2)*sin(13pi/2*(2*x))*pow(2, 10 * ((2*x) - 1)) ; [0,0.5)
192// y = (1/2)*(sin(-13pi/2*((2x-1)+1))*pow(2,-10(2*x-1)) + 2) ; [0.5, 1]
193float ElasticInOut(float p) {
194 if (p < 0.5f) { // NOLINT
195 return 0.5f * std::sin(13.f * kPi2 * (2.f * p)) * // NOLINT
196 std::pow(2.f, 10.f * ((2.f * p) - 1.f)); // NOLINT
197 }
198 return 0.5f * (std::sin(-13.f * kPi2 * ((2.f * p - 1.f) + 1.f)) * // NOLINT
199 std::pow(2.f, -10.f * (2.f * p - 1.f)) + // NOLINT
200 2.f); // NOLINT
201}
202
203// Modeled after the overshooting cubic y = x^3-x*sin(x*pi)
204float BackIn(float p) {
205 return p * p * p - p * std::sin(p * kPi);
206}
207
208// Modeled after overshooting cubic y = 1-((1-x)^3-(1-x)*sin((1-x)*pi))
209float BackOut(float p) {
210 const float f = (1.f - p);
211 return 1.f - (f * f * f - f * std::sin(f * kPi));
212}
213
214// Modeled after the piecewise overshooting cubic function:
215// y = (1/2)*((2x)^3-(2x)*sin(2*x*pi)) ; [0, 0.5)
216// y = (1/2)*(1-((1-x)^3-(1-x)*sin((1-x)*pi))+1) ; [0.5, 1]
217float BackInOut(float p) {
218 if (p < 0.5f) { // NOLINT
219 const float f = 2.f * p;
220 return 0.5f * (f * f * f - f * std::sin(f * kPi)); // NOLINT
221 }
222 const float f = (1.f - (2.f * p - 1.f)); // NOLINT
223 return 0.5f * (1.f - (f * f * f - f * std::sin(f * kPi))) + 0.5f; // NOLINT
224}
225
226float BounceIn(float p) {
227 return 1.f - BounceOut(1.f - p);
228}
229
230float BounceOut(float p) {
231 if (p < 4.f / 11.f) { // NOLINT
232 return (121.f * p * p) / 16.f; // NOLINT
233 }
234
235 if (p < 8.f / 11.f) { // NOLINT
236 return (363.f / 40.f * p * p) - (99.f / 10.f * p) + 17.f / 5.f; // NOLINT
237 }
238
239 if (p < 9.f / 10.f) { // NOLINT
240 return (4356.f / 361.f * p * p) - (35442.f / 1805.f * p) + // NOLINT
241 16061.f / 1805.f; // NOLINT
242 }
243
244 return (54.f / 5.f * p * p) - (513 / 25.f * p) + 268 / 25.f; // NOLINT
245}
246
247float BounceInOut(float p) { // NOLINT
248 if (p < 0.5f) { // NOLINT
249 return 0.5f * BounceIn(p * 2.f); // NOLINT
250 }
251 return 0.5f * BounceOut(p * 2.f - 1.f) + 0.5f; // NOLINT
252}
253
254} // namespace easing
255
257 float to,
258 Duration duration,
259 easing::Function easing_function,
260 Duration delay)
261 : value_(from),
262 from_(*from),
263 to_(to),
264 duration_(duration),
265 easing_function_(std::move(easing_function)),
266 current_(-delay) {
268}
269
271 current_ += params.duration();
272
273 if (current_ >= duration_) {
274 *value_ = to_;
275 return;
276 }
277
278 if (current_ <= Duration()) {
279 *value_ = from_;
280 } else {
281 *value_ = from_ +
282 (to_ - from_) * easing_function_(current_ / duration_); // NOLINT
283 }
284
286}
287
288} // namespace ftxui::animation
Animator(float *from, float to=0.f, Duration duration=std::chrono::milliseconds(250), easing::Function easing_function=easing::Linear, Duration delay=std::chrono::milliseconds(0))
Duration duration() const
The duration this animation step represents.
Definition animation.hpp:29
float ElasticIn(float p)
float CircularInOut(float p)
float SineInOut(float p)
float BounceInOut(float p)
float CubicIn(float p)
Definition animation.cpp:53
float CubicInOut(float p)
Definition animation.cpp:66
float Linear(float p)
Definition animation.cpp:29
float BounceIn(float p)
float CircularOut(float p)
float BackInOut(float p)
float ExponentialInOut(float p)
float ElasticInOut(float p)
float QuarticInOut(float p)
Definition animation.cpp:88
float QuadraticInOut(float p)
Definition animation.cpp:46
float QuarticOut(float p)
Definition animation.cpp:80
float CircularIn(float p)
float ExponentialOut(float p)
float QuadraticOut(float p)
Definition animation.cpp:39
float QuinticOut(float p)
float QuadraticIn(float p)
Definition animation.cpp:34
float ExponentialIn(float p)
float QuinticIn(float p)
Definition animation.cpp:97
float BounceOut(float p)
float CubicOut(float p)
Definition animation.cpp:58
float ElasticOut(float p)
std::function< float(float)> Function
Definition animation.hpp:36
float QuarticIn(float p)
Definition animation.cpp:75
float QuinticInOut(float p)
std::chrono::duration< float > Duration
Definition animation.hpp:21