Animation.cpp 62 KB

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