On the Viability of HTML5 Games
February 1st, 2012There is sometimes significant rancor within the web development community as we transition from Flash to HTML5/JS for our audio-visual and interactive web content.
Let’s look at what went into a real-world game of Cut The Rope. While it was implemented as a promotional tool for the Windows 8 Store and IE9, ZengaLabs reports that it does work reasonably well on all HTML5-friendly desktop browsers.
First, we have the libraries. They chose jQuery 1.7, plus the FancyBox and the jQuery Easing plugins. To this, they added SoundManager 2 and Modernizr 2.0.6. These weighed in at just under 62KB.
Of course, no website is complete without tracking (Google Analytics in this case), as well as Facebook and Twitter integration, but those libraries are typically hosted remotely and are outside of our control.
This leaves us with a 69.7KB minified JavaScript file, ctr.js. This file contains significant swaths that look like
a.lineTo(32.7,183.4); a.bezierCurveTo(31.8,176.3,31,168.9,30.3,161.3);
and
$("#resultStar3").removeClass("starEmpty").addClass("star");
What of the HTML? At a scant 4KB, it is largely boilerplate. Of particular interest is this snippet:
// html5 audio is unreliable in many browsers so its only enabled by default on // IE9 or greater. You can force html5 audio by setting the querystring: // http://www.cuttherope.ie/?html5audio=true soundManager.useHTML5Audio = (isIE9OrGreater() || ZeptoLab.ctr.forceHTML5Audio); soundManager.preferFlash = !soundManager.useHTML5Audio; // initialize the game ZeptoLab.ctr.run();
Note that we’re still resorting to browser-specific hard hacks. This is an understandable trade-off, though falling back to Flash doesn’t work if you also want to function in mobile environments. Our AV content is still forcing us to produce multiple formats.
It is also interesting to see the game effectively self-enclosed inside of a single object following a Java-like naming convention. This not only exhibits best practices, but also encloses it within a closure, making it harder for the code to be attacked.
The developers also reported another concern. Since the code can easily run before all of the required image and audio assets are loaded and the full game weighs in at 6MB, they needed a loader (much like what you have seen in traditional Flash environments). Happily, they released this original code as PxLoader under an MIT license.
Additional overhead is spent tracking the frame-rate to caution the user when performance suffers.
So what lessons can be drawn from this?
For the most part, once ActionScript developers become familiar with JavaScript development, the code they eventually produce will be fairly straightforward. Libraries abstract out most browser incompatibilities, although concurrent Flash and HTML5 development is still a fact of life unless you can be sure your users are on Internet Explorer 9+, Firefox 4+, Safari 5+, Opera 10+ or Chrome 10+.
It is unfortunate that patents on media formats are actually encumbering innovation among the people who would use them. Effectively, all media must be encoded in a minimum of 4 versions: traditional Flash, Chrome, Safari and MPEG. This is a drain on developer time and resources.
Finally, modern browsers do offer sufficient performance to implement complex interactive content natively within JavaScript. Plugins are no longer required to get work done.



