Animation standards ready to use

There are at least three ways to animate images other than gif.

By Cameron Laird  Add a new comment

'Ever needed to animate a sequence of images, and run into the historical irritation that only GIF defines an effective standard for animation? In fact, there are other choices; if you're viewing this particular page through Safari, Chrome, Opera, or a few other modern browsers (Internet Explorer 9 and Firefox 4 are scheduled to join the crowd later), for example, you see embedded

in this sentence a small bouncing ball realized as an animation of JPEG images.

The techniques this particular Tip discusses are not new, nor are they widely-used, for several reasons:

  • Most Web designers prefer Flash or video formats or other "high-end" solutions;
  • When you do choose image animation, of course, you can in principle re-format your sequence as GIF; and
  • Not all browsers support all the techniques discussed here.

If you know you and any other readers will have a modern browser available, though, and especially if you're looking for a "light-weight" solution, you'll want to know about these possibilities:

  • JavaScript-coded animation;
  • CSS animation; and
  • SVG-based SMIL animation.

Animation has been coded in terms of JavaScript since JavaScript first became available, and continues to be widely used. Nowadays it most often appears as an invocation of a standard library function, for several widely-used libraries support animation. The library vendors adequately document what these libraries do.

Just in the last few years, a more declarative approach has become possible since browsers have begun to support 2008's definition of "CSS animation". Many articles on CSS animation appeared in 2009 and 2010.

SVG to the rescue

My favorite for quick animations, though, is none of these; what I find as the most concise and readable implementation is SMIL animation which SVG builds in. It's quite brief: the entire source is

  <?xml version="1.0"?>
  <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
    "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

  <svg version="1.2"
       xmlns="http://www.w3.org/2000/svg"
       xmlns:xlink="http://www.w3.org/1999/xlink"
       width = '40' height = '40'
  >

  <g id="test"/>
  <image width = '40' height = '40' xlink:href="BouncingBall1.jpg">
    <animate
      attributeName="xlink:href" attributeType="XML"
      values = 'BouncingBall1.jpg;BouncingBall2.jpg;BouncingBall3.jpg;BouncingBall4.jpg'
      dur="1s" repeatCount="indefinite"/>
  </image>
  </svg>

Do you see what this does? SVG's definition gives it the ability to render other image formats, and it can animate all that it knows. To use it this way--SVG without any of SVG's own graphics--is a kind of hack, something on the order of having a conversation with someone in the next room by way of IRC bounced through a server on a different continent. I find it convenient, though, and perhaps you will also. If the infrastructure is there, we might as well use it. More soberly, this kind of hack is, if nothing else, a good opportunity to practice with SVG, and especially to recognize how programmable it is. You can read for yourself how easy it is to insert more images into the values animation sequence, adjust its height and width, and so on. Notice also the robustness of this animation, at least in comparison to JavaScript, where a greedy thread doing other processing has the potential to delay or stall the thread which managers animation.

Browsers are building in remarkable capabilities. It's a good time to use them.

ITworld LIVE

DevelopmentWhite Papers & Webcasts

Webcast On Demand

How to Distribute Apps to Your Mobile Workforce

When considering enterprise app deployment, you may find some unexpected challenges and a number of options that range from simple distribution to running your own enterprise market. How can you determine the best approach for your organization? MOTODEV for Enterprise can help you understand and evaluate current enterprise deployment technologies and learn best practices that support your choice.

Sponsor: Motorola Mobility

Webcast On Demand

Authentication, Certificates and VPNs

MOTODEV for Enterprise can help get you up to speed quickly on key topics such as how to enable secure access to a company intranet from outside the firewall. This webinar provides a clear explanation of terms and technologies and what they can do for your enterprise app development.

Sponsor: Motorola Mobility

Webcast On Demand

Improving Enterprise App Quality with MOTODEV App Validator

MOTODEV for Enterprise supports quality app development for businesses, government, and institutions with technical resources and tools such as the MOTODEV App Validator, a free static analysis testing tool.

Sponsor: Motorola Mobility

White Paper

HR Analytics: Driving Return on Human Capital Investments

In today's economy, it's critical for organizations to make employee retention and development a major business focus, to ensure that valuable employees are not lost as the economy improves. With advanced BI solutions, organizations can be supported by workforce analytics to drive return on human capital investment and to see the value the workforce delivers to organizational performance. This white paper demonstrates how the increased power of having metrics and analytic insight can align core HR business processes with organizational goals and strategies and help ensure organizations make the right business decisions today for tomorrow.

White Paper

Positioning the CIO as a Powerful Business Partner with IT Portfolio Governance

In this whitepaper, learn how you can become a visionary portfolio manager and transform IT into a streamlined revenue and profit center.

See more White Papers | Webcasts

Ask a question

Ask a Question