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// Copyright (c) 2011, Auerhaus Development, LLC
21//
22// このプログラムはフリーソフトウェアです。適用される法律で許可されている範囲で、いかなる保証もありません。
23// Sam Hocevarによって公開されたDo What The Fuck You Want
24// To Public License, Version 2の条件に基づいて再配布および/または変更することができます。
25// 詳細については、http://sam.zoy.org/wtfpl/COPYING を参照してください。
26
27/// @brief 直線 y = x に基づく
28float Linear(float p) {
29 return p;
30}
31
32/// @brief 放物線 y = x^2 に基づく
33float QuadraticIn(float p) {
34 return p * p;
35}
36
37// @brief 放物線 y = -x^2 + 2x に基づく
38float QuadraticOut(float p) {
39 return -(p * (p - 2.f));
40}
41
42// @brief 区分的二次関数に基づく
43// y = (1/2)((2x)^2) ; [0, 0.5)
44// y = -(1/2)((2x-1)*(2x-3) - 1) ; [0.5, 1]
45float QuadraticInOut(float p) {
46 return p < 0.5f ? 2.f * p * p : (-2.f * p * p) + (4.f * p) - 1.f;
47}
48
49// @brief 三次関数 y = x^3 に基づく
50float CubicIn(float p) {
51 return p * p * p;
52}
53
54// @brief 三次関数 y = (x - 1)^3 + 1 に基づく
55float CubicOut(float p) {
56 const float f = (p - 1.f);
57 return f * f * f + 1.f;
58}
59
60// @brief 区分的三次関数に基づく
61// y = (1/2)((2x)^3) ; [0, 0.5)
62// y = (1/2)((2x-2)^3 + 2) ; [0.5, 1]
63float CubicInOut(float p) {
64 if (p < 0.5f) {
65 return 4.f * p * p * p;
66 }
67 const float f = ((2.f * p) - 2.f);
68 return 0.5f * f * f * f + 1.f;
69}
70
71// @brief 四次関数 x^4 に基づく
72float QuarticIn(float p) {
73 return p * p * p * p;
74}
75
76// @brief 四次関数 y = 1 - (x - 1)^4 に基づく
77float QuarticOut(float p) {
78 const float f = (p - 1.f);
79 return f * f * f * (1.f - p) + 1.f;
80}
81
82// @brief 区分的四次関数に基づく
83// y = (1/2)((2x)^4) ; [0, 0.5)
84// y = -(1/2)((2x-2)^4 - 2) ; [0.5, 1]
85float QuarticInOut(float p) {
86 if (p < 0.5f) {
87 return 8.f * p * p * p * p;
88 }
89 const float f = (p - 1.f);
90 return -8.f * f * f * f * f + 1.f;
91}
92
93// @brief 五次関数 y = x^5 に基づく
94float QuinticIn(float p) {
95 return p * p * p * p * p;
96}
97
98// @brief 五次関数 y = (x - 1)^5 + 1 に基づく
99float QuinticOut(float p) {
100 const float f = (p - 1.f);
101 return f * f * f * f * f + 1.f;
102}
103
104// @brief 区分的五次関数に基づく
105// y = (1/2)((2x)^5) ; [0, 0.5)
106// y = (1/2)((2x-2)^5 + 2) ; [0.5, 1]
107float QuinticInOut(float p) {
108 if (p < 0.5f) {
109 return 16.f * p * p * p * p * p;
110 }
111 const float f = ((2.f * p) - 2.f);
112 return 0.5f * f * f * f * f * f + 1.f;
113}
114
115// @brief サイン波の四半周期に基づく
116float SineIn(float p) {
117 return std::sin((p - 1.f) * kPi2) + 1.f;
118}
119
120// @brief サイン波の四半周期に基づく (異なる位相)
121float SineOut(float p) {
122 return std::sin(p * kPi2);
123}
124
125// @brief 半分のサイン波に基づく
126float SineInOut(float p) {
127 return 0.5f * (1.f - std::cos(p * kPi));
128}
129
130// @brief 単位円の第IV象限をシフトしたものに基づく
131float CircularIn(float p) {
132 return 1.f - std::sqrt(1.f - (p * p));
133}
134
135// @brief 単位円の第II象限をシフトしたものに基づく
136float CircularOut(float p) {
137 return std::sqrt((2.f - p) * p);
138}
139
140// @brief 区分的円関数に基づく
141// y = (1/2)(1 - sqrt(1 - 4x^2)) ; [0, 0.5)
142// y = (1/2)(sqrt(-(2x - 3)*(2x - 1)) + 1) ; [0.5, 1]
143float CircularInOut(float p) {
144 if (p < 0.5f) {
145 return 0.5f * (1.f - std::sqrt(1.f - 4.f * (p * p)));
146 }
147 return 0.5f * (std::sqrt(-((2.f * p) - 3.f) * ((2.f * p) - 1.f)) + 1.f);
148}
149
150// @brief 指数関数 y = 2^(10(x - 1)) に基づく
151float ExponentialIn(float p) {
152 return (p == 0.f) ? p : std::pow(2.f, 10.f * (p - 1.f));
153}
154
155// @brief 指数関数 y = -2^(-10x) + 1 に基づく
156float ExponentialOut(float p) {
157 return (p == 1.f) ? p : 1.f - std::pow(2.f, -10.f * p);
158}
159
160// @brief 区分的指数関数に基づく
161// y = (1/2)2^(10(2x - 1)) ; [0,0.5)
162// y = -(1/2)*2^(-10(2x - 1))) + 1 ; [0.5,1]
163float ExponentialInOut(float p) {
164 if (p == 0.f || p == 1.f) {
165 return p;
166 }
167
168 if (p < 0.5f) {
169 return 0.5f * std::pow(2.f, (20.f * p) - 10.f);
170 }
171 return -0.5f * std::pow(2.f, (-20.f * p) + 10.f) + 1.f;
172}
173
174// @brief 減衰サイン波 y = sin(13pi/2*x)*pow(2, 10 * (x - 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, -10x) + 1 に基づく
180float ElasticOut(float p) {
181 return std::sin(-13.f * kPi2 * (p + 1.f)) * std::pow(2.f, -10.f * p) + 1.f;
182}
183
184// @brief 区分的指数減衰サイン波に基づく:
185// y = (1/2)*sin(13pi/2*(2*x))*pow(2, 10 * ((2*x) - 1)) ; [0,0.5)
186// y = (1/2)*(sin(-13pi/2*((2x-1)+1))*pow(2,-10(2*x-1)) + 2) ; [0.5, 1]
187float ElasticInOut(float p) {
188 if (p < 0.5f) {
189 return 0.5f * std::sin(13.f * kPi2 * (2.f * p)) *
190 std::pow(2.f, 10.f * ((2.f * p) - 1.f));
191 }
192 return 0.5f * (std::sin(-13.f * kPi2 * ((2.f * p - 1.f) + 1.f)) *
193 std::pow(2.f, -10.f * (2.f * p - 1.f)) +
194 2.f);
195}
196
197// @brief オーバーシュートする三次関数 y = x^3-x*sin(x*pi) に基づく
198float BackIn(float p) {
199 return p * p * p - p * std::sin(p * kPi);
200}
201
202// @brief オーバーシュートする三次関数 y = 1-((1-x)^3-(1-x)*sin((1-x)*pi)) に基づく
203float BackOut(float p) {
204 const float f = (1.f - p);
205 return 1.f - (f * f * f - f * std::sin(f * kPi));
206}
207
208// @brief 区分的オーバーシュート三次関数に基づく:
209// y = (1/2)*((2x)^3-(2x)*sin(2*x*pi)) ; [0, 0.5)
210// y = (1/2)*(1-((1-x)^3-(1-x)*sin((1-x)*pi))+1) ; [0.5, 1]
211float BackInOut(float p) {
212 if (p < 0.5f) {
213 const float f = 2.f * p;
214 return 0.5f * (f * f * f - f * std::sin(f * kPi));
215 }
216 const float f = (1.f - (2.f * p - 1.f));
217 return 0.5f * (1.f - (f * f * f - f * std::sin(f * kPi))) + 0.5f;
218}
219
220float BounceIn(float p) {
221 return 1.f - BounceOut(1.f - p);
222}
223
224float BounceOut(float p) {
225 if (p < 4.f / 11.f) {
226 return (121.f * p * p) / 16.f;
227 }
228
229 if (p < 8.f / 11.f) {
230 return (363.f / 40.f * p * p) - (99.f / 10.f * p) + 17.f / 5.f;
231 }
232
233 if (p < 9.f / 10.f) {
234 return (4356.f / 361.f * p * p) - (35442.f / 1805.f * p) + 16061.f / 1805.f;
235 }
236
237 return (54.f / 5.f * p * p) - (513 / 25.f * p) + 268 / 25.f;
238}
239
240float BounceInOut(float p) {
241 if (p < 0.5f) {
242 return 0.5f * BounceIn(p * 2.f);
243 }
244 return 0.5f * BounceOut(p * 2.f - 1.f) + 0.5f;
245}
246
247} // namespace easing
248
250 float to,
251 Duration duration,
252 easing::Function easing_function,
253 Duration delay)
254 : value_(from),
255 from_(*from),
256 to_(to),
257 duration_(duration),
258 easing_function_(std::move(easing_function)),
259 current_(-delay) {
261}
262
264 current_ += params.duration();
265
266 if (current_ >= duration_) {
267 *value_ = to_;
268 return;
269 }
270
271 if (current_ <= Duration()) {
272 *value_ = from_;
273 } else {
274 *value_ = from_ + (to_ - from_) * easing_function_(current_ / duration_);
275 }
276
278}
279
280} // namespace ftxui::animation
281
282// 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:32
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:50
float CubicInOut(float p)
Definition animation.cpp:63
float Linear(float p)
直線 y = x に基づく
Definition animation.cpp:28
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:85
float QuadraticInOut(float p)
Definition animation.cpp:45
float QuarticOut(float p)
Definition animation.cpp:77
float CircularIn(float p)
float ExponentialOut(float p)
float QuadraticOut(float p)
Definition animation.cpp:38
float QuinticOut(float p)
Definition animation.cpp:99
float QuadraticIn(float p)
放物線 y = x^2 に基づく
Definition animation.cpp:33
float ExponentialIn(float p)
float QuinticIn(float p)
Definition animation.cpp:94
float BounceOut(float p)
float CubicOut(float p)
Definition animation.cpp:55
float ElasticOut(float p)
std::function< float(float)> Function
Definition animation.hpp:39
float QuarticIn(float p)
Definition animation.cpp:72
float QuinticInOut(float p)
FTXUIのftxui::animation::名前空間。
Definition animation.hpp:9
std::chrono::duration< float > Duration
Definition animation.hpp:24
std::string value_