Animation.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Animations/Animation.h>
  7. #include <LibWeb/Animations/AnimationEffect.h>
  8. #include <LibWeb/Animations/DocumentTimeline.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/HTML/Window.h>
  12. #include <LibWeb/WebIDL/ExceptionOr.h>
  13. #include <LibWeb/WebIDL/Promise.h>
  14. namespace Web::Animations {
  15. // https://www.w3.org/TR/web-animations-1/#dom-animation-animation
  16. JS::NonnullGCPtr<Animation> Animation::create(JS::Realm& realm, JS::GCPtr<AnimationEffect> effect, JS::GCPtr<AnimationTimeline> timeline)
  17. {
  18. // 1. Let animation be a new Animation object.
  19. auto animation = realm.heap().allocate<Animation>(realm, realm);
  20. // 2. Run the procedure to set the timeline of an animation on animation passing timeline as the new timeline or, if
  21. // a timeline argument is missing, passing the default document timeline of the Document associated with the
  22. // Window that is the current global object.
  23. if (!timeline) {
  24. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  25. timeline = window.associated_document().timeline();
  26. }
  27. animation->set_timeline(timeline);
  28. // 3. Run the procedure to set the associated effect of an animation on animation passing source as the new effect.
  29. animation->set_effect(effect);
  30. return animation;
  31. }
  32. WebIDL::ExceptionOr<JS::NonnullGCPtr<Animation>> Animation::construct_impl(JS::Realm& realm, JS::GCPtr<AnimationEffect> effect, JS::GCPtr<AnimationTimeline> timeline)
  33. {
  34. return create(realm, effect, timeline);
  35. }
  36. // https://www.w3.org/TR/web-animations-1/#animation-set-the-associated-effect-of-an-animation
  37. void Animation::set_effect(JS::GCPtr<AnimationEffect> new_effect)
  38. {
  39. // Setting this attribute updates the object’s associated effect using the procedure to set the associated effect of
  40. // an animation.
  41. // 1. Let old effect be the current associated effect of animation, if any.
  42. auto old_effect = m_effect;
  43. // 2. If new effect is the same object as old effect, abort this procedure.
  44. if (new_effect == old_effect)
  45. return;
  46. // 3. If animation has a pending pause task, reschedule that task to run as soon as animation is ready.
  47. if (m_pending_pause_task == TaskState::Pending)
  48. m_pending_pause_task = TaskState::RunAsSoonAsReady;
  49. // 4. If animation has a pending play task, reschedule that task to run as soon as animation is ready to play ne
  50. // effect.
  51. if (m_pending_play_task == TaskState::Pending)
  52. m_pending_play_task = TaskState::RunAsSoonAsReady;
  53. // 5. If new effect is not null and if new effect is the associated effect of another animation, previous animation,
  54. // run the procedure to set the associated effect of an animation (this procedure) on previous animation passing
  55. // null as new effect.
  56. if (new_effect && new_effect->associated_animation() != this) {
  57. if (auto animation = new_effect->associated_animation())
  58. animation->set_effect({});
  59. }
  60. // 6. Let the associated effect of animation be new effect.
  61. if (new_effect)
  62. new_effect->set_associated_animation(this);
  63. if (m_effect)
  64. m_effect->set_associated_animation({});
  65. m_effect = new_effect;
  66. // FIXME: 7. Run the procedure to update an animation’s finished state for animation with the did seek flag set to
  67. // false, and the synchronously notify flag set to false.
  68. }
  69. // https://www.w3.org/TR/web-animations-1/#animation-set-the-timeline-of-an-animation
  70. void Animation::set_timeline(JS::GCPtr<AnimationTimeline> new_timeline)
  71. {
  72. // Setting this attribute updates the object’s timeline using the procedure to set the timeline of an animation.
  73. // 1. Let old timeline be the current timeline of animation, if any.
  74. auto old_timeline = m_timeline;
  75. // 2. If new timeline is the same object as old timeline, abort this procedure.
  76. if (new_timeline == old_timeline)
  77. return;
  78. // 3. Let the timeline of animation be new timeline.
  79. if (m_timeline)
  80. m_timeline->disassociate_with_animation(*this);
  81. m_timeline = new_timeline;
  82. m_timeline->associate_with_animation(*this);
  83. // 4. If the start time of animation is resolved, make animation’s hold time unresolved.
  84. if (m_start_time.has_value())
  85. m_hold_time = {};
  86. // FIXME: 5. Run the procedure to update an animation’s finished state for animation with the did seek flag set to
  87. // false, and the synchronously notify flag set to false.
  88. }
  89. // https://www.w3.org/TR/web-animations-1/#dom-animation-starttime
  90. // https://www.w3.org/TR/web-animations-1/#set-the-start-time
  91. void Animation::set_start_time(Optional<double> const& new_start_time)
  92. {
  93. // Setting this attribute updates the start time using the procedure to set the start time of this object to the new
  94. // value.
  95. // 1. Let timeline time be the current time value of the timeline that animation is associated with. If there is no
  96. // timeline associated with animation or the associated timeline is inactive, let the timeline time be
  97. // unresolved.
  98. auto timeline_time = m_timeline && !m_timeline->is_inactive() ? m_timeline->current_time() : Optional<double> {};
  99. // 2. If timeline time is unresolved and new start time is resolved, make animation’s hold time unresolved.
  100. if (!timeline_time.has_value() && new_start_time.has_value())
  101. m_hold_time = {};
  102. // 3. Let previous current time be animation’s current time.
  103. auto previous_current_time = current_time();
  104. // 4. Apply any pending playback rate on animation.
  105. apply_any_pending_playback_rate();
  106. // 5. Set animation’s start time to new start time.
  107. m_start_time = new_start_time;
  108. // 6. Update animation’s hold time based on the first matching condition from the following,
  109. // -> If new start time is resolved,
  110. if (new_start_time.has_value()) {
  111. // If animation’s playback rate is not zero, make animation’s hold time unresolved.
  112. if (m_playback_rate != 0.0)
  113. m_hold_time = {};
  114. }
  115. // -> Otherwise (new start time is unresolved),
  116. else {
  117. // Set animation’s hold time to previous current time even if previous current time is unresolved.
  118. m_hold_time = previous_current_time;
  119. }
  120. // 7. If animation has a pending play task or a pending pause task, cancel that task and resolve animation’s current
  121. // ready promise with animation.
  122. if (m_pending_play_task == TaskState::Pending || m_pending_pause_task == TaskState::Pending) {
  123. m_pending_play_task = TaskState::None;
  124. m_pending_pause_task = TaskState::None;
  125. WebIDL::resolve_promise(realm(), current_ready_promise(), this);
  126. }
  127. // FIXME: 8. Run the procedure to update an animation’s finished state for animation with the did seek flag set to
  128. // true, and the synchronously notify flag set to false.
  129. }
  130. // https://www.w3.org/TR/web-animations-1/#animation-current-time
  131. Optional<double> Animation::current_time() const
  132. {
  133. // The current time is calculated from the first matching condition from below:
  134. // -> If the animation’s hold time is resolved,
  135. if (m_hold_time.has_value()) {
  136. // The current time is the animation’s hold time.
  137. return m_hold_time.value();
  138. }
  139. // -> If any of the following are true:
  140. // - the animation has no associated timeline, or
  141. // - the associated timeline is inactive, or
  142. // - the animation’s start time is unresolved.
  143. if (!m_timeline || m_timeline->is_inactive() || !m_start_time.has_value()) {
  144. // The current time is an unresolved time value.
  145. return {};
  146. }
  147. // -> Otherwise,
  148. // current time = (timeline time - start time) × playback rate
  149. // Where timeline time is the current time value of the associated timeline. The playback rate value is defined
  150. // in §4.4.15 Speed control.
  151. return (m_timeline->current_time().value() - m_start_time.value()) * playback_rate();
  152. }
  153. // https://www.w3.org/TR/web-animations-1/#animation-set-the-current-time
  154. WebIDL::ExceptionOr<void> Animation::set_current_time(Optional<double> const& seek_time)
  155. {
  156. // FIXME: Implement
  157. (void)seek_time;
  158. return {};
  159. }
  160. // https://www.w3.org/TR/web-animations-1/#dom-animation-playbackrate
  161. // https://www.w3.org/TR/web-animations-1/#set-the-playback-rate
  162. WebIDL::ExceptionOr<void> Animation::set_playback_rate(double new_playback_rate)
  163. {
  164. // FIXME: Implement
  165. (void)new_playback_rate;
  166. return {};
  167. }
  168. // https://www.w3.org/TR/web-animations-1/#animation-play-state
  169. Bindings::AnimationPlayState Animation::play_state() const
  170. {
  171. // FIXME: Implement
  172. return Bindings::AnimationPlayState::Idle;
  173. }
  174. // https://www.w3.org/TR/web-animations-1/#apply-any-pending-playback-rate
  175. void Animation::apply_any_pending_playback_rate()
  176. {
  177. // 1. If animation does not have a pending playback rate, abort these steps.
  178. if (!m_pending_playback_rate.has_value())
  179. return;
  180. // 2. Set animation’s playback rate to its pending playback rate.
  181. m_playback_rate = m_pending_playback_rate.value();
  182. // 3. Clear animation’s pending playback rate.
  183. m_pending_playback_rate = {};
  184. }
  185. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_ready_promise() const
  186. {
  187. if (!m_current_ready_promise) {
  188. // The current ready promise is initially a resolved Promise created using the procedure to create a new
  189. // resolved Promise with the animation itself as its value and created in the relevant Realm of the animation.
  190. m_current_ready_promise = WebIDL::create_resolved_promise(realm(), this);
  191. }
  192. return *m_current_ready_promise;
  193. }
  194. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_finished_promise() const
  195. {
  196. if (!m_current_finished_promise) {
  197. // The current finished promise is initially a pending Promise object.
  198. m_current_finished_promise = WebIDL::create_promise(realm());
  199. }
  200. return *m_current_finished_promise;
  201. }
  202. Animation::Animation(JS::Realm& realm)
  203. : DOM::EventTarget(realm)
  204. {
  205. }
  206. void Animation::initialize(JS::Realm& realm)
  207. {
  208. Base::initialize(realm);
  209. set_prototype(&Bindings::ensure_web_prototype<Bindings::AnimationPrototype>(realm, "Animation"));
  210. }
  211. void Animation::visit_edges(Cell::Visitor& visitor)
  212. {
  213. Base::visit_edges(visitor);
  214. visitor.visit(m_effect);
  215. visitor.visit(m_timeline);
  216. visitor.visit(m_current_ready_promise);
  217. visitor.visit(m_current_finished_promise);
  218. }
  219. }