Creating browser-based games with HTML5 and JavaScript

Murilo Pereira (@mpereira), April 10th 2012

Creating things

Do Things, tell people

(ideally in that order)

Let's talk about JavaScript

Try not to do it (all the time)

JavaScript has a history of being treated as a second-class citizen on web applications.

Second class citizen t-shirt

Mostly used for "web 2.0" (ughh) visual effects and occasional AJAX requests.

Rarely used by people who actually know the language. Seen as a toy.

Will code HTML for food guy

Snippets spread through the codebase make it unmaintanable, error-prone mess.

Crying baby covered in spaguetti

Inlined code make logic non-reusable, hard to debug and troubleshoot.

Possible improvement: separate behaviour from content (unobtrusive), in the same way that CSS separates presentation from content.

Backbone.js logo

To the rescue!1

Backbone.js

is:

  • MVC JavaScript framework
  • Hash-routing engine
  • RESTful JSON interface

We'll focus on the MVC bit.

MVC

MVC or is it?

MVC (roughly)

MVC
  • Model: data + business logic
  • View: user interface
  • Controller: input handler (and everything else)

I wanna see code!1


              <div id='character'></div>
            

              var character = new Backbone.Model({
                name: 'Frodo'
              });

              var characterView = new Backbone.View({
                el: '#character',

                model: character,

                render: function() {
                  this.$el.append('Hi, I\'m ' + this.model.get('name'));
                }
              });

              characterView.render();
            
Down arrow

              <div id='character'>Hi, I'm Frodo</div>
            

K, what about canvas?

<canvas>

is:

  • Bitmap drawed by JavaScript
  • 2D shapes and images
Blank canvas

<canvas>

Painting brushes

JavaScript

Kids with paint

Enables you to do cool stuff

A diagonal line


              <canvas id='first-canvas' width='800px' height='200px'></canvas>
            

              var context = document.getElementById('first-canvas').getContext('2d');

              context.moveTo(150, 50);
              context.lineTo(600, 150);
              context.lineWidth = 15;
              context.stroke();
            
Down arrow

A rectangle with borders


              <canvas id='second-canvas' width='800px' height='200px'></canvas>
            

              var context = document.getElementById('second-canvas').getContext('2d');

              context.beginPath();
              context.rect(225, 25, 300, 150);
              context.fillStyle = 'yellow';
              context.fill();
              context.lineWidth = 5;
              context.strokeStyle = 'black';
              context.stroke();
            
Down arrow

A semicircle with gradient


              <canvas id='third-canvas' width='800px' height='150px'></canvas>
            

              var context = document.getElementById('third-canvas').getContext('2d');

              context.beginPath();
              context.arc(400, 60, 100, 0, Math.PI, false);
              context.closePath();
              context.lineWidth = 15;
              var gradient = context.createLinearGradient(300, 50, 500, 50);
              gradient.addColorStop(0, 'yellow');
              gradient.addColorStop(1, 'green');
              context.fillStyle = gradient;
              context.fill();
              context.strokeStyle = 'black';
              context.stroke();
            
Down arrow

Let's build a simple "game"

*Try moving with WASD