GAME JOBS
Contents
Making the Move to HTML5, Part 3
 
 
Printer-Friendly VersionPrinter-Friendly Version
 
Latest Jobs
spacer View All     Post a Job     RSS spacer
 
June 6, 2013
 
LeapFrog
Associate Producer
 
Off Base Productions
Senior Front End Software Engineer
 
EA - Austin
Producer
 
Zindagi Games
Senior/Lead Online Multiplayer
 
Off Base Productions
Web Application Developer
 
Gameloft
Java Developers
spacer
Latest Blogs
spacer View All     Post     RSS spacer
 
June 6, 2013
 
Tenets of Videodreams, Part 3: Musicality
 
Post Mortem: Minecraft Oakland
 
Free to Play: A Call for Games Lacking Challenge [1]
 
Cracking the Touchscreen Code [3]
 
10 Business Law and Tax Law Steps to Improve the Chance of Crowdfunding Success
spacer
About
spacer Editor-In-Chief:
Kris Graft
Blog Director:
Christian Nutt
Senior Contributing Editor:
Brandon Sheffield
News Editors:
Mike Rose, Kris Ligman
Editors-At-Large:
Leigh Alexander, Chris Morris
Advertising:
Jennifer Sulik
Recruitment:
Gina Gross
Education:
Gillian Crowley
 
Contact Gamasutra
 
Report a Problem
 
Submit News
 
Comment Guidelines
 
Blogging Guidelines
Sponsor
Features
  Making the Move to HTML5, Part 3
by David Galeano, Duncan Tebbs [Programming, Social/Online, Smartphone/Tablet]
8 comments Share on Twitter Share on Facebook RSS
 
 
March 7, 2013 Article Start Previous Page 2 of 3 Next
 

Resource Loading

Loading of resources in the browsers requires HTTP requests, and these requests have high overhead. Depending on the browser the cost can be between 300 and 700 bytes per request, and potentially high latency. The latency will increase with the distance to the servers hosting the data, to between tens of milliseconds to hundreds of milliseconds. Browsers do load multiple resources in parallel but they have a fixed limit of how many resources they load at the same time, usually between 4 to 8 resources in parallel. For example, you can request 1000 resources at the same time but only four may be actively downloading in parallel. Data from these resources will be passed to event callbacks serially as it becomes available.

A good way to reduce latency is to generate requests to servers as close as possible to the user. This requires a network of servers distributed across the world that can serve content to users in their region, usually known as a Content Delivery Network (CDN).



Obviously, connection bandwidth will also affect load times. Average download speeds can range from 200 kilobytes per second to 5 megabytes per second depending on the country.

With these limitations there are two critical recommendations:

  1. Download as little as possible for what is being rendered on screen.
  2. Convince the browser to cache as much as possible locally.

#1 requires traditional techniques employed when loading from an slow optical medium:

  • Aggressively compress data offline. Browsers do provide automatic decompression of files encoded as gzip.
  • Sort and group your data according to when it is needed.
  • Keep downloading data in the background for what may come next.
  • Provide several levels of detail for heavy resources and load different ones based on speed or need. For example, load low quality textures first and only request the high quality ones later if the connection is fast or the user demands high quality.

The most common compression format supported by all browsers is gzip. Your servers will need to respond to the request with special HTTP headers to indicate the compression format and the browser will automatically decompress the data before passing it to game code. Although gzip is a standard format, the size of the compressed files will vary significantly from compressor to compressor. We found that 7-Zip generates the smallest files (this tool supports several compression formats but only gzip will work natively on your browser). Remember that every byte counts, not only because when hosting data in the cloud you will pay for the volume of data stored and transferred, but also loading times will suffer with very slow connections.

An additional form of compression for JavaScript code is minification. JavaScript code can be quite verbose, and even with gzip compression, all the helpful comments, descriptive variable names and white space formatting adds unnecessary bytes to the download. Several minification tools exist that can reduce the size of JavaScript code to around 25 percent of the original, without changing its functionality. These tools rename local variables, remove comments, remove unnecessary white space, etc:

  • YUI Compressor
    • Venerable tool written in Java. In our tests this tool generated the biggest files.
  • UglifyJS
    • Tool written in JavaScript running on Node.js. In most of our tests this tool generated the smallest files and in the least amount of time.
  • Closure Compiler
    • Advanced tool written in Java. On some of our tests this tool generated the smallest files, but usually taking five times longer to do it.
    • This tool provides optional advanced code manipulation that can significantly reduce file size, but the generated code can have different behaviour than the original, and in some cases this could actually break your application.

The second of our recommendations (persuade the browser to cache data) requires also playing with the HTTP header that the server returns with the requested data. Basically the server needs to tell the browser for how long the data is valid and when the browser should check again for an updated version. Of course the browser will still do whatever it wants in many cases. It may decide to cache only really small files or to reserve a very small amount of disk space for the cache, constantly purging and updating it, but most browsers will try to honor the "time to live" information. There are two main ways to tell a browser for how long it should cache your data:

  • Using the HTTP headers Last-Modified and Expires.
    • The first header represents that time that the data was last modified, and the second header represents the time the data will expire. For example:
      • Last-Modified: Thu, 08 Dec 2011 12:07:02 GMT
      • Expires: Mon, 30 Jan 2012 18:05:22 GMT
  • Using the HTTP header Cache-Control.
    • Specifies for how many seconds the data is valid, and whether it can be cached for everyone or just the current browser user. For example:
      • Cache-Control: max-age=3600, public

Servers can return both sets of headers for the same file, but we recommend using only the latter because of its simplicity.

The expire or max-age information gets stored per resource, so if the values are too aggressive the browser may not ask for a new version of the file for a long time. If you are updating data or code then your changes may not be reflected for a long time. In the case of both functionality updates and bug fixes this can conflict with the need to deploy new versions as soon as possible. To avoid this issue, resources are usually given unique names. In this way, new resources will be requested by an updated name, which will bypass the existing cache and force a reload of the new data. Unique names are generated either from an incremental version number or the hash of the contents.

At Turbulenz our resources are named with the hash of their contents, and references are translated at runtime from their logical names (e.g. mymesh.dae) to the unique physical ones (e.g. <hash>.dae.json). This allows us to tell the browser to cache the data for 10 years, which can improve loading times dramatically when playing the game for a second time. As updated resources get new unique names, we can release updates almost immediately. Obviously the resource that performs the translation from logical to physical is not cached at all because that information is dynamic.

The AppCache API is worth noting at this point. This allows developers to declare in advance the resources that will be required so the browser can download them ahead of time. The developer has control over what gets cached and what doesn't, and can use this interface to create web applications that work offline.

 
Article Start Previous Page 2 of 3 Next
 
Top Stories

image
Microsoft's official stance on used games for Xbox One
image
Keeping the simulation dream alive
image
A 15-year-old critique of the game industry that's still relevant today
image
The demo is dead, revisited
Comments

Kevin Gadd
profile image
Re: Audio, please be aware that Linux builds of Chrome often don't support MP3 (or H264, but that probably doesn't matter as much to you) - I think because of patent issues. So make sure you aren't just handing MP3s to Chrome users, and that you actually do format detection/fallback.

David Galeano
profile image
You are right, feature detection is the right thing to do, and that is what we do, we check supported formats to load the appropriate assets.

Steven Christian
profile image
MP3 has messy licencing fees; people just use it because it is familiar or because they aren't aware of the MP3 patents that are costing developers money.

David M
profile image
Good article, but I'm still not clear why HTML5 is attractive to anyone. It's clear that Apple and Google are going to keep pushing native apps on mobile, so HTML5 is basically useful only for desktop browser development at this point, and even then there is clearly a lot of technological risk.

The Turbulenz games feel quite slow and laggy on my Windows PC. If you are still developing browser games, I don't really see the advantage of adopting HTML5 over Flash at this stage. As much as I dislike proprietary languages, there are a lot of talented Flash developers who have shipped great titles. HTML5 developers who have shipped actual games seem fairly rare by comparison, and there have been a number of high profile failures by studios who relied on HTML5 for their game tech.

David Galeano
profile image
Could you give us more information about the machine and browser used to try the Turbulenz games?

Maciej Bacal
profile image
You can develop native apps for Windows 8 devices, Ubuntu lets you do the same, with Ejecta you can create close to native games for iOS, even without it you can wrap the game code with a browser and launch it like a native application. You can't do these things with Flash. Beside that HTML5 is like 2 years old maybe, in like 3 years time everything will be capable of running HTML5 at a decent speed, including the mobile and the support IS going to get better, even if, as you say, Google and Apple will push for native (which i haven't heard anywhere TBH, especially from Google). Beside Firefox and IE every major browser is going to be running on webkit and contributing to it, including Opera. IE 10 already has great HTML5 support, the only real issue is Firefox, that's about it. Sure, NOW Flash is better and C++ is better, but think like 1 year from now.

Phil M
profile image
I like the idea of HTML5, and this is coming from a Flash developer, but the problem HTML5 has is not so much the language/tech which will improve over the coming years, no the problem is the environment in which it will try to operate. Flash got where it got because of one very crucial thing and that was it was one file (.swf) that you could send around the internet. The important thing here again wasn't so much the tech, but the fact that people other than the developer could make money off of it (by ads around the .swf which were hosted on their website). This led to a whole ecosystem growing up around the distribution of the .swf file.

HTML5 doesn't allow for that financial benefit, and therefore will always be held back. It can work as a technology on already big sites, sites that already have lots of traffic and are established so it makes sense to use it as a tool to create advergames for example.

And has already been said, when all said and done where is the reason for platform holders to really push a platform which is so open? the money for platform holders is in that 30% how would they get that from an open environment?

So HTML5 will no doubt continue to evolve but the future is closed game markets, whether it be iOS, Android, Windows or Steam. So yes for web animation etc, but it probably won't be the tech everyone is using in 5 years time to make games.

Peter Moskovits
profile image
Nice article.
On the WebSocket/communications aspects, you may want to check out some examples showcasing the technology:
o Use your phone as a remote control to drive an HTML5/WebGL race car on your computer screen: http://goo.gl/mrBmK
o Multi-user light table demo: https://vimeo.com/38902371
o A Step-by-Step Tutorial of Building a Simple Peer-to-Peer WebSocket Application: http://tutorial.kaazing.com

To learn more about the underlying technology, The Definitive Guide to HTML5 WebSocket [disclaimer: a book I co-authored] is a very good starting point: http://www.websocket.org/book.html


none
 
Comment:
 




UBM Tech