Animation.cpp 61 KB

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