{"id":27,"date":"2007-08-13T15:45:52","date_gmt":"2007-08-13T23:45:52","guid":{"rendered":"http:\/\/mozillalabs.com\/2007\/08\/better-animations-in-firefox-3\/"},"modified":"2007-08-13T15:45:52","modified_gmt":"2007-08-13T23:45:52","slug":"better-animations-in-firefox-3","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/labs\/2007\/08\/better-animations-in-firefox-3\/","title":{"rendered":"Better animations in Firefox 3"},"content":{"rendered":"<p>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 <a href=\"http:\/\/en.wikipedia.org\/wiki\/GIF\">GIF image format<\/a> has a number of limitations and is overdue for replacement. The <a href=\"http:\/\/en.wikipedia.org\/wiki\/PNG\">PNG image format<\/a> 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 <a href=\"http:\/\/wiki.mozilla.org\/APNG_Specification\">Animated PNG format<\/a> (APNG) is a simple extension to PNG, making it superior for animations too.<\/p>\n<p><strong>The problem<\/strong><\/p>\n<p>So, what&#8217;s wrong with GIFs? The two most apparent problems are that GIF images can only contain 256 colors, and lack support for <a href=\"http:\/\/en.wikipedia.org\/wiki\/Transparency_(graphic)\">partially-transparent<\/a> 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&#8217;t always work well in different situations &#8212; especially if the image is animated. Let&#8217;s look at an example of this problem&#8230;<\/p>\n<p><strong>Example: the Firefox &#8220;throbber&#8221;<\/strong><\/p>\n<p>In Firefox 2, the animation used to indicate that a page is still loading (affectionately known as a &#8220;<a href=\"http:\/\/en.wikipedia.org\/wiki\/Throbber\">throbber<\/a>&#8220;) is an animated GIF. Here&#8217;s the actual image used on OS X, placed against some different backgrounds:<\/p>\n<p>.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);}<br \/>\n<img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/apng\/loading_16.gif\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/apng\/loading_16.gif\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/apng\/loading_16.gif\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/apng\/loading_16.gif\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/apng\/loading_16.gif\"><\/p>\n<p>You can see that when used on a light gray background (OS X&#8217;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.<\/p>\n<p>Here&#8217;s the throbber, now rendered as an APNG: [If you&#8217;re viewing this page with Gran Paradiso, the development version of Firefox 3, you&#8217;ll see the images below as they, uhh, throb. If you&#8217;re not, you should just see the static first frame &#8212; APNG is backwards compatible with existing PNG viewers.]<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/blogimg\/macthrob-small.png\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/blogimg\/macthrob-small.png\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/blogimg\/macthrob-small.png\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/blogimg\/macthrob-small.png\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/blogimg\/macthrob-small.png\"><\/p>\n<p>The APNG version clearly looks better against different backgrounds. And that&#8217;s useful to have in practice, as it allows Firefox themes and extensions (like <a href=\"https:\/\/addons.mozilla.org\/en-US\/firefox\/addon\/3810\">Chromatabs<\/a> \ud83d\ude42 to change the browser&#8217;s chrome while reusing the existing throbber, and not end up with an ugly mess.<\/p>\n<p><strong>A chicken and egg&#8230;<\/strong><\/p>\n<p>A spiffy new animation format is nice, but without an authoring tool no one will use it. But who&#8217;s going build such a tool before anyone is using the format? Chicken, meet egg.<\/p>\n<p>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&#8217;s called &#8220;APNG Edit.&#8221; Not the most creative name, but it&#8217;ll do. Here&#8217;s what it looks like:<\/p>\n<p><a href=\"http:\/\/people.mozilla.com\/~dolske\/blogimg\/apngedit-ui.png\"><img><\/a><\/p>\n<p>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.<\/p>\n<p>The red dinosaur head shown in the screenshot above is &#8220;Chompy,&#8221; who serves as an amusing interstitial while <a href=\"https:\/\/bugzilla.mozilla.org\">Bugzilla<\/a> processes a user&#8217;s search query. Chompy looks a little rough <a href=\"http:\/\/people.mozilla.com\/~dolske\/apng\/chompy.gif\">as an animated GIF<\/a>; problems similar to the GIF throbber above are visible, and it also shows some dithering artifacts.<\/p>\n<p>I obtained the individual frames as PNGs from <a href=\"http:\/\/www.dugnet.org\/klown\/\">Klowner<\/a>, the Mozilla community member who originally rendered Chompy, and was able to quickly load them into APNG Edit and save the resulting animation. Here&#8217;s Chompy as a high-quality APNG:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/apng\/chompy2.png\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/apng\/chompy2.png\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/apng\/chompy2.png\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/apng\/chompy2.png\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/apng\/chompy2.png\"><\/p>\n<p><strong>&#8230;with a side of bacon<\/strong><\/p>\n<p>Oh, and &#8220;one more thing&#8221;!<\/p>\n<p>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 <a href=\"http:\/\/developer.mozilla.org\/en\/docs\/HTML:Canvas\">&lt;canvas&gt;<\/a>, 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&#8217;s far easier to script the drawing tools provided by &lt;canvas&gt; so that the animation can be generated automatically with the parameters you specify.<\/p>\n<p>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:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/blogimg\/macthrob2.png\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/blogimg\/macthrob2.png\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/blogimg\/macthrob2.png\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/blogimg\/macthrob2.png\"> <img decoding=\"async\" src=\"http:\/\/people.mozilla.com\/~dolske\/blogimg\/macthrob2.png\"><\/p>\n<p>Here&#8217;s an existing <a href=\"http:\/\/developer.mozilla.org\/en\/docs\/Canvas_tutorial:Basic_animations#An_animation_example_2\">&lt;canvas&gt; demo<\/a>, which I easily converted into a script for generating an APNG:<\/p>\n<p><img><\/p>\n<p>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 .<\/p>\n<p><strong>Try it, extend it<\/strong><\/p>\n<p>Try out the editor for yourself. You&#8217;ll need:<\/p>\n<ul>\n<li> <a href=\"http:\/\/www.mozilla.org\/projects\/firefox\/3.0a7\/releasenotes\/\">Gran Paradiso Alpha 7<\/a> (or newer). APNG Edit does not work on Firefox 2.<\/li>\n<li> The <a href=\"https:\/\/addons.mozilla.org\/en-US\/firefox\/addon\/5519\">APNG Edit extension<\/a> from the Mozilla Addons site.<\/li>\n<li>(Optional) Some <a href=\"http:\/\/people.mozilla.com\/~dolske\/apng\/examples.zip\">sample scripts and images<\/a> to experiment with.<\/li>\n<\/ul>\n<p>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&#8217;s some ideas for future directions:<\/p>\n<ul>\n<li>Add the ability to edit frames in an existing APNG or animated GIF (needs changes to imgIDecoder)<\/li>\n<li>Reorder frames by drag&#8217;n&#8217;drop<\/li>\n<li>Save APNGs as color mapped 8-, 4-, or 2-bit images (for decreased file size)<\/li>\n<li>Scripts to perform more kinds of frame manipulations.\n\t<\/li>\n<li>Sandboxing of scripts to prevent malicious code<\/li>\n<\/ul>\n<ul>\n<li>Cropping frames<\/li>\n<li>Scaling frames<\/li>\n<li>Animated text with the new &lt;canvas&gt; <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=339553\">drawString()<\/a> API<\/li>\n<\/ul>\n<li>SVG-based frame rendering<\/li>\n<p><strong>Gratuitous spinning logo<\/strong><\/p>\n<p><img><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a class=\"go\" href=\"https:\/\/blog.mozilla.org\/labs\/2007\/08\/better-animations-in-firefox-3\/\">Continue reading<\/a><\/p>\n","protected":false},"author":51,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/labs\/wp-json\/wp\/v2\/posts\/27"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/labs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/labs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/labs\/wp-json\/wp\/v2\/users\/51"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/labs\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/labs\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/labs\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/labs\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/labs\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}