Animation.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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/DOM/Document.h>
  13. #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
  14. #include <LibWeb/HTML/Window.h>
  15. #include <LibWeb/WebIDL/ExceptionOr.h>
  16. #include <LibWeb/WebIDL/Promise.h>
  17. namespace Web::Animations {
  18. JS_DEFINE_ALLOCATOR(Animation);
  19. // https://www.w3.org/TR/web-animations-1/#dom-animation-animation
  20. JS::NonnullGCPtr<Animation> Animation::create(JS::Realm& realm, JS::GCPtr<AnimationEffect> effect, Optional<JS::GCPtr<AnimationTimeline>> timeline)
  21. {
  22. // 1. Let animation be a new Animation object.
  23. auto animation = realm.heap().allocate<Animation>(realm, realm);
  24. // 2. Run the procedure to set the timeline of an animation on animation passing timeline as the new timeline or, if
  25. // a timeline argument is missing, passing the default document timeline of the Document associated with the
  26. // Window that is the current global object.
  27. if (!timeline.has_value()) {
  28. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  29. timeline = window.associated_document().timeline();
  30. }
  31. animation->set_timeline(timeline.release_value());
  32. // 3. Run the procedure to set the associated effect of an animation on animation passing source as the new effect.
  33. animation->set_effect(effect);
  34. return animation;
  35. }
  36. WebIDL::ExceptionOr<JS::NonnullGCPtr<Animation>> Animation::construct_impl(JS::Realm& realm, JS::GCPtr<AnimationEffect> effect, Optional<JS::GCPtr<AnimationTimeline>> timeline)
  37. {
  38. return create(realm, effect, timeline);
  39. }
  40. // https://www.w3.org/TR/web-animations-1/#animation-set-the-associated-effect-of-an-animation
  41. void Animation::set_effect(JS::GCPtr<AnimationEffect> new_effect)
  42. {
  43. // Setting this attribute updates the object’s associated effect using the procedure to set the associated effect of
  44. // an animation.
  45. // 1. Let old effect be the current associated effect of animation, if any.
  46. auto old_effect = m_effect;
  47. // 2. If new effect is the same object as old effect, abort this procedure.
  48. if (new_effect == old_effect)
  49. return;
  50. // 3. If animation has a pending pause task, reschedule that task to run as soon as animation is ready.
  51. // 4. If animation has a pending play task, reschedule that task to run as soon as animation is ready to play ne
  52. // effect.
  53. // Note: There is no real difference between "pending" and "as soon as possible", so this step is a no-op.
  54. // 5. If new effect is not null and if new effect is the associated effect of another animation, previous animation,
  55. // run the procedure to set the associated effect of an animation (this procedure) on previous animation passing
  56. // null as new effect.
  57. if (new_effect && new_effect->associated_animation() != this) {
  58. if (auto animation = new_effect->associated_animation())
  59. animation->set_effect({});
  60. }
  61. // 6. Let the associated effect of animation be new effect.
  62. if (new_effect)
  63. new_effect->set_associated_animation(this);
  64. if (m_effect)
  65. m_effect->set_associated_animation({});
  66. m_effect = new_effect;
  67. // 7. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  68. // and the synchronously notify flag set to false.
  69. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  70. }
  71. // https://www.w3.org/TR/web-animations-1/#animation-set-the-timeline-of-an-animation
  72. void Animation::set_timeline(JS::GCPtr<AnimationTimeline> new_timeline)
  73. {
  74. // Setting this attribute updates the object’s timeline using the procedure to set the timeline of an animation.
  75. // 1. Let old timeline be the current timeline of animation, if any.
  76. auto old_timeline = m_timeline;
  77. // 2. If new timeline is the same object as old timeline, abort this procedure.
  78. if (new_timeline == old_timeline)
  79. return;
  80. // 3. Let the timeline of animation be new timeline.
  81. if (m_timeline)
  82. m_timeline->disassociate_with_animation(*this);
  83. m_timeline = new_timeline;
  84. m_timeline->associate_with_animation(*this);
  85. // 4. If the start time of animation is resolved, make animation’s hold time unresolved.
  86. if (m_start_time.has_value())
  87. m_hold_time = {};
  88. // 5. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  89. // and the synchronously notify flag set to false.
  90. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  91. }
  92. // https://www.w3.org/TR/web-animations-1/#dom-animation-starttime
  93. // https://www.w3.org/TR/web-animations-1/#set-the-start-time
  94. void Animation::set_start_time(Optional<double> const& new_start_time)
  95. {
  96. // Setting this attribute updates the start time using the procedure to set the start time of this object to the new
  97. // value.
  98. // 1. Let timeline time be the current time value of the timeline that animation is associated with. If there is no
  99. // timeline associated with animation or the associated timeline is inactive, let the timeline time be
  100. // unresolved.
  101. auto timeline_time = m_timeline && !m_timeline->is_inactive() ? m_timeline->current_time() : Optional<double> {};
  102. // 2. If timeline time is unresolved and new start time is resolved, make animation’s hold time unresolved.
  103. if (!timeline_time.has_value() && new_start_time.has_value())
  104. m_hold_time = {};
  105. // 3. Let previous current time be animation’s current time.
  106. auto previous_current_time = current_time();
  107. // 4. Apply any pending playback rate on animation.
  108. apply_any_pending_playback_rate();
  109. // 5. Set animation’s start time to new start time.
  110. m_start_time = new_start_time;
  111. // 6. Update animation’s hold time based on the first matching condition from the following,
  112. // -> If new start time is resolved,
  113. if (new_start_time.has_value()) {
  114. // If animation’s playback rate is not zero, make animation’s hold time unresolved.
  115. if (m_playback_rate != 0.0)
  116. m_hold_time = {};
  117. }
  118. // -> Otherwise (new start time is unresolved),
  119. else {
  120. // Set animation’s hold time to previous current time even if previous current time is unresolved.
  121. m_hold_time = previous_current_time;
  122. }
  123. // 7. If animation has a pending play task or a pending pause task, cancel that task and resolve animation’s current
  124. // ready promise with animation.
  125. if (pending()) {
  126. m_pending_play_task = TaskState::None;
  127. m_pending_pause_task = TaskState::None;
  128. WebIDL::resolve_promise(realm(), current_ready_promise(), this);
  129. }
  130. // 8. Run the procedure to update an animation’s finished state for animation with the did seek flag set to true,
  131. // and the synchronously notify flag set to false.
  132. update_finished_state(DidSeek::Yes, SynchronouslyNotify::No);
  133. }
  134. // https://www.w3.org/TR/web-animations-1/#animation-current-time
  135. Optional<double> Animation::current_time() const
  136. {
  137. // The current time is calculated from the first matching condition from below:
  138. // -> If the animation’s hold time is resolved,
  139. if (m_hold_time.has_value()) {
  140. // The current time is the animation’s hold time.
  141. return m_hold_time.value();
  142. }
  143. // -> If any of the following are true:
  144. // - the animation has no associated timeline, or
  145. // - the associated timeline is inactive, or
  146. // - the animation’s start time is unresolved.
  147. if (!m_timeline || m_timeline->is_inactive() || !m_start_time.has_value()) {
  148. // The current time is an unresolved time value.
  149. return {};
  150. }
  151. // -> Otherwise,
  152. // current time = (timeline time - start time) × playback rate
  153. // Where timeline time is the current time value of the associated timeline. The playback rate value is defined
  154. // in §4.4.15 Speed control.
  155. return (m_timeline->current_time().value() - m_start_time.value()) * playback_rate();
  156. }
  157. // https://www.w3.org/TR/web-animations-1/#animation-set-the-current-time
  158. WebIDL::ExceptionOr<void> Animation::set_current_time(Optional<double> const& seek_time)
  159. {
  160. // 1. Run the steps to silently set the current time of animation to seek time.
  161. TRY(silently_set_current_time(seek_time));
  162. // 2. If animation has a pending pause task, synchronously complete the pause operation by performing the following
  163. // steps:
  164. if (m_pending_pause_task == TaskState::Scheduled) {
  165. // 1. Set animation’s hold time to seek time.
  166. m_hold_time = seek_time;
  167. // 2. Apply any pending playback rate to animation.
  168. apply_any_pending_playback_rate();
  169. // 3. Make animation’s start time unresolved.
  170. m_start_time = {};
  171. // 4. Cancel the pending pause task.
  172. m_pending_pause_task = TaskState::None;
  173. // 5 Resolve animation’s current ready promise with animation.
  174. WebIDL::resolve_promise(realm(), current_ready_promise(), this);
  175. }
  176. // 3. Run the procedure to update an animation’s finished state for animation with the did seek flag set to true,
  177. // and the synchronously notify flag set to false.
  178. update_finished_state(DidSeek::Yes, SynchronouslyNotify::No);
  179. return {};
  180. }
  181. // https://www.w3.org/TR/web-animations-1/#dom-animation-playbackrate
  182. // https://www.w3.org/TR/web-animations-1/#set-the-playback-rate
  183. WebIDL::ExceptionOr<void> Animation::set_playback_rate(double new_playback_rate)
  184. {
  185. // Setting this attribute follows the procedure to set the playback rate of this object to the new value.
  186. // 1. Clear any pending playback rate on animation.
  187. m_pending_playback_rate = {};
  188. // 2. Let previous time be the value of the current time of animation before changing the playback rate.
  189. auto previous_time = current_time();
  190. // 3. Let previous playback rate be the current effective playback rate of animation.
  191. auto previous_playback_rate = playback_rate();
  192. // 4. Set the playback rate to new playback rate.
  193. m_playback_rate = new_playback_rate;
  194. // 5. Perform the steps corresponding to the first matching condition from the following, if any:
  195. // -> If animation is associated with a monotonically increasing timeline and the previous time is resolved,
  196. if (m_timeline && m_timeline->is_monotonically_increasing() && previous_time.has_value()) {
  197. // set the current time of animation to previous time.
  198. TRY(set_current_time(previous_time));
  199. }
  200. // -> If animation is associated with a non-null timeline that is not monotonically increasing, the start time of
  201. // animation is resolved, associated effect end is not infinity, and either:
  202. // - the previous playback rate < 0 and the new playback rate ≥ 0, or
  203. // - the previous playback rate ≥ 0 and the new playback rate < 0,
  204. 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))) {
  205. // Set animation’s start time to the result of evaluating associated effect end - start time for animation.
  206. m_start_time = associated_effect_end() - m_start_time.value();
  207. }
  208. return {};
  209. }
  210. // https://www.w3.org/TR/web-animations-1/#animation-play-state
  211. Bindings::AnimationPlayState Animation::play_state() const
  212. {
  213. // The play state of animation, animation, at a given moment is the state corresponding to the first matching
  214. // condition from the following:
  215. // -> All of the following conditions are true:
  216. // - The current time of animation is unresolved, and
  217. // - the start time of animation is unresolved, and
  218. // - animation does not have either a pending play task or a pending pause task,
  219. auto current_time = this->current_time();
  220. if (!current_time.has_value() && !m_start_time.has_value() && !pending()) {
  221. // → idle
  222. return Bindings::AnimationPlayState::Idle;
  223. }
  224. // -> Either of the following conditions are true:
  225. // - animation has a pending pause task, or
  226. // - both the start time of animation is unresolved and it does not have a pending play task,
  227. if (m_pending_pause_task == TaskState::Scheduled || (!m_start_time.has_value() && m_pending_play_task == TaskState::None)) {
  228. // → paused
  229. return Bindings::AnimationPlayState::Paused;
  230. }
  231. // -> For animation, current time is resolved and either of the following conditions are true:
  232. // - animation’s effective playback rate > 0 and current time ≥ associated effect end; or
  233. // - animation’s effective playback rate < 0 and current time ≤ 0,
  234. auto effective_playback_rate = this->effective_playback_rate();
  235. 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))) {
  236. // → finished
  237. return Bindings::AnimationPlayState::Finished;
  238. }
  239. // -> Otherwise,
  240. // → running
  241. return Bindings::AnimationPlayState::Running;
  242. }
  243. // https://www.w3.org/TR/web-animations-1/#dom-animation-play
  244. WebIDL::ExceptionOr<void> Animation::play()
  245. {
  246. // Begins or resumes playback of the animation by running the procedure to play an animation passing true as the
  247. // value of the auto-rewind flag.
  248. return play_an_animation(AutoRewind::Yes);
  249. }
  250. // https://www.w3.org/TR/web-animations-1/#play-an-animation
  251. WebIDL::ExceptionOr<void> Animation::play_an_animation(AutoRewind auto_rewind)
  252. {
  253. // 1. Let aborted pause be a boolean flag that is true if animation has a pending pause task, and false otherwise.
  254. auto aborted_pause = m_pending_pause_task == TaskState::Scheduled;
  255. // 2. Let has pending ready promise be a boolean flag that is initially false.
  256. auto has_pending_ready_promise = false;
  257. // 3. Let seek time be a time value that is initially unresolved.
  258. Optional<double> seek_time;
  259. // 4. If the auto-rewind flag is true, perform the steps corresponding to the first matching condition from the
  260. // following, if any:
  261. if (auto_rewind == AutoRewind::Yes) {
  262. auto playback_rate = this->playback_rate();
  263. auto current_time = this->current_time();
  264. auto associated_effect_end = this->associated_effect_end();
  265. // -> If animation’s effective playback rate ≥ 0, and animation’s current time is either:
  266. // - unresolved, or
  267. // - less than zero, or
  268. // - greater than or equal to associated effect end,
  269. if (playback_rate >= 0.0 && (!current_time.has_value() || current_time.value() < 0.0 || current_time.value() >= associated_effect_end)) {
  270. // Set seek time to zero.
  271. seek_time = 0.0;
  272. }
  273. // -> If animation’s effective playback rate < 0, and animation’s current time is either:
  274. // - unresolved, or
  275. // - less than or equal to zero, or
  276. // - greater than associated effect end,
  277. else if (playback_rate < 0.0 && (!current_time.has_value() || current_time.value() <= 0.0 || current_time.value() > associated_effect_end)) {
  278. // -> If associated effect end is positive infinity,
  279. if (isinf(associated_effect_end) && associated_effect_end > 0.0) {
  280. // throw an "InvalidStateError" DOMException and abort these steps.
  281. return WebIDL::InvalidStateError::create(realm(), "Cannot rewind an animation with an infinite effect end"_fly_string);
  282. }
  283. // -> Otherwise,
  284. // Set seek time to animation’s associated effect end.
  285. seek_time = associated_effect_end;
  286. }
  287. }
  288. // 5. If the following three conditions are all satisfied:
  289. // - seek time is unresolved, and
  290. // - animation’s start time is unresolved, and
  291. // - animation’s current time is unresolved,
  292. if (!seek_time.has_value() && !m_start_time.has_value() && !current_time().has_value()) {
  293. // set seek time to zero.
  294. seek_time = 0.0;
  295. }
  296. // 6. Let has finite timeline be true if animation has an associated timeline that is not monotonically increasing.
  297. auto has_finite_timeline = m_timeline && !m_timeline->is_monotonically_increasing();
  298. // 7. If seek time is resolved,
  299. if (seek_time.has_value()) {
  300. // -> If has finite timeline is true,
  301. if (has_finite_timeline) {
  302. // 1. Set animation’s start time to seek time.
  303. m_start_time = seek_time;
  304. // 2. Let animation’s hold time be unresolved.
  305. m_hold_time = {};
  306. // 3. Apply any pending playback rate on animation.
  307. apply_any_pending_playback_rate();
  308. }
  309. // Otherwise,
  310. else {
  311. // Set animation’s hold time to seek time.
  312. m_hold_time = seek_time;
  313. }
  314. }
  315. // 8. If animation’s hold time is resolved, let its start time be unresolved.
  316. if (m_hold_time.has_value())
  317. m_start_time = {};
  318. // 9. If animation has a pending play task or a pending pause task,
  319. if (pending()) {
  320. // 1. Cancel that task.
  321. m_pending_play_task = TaskState::None;
  322. m_pending_pause_task = TaskState::None;
  323. // 2. Set has pending ready promise to true.
  324. has_pending_ready_promise = true;
  325. }
  326. // 10. If the following four conditions are all satisfied:
  327. // - animation’s hold time is unresolved, and
  328. // - seek time is unresolved, and
  329. // - aborted pause is false, and
  330. // - animation does not have a pending playback rate,
  331. if (!m_hold_time.has_value() && !seek_time.has_value() && !aborted_pause && !m_pending_playback_rate.has_value()) {
  332. // abort this procedure.
  333. return {};
  334. }
  335. // 11. If has pending ready promise is false, let animation’s current ready promise be a new promise in the relevant
  336. // Realm of animation.
  337. if (!has_pending_ready_promise)
  338. m_current_ready_promise = WebIDL::create_promise(realm());
  339. // 12. Schedule a task to run as soon as animation is ready. The task shall perform the following steps:
  340. //
  341. // Note: Steps omitted, set run_pending_play_task()
  342. //
  343. // So long as the above task is scheduled but has yet to run, animation is described as having a pending play
  344. // task. While the task is running, however, animation does not have a pending play task.
  345. //
  346. // If a user agent determines that animation is immediately ready, it may schedule the above task as a microtask
  347. // such that it runs at the next microtask checkpoint, but it must not perform the task synchronously.
  348. m_pending_play_task = TaskState::Scheduled;
  349. // 13. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  350. // and the synchronously notify flag set to false.
  351. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  352. return {};
  353. }
  354. // https://www.w3.org/TR/web-animations-1/#animation-time-to-timeline-time
  355. Optional<double> Animation::convert_an_animation_time_to_timeline_time(Optional<double> time) const
  356. {
  357. // 1. If time is unresolved, return time.
  358. if (!time.has_value())
  359. return time;
  360. // 2. If time is infinity, return an unresolved time value.
  361. if (isinf(time.value()))
  362. return {};
  363. // 3. If animation’s playback rate is zero, return an unresolved time value.
  364. if (m_playback_rate == 0.0)
  365. return {};
  366. // 4. If animation’s start time is unresolved, return an unresolved time value.
  367. if (!m_start_time.has_value())
  368. return {};
  369. // 5. Return the result of calculating: time × (1 / playback rate) + start time (where playback rate and start time
  370. // are the playback rate and start time of animation, respectively).
  371. return (time.value() * (1.0 / m_playback_rate)) + m_start_time.value();
  372. }
  373. // https://www.w3.org/TR/web-animations-1/#animation-time-to-origin-relative-time
  374. Optional<double> Animation::convert_a_timeline_time_to_an_origin_relative_time(Optional<double> time) const
  375. {
  376. // 1. Let timeline time be the result of converting time from an animation time to a timeline time.
  377. auto timeline_time = convert_an_animation_time_to_timeline_time(time);
  378. // 2. If timeline time is unresolved, return time.
  379. if (!timeline_time.has_value())
  380. return time;
  381. // 3. If animation is not associated with a timeline, return an unresolved time value.
  382. if (!m_timeline)
  383. return {};
  384. // 4. If animation is associated with an inactive timeline, return an unresolved time value.
  385. if (m_timeline->is_inactive())
  386. return {};
  387. // 5. If there is no procedure to convert a timeline time to an origin-relative time for the timeline associated
  388. // with animation, return an unresolved time value.
  389. if (!m_timeline->can_convert_a_timeline_time_to_an_origin_relative_time())
  390. return {};
  391. // 6. Return the result of converting timeline time to an origin-relative time using the procedure defined for the
  392. // timeline associated with animation.
  393. return m_timeline->convert_a_timeline_time_to_an_origin_relative_time(timeline_time);
  394. }
  395. // https://www.w3.org/TR/web-animations-1/#animation-document-for-timing
  396. JS::GCPtr<DOM::Document> Animation::document_for_timing() const
  397. {
  398. // An animation’s document for timing is the Document with which its timeline is associated. If an animation is not
  399. // associated with a timeline, or its timeline is not associated with a document, then it has no document for
  400. // timing.
  401. if (!m_timeline)
  402. return {};
  403. return m_timeline->associated_document();
  404. }
  405. void Animation::notify_timeline_time_did_change()
  406. {
  407. update_finished_state(DidSeek::No, SynchronouslyNotify::Yes);
  408. // Act on the pending play or pause task
  409. if (m_pending_pause_task == TaskState::Scheduled) {
  410. m_pending_pause_task = TaskState::None;
  411. // FIXME:
  412. // run_pending_pause_task();
  413. }
  414. if (m_pending_play_task == TaskState::Scheduled) {
  415. m_pending_play_task = TaskState::None;
  416. run_pending_play_task();
  417. }
  418. }
  419. void Animation::effect_timing_changed(Badge<AnimationEffect>)
  420. {
  421. update_finished_state(DidSeek::No, SynchronouslyNotify::Yes);
  422. }
  423. // https://www.w3.org/TR/web-animations-1/#associated-effect-end
  424. double Animation::associated_effect_end() const
  425. {
  426. // The associated effect end of an animation is equal to the end time of the animation’s associated effect. If the
  427. // animation has no associated effect, the associated effect end is zero.
  428. return m_effect ? m_effect->end_time() : 0.0;
  429. }
  430. // https://www.w3.org/TR/web-animations-1/#effective-playback-rate
  431. double Animation::effective_playback_rate() const
  432. {
  433. // The effective playback rate of an animation is its pending playback rate, if set, otherwise it is the animation’s
  434. // playback rate.
  435. return m_pending_playback_rate.has_value() ? m_pending_playback_rate.value() : m_playback_rate;
  436. }
  437. // https://www.w3.org/TR/web-animations-1/#apply-any-pending-playback-rate
  438. void Animation::apply_any_pending_playback_rate()
  439. {
  440. // 1. If animation does not have a pending playback rate, abort these steps.
  441. if (!m_pending_playback_rate.has_value())
  442. return;
  443. // 2. Set animation’s playback rate to its pending playback rate.
  444. m_playback_rate = m_pending_playback_rate.value();
  445. // 3. Clear animation’s pending playback rate.
  446. m_pending_playback_rate = {};
  447. }
  448. // https://www.w3.org/TR/web-animations-1/#animation-silently-set-the-current-time
  449. WebIDL::ExceptionOr<void> Animation::silently_set_current_time(Optional<double> seek_time)
  450. {
  451. // 1. If seek time is an unresolved time value, then perform the following steps.
  452. if (!seek_time.has_value()) {
  453. // 1. If the current time is resolved, then throw a TypeError.
  454. if (current_time().has_value()) {
  455. return WebIDL::SimpleException {
  456. WebIDL::SimpleExceptionType::TypeError,
  457. "Cannot change an animation's current time from a resolve value to an unresolved value"sv
  458. };
  459. }
  460. // 2. Abort these steps.
  461. return {};
  462. }
  463. // 2. Update either animation’s hold time or start time as follows:
  464. // -> If any of the following conditions are true:
  465. // - animation’s hold time is resolved, or
  466. // - animation’s start time is unresolved, or
  467. // - animation has no associated timeline or the associated timeline is inactive, or
  468. // - animation’s playback rate is 0,
  469. if (m_hold_time.has_value() || !m_start_time.has_value() || !m_timeline || m_timeline->is_inactive() || m_playback_rate == 0.0) {
  470. // Set animation’s hold time to seek time.
  471. m_hold_time = seek_time;
  472. }
  473. // -> Otherwise,
  474. else {
  475. // Set animation’s start time to the result of evaluating timeline time - (seek time / playback rate) where
  476. // timeline time is the current time value of timeline associated with animation.
  477. m_start_time = m_timeline->current_time().value() - (seek_time.value() / m_playback_rate);
  478. }
  479. // 3. If animation has no associated timeline or the associated timeline is inactive, make animation’s start time
  480. // unresolved.
  481. if (!m_timeline || m_timeline->is_inactive())
  482. m_start_time = {};
  483. // 4. Make animation’s previous current time unresolved.
  484. m_previous_current_time = {};
  485. return {};
  486. }
  487. // https://www.w3.org/TR/web-animations-1/#update-an-animations-finished-state
  488. void Animation::update_finished_state(DidSeek did_seek, SynchronouslyNotify synchronously_notify)
  489. {
  490. // 1. Let the unconstrained current time be the result of calculating the current time substituting an unresolved
  491. // time value for the hold time if did seek is false. If did seek is true, the unconstrained current time is
  492. // equal to the current time.
  493. //
  494. // Note: This is required to accommodate timelines that may change direction. Without this definition, a once-
  495. // finished animation would remain finished even when its timeline progresses in the opposite direction.
  496. Optional<double> unconstrained_current_time;
  497. if (did_seek == DidSeek::No) {
  498. TemporaryChange change(m_hold_time, {});
  499. unconstrained_current_time = current_time();
  500. } else {
  501. unconstrained_current_time = current_time();
  502. }
  503. // 2. If all three of the following conditions are true,
  504. // - the unconstrained current time is resolved, and
  505. // - animation’s start time is resolved, and
  506. // - animation does not have a pending play task or a pending pause task,
  507. if (unconstrained_current_time.has_value() && m_start_time.has_value() && !pending()) {
  508. // then update animation’s hold time based on the first matching condition for animation from below, if any:
  509. // -> If playback rate > 0 and unconstrained current time is greater than or equal to associated effect end,
  510. auto associated_effect_end = this->associated_effect_end();
  511. if (m_playback_rate > 0.0 && unconstrained_current_time.value() >= associated_effect_end) {
  512. // If did seek is true, let the hold time be the value of unconstrained current time.
  513. if (did_seek == DidSeek::Yes) {
  514. m_hold_time = unconstrained_current_time;
  515. }
  516. // If did seek is false, let the hold time be the maximum value of previous current time and associated
  517. // effect end. If the previous current time is unresolved, let the hold time be associated effect end.
  518. else if (m_previous_current_time.has_value()) {
  519. m_hold_time = max(m_previous_current_time.value(), associated_effect_end);
  520. } else {
  521. m_hold_time = associated_effect_end;
  522. }
  523. }
  524. // -> If playback rate < 0 and unconstrained current time is less than or equal to 0,
  525. else if (m_playback_rate < 0.0 && unconstrained_current_time.value() <= 0.0) {
  526. // If did seek is true, let the hold time be the value of unconstrained current time.
  527. if (did_seek == DidSeek::Yes) {
  528. m_hold_time = unconstrained_current_time;
  529. }
  530. // If did seek is false, let the hold time be the minimum value of previous current time and zero. If the
  531. // previous current time is unresolved, let the hold time be zero.
  532. else if (m_previous_current_time.has_value()) {
  533. m_hold_time = min(m_previous_current_time.value(), 0.0);
  534. } else {
  535. m_hold_time = 0.0;
  536. }
  537. }
  538. // -> If playback rate ≠ 0, and animation is associated with an active timeline,
  539. else if (m_playback_rate != 0.0 && m_timeline && !m_timeline->is_inactive()) {
  540. // Perform the following steps:
  541. // 1. If did seek is true and the hold time is resolved, let animation’s start time be equal to the result
  542. // of evaluating timeline time - (hold time / playback rate) where timeline time is the current time
  543. // value of timeline associated with animation.
  544. if (did_seek == DidSeek::Yes && m_hold_time.has_value())
  545. m_start_time = m_timeline->current_time().value() - (m_hold_time.value() / m_playback_rate);
  546. // 2. Let the hold time be unresolved.
  547. m_hold_time = {};
  548. }
  549. }
  550. // 3. Set the previous current time of animation be the result of calculating its current time.
  551. m_previous_current_time = current_time();
  552. // 4. Let current finished state be true if the play state of animation is finished. Otherwise, let it be false.
  553. auto current_finished_state = play_state() == Bindings::AnimationPlayState::Finished;
  554. // 5. If current finished state is true and the current finished promise is not yet resolved, perform the following
  555. // steps:
  556. if (current_finished_state && !m_current_finished_promise_resolved) {
  557. // 1. Let finish notification steps refer to the following procedure:
  558. JS::SafeFunction<void()> finish_notification_steps = [&]() {
  559. if (m_should_abort_finish_notification_microtask) {
  560. m_should_abort_finish_notification_microtask = false;
  561. m_has_finish_notification_microtask_scheduled = false;
  562. return;
  563. }
  564. // 1. If animation’s play state is not equal to finished, abort these steps.
  565. if (play_state() != Bindings::AnimationPlayState::Finished)
  566. return;
  567. // 2. Resolve animation’s current finished promise object with animation.
  568. WebIDL::resolve_promise(realm(), current_finished_promise(), this);
  569. m_current_finished_promise_resolved = true;
  570. // 3. Create an AnimationPlaybackEvent, finishEvent.
  571. // 4. Set finishEvent’s type attribute to finish.
  572. // 5. Set finishEvent’s currentTime attribute to the current time of animation.
  573. auto& realm = this->realm();
  574. AnimationPlaybackEventInit init;
  575. init.current_time = current_time();
  576. auto finish_event = heap().allocate<AnimationPlaybackEvent>(realm, realm, "finish"_fly_string, init);
  577. // 6. Set finishEvent’s timelineTime attribute to the current time of the timeline with which animation is
  578. // associated. If animation is not associated with a timeline, or the timeline is inactive, let
  579. // timelineTime be null.
  580. if (m_timeline && !m_timeline->is_inactive())
  581. finish_event->set_timeline_time(m_timeline->current_time());
  582. else
  583. finish_event->set_timeline_time({});
  584. // 7. If animation has a document for timing, then append finishEvent to its document for timing's pending
  585. // animation event queue along with its target, animation. For the scheduled event time, use the result
  586. // of converting animation’s associated effect end to an origin-relative time.
  587. if (auto document_for_timing = this->document_for_timing()) {
  588. document_for_timing->append_pending_animation_event({ .event = finish_event,
  589. .target = *this,
  590. .scheduled_event_time = convert_a_timeline_time_to_an_origin_relative_time(associated_effect_end()) });
  591. }
  592. // Otherwise, queue a task to dispatch finishEvent at animation. The task source for this task is the DOM
  593. // manipulation task source.
  594. else {
  595. HTML::queue_global_task(HTML::Task::Source::DOMManipulation, realm.global_object(), [this, finish_event]() {
  596. dispatch_event(finish_event);
  597. });
  598. }
  599. m_has_finish_notification_microtask_scheduled = false;
  600. };
  601. // 2. If synchronously notify is true, cancel any queued microtask to run the finish notification steps for this
  602. // animation, and run the finish notification steps immediately.
  603. if (synchronously_notify == SynchronouslyNotify::Yes) {
  604. m_should_abort_finish_notification_microtask = false;
  605. finish_notification_steps();
  606. m_should_abort_finish_notification_microtask = true;
  607. }
  608. // Otherwise, if synchronously notify is false, queue a microtask to run finish notification steps for
  609. // animation unless there is already a microtask queued to run those steps for animation.
  610. else {
  611. if (!m_has_finish_notification_microtask_scheduled)
  612. HTML::queue_a_microtask({}, move(finish_notification_steps));
  613. m_has_finish_notification_microtask_scheduled = true;
  614. m_should_abort_finish_notification_microtask = false;
  615. }
  616. }
  617. // 6. If current finished state is false and animation’s current finished promise is already resolved, set
  618. // animation’s current finished promise to a new promise in the relevant Realm of animation.
  619. if (!current_finished_state && m_current_finished_promise_resolved) {
  620. m_current_finished_promise = WebIDL::create_promise(realm());
  621. m_current_finished_promise_resolved = false;
  622. }
  623. // Invalidate the style of our target element, if applicable
  624. if (m_effect) {
  625. if (auto target = m_effect->target())
  626. target->invalidate_style();
  627. }
  628. }
  629. // Step 12 of https://www.w3.org/TR/web-animations-1/#playing-an-animation-section
  630. void Animation::run_pending_play_task()
  631. {
  632. // 1. Assert that at least one of animation’s start time or hold time is resolved.
  633. VERIFY(m_start_time.has_value() || m_hold_time.has_value());
  634. // 2. Let ready time be the time value of the timeline associated with animation at the moment when animation became
  635. // ready.
  636. // FIXME: Ideally we would save the time before the update_finished_state call in play_an_animation() and use that
  637. // as the ready time, as step 2 indicates, however at that point the timeline will not have had it's current
  638. // time updated by the Document, so the time we would save would be incorrect if there are no other
  639. // animations running.
  640. auto ready_time = m_timeline->current_time();
  641. // Note: The timeline being active is a precondition for this method to be called
  642. VERIFY(ready_time.has_value());
  643. // 3. Perform the steps corresponding to the first matching condition below, if any:
  644. // -> If animation’s hold time is resolved,
  645. if (m_hold_time.has_value()) {
  646. // 1. Apply any pending playback rate on animation.
  647. apply_any_pending_playback_rate();
  648. // 2. Let new start time be the result of evaluating ready time - hold time / playback rate for animation. If
  649. // the playback rate is zero, let new start time be simply ready time.
  650. auto new_start_time = m_playback_rate != 0.0 ? ready_time.value() - (m_hold_time.value() / m_playback_rate) : ready_time;
  651. // 3. Set the start time of animation to new start time.
  652. m_start_time = new_start_time;
  653. // 4. If animation’s playback rate is not 0, make animation’s hold time unresolved.
  654. if (m_playback_rate != 0.0)
  655. m_hold_time = {};
  656. }
  657. // -> If animation’s start time is resolved and animation has a pending playback rate,
  658. else if (m_start_time.has_value() && m_pending_playback_rate.has_value()) {
  659. // 1. Let current time to match be the result of evaluating (ready time - start time) × playback rate for
  660. // animation.
  661. auto current_time_to_match = (ready_time.value() - m_start_time.value()) * m_playback_rate;
  662. // 2. Apply any pending playback rate on animation.
  663. apply_any_pending_playback_rate();
  664. // 3. If animation’s playback rate is zero, let animation’s hold time be current time to match.
  665. if (m_playback_rate == 0.0)
  666. m_hold_time = current_time_to_match;
  667. // 4. Let new start time be the result of evaluating ready time - current time to match / playback rate for
  668. // animation. If the playback rate is zero, let new start time be simply ready time.
  669. auto new_start_time = m_playback_rate != 0.0 ? ready_time.value() - (current_time_to_match / m_playback_rate) : ready_time;
  670. // 5. Set the start time of animation to new start time.
  671. m_start_time = new_start_time;
  672. }
  673. // 4. Resolve animation’s current ready promise with animation.
  674. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm()) };
  675. WebIDL::resolve_promise(realm(), current_ready_promise(), this);
  676. // 5. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  677. // and the synchronously notify flag set to false.
  678. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  679. }
  680. void Animation::run_pending_pause_task()
  681. {
  682. // FIXME: Implement
  683. }
  684. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_ready_promise() const
  685. {
  686. if (!m_current_ready_promise) {
  687. // The current ready promise is initially a resolved Promise created using the procedure to create a new
  688. // resolved Promise with the animation itself as its value and created in the relevant Realm of the animation.
  689. m_current_ready_promise = WebIDL::create_resolved_promise(realm(), this);
  690. }
  691. return *m_current_ready_promise;
  692. }
  693. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_finished_promise() const
  694. {
  695. if (!m_current_finished_promise) {
  696. // The current finished promise is initially a pending Promise object.
  697. m_current_finished_promise = WebIDL::create_promise(realm());
  698. }
  699. return *m_current_finished_promise;
  700. }
  701. Animation::Animation(JS::Realm& realm)
  702. : DOM::EventTarget(realm)
  703. {
  704. static unsigned int next_animation_list_order = 0;
  705. m_global_animation_list_order = next_animation_list_order++;
  706. }
  707. void Animation::initialize(JS::Realm& realm)
  708. {
  709. Base::initialize(realm);
  710. set_prototype(&Bindings::ensure_web_prototype<Bindings::AnimationPrototype>(realm, "Animation"_fly_string));
  711. }
  712. void Animation::visit_edges(Cell::Visitor& visitor)
  713. {
  714. Base::visit_edges(visitor);
  715. visitor.visit(m_effect);
  716. visitor.visit(m_timeline);
  717. visitor.visit(m_current_ready_promise);
  718. visitor.visit(m_current_finished_promise);
  719. }
  720. }