Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart a gif animation without reloading the file

Is it possible to restart a gif animation without downloading the file every time? My current code looks like this:

var img = new Image();
img.src = 'imgages/src/myImage.gif';

$('#id').css('background-image', 'url("' + img.src + '?x=' + Date.now() + '")' );

Edit

When I insert the gif into the dom it didn't restart the gif animation. I can only achieve this by appending a random string to the image src but this will download the image again.

I want to know if it is possible to restart the gif animation without downloading the gif.

like image 704
Erfan Ebrahimnia Avatar asked Nov 07 '13 08:11

Erfan Ebrahimnia


People also ask

How do you repeat an animated GIF?

Loop a part of an animated GIF image Or you can right-click and choose 'Split'. Right-click on the other clips and select 'Delete'. You can now add the same GIF file and repeat the process to create a loop.

How do you make a GIF not repeat?

Open the Animated gif in Photoshop. 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".

How do I play a GIF again?

To play animated GIF files, you must open the files in the Preview/Properties window. To do this, select the animated GIF file, and then on the View menu, click Preview/Properties. If the GIF does not play, try re-saving the animated GIF in the collection in which you want to put it.


2 Answers

I've had similar requirement.

var img = document.createElement("img"),
    imageUrl = "http://i73.photobucket.com/albums/i231/charma13/love240.gif";
img.src = imageUrl;
document.body.appendChild(img);

window.restartAnim = function () {
    img.src = "";
    img.src = imageUrl;
}
like image 133
Sai Dubbaka Avatar answered Oct 22 '22 14:10

Sai Dubbaka


for example on facebook - animated emoticons are not .gifs but a set of static frames on png file with dynamically set background offset. This way you have full control over your animation from javascript - you can even pause/unpause it or change its speed.

It's possible to split your .gif file into separate frames and generate a .png file on server side dynamically.

This looks like a good weekend project for me ;)

like image 7
Adassko Avatar answered Oct 22 '22 14:10

Adassko