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