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// 緩動函數取自:
17// https://github.com/warrenm/AHEasing/blob/master/AHEasing/easing.c
18//
19// 相應的許可證:
20// 版權所有 (c) 2011, Auerhaus Development, LLC
21//
22// 本程式為自由軟體,不附帶任何形式的保證,在適用法律允許的最大範圍內。您可以根據 Sam Hocevar 發布的 Do What The Fuck You Want
23// To Public License, Version 2 的條款重新分發和/或修改它。詳情請參閱
24// http://sam.zoy.org/wtfpl/COPYING。
25
26/// @brief 模仿直線 y = x
27float Linear(float p) {
28 return p;
29}
30
31/// @brief 模仿拋物線 y = x^2
32float QuadraticIn(float p) {
33 return p * p;
34}
35
36// @brief 模仿拋物線 y = -x^2 + 2x
37float QuadraticOut(float p) {
38 return -(p * (p - 2.f));
39}
40
41// @brief 模仿分段二次函數
42// y = (1/2)((2x)^2) ; [0, 0.5)
43// y = -(1/2)((2x-1)*(2x-3) - 1) ; [0.5, 1]
44float QuadraticInOut(float p) {
45 return p < 0.5f ? 2.f * p * p : (-2.f * p * p) + (4.f * p) - 1.f;
46}
47
48// @brief 模仿三次函數 y = x^3
49float CubicIn(float p) {
50 return p * p * p;
51}
52
53// @brief 模仿三次函數 y = (x - 1)^3 + 1
54float CubicOut(float p) {
55 const float f = (p - 1.f);
56 return f * f * f + 1.f;
57}
58
59// @brief 模仿分段三次函數
60// y = (1/2)((2x)^3) ; [0, 0.5)
61// y = (1/2)((2x-2)^3 + 2) ; [0.5, 1]
62float CubicInOut(float p) {
63 if (p < 0.5f) {
64 return 4.f * p * p * p;
65 }
66 const float f = ((2.f * p) - 2.f);
67 return 0.5f * f * f * f + 1.f;
68}
69
70// @brief 模仿四次函數 x^4
71float QuarticIn(float p) {
72 return p * p * p * p;
73}
74
75// @brief 模仿四次函數 y = 1 - (x - 1)^4
76float QuarticOut(float p) {
77 const float f = (p - 1.f);
78 return f * f * f * (1.f - p) + 1.f;
79}
80
81// @brief 模仿分段四次函數
82// y = (1/2)((2x)^4) ; [0, 0.5)
83// y = -(1/2)((2x-2)^4 - 2) ; [0.5, 1]
84float QuarticInOut(float p) {
85 if (p < 0.5f) {
86 return 8.f * p * p * p * p;
87 }
88 const float f = (p - 1.f);
89 return -8.f * f * f * f * f + 1.f;
90}
91
92// @brief 模仿五次函數 y = x^5
93float QuinticIn(float p) {
94 return p * p * p * p * p;
95}
96
97// @brief 模仿五次函數 y = (x - 1)^5 + 1
98float QuinticOut(float p) {
99 const float f = (p - 1.f);
100 return f * f * f * f * f + 1.f;
101}
102
103// @brief 模仿分段五次函數
104// y = (1/2)((2x)^5) ; [0, 0.5)
105// y = (1/2)((2x-2)^5 + 2) ; [0.5, 1]
106float QuinticInOut(float p) {
107 if (p < 0.5f) {
108 return 16.f * p * p * p * p * p;
109 }
110 const float f = ((2.f * p) - 2.f);
111 return 0.5f * f * f * f * f * f + 1.f;
112}
113
114// @brief 模仿四分之一周期的正弦波
115float SineIn(float p) {
116 return std::sin((p - 1.f) * kPi2) + 1.f;
117}
118
119// @brief 模仿四分之一周期的正弦波 (不同相位)
120float SineOut(float p) {
121 return std::sin(p * kPi2);
122}
123
124// @brief 模仿半個正弦波
125float SineInOut(float p) {
126 return 0.5f * (1.f - std::cos(p * kPi));
127}
128
129// @brief 模仿單位圓的第四象限偏移
130float CircularIn(float p) {
131 return 1.f - std::sqrt(1.f - (p * p));
132}
133
134// @brief 模仿單位圓的第二象限偏移
135float CircularOut(float p) {
136 return std::sqrt((2.f - p) * p);
137}
138
139// @brief 模仿分段圓形函數
140// y = (1/2)(1 - sqrt(1 - 4x^2)) ; [0, 0.5)
141// y = (1/2)(sqrt(-(2x - 3)*(2x - 1)) + 1) ; [0.5, 1]
142float CircularInOut(float p) {
143 if (p < 0.5f) {
144 return 0.5f * (1.f - std::sqrt(1.f - 4.f * (p * p)));
145 }
146 return 0.5f * (std::sqrt(-((2.f * p) - 3.f) * ((2.f * p) - 1.f)) + 1.f);
147}
148
149// @brief 模仿指數函數 y = 2^(10(x - 1))
150float ExponentialIn(float p) {
151 return (p == 0.f) ? p : std::pow(2.f, 10.f * (p - 1.f));
152}
153
154// @brief 模仿指數函數 y = -2^(-10x) + 1
155float ExponentialOut(float p) {
156 return (p == 1.f) ? p : 1.f - std::pow(2.f, -10.f * p);
157}
158
159// @brief 模仿分段指數函數
160// y = (1/2)2^(10(2x - 1)) ; [0,0.5)
161// y = -(1/2)*2^(-10(2x - 1))) + 1 ; [0.5,1]
162float ExponentialInOut(float p) {
163 if (p == 0.f || p == 1.f) {
164 return p;
165 }
166
167 if (p < 0.5f) {
168 return 0.5f * std::pow(2.f, (20.f * p) - 10.f);
169 }
170 return -0.5f * std::pow(2.f, (-20.f * p) + 10.f) + 1.f;
171}
172
173// @brief 模仿阻尼正弦波 y = sin(13pi/2*x)*pow(2, 10 * (x -
174// 1))
175float ElasticIn(float p) {
176 return std::sin(13.f * kPi2 * p) * std::pow(2.f, 10.f * (p - 1.f));
177}
178
179// @brief 模仿阻尼正弦波 y = sin(-13pi/2*(x + 1))*pow(2,
180// -10x) +
181// 1
182float ElasticOut(float p) {
183 return std::sin(-13.f * kPi2 * (p + 1.f)) * std::pow(2.f, -10.f * p) + 1.f;
184}
185
186// @brief 模仿分段指數阻尼正弦波:
187// y = (1/2)*sin(13pi/2*(2*x))*pow(2, 10 * ((2*x) - 1)) ; [0,0.5)
188// y = (1/2)*(sin(-13pi/2*((2x-1)+1))*pow(2,-10(2*x-1)) + 2) ; [0.5, 1]
189float ElasticInOut(float p) {
190 if (p < 0.5f) {
191 return 0.5f * std::sin(13.f * kPi2 * (2.f * p)) *
192 std::pow(2.f, 10.f * ((2.f * p) - 1.f));
193 }
194 return 0.5f * (std::sin(-13.f * kPi2 * ((2.f * p - 1.f) + 1.f)) *
195 std::pow(2.f, -10.f * (2.f * p - 1.f)) +
196 2.f);
197}
198
199// @brief 模仿過沖三次函數 y = x^3-x*sin(x*pi)
200float BackIn(float p) {
201 return p * p * p - p * std::sin(p * kPi);
202}
203
204// @brief 模仿過沖三次函數 y = 1-((1-x)^3-(1-x)*sin((1-x)*pi))
205float BackOut(float p) {
206 const float f = (1.f - p);
207 return 1.f - (f * f * f - f * std::sin(f * kPi));
208}
209
210// @brief 模仿分段過沖三次函數:
211// y = (1/2)*((2x)^3-(2x)*sin(2*x*pi)) ; [0, 0.5)
212// y = (1/2)*(1-((1-x)^3-(1-x)*sin((1-x)*pi))+1) ; [0.5, 1]
213float BackInOut(float p) {
214 if (p < 0.5f) {
215 const float f = 2.f * p;
216 return 0.5f * (f * f * f - f * std::sin(f * kPi));
217 }
218 const float f = (1.f - (2.f * p - 1.f));
219 return 0.5f * (1.f - (f * f * f - f * std::sin(f * kPi))) + 0.5f;
220}
221
222float BounceIn(float p) {
223 return 1.f - BounceOut(1.f - p);
224}
225
226float BounceOut(float p) {
227 if (p < 4.f / 11.f) {
228 return (121.f * p * p) / 16.f;
229 }
230
231 if (p < 8.f / 11.f) {
232 return (363.f / 40.f * p * p) - (99.f / 10.f * p) + 17.f / 5.f;
233 }
234
235 if (p < 9.f / 10.f) {
236 return (4356.f / 361.f * p * p) - (35442.f / 1805.f * p) + 16061.f / 1805.f;
237 }
238
239 return (54.f / 5.f * p * p) - (513 / 25.f * p) + 268 / 25.f;
240}
241
242float BounceInOut(float p) {
243 if (p < 0.5f) {
244 return 0.5f * BounceIn(p * 2.f);
245 }
246 return 0.5f * BounceOut(p * 2.f - 1.f) + 0.5f;
247}
248
249} // namespace easing
250
252 float to,
253 Duration duration,
254 easing::Function easing_function,
255 Duration delay)
256 : value_(from),
257 from_(*from),
258 to_(to),
259 duration_(duration),
260 easing_function_(std::move(easing_function)),
261 current_(-delay) {
263}
264
266 current_ += params.duration();
267
268 if (current_ >= duration_) {
269 *value_ = to_;
270 return;
271 }
272
273 if (current_ <= Duration()) {
274 *value_ = from_;
275 } else {
276 *value_ = from_ + (to_ - from_) * easing_function_(current_ / duration_);
277 }
278
280}
281
282} // namespace ftxui::animation
283
284// 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
此動畫步驟表示的持續時間。
Definition animation.hpp:33
void RequestAnimationFrame()
RequestAnimationFrame 是一個函式,它請求在下一個動畫週期中繪製新畫面。
FTXUI ftxui::animation::easing:: 命名空間
float ElasticIn(float p)
float CircularInOut(float p)
float SineInOut(float p)
float BounceInOut(float p)
float CubicIn(float p)
Definition animation.cpp:49
float CubicInOut(float p)
Definition animation.cpp:62
float Linear(float p)
模仿直線 y = x
Definition animation.cpp:27
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:84
float QuadraticInOut(float p)
Definition animation.cpp:44
float QuarticOut(float p)
Definition animation.cpp:76
float CircularIn(float p)
float ExponentialOut(float p)
float QuadraticOut(float p)
Definition animation.cpp:37
float QuinticOut(float p)
Definition animation.cpp:98
float QuadraticIn(float p)
模仿拋物線 y = x^2
Definition animation.cpp:32
float ExponentialIn(float p)
float QuinticIn(float p)
Definition animation.cpp:93
float BounceOut(float p)
float CubicOut(float p)
Definition animation.cpp:54
float ElasticOut(float p)
std::function< float(float)> Function
Definition animation.hpp:40
float QuarticIn(float p)
Definition animation.cpp:71
float QuinticInOut(float p)
FTXUI ftxui::animation:: 命名空間
Definition animation.hpp:10
std::chrono::duration< float > Duration
Definition animation.hpp:25
std::string value_