TimingFunction.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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::Animations {
  11. // https://www.w3.org/TR/css-easing-1/#the-linear-easing-function
  12. struct LinearTimingFunction {
  13. double operator()(double t, bool) const;
  14. };
  15. // https://www.w3.org/TR/css-easing-1/#cubic-bezier-easing-functions
  16. struct CubicBezierTimingFunction {
  17. double x1;
  18. double y1;
  19. double x2;
  20. double y2;
  21. struct CachedSample {
  22. double x;
  23. double y;
  24. double t;
  25. };
  26. mutable Vector<CachedSample, 64> m_cached_x_samples = {};
  27. double operator()(double input_progress, bool) const;
  28. };
  29. // https://www.w3.org/TR/css-easing-1/#step-easing-functions
  30. struct StepsTimingFunction {
  31. size_t number_of_steps;
  32. bool jump_at_start;
  33. bool jump_at_end;
  34. double operator()(double input_progress, bool before_flag) const;
  35. };
  36. struct TimingFunction {
  37. Variant<LinearTimingFunction, CubicBezierTimingFunction, StepsTimingFunction> function;
  38. double operator()(double input_progress, bool before_flag) const;
  39. };
  40. static TimingFunction linear_timing_function { LinearTimingFunction {} };
  41. // NOTE: Magic values from <https://www.w3.org/TR/css-easing-1/#valdef-cubic-bezier-easing-function-ease>
  42. static TimingFunction ease_timing_function { CubicBezierTimingFunction { 0.25, 0.1, 0.25, 1.0 } };
  43. static TimingFunction ease_in_timing_function { CubicBezierTimingFunction { 0.42, 0.0, 1.0, 1.0 } };
  44. static TimingFunction ease_out_timing_function { CubicBezierTimingFunction { 0.0, 0.0, 0.58, 1.0 } };
  45. static TimingFunction ease_in_out_timing_function { CubicBezierTimingFunction { 0.42, 0.0, 0.58, 1.0 } };
  46. }