In this second installment of a three-part series, seasoned console developers, formerly of Criterion, describe the process of moving to HTML5 development, covering the essentials, including timers and graphics. The first part of the series can be found here.
In Part 1 of this series, we introduced HTML5 for developing high quality games, giving an overview of what developers can expect when transitioning from other platforms. In this second article we talk about specific areas of functionality provided by HTML5 and related technologies, with particular focus on games.
In some cases there are multiple interfaces for a given subsystem, each with their own characteristics and availability. We discuss how these interfaces can be used to deliver a high quality gaming experience across a range of browsers and platforms, and talk about strategies we have adopted in implementing the Turbulenz game platform.
An example of the level of quality that can be achieved with well-optimized code using the latest browser APIs can be seen in this presentation of the Turbulenz Engine at WebGL Camp Europe.
This global window object contains all other global objects, meaning that these two statements behave identically:
var r = window.Math.sqrt(4); // explicit
var r = Math.sqrt(4); // implicit
We recommend using window explicitly when checking for the existence of global objects.
The window object provides some fundamental functionality to the application:
- Timers and intervals.
- With setTimeout/clearTimeout and setInterval/clearInterval.
- An interval can be used to execute the main game loop, by requesting that the browser invoke a callback function every 16 milliseconds, for example.
- setTimeout and setInterval both work with integer values of milliseconds, so there is no chance of getting a stable 60 frames per second using this API naively.
- Alerts and confirmations.
- With alert, confirm, and prompt.
- To show modal dialogs with information for the user or to request confirmation.
- Not recommended for frequent use, only for critical notifications.
- Screen information.
- Via the screen object.
- Minimal information about the desktop, its dimensions and color depth.
- Browser information.
- Using the navigator object.
- Information about the browser itself, browser name, version number, OS platform and the user agent string it uses when talking to your servers.
- With addEventListener and removeEventListener.
- For keyboard and mouse events among other things.
- HTML DOM
- The document object provides access to all HTML elements in a page.
- Your game will render into an HTML element so controlling its location, dimensions and behaviour may require some HTML element operations. Libraries such as jQuery take care of the different issues and incompatibilities between browsers and are often used instead of trying to manipulate elements manually.
- The console object provides logging functionality. Although not supported initially, nowadays it either exists by default on the browser, or extensions exist to emulate it (for example Firebug includes an implementation for Firefox).
The Math object provides scalar mathematical operations and constants -- possibly one of the most reliable features across browsers.
All mathematical operations and constants provide double precision floating point values. Basic math operations like addition also work with double precision if the arguments do not fit on a 32-bit signed integer.
The Date constructor creates Date objects that provide basic date and time functionality. This constructor also provides a useful method called now that returns the number of milliseconds since the Unix epoch, without the need to create a Date object. The resolution of the time information varies by browser and is generally between 4 milliseconds to 15 milliseconds.
This object provides support for making HTTP requests. These requests could be asking for a specific resource or may represent operations to execute on the server. Browsers use the HTTP protocol as the main form of communication between the client and the server.
Although the object XMLHttpRequest requires different creation code depending on the browser (generally Internet Explorer is different from the rest) it has become common functionality so only really ancient browsers will lack support for it. Code like the following can be used to create an XMLHTTPRequest:
xhr = new window.XMLHttpRequest();
else if (window.ActiveXObject)
xhr = new window.ActiveXObject("Microsoft.XMLHTTP");
Despite its name, XMLHttpRequest can download any resource type, text or binary. This code demonstrates how to support loading binary data:
xhr.open("GET", url, true);
if (xhr.hasOwnProperty && xhr.hasOwnProperty("responseType"))
xhr.responseType = "arraybuffer";
else if (xhr.overrideMimeType)
xhr.setRequestHeader("Content-Type", "text/plain; charset=x-user-defined");
You can then access the resource data:
if (xhr.responseType === "arraybuffer")
buffer = xhr.response;
else if (xhr.mozResponseArrayBuffer)
buffer = xhr.mozResponseArrayBuffer;
var text = xhr.responseText;
var numChars = text.length;
buffer = ;
buffer.length = numChars;
for (var i = 0; i < numChars; i += 1)
buffer[i] = (text.charCodeAt(i) & 0xff);
We will talk about resource loading in more detail in a later article.