Animated .Gif VS Spritesheet + Js/Css

Animated .GIF vs Spritesheet + JS/CSS

As I was curious, I implemented it in javascript.

JsFiddle demo (edited image host as per comments).

What I found out:

  • It works! And better than expected.
  • The CPU usage is actually low (did not test it in IE6 dinosaur and I'm sure it would require "fixes").
  • The size can be cut off, and/or quality can be increased, significantly (source-dependent).
  • Unlike Mikey G.'s concept, this works even if you zoom in/out (try it in both).
  • It allows variable frame duration just like a gif.
  • With more work, it could even have a player-like API (pause/resume, fastforward, skip, etc...).
  • Leverages other formats: 8-bit alpha PNG, progressive JPEG, <canvas>, SVG, WebP...

More info in the JsFiddle demo page.

Why not animated GIF instead of animated CSS sprites?

  • Control

    You have no control over animated GIFs. You can't start them, you can't stop them. They just animate as soon as they load.

    With sprites, you can control the animation. You can start, stop and react to browser events, pan through the animation. For example, Google Doodles usually activate when you click on them.

    A nifty GIF control system can be found in the 9gag. You can start them by appending them to the DOM, and stop them by removing them and swapping them with a pre-generated "first-frame view". But that's as far as GIFs go.

  • Independent Instances

    When you load multiple instances of the same GIF, all these instances use the same image across the page and move at the same time. If you have a row of dancing unicorns GIFs, they'd be dancing at the same time. Synchronized dancing!

    But with sprites, even if you are using the same images, the animation relies on the underlying script. So if one sprite is animated by one script and another by another script, both animations can run independently, and differently from one another.

    This saves you from creating another GIF and it's easy to modify since you are only changing the script.

  • Ensuring smooth animation

    Animated GIFs just animate while loading, and when the internet is slow, the animations freeze up until more of the image gets loaded.

    In contrast, the advantage of sprites is you can pre-load them, ensure all images load beforehand. This makes sure that the resources used for that animation are already loaded prior to animation to make sure it animates as smooth as possible.

    However, GIFs are still images. You can dynamically load them off the DOM and listen for a load event before you append them to the DOM.

  • Partial rendering

    With PNG sprites, you can do "partials" in the animation, breaking an animation scene to parts. For example, when a character stands still, but the arms are waving. You don't need to animate the entire character, or the entire scene. You can place an element depicting the sprite of the character's body in a "freeze" state while the arms are a different element that is animating. This conserves space and size of the sprite sheet. A good example for this was the 2012 Mother's Day Doodle by Google.

    In contrast, most of the time, every frame in a GIF animation is whole image, and animates whether or not anything in it moves. The more frames, the bigger the size of the GIF.

  • GIFs just don't scale

    GIFs were meant for icons. The ratio of file size to image size don't scale up that well in GIFs as compared with PNG and JPG.

Can you control GIF animation with Javascript?

You can do it with a single image using CSS sprites.

Sprite multiple gif files with css info

Thanks to ImageMagick forum I finally found the answer.

Sample Image+Sample Image+Sample Image+

convert -background none \
\( smilie_yellow.gif -coalesce +append \) \
\( smilie_green.gif -coalesce +append \) \
\( smilie_red.gif -coalesce +append \) \
-append -flatten 3_smilies.png

= Sample Image



Related Topics



Leave a reply



Submit