CSSTransition.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2024, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Animations/DocumentTimeline.h>
  8. #include <LibWeb/Bindings/CSSTransitionPrototype.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/CSS/CSSStyleDeclaration.h>
  11. #include <LibWeb/CSS/CSSTransition.h>
  12. #include <LibWeb/CSS/Interpolation.h>
  13. #include <LibWeb/DOM/Document.h>
  14. #include <LibWeb/DOM/Element.h>
  15. #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
  16. namespace Web::CSS {
  17. JS_DEFINE_ALLOCATOR(CSSTransition);
  18. JS::NonnullGCPtr<CSSTransition> CSSTransition::start_a_transition(DOM::Element& element, PropertyID property_id, size_t transition_generation,
  19. double start_time, double end_time, NonnullRefPtr<CSSStyleValue const> start_value, NonnullRefPtr<CSSStyleValue const> end_value,
  20. NonnullRefPtr<CSSStyleValue const> reversing_adjusted_start_value, double reversing_shortening_factor)
  21. {
  22. auto& realm = element.realm();
  23. return realm.heap().allocate<CSSTransition>(realm, realm, element, property_id, transition_generation, start_time, end_time, start_value, end_value, reversing_adjusted_start_value, reversing_shortening_factor);
  24. }
  25. Animations::AnimationClass CSSTransition::animation_class() const
  26. {
  27. return Animations::AnimationClass::CSSTransition;
  28. }
  29. Optional<int> CSSTransition::class_specific_composite_order(JS::NonnullGCPtr<Animations::Animation> other_animation) const
  30. {
  31. auto other = JS::NonnullGCPtr { verify_cast<CSSTransition>(*other_animation) };
  32. // Within the set of CSS Transitions, two animations A and B are sorted in composite order (first to last) as
  33. // follows:
  34. // 1. If neither A nor B has an owning element, sort based on their relative position in the global animation list.
  35. if (!owning_element() && !other->owning_element())
  36. return global_animation_list_order() - other->global_animation_list_order();
  37. // 2. Otherwise, if only one of A or B has an owning element, let the animation with an owning element sort first.
  38. if (owning_element() && !other->owning_element())
  39. return -1;
  40. if (!owning_element() && other->owning_element())
  41. return 1;
  42. // 3. Otherwise, if the owning element of A and B differs, sort A and B by tree order of their corresponding owning
  43. // elements. With regard to pseudo-elements, the sort order is as follows:
  44. // - element
  45. // - ::marker
  46. // - ::before
  47. // - any other pseudo-elements not mentioned specifically in this list, sorted in ascending order by the Unicode
  48. // codepoints that make up each selector
  49. // - ::after
  50. // - element children
  51. if (owning_element().ptr() != other->owning_element().ptr()) {
  52. // FIXME: Actually sort by tree order
  53. return {};
  54. }
  55. // 4. Otherwise, if A and B have different transition generation values, sort by their corresponding transition
  56. // generation in ascending order.
  57. if (m_transition_generation != other->m_transition_generation)
  58. return m_transition_generation - other->m_transition_generation;
  59. // FIXME:
  60. // 5. Otherwise, sort A and B in ascending order by the Unicode codepoints that make up the expanded transition
  61. // property name of each transition (i.e. without attempting case conversion and such that ‘-moz-column-width’
  62. // sorts before ‘column-width’).
  63. return {};
  64. }
  65. CSSTransition::CSSTransition(JS::Realm& realm, DOM::Element& element, PropertyID property_id, size_t transition_generation,
  66. double start_time, double end_time, NonnullRefPtr<CSSStyleValue const> start_value, NonnullRefPtr<CSSStyleValue const> end_value,
  67. NonnullRefPtr<CSSStyleValue const> reversing_adjusted_start_value, double reversing_shortening_factor)
  68. : Animations::Animation(realm)
  69. , m_transition_property(property_id)
  70. , m_transition_generation(transition_generation)
  71. , m_start_time(start_time)
  72. , m_end_time(end_time)
  73. , m_start_value(move(start_value))
  74. , m_end_value(move(end_value))
  75. , m_reversing_adjusted_start_value(move(reversing_adjusted_start_value))
  76. , m_reversing_shortening_factor(reversing_shortening_factor)
  77. , m_keyframe_effect(Animations::KeyframeEffect::create(realm))
  78. {
  79. // FIXME:
  80. // Transitions generated using the markup defined in this specification are not added to the global animation list
  81. // when they are created. Instead, these animations are appended to the global animation list at the first moment
  82. // when they transition out of the idle play state after being disassociated from their owning element. Transitions
  83. // that have been disassociated from their owning element but are still idle do not have a defined composite order.
  84. set_start_time(start_time - element.document().timeline()->current_time().value());
  85. // Construct a KeyframesEffect for our animation
  86. m_keyframe_effect->set_target(&element);
  87. m_keyframe_effect->set_start_delay(start_time);
  88. m_keyframe_effect->set_iteration_duration(m_end_time - start_time);
  89. m_keyframe_effect->set_timing_function(element.property_transition_attributes(property_id)->timing_function);
  90. auto key_frame_set = adopt_ref(*new Animations::KeyframeEffect::KeyFrameSet);
  91. Animations::KeyframeEffect::KeyFrameSet::ResolvedKeyFrame initial_keyframe;
  92. initial_keyframe.properties.set(property_id, m_start_value);
  93. Animations::KeyframeEffect::KeyFrameSet::ResolvedKeyFrame final_keyframe;
  94. final_keyframe.properties.set(property_id, m_end_value);
  95. key_frame_set->keyframes_by_key.insert(0, initial_keyframe);
  96. key_frame_set->keyframes_by_key.insert(100 * Animations::KeyframeEffect::AnimationKeyFrameKeyScaleFactor, final_keyframe);
  97. m_keyframe_effect->set_key_frame_set(key_frame_set);
  98. set_timeline(element.document().timeline());
  99. set_owning_element(element);
  100. set_effect(m_keyframe_effect);
  101. element.associate_with_animation(*this);
  102. element.set_transition(m_transition_property, *this);
  103. HTML::TemporaryExecutionContext context(element.document().relevant_settings_object());
  104. play().release_value_but_fixme_should_propagate_errors();
  105. }
  106. void CSSTransition::initialize(JS::Realm& realm)
  107. {
  108. Base::initialize(realm);
  109. WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSTransition);
  110. }
  111. void CSSTransition::visit_edges(Cell::Visitor& visitor)
  112. {
  113. Base::visit_edges(visitor);
  114. visitor.visit(m_cached_declaration);
  115. visitor.visit(m_keyframe_effect);
  116. }
  117. double CSSTransition::timing_function_output_at_time(double t) const
  118. {
  119. auto progress = (t - transition_start_time()) / (transition_end_time() - transition_start_time());
  120. // FIXME: Is this before_flag value correct?
  121. bool before_flag = t < transition_start_time();
  122. return m_keyframe_effect->timing_function().evaluate_at(progress, before_flag);
  123. }
  124. NonnullRefPtr<CSSStyleValue const> CSSTransition::value_at_time(double t) const
  125. {
  126. // https://drafts.csswg.org/css-transitions/#application
  127. auto progress = timing_function_output_at_time(t);
  128. auto result = interpolate_property(*m_keyframe_effect->target(), m_transition_property, m_start_value, m_end_value, progress);
  129. if (result)
  130. return result.release_nonnull();
  131. return m_start_value;
  132. }
  133. }