TimingFunction.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2023, Ali Mohammad Pur <mpfard@serenityos.org>
  3. * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Function.h>
  9. #include <AK/Vector.h>
  10. namespace Web::CSS {
  11. class EasingStyleValue;
  12. }
  13. namespace Web::Animations {
  14. // https://www.w3.org/TR/css-easing-1/#the-linear-easing-function
  15. struct LinearTimingFunction {
  16. double operator()(double t, bool) const;
  17. };
  18. // https://www.w3.org/TR/css-easing-1/#cubic-bezier-easing-functions
  19. struct CubicBezierTimingFunction {
  20. double x1;
  21. double y1;
  22. double x2;
  23. double y2;
  24. struct CachedSample {
  25. double x;
  26. double y;
  27. double t;
  28. };
  29. mutable Vector<CachedSample, 64> m_cached_x_samples = {};
  30. double operator()(double input_progress, bool) const;
  31. };
  32. // https://www.w3.org/TR/css-easing-1/#step-easing-functions
  33. struct StepsTimingFunction {
  34. size_t number_of_steps;
  35. bool jump_at_start;
  36. bool jump_at_end;
  37. double operator()(double input_progress, bool before_flag) const;
  38. };
  39. struct TimingFunction {
  40. static TimingFunction from_easing_style_value(CSS::EasingStyleValue const&);
  41. Variant<LinearTimingFunction, CubicBezierTimingFunction, StepsTimingFunction> function;
  42. double operator()(double input_progress, bool before_flag) const;
  43. };
  44. static TimingFunction linear_timing_function { LinearTimingFunction {} };
  45. // NOTE: Magic values from <https://www.w3.org/TR/css-easing-1/#valdef-cubic-bezier-easing-function-ease>
  46. static TimingFunction ease_timing_function { CubicBezierTimingFunction { 0.25, 0.1, 0.25, 1.0 } };
  47. static TimingFunction ease_in_timing_function { CubicBezierTimingFunction { 0.42, 0.0, 1.0, 1.0 } };
  48. static TimingFunction ease_out_timing_function { CubicBezierTimingFunction { 0.0, 0.0, 0.58, 1.0 } };
  49. static TimingFunction ease_in_out_timing_function { CubicBezierTimingFunction { 0.42, 0.0, 0.58, 1.0 } };
  50. }