LibWeb: Reset animated frame index when loading new img element src

When an img element is changed from animated image to static image, the
animation briefly continues into the new image, even if the new image
has only a single frame (static image).

Could also impact when going from animated image to another animated
image, but the new image has less frames versus the previously animated
image.

In some cases a newly loaded static image would continue to be animated,
so that is also stopped.

fixes: #7879
fixes: #7945
This commit is contained in:
Rocco Corsi 2026-02-18 10:04:38 -05:00 committed by Andreas Kling
parent 8867f95f41
commit 7eb0bb7c70
Notes: github-actions[bot] 2026-02-19 09:52:43 +00:00

View file

@ -656,6 +656,7 @@ void HTMLImageElement::update_the_image_data_impl(bool restart_animations, bool
m_current_request = ImageRequest::create(realm(), document().page());
m_current_request->set_image_data(entry->image_data);
m_current_request->set_state(ImageRequest::State::CompletelyAvailable);
m_current_frame_index = 0;
// 5. Prepare the current request for presentation given the img element.
m_current_request->prepare_for_presentation(*this);
@ -918,8 +919,9 @@ void HTMLImageElement::add_callbacks_to_image_request(GC::Ref<ImageRequest> imag
if (!maybe_omit_events || previous_url != url_string)
dispatch_event(DOM::Event::create(realm(), HTML::EventNames::load));
m_current_frame_index = 0;
m_animation_timer->stop();
if (image_data->is_animated() && image_data->frame_count() > 1) {
m_current_frame_index = 0;
m_animation_timer->set_interval(image_data->frame_duration(0));
m_animation_timer->start();
}