EasingStyleValue.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. * Copyright (c) 2023, Ali Mohammad Pur <mpfard@serenityos.org>
  7. *
  8. * SPDX-License-Identifier: BSD-2-Clause
  9. */
  10. #include "EasingStyleValue.h"
  11. #include <AK/BinarySearch.h>
  12. #include <AK/StringBuilder.h>
  13. namespace Web::CSS {
  14. // NOTE: Magic cubic bezier values from https://www.w3.org/TR/css-easing-1/#valdef-cubic-bezier-easing-function-ease
  15. EasingStyleValue::CubicBezier EasingStyleValue::CubicBezier::ease()
  16. {
  17. static CubicBezier bezier { 0.25, 0.1, 0.25, 1.0 };
  18. return bezier;
  19. }
  20. EasingStyleValue::CubicBezier EasingStyleValue::CubicBezier::ease_in()
  21. {
  22. static CubicBezier bezier { 0.42, 0.0, 1.0, 1.0 };
  23. return bezier;
  24. }
  25. EasingStyleValue::CubicBezier EasingStyleValue::CubicBezier::ease_out()
  26. {
  27. static CubicBezier bezier { 0.0, 0.0, 0.58, 1.0 };
  28. return bezier;
  29. }
  30. EasingStyleValue::CubicBezier EasingStyleValue::CubicBezier::ease_in_out()
  31. {
  32. static CubicBezier bezier { 0.42, 0.0, 0.58, 1.0 };
  33. return bezier;
  34. }
  35. EasingStyleValue::Steps EasingStyleValue::Steps::step_start() {
  36. static Steps steps { 1, Steps::Position::Start };
  37. return steps;
  38. }
  39. EasingStyleValue::Steps EasingStyleValue::Steps::step_end() {
  40. static Steps steps { 1, Steps::Position::End };
  41. return steps;
  42. }
  43. bool EasingStyleValue::CubicBezier::operator==(Web::CSS::EasingStyleValue::CubicBezier const& other) const
  44. {
  45. return x1 == other.x1 && y1 == other.y1 && x2 == other.x2 && y2 == other.y2;
  46. }
  47. double EasingStyleValue::Function::evaluate_at(double input_progress, bool before_flag) const
  48. {
  49. constexpr static auto cubic_bezier_at = [](double x1, double x2, double t)
  50. {
  51. auto a = 1.0 - 3.0 * x2 + 3.0 * x1;
  52. auto b = 3.0 * x2 - 6.0 * x1;
  53. auto c = 3.0 * x1;
  54. auto t2 = t * t;
  55. auto t3 = t2 * t;
  56. return (a * t3) + (b * t2) + (c * t);
  57. };
  58. return visit(
  59. [&](Linear const&) { return input_progress; },
  60. [&](CubicBezier const& bezier) {
  61. auto const& [x1, y1, x2, y2, cached_x_samples] = bezier;
  62. // https://www.w3.org/TR/css-easing-1/#cubic-bezier-algo
  63. // For input progress values outside the range [0, 1], the curve is extended infinitely using tangent of the curve
  64. // at the closest endpoint as follows:
  65. // - For input progress values less than zero,
  66. if (input_progress < 0.0) {
  67. // 1. If the x value of P1 is greater than zero, use a straight line that passes through P1 and P0 as the
  68. // tangent.
  69. if (x1 > 0.0)
  70. return y1 / x1 * input_progress;
  71. // 2. Otherwise, if the x value of P2 is greater than zero, use a straight line that passes through P2 and P0 as
  72. // the tangent.
  73. if (x2 > 0.0)
  74. return y2 / x2 * input_progress;
  75. // 3. Otherwise, let the output progress value be zero for all input progress values in the range [-∞, 0).
  76. return 0.0;
  77. }
  78. // - For input progress values greater than one,
  79. if (input_progress > 1.0) {
  80. // 1. If the x value of P2 is less than one, use a straight line that passes through P2 and P3 as the tangent.
  81. if (x2 < 1.0)
  82. return (1.0 - y2) / (1.0 - x2) * (input_progress - 1.0) + 1.0;
  83. // 2. Otherwise, if the x value of P1 is less than one, use a straight line that passes through P1 and P3 as the
  84. // tangent.
  85. if (x1 < 1.0)
  86. return (1.0 - y1) / (1.0 - x1) * (input_progress - 1.0) + 1.0;
  87. // 3. Otherwise, let the output progress value be one for all input progress values in the range (1, ∞].
  88. return 1.0;
  89. }
  90. // Note: The spec does not specify the precise algorithm for calculating values in the range [0, 1]:
  91. // "The evaluation of this curve is covered in many sources such as [FUND-COMP-GRAPHICS]."
  92. auto x = input_progress;
  93. auto solve = [&](auto t) {
  94. auto x = cubic_bezier_at(x1, x2, t);
  95. auto y = cubic_bezier_at(y1, y2, t);
  96. return CubicBezier::CachedSample { x, y, t };
  97. };
  98. if (cached_x_samples.is_empty())
  99. cached_x_samples.append(solve(0.));
  100. size_t nearby_index = 0;
  101. if (auto found = binary_search(cached_x_samples, x, &nearby_index, [](auto x, auto& sample) {
  102. if (x > sample.x)
  103. return 1;
  104. if (x < sample.x)
  105. return -1;
  106. return 0;
  107. }))
  108. return found->y;
  109. if (nearby_index == cached_x_samples.size() || nearby_index + 1 == cached_x_samples.size()) {
  110. // Produce more samples until we have enough.
  111. auto last_t = cached_x_samples.last().t;
  112. auto last_x = cached_x_samples.last().x;
  113. while (last_x <= x && last_t < 1.0) {
  114. last_t += 1. / 60.;
  115. auto solution = solve(last_t);
  116. cached_x_samples.append(solution);
  117. last_x = solution.x;
  118. }
  119. if (auto found = binary_search(cached_x_samples, x, &nearby_index, [](auto x, auto& sample) {
  120. if (x > sample.x)
  121. return 1;
  122. if (x < sample.x)
  123. return -1;
  124. return 0;
  125. }))
  126. return found->y;
  127. }
  128. // We have two samples on either side of the x value we want, so we can linearly interpolate between them.
  129. auto& sample1 = cached_x_samples[nearby_index];
  130. auto& sample2 = cached_x_samples[nearby_index + 1];
  131. auto factor = (x - sample1.x) / (sample2.x - sample1.x);
  132. return sample1.y + factor * (sample2.y - sample1.y);
  133. },
  134. [&](Steps const& steps) {
  135. // https://www.w3.org/TR/css-easing-1/#step-easing-algo
  136. // 1. Calculate the current step as floor(input progress value × steps).
  137. auto [number_of_steps, position] = steps;
  138. auto current_step = floor(input_progress * number_of_steps);
  139. // 2. If the step position property is one of:
  140. // - jump-start,
  141. // - jump-both,
  142. // increment current step by one.
  143. if (position == Steps::Position::JumpStart || position == Steps::Position::JumpBoth)
  144. current_step += 1;
  145. // 3. If both of the following conditions are true:
  146. // - the before flag is set, and
  147. // - input progress value × steps mod 1 equals zero (that is, if input progress value × steps is integral), then
  148. // decrement current step by one.
  149. auto step_progress = input_progress * number_of_steps;
  150. if (before_flag && trunc(step_progress) == step_progress)
  151. current_step -= 1;
  152. // 4. If input progress value ≥ 0 and current step < 0, let current step be zero.
  153. if (input_progress >= 0.0 && current_step < 0.0)
  154. current_step = 0.0;
  155. // 5. Calculate jumps based on the step position as follows:
  156. // jump-start or jump-end -> steps
  157. // jump-none -> steps - 1
  158. // jump-both -> steps + 1
  159. auto jumps = steps.number_of_intervals;
  160. if (position == Steps::Position::JumpNone) {
  161. jumps--;
  162. } else if (position == Steps::Position::JumpBoth) {
  163. jumps++;
  164. }
  165. // 6. If input progress value ≤ 1 and current step > jumps, let current step be jumps.
  166. if (input_progress <= 1.0 && current_step > jumps)
  167. current_step = jumps;
  168. // 7. The output progress value is current step / jumps.
  169. return current_step / jumps;
  170. });
  171. }
  172. String EasingStyleValue::Function::to_string() const
  173. {
  174. StringBuilder builder;
  175. visit(
  176. [&](Linear const& linear) {
  177. builder.append("linear"sv);
  178. if (!linear.stops.is_empty()) {
  179. builder.append('(');
  180. bool first = true;
  181. for (auto const& stop : linear.stops) {
  182. if (!first)
  183. builder.append(", "sv);
  184. first = false;
  185. builder.appendff("{}"sv, stop.offset);
  186. if (stop.position.has_value())
  187. builder.appendff(" {}"sv, stop.position.value());
  188. }
  189. builder.append(')');
  190. }
  191. },
  192. [&](CubicBezier const& bezier) {
  193. if (bezier == CubicBezier::ease()) {
  194. builder.append("ease"sv);
  195. } else if (bezier == CubicBezier::ease_in()) {
  196. builder.append("ease-in"sv);
  197. } else if (bezier == CubicBezier::ease_out()) {
  198. builder.append("ease-out"sv);
  199. } else if (bezier == CubicBezier::ease_in_out()) {
  200. builder.append("ease-in-out"sv);
  201. } else {
  202. builder.appendff("cubic-bezier({}, {}, {}, {})", bezier.x1, bezier.y1, bezier.x2, bezier.y2);
  203. }
  204. },
  205. [&](Steps const& steps) {
  206. if (steps == Steps::step_start()) {
  207. builder.append("step-start"sv);
  208. } else if (steps == Steps::step_end()) {
  209. builder.append("step-end"sv);
  210. } else {
  211. auto position = [&] -> Optional<StringView> {
  212. switch (steps.position) {
  213. case Steps::Position::JumpStart:
  214. return "jump-start"sv;
  215. case Steps::Position::JumpNone:
  216. return "jump-none"sv;
  217. case Steps::Position::JumpBoth:
  218. return "jump-both"sv;
  219. case Steps::Position::Start:
  220. return "start"sv;
  221. default:
  222. return {};
  223. }
  224. }();
  225. if (position.has_value()) {
  226. builder.appendff("steps({}, {})", steps.number_of_intervals, position.value());
  227. } else {
  228. builder.appendff("steps({})", steps.number_of_intervals);
  229. }
  230. }
  231. });
  232. return MUST(builder.to_string());
  233. }
  234. }