Animation.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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_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. m_replace_state = value;
  295. if (value == Bindings::AnimationReplaceState::Removed) {
  296. // Remove the associated effect from its target, if applicable
  297. if (m_effect && m_effect->target())
  298. m_effect->target()->disassociate_with_animation(*this);
  299. // Remove this animation from its timeline
  300. m_timeline->disassociate_with_animation(*this);
  301. }
  302. }
  303. // https://www.w3.org/TR/web-animations-1/#dom-animation-onfinish
  304. JS::GCPtr<WebIDL::CallbackType> Animation::onfinish()
  305. {
  306. return event_handler_attribute(HTML::EventNames::finish);
  307. }
  308. // https://www.w3.org/TR/web-animations-1/#dom-animation-onfinish
  309. void Animation::set_onfinish(JS::GCPtr<WebIDL::CallbackType> event_handler)
  310. {
  311. set_event_handler_attribute(HTML::EventNames::finish, event_handler);
  312. }
  313. // https://www.w3.org/TR/web-animations-1/#dom-animation-oncancel
  314. JS::GCPtr<WebIDL::CallbackType> Animation::oncancel()
  315. {
  316. return event_handler_attribute(HTML::EventNames::cancel);
  317. }
  318. // https://www.w3.org/TR/web-animations-1/#dom-animation-oncancel
  319. void Animation::set_oncancel(JS::GCPtr<WebIDL::CallbackType> event_handler)
  320. {
  321. set_event_handler_attribute(HTML::EventNames::cancel, event_handler);
  322. }
  323. // https://www.w3.org/TR/web-animations-1/#dom-animation-onremove
  324. JS::GCPtr<WebIDL::CallbackType> Animation::onremove()
  325. {
  326. return event_handler_attribute(HTML::EventNames::remove);
  327. }
  328. // https://www.w3.org/TR/web-animations-1/#dom-animation-onremove
  329. void Animation::set_onremove(JS::GCPtr<WebIDL::CallbackType> event_handler)
  330. {
  331. set_event_handler_attribute(HTML::EventNames::remove, event_handler);
  332. }
  333. // https://www.w3.org/TR/web-animations-1/#dom-animation-cancel
  334. void Animation::cancel(ShouldInvalidate should_invalidate)
  335. {
  336. // Note: When called from JS, we always want to invalidate the animation target's style. However, this method is
  337. // also called from the StyleComputer when the animation-name CSS property changes. That happens in the
  338. // middle of a cascade, and importantly, _before_ computing the animation effect stack, so there is no
  339. // need for another invalidation. And in fact, if we did invalidate, it would lead to a crash, as the element
  340. // would not have it's "m_needs_style_update" flag cleared.
  341. auto& realm = this->realm();
  342. // 1. If animation’s play state is not idle, perform the following steps:
  343. if (play_state() != Bindings::AnimationPlayState::Idle) {
  344. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm) };
  345. // 1. Run the procedure to reset an animation’s pending tasks on animation.
  346. reset_an_animations_pending_tasks();
  347. // 2. Reject the current finished promise with a DOMException named "AbortError".
  348. auto dom_exception = WebIDL::AbortError::create(realm, "Animation was cancelled"_fly_string);
  349. WebIDL::reject_promise(realm, current_finished_promise(), dom_exception);
  350. // 3. Set the [[PromiseIsHandled]] internal slot of the current finished promise to true.
  351. WebIDL::mark_promise_as_handled(current_finished_promise());
  352. // 4. Let current finished promise be a new promise in the relevant Realm of animation.
  353. m_current_finished_promise = WebIDL::create_promise(realm);
  354. m_is_finished = false;
  355. // 5. Create an AnimationPlaybackEvent, cancelEvent.
  356. // 6. Set cancelEvent’s type attribute to cancel.
  357. // 7. Set cancelEvent’s currentTime to null.
  358. // 8. Let timeline time be the current time of the timeline with which animation is associated. If animation is
  359. // not associated with an active timeline, let timeline time be an unresolved time value.
  360. // 9. Set cancelEvent’s timelineTime to timeline time. If timeline time is unresolved, set it to null.
  361. AnimationPlaybackEventInit init;
  362. init.timeline_time = m_timeline && !m_timeline->is_inactive() ? m_timeline->current_time() : Optional<double> {};
  363. auto cancel_event = AnimationPlaybackEvent::create(realm, HTML::EventNames::cancel, init);
  364. // 10. If animation has a document for timing, then append cancelEvent to its document for timing's pending
  365. // animation event queue along with its target, animation. If animation is associated with an active
  366. // timeline that defines a procedure to convert timeline times to origin-relative time, let the scheduled
  367. // event time be the result of applying that procedure to timeline time. Otherwise, the scheduled event time
  368. // is an unresolved time value.
  369. // Otherwise, queue a task to dispatch cancelEvent at animation. The task source for this task is the DOM
  370. // manipulation task source.
  371. if (auto document = document_for_timing()) {
  372. Optional<double> scheduled_event_time;
  373. if (m_timeline && !m_timeline->is_inactive() && m_timeline->can_convert_a_timeline_time_to_an_origin_relative_time())
  374. scheduled_event_time = m_timeline->convert_a_timeline_time_to_an_origin_relative_time(m_timeline->current_time());
  375. document->append_pending_animation_event({ cancel_event, *this, *this, scheduled_event_time });
  376. } else {
  377. HTML::queue_global_task(HTML::Task::Source::DOMManipulation, realm.global_object(), JS::create_heap_function(heap(), [this, cancel_event]() {
  378. dispatch_event(cancel_event);
  379. }));
  380. }
  381. }
  382. // 2. Make animation’s hold time unresolved.
  383. m_hold_time = {};
  384. // 3. Make animation’s start time unresolved.
  385. m_start_time = {};
  386. if (should_invalidate == ShouldInvalidate::Yes)
  387. invalidate_effect();
  388. }
  389. // https://www.w3.org/TR/web-animations-1/#dom-animation-finish
  390. WebIDL::ExceptionOr<void> Animation::finish()
  391. {
  392. // 1. If animation’s effective playback rate is zero, or if animation’s effective playback rate > 0 and associated
  393. // effect end is infinity, throw an "InvalidStateError" DOMException and abort these steps.
  394. auto effective_playback_rate = this->effective_playback_rate();
  395. if (effective_playback_rate == 0.0)
  396. return WebIDL::InvalidStateError::create(realm(), "Animation with a playback rate of 0 cannot be finished"_fly_string);
  397. if (effective_playback_rate > 0.0 && isinf(associated_effect_end()))
  398. return WebIDL::InvalidStateError::create(realm(), "Animation with no end cannot be finished"_fly_string);
  399. // 2. Apply any pending playback rate to animation.
  400. apply_any_pending_playback_rate();
  401. // 3. Set limit as follows:
  402. // -> If playback rate > 0,
  403. // Let limit be associated effect end.
  404. // -> Otherwise,
  405. // Let limit be zero.
  406. auto playback_rate = this->playback_rate();
  407. auto limit = playback_rate > 0.0 ? associated_effect_end() : 0.0;
  408. // 4. Silently set the current time to limit.
  409. TRY(silently_set_current_time(limit));
  410. // 5. If animation’s start time is unresolved and animation has an associated active timeline, let the start time be
  411. // the result of evaluating timeline time - (limit / playback rate) where timeline time is the current time value
  412. // of the associated timeline.
  413. if (!m_start_time.has_value() && m_timeline && !m_timeline->is_inactive())
  414. m_start_time = m_timeline->current_time().value() - (limit / playback_rate);
  415. // 6. If there is a pending pause task and start time is resolved,
  416. auto should_resolve_ready_promise = false;
  417. if (m_pending_pause_task == TaskState::Scheduled && m_start_time.has_value()) {
  418. // 1. Let the hold time be unresolved.
  419. // Note: Typically the hold time will already be unresolved except in the case when the animation was previously
  420. // idle.
  421. m_hold_time = {};
  422. // 2. Cancel the pending pause task.
  423. m_pending_pause_task = TaskState::None;
  424. // 3. Resolve the current ready promise of animation with animation.
  425. should_resolve_ready_promise = true;
  426. }
  427. // 7. If there is a pending play task and start time is resolved, cancel that task and resolve the current ready
  428. // promise of animation with animation.
  429. if (m_pending_play_task == TaskState::Scheduled && m_start_time.has_value()) {
  430. m_pending_play_task = TaskState::None;
  431. should_resolve_ready_promise = true;
  432. }
  433. if (should_resolve_ready_promise) {
  434. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm()) };
  435. WebIDL::resolve_promise(realm(), current_ready_promise(), this);
  436. }
  437. // 8. Run the procedure to update an animation’s finished state for animation with the did seek flag set to true,
  438. // and the synchronously notify flag set to true.
  439. update_finished_state(DidSeek::Yes, SynchronouslyNotify::Yes);
  440. return {};
  441. }
  442. // https://www.w3.org/TR/web-animations-1/#dom-animation-play
  443. WebIDL::ExceptionOr<void> Animation::play()
  444. {
  445. // Begins or resumes playback of the animation by running the procedure to play an animation passing true as the
  446. // value of the auto-rewind flag.
  447. return play_an_animation(AutoRewind::Yes);
  448. }
  449. // https://www.w3.org/TR/web-animations-1/#play-an-animation
  450. WebIDL::ExceptionOr<void> Animation::play_an_animation(AutoRewind auto_rewind)
  451. {
  452. if (auto document = document_for_timing())
  453. document->ensure_animation_timer();
  454. // 1. Let aborted pause be a boolean flag that is true if animation has a pending pause task, and false otherwise.
  455. auto aborted_pause = m_pending_pause_task == TaskState::Scheduled;
  456. // 2. Let has pending ready promise be a boolean flag that is initially false.
  457. auto has_pending_ready_promise = false;
  458. // 3. Let seek time be a time value that is initially unresolved.
  459. Optional<double> seek_time;
  460. // 4. If the auto-rewind flag is true, perform the steps corresponding to the first matching condition from the
  461. // following, if any:
  462. if (auto_rewind == AutoRewind::Yes) {
  463. auto playback_rate = this->playback_rate();
  464. auto current_time = this->current_time();
  465. auto associated_effect_end = this->associated_effect_end();
  466. // -> If animation’s effective playback rate ≥ 0, and animation’s current time is either:
  467. // - unresolved, or
  468. // - less than zero, or
  469. // - greater than or equal to associated effect end,
  470. if (playback_rate >= 0.0 && (!current_time.has_value() || current_time.value() < 0.0 || current_time.value() >= associated_effect_end)) {
  471. // Set seek time to zero.
  472. seek_time = 0.0;
  473. }
  474. // -> If animation’s effective playback rate < 0, and animation’s current time is either:
  475. // - unresolved, or
  476. // - less than or equal to zero, or
  477. // - greater than associated effect end,
  478. else if (playback_rate < 0.0 && (!current_time.has_value() || current_time.value() <= 0.0 || current_time.value() > associated_effect_end)) {
  479. // -> If associated effect end is positive infinity,
  480. if (isinf(associated_effect_end) && associated_effect_end > 0.0) {
  481. // throw an "InvalidStateError" DOMException and abort these steps.
  482. return WebIDL::InvalidStateError::create(realm(), "Cannot rewind an animation with an infinite effect end"_fly_string);
  483. }
  484. // -> Otherwise,
  485. // Set seek time to animation’s associated effect end.
  486. seek_time = associated_effect_end;
  487. }
  488. }
  489. // 5. If the following three conditions are all satisfied:
  490. // - seek time is unresolved, and
  491. // - animation’s start time is unresolved, and
  492. // - animation’s current time is unresolved,
  493. if (!seek_time.has_value() && !m_start_time.has_value() && !current_time().has_value()) {
  494. // set seek time to zero.
  495. seek_time = 0.0;
  496. }
  497. // 6. Let has finite timeline be true if animation has an associated timeline that is not monotonically increasing.
  498. auto has_finite_timeline = m_timeline && !m_timeline->is_monotonically_increasing();
  499. // 7. If seek time is resolved,
  500. if (seek_time.has_value()) {
  501. // -> If has finite timeline is true,
  502. if (has_finite_timeline) {
  503. // 1. Set animation’s start time to seek time.
  504. m_start_time = seek_time;
  505. // 2. Let animation’s hold time be unresolved.
  506. m_hold_time = {};
  507. // 3. Apply any pending playback rate on animation.
  508. apply_any_pending_playback_rate();
  509. }
  510. // Otherwise,
  511. else {
  512. // Set animation’s hold time to seek time.
  513. m_hold_time = seek_time;
  514. }
  515. }
  516. // 8. If animation’s hold time is resolved, let its start time be unresolved.
  517. if (m_hold_time.has_value())
  518. m_start_time = {};
  519. // 9. If animation has a pending play task or a pending pause task,
  520. if (pending()) {
  521. // 1. Cancel that task.
  522. m_pending_play_task = TaskState::None;
  523. m_pending_pause_task = TaskState::None;
  524. // 2. Set has pending ready promise to true.
  525. has_pending_ready_promise = true;
  526. }
  527. // 10. If the following four conditions are all satisfied:
  528. // - animation’s hold time is unresolved, and
  529. // - seek time is unresolved, and
  530. // - aborted pause is false, and
  531. // - animation does not have a pending playback rate,
  532. if (!m_hold_time.has_value() && !seek_time.has_value() && !aborted_pause && !m_pending_playback_rate.has_value()) {
  533. // abort this procedure.
  534. return {};
  535. }
  536. // 11. If has pending ready promise is false, let animation’s current ready promise be a new promise in the relevant
  537. // Realm of animation.
  538. if (!has_pending_ready_promise)
  539. m_current_ready_promise = WebIDL::create_promise(realm());
  540. // 12. Schedule a task to run as soon as animation is ready. The task shall perform the following steps:
  541. //
  542. // Note: Steps omitted, set run_pending_play_task()
  543. //
  544. // So long as the above task is scheduled but has yet to run, animation is described as having a pending play
  545. // task. While the task is running, however, animation does not have a pending play task.
  546. //
  547. // If a user agent determines that animation is immediately ready, it may schedule the above task as a microtask
  548. // such that it runs at the next microtask checkpoint, but it must not perform the task synchronously.
  549. m_pending_play_task = TaskState::Scheduled;
  550. m_saved_play_time = m_timeline->current_time().value();
  551. // 13. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  552. // and the synchronously notify flag set to false.
  553. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  554. return {};
  555. }
  556. // https://www.w3.org/TR/web-animations-1/#dom-animation-pause
  557. WebIDL::ExceptionOr<void> Animation::pause()
  558. {
  559. // 1. If animation has a pending pause task, abort these steps.
  560. if (m_pending_pause_task == TaskState::Scheduled)
  561. return {};
  562. // 2. If the play state of animation is paused, abort these steps.
  563. if (play_state() == Bindings::AnimationPlayState::Paused)
  564. return {};
  565. // 3. Let seek time be a time value that is initially unresolved.
  566. Optional<double> seek_time;
  567. // 4. Let has finite timeline be true if animation has an associated timeline that is not monotonically increasing.
  568. auto has_finite_timeline = m_timeline && !m_timeline->is_monotonically_increasing();
  569. // 5. If the animation’s current time is unresolved, perform the steps according to the first matching condition
  570. // from below:
  571. if (!current_time().has_value()) {
  572. // -> If animation’s playback rate is ≥ 0,
  573. if (playback_rate() >= 0.0) {
  574. // Set seek time to zero.
  575. seek_time = 0.0;
  576. }
  577. // -> Otherwise
  578. else {
  579. // If associated effect end for animation is positive infinity,
  580. auto associated_effect_end = this->associated_effect_end();
  581. if (isinf(associated_effect_end) && associated_effect_end > 0.0) {
  582. // throw an "InvalidStateError" DOMException and abort these steps.
  583. return WebIDL::InvalidStateError::create(realm(), "Cannot pause an animation with an infinite effect end"_fly_string);
  584. }
  585. // Otherwise,
  586. // Set seek time to animation’s associated effect end.
  587. seek_time = associated_effect_end;
  588. }
  589. }
  590. // 6. If seek time is resolved,
  591. if (seek_time.has_value()) {
  592. // If has finite timeline is true,
  593. if (has_finite_timeline) {
  594. // Set animation’s start time to seek time.
  595. m_start_time = seek_time;
  596. }
  597. // Otherwise,
  598. else {
  599. // Set animation’s hold time to seek time.
  600. m_hold_time = seek_time;
  601. }
  602. }
  603. // 7. Let has pending ready promise be a boolean flag that is initially false.
  604. auto has_pending_ready_promise = false;
  605. // 8. If animation has a pending play task, cancel that task and let has pending ready promise be true.
  606. if (m_pending_play_task == TaskState::Scheduled) {
  607. m_pending_pause_task = TaskState::None;
  608. has_pending_ready_promise = true;
  609. }
  610. // 9. If has pending ready promise is false, set animation’s current ready promise to a new promise in the relevant
  611. // Realm of animation.
  612. if (!has_pending_ready_promise)
  613. m_current_ready_promise = WebIDL::create_promise(realm());
  614. // 10. Schedule a task to be executed at the first possible moment where both of the following conditions are true:
  615. // - the user agent has performed any processing necessary to suspend the playback of animation’s associated
  616. // effect, if any.
  617. // - the animation is associated with a timeline that is not inactive.
  618. //
  619. // Note: This is run_pending_pause_task()
  620. m_pending_pause_task = TaskState::Scheduled;
  621. m_saved_pause_time = m_timeline->current_time().value();
  622. // 11. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  623. // and the synchronously notify flag set to false.
  624. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  625. return {};
  626. }
  627. // https://www.w3.org/TR/web-animations-1/#dom-animation-updateplaybackrate
  628. WebIDL::ExceptionOr<void> Animation::update_playback_rate(double new_playback_rate)
  629. {
  630. // 1. Let previous play state be animation’s play state.
  631. // Note: It is necessary to record the play state before updating animation’s effective playback rate since, in the
  632. // following logic, we want to immediately apply the pending playback rate of animation if it is currently
  633. // finished regardless of whether or not it will still be finished after we apply the pending playback rate.
  634. auto previous_play_state = play_state();
  635. // 2. Let animation’s pending playback rate be new playback rate.
  636. m_pending_playback_rate = new_playback_rate;
  637. // 3. Perform the steps corresponding to the first matching condition from below:
  638. // -> If animation has a pending play task or a pending pause task,
  639. if (pending()) {
  640. // Abort these steps.
  641. // Note: The different types of pending tasks will apply the pending playback rate when they run so there is no
  642. // further action required in this case.
  643. return {};
  644. }
  645. // -> If previous play state is idle or paused, or animation’s current time is unresolved,
  646. if (previous_play_state == Bindings::AnimationPlayState::Idle || previous_play_state == Bindings::AnimationPlayState::Paused || !current_time().has_value()) {
  647. // Apply any pending playback rate on animation.
  648. // Note: the second condition above is required so that if we have a running animation with an unresolved
  649. // current time and no pending play task, we do not attempt to play it below.
  650. apply_any_pending_playback_rate();
  651. }
  652. // -> If previous play state is finished,
  653. else if (previous_play_state == Bindings::AnimationPlayState::Finished) {
  654. // 1. Let the unconstrained current time be the result of calculating the current time of animation
  655. // substituting an unresolved time value for the hold time.
  656. Optional<double> unconstrained_current_time;
  657. {
  658. TemporaryChange change(m_hold_time, {});
  659. unconstrained_current_time = current_time();
  660. }
  661. // 2. Let animation’s start time be the result of evaluating the following expression:
  662. // timeline time - (unconstrained current time / pending playback rate)
  663. // Where timeline time is the current time value of the timeline associated with animation.
  664. // If pending playback rate is zero, let animation’s start time be timeline time.
  665. if (m_pending_playback_rate.value() == 0.0) {
  666. m_start_time = m_timeline->current_time().value();
  667. } else {
  668. m_start_time = m_timeline->current_time().value() - (unconstrained_current_time.value() / m_pending_playback_rate.value());
  669. }
  670. // 3. Apply any pending playback rate on animation.
  671. apply_any_pending_playback_rate();
  672. // 4. Run the procedure to update an animation’s finished state for animation with the did seek flag set to
  673. // false, and the synchronously notify flag set to false.
  674. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  675. }
  676. // -> Otherwise,
  677. else {
  678. // Run the procedure to play an animation for animation with the auto-rewind flag set to false.
  679. TRY(play_an_animation(AutoRewind::No));
  680. }
  681. return {};
  682. }
  683. // https://www.w3.org/TR/web-animations-1/#dom-animation-reverse
  684. WebIDL::ExceptionOr<void> Animation::reverse()
  685. {
  686. auto& realm = this->realm();
  687. // 1. If there is no timeline associated with animation, or the associated timeline is inactive throw an
  688. // "InvalidStateError" DOMException and abort these steps.
  689. if (!m_timeline || m_timeline->is_inactive())
  690. return WebIDL::InvalidStateError::create(realm, "Cannot reverse an animation with an inactive timeline"_fly_string);
  691. // 2. Let original pending playback rate be animation’s pending playback rate.
  692. auto original_pending_playback_rate = m_pending_playback_rate;
  693. // 3. Let animation’s pending playback rate be the additive inverse of its effective playback rate (i.e.
  694. // -effective playback rate).
  695. m_pending_playback_rate = -effective_playback_rate();
  696. // 4. Run the steps to play an animation for animation with the auto-rewind flag set to true.
  697. // If the steps to play an animation throw an exception, set animation’s pending playback rate to original
  698. // pending playback rate and propagate the exception.
  699. auto result = play_an_animation(AutoRewind::Yes);
  700. if (result.is_error()) {
  701. m_pending_playback_rate = original_pending_playback_rate;
  702. return result;
  703. }
  704. return {};
  705. }
  706. // https://www.w3.org/TR/web-animations-1/#dom-animation-persist
  707. void Animation::persist()
  708. {
  709. // Sets this animation’s replace state to persisted.
  710. set_replace_state(Bindings::AnimationReplaceState::Persisted);
  711. }
  712. // https://www.w3.org/TR/web-animations-1/#animation-time-to-timeline-time
  713. Optional<double> Animation::convert_an_animation_time_to_timeline_time(Optional<double> time) const
  714. {
  715. // 1. If time is unresolved, return time.
  716. if (!time.has_value())
  717. return time;
  718. // 2. If time is infinity, return an unresolved time value.
  719. if (isinf(time.value()))
  720. return {};
  721. // 3. If animation’s playback rate is zero, return an unresolved time value.
  722. if (m_playback_rate == 0.0)
  723. return {};
  724. // 4. If animation’s start time is unresolved, return an unresolved time value.
  725. if (!m_start_time.has_value())
  726. return {};
  727. // 5. Return the result of calculating: time × (1 / playback rate) + start time (where playback rate and start time
  728. // are the playback rate and start time of animation, respectively).
  729. return (time.value() * (1.0 / m_playback_rate)) + m_start_time.value();
  730. }
  731. // https://www.w3.org/TR/web-animations-1/#animation-time-to-origin-relative-time
  732. Optional<double> Animation::convert_a_timeline_time_to_an_origin_relative_time(Optional<double> time) const
  733. {
  734. // 1. Let timeline time be the result of converting time from an animation time to a timeline time.
  735. auto timeline_time = convert_an_animation_time_to_timeline_time(time);
  736. // 2. If timeline time is unresolved, return time.
  737. if (!timeline_time.has_value())
  738. return time;
  739. // 3. If animation is not associated with a timeline, return an unresolved time value.
  740. if (!m_timeline)
  741. return {};
  742. // 4. If animation is associated with an inactive timeline, return an unresolved time value.
  743. if (m_timeline->is_inactive())
  744. return {};
  745. // 5. If there is no procedure to convert a timeline time to an origin-relative time for the timeline associated
  746. // with animation, return an unresolved time value.
  747. if (!m_timeline->can_convert_a_timeline_time_to_an_origin_relative_time())
  748. return {};
  749. // 6. Return the result of converting timeline time to an origin-relative time using the procedure defined for the
  750. // timeline associated with animation.
  751. return m_timeline->convert_a_timeline_time_to_an_origin_relative_time(timeline_time);
  752. }
  753. // https://www.w3.org/TR/web-animations-1/#animation-document-for-timing
  754. JS::GCPtr<DOM::Document> Animation::document_for_timing() const
  755. {
  756. // An animation’s document for timing is the Document with which its timeline is associated. If an animation is not
  757. // associated with a timeline, or its timeline is not associated with a document, then it has no document for
  758. // timing.
  759. if (!m_timeline)
  760. return {};
  761. return m_timeline->associated_document();
  762. }
  763. void Animation::notify_timeline_time_did_change()
  764. {
  765. update_finished_state(DidSeek::No, SynchronouslyNotify::Yes);
  766. // Act on the pending play or pause task
  767. if (m_pending_play_task == TaskState::Scheduled) {
  768. m_pending_play_task = TaskState::None;
  769. run_pending_play_task();
  770. }
  771. if (m_pending_pause_task == TaskState::Scheduled) {
  772. m_pending_pause_task = TaskState::None;
  773. run_pending_pause_task();
  774. }
  775. }
  776. void Animation::effect_timing_changed(Badge<AnimationEffect>)
  777. {
  778. update_finished_state(DidSeek::No, SynchronouslyNotify::Yes);
  779. }
  780. // https://www.w3.org/TR/web-animations-1/#associated-effect-end
  781. double Animation::associated_effect_end() const
  782. {
  783. // The associated effect end of an animation is equal to the end time of the animation’s associated effect. If the
  784. // animation has no associated effect, the associated effect end is zero.
  785. return m_effect ? m_effect->end_time() : 0.0;
  786. }
  787. // https://www.w3.org/TR/web-animations-1/#effective-playback-rate
  788. double Animation::effective_playback_rate() const
  789. {
  790. // The effective playback rate of an animation is its pending playback rate, if set, otherwise it is the animation’s
  791. // playback rate.
  792. return m_pending_playback_rate.has_value() ? m_pending_playback_rate.value() : m_playback_rate;
  793. }
  794. // https://www.w3.org/TR/web-animations-1/#apply-any-pending-playback-rate
  795. void Animation::apply_any_pending_playback_rate()
  796. {
  797. // 1. If animation does not have a pending playback rate, abort these steps.
  798. if (!m_pending_playback_rate.has_value())
  799. return;
  800. // 2. Set animation’s playback rate to its pending playback rate.
  801. m_playback_rate = m_pending_playback_rate.value();
  802. // 3. Clear animation’s pending playback rate.
  803. m_pending_playback_rate = {};
  804. }
  805. // https://www.w3.org/TR/web-animations-1/#animation-silently-set-the-current-time
  806. WebIDL::ExceptionOr<void> Animation::silently_set_current_time(Optional<double> seek_time)
  807. {
  808. // 1. If seek time is an unresolved time value, then perform the following steps.
  809. if (!seek_time.has_value()) {
  810. // 1. If the current time is resolved, then throw a TypeError.
  811. if (current_time().has_value()) {
  812. return WebIDL::SimpleException {
  813. WebIDL::SimpleExceptionType::TypeError,
  814. "Cannot change an animation's current time from a resolve value to an unresolved value"sv
  815. };
  816. }
  817. // 2. Abort these steps.
  818. return {};
  819. }
  820. // 2. Update either animation’s hold time or start time as follows:
  821. // -> If any of the following conditions are true:
  822. // - animation’s hold time is resolved, or
  823. // - animation’s start time is unresolved, or
  824. // - animation has no associated timeline or the associated timeline is inactive, or
  825. // - animation’s playback rate is 0,
  826. if (m_hold_time.has_value() || !m_start_time.has_value() || !m_timeline || m_timeline->is_inactive() || m_playback_rate == 0.0) {
  827. // Set animation’s hold time to seek time.
  828. m_hold_time = seek_time;
  829. }
  830. // -> Otherwise,
  831. else {
  832. // Set animation’s start time to the result of evaluating timeline time - (seek time / playback rate) where
  833. // timeline time is the current time value of timeline associated with animation.
  834. m_start_time = m_timeline->current_time().value() - (seek_time.value() / m_playback_rate);
  835. }
  836. // 3. If animation has no associated timeline or the associated timeline is inactive, make animation’s start time
  837. // unresolved.
  838. if (!m_timeline || m_timeline->is_inactive())
  839. m_start_time = {};
  840. // 4. Make animation’s previous current time unresolved.
  841. m_previous_current_time = {};
  842. return {};
  843. }
  844. // https://www.w3.org/TR/web-animations-1/#update-an-animations-finished-state
  845. void Animation::update_finished_state(DidSeek did_seek, SynchronouslyNotify synchronously_notify)
  846. {
  847. auto& realm = this->realm();
  848. // 1. Let the unconstrained current time be the result of calculating the current time substituting an unresolved
  849. // time value for the hold time if did seek is false. If did seek is true, the unconstrained current time is
  850. // equal to the current time.
  851. //
  852. // Note: This is required to accommodate timelines that may change direction. Without this definition, a once-
  853. // finished animation would remain finished even when its timeline progresses in the opposite direction.
  854. Optional<double> unconstrained_current_time;
  855. if (did_seek == DidSeek::No) {
  856. TemporaryChange change(m_hold_time, {});
  857. unconstrained_current_time = current_time();
  858. } else {
  859. unconstrained_current_time = current_time();
  860. }
  861. // 2. If all three of the following conditions are true,
  862. // - the unconstrained current time is resolved, and
  863. // - animation’s start time is resolved, and
  864. // - animation does not have a pending play task or a pending pause task,
  865. if (unconstrained_current_time.has_value() && m_start_time.has_value() && !pending()) {
  866. // then update animation’s hold time based on the first matching condition for animation from below, if any:
  867. // -> If playback rate > 0 and unconstrained current time is greater than or equal to associated effect end,
  868. auto associated_effect_end = this->associated_effect_end();
  869. if (m_playback_rate > 0.0 && unconstrained_current_time.value() >= associated_effect_end) {
  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 maximum value of previous current time and associated
  875. // effect end. If the previous current time is unresolved, let the hold time be associated effect end.
  876. else if (m_previous_current_time.has_value()) {
  877. m_hold_time = max(m_previous_current_time.value(), associated_effect_end);
  878. } else {
  879. m_hold_time = associated_effect_end;
  880. }
  881. }
  882. // -> If playback rate < 0 and unconstrained current time is less than or equal to 0,
  883. else if (m_playback_rate < 0.0 && unconstrained_current_time.value() <= 0.0) {
  884. // If did seek is true, let the hold time be the value of unconstrained current time.
  885. if (did_seek == DidSeek::Yes) {
  886. m_hold_time = unconstrained_current_time;
  887. }
  888. // If did seek is false, let the hold time be the minimum value of previous current time and zero. If the
  889. // previous current time is unresolved, let the hold time be zero.
  890. else if (m_previous_current_time.has_value()) {
  891. m_hold_time = min(m_previous_current_time.value(), 0.0);
  892. } else {
  893. m_hold_time = 0.0;
  894. }
  895. }
  896. // -> If playback rate ≠ 0, and animation is associated with an active timeline,
  897. else if (m_playback_rate != 0.0 && m_timeline && !m_timeline->is_inactive()) {
  898. // Perform the following steps:
  899. // 1. If did seek is true and the hold time is resolved, let animation’s start time be equal to the result
  900. // of evaluating timeline time - (hold time / playback rate) where timeline time is the current time
  901. // value of timeline associated with animation.
  902. if (did_seek == DidSeek::Yes && m_hold_time.has_value())
  903. m_start_time = m_timeline->current_time().value() - (m_hold_time.value() / m_playback_rate);
  904. // 2. Let the hold time be unresolved.
  905. m_hold_time = {};
  906. }
  907. }
  908. // 3. Set the previous current time of animation be the result of calculating its current time.
  909. m_previous_current_time = current_time();
  910. // 4. Let current finished state be true if the play state of animation is finished. Otherwise, let it be false.
  911. auto current_finished_state = play_state() == Bindings::AnimationPlayState::Finished;
  912. // 5. If current finished state is true and the current finished promise is not yet resolved, perform the following
  913. // steps:
  914. if (current_finished_state && !m_is_finished) {
  915. // 1. Let finish notification steps refer to the following procedure:
  916. auto finish_notification_steps = JS::create_heap_function(heap(), [this, &realm]() {
  917. // 1. If animation’s play state is not equal to finished, abort these steps.
  918. if (play_state() != Bindings::AnimationPlayState::Finished)
  919. return;
  920. // 2. Resolve animation’s current finished promise object with animation.
  921. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm) };
  922. WebIDL::resolve_promise(realm, current_finished_promise(), this);
  923. m_is_finished = true;
  924. // 3. Create an AnimationPlaybackEvent, finishEvent.
  925. // 4. Set finishEvent’s type attribute to finish.
  926. // 5. Set finishEvent’s currentTime attribute to the current time of animation.
  927. AnimationPlaybackEventInit init;
  928. init.current_time = current_time();
  929. auto finish_event = AnimationPlaybackEvent::create(realm, HTML::EventNames::finish, init);
  930. // 6. Set finishEvent’s timelineTime attribute to the current time of the timeline with which animation is
  931. // associated. If animation is not associated with a timeline, or the timeline is inactive, let
  932. // timelineTime be null.
  933. if (m_timeline && !m_timeline->is_inactive())
  934. finish_event->set_timeline_time(m_timeline->current_time());
  935. else
  936. finish_event->set_timeline_time({});
  937. // 7. If animation has a document for timing, then append finishEvent to its document for timing's pending
  938. // animation event queue along with its target, animation. For the scheduled event time, use the result
  939. // of converting animation’s associated effect end to an origin-relative time.
  940. if (auto document_for_timing = this->document_for_timing()) {
  941. document_for_timing->append_pending_animation_event({
  942. .event = finish_event,
  943. .animation = *this,
  944. .target = *this,
  945. .scheduled_event_time = convert_a_timeline_time_to_an_origin_relative_time(associated_effect_end()),
  946. });
  947. }
  948. // Otherwise, queue a task to dispatch finishEvent at animation. The task source for this task is the DOM
  949. // manipulation task source.
  950. else {
  951. // Manually create a task so its ID can be saved
  952. auto& document = verify_cast<HTML::Window>(realm.global_object()).associated_document();
  953. auto task = HTML::Task::create(vm(), HTML::Task::Source::DOMManipulation, &document,
  954. JS::create_heap_function(heap(), [this, finish_event]() {
  955. dispatch_event(finish_event);
  956. }));
  957. m_pending_finish_microtask_id = task->id();
  958. HTML::main_thread_event_loop().task_queue().add(task);
  959. }
  960. });
  961. // 2. If synchronously notify is true, cancel any queued microtask to run the finish notification steps for this
  962. // animation, and run the finish notification steps immediately.
  963. if (synchronously_notify == SynchronouslyNotify::Yes) {
  964. if (m_pending_finish_microtask_id.has_value()) {
  965. HTML::main_thread_event_loop().task_queue().remove_tasks_matching([id = move(m_pending_finish_microtask_id)](auto const& task) {
  966. return task.id() == id;
  967. });
  968. }
  969. finish_notification_steps->function()();
  970. }
  971. // Otherwise, if synchronously notify is false, queue a microtask to run finish notification steps for
  972. // animation unless there is already a microtask queued to run those steps for animation.
  973. else if (!m_pending_finish_microtask_id.has_value()) {
  974. auto& document = verify_cast<HTML::Window>(realm.global_object()).associated_document();
  975. auto task = HTML::Task::create(vm(), HTML::Task::Source::DOMManipulation, &document, move(finish_notification_steps));
  976. m_pending_finish_microtask_id = task->id();
  977. HTML::main_thread_event_loop().task_queue().add(move(task));
  978. }
  979. }
  980. // 6. If current finished state is false and animation’s current finished promise is already resolved, set
  981. // animation’s current finished promise to a new promise in the relevant Realm of animation.
  982. if (!current_finished_state && m_is_finished) {
  983. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm) };
  984. m_current_finished_promise = WebIDL::create_promise(realm);
  985. m_is_finished = false;
  986. }
  987. invalidate_effect();
  988. }
  989. // https://www.w3.org/TR/web-animations-1/#animation-reset-an-animations-pending-tasks
  990. void Animation::reset_an_animations_pending_tasks()
  991. {
  992. auto& realm = this->realm();
  993. // 1. If animation does not have a pending play task or a pending pause task, abort this procedure.
  994. if (!pending())
  995. return;
  996. // 2. If animation has a pending play task, cancel that task.
  997. m_pending_play_task = TaskState::None;
  998. // 3. If animation has a pending pause task, cancel that task.
  999. m_pending_pause_task = TaskState::None;
  1000. // 4. Apply any pending playback rate on animation.
  1001. apply_any_pending_playback_rate();
  1002. // 5. Reject animation’s current ready promise with a DOMException named "AbortError".
  1003. auto dom_exception = WebIDL::AbortError::create(realm, "Animation was cancelled"_fly_string);
  1004. WebIDL::reject_promise(realm, current_ready_promise(), dom_exception);
  1005. // 6. Set the [[PromiseIsHandled]] internal slot of animation’s current ready promise to true.
  1006. WebIDL::mark_promise_as_handled(current_ready_promise());
  1007. // 7. Let animation’s current ready promise be the result of creating a new resolved Promise object with value
  1008. // animation in the relevant Realm of animation.
  1009. m_current_ready_promise = WebIDL::create_resolved_promise(realm, this);
  1010. }
  1011. // Step 12 of https://www.w3.org/TR/web-animations-1/#playing-an-animation-section
  1012. void Animation::run_pending_play_task()
  1013. {
  1014. // 1. Assert that at least one of animation’s start time or hold time is resolved.
  1015. VERIFY(m_start_time.has_value() || m_hold_time.has_value());
  1016. // 2. Let ready time be the time value of the timeline associated with animation at the moment when animation became
  1017. // ready.
  1018. auto ready_time = m_saved_play_time.release_value();
  1019. // 3. Perform the steps corresponding to the first matching condition below, if any:
  1020. // -> If animation’s hold time is resolved,
  1021. if (m_hold_time.has_value()) {
  1022. // 1. Apply any pending playback rate on animation.
  1023. apply_any_pending_playback_rate();
  1024. // 2. Let new start time be the result of evaluating ready time - hold time / playback rate for animation. If
  1025. // the playback rate is zero, let new start time be simply ready time.
  1026. auto new_start_time = m_playback_rate != 0.0 ? ready_time - (m_hold_time.value() / m_playback_rate) : ready_time;
  1027. // 3. Set the start time of animation to new start time.
  1028. m_start_time = new_start_time;
  1029. // 4. If animation’s playback rate is not 0, make animation’s hold time unresolved.
  1030. if (m_playback_rate != 0.0)
  1031. m_hold_time = {};
  1032. }
  1033. // -> If animation’s start time is resolved and animation has a pending playback rate,
  1034. else if (m_start_time.has_value() && m_pending_playback_rate.has_value()) {
  1035. // 1. Let current time to match be the result of evaluating (ready time - start time) × playback rate for
  1036. // animation.
  1037. auto current_time_to_match = (ready_time - m_start_time.value()) * m_playback_rate;
  1038. // 2. Apply any pending playback rate on animation.
  1039. apply_any_pending_playback_rate();
  1040. // 3. If animation’s playback rate is zero, let animation’s hold time be current time to match.
  1041. if (m_playback_rate == 0.0)
  1042. m_hold_time = current_time_to_match;
  1043. // 4. Let new start time be the result of evaluating ready time - current time to match / playback rate for
  1044. // animation. If the playback rate is zero, let new start time be simply ready time.
  1045. auto new_start_time = m_playback_rate != 0.0 ? ready_time - (current_time_to_match / m_playback_rate) : ready_time;
  1046. // 5. Set the start time of animation to new start time.
  1047. m_start_time = new_start_time;
  1048. }
  1049. // 4. Resolve animation’s current ready promise with animation.
  1050. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm()) };
  1051. WebIDL::resolve_promise(realm(), current_ready_promise(), this);
  1052. // 5. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  1053. // and the synchronously notify flag set to false.
  1054. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  1055. }
  1056. // Step 10 of https://www.w3.org/TR/web-animations-1/#pause-an-animation
  1057. void Animation::run_pending_pause_task()
  1058. {
  1059. // 1. Let ready time be the time value of the timeline associated with animation at the moment when the user agent
  1060. // completed processing necessary to suspend playback of animation’s associated effect.
  1061. VERIFY(m_saved_pause_time.has_value());
  1062. auto ready_time = m_saved_pause_time.release_value();
  1063. // 2. If animation’s start time is resolved and its hold time is not resolved, let animation’s hold time be the
  1064. // result of evaluating (ready time - start time) × playback rate.
  1065. // Note: The hold time might be already set if the animation is finished, or if the animation has a pending play
  1066. // task. In either case we want to preserve the hold time as we enter the paused state.
  1067. if (m_start_time.has_value() && !m_hold_time.has_value())
  1068. m_hold_time = (ready_time - m_start_time.value()) * m_playback_rate;
  1069. // 3. Apply any pending playback rate on animation.
  1070. apply_any_pending_playback_rate();
  1071. // 4. Make animation’s start time unresolved.
  1072. m_start_time = {};
  1073. // 5. Resolve animation’s current ready promise with animation.
  1074. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm()) };
  1075. WebIDL::resolve_promise(realm(), current_ready_promise(), this);
  1076. // 6. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  1077. // and the synchronously notify flag set to false.
  1078. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  1079. }
  1080. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_ready_promise() const
  1081. {
  1082. if (!m_current_ready_promise) {
  1083. // The current ready promise is initially a resolved Promise created using the procedure to create a new
  1084. // resolved Promise with the animation itself as its value and created in the relevant Realm of the animation.
  1085. m_current_ready_promise = WebIDL::create_resolved_promise(realm(), this);
  1086. }
  1087. return *m_current_ready_promise;
  1088. }
  1089. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_finished_promise() const
  1090. {
  1091. if (!m_current_finished_promise) {
  1092. // The current finished promise is initially a pending Promise object.
  1093. m_current_finished_promise = WebIDL::create_promise(realm());
  1094. }
  1095. return *m_current_finished_promise;
  1096. }
  1097. void Animation::invalidate_effect()
  1098. {
  1099. if (m_effect) {
  1100. if (auto target = m_effect->target(); target && target->paintable()) {
  1101. target->document().set_needs_animated_style_update();
  1102. target->paintable()->set_needs_display();
  1103. }
  1104. }
  1105. }
  1106. Animation::Animation(JS::Realm& realm)
  1107. : DOM::EventTarget(realm)
  1108. {
  1109. static unsigned int next_animation_list_order = 0;
  1110. m_global_animation_list_order = next_animation_list_order++;
  1111. }
  1112. void Animation::initialize(JS::Realm& realm)
  1113. {
  1114. Base::initialize(realm);
  1115. WEB_SET_PROTOTYPE_FOR_INTERFACE(Animation);
  1116. }
  1117. void Animation::visit_edges(Cell::Visitor& visitor)
  1118. {
  1119. Base::visit_edges(visitor);
  1120. visitor.visit(m_effect);
  1121. visitor.visit(m_timeline);
  1122. visitor.visit(m_current_ready_promise);
  1123. visitor.visit(m_current_finished_promise);
  1124. }
  1125. }