Search code examples
gogifanimated-gif

How to create a gif animation that plays only once


I am trying to generate an animated gif that plays the frames only once.

When I set LoopCount to 1, it plays the frames twice. When I set LoopCount to 0 or -1, it loops infinitely.

img := image.(*image.Paletted)

outGif := &gif.GIF{}
outGif.LoopCount = 0
outGif.Image = append(outGif.Image, img)
outGif.Delay = append(outGif.Delay, 10)

f, err := os.Create("/tmp/test.gif")
if err != nil {
    panic(err)
}
defer f.Close()

gif.EncodeAll(f, outGif)

How to I make sure it only plays the frames once?


Solution

  • Browsers interpret differently the loop counter. For a single loop gif, Chrome is looping twice while Firefox is playing the animation exactly once.

    See this issue of GifCreator - Unable to create non loop GIF image.