Behavior, Content, Money – 3 Things you should never give away for free!!!

BCmoney MobileTV

Animations on the web – Applets .vs. Flash .vs. SVG .vs. setTimeout .vs. jQuery.animate .vs. requestAnimationFrame .vs. CSS3 animations

Posted by bcmoney on October 23, 2014 in CSS, Flash, Java, JavaScript, Multimedia with No Comments


No Gravatar

In a follow-up to my prior article on Interactive Graphics on the web from a few years back, today I’ll be looking a bit closer at the latest Animation options on the web.

Animations have always been the specialty of Flash and before that pioneered by Java Applets on the web, but with the introduction of HTML5 and CSS3 things are sure changing fast!

This post is a quick round-up of the leading approaches to do something that a decade ago when I was first learning to develop I would have had trouble believing were even possible without a plugin/tool like Flash or Java. That is to simply move a box around through animation, moving it from left-to-right (but could be any direction) across the screen perpetually within a user’s browser, with simple controls to control the starting and stopping of the animation.

Java Applets

Historically the first (and back then pretty much only) way to accomplish your dynamic and interactive content needs, Java Applets offered the ability to code “Rich Internet Applications” in Java, similar to what it could already do on the Desktop via AWT GUIs, but run them on the web in the user’s browser client.

-or-

This loses alot of marks because of the sorry state of Java plugins and security issues since Oracle took over from Sun Microsystems and seemingly de-prioritized Java’s RIA features in favour of backend and enterprise parts of the stack which have a closer potential tie-in to its database and related products/services. Its also more cumbersome as it depends on the Java Runtime Environment (JRE) being installed to work, along with a Java browser plugin (IcedTea for OpenJDK or JavaWebStart for Sun/Oracle) so it would not work on a majority of mobile devices without rooting the device and layering in Java support (if even possible – depending on device type it may not be).

Many legacy devices do run Java Mobile/Micro Edition (JME) already, but the average user would have trouble even with some of the installer tools out there. Users also grow tiresome of the constant updates, warnings and security concerns that Applets present in the browser. Java in the browser lives on in the form of JavaFX and JNLP app launchers, but Applets are on their way out slowly but surely.

Score: 2.5/10 (I recall being wowed when I first saw dynamic and highly interactive content being rendered by Java, but have to admit the web has changed a lot since the 1990s).

Flash

Shortly after the debut of Applets, Macromedia launched their web-version of Shockwave (later Flash). Flash used a concept of “Motion tweens” where you basically draw out by hand the path that you want the animation to take, and the Flash plugin/player takes over and renders the desired motion path on your graphic objects, the desired number of times (or endlessly on a cycle).

-or-

This can be quite performant on Desktop devices that have the Flash plugin installed and enabled (most Computer browsers at this point in time), however not so much on Mobile devices. Also, thanks to Steve Jobs’ infamous statement that he’d never allow Flash on the iPhone due to its known security and performance issues on mobile (FlashLite), it may never be the case that you can rely on Flash to hit a majority of mobile browsers. This could still be a decent option however, if you’re already invested heavily in the Flash stack and/or Flex framework for your multimedia needs, and are ok focusing on Computer users. With Flash suffering pretty much as many security issues of late as old school Java Applets, I’m guessing most Developers are rejoicing with the other options emerging.

Score: 3/10 (I also have fond mostly painful memories struggling to get my first “motion tween” working in Flash Studio 2004; again, the web is moving to more built-in options for animation needs as we’ll soon see, but for a quick one-off this is still a viable option for a few more years, as long as you don’t need to support iPhone users).

SVG

Scalable Vector Graphics (SVG) is one of the up-and-coming mechanisms for multimedia on the web. I recall when it was initially being pushed by W3C in the early 2000s, but at that time, you needed a dedicated browser extension just to render the SVG. Later, JavaScript cross-browser polyfills and libraries like Raphael.JS, Processing, and D3 emerged which built heavily on SVG (or supported it as a primary export format).

The

https://output.jsbin.com/bubabil

Score: 4/10 (I also have fond mostly painful memories struggling to get my first “motion tween” working, again, the web is moving to more built-in options for animation needs)

setTimeout

This approach in vanilla JS uses a combination of “setTimeout” and “clearTimeout” (along with a helper “global variable” you can’t really get around).

-or-

The problem is that first of all its fairly obviously laggy due to rounding errors in the calculation of nanoseconds (basically the timer is leaky and gets compoundingly inaccurate the longer it runs); but worst of all, if left running in an idle window too long it can become even further janky, jump all over the place, and consume too many resources causing the Tab to crash. This is not a viable animation option unless you are detecting the “active” state of the browser client to ensure the animation is only played when the user is actively on the page and interacting within the desired content regions such that its worth showing the anitmation. Even then, there’s no guarantee it will work equally well across Mobile and Desktop browsers.

Score: 5.5/10 (cool idea that it’s possible, but not the best approach for serious uses)

jQuery.animate();

This is probably the leading go-to answer if you ask StackOverflow, as usual, the Dev community seems to prefer to hand-off the cross-browser edge cases to jQuery which “usually” performs reasonably well. This makes sense, why reinvent the wheel? In this case though, this solution too will lead to jankyness if you leave it running over longer periods of time, but not nearly as wildly as the first setTimeout example without jQuery, as jQuery’s $.animate() doesn’t suffer from the same nanosecond rounding errors as setTimeout does.

-or-

Score: 7/10 (convenient enough, but still not even close to the best performing approach)

requestAnimationFrame();

This is the new HTML5 + JavaScript option that was previously not available. Depending on your target browsers, you may want to check its availability, but its shipping in Firefox, Chrome, Safari and even IE 10+ now, so it should be ready for prime time!

It performs better because it uses purpose-built animation APIs rather than relying on the single client-thread’s timer to continue running and stay accurate, or, and cludgy browser workarounds and approximations that attempt to overcome this with the jQuery.animate() approach.

-or-

The only downsides here, is that for now, its still a bunch of extra code to ensure you’re covering all the browsers with cross-browser vendor prefixes, other than that its fairly smooth and fluid, handily beating both the vanilla JS setTimeout and jQuery.animate optons.

Score: 8.5/10 (convenient enough, but still not even close to the best performing approach)

CSS3 Animations

Last, but certainly not least, what I’m probably most excited about is CSS3 Animations, natively within the browser… I mean how crazy is that?! This stuff would have been unheard of a few short years ago.

-or-

Ok so while not totally 100% CSS3 only, we need at least a tiny snippet of JS just to control the currently active class based on the desired state of the animation, toggling the starting of the motion through a class called “running” being added, and stopping of the motion through a class called “paused” being swapped out instead. Still, I found this pretty impressive once I realized the possibilities of CSS3 Animations & Transitions… the future will be interesting.

Score: 9.5/10 (I might be biased on this one, but I’m blown away by the fact this can now be done in CSS!)

Just to be comprehensive I’ll at least mention there’s also other options like Animated GIFs, VRML/X3D, Canvas, JavaFX, Flex, Silverlight, Unity, and even WebGL; but for practical animation needs, and programmatic access to control the animation, those listed above are the most accessible and sensible options for developers today.

Leave a Reply

No trackbacks yet.

No post with similar tags yet.

Posts in similar categories