Animation.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. /*
  2. * Copyright (c) 2023-2024, Matthew Olsson <mattco@serenityos.org>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TemporaryChange.h>
  7. #include <LibWeb/Animations/Animation.h>
  8. #include <LibWeb/Animations/AnimationEffect.h>
  9. #include <LibWeb/Animations/AnimationPlaybackEvent.h>
  10. #include <LibWeb/Animations/DocumentTimeline.h>
  11. #include <LibWeb/Bindings/Intrinsics.h>
  12. #include <LibWeb/CSS/CSSAnimation.h>
  13. #include <LibWeb/DOM/Document.h>
  14. #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
  15. #include <LibWeb/HTML/Window.h>
  16. #include <LibWeb/WebIDL/ExceptionOr.h>
  17. #include <LibWeb/WebIDL/Promise.h>
  18. namespace Web::Animations {
  19. JS_DEFINE_ALLOCATOR(Animation);
  20. // https://www.w3.org/TR/web-animations-1/#dom-animation-animation
  21. JS::NonnullGCPtr<Animation> Animation::create(JS::Realm& realm, JS::GCPtr<AnimationEffect> effect, Optional<JS::GCPtr<AnimationTimeline>> timeline)
  22. {
  23. // 1. Let animation be a new Animation object.
  24. auto animation = realm.heap().allocate<Animation>(realm, realm);
  25. // 2. Run the procedure to set the timeline of an animation on animation passing timeline as the new timeline or, if
  26. // a timeline argument is missing, passing the default document timeline of the Document associated with the
  27. // Window that is the current global object.
  28. if (!timeline.has_value()) {
  29. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  30. timeline = window.associated_document().timeline();
  31. }
  32. animation->set_timeline(timeline.release_value());
  33. // 3. Run the procedure to set the associated effect of an animation on animation passing source as the new effect.
  34. animation->set_effect(effect);
  35. return animation;
  36. }
  37. WebIDL::ExceptionOr<JS::NonnullGCPtr<Animation>> Animation::construct_impl(JS::Realm& realm, JS::GCPtr<AnimationEffect> effect, Optional<JS::GCPtr<AnimationTimeline>> timeline)
  38. {
  39. return create(realm, effect, timeline);
  40. }
  41. // https://www.w3.org/TR/web-animations-1/#animation-set-the-associated-effect-of-an-animation
  42. void Animation::set_effect(JS::GCPtr<AnimationEffect> new_effect)
  43. {
  44. // Setting this attribute updates the object’s associated effect using the procedure to set the associated effect of
  45. // an animation.
  46. // 1. Let old effect be the current associated effect of animation, if any.
  47. auto old_effect = m_effect;
  48. // 2. If new effect is the same object as old effect, abort this procedure.
  49. if (new_effect == old_effect)
  50. return;
  51. // 3. If animation has a pending pause task, reschedule that task to run as soon as animation is ready.
  52. // 4. If animation has a pending play task, reschedule that task to run as soon as animation is ready to play ne
  53. // effect.
  54. // Note: There is no real difference between "pending" and "as soon as possible", so this step is a no-op.
  55. // 5. If new effect is not null and if new effect is the associated effect of another animation, previous animation,
  56. // run the procedure to set the associated effect of an animation (this procedure) on previous animation passing
  57. // null as new effect.
  58. if (new_effect && new_effect->associated_animation() != this) {
  59. if (auto animation = new_effect->associated_animation())
  60. animation->set_effect({});
  61. }
  62. // 6. Let the associated effect of animation be new effect.
  63. if (new_effect)
  64. new_effect->set_associated_animation(this);
  65. if (m_effect)
  66. m_effect->set_associated_animation({});
  67. m_effect = new_effect;
  68. // 7. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  69. // and the synchronously notify flag set to false.
  70. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  71. }
  72. // https://www.w3.org/TR/web-animations-1/#animation-set-the-timeline-of-an-animation
  73. void Animation::set_timeline(JS::GCPtr<AnimationTimeline> new_timeline)
  74. {
  75. // Setting this attribute updates the object’s timeline using the procedure to set the timeline of an animation.
  76. // 1. Let old timeline be the current timeline of animation, if any.
  77. auto old_timeline = m_timeline;
  78. // 2. If new timeline is the same object as old timeline, abort this procedure.
  79. if (new_timeline == old_timeline)
  80. return;
  81. // 3. Let the timeline of animation be new timeline.
  82. if (m_timeline)
  83. m_timeline->disassociate_with_animation(*this);
  84. m_timeline = new_timeline;
  85. m_timeline->associate_with_animation(*this);
  86. // 4. If the start time of animation is resolved, make animation’s hold time unresolved.
  87. if (m_start_time.has_value())
  88. m_hold_time = {};
  89. // 5. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  90. // and the synchronously notify flag set to false.
  91. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  92. }
  93. // https://www.w3.org/TR/web-animations-1/#dom-animation-starttime
  94. // https://www.w3.org/TR/web-animations-1/#set-the-start-time
  95. void Animation::set_start_time(Optional<double> const& new_start_time)
  96. {
  97. // Setting this attribute updates the start time using the procedure to set the start time of this object to the new
  98. // value.
  99. // 1. Let timeline time be the current time value of the timeline that animation is associated with. If there is no
  100. // timeline associated with animation or the associated timeline is inactive, let the timeline time be
  101. // unresolved.
  102. auto timeline_time = m_timeline && !m_timeline->is_inactive() ? m_timeline->current_time() : Optional<double> {};
  103. // 2. If timeline time is unresolved and new start time is resolved, make animation’s hold time unresolved.
  104. if (!timeline_time.has_value() && new_start_time.has_value())
  105. m_hold_time = {};
  106. // 3. Let previous current time be animation’s current time.
  107. auto previous_current_time = current_time();
  108. // 4. Apply any pending playback rate on animation.
  109. apply_any_pending_playback_rate();
  110. // 5. Set animation’s start time to new start time.
  111. m_start_time = new_start_time;
  112. // 6. Update animation’s hold time based on the first matching condition from the following,
  113. // -> If new start time is resolved,
  114. if (new_start_time.has_value()) {
  115. // If animation’s playback rate is not zero, make animation’s hold time unresolved.
  116. if (m_playback_rate != 0.0)
  117. m_hold_time = {};
  118. }
  119. // -> Otherwise (new start time is unresolved),
  120. else {
  121. // Set animation’s hold time to previous current time even if previous current time is unresolved.
  122. m_hold_time = previous_current_time;
  123. }
  124. // 7. If animation has a pending play task or a pending pause task, cancel that task and resolve animation’s current
  125. // ready promise with animation.
  126. if (pending()) {
  127. m_pending_play_task = TaskState::None;
  128. m_pending_pause_task = TaskState::None;
  129. WebIDL::resolve_promise(realm(), current_ready_promise(), this);
  130. }
  131. // 8. Run the procedure to update an animation’s finished state for animation with the did seek flag set to true,
  132. // and the synchronously notify flag set to false.
  133. update_finished_state(DidSeek::Yes, SynchronouslyNotify::No);
  134. }
  135. // https://www.w3.org/TR/web-animations-1/#animation-current-time
  136. Optional<double> Animation::current_time() const
  137. {
  138. // The current time is calculated from the first matching condition from below:
  139. // -> If the animation’s hold time is resolved,
  140. if (m_hold_time.has_value()) {
  141. // The current time is the animation’s hold time.
  142. return m_hold_time.value();
  143. }
  144. // -> If any of the following are true:
  145. // - the animation has no associated timeline, or
  146. // - the associated timeline is inactive, or
  147. // - the animation’s start time is unresolved.
  148. if (!m_timeline || m_timeline->is_inactive() || !m_start_time.has_value()) {
  149. // The current time is an unresolved time value.
  150. return {};
  151. }
  152. // -> Otherwise,
  153. // current time = (timeline time - start time) × playback rate
  154. // Where timeline time is the current time value of the associated timeline. The playback rate value is defined
  155. // in §4.4.15 Speed control.
  156. return (m_timeline->current_time().value() - m_start_time.value()) * playback_rate();
  157. }
  158. // https://www.w3.org/TR/web-animations-1/#animation-set-the-current-time
  159. WebIDL::ExceptionOr<void> Animation::set_current_time(Optional<double> const& seek_time)
  160. {
  161. // 1. Run the steps to silently set the current time of animation to seek time.
  162. TRY(silently_set_current_time(seek_time));
  163. // 2. If animation has a pending pause task, synchronously complete the pause operation by performing the following
  164. // steps:
  165. if (m_pending_pause_task == TaskState::Scheduled) {
  166. // 1. Set animation’s hold time to seek time.
  167. m_hold_time = seek_time;
  168. // 2. Apply any pending playback rate to animation.
  169. apply_any_pending_playback_rate();
  170. // 3. Make animation’s start time unresolved.
  171. m_start_time = {};
  172. // 4. Cancel the pending pause task.
  173. m_pending_pause_task = TaskState::None;
  174. // 5 Resolve animation’s current ready promise with animation.
  175. WebIDL::resolve_promise(realm(), current_ready_promise(), this);
  176. }
  177. // 3. Run the procedure to update an animation’s finished state for animation with the did seek flag set to true,
  178. // and the synchronously notify flag set to false.
  179. update_finished_state(DidSeek::Yes, SynchronouslyNotify::No);
  180. return {};
  181. }
  182. // https://www.w3.org/TR/web-animations-1/#dom-animation-playbackrate
  183. // https://www.w3.org/TR/web-animations-1/#set-the-playback-rate
  184. WebIDL::ExceptionOr<void> Animation::set_playback_rate(double new_playback_rate)
  185. {
  186. // Setting this attribute follows the procedure to set the playback rate of this object to the new value.
  187. // 1. Clear any pending playback rate on animation.
  188. m_pending_playback_rate = {};
  189. // 2. Let previous time be the value of the current time of animation before changing the playback rate.
  190. auto previous_time = current_time();
  191. // 3. Let previous playback rate be the current effective playback rate of animation.
  192. auto previous_playback_rate = playback_rate();
  193. // 4. Set the playback rate to new playback rate.
  194. m_playback_rate = new_playback_rate;
  195. // 5. Perform the steps corresponding to the first matching condition from the following, if any:
  196. // -> If animation is associated with a monotonically increasing timeline and the previous time is resolved,
  197. if (m_timeline && m_timeline->is_monotonically_increasing() && previous_time.has_value()) {
  198. // set the current time of animation to previous time.
  199. TRY(set_current_time(previous_time));
  200. }
  201. // -> If animation is associated with a non-null timeline that is not monotonically increasing, the start time of
  202. // animation is resolved, associated effect end is not infinity, and either:
  203. // - the previous playback rate < 0 and the new playback rate ≥ 0, or
  204. // - the previous playback rate ≥ 0 and the new playback rate < 0,
  205. else if (m_timeline && !m_timeline->is_monotonically_increasing() && m_start_time.has_value() && !isinf(associated_effect_end()) && ((previous_playback_rate < 0.0 && new_playback_rate >= 0.0) || (previous_playback_rate >= 0 && new_playback_rate < 0))) {
  206. // Set animation’s start time to the result of evaluating associated effect end - start time for animation.
  207. m_start_time = associated_effect_end() - m_start_time.value();
  208. }
  209. return {};
  210. }
  211. // https://www.w3.org/TR/web-animations-1/#animation-play-state
  212. Bindings::AnimationPlayState Animation::play_state() const
  213. {
  214. // The play state of animation, animation, at a given moment is the state corresponding to the first matching
  215. // condition from the following:
  216. // -> All of the following conditions are true:
  217. // - The current time of animation is unresolved, and
  218. // - the start time of animation is unresolved, and
  219. // - animation does not have either a pending play task or a pending pause task,
  220. auto current_time = this->current_time();
  221. if (!current_time.has_value() && !m_start_time.has_value() && !pending()) {
  222. // → idle
  223. return Bindings::AnimationPlayState::Idle;
  224. }
  225. // -> Either of the following conditions are true:
  226. // - animation has a pending pause task, or
  227. // - both the start time of animation is unresolved and it does not have a pending play task,
  228. if (m_pending_pause_task == TaskState::Scheduled || (!m_start_time.has_value() && m_pending_play_task == TaskState::None)) {
  229. // → paused
  230. return Bindings::AnimationPlayState::Paused;
  231. }
  232. // -> For animation, current time is resolved and either of the following conditions are true:
  233. // - animation’s effective playback rate > 0 and current time ≥ associated effect end; or
  234. // - animation’s effective playback rate < 0 and current time ≤ 0,
  235. auto effective_playback_rate = this->effective_playback_rate();
  236. if (current_time.has_value() && ((effective_playback_rate > 0.0 && current_time.value() >= associated_effect_end()) || (effective_playback_rate < 0.0 && current_time.value() <= 0.0))) {
  237. // → finished
  238. return Bindings::AnimationPlayState::Finished;
  239. }
  240. // -> Otherwise,
  241. // → running
  242. return Bindings::AnimationPlayState::Running;
  243. }
  244. // https://www.w3.org/TR/web-animations-1/#animation-relevant
  245. bool Animation::is_relevant() const
  246. {
  247. // An animation is relevant if:
  248. // - Its associated effect is current or in effect, and
  249. // - Its replace state is not removed.
  250. return (m_effect->is_current() || m_effect->is_in_effect()) && replace_state() != Bindings::AnimationReplaceState::Removed;
  251. }
  252. // https://www.w3.org/TR/web-animations-1/#replaceable-animation
  253. bool Animation::is_replaceable() const
  254. {
  255. // An animation is replaceable if all of the following conditions are true:
  256. // - The existence of the animation is not prescribed by markup. That is, it is not a CSS animation with an owning
  257. // element, nor a CSS transition with an owning element.
  258. // FIXME: Check for transitions
  259. if (is_css_animation() && static_cast<CSS::CSSAnimation const*>(this)->owning_element())
  260. return false;
  261. // - The animation's play state is finished.
  262. if (play_state() != Bindings::AnimationPlayState::Finished)
  263. return false;
  264. // - The animation's replace state is not removed.
  265. if (replace_state() == Bindings::AnimationReplaceState::Removed)
  266. return false;
  267. // - The animation is associated with a monotonically increasing timeline.
  268. if (!m_timeline || !m_timeline->is_monotonically_increasing())
  269. return false;
  270. // - The animation has an associated effect.
  271. if (!m_effect)
  272. return false;
  273. // - The animation's associated effect is in effect.
  274. if (!m_effect->is_in_effect())
  275. return false;
  276. // - The animation's associated effect has an effect target.
  277. if (!m_effect->target())
  278. return false;
  279. return true;
  280. }
  281. void Animation::set_replace_state(Bindings::AnimationReplaceState value)
  282. {
  283. m_replace_state = value;
  284. if (value == Bindings::AnimationReplaceState::Removed) {
  285. // Remove the associated effect from its target, if applicable
  286. if (m_effect && m_effect->target())
  287. m_effect->target()->disassociate_with_effect(*m_effect);
  288. // Remove this animation from its timeline
  289. m_timeline->disassociate_with_animation(*this);
  290. }
  291. }
  292. // https://www.w3.org/TR/web-animations-1/#dom-animation-onfinish
  293. JS::GCPtr<WebIDL::CallbackType> Animation::onfinish()
  294. {
  295. return event_handler_attribute(HTML::EventNames::finish);
  296. }
  297. // https://www.w3.org/TR/web-animations-1/#dom-animation-onfinish
  298. void Animation::set_onfinish(JS::GCPtr<WebIDL::CallbackType> event_handler)
  299. {
  300. set_event_handler_attribute(HTML::EventNames::finish, event_handler);
  301. }
  302. // https://www.w3.org/TR/web-animations-1/#dom-animation-oncancel
  303. JS::GCPtr<WebIDL::CallbackType> Animation::oncancel()
  304. {
  305. return event_handler_attribute(HTML::EventNames::cancel);
  306. }
  307. // https://www.w3.org/TR/web-animations-1/#dom-animation-oncancel
  308. void Animation::set_oncancel(JS::GCPtr<WebIDL::CallbackType> event_handler)
  309. {
  310. set_event_handler_attribute(HTML::EventNames::cancel, event_handler);
  311. }
  312. // https://www.w3.org/TR/web-animations-1/#dom-animation-onremove
  313. JS::GCPtr<WebIDL::CallbackType> Animation::onremove()
  314. {
  315. return event_handler_attribute(HTML::EventNames::remove);
  316. }
  317. // https://www.w3.org/TR/web-animations-1/#dom-animation-onremove
  318. void Animation::set_onremove(JS::GCPtr<WebIDL::CallbackType> event_handler)
  319. {
  320. set_event_handler_attribute(HTML::EventNames::remove, event_handler);
  321. }
  322. // https://www.w3.org/TR/web-animations-1/#dom-animation-cancel
  323. void Animation::cancel(ShouldInvalidate should_invalidate)
  324. {
  325. // Note: When called from JS, we always want to invalidate the animation target's style. However, this method is
  326. // also called from the StyleComputer when the animation-name CSS property changes. That happens in the
  327. // middle of a cascade, and importantly, _before_ computing the animation effect stack, so there is no
  328. // need for another invalidation. And in fact, if we did invalidate, it would lead to a crash, as the element
  329. // would not have it's "m_needs_style_update" flag cleared.
  330. auto& realm = this->realm();
  331. // 1. If animation’s play state is not idle, perform the following steps:
  332. if (play_state() != Bindings::AnimationPlayState::Idle) {
  333. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm) };
  334. // 1. Run the procedure to reset an animation’s pending tasks on animation.
  335. reset_an_animations_pending_tasks();
  336. // 2. Reject the current finished promise with a DOMException named "AbortError".
  337. auto dom_exception = WebIDL::AbortError::create(realm, "Animation was cancelled"_fly_string);
  338. WebIDL::reject_promise(realm, current_finished_promise(), dom_exception);
  339. // 3. Set the [[PromiseIsHandled]] internal slot of the current finished promise to true.
  340. WebIDL::mark_promise_as_handled(current_finished_promise());
  341. // 4. Let current finished promise be a new promise in the relevant Realm of animation.
  342. m_current_finished_promise = WebIDL::create_promise(realm);
  343. // 5. Create an AnimationPlaybackEvent, cancelEvent.
  344. // 6. Set cancelEvent’s type attribute to cancel.
  345. // 7. Set cancelEvent’s currentTime to null.
  346. // 8. Let timeline time be the current time of the timeline with which animation is associated. If animation is
  347. // not associated with an active timeline, let timeline time be an unresolved time value.
  348. // 9. Set cancelEvent’s timelineTime to timeline time. If timeline time is unresolved, set it to null.
  349. AnimationPlaybackEventInit init;
  350. init.timeline_time = m_timeline && !m_timeline->is_inactive() ? m_timeline->current_time() : Optional<double> {};
  351. auto cancel_event = AnimationPlaybackEvent::create(realm, HTML::EventNames::cancel, init);
  352. // 10. If animation has a document for timing, then append cancelEvent to its document for timing's pending
  353. // animation event queue along with its target, animation. If animation is associated with an active
  354. // timeline that defines a procedure to convert timeline times to origin-relative time, let the scheduled
  355. // event time be the result of applying that procedure to timeline time. Otherwise, the scheduled event time
  356. // is an unresolved time value.
  357. // Otherwise, queue a task to dispatch cancelEvent at animation. The task source for this task is the DOM
  358. // manipulation task source.
  359. if (auto document = document_for_timing()) {
  360. Optional<double> scheduled_event_time;
  361. if (m_timeline && !m_timeline->is_inactive() && m_timeline->can_convert_a_timeline_time_to_an_origin_relative_time())
  362. scheduled_event_time = m_timeline->convert_a_timeline_time_to_an_origin_relative_time(m_timeline->current_time());
  363. document->append_pending_animation_event({ cancel_event, *this, scheduled_event_time });
  364. } else {
  365. HTML::queue_global_task(HTML::Task::Source::DOMManipulation, realm.global_object(), [this, cancel_event]() {
  366. dispatch_event(cancel_event);
  367. });
  368. }
  369. }
  370. // 2. Make animation’s hold time unresolved.
  371. m_hold_time = {};
  372. // 3. Make animation’s start time unresolved.
  373. m_start_time = {};
  374. if (should_invalidate == ShouldInvalidate::Yes)
  375. invalidate_effect();
  376. }
  377. // https://www.w3.org/TR/web-animations-1/#dom-animation-finish
  378. WebIDL::ExceptionOr<void> Animation::finish()
  379. {
  380. // 1. If animation’s effective playback rate is zero, or if animation’s effective playback rate > 0 and associated
  381. // effect end is infinity, throw an "InvalidStateError" DOMException and abort these steps.
  382. auto effective_playback_rate = this->effective_playback_rate();
  383. if (effective_playback_rate == 0.0)
  384. return WebIDL::InvalidStateError::create(realm(), "Animation with a playback rate of 0 cannot be finished"_fly_string);
  385. if (effective_playback_rate > 0.0 && isinf(associated_effect_end()))
  386. return WebIDL::InvalidStateError::create(realm(), "Animation with no end cannot be finished"_fly_string);
  387. // 2. Apply any pending playback rate to animation.
  388. apply_any_pending_playback_rate();
  389. // 3. Set limit as follows:
  390. // -> If playback rate > 0,
  391. // Let limit be associated effect end.
  392. // -> Otherwise,
  393. // Let limit be zero.
  394. auto playback_rate = this->playback_rate();
  395. auto limit = playback_rate > 0.0 ? associated_effect_end() : 0.0;
  396. // 4. Silently set the current time to limit.
  397. TRY(silently_set_current_time(limit));
  398. // 5. If animation’s start time is unresolved and animation has an associated active timeline, let the start time be
  399. // the result of evaluating timeline time - (limit / playback rate) where timeline time is the current time value
  400. // of the associated timeline.
  401. if (!m_start_time.has_value() && m_timeline && !m_timeline->is_inactive())
  402. m_start_time = m_timeline->current_time().value() - (limit / playback_rate);
  403. // 6. If there is a pending pause task and start time is resolved,
  404. auto should_resolve_ready_promise = false;
  405. if (m_pending_pause_task == TaskState::Scheduled && m_start_time.has_value()) {
  406. // 1. Let the hold time be unresolved.
  407. // Note: Typically the hold time will already be unresolved except in the case when the animation was previously
  408. // idle.
  409. m_hold_time = {};
  410. // 2. Cancel the pending pause task.
  411. m_pending_pause_task = TaskState::None;
  412. // 3. Resolve the current ready promise of animation with animation.
  413. should_resolve_ready_promise = true;
  414. }
  415. // 7. If there is a pending play task and start time is resolved, cancel that task and resolve the current ready
  416. // promise of animation with animation.
  417. if (m_pending_play_task == TaskState::Scheduled && m_start_time.has_value()) {
  418. m_pending_play_task = TaskState::None;
  419. should_resolve_ready_promise = true;
  420. }
  421. if (should_resolve_ready_promise) {
  422. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm()) };
  423. WebIDL::resolve_promise(realm(), current_ready_promise(), this);
  424. }
  425. // 8. Run the procedure to update an animation’s finished state for animation with the did seek flag set to true,
  426. // and the synchronously notify flag set to true.
  427. update_finished_state(DidSeek::Yes, SynchronouslyNotify::Yes);
  428. return {};
  429. }
  430. // https://www.w3.org/TR/web-animations-1/#dom-animation-play
  431. WebIDL::ExceptionOr<void> Animation::play()
  432. {
  433. // Begins or resumes playback of the animation by running the procedure to play an animation passing true as the
  434. // value of the auto-rewind flag.
  435. return play_an_animation(AutoRewind::Yes);
  436. }
  437. // https://www.w3.org/TR/web-animations-1/#play-an-animation
  438. WebIDL::ExceptionOr<void> Animation::play_an_animation(AutoRewind auto_rewind)
  439. {
  440. if (auto document = document_for_timing())
  441. document->ensure_animation_timer();
  442. // 1. Let aborted pause be a boolean flag that is true if animation has a pending pause task, and false otherwise.
  443. auto aborted_pause = m_pending_pause_task == TaskState::Scheduled;
  444. // 2. Let has pending ready promise be a boolean flag that is initially false.
  445. auto has_pending_ready_promise = false;
  446. // 3. Let seek time be a time value that is initially unresolved.
  447. Optional<double> seek_time;
  448. // 4. If the auto-rewind flag is true, perform the steps corresponding to the first matching condition from the
  449. // following, if any:
  450. if (auto_rewind == AutoRewind::Yes) {
  451. auto playback_rate = this->playback_rate();
  452. auto current_time = this->current_time();
  453. auto associated_effect_end = this->associated_effect_end();
  454. // -> If animation’s effective playback rate ≥ 0, and animation’s current time is either:
  455. // - unresolved, or
  456. // - less than zero, or
  457. // - greater than or equal to associated effect end,
  458. if (playback_rate >= 0.0 && (!current_time.has_value() || current_time.value() < 0.0 || current_time.value() >= associated_effect_end)) {
  459. // Set seek time to zero.
  460. seek_time = 0.0;
  461. }
  462. // -> If animation’s effective playback rate < 0, and animation’s current time is either:
  463. // - unresolved, or
  464. // - less than or equal to zero, or
  465. // - greater than associated effect end,
  466. else if (playback_rate < 0.0 && (!current_time.has_value() || current_time.value() <= 0.0 || current_time.value() > associated_effect_end)) {
  467. // -> If associated effect end is positive infinity,
  468. if (isinf(associated_effect_end) && associated_effect_end > 0.0) {
  469. // throw an "InvalidStateError" DOMException and abort these steps.
  470. return WebIDL::InvalidStateError::create(realm(), "Cannot rewind an animation with an infinite effect end"_fly_string);
  471. }
  472. // -> Otherwise,
  473. // Set seek time to animation’s associated effect end.
  474. seek_time = associated_effect_end;
  475. }
  476. }
  477. // 5. If the following three conditions are all satisfied:
  478. // - seek time is unresolved, and
  479. // - animation’s start time is unresolved, and
  480. // - animation’s current time is unresolved,
  481. if (!seek_time.has_value() && !m_start_time.has_value() && !current_time().has_value()) {
  482. // set seek time to zero.
  483. seek_time = 0.0;
  484. }
  485. // 6. Let has finite timeline be true if animation has an associated timeline that is not monotonically increasing.
  486. auto has_finite_timeline = m_timeline && !m_timeline->is_monotonically_increasing();
  487. // 7. If seek time is resolved,
  488. if (seek_time.has_value()) {
  489. // -> If has finite timeline is true,
  490. if (has_finite_timeline) {
  491. // 1. Set animation’s start time to seek time.
  492. m_start_time = seek_time;
  493. // 2. Let animation’s hold time be unresolved.
  494. m_hold_time = {};
  495. // 3. Apply any pending playback rate on animation.
  496. apply_any_pending_playback_rate();
  497. }
  498. // Otherwise,
  499. else {
  500. // Set animation’s hold time to seek time.
  501. m_hold_time = seek_time;
  502. }
  503. }
  504. // 8. If animation’s hold time is resolved, let its start time be unresolved.
  505. if (m_hold_time.has_value())
  506. m_start_time = {};
  507. // 9. If animation has a pending play task or a pending pause task,
  508. if (pending()) {
  509. // 1. Cancel that task.
  510. m_pending_play_task = TaskState::None;
  511. m_pending_pause_task = TaskState::None;
  512. // 2. Set has pending ready promise to true.
  513. has_pending_ready_promise = true;
  514. }
  515. // 10. If the following four conditions are all satisfied:
  516. // - animation’s hold time is unresolved, and
  517. // - seek time is unresolved, and
  518. // - aborted pause is false, and
  519. // - animation does not have a pending playback rate,
  520. if (!m_hold_time.has_value() && !seek_time.has_value() && !aborted_pause && !m_pending_playback_rate.has_value()) {
  521. // abort this procedure.
  522. return {};
  523. }
  524. // 11. If has pending ready promise is false, let animation’s current ready promise be a new promise in the relevant
  525. // Realm of animation.
  526. if (!has_pending_ready_promise)
  527. m_current_ready_promise = WebIDL::create_promise(realm());
  528. // 12. Schedule a task to run as soon as animation is ready. The task shall perform the following steps:
  529. //
  530. // Note: Steps omitted, set run_pending_play_task()
  531. //
  532. // So long as the above task is scheduled but has yet to run, animation is described as having a pending play
  533. // task. While the task is running, however, animation does not have a pending play task.
  534. //
  535. // If a user agent determines that animation is immediately ready, it may schedule the above task as a microtask
  536. // such that it runs at the next microtask checkpoint, but it must not perform the task synchronously.
  537. m_pending_play_task = TaskState::Scheduled;
  538. // 13. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  539. // and the synchronously notify flag set to false.
  540. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  541. return {};
  542. }
  543. // https://www.w3.org/TR/web-animations-1/#dom-animation-pause
  544. WebIDL::ExceptionOr<void> Animation::pause()
  545. {
  546. // 1. If animation has a pending pause task, abort these steps.
  547. if (m_pending_pause_task == TaskState::Scheduled)
  548. return {};
  549. // 2. If the play state of animation is paused, abort these steps.
  550. if (play_state() == Bindings::AnimationPlayState::Paused)
  551. return {};
  552. // 3. Let seek time be a time value that is initially unresolved.
  553. Optional<double> seek_time;
  554. // 4. Let has finite timeline be true if animation has an associated timeline that is not monotonically increasing.
  555. auto has_finite_timeline = m_timeline && !m_timeline->is_monotonically_increasing();
  556. // 5. If the animation’s current time is unresolved, perform the steps according to the first matching condition
  557. // from below:
  558. if (!current_time().has_value()) {
  559. // -> If animation’s playback rate is ≥ 0,
  560. if (playback_rate() >= 0.0) {
  561. // Set seek time to zero.
  562. seek_time = 0.0;
  563. }
  564. // -> Otherwise
  565. else {
  566. // If associated effect end for animation is positive infinity,
  567. auto associated_effect_end = this->associated_effect_end();
  568. if (isinf(associated_effect_end) && associated_effect_end > 0.0) {
  569. // throw an "InvalidStateError" DOMException and abort these steps.
  570. return WebIDL::InvalidStateError::create(realm(), "Cannot pause an animation with an infinite effect end"_fly_string);
  571. }
  572. // Otherwise,
  573. // Set seek time to animation’s associated effect end.
  574. seek_time = associated_effect_end;
  575. }
  576. }
  577. // 6. If seek time is resolved,
  578. if (seek_time.has_value()) {
  579. // If has finite timeline is true,
  580. if (has_finite_timeline) {
  581. // Set animation’s start time to seek time.
  582. m_start_time = seek_time;
  583. }
  584. // Otherwise,
  585. else {
  586. // Set animation’s hold time to seek time.
  587. m_hold_time = seek_time;
  588. }
  589. }
  590. // 7. Let has pending ready promise be a boolean flag that is initially false.
  591. auto has_pending_ready_promise = false;
  592. // 8. If animation has a pending play task, cancel that task and let has pending ready promise be true.
  593. if (m_pending_play_task == TaskState::Scheduled) {
  594. m_pending_pause_task = TaskState::None;
  595. has_pending_ready_promise = true;
  596. }
  597. // 9. If has pending ready promise is false, set animation’s current ready promise to a new promise in the relevant
  598. // Realm of animation.
  599. if (!has_pending_ready_promise)
  600. m_current_ready_promise = WebIDL::create_promise(realm());
  601. // 10. Schedule a task to be executed at the first possible moment where both of the following conditions are true:
  602. // - the user agent has performed any processing necessary to suspend the playback of animation’s associated
  603. // effect, if any.
  604. // - the animation is associated with a timeline that is not inactive.
  605. //
  606. // Note: This is run_pending_pause_task()
  607. m_pending_pause_task = TaskState::Scheduled;
  608. // 11. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  609. // and the synchronously notify flag set to false.
  610. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  611. return {};
  612. }
  613. // https://www.w3.org/TR/web-animations-1/#dom-animation-updateplaybackrate
  614. WebIDL::ExceptionOr<void> Animation::update_playback_rate(double new_playback_rate)
  615. {
  616. // 1. Let previous play state be animation’s play state.
  617. // Note: It is necessary to record the play state before updating animation’s effective playback rate since, in the
  618. // following logic, we want to immediately apply the pending playback rate of animation if it is currently
  619. // finished regardless of whether or not it will still be finished after we apply the pending playback rate.
  620. auto previous_play_state = play_state();
  621. // 2. Let animation’s pending playback rate be new playback rate.
  622. m_pending_playback_rate = new_playback_rate;
  623. // 3. Perform the steps corresponding to the first matching condition from below:
  624. // -> If animation has a pending play task or a pending pause task,
  625. if (pending()) {
  626. // Abort these steps.
  627. // Note: The different types of pending tasks will apply the pending playback rate when they run so there is no
  628. // further action required in this case.
  629. return {};
  630. }
  631. // -> If previous play state is idle or paused, or animation’s current time is unresolved,
  632. if (previous_play_state == Bindings::AnimationPlayState::Idle || previous_play_state == Bindings::AnimationPlayState::Paused || !current_time().has_value()) {
  633. // Apply any pending playback rate on animation.
  634. // Note: the second condition above is required so that if we have a running animation with an unresolved
  635. // current time and no pending play task, we do not attempt to play it below.
  636. apply_any_pending_playback_rate();
  637. }
  638. // -> If previous play state is finished,
  639. else if (previous_play_state == Bindings::AnimationPlayState::Finished) {
  640. // 1. Let the unconstrained current time be the result of calculating the current time of animation
  641. // substituting an unresolved time value for the hold time.
  642. Optional<double> unconstrained_current_time;
  643. {
  644. TemporaryChange change(m_hold_time, {});
  645. unconstrained_current_time = current_time();
  646. }
  647. // 2. Let animation’s start time be the result of evaluating the following expression:
  648. // timeline time - (unconstrained current time / pending playback rate)
  649. // Where timeline time is the current time value of the timeline associated with animation.
  650. // If pending playback rate is zero, let animation’s start time be timeline time.
  651. if (m_pending_playback_rate.value() == 0.0) {
  652. m_start_time = m_timeline->current_time().value();
  653. } else {
  654. m_start_time = m_timeline->current_time().value() - (unconstrained_current_time.value() / m_pending_playback_rate.value());
  655. }
  656. // 3. Apply any pending playback rate on animation.
  657. apply_any_pending_playback_rate();
  658. // 4. Run the procedure to update an animation’s finished state for animation with the did seek flag set to
  659. // false, and the synchronously notify flag set to false.
  660. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  661. }
  662. // -> Otherwise,
  663. else {
  664. // Run the procedure to play an animation for animation with the auto-rewind flag set to false.
  665. TRY(play_an_animation(AutoRewind::No));
  666. }
  667. return {};
  668. }
  669. // https://www.w3.org/TR/web-animations-1/#dom-animation-reverse
  670. WebIDL::ExceptionOr<void> Animation::reverse()
  671. {
  672. auto& realm = this->realm();
  673. // 1. If there is no timeline associated with animation, or the associated timeline is inactive throw an
  674. // "InvalidStateError" DOMException and abort these steps.
  675. if (!m_timeline || m_timeline->is_inactive())
  676. return WebIDL::InvalidStateError::create(realm, "Cannot reverse an animation with an inactive timeline"_fly_string);
  677. // 2. Let original pending playback rate be animation’s pending playback rate.
  678. auto original_pending_playback_rate = m_pending_playback_rate;
  679. // 3. Let animation’s pending playback rate be the additive inverse of its effective playback rate (i.e.
  680. // -effective playback rate).
  681. m_pending_playback_rate = -effective_playback_rate();
  682. // 4. Run the steps to play an animation for animation with the auto-rewind flag set to true.
  683. // If the steps to play an animation throw an exception, set animation’s pending playback rate to original
  684. // pending playback rate and propagate the exception.
  685. auto result = play_an_animation(AutoRewind::Yes);
  686. if (result.is_error()) {
  687. m_pending_playback_rate = original_pending_playback_rate;
  688. return result;
  689. }
  690. return {};
  691. }
  692. // https://www.w3.org/TR/web-animations-1/#dom-animation-persist
  693. void Animation::persist()
  694. {
  695. // Sets this animation’s replace state to persisted.
  696. set_replace_state(Bindings::AnimationReplaceState::Persisted);
  697. }
  698. // https://www.w3.org/TR/web-animations-1/#animation-time-to-timeline-time
  699. Optional<double> Animation::convert_an_animation_time_to_timeline_time(Optional<double> time) const
  700. {
  701. // 1. If time is unresolved, return time.
  702. if (!time.has_value())
  703. return time;
  704. // 2. If time is infinity, return an unresolved time value.
  705. if (isinf(time.value()))
  706. return {};
  707. // 3. If animation’s playback rate is zero, return an unresolved time value.
  708. if (m_playback_rate == 0.0)
  709. return {};
  710. // 4. If animation’s start time is unresolved, return an unresolved time value.
  711. if (!m_start_time.has_value())
  712. return {};
  713. // 5. Return the result of calculating: time × (1 / playback rate) + start time (where playback rate and start time
  714. // are the playback rate and start time of animation, respectively).
  715. return (time.value() * (1.0 / m_playback_rate)) + m_start_time.value();
  716. }
  717. // https://www.w3.org/TR/web-animations-1/#animation-time-to-origin-relative-time
  718. Optional<double> Animation::convert_a_timeline_time_to_an_origin_relative_time(Optional<double> time) const
  719. {
  720. // 1. Let timeline time be the result of converting time from an animation time to a timeline time.
  721. auto timeline_time = convert_an_animation_time_to_timeline_time(time);
  722. // 2. If timeline time is unresolved, return time.
  723. if (!timeline_time.has_value())
  724. return time;
  725. // 3. If animation is not associated with a timeline, return an unresolved time value.
  726. if (!m_timeline)
  727. return {};
  728. // 4. If animation is associated with an inactive timeline, return an unresolved time value.
  729. if (m_timeline->is_inactive())
  730. return {};
  731. // 5. If there is no procedure to convert a timeline time to an origin-relative time for the timeline associated
  732. // with animation, return an unresolved time value.
  733. if (!m_timeline->can_convert_a_timeline_time_to_an_origin_relative_time())
  734. return {};
  735. // 6. Return the result of converting timeline time to an origin-relative time using the procedure defined for the
  736. // timeline associated with animation.
  737. return m_timeline->convert_a_timeline_time_to_an_origin_relative_time(timeline_time);
  738. }
  739. // https://www.w3.org/TR/web-animations-1/#animation-document-for-timing
  740. JS::GCPtr<DOM::Document> Animation::document_for_timing() const
  741. {
  742. // An animation’s document for timing is the Document with which its timeline is associated. If an animation is not
  743. // associated with a timeline, or its timeline is not associated with a document, then it has no document for
  744. // timing.
  745. if (!m_timeline)
  746. return {};
  747. return m_timeline->associated_document();
  748. }
  749. void Animation::notify_timeline_time_did_change()
  750. {
  751. update_finished_state(DidSeek::No, SynchronouslyNotify::Yes);
  752. // Act on the pending play or pause task
  753. if (m_pending_play_task == TaskState::Scheduled) {
  754. m_pending_play_task = TaskState::None;
  755. run_pending_play_task();
  756. }
  757. if (m_pending_pause_task == TaskState::Scheduled) {
  758. m_pending_pause_task = TaskState::None;
  759. run_pending_pause_task();
  760. }
  761. }
  762. void Animation::effect_timing_changed(Badge<AnimationEffect>)
  763. {
  764. update_finished_state(DidSeek::No, SynchronouslyNotify::Yes);
  765. }
  766. // https://www.w3.org/TR/web-animations-1/#associated-effect-end
  767. double Animation::associated_effect_end() const
  768. {
  769. // The associated effect end of an animation is equal to the end time of the animation’s associated effect. If the
  770. // animation has no associated effect, the associated effect end is zero.
  771. return m_effect ? m_effect->end_time() : 0.0;
  772. }
  773. // https://www.w3.org/TR/web-animations-1/#effective-playback-rate
  774. double Animation::effective_playback_rate() const
  775. {
  776. // The effective playback rate of an animation is its pending playback rate, if set, otherwise it is the animation’s
  777. // playback rate.
  778. return m_pending_playback_rate.has_value() ? m_pending_playback_rate.value() : m_playback_rate;
  779. }
  780. // https://www.w3.org/TR/web-animations-1/#apply-any-pending-playback-rate
  781. void Animation::apply_any_pending_playback_rate()
  782. {
  783. // 1. If animation does not have a pending playback rate, abort these steps.
  784. if (!m_pending_playback_rate.has_value())
  785. return;
  786. // 2. Set animation’s playback rate to its pending playback rate.
  787. m_playback_rate = m_pending_playback_rate.value();
  788. // 3. Clear animation’s pending playback rate.
  789. m_pending_playback_rate = {};
  790. }
  791. // https://www.w3.org/TR/web-animations-1/#animation-silently-set-the-current-time
  792. WebIDL::ExceptionOr<void> Animation::silently_set_current_time(Optional<double> seek_time)
  793. {
  794. // 1. If seek time is an unresolved time value, then perform the following steps.
  795. if (!seek_time.has_value()) {
  796. // 1. If the current time is resolved, then throw a TypeError.
  797. if (current_time().has_value()) {
  798. return WebIDL::SimpleException {
  799. WebIDL::SimpleExceptionType::TypeError,
  800. "Cannot change an animation's current time from a resolve value to an unresolved value"sv
  801. };
  802. }
  803. // 2. Abort these steps.
  804. return {};
  805. }
  806. // 2. Update either animation’s hold time or start time as follows:
  807. // -> If any of the following conditions are true:
  808. // - animation’s hold time is resolved, or
  809. // - animation’s start time is unresolved, or
  810. // - animation has no associated timeline or the associated timeline is inactive, or
  811. // - animation’s playback rate is 0,
  812. if (m_hold_time.has_value() || !m_start_time.has_value() || !m_timeline || m_timeline->is_inactive() || m_playback_rate == 0.0) {
  813. // Set animation’s hold time to seek time.
  814. m_hold_time = seek_time;
  815. }
  816. // -> Otherwise,
  817. else {
  818. // Set animation’s start time to the result of evaluating timeline time - (seek time / playback rate) where
  819. // timeline time is the current time value of timeline associated with animation.
  820. m_start_time = m_timeline->current_time().value() - (seek_time.value() / m_playback_rate);
  821. }
  822. // 3. If animation has no associated timeline or the associated timeline is inactive, make animation’s start time
  823. // unresolved.
  824. if (!m_timeline || m_timeline->is_inactive())
  825. m_start_time = {};
  826. // 4. Make animation’s previous current time unresolved.
  827. m_previous_current_time = {};
  828. return {};
  829. }
  830. // https://www.w3.org/TR/web-animations-1/#update-an-animations-finished-state
  831. void Animation::update_finished_state(DidSeek did_seek, SynchronouslyNotify synchronously_notify)
  832. {
  833. auto& realm = this->realm();
  834. // 1. Let the unconstrained current time be the result of calculating the current time substituting an unresolved
  835. // time value for the hold time if did seek is false. If did seek is true, the unconstrained current time is
  836. // equal to the current time.
  837. //
  838. // Note: This is required to accommodate timelines that may change direction. Without this definition, a once-
  839. // finished animation would remain finished even when its timeline progresses in the opposite direction.
  840. Optional<double> unconstrained_current_time;
  841. if (did_seek == DidSeek::No) {
  842. TemporaryChange change(m_hold_time, {});
  843. unconstrained_current_time = current_time();
  844. } else {
  845. unconstrained_current_time = current_time();
  846. }
  847. // 2. If all three of the following conditions are true,
  848. // - the unconstrained current time is resolved, and
  849. // - animation’s start time is resolved, and
  850. // - animation does not have a pending play task or a pending pause task,
  851. if (unconstrained_current_time.has_value() && m_start_time.has_value() && !pending()) {
  852. // then update animation’s hold time based on the first matching condition for animation from below, if any:
  853. // -> If playback rate > 0 and unconstrained current time is greater than or equal to associated effect end,
  854. auto associated_effect_end = this->associated_effect_end();
  855. if (m_playback_rate > 0.0 && unconstrained_current_time.value() >= associated_effect_end) {
  856. // If did seek is true, let the hold time be the value of unconstrained current time.
  857. if (did_seek == DidSeek::Yes) {
  858. m_hold_time = unconstrained_current_time;
  859. }
  860. // If did seek is false, let the hold time be the maximum value of previous current time and associated
  861. // effect end. If the previous current time is unresolved, let the hold time be associated effect end.
  862. else if (m_previous_current_time.has_value()) {
  863. m_hold_time = max(m_previous_current_time.value(), associated_effect_end);
  864. } else {
  865. m_hold_time = associated_effect_end;
  866. }
  867. }
  868. // -> If playback rate < 0 and unconstrained current time is less than or equal to 0,
  869. else if (m_playback_rate < 0.0 && unconstrained_current_time.value() <= 0.0) {
  870. // If did seek is true, let the hold time be the value of unconstrained current time.
  871. if (did_seek == DidSeek::Yes) {
  872. m_hold_time = unconstrained_current_time;
  873. }
  874. // If did seek is false, let the hold time be the minimum value of previous current time and zero. If the
  875. // previous current time is unresolved, let the hold time be zero.
  876. else if (m_previous_current_time.has_value()) {
  877. m_hold_time = min(m_previous_current_time.value(), 0.0);
  878. } else {
  879. m_hold_time = 0.0;
  880. }
  881. }
  882. // -> If playback rate ≠ 0, and animation is associated with an active timeline,
  883. else if (m_playback_rate != 0.0 && m_timeline && !m_timeline->is_inactive()) {
  884. // Perform the following steps:
  885. // 1. If did seek is true and the hold time is resolved, let animation’s start time be equal to the result
  886. // of evaluating timeline time - (hold time / playback rate) where timeline time is the current time
  887. // value of timeline associated with animation.
  888. if (did_seek == DidSeek::Yes && m_hold_time.has_value())
  889. m_start_time = m_timeline->current_time().value() - (m_hold_time.value() / m_playback_rate);
  890. // 2. Let the hold time be unresolved.
  891. m_hold_time = {};
  892. }
  893. }
  894. // 3. Set the previous current time of animation be the result of calculating its current time.
  895. m_previous_current_time = current_time();
  896. // 4. Let current finished state be true if the play state of animation is finished. Otherwise, let it be false.
  897. auto current_finished_state = play_state() == Bindings::AnimationPlayState::Finished;
  898. // 5. If current finished state is true and the current finished promise is not yet resolved, perform the following
  899. // steps:
  900. if (current_finished_state && !m_is_finished) {
  901. // 1. Let finish notification steps refer to the following procedure:
  902. JS::SafeFunction<void()> finish_notification_steps = [&]() {
  903. // 1. If animation’s play state is not equal to finished, abort these steps.
  904. if (play_state() != Bindings::AnimationPlayState::Finished)
  905. return;
  906. // 2. Resolve animation’s current finished promise object with animation.
  907. {
  908. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm) };
  909. WebIDL::resolve_promise(realm, current_finished_promise(), this);
  910. }
  911. m_is_finished = true;
  912. // 3. Create an AnimationPlaybackEvent, finishEvent.
  913. // 4. Set finishEvent’s type attribute to finish.
  914. // 5. Set finishEvent’s currentTime attribute to the current time of animation.
  915. AnimationPlaybackEventInit init;
  916. init.current_time = current_time();
  917. auto finish_event = AnimationPlaybackEvent::create(realm, HTML::EventNames::finish, init);
  918. // 6. Set finishEvent’s timelineTime attribute to the current time of the timeline with which animation is
  919. // associated. If animation is not associated with a timeline, or the timeline is inactive, let
  920. // timelineTime be null.
  921. if (m_timeline && !m_timeline->is_inactive())
  922. finish_event->set_timeline_time(m_timeline->current_time());
  923. else
  924. finish_event->set_timeline_time({});
  925. // 7. If animation has a document for timing, then append finishEvent to its document for timing's pending
  926. // animation event queue along with its target, animation. For the scheduled event time, use the result
  927. // of converting animation’s associated effect end to an origin-relative time.
  928. if (auto document_for_timing = this->document_for_timing()) {
  929. document_for_timing->append_pending_animation_event({ .event = finish_event,
  930. .target = *this,
  931. .scheduled_event_time = convert_a_timeline_time_to_an_origin_relative_time(associated_effect_end()) });
  932. }
  933. // Otherwise, queue a task to dispatch finishEvent at animation. The task source for this task is the DOM
  934. // manipulation task source.
  935. else {
  936. // Manually create a task so its ID can be saved
  937. auto& document = verify_cast<HTML::Window>(realm.global_object()).associated_document();
  938. auto task = HTML::Task::create(HTML::Task::Source::DOMManipulation, &document, [this, finish_event]() {
  939. dispatch_event(finish_event);
  940. });
  941. m_pending_finish_microtask_id = task->id();
  942. HTML::main_thread_event_loop().task_queue().add(move(task));
  943. }
  944. };
  945. // 2. If synchronously notify is true, cancel any queued microtask to run the finish notification steps for this
  946. // animation, and run the finish notification steps immediately.
  947. if (synchronously_notify == SynchronouslyNotify::Yes) {
  948. if (m_pending_finish_microtask_id.has_value()) {
  949. HTML::main_thread_event_loop().task_queue().remove_tasks_matching([id = move(m_pending_finish_microtask_id)](auto const& task) {
  950. return task.id() == id;
  951. });
  952. }
  953. finish_notification_steps();
  954. }
  955. // Otherwise, if synchronously notify is false, queue a microtask to run finish notification steps for
  956. // animation unless there is already a microtask queued to run those steps for animation.
  957. else if (!m_pending_finish_microtask_id.has_value()) {
  958. auto& document = verify_cast<HTML::Window>(realm.global_object()).associated_document();
  959. auto task = HTML::Task::create(HTML::Task::Source::DOMManipulation, &document, move(finish_notification_steps));
  960. m_pending_finish_microtask_id = task->id();
  961. HTML::main_thread_event_loop().task_queue().add(move(task));
  962. }
  963. }
  964. // 6. If current finished state is false and animation’s current finished promise is already resolved, set
  965. // animation’s current finished promise to a new promise in the relevant Realm of animation.
  966. if (!current_finished_state && m_is_finished) {
  967. {
  968. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm) };
  969. m_current_finished_promise = WebIDL::create_promise(realm);
  970. }
  971. m_is_finished = false;
  972. }
  973. invalidate_effect();
  974. }
  975. // https://www.w3.org/TR/web-animations-1/#animation-reset-an-animations-pending-tasks
  976. void Animation::reset_an_animations_pending_tasks()
  977. {
  978. auto& realm = this->realm();
  979. // 1. If animation does not have a pending play task or a pending pause task, abort this procedure.
  980. if (!pending())
  981. return;
  982. // 2. If animation has a pending play task, cancel that task.
  983. m_pending_play_task = TaskState::None;
  984. // 3. If animation has a pending pause task, cancel that task.
  985. m_pending_pause_task = TaskState::None;
  986. // 4. Apply any pending playback rate on animation.
  987. apply_any_pending_playback_rate();
  988. // 5. Reject animation’s current ready promise with a DOMException named "AbortError".
  989. auto dom_exception = WebIDL::AbortError::create(realm, "Animation was cancelled"_fly_string);
  990. WebIDL::reject_promise(realm, current_ready_promise(), dom_exception);
  991. // 6. Set the [[PromiseIsHandled]] internal slot of animation’s current ready promise to true.
  992. WebIDL::mark_promise_as_handled(current_ready_promise());
  993. // 7. Let animation’s current ready promise be the result of creating a new resolved Promise object with value
  994. // animation in the relevant Realm of animation.
  995. m_current_ready_promise = WebIDL::create_resolved_promise(realm, this);
  996. }
  997. // Step 12 of https://www.w3.org/TR/web-animations-1/#playing-an-animation-section
  998. void Animation::run_pending_play_task()
  999. {
  1000. // 1. Assert that at least one of animation’s start time or hold time is resolved.
  1001. VERIFY(m_start_time.has_value() || m_hold_time.has_value());
  1002. // 2. Let ready time be the time value of the timeline associated with animation at the moment when animation became
  1003. // ready.
  1004. // FIXME: Ideally we would save the time before the update_finished_state call in play_an_animation() and use that
  1005. // as the ready time, as step 2 indicates, however at that point the timeline will not have had it's current
  1006. // time updated by the Document, so the time we would save would be incorrect if there are no other
  1007. // animations running.
  1008. auto ready_time = m_timeline->current_time();
  1009. // Note: The timeline being active is a precondition for this method to be called
  1010. VERIFY(ready_time.has_value());
  1011. // 3. Perform the steps corresponding to the first matching condition below, if any:
  1012. // -> If animation’s hold time is resolved,
  1013. if (m_hold_time.has_value()) {
  1014. // 1. Apply any pending playback rate on animation.
  1015. apply_any_pending_playback_rate();
  1016. // 2. Let new start time be the result of evaluating ready time - hold time / playback rate for animation. If
  1017. // the playback rate is zero, let new start time be simply ready time.
  1018. auto new_start_time = m_playback_rate != 0.0 ? ready_time.value() - (m_hold_time.value() / m_playback_rate) : ready_time;
  1019. // 3. Set the start time of animation to new start time.
  1020. m_start_time = new_start_time;
  1021. // 4. If animation’s playback rate is not 0, make animation’s hold time unresolved.
  1022. if (m_playback_rate != 0.0)
  1023. m_hold_time = {};
  1024. }
  1025. // -> If animation’s start time is resolved and animation has a pending playback rate,
  1026. else if (m_start_time.has_value() && m_pending_playback_rate.has_value()) {
  1027. // 1. Let current time to match be the result of evaluating (ready time - start time) × playback rate for
  1028. // animation.
  1029. auto current_time_to_match = (ready_time.value() - m_start_time.value()) * m_playback_rate;
  1030. // 2. Apply any pending playback rate on animation.
  1031. apply_any_pending_playback_rate();
  1032. // 3. If animation’s playback rate is zero, let animation’s hold time be current time to match.
  1033. if (m_playback_rate == 0.0)
  1034. m_hold_time = current_time_to_match;
  1035. // 4. Let new start time be the result of evaluating ready time - current time to match / playback rate for
  1036. // animation. If the playback rate is zero, let new start time be simply ready time.
  1037. auto new_start_time = m_playback_rate != 0.0 ? ready_time.value() - (current_time_to_match / m_playback_rate) : ready_time;
  1038. // 5. Set the start time of animation to new start time.
  1039. m_start_time = new_start_time;
  1040. }
  1041. // 4. Resolve animation’s current ready promise with animation.
  1042. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm()) };
  1043. WebIDL::resolve_promise(realm(), current_ready_promise(), this);
  1044. // 5. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  1045. // and the synchronously notify flag set to false.
  1046. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  1047. }
  1048. // Step 10 of https://www.w3.org/TR/web-animations-1/#pause-an-animation
  1049. void Animation::run_pending_pause_task()
  1050. {
  1051. // 1. Let ready time be the time value of the timeline associated with animation at the moment when the user agent
  1052. // completed processing necessary to suspend playback of animation’s associated effect.
  1053. // FIXME: Ideally we would save the time before the update_finished_state call in pause_an_animation() and use that
  1054. // as the ready time, as step 1 indicates, however at that point the timeline will not have had it's current
  1055. // time updated by the Document, so the time we would save would be incorrect if there are no other
  1056. // animations running.
  1057. auto ready_time = m_timeline->current_time();
  1058. // Note: The timeline being active is a precondition for this method to be called
  1059. VERIFY(ready_time.has_value());
  1060. // 2. If animation’s start time is resolved and its hold time is not resolved, let animation’s hold time be the
  1061. // result of evaluating (ready time - start time) × playback rate.
  1062. // Note: The hold time might be already set if the animation is finished, or if the animation has a pending play
  1063. // task. In either case we want to preserve the hold time as we enter the paused state.
  1064. if (m_start_time.has_value() && !m_hold_time.has_value())
  1065. m_hold_time = (ready_time.value() - m_start_time.value()) * m_playback_rate;
  1066. // 3. Apply any pending playback rate on animation.
  1067. apply_any_pending_playback_rate();
  1068. // 4. Make animation’s start time unresolved.
  1069. m_start_time = {};
  1070. // 5. Resolve animation’s current ready promise with animation.
  1071. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm()) };
  1072. WebIDL::resolve_promise(realm(), current_ready_promise(), this);
  1073. // 6. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  1074. // and the synchronously notify flag set to false.
  1075. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  1076. }
  1077. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_ready_promise() const
  1078. {
  1079. if (!m_current_ready_promise) {
  1080. // The current ready promise is initially a resolved Promise created using the procedure to create a new
  1081. // resolved Promise with the animation itself as its value and created in the relevant Realm of the animation.
  1082. m_current_ready_promise = WebIDL::create_resolved_promise(realm(), this);
  1083. }
  1084. return *m_current_ready_promise;
  1085. }
  1086. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_finished_promise() const
  1087. {
  1088. if (!m_current_finished_promise) {
  1089. // The current finished promise is initially a pending Promise object.
  1090. m_current_finished_promise = WebIDL::create_promise(realm());
  1091. }
  1092. return *m_current_finished_promise;
  1093. }
  1094. void Animation::invalidate_effect()
  1095. {
  1096. if (m_effect) {
  1097. if (auto target = m_effect->target())
  1098. target->invalidate_style();
  1099. }
  1100. }
  1101. Animation::Animation(JS::Realm& realm)
  1102. : DOM::EventTarget(realm)
  1103. {
  1104. static unsigned int next_animation_list_order = 0;
  1105. m_global_animation_list_order = next_animation_list_order++;
  1106. }
  1107. void Animation::initialize(JS::Realm& realm)
  1108. {
  1109. Base::initialize(realm);
  1110. set_prototype(&Bindings::ensure_web_prototype<Bindings::AnimationPrototype>(realm, "Animation"_fly_string));
  1111. }
  1112. void Animation::visit_edges(Cell::Visitor& visitor)
  1113. {
  1114. Base::visit_edges(visitor);
  1115. visitor.visit(m_effect);
  1116. visitor.visit(m_timeline);
  1117. visitor.visit(m_current_ready_promise);
  1118. visitor.visit(m_current_finished_promise);
  1119. }
  1120. }