I have a GIF and I want it to only run once, without any repetitions. Is there any way to do this? How?
I have a GIF and I want it to only run once, without any repetitions. Is there any way to do this? How?
Open the image in a graphical editor and remove the infinite loop option.
Now with Javascript you can create a static image in a canvas
. And after a while (in this case, the duration of .gif) the animated image will be removed and in its place the canvas
will be displayed:
(function(){
var $gif = document.querySelector('.gif'),
GIF_DURATION = 1050;
function handleGif(){
// Cria o canvas com o mesmo tamanho da imagem.
var $canvas = document.createElement('canvas');
$canvas.setAttribute('width', $gif.width);
$canvas.setAttribute('height', $gif.height);
// Desenha a imagem no canvas
var context = $canvas.getContext('2d');
context.drawImage($gif, 0, 0);
// Remove a imagem e insere o canvas
document.body.removeChild($gif);
document.body.appendChild($canvas);
}
var timeout = setTimeout(function(){
handleGif();
clearTimeout(timeout);
}, GIF_DURATION);
})();
<img class='gif' src='http://i.stack.imgur.com/l7bin.gif'/>
Look Daniel found something that might help you: link
With it you can make the "touch" gif just once.
Include gifffer.min.js
on the page.
<script type="text/javascript" src="gifffer.min.js"></script>
Instead of assigning value in the src of your image, use data-gifffer
.
<img data-gifffer="image.gif" />
And finally add the Grifffer();
call wherever you want. For example:
window.onload = function() {
Gifffer();
}