Games

A brief study of browser games

Why HTML5?

  • Cross-platform
  • Open standard
  • No additional plugins required
  • Features like high performance 2D and 3D graphics, offline asset storage, rich audio APIs, geo-location API and socket-based networking let you create compelling modern games

Rough Timeline of

Web Technologies

1994HTML2
1996CSS1 + javascript
1997HTML4
1998CSS2
2000XHTML1
2002Tableless Web Design
2005AJAX
2009HTML5

What is new in HTML5?

  • The main competitors in web multimedia management are Flex by Flash and Silverlight by Microsoft.
    They require user to download a browser plugin
  • HTML5 is simply a markup language with an extended layer of tags and attributes to handle rich media content
  • Produces animations and visual effects using JavaScripts and Scalable Vector Graphics

What is new in HTML5?

  • Real time communication is enabled using websockets
  • Compatibility with different platforms using Canvas
  • Also includes offline local storage and Geo Location
  • Video and audio elements are easy to handle without third party plugins


HTML5 has the tools you need to create amazing games

Web Sockets

Google's Ian Hickson—the HTML5 specification lead says:

Reducing kilobytes of data to 2 bytes…and reducing latency from 150ms to 50ms is far more than marginal. In fact, these two factors alone are enough to make Web Sockets seriously interesting to Google.

Polling vs Web Sockets

  • With polling, the browser sends HTTP requests at regular intervals and immediately receives a response.
  • HTTP request and response headers contain unnecessary header information introducing latency
  • Also two connections are opened to create a full-duplex communication over half-duplex HTTP
  • Web Sockets define a full-duplex communication channel that operates through a single socket over the Web.

Web Socket Handshake

  • Client and server upgrade from HTTP protocol during their initial handshake
  • Browser Request Server Response
    
      GET /text HTTP/1.1\r\n 
      Upgrade: WebSocket\r\n 
      Connection: Upgrade\r\n 
      Host: www.websocket.org\r\n 
      …\r\n 
                      
    
      HTTP/1.1 101 WebSocket Protocol Handshake\r\n 
      Upgrade: WebSocket\r\n
      Connection: Upgrade\r\n 
      …\r\n
    
                      
  • Both text and binary data can be sent on full-duplex after upgrade as data strings in JavaScript
  • Data is framed minimally with just 2 bytes
  • Text frames use a terminator and binary frames use length prefix

Comet vs WebSockets

  • Web application which request real-time stock data from RabbitMQ message broker was created.
  • Comet uses half-duplex polling to get updates
  • Using LiveHTTP Headers (a Firefox add-on to show header traffic), the total overhead of request and response headers was observed to be 871 bytes!

Writing web sockets is simple !!!

Simply establish connection and use open, error, message and close events as necessary


    var socket = new WebSocket('ws://localhost:8080/');
    socket.onopen = function () {
        alert('Connected!');
    };
    socket.onmessage = function (event) {
        alert('Received data: ' + event.data);
        socket.close();
    };
    socket.onclose = function () {
        alert('Lost connection!');
    };
    socket.send('hello, world!');
        

File access & Offline Storage

Local Storage : HTML5 allows to store normalized relational database which can be explored using SQL. You can cache content or use a web app to use when you’re not connected to the Internet. An e-commerce HTML5 application could allow people to add items to buy later and then finish the purchase when they are back online.

Gmail Mobile app uses AppCache - manifest

Canvas

  • Allows easy manipulation of pixels without external plugins
  • Auto resize example using simple canvas element:
    
        function resizeGame() { 
            var gameArea = document.getElementById('gameArea'); 
            var newWidth = window.innerWidth; 
            var newHeight = window.innerHeight; 
            gameArea.style.height = newHeight + 'px'; 
            gameArea.style.width = newWidth + 'px'; 
            var gameCanvas = document.getElementById('gameCanvas');
            gameCanvas.width = newWidth; 
            gameCanvas.height = newHeight; }
                
  • With the aid of JS, canvas 3D can make stunning visual effects

Canvas 3D (WebGL)


    <canvas id="canvas" width="858" height="220"></canvas>

    <script>
      var gl = document.getElementById("canvas").getContext("expt-webgl");
      gl.viewport(0, 0, canvas.width, canvas.height);
      ...
    </script>
        

Multimedia

Audio and Video

The tracks are loaded the same way all the other game effects are loaded with XMLHttpRequests and the arraybuffer response type and stored in buffers. They are played using <audio> and <video> HTML5 tags

Plugin free access to camera/microphone

Courtesy: photobooth, by Google

HTML5 Audio

Web Audio API createMediaSourceElement() Example

Courtesy: Web Audio API createMediaSourceElement() Example, Google

HTML5 Video Destruction

Click here to see the true awesomeness! :)

Refer: Blowing HTML5 video and mapping it into 3D space

Access Native Hardware

Device APIs

Device APIs WG: http://www.w3.org/2009/dap/
  • Geolocation API
  • Device Orientation API ( accelerometer )
  • WebGL ( GPU )
  • HTML5 Filesystem API ( sandboxed filesystem )
  • navigator.onLine / navigator.connection ( network connectivity )
  • Battery API
  • Gamepad API
  • WebRTC ( voice & video input ) / Web Audio API ( core audio )

The Web Can Do Amazing Things...

  • CSS For Web Apps
  • Dynamic CSS
  • Data binding
  • Access a Filesystem
  • sudo make me a sandwich
  • Serverless Downloads
  • Efficiently Transfer Data
  • Access Native Hardware
  • Make <audio> Sexy Again
  • Stream Multimedia

Refer: http://www.htmlfivecan.com

Games using HTML5

Biolab Disaster
Biolab Disaster
Canvas Rider
Canvas Rider
Field Runners
Field Runners
Crystal Galaxy
Crystal Galaxy

Mobile Browser Compatibility

Drawbacks

  • Connectivity - HTML5 apps usually require an internet connection to at least load, if not use
  • Performance - native apps are faster
  • Doesn't include digital rights management (DRM) technology to prevent copying
  • Support for different HTML5 video formats - differ in browsers
  • Security Concerns
  • Evolving and no complete support

Conclusions

  • HTML5 is still considered as work-in-progress and has features which are not compatible with all the browsers.
  • HTML5 does not differentiate between mobile/PC platforms and avoids plugin installations
  • Hence, HTML5 will soon emerge as a new standard for rich media applications over the internet

References

Demos:
  1. Reveal.js - HTML Presentation Framework
  2. http://www.html5rocks.com - A resource for open web HTML5 developers, by Google
  3. http://www.htmlfivecan.com
Others:
  1. Impact of HTML5 as a Standard, Upul Senanayake,Faculty of Engineering, University of Peradeniya
  2. http://www.websocket.org/
  3. http://www.sitepoint.com/5-awesome-html5-games/
  4. http://ajf.me/websocket/#demo
  5. http://amix.dk/blog/post/19707
  6. http://www.zdnet.com/why-html5-is-in-trouble-on-the-mobile-front-7000003748/
  7. http://www.businessinsider.com/bii-report-why-html5-is-still-not-ready-for-prime-time-2012-9

Thank You

:)

For live demo see http://hasankumar.com/html5/games/slides/

Source available at https://github.com/mintuhouse/HTML5Games