FTXUI 6.1.9
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 <utility> // for move
3
5
6// NOLINTBEGIN(*-magic-numbers)
7namespace ftxui::animation {
8
9namespace easing {
10
11namespace {
12constexpr float kPi = 3.14159265358979323846f;
13constexpr float kPi2 = kPi / 2.f;
14} // namespace
15
16// Las funciones de aceleración han sido tomadas de:
17// https://github.com/warrenm/AHEasing/blob/master/AHEasing/easing.c
18//
19// Licencia correspondiente:
20// Copyright (c) 2011, Auerhaus Development, LLC
21//
22// Este programa es software libre. Se proporciona sin ninguna garantía, en
23// la medida permitida por la ley aplicable. Puedes redistribuirlo
24// y/o modificarlo bajo los términos de la Licencia Pública Do What The Fuck You Want
25// To, Versión 2, publicada por Sam Hocevar. Consulta
26// http://sam.zoy.org/wtfpl/COPYING para más detalles.
27
28/// @brief Modelado según la línea y = x
29float Linear(float p) {
30 return p;
31}
32
33/// @brief Modelado según la parábola y = x^2
34float QuadraticIn(float p) {
35 return p * p;
36}
37
38// @brief Modelado según la parábola y = -x^2 + 2x
39float QuadraticOut(float p) {
40 return -(p * (p - 2.f));
41}
42
43// @brief Modelado según la cuadrática a trozos
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 ? 2.f * p * p : (-2.f * p * p) + (4.f * p) - 1.f;
48}
49
50// @brief Modelado según la cúbica y = x^3
51float CubicIn(float p) {
52 return p * p * p;
53}
54
55// @brief Modelado según la cúbica y = (x - 1)^3 + 1
56float CubicOut(float p) {
57 const float f = (p - 1.f);
58 return f * f * f + 1.f;
59}
60
61// @brief Modelado según la cúbica a trozos
62// y = (1/2)((2x)^3) ; [0, 0.5)
63// y = (1/2)((2x-2)^3 + 2) ; [0.5, 1]
64float CubicInOut(float p) {
65 if (p < 0.5f) {
66 return 4.f * p * p * p;
67 }
68 const float f = ((2.f * p) - 2.f);
69 return 0.5f * f * f * f + 1.f;
70}
71
72// @brief Modelado según la cuártica x^4
73float QuarticIn(float p) {
74 return p * p * p * p;
75}
76
77// @brief Modelado según la cuártica y = 1 - (x - 1)^4
78float QuarticOut(float p) {
79 const float f = (p - 1.f);
80 return f * f * f * (1.f - p) + 1.f;
81}
82
83// @brief Modelado según la cuártica a trozos
84// y = (1/2)((2x)^4) ; [0, 0.5)
85// y = -(1/2)((2x-2)^4 - 2) ; [0.5, 1]
86float QuarticInOut(float p) {
87 if (p < 0.5f) {
88 return 8.f * p * p * p * p;
89 }
90 const float f = (p - 1.f);
91 return -8.f * f * f * f * f + 1.f;
92}
93
94// @brief Modelado según la quíntica y = x^5
95float QuinticIn(float p) {
96 return p * p * p * p * p;
97}
98
99// @brief Modelado según la quíntica y = (x - 1)^5 + 1
100float QuinticOut(float p) {
101 const float f = (p - 1.f);
102 return f * f * f * f * f + 1.f;
103}
104
105// @brief Modelado según la quíntica a trozos
106// y = (1/2)((2x)^5) ; [0, 0.5)
107// y = (1/2)((2x-2)^5 + 2) ; [0.5, 1]
108float QuinticInOut(float p) {
109 if (p < 0.5f) {
110 return 16.f * p * p * p * p * p;
111 }
112 const float f = ((2.f * p) - 2.f);
113 return 0.5f * f * f * f * f * f + 1.f;
114}
115
116// @brief Modelado según el cuarto de ciclo de onda sinusoidal
117float SineIn(float p) {
118 return std::sin((p - 1.f) * kPi2) + 1.f;
119}
120
121// @brief Modelado según el cuarto de ciclo de onda sinusoidal (fase diferente)
122float SineOut(float p) {
123 return std::sin(p * kPi2);
124}
125
126// @brief Modelado según la media onda sinusoidal
127float SineInOut(float p) {
128 return 0.5f * (1.f - std::cos(p * kPi));
129}
130
131// @brief Modelado según el cuadrante IV desplazado del círculo unitario
132float CircularIn(float p) {
133 return 1.f - std::sqrt(1.f - (p * p));
134}
135
136// @brief Modelado según el cuadrante II desplazado del círculo unitario
137float CircularOut(float p) {
138 return std::sqrt((2.f - p) * p);
139}
140
141// @brief Modelado según la función circular a trozos
142// y = (1/2)(1 - sqrt(1 - 4x^2)) ; [0, 0.5)
143// y = (1/2)(sqrt(-(2x - 3)*(2x - 1)) + 1) ; [0.5, 1]
144float CircularInOut(float p) {
145 if (p < 0.5f) {
146 return 0.5f * (1.f - std::sqrt(1.f - 4.f * (p * p)));
147 }
148 return 0.5f * (std::sqrt(-((2.f * p) - 3.f) * ((2.f * p) - 1.f)) + 1.f);
149}
150
151// @brief Modelado según la función exponencial y = 2^(10(x - 1))
152float ExponentialIn(float p) {
153 return (p == 0.f) ? p : std::pow(2.f, 10.f * (p - 1.f));
154}
155
156// @brief Modelado según la función exponencial y = -2^(-10x) + 1
157float ExponentialOut(float p) {
158 return (p == 1.f) ? p : 1.f - std::pow(2.f, -10.f * p);
159}
160
161// @brief Modelado según la exponencial a trozos
162// y = (1/2)2^(10(2x - 1)) ; [0,0.5)
163// y = -(1/2)*2^(-10(2x - 1))) + 1 ; [0.5,1]
164float ExponentialInOut(float p) {
165 if (p == 0.f || p == 1.f) {
166 return p;
167 }
168
169 if (p < 0.5f) {
170 return 0.5f * std::pow(2.f, (20.f * p) - 10.f);
171 }
172 return -0.5f * std::pow(2.f, (-20.f * p) + 10.f) + 1.f;
173}
174
175// @brief Modelado según la onda sinusoidal amortiguada y = sin(13pi/2*x)*pow(2, 10 * (x - 1))
176float ElasticIn(float p) {
177 return std::sin(13.f * kPi2 * p) * std::pow(2.f, 10.f * (p - 1.f));
178}
179
180// @brief Modelado según la onda sinusoidal amortiguada y = sin(-13pi/2*(x + 1))*pow(2,
181// -10x) +
182// 1
183float ElasticOut(float p) {
184 return std::sin(-13.f * kPi2 * (p + 1.f)) * std::pow(2.f, -10.f * p) + 1.f;
185}
186
187// @brief Modelado según la onda sinusoidal amortiguada exponencialmente a trozos:
188// y = (1/2)*sin(13pi/2*(2*x))*pow(2, 10 * ((2*x) - 1)) ; [0,0.5)
189// y = (1/2)*(sin(-13pi/2*((2x-1)+1))*pow(2,-10(2*x-1)) + 2) ; [0.5, 1]
190float ElasticInOut(float p) {
191 if (p < 0.5f) {
192 return 0.5f * std::sin(13.f * kPi2 * (2.f * p)) *
193 std::pow(2.f, 10.f * ((2.f * p) - 1.f));
194 }
195 return 0.5f * (std::sin(-13.f * kPi2 * ((2.f * p - 1.f) + 1.f)) *
196 std::pow(2.f, -10.f * (2.f * p - 1.f)) +
197 2.f);
198}
199
200// @brief Modelado según la cúbica con sobrepaso y = x^3-x*sin(x*pi)
201float BackIn(float p) {
202 return p * p * p - p * std::sin(p * kPi);
203}
204
205// @brief Modelado según la cúbica con sobrepaso y = 1-((1-x)^3-(1-x)*sin((1-x)*pi))
206float BackOut(float p) {
207 const float f = (1.f - p);
208 return 1.f - (f * f * f - f * std::sin(f * kPi));
209}
210
211// @brief Modelado según la función cúbica con sobrepaso a trozos:
212// y = (1/2)*((2x)^3-(2x)*sin(2*x*pi)) ; [0, 0.5)
213// y = (1/2)*(1-((1-x)^3-(1-x)*sin((1-x)*pi))+1) ; [0.5, 1]
214float BackInOut(float p) {
215 if (p < 0.5f) {
216 const float f = 2.f * p;
217 return 0.5f * (f * f * f - f * std::sin(f * kPi));
218 }
219 const float f = (1.f - (2.f * p - 1.f));
220 return 0.5f * (1.f - (f * f * f - f * std::sin(f * kPi))) + 0.5f;
221}
222
223float BounceIn(float p) {
224 return 1.f - BounceOut(1.f - p);
225}
226
227float BounceOut(float p) {
228 if (p < 4.f / 11.f) {
229 return (121.f * p * p) / 16.f;
230 }
231
232 if (p < 8.f / 11.f) {
233 return (363.f / 40.f * p * p) - (99.f / 10.f * p) + 17.f / 5.f;
234 }
235
236 if (p < 9.f / 10.f) {
237 return (4356.f / 361.f * p * p) - (35442.f / 1805.f * p) + 16061.f / 1805.f;
238 }
239
240 return (54.f / 5.f * p * p) - (513 / 25.f * p) + 268 / 25.f;
241}
242
243float BounceInOut(float p) {
244 if (p < 0.5f) {
245 return 0.5f * BounceIn(p * 2.f);
246 }
247 return 0.5f * BounceOut(p * 2.f - 1.f) + 0.5f;
248}
249
250} // namespace easing
251
253 float to,
254 Duration duration,
255 easing::Function easing_function,
256 Duration delay)
257 : value_(from),
258 from_(*from),
259 to_(to),
260 duration_(duration),
261 easing_function_(std::move(easing_function)),
262 current_(-delay) {
264}
265
267 current_ += params.duration();
268
269 if (current_ >= duration_) {
270 *value_ = to_;
271 return;
272 }
273
274 if (current_ <= Duration()) {
275 *value_ = from_;
276 } else {
277 *value_ = from_ + (to_ - from_) * easing_function_(current_ / duration_);
278 }
279
281}
282
283} // namespace ftxui::animation
284
285// NOLINTEND(*-magic-numbers)
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
La duración que representa este paso de animación.
Definition animation.hpp:37
void RequestAnimationFrame()
RequestAnimationFrame es una función que solicita que se dibuje un nuevo fotograma en el siguiente ci...
El espacio de nombres ftxui::animation::easing:: de FTXUI.
float ElasticIn(float p)
float CircularInOut(float p)
float SineInOut(float p)
float BounceInOut(float p)
float CubicIn(float p)
Definition animation.cpp:51
float CubicInOut(float p)
Definition animation.cpp:64
float Linear(float p)
Modelado según la línea y = x.
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:86
float QuadraticInOut(float p)
Definition animation.cpp:46
float QuarticOut(float p)
Definition animation.cpp:78
float CircularIn(float p)
float ExponentialOut(float p)
float QuadraticOut(float p)
Definition animation.cpp:39
float QuinticOut(float p)
float QuadraticIn(float p)
Modelado según la parábola y = x^2.
Definition animation.cpp:34
float ExponentialIn(float p)
float QuinticIn(float p)
Definition animation.cpp:95
float BounceOut(float p)
float CubicOut(float p)
Definition animation.cpp:56
float ElasticOut(float p)
std::function< float(float)> Function
Definition animation.hpp:44
float QuarticIn(float p)
Definition animation.cpp:73
float QuinticInOut(float p)
El espacio de nombres ftxui::animation:: de FTXUI.
Definition animation.hpp:10
std::chrono::duration< float > Duration
Definition animation.hpp:29
std::string value_