Animation.cpp 61 KB

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