Animation.cpp 62 KB

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