A GIF with several images is an animation only by convention: the file is a series of images, and the graphic control extension before each one says how long it stays and what to do with it afterwards. An image can be smaller than the canvas, so a frame usually holds only what changed.
Drawing the frames
A reader keeps a canvas the size of the logical screen, then for each image:
- Disposes of the previous frame as its disposal method says.
- Draws this image at its position on the canvas, using its local colour table or else the global one. Pixels of the transparent index, and pixels of a colour number past the end of the table, leave the canvas unchanged. (A colour number past the end of the table is not defined by the specification: Chrome draws it black, while macOS’s decoder, which Safari uses, and the explorer leave the canvas unchanged.)
- Shows the canvas for the delay given, then moves to the next image.
The canvas starts out blank. Chrome treats that blank canvas as fully transparent rather than as the background colour, and so does the explorer. Other decoders differ: Pillow, ffmpeg and macOS’s ImageIO paint the part of the canvas that no image has covered with the background colour (or colour 0) when the file sets no transparent colour. Both were checked here against real files and built ones.
Disposal methods
Bits 4 to 2 of the extension’s flags say what happens to a frame once its delay has passed, before the next is drawn:
| Value | Name | What happens |
|---|---|---|
| 0 | Not specified | The reader need not do anything; in practice the frame stays. |
| 1 | Do not dispose | The frame stays on the canvas. |
| 2 | Restore to background | The frame’s area becomes the background colour. Chrome clears it to transparent instead, and so does the explorer. |
| 3 | Restore to previous | The frame’s area goes back to what was there before the frame was drawn. |
| 4–7 | Not defined | Chrome, macOS’s ImageIO and Pillow treat 4 like 3, as some older files use it that way; ffmpeg does not. Chrome treats 5 to 7 like 0 and so does ffmpeg, while ImageIO and Pillow do something else. The explorer follows Chrome. |
The explorer composes frames the way Chrome does: its frames were compared with Chrome’s own decoder on 53 built files and with three other decoders on real files, so that what it shows matches what the same file does when played. Pictures shown by Show frame are the canvas after that frame is drawn.
Delay
The delay is a 16-bit number of hundredths of a second, counted once the frame has been drawn. A delay of 0 means no delay, which is often used to draw a picture from several images without pause. Browsers commonly lengthen very short delays to about a tenth of a second: macOS’s decoder reports a delay of 0 as 0.1 s as its clamped value, which was checked here. The explorer shows the delay in the file.
Looping
GIF89a itself does not say that an animation repeats. The
NETSCAPE2.0 application
extension gives a loop count, and readers that do not find one play the animation
once. A count of 0 means forever; a count of n means n further
plays after the first (Firefox’s FrameAnimator.cpp). The specification
itself says GIF is not intended as a platform for animation, even though it can be done
in a limited way, and tells a decoder to process each graphic in turn without delays
other than those in the control information.
Transparency
A pixel holding the transparent colour index does not change the canvas. That lets a frame hold only the pixels that differ from the last, while the rest of the frame shows through, and is why GIF is often used for simple moving pictures over a plain background. The transparent colour is set frame by frame by the graphic control extension.
Not described
What this page leaves out, and what the explorer does not do with animations.
Known, not yet covered
- Reference: what Firefox does with anything on this page: its source is public but it was not checked. Chrome and the decoders of macOS (ImageIO), Pillow and ffmpeg were.
- Reference: which short delays Chrome and Firefox lengthen: browser source settles it; only ImageIO’s clamp of 0 to 0.1 s was checked.
- Reference: what a browser does with the wait for user input flag: the specification defines the flag; the browser behaviour is not described.
- Viewer: the animation is played by the browser, not by the explorer; its own timing, looping and the user input flag (all defined by the specification) are not applied. The explorer shows the file’s delays and the composed frames.
- Viewer: composed pictures are drawn for the first 24 frames automatically and for any other frame on request; the background colour is not painted and the canvas starts transparent.
- Viewer: plain text between frames is not drawn (see Extensions).
Unknown
- Reference and viewer: disposal methods 4 to 7: the specification says “to be defined”. They are described from built files only, since no real file on this Mac uses 4 to 6; the viewer draws 4 as 3 and 5 to 7 as 0, as Chrome does, while other decoders differ.
- Reference and viewer: a colour number past the end of the colour table: the specification does not define it and decoders differ. The viewer leaves the canvas unchanged.