Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping GIF Animation Programmatically

I am developing a Twitter application which references to the images directly from Twitter. How can I prevent animated gifs from being played?

Using window.stop() at the end of the page does not work for me in Firefox.

Is there a better JavaScript hack? Preferable this should work for all browsers

like image 742
Karussell Avatar asked Sep 10 '10 21:09

Karussell


People also ask

How do you pause a GIF in Javascript?

js installed, it's time to start controlling the GIF. Here's how you can pause a GIF then play on command — just add the freezeframe class (or a custom selector) to the GIFs you'd like control over. It's that easy! That's it!

Can I block animated GIFs?

If you want to stop animated GIFs forever: Go to Internet Options (via the Tools menu "gear" at the upper right) Select the Advanced tab. Scroll down to Multimedia to uncheck "Play animations in web pages."

How do you stop a GIF from moving?

The next time you're tired of seeing a GIF on a webpage just hit Esc on your keyboard and the GIF stops. This includes every GIF on a webpage. Even a page filled with animated GIFs, like Giphy, will stop with one key press. Hit Esc again and the GIFs start moving again.

How do you make a GIF stop?

Go to the Window tab and select timeline(if the timeline is not already open). At the bottom of the timeline panel, you will find an option, which says "Forever". Change that to "Once". Go to File> Export> Export for Web and save it as a gif.


2 Answers

Inspired by the answer of @Karussell I wrote Gifffer. Check it out here https://github.com/krasimir/gifffer

It automatically adds stop/play control on top of your Gif.

like image 188
Krasimir Avatar answered Oct 15 '22 00:10

Krasimir


This is not a cross browser solution but this worked in firefox and opera (not in ie8 :-/). Taken from here

[].slice.apply(document.images).filter(is_gif_image).map(freeze_gif);  function is_gif_image(i) {     return /^(?!data:).*\.gif/i.test(i.src); }  function freeze_gif(i) {     var c = document.createElement('canvas');     var w = c.width = i.width;     var h = c.height = i.height;     c.getContext('2d').drawImage(i, 0, 0, w, h);     try {         i.src = c.toDataURL("image/gif"); // if possible, retain all css aspects     } catch(e) { // cross-domain -- mimic original with all its tag attributes         for (var j = 0, a; a = i.attributes[j]; j++)             c.setAttribute(a.name, a.value);         i.parentNode.replaceChild(c, i);     } } 
like image 26
Karussell Avatar answered Oct 15 '22 00:10

Karussell