Animations
Introduction
The manager keeps every frame of an animated GIF, unless you turn off IMAGE_DECODE_ANIMATION. Modifiers apply to every frame, so resizing or watermarking an animation works the same as for a still image. Trim is the exception and refuses animations. Insert draws the inserted image's first frame on every frame:
img, err := image.From(r).Open("resources/images/loading.gif")
if err != nil {
return web.Fail(err)
}
return image.Response(img.Scale(120, 0), image.GIF)Inspecting animations
IsAnimated reports whether the image has more than one frame. Frames returns each frame's pixels and how long the animation shows it:
if img.IsAnimated() {
for _, frame := range img.Frames() {
fmt.Println(frame.Delay) // 100ms
}
}Slicing and removing animations
SliceAnimation keeps a number of frames from an offset, counting from 0. A length of 0 keeps every frame from the offset:
img.SliceAnimation(2, 5) // frames 2 to 6RemoveAnimation keeps a single frame, so the image is no longer animated. This is useful for a still preview:
img.RemoveAnimation(0)Looping
Loops and SetLoops follow the standard library's image/gif. 0 repeats forever and -1 plays the animation once:
img.SetLoops(0)Encoding animations
GIF is the only format that encodes every frame. Other formats encode the first frame. When encoding a GIF, the package maps each frame's colors to the 216-color web-safe palette and keeps transparency.