One of the cool new features coming in Firefox 3 is support for a new animated image format. Browsers have supported animated GIFs for more than a decade, but the GIF image format has a number of limitations and is overdue for replacement. The PNG image format is now widely accepted as a superior replacement for static GIF images, but for animated GIFs there has not yet been a clear successor. The new Animated PNG format (APNG) is a simple extension to PNG, making it superior for animations too.
The problem
So, what’s wrong with GIFs? The two most apparent problems are that GIF images can only contain 256 colors, and lack support for partially-transparent pixels. This results in images which often look grainy or rough, and which do not smoothly blend into their background. A clever designer can somewhat mitigate these problems, but the tricks used don’t always work well in different situations — especially if the image is animated. Let’s look at an example of this problem…
Example: the Firefox “throbber”
In Firefox 2, the animation used to indicate that a page is still loading (affectionately known as a “throbber“) is an animated GIF. Here’s the actual image used on OS X, placed against some different backgrounds:
.demoimg1 { padding: 1em; background-color: #404040;} .demoimg2 { padding: 1em; background-color: #ebebeb;} .demoimg3 { padding: 1em; background-color: #40ff40;} .demoimg4 { padding: 1em; background-color: #dd0041;} .demoimg5 { padding: 1em; background-image: url(http://people.mozilla.com/~dolske/apng/checker.png);}
You can see that when used on a light gray background (OS X’s default color), the throbber looks just fine. But against any other background, it looks terrible. Why? To antialias the image, in order to make it look smooth, pixels must be blended to a fixed color (the expected background). If partial transparency was supported, the antialiased pixels could be made appropriately transparent, and the animation would look fine against any background.
Here’s the throbber, now rendered as an APNG: [If you’re viewing this page with Gran Paradiso, the development version of Firefox 3, you’ll see the images below as they, uhh, throb. If you’re not, you should just see the static first frame — APNG is backwards compatible with existing PNG viewers.]
The APNG version clearly looks better against different backgrounds. And that’s useful to have in practice, as it allows Firefox themes and extensions (like Chromatabs 🙂 to change the browser’s chrome while reusing the existing throbber, and not end up with an ugly mess.
A chicken and egg…
A spiffy new animation format is nice, but without an authoring tool no one will use it. But who’s going build such a tool before anyone is using the format? Chicken, meet egg.
Luckily, here at Mozilla Labs we have the technology to build both the chicken and the egg! The Mozilla platform can encode as well as decode APNG images, runs on multiple platforms, and has familiar environment for dealing with UI/scripting/graphics. So, yadda yadda, I wrote a Firefox extension to make creating APNGs easy. It’s called “APNG Edit.” Not the most creative name, but it’ll do. Here’s what it looks like:
My goal was to create a simple editor; the primary workflow being to load up a series of images (one for each frame), adjust the various settings for each frame, and then encode and save the finished animation as an APNG file.
The red dinosaur head shown in the screenshot above is “Chompy,” who serves as an amusing interstitial while Bugzilla processes a user’s search query. Chompy looks a little rough as an animated GIF; problems similar to the GIF throbber above are visible, and it also shows some dithering artifacts.
I obtained the individual frames as PNGs from Klowner, the Mozilla community member who originally rendered Chompy, and was able to quickly load them into APNG Edit and save the resulting animation. Here’s Chompy as a high-quality APNG:
…with a side of bacon
Oh, and “one more thing”!
I quickly realized that while a simple editor to assemble frames was a good first step, a Mozilla-based editor could be a lot more powerful with some minimal effort. Why not take advantage of Javascript and <canvas>, so that animations can be dynamically created and modified? Instead of drawing each frame in Photoshop or Gimp (over and over, as you fine-tune how you want the animation to look), it’s far easier to script the drawing tools provided by <canvas> so that the animation can be generated automatically with the parameters you specify.
For example, to make the OS X throbber, I wrote a script to draw lines with the appropriate shape, color, and rotation for each frame. I was curious what a larger version might look like, so I basically just changed the X and Y size specified in my script, and ran it again:
Here’s an existing <canvas> demo, which I easily converted into a script for generating an APNG:
The first version of the clock animation resulted in a rather large file (about 450K) because each frame was a complete rendering. I wrote another script to optimize each frame by only encoding interframe differences, which knocked the size down to 79K. I could also make easy adjustments to the dimensions or frame count, by simply adjusting the script parameters and re-rendering the animation .
Try it, extend it
Try out the editor for yourself. You’ll need:
- Gran Paradiso Alpha 7 (or newer). APNG Edit does not work on Firefox 2.
- The APNG Edit extension from the Mozilla Addons site.
- (Optional) Some sample scripts and images to experiment with.
There are still some rough spots in the editor which need improved, and there are a lot of interesting possibilities for new features. Unfortunately I anticipate most of my time will be spent working on Firefox 3; however I would welcome patches and scripts from anyone interested. Here’s some ideas for future directions:
- Add the ability to edit frames in an existing APNG or animated GIF (needs changes to imgIDecoder)
- Reorder frames by drag’n’drop
- Save APNGs as color mapped 8-, 4-, or 2-bit images (for decreased file size)
- Scripts to perform more kinds of frame manipulations.
- Sandboxing of scripts to prevent malicious code
- Cropping frames
- Scaling frames
- Animated text with the new <canvas> drawString() API
Gratuitous spinning logo
BrianZ wrote on :
FF gold wrote on :
FF Garbage wrote on :
Hawaii wrote on :
Lindsay wrote on :
//arcio wrote on :
Justin Dolske wrote on :
John wrote on :
Anonymous Guy wrote on :
Tapeten wrote on :
Aaron wrote on :
译佰翻译 wrote on :
上海翻译 wrote on :
Holger Will wrote on :
Hawaiian Guy wrote on :
Walter wrote on :
Holger Will wrote on :
Sean Brown wrote on :
Sweet wrote on :
Justin Dolske wrote on :
Lindsay wrote on :
OS wrote on :
Edward D. wrote on :
Holger Will wrote on :
skierpage wrote on :
Stephen Donner wrote on :
Tom Sieron wrote on :
Justin Dolske wrote on :
iPRED wrote on :
iPRED wrote on :
H. Jalonen wrote on :
Gaabi wrote on :
Havvy wrote on :
Gusar wrote on :
Collin wrote on :
Markus wrote on :
alex.r. wrote on :
Steffen wrote on :
Daniel Holbert wrote on :
gamer_boy wrote on :
Gerv wrote on :
Jakub Steiner wrote on :