HTML5 Development Center

Developed for you in part by
 
661-tron-219

Tron: an HTML5 Game in 219 Bytes

By | | Community | HTML | HTML5 | News | Programming | Web standards

I’ve never been keen on this multi-megabyte web page nonsense. Perhaps it’s because I started web development during the dial-up days when a total page weight of 100Kb was considered excessive? Or perhaps it’s simply because I don’t understand why extravagant bandwidth is necessary?

I’ve mentioned a number of 1KB JavaScript and CSS competitions before. There have been some remarkable entries but few are as bandwidth-friendly as 219 Byte Tron. Yes, 219 bytes — which includes all the HTML5 and JavaScript code.

The project started as a challenge and several collaborators contributed to the memory-shaving exercise. You can play it here — use the keys i, j, k and l to start and steer. I found the game worked best in Opera but it’s fine in Chrome and Safari. Firefox and IE9 aren’t compatible.

While it’ll never win any game-play awards or replace your Call of Duty sessions, it’s an impressive exercise in mental agility. The developers have documented each ‘line’ of the code and there are some useful tips and tricks which you could apply to your JavaScript programming.

  • In all browsers except Firefox, assigning an ID to an element exposes the node object as a variable within JavaScript, e.g.
    
    <div id="test">Hello</div>
    <script>test.textContent = "Goodbye";</script>
    
  • Browsers will happily accept non-quoted tag attributes if they don’t contain >, space or tab characters.
  • Variables can be initialized within function calls, e.g.
    
    myFunction(a=1, b=2, c=3);
    
  • A binary & can be used instead of && if both sides of the operator are boolean values.
  • body is the only essential HTML tag on a web page!

Remember that Tron takes byte saving to an extreme level. Some of these tips are useful but you should always weigh that against:

  1. Functionality. For example, just because onkeyup is shorter than onkeydown, that doesn’t necessarily make it the most appropriate event to use.
  2. Readability. Without the documentation, Tron’s code would have been impossible to understand. Few developers would persist in trying to make sense of it.
  3. Compatibility. I advocate cross-browser operation and suspect IE9 and Firefox could be fixed with a few additional bytes.

219 Byte Tron is astounding. While you may never adopt many of these techniques, you should consider what’s possible before adding another megabyte of superfluous bulk to your pages.

Learn Responsive Web Design

Join Learnable $29 Includes all SitePoint books

Craig Buckler

Craig is a Director of OptimalWorks, a UK consultancy dedicated to building award-winning websites implementing standards, accessibility, SEO, and best-practice techniques.

More Posts - Website

{ 19 comments }

freak April 11, 2012 at 7:42 am

Its working actually. I just forgot to press the keys to start the game.

freak April 11, 2012 at 7:36 am

Does not work in Chrome. All I get is a black screen. Not worth 219 bytes.

Ruald April 11, 2012 at 5:50 am

Just a blank black box…. IE9, Firefox11 works in Opera 11.something, once you die it just counts up the score… Nice try though.

Stefan Froelich April 10, 2012 at 2:46 am

For all those complaining about the “e is not defined” error,
you will get this no matter what browser you use. Just confirmed it on chrome 18, firefox 11 and opera 11.62.

Initially, i thought this was an error, but in all these browsers, when you press i,j,k, or l, the game will start.
Its quite fast I should say, but hey, Im not complaining.

If you want a full featured HTML5 game, try http://cuttherope.ie for a browser version of cut the rope. Wicked fun.

Anonymous April 6, 2012 at 3:42 pm

I don’t understand the controls… i is up, sometimes… but m is never down?

Jon Zumbrun April 6, 2012 at 10:44 am

Same as @Malcolm Sheridan. Just a black box.

Niels April 5, 2012 at 11:42 pm

Hey, this is the good old snake game. From the eighties this game was already very very small when it comes to the number of bytes. I am not surprised that this can be done in html5 and javascript as well.

Miroslav Ćurčić April 5, 2012 at 12:00 pm

Doesnt work in FF 10.0.4

mr_switch April 4, 2012 at 6:41 am

Hopefully you can see this….
<div id=”i”>Hello</div>

mr_switch April 4, 2012 at 6:35 am

You can pass anything within a function. e.g. myFunc( confirm(‘Pants’) );… javascript is awesome.

Didn’t know about the id as a window variable name, nice shortcut but wont be using it…

var i=0;
Hello
console.log(i);// 0, thankfully

Stephen Scott April 3, 2012 at 2:55 am

A nice little demo. But it makes me think about the sheer amount of code being added to webpages these days. Even on a good broadband connection, pages seem to be very sluggish to load. Firefox occasionally struggles if there are excess plugins on the same page, or multiple tabs.

I occasionally look at the source code for these sites, and it is shocking how much whitespace and markup is used. I think we’ve got a little complacent with loadtimes now. Try viewing websites on a mobile device and see what I mean. My little Blackberry 8520 really struggles with non-mobile sites, and it’s not that old a phone.

I think obesity exists in the digital world as well as the real world.

TehYoyo April 2, 2012 at 3:41 pm

Nice. Too bad more websites don’t strive to achieve such low sizes. Of course, there’s a line b/w concise and unfunctional, but if we could all minimize our loading times, that’d be great.

Christian March 29, 2012 at 4:50 pm

I thought BODY was no longer a requirement in HTML5.

Malcolm Sheridan March 28, 2012 at 10:15 pm

e is not defined

I get this is Chrome and Firefox. Maybe they minified it too much!

Stefan Froelich April 5, 2012 at 12:48 pm

It mentions that it is incompatible in firefox due to lack of support for the shortcut id to element naming.
Chrome should work fine though.

Aaron T. Grogg March 28, 2012 at 12:07 pm

Hmm, I’m getting the following error in my Chrome Console when I try to open the “play it here” link…
Uncaught ReferenceError: e is not defined

Maybe a couple more Bytes would be okay… :-)

Atg

Grumpy March 28, 2012 at 11:59 am

Meh.

USPaperchaser March 29, 2012 at 1:37 am

I concur. Nothing but a novelty, it’s not practical, especially if you want to make money from the app. Show me a CRM HTML5 app then I will use HTML5, until then, its beta to me.

Jasper March 28, 2012 at 10:45 am

Works perfectly in Firefox 12.0b :)

Comments on this entry are closed.