Chapter 11. Further Explorations

There are many emerging technologies and frameworks that can help take HTML5 Canvas into rarely explored areas. In this chapter, we will cover a couple of those areas: using Canvas for 3D with WebGL, and using Canvas for multiplayer applications. Both of these areas are still experimental, requiring you either to download beta/developer versions of browsers or to launch browsers using command-line switches so that you can turn various technologies off and on.

We will also cover a couple more topics that, while still involving the Canvas, veer into software design and emerging platforms. We will create a sample structure for our Canvas code and then apply it to a drag-and-drop application. After that, we will take that application and deploy it on the Microsoft Windows 8 desktop.

This chapter is structured a bit differently. The discussions are focused on giving you some tools and information about these new and emerging areas for Canvas. While we will offer code, examples, and some explanation, it’s geared more toward getting you started on the path to learning than on teaching you how every detail works.

3D with WebGL

The 2D capabilities of HTML5 Canvas are impressive, but what about 3D? There is no “production” 3D context available in the standard version of any web browser at this time. However, the best support for a 3D context will probably come in the form of WebGL.

What Is WebGL?

WebGL is a JavaScript API that gives programmers access to the 3D hardware on the user’s machine. Currently, it is supported only by the debug/development versions of Opera, Firefox, and Chrome. The API is managed by Kronos, the same organization that manages OpenGL. In fact, much of WebGL is similar to programming in OpenGL. This is both good and bad. It’s good because it’s a standard programming interface that is recognizable to many developers, but it is bad because it is not as easy to learn as the 2D Canvas context.

How Does One Test WebGL?

First, you need to find a web browser that supports WebGL. When trying to run a WebGL application, a browser that does not support WebGL might give a message like the one shown in Figure 11-1.

Trying to run WebGL in a standard web browser
Figure 11-1. Trying to run WebGL in a standard web browser

Currently, the release versions of both Google Chrome and Firefox support WebGL. When you have a browser that can display WebGL, you need to write the code to make it happen. You start that process by accessing the WebGL context instead of the Canvas 2d context. So, instead of the following code, which we have used throughout this book:

context = theCanvas.getContext("2d");

We reference the experimental-webgl context, like this:

gl = theCanvas.getContext("experimental-webgl");

How Do I Learn More About WebGL?

The best place to learn about WebGL is at http://learningwebgl.com/. This site has an FAQ, a blog, and some helpful low-level lessons on how to create apps using WebGL. You can also find a ton of great content about WebGL at http://developer.mozilla.org.

One warning, however: programming WebGL is not for the uninitiated. Although WebGL is based on OpenGL, it is still a very low-level API, meaning that you will need to create everything by hand. At the end of this section, we will guide you toward some higher-level libraries that should make this process a bit easier.

What Does a WebGL Application Look Like?

Now we are going to show you a WebGL application demo that rotates a 3D cube on Canvas (see Figure 11-2). Because we are not experts in 3D graphics, we will forgo our practice of describing every line of code in the example; instead, we will highlight interesting sections of code to help you understand what is happening.

This demo is based on Lesson 4 from Giles Thomas’s Learning WebGL website. While this is only one short demo, it should give you a very good idea of how to structure and build code for a WebGL application.

Much of this code has been adapted from the work of Giles Thomas with his express written permission.

3D rotating cube (CH11EX1.html)
Figure 11-2. 3D rotating cube (CH11EX1.html)

JavaScript libraries

First, we add some JavaScript libraries. Modernizr includes a test for WebGL support in a web browser. This version was freshly released, but it could be updated with new features at any time (in fact, at the time of this writing, this had been updated to version 2.6). It is necessary to make sure that you have the most recent versions of your libraries:

<script src="modernizr.js"></script>

We now need to include some JavaScript libraries to assist with our application. sylvester.js and glUtils.js are two libraries that you will find included for most apps that use WebGL. sylvester.js is a library that helps when performing vector and matrix math calculations in JavaScript. glUtils.js is an extension for sylvester.js, specifically for helping with math related to WebGL:

<script type="text/javascript" src="sylvester.js"></script>
<script type="text/javascript" src="glUtils.js"></script>

Shaders

Shaders are pieces of code that run directly on a graphics card. They describe how a scene—how you refer to a 3D canvas when working with WebGL—should be rendered. Many of these little programs perform mathematical transformations that would otherwise run very slowly in JavaScript. In fact, we are pointing these out because they are not JavaScript; they are written in a way that WebGL can understand. These sections of code will be read in like text files and passed to the graphics hardware. Full discussions of topics like shaders are far out of scope for this little section of the book, but we will tell you a bit about each one of them to help set the tone for what comes next.

The first shader below is a fragment shader, which tells the graphics card that we will be using floating-point numbers and blended colors. The second shader is the vertex shader. It works with the vertices (defined points in 3D space used to create 3D objects) and will be used for every vertex we draw onto the Canvas 3D context:

<script id="shader-fs" type="x-shader/x-fragment">
  #ifdef GL_ES
  precision highp float;
  #endif

  varying vec4 vColor;

  void main(void) {
    gl_FragColor = vColor;
  }
</script>

<script id="shader-vs" type="x-shader/x-vertex">
  attribute vec3 aVertexPosition;
  attribute vec4 aVertexColor;

  uniform mat4 uMVMatrix;
  uniform mat4 uPMatrix;

  varying vec4 vColor;

  void main(void) {
    gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
    vColor = aVertexColor;
  }
</script>

Testing for WebGL support with Modernizr

The structure of the code in this example is much like the other applications we have written in this book. However, it has been modified to work with the specific needs of the 3D context. In the canvasApp() function, we need to test to see whether the browser has WebGL support. This is easily accomplished by using the Modernizr.webgl static constant in Modernizr:

if ( !webglSupport()) {
   alert("Unable to initialize WebGL");
   return;
}
function webglSupport() {
   return Modernizr.webgl;
}

Initialization in canvasApp()

In canvasApp(), we still get a context, but this time it is the experimental-webgl context. Also, just like in our other apps, we still call drawScreen() on an interval to render the canvas:

var theCanvas = document.getElementById("canvasOne");
webGLContext = theCanvas.getContext("experimental-webgl");

setInterval(drawScreen, 33);

However, there is additional code in canvasApp() required to set up the application to rotate the cube. A couple of the most important initialization steps are the calls to initShaders() and initBuffers():

initShaders();
initBuffers();

The initShaders() function itself calls a function named getShader() to load in the text of the shader programs we have already defined. You can see the code for these functions in the code listing in Example A-3.

You can learn about the shaders used in this program in “Lesson 2—Adding colour” on the LearningWebGL website.

After we have loaded the shader programs, we need to create the buffers. Buffers refer to space in the video card’s memory that we set aside to hold the geometry describing our 3D objects. In our case, we need to create buffers to describe the cube we will rotate on the canvas. We do this in initBuffers().

The initBuffers() function contains a lot of code, but we’ll discuss only a couple very interesting sections. The first is the Vertex Position buffer, which describes the vertices that make up the sides of the cube:

webGLContext.bindBuffer(webGLContext.ARRAY_BUFFER, cubeVertexPositionBuffer);
      vertices = [
        // Front face
        1.0, 1.0,  1.0,
         1.0, 1.0,  1.0,
         1.0,  1.0,  1.0,
        1.0,  1.0,  1.0,

        // Back face
        1.0, 1.0, 1.0,
        1.0,  1.0, 1.0,
         1.0,  1.0, 1.0,
         1.0, 1.0, 1.0,

        // Top face
        1.0,  1.0, 1.0,
        1.0,  1.0,  1.0,
         1.0,  1.0,  1.0,
         1.0,  1.0, 1.0,

        // Bottom face
        1.0, 1.0, 1.0,
         1.0, 1.0, 1.0,
         1.0, 1.0,  1.0,
        1.0, 1.0,  1.0,        // Right face
         1.0, 1.0, 1.0,
         1.0,  1.0, 1.0,
         1.0,  1.0,  1.0,
         1.0, 1.0,  1.0,

        // Left face
        1.0, 1.0, 1.0,
        1.0, 1.0,  1.0,
        1.0,  1.0,  1.0,
        1.0,  1.0, 1.0,
      ];

The Vertex Color buffer holds information about the color that will appear on each side of the cube. These values are set as percentages of RGBA values (red, green, blue, alpha):

webGLContext.bindBuffer(webGLContext.ARRAY_BUFFER, cubeVertexColorBuffer);
      var colors = [
        [1.0, 1.0, 1.0, 1.0],     // Front face
        [0.9, 0.0, 0.0, 1.0],     // Back face
        [0.6, 0.6, 0.6, 1.0],     // Top face
        [0.6, 0.0, 0.0, 1.0],     // Bottom face
        [0.3 ,0.0, 0.0, 1.0],     // Right face
        [0.3, 0.3, 0.3, 1.0],     // Left face
      ];

The Vertex Index buffer is kind of like a map that builds the object (our cube) based on the colors specified in Vertex Color (the order of these elements) and the vertices specified in the Vertex Position buffer. Each of these sets of three values represents a triangle that will be drawn onto the 3D context:

webGLContext.bindBuffer(webGLContext.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
      var cubeVertexIndices = [
        0, 1, 2,      0, 2, 3,    // Front face
        4, 5, 6,      4, 6, 7,    // Back face
        8, 9, 10,     8, 10, 11,  // Top face
        12, 13, 14,   12, 14, 15, // Bottom face
        16, 17, 18,   16, 18, 19, // Right face
        20, 21, 22,   20, 22, 23  // Left face
      ]

Again, there is more code in initBuffers() than we described here, but start with these three sections when you want to play with the code and make your own objects.

Animating the cube

Now that you know a bit about creating an object in WebGL, let’s learn about animating the cube on the canvas. Similar to what we did in the 2D context, we use the drawScreen() function to position, draw, and animate objects in the 3D context. The first thing we do here is set up the viewport, which defines the canvas’s view of the 3D scene. Next, we clear the canvas and then set up the perspective:

function drawScreen() {

   webGLContext.viewport(0, 0, webGLContext.viewportWidth,
       webGLContext.viewportHeight);
   webGLContext.clear(webGLContext.COLOR_BUFFER_BIT |
       webGLContext.DEPTH_BUFFER_BIT);

   perspective(25, (webGLContext.viewportWidth / webGLContext.viewportHeight),
     0.1, 100.0);

The perspective has four parameters:

Field of view

The angle at which we will view the 3D scene (25 degrees).

Width-to-height ratio

The radio of width to height of the current size of the canvas (500×500).

Minimum units

The smallest unit size away from our viewport that we want to display (0.1).

Maximum units

The furthest unit size away from our viewport that we want to see (100.0).

Next, we move to the center of the 3D scene, calling loadIdentity() so that we can start drawing. We then call mvTranslate(), passing the locations on the x-, y-, and z-axes to draw the cube. To rotate the cube, we call a function named mvPushMatrix(), and later mvPopMatrix(), which is similar to how we called context.save() and context.restore() when rotating objects on the 2D canvas. The call to mvRotate() then makes the cube rotate from the center, tilted up and to the right:

loadIdentity();

mvTranslate([0, 0.0, 10.0]);

mvPushMatrix();
mvRotate(rotateCube, [0, .5, .5]);

Next, we draw the cube by binding the buffers that hold the vertices and color information that we set up earlier for the cube’s sides. We then draw each side, made up of two triangles each:

webGLContext.bindBuffer(webGLContext.ARRAY_BUFFER, cubeVertexPositionBuffer);
webGLContext.vertexAttribPointer(shaderProgram.vertexPositionAttribute,
    cubeVertexPositionBuffer.itemSize, webGLContext.FLOAT, false, 0, 0);

webGLContext.bindBuffer(webGLContext.ARRAY_BUFFER, cubeVertexColorBuffer);
webGLContext.vertexAttribPointer(shaderProgram.vertexColorAttribute,
    cubeVertexColorBuffer.itemSize, webGLContext.FLOAT, false, 0, 0);

webGLContext.bindBuffer(webGLContext.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
setMatrixUniforms();webGLContext.drawElements(webGLContext.TRIANGLES, 
    cubeVertexIndexBuffer.numItems, webGLContext.UNSIGNED_SHORT, 0);

mvPopMatrix();

Finally, we increase the rotateCube variable so that the next time drawScreen() is called, the cube will be updated with a new angle. The following code adds 2 degrees to the rotation angle each time drawScreen() is called:

  rotateCube += 2;

   }

Further Explorations with WebGL

Obviously, we cannot teach you all about WebGL in this chapter. We opted to include this demo and short discussion to introduce you to WebGL and show you what it looks like. In reality, a full discussion of WebGL, even the basic concepts, could take up an entire volume.

If you are interested in WebGL, we strongly recommend that you consult http://learningwebgl.com for more examples and the latest information about this exciting yet still experimental context for HTML5 Canvas.

WebGL JavaScript Libraries

At the start of this section, we promised to show you some libraries that can be used with WebGL to make it easier to develop applications. Here are some of the more interesting libraries and projects.

Google O3D

Google’s O3D library was once a browser plug-in, but has now been released as a standalone JavaScript library for WebGL. The examples of using O3D with JavaScript—including a fairly spectacular 3D pool game—are very impressive. O3D allows you to load COLLADA 3D models created with Google SketchUp (as well as other 3D packages).

The required code looks about as complex as straight WebGL code, so while this is very powerful, you might want to look at some of the other libraries here first if you are just starting out.

C3DL

The tagline for C3DL is “WebGL made easy!” C3DL, or “Canvas 3D JS Library,” is similar to GLGE, but it seems to have a head start thanks to a larger API and more support. This library also appears to be slanted toward games; a real-time strategy (RTS) and an arcade game are featured as its more prominent demos. The library supports COLLADA models, and the code also appears very straightforward to implement.

SpiderGL

“3D Graphics for Next-Generation WWW” is how SpiderGL bills itself to the world. This library appears to be very similar to GLGE and C3DL, except that the demos focus more on lighting, color, and textures than on games and applications. It also supports COLLADA models.

SceneJS

SceneJS is geared toward rendering 3D scenes built as COLLADA JSON models in WebGL. You can also define and manipulate 3D scenes. Loading and rendering the models is a straightforward process, and the results are quite impressive.

CopperLicht

This commercial library advertises itself as the “fast WebGL JavaScript 3D Engine.” All the demos are game-oriented, and the library supports many commercial 3D formats. It has both collision detection and physics built in. The demos are fast and are fun to play. This library appears to be centered on loading and using external 3D assets, so if that is what you are looking for, this might be your best choice.

GLGE

“WebGL for the lazy” is the tagline for this JavaScript library. The author of the library, Paul Brunt, says this about GLGE:

The aim of GLGE is to mask the involved nature of WebGL from the web developer, who can then spend his/her time creating richer content for the Web.

This is a high-level API that is still in development. Just like O3D, it has the ability to load COLLADA models. Applications written with GLGE are created with a combination of XML and JavaScript. It looks very promising.

Of all of these libraries, GLGE appears to be a favorite among indie developers. It takes a lot of the pain out of WebGL by using XML to define 3D objects, meshes, materials, and so on.

Three.js

The most promising WebGL library might be three.js. It’s a free, lightweight API that is gaining popularity because it is easy to use and implement.

One final note about WebGL: Microsoft has vowed to not support WebGL in the IE browser. They believe that it poses a security threat, and they balk at it because it is not a W3C standard. However, there is a plug-in named iewebgl that will run most WebGL content in Internet Explorer.

Multiplayer Applications with ElectroServer 5

Because Flash has built-in support for communication via sockets, its applications have had the ability to open socket communications with server-side applications for many years. HTML (until Web Sockets), on the other hand, has never had the ability to reliably communicate to a socket server without performing some sleight of hand, usually involving constant polling by the web browser for new information from the web server.

ElectroServer from Electrotank was one of the first reliable socket-server applications built to communicate with Flash clients. Over the past couple years, ElectroServer has been updated with APIs for iOS, C#, C++, and now JavaScript. This first iteration of the ElectroServer JavaScript API does not use WebSockets but instead implements JavaScript polling. However, with the availability of ElectroServer’s simplified JavaScript API, you can still start to write multiplayer applications using HTML5 Canvas.

While this portion of the chapter is specific to ElectroServer, many of the multiplayer/multiuser concepts are applicable to other technologies as well.

Installing ElectroServer

To get started with multiplayer development using HTML5 Canvas and the ElectroServer socket server, you first need to download the free, 25-user version of the software from Electrotank. You can download the appropriate version for your operating system (Windows, Mac, Linux) at this site.

There are some installation prerequisites, such as having Java version 1.6. For detailed installation instructions for every OS, visit this site.

The install package includes the server software, client APIs, documentation, and sample applications. After you have installed the server software, you should have a folder named something like Electroserver_5_x_ on your computer. We used Mac OS X for this test, so this folder was created inside the Mac Applications folder. On Windows, it will be created in the location you specify upon installation.

Starting the server

After you have the files installed, you need to start the ElectroServer socket server by finding the installation directory and executing the file Start_ElectroServer_5_0_1. (Note: the three numbers at the end of this file will change as the version is upgraded, but the concept will remain the same.)

When ElectroServer starts, you should see a screen similar to Figure 11-3.

ElectroServer started
Figure 11-3. ElectroServer started

The server will run on your local machine for testing purposes. However, for any real-world application, you will need to install a production version of the software on a web server.

The ElectroServer admin tool

Because ElectroServer is a socket server, it listens on a specified port for communication from the JavaScript client using one of the supported protocols. ElectroServer supports multiple protocols, but we need to make sure we are using the BinaryHTTP protocol for the JavaScript API. The default port for BinaryHTTP in ElectroServer is 8989.

When the ElectroServer JavaScript API is updated to support WebSockets, the port and protocol will likely be different.

There is a nifty admin tool for ElectroServer that allows you to view and modify all the supported protocols and ports, as well as many other cool features of the socket server. In the /admin directory of the install folder, you should find both an installer for an Adobe AIR admin tool (named something like es5-airadmin-5.0.0.air), and a /webadmin directory with an HTML file named webadmin.html. Either one will work for this exercise.

For the admin console to display properly, the server needs to be started.

When you launch the admin tool, you will be asked to supply a username and password. The default is administrator and password, unless you changed them upon installation.

After you log in, click the Server Management button on the top menu, and then choose the Gateways option from the side menu. You should see a screen that looks similar to Figure 11-4.

ElectroServer ports and protocols
Figure 11-4. ElectroServer ports and protocols

This screen shows you the port settings for each protocol that ElectroServer supports. For the JavaScript API, we are most interested in the BinaryHTTP setting, which you can see is set to port 8989.

The JavaScript API

Besides starting ElectroServer, you will also need the JavaScript API so that you can begin building Canvas apps that connect to the server. You should be able to find the JavaScript API in the /apis/client/javascript directory of the folder in which you installed ElectroServer. (This name might change in the final version.) The API should be named ElectroServer-5-Client-JavaScript.js.

The Basic Architecture of a Socket-Server Application

Now that you have ElectroServer ready to go and you have the JavaScript API, it is time to learn a bit about how socket-server-based multiplayer/multiuser applications are designed. Using a socket server means you are creating an application that relies on a client for input from a user, as well as relying on a server to distribute that input to other users who are connected to the first user.

A good example of this is a chat application. Most chat applications require a user to enter a room (a logical space in which people are “chatting”—that is, exchanging messages), where that user can see the messages of other people in the same virtual space. In that room, the client is “connected” to those other users. However, it is usually not a direct connection (such as peer-to-peer), but instead, it is a connection through a port to a socket server.

The socket server acts as the traffic cop for the chat messages. It listens on a port (in our case, 8989) for messages coming in from the clients. Those messages need to be formatted in a way that the server can understand so that it can process them. The JavaScript API we will use performs this formatting for our client applications.

When the socket server receives a message from the client, it routes the various text messages sent by each client back out to the other clients in the room. However, it can also do much more by using server-side processing, such as holding the list of current messages, so that people entering the room while the chat is ongoing can see what has been said previously, scan chat messages for swear words, award points to users for their input, or anything else you can dream up.

When the server finally processes the message and sends it back, the client then processes that message. In the case of the chat, that processing usually involves displaying the message on the canvas.

The Basic Architecture of an ElectroServer Application

ElectroServer acts very much like the socket-server application we described in the previous section. It listens on specified ports for different protocols; when messages arrive, they are routed back to the connected clients.

However, ElectroServer has some specific features that we should discuss. Some of these exist on other socket-server platforms, while some don’t. However, much of this discussion will still be applicable to other socket servers when they make JavaScript APIs available.

Client

The client for an ElectroServer application is a program written in one of the API-supported language platforms, including Flash ActionScript 2, Flash ActionScript 3, Java, Objective-C, C#/.NET, and now JavaScript. The client is the application, which the user will manipulate to send messages through the API to ElectroServer. This is usually a game, a chat room, a virtual world, or some other kind of multiuser social or communication application.

All the communication with ElectroServer is event-based. The client application uses the JavaScript API to send events, and the client defines event handlers that listen for messages from ElectroServer. All of these messages and events are communicated through the API, which in turn is communicating through port 8989 using the BinaryHTTP protocol (at least for our examples).

Zones, rooms, and games

When a user first connects to ElectroServer, she needs to join or create a zone, which is simply a collection of rooms. If the user tries to create a zone that already exists, she will be added to that zone without creating a new one.

After entering a zone, the user needs to join a room in that zone. If a user attempts to create a new room that already exists, she will be added to that room instead.

Beyond zones and rooms, ElectroServer also offers a GameManager API that allows you to further segment users into specific instances of a game that is being played. We do not get this granular for the examples in this chapter.

Extensions

Extensions are server-side code modules that can process data sent by clients before that data is sent back to other clients. Extensions can also process and create their own events. For many games, the extension contains much of the game logic, relying on the clients for displaying and gathering user input.

At the very minimum, an extension contains what is known as a plug-in. A plug-in is a code module written in ActionScript 1 (basically JavaScript) or Java that can be instantiated and scoped to a room. For example, if you were making a card game, you would want a card game plug-in on the server to handle things like shuffling the deck and making sure the correct player wins a hand. In this way, the server holds the true state of the game. Using an extension helps keep a game flowing and lessens the users’ ability to cheat. For the simple examples in this chapter, we will not be using any server-side extensions. However, if you delve further into ElectroServer or other socket-server applications, you should make sure to learn as much as possible about them.

Creating a Chat Application with ElectroServer

As an example, we are going to create a single chat application using the ElectroServer JavaScript API. Users will submit a chat message through an HTML form, and the displayed chat will be in HTML5 Canvas. We are also going to create and display some messages from ElectroServer so that you can see the status of the connection to the server.

Establishing a connection to ElectroServer

First, a client application is written so that it includes the ElectroServer JavaScript API:

<script src="ElectroServer-5-Client-JavaScript.js"></script>

The client application makes a connection to ElectroServer running on a server at a specific URL, listening on a specific port, using a specific protocol. For our examples, this will be localhost, 8989, and BinaryHTTP, respectively.

We need to use these values to make a connection from the client to the server. We do this by first creating an instance of the ElectroServer object and then calling its methods. We start by creating an instance of an ElectroServer server connection named server. We then configure a new variable named availableConnection with the previous properties we described, and then we add it to the server variable with a call to the method addAvailableConnection(). We will create all of this code inside our canvasApp() function:

var server = new ElectroServer.Server("server1");
var availableConnection = new ElectroServer.AvailableConnection
    ("localhost", 8989, ElectroServer.TransportType.BinaryHTTP);
server.addAvailableConnection(availableConnection);

Now we need to use the server variable we just configured to establish a connection to ElectroServer. We do this by setting a new variable, es, as an instance of the class ElectroServer. We then call its initialize() method and add the server we just configured to the es object by calling the addServer() method of the ElectroServer server engine property:

var es = new ElectroServer();
es.initialize();
es.engine.addServer(server);

We are almost ready to try to connect to ElectroServer. However, first we need to create some event handlers for ElectroServer events. Remember when we told you that all the communication with ElectroServer is done through creating and listening for events? This is where that process begins. We need to listen for the following events: ConnectionResponse, LoginResponse, JoinRoomEvent, JoinZoneEvent, ConnectionAttemptResponse, and PublicMessageEvent:

es.engine.addEventListener(MessageType.ConnectionResponse, onConnectionResponse);
es.engine.addEventListener(MessageType.LoginResponse, onLoginResponse);
es.engine.addEventListener(MessageType.JoinRoomEvent, onJoinRoomEvent);
es.engine.addEventListener(MessageType.JoinZoneEvent, onJoinZoneEvent);
es.engine.addEventListener(MessageType.ConnectionAttemptResponse,
    onConnectionAttemptResponse);
es.engine.addEventListener(MessageType.PublicMessageEvent, onPublicMessageEvent);

Finally, when we have everything ready, we call the connect method of the ElectroServer object and wait for events to be handled by the event listener functions we have just established:

es.engine.connect();

When the ElectroServer API object tries to connect to an ElectroServer server, a ConnectionAttemptResponse event will be fired back to the client from the server. We handle that event with the onConnectionAttemptResponse() event handler. For our application, we don’t do anything with this event except create a status message for it that we will display. The statusMessages variable is an array of messages that we keep around to display back as debug information for our chat application. We will discuss this briefly in the next section:

function onConnectionAttemptResponse(event) {
 statusMessages.push("connection attempt response!!");
}

At this point, the client waits for a ConnectionResponse event to be sent back from the ElectroServer server. When the client application receives a ConnectionResponse event, it handles it with the onConnectionResponse() event handler. When the connection is established, the client then attempts to log on to the server. To make a logon attempt, we need a username. We will create a random username, but it could come from an account on a web server, a form field or cookie, Facebook Connect, or any other location or service you might have available.

After we have a username, we create a LoginRequest() object, set the userName property, and then call the send() method of the es.engine object. This is how we will send all messages to ElectroServer from this point forward:

function onConnectionResponse(event) {
   statusMessages.push("Connect Successful?: "+event.successful);
   var r = new LoginRequest();
   r.userName = "CanvasUser_" + Math.floor(Math.random() * 1000);
   es.engine.send(r);
 }

When ElectroServer responds from the LoginRequest, it is time to join a zone and a room. Recall that any user connected to ElectroServer needs to belong to a room, and every room belongs to a zone. Therefore, we need to make a user belong to one of each, which we accomplish with a CreateRoomRequest(). We set the zoneName property to TestZoneChat and the roomName property to TestRoomChat. If either of these does not already exist, it will be created by the server. If they do exist, the user will be added to them. We then send the message to ElectroServer:

function onLoginResponse(event) {
   statusMessages.push("Login Successful?: "+event.successful);

   username = event.userName;

   var crr = new CreateRoomRequest();
   crr.zoneName = "TestZoneChat";
   crr.roomName = "TestRoomChat";

   es.engine.send(crr);
}

We still need to wait for a couple responses from ElectroServer events that come back through the API via port 8989. We know we have to join a zone, and we handle the event with the function onJoinZoneEvent(), but we don’t need to do anything with it:

function onJoinZoneEvent(event) {
   statusMessages.push("joined a zone");
}

The most important event we are waiting to handle is JoinRoomEvent. When we receive this event, we know that we have joined both a zone and a room, and the application is ready to run. For the chat application, this means the user can start typing and sending messages. First, we set the _room variable equal to the Room object, which was returned by the event from ElectroServer. We will use this variable for our further communications with ElectroServer. The other thing we do in this function is set an HTML <div> with the id of inputForm, which is made visible by changing its style. The inputForm <div> is invisible when the page loads. We do this so that the user won’t send chat messages before the connection to ElectroServer is established. Now that everything is ready to go, we display the inputForm <div> so that chatting can start:

function onJoinRoomEvent(event) {
            statusMessages.push("joined a room");
            _room = es.managerHelper.zoneManager.zoneById
                (event.zoneId).roomById(event.roomId);
            var formElement = document.getElementById("inputForm");
            formElement.setAttribute("style", "display:true");
         }

Creating the chat functionality

Now that we have established a connection to ElectroServer and joined a zone and a room, the chat application can start.

First, let’s talk a bit about a few more variables we have created in our canvasApp() function, which we must scope to the rest of the chat application. The statusMessages array will hold a set of messages that we want to keep about the connection to ElectroServer. We will display these in a box on the right side of the canvas. The chatMessages array holds all the messages users have sent into the chat room. The username variable holds the name of the user who is running the Canvas application, and _room is a reference to the room object that user has joined:

var statusMessages = new Array();
var chatMessages = new Array();
var username;
var _room;

The HTML page holds a <form> that we will use to collect the chat messages from the user. It contains a text box for the user to type into (the id of textBox), and a button with the id of sendChat. This is the same form that was invisible until we received the JoinRoomEvent event:

<form>
<input id="textBox" placeholder="your text" />
<input type="button" id ="sendChat" value="Send"/>
</form>

In canvasApp(), we set up an event listener for when the user clicks the sendChat button. When a click event occurs, the function sendMessage handles the event:

var formElement = document.getElementById("sendChat");
formElement.addEventListener('click', sendMessage, false);

The sendMessage() function is one of the most important functions in this application. This is where we create a couple very critical objects for communicating with ElectroServer. The first is a PublicMessageRequest, which is one of several types we can make to the ElectroServer socket server. Others include a PrivateMessageRequest and a PluginMessageRequest. A PublicMessageRequest is a message that will be sent to everyone in the room. We send that data using an EsObject, which is native to the ElectroServer API. It allows you to create and access ad hoc data elements for any type of information you want to send to other users in the same room.

For a full discussion of EsObject and ElectroServer events, see the ElectroServer documentation. It is installed with the server on your local machine in [your install folder]//documentation/html/index.html *.

For this simple chat example, we want to send the chat message the user typed and submitted. To do this, we will use the setString() method of EsObject. This method takes two parameters: the text you want to send, and an identifier you can use to access the text. We also set another element named type, which will tell us what kind of message we are sending. We do this because, in a more complicated application, you might send all sorts of messages and need a way to identify what they are so that you can process them.

After we have configured our PublicMessageEvent with the roomId, the zoneId, and the EsObject, we call es.engine.send(pmr) to send it to the rest of the room:

function sendMessage(event) {
   var formElement = document.getElementById("textBox");
   var pmr = new PublicMessageRequest();
   pmr.message = "";
   pmr.roomId = _room.id;
   pmr.zoneId = _room.zoneId;
   var esob = new ElectroServer.EsObject();
   esob.setString("message", formElement.value);
   esob.setString("type","chatmessage");
   pmr.esObject = esob;
   es.engine.send(pmr);
   statusMessages.push("message sent");
}

Notice that we did not print the user’s chat message to the canvas when it was submitted. Instead, we will wait for the PublicMessageEvent to return from ElectroServer and then handle it like all the other chats. This keeps the interface clean, while preserving a create event/handle event processing model across the entire application.

After the socket server processes the chat message, it is broadcast out to all the users in the room. All the users must create an event handler for a PublicMessageEvent so that they can receive and process the message; we have created the onPublicMessageEvent handler for this purpose. This function is very simple. It checks the type EsObject variable we set to see whether it is a chatmessage. If so, it pushes a string that includes the user who submitted the message (event.userName) and the message itself (esob.getString("message")) into the chatMessages array. This is what will be displayed on the canvas:

function onPublicMessageEvent(event) {

   var esob = event.esObject;
   statusMessages.push("message received");
   if (esob.getString("type") == "chatmessage") {

      chatMessages.push(event.userName + ":" + esob.getString("message"));
      }
}

Now all that remains is to display the messages that we have collected. We do this (where else?) in drawScreen(). For both the statusMessages and chatMessages arrays, we need to display the “current” 22 messages (if we have 22) and start them at the y position of 15 pixels. We display only the last 22 messages so that both the chat and the status messages will appear to scroll up the screen as more chatting and status messages are generated:

var starty = 15;
var maxMessages = 22;

If the array is larger than maxMessages, we display only the latest 22. To find those messages, we set a new variable named starti to the length of the statusMessages array, subtracted by the value in maxMessages. This gives us the index into the array of the first message we want to display. We do the exact same thing for the chatMessages array:

//status box
   context.strokeStyle = '#000000';
     context.strokeRect(345,  10, 145, 285);
         var starti = 0;

         if (statusMessages.length > maxMessages) {
             starti = (statusMessages.length) - maxMessages;

         }
         for (var i = starti;i< statusMessages.length;i++) {
            context.fillText  (statusMessages[i], 350, starty );
            starty+=12;
//chat box
         context.strokeStyle = '#000000';
         context.strokeRect(10,  10, 335, 285);

         starti = 0;
         lastMessage = chatMessages.length-1;
         if (chatMessages.length > maxMessages) {
             starti = (chatMessages.length) - maxMessages;
         }
         starty = 15;
         for (var i = starti;i< chatMessages.length;i++) {
            context.fillText  (chatMessages[i], 10, starty );
            starty+=12;
         }
      }

That’s it! We’ve finished developing our multiuser chat application.

Testing the Application in Google Chrome

To test the current ElectroServer JavaScript API, you need to start Google Chrome with web security disabled. The method of doing this varies by operating system, but on Mac OS X, you can open a Terminal session and execute the following command (which will open Chrome if you have it in your Applications folder):

/Applications/Google\ Chrome.app/Contents/MacOS/Google\
    Chrome --disable-web-security

On a Windows-based PC, input a command similar to this from a command prompt or from a .bat file:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
    --disable-web-security

Obviously this is not a workable solution for a production application. As Electrotank (and other companies who make similar products) continue to improve the functionality of their APIs and add support for HTML5 WebSockets, this limitation should disappear.

The best way to test a multiplayer application on your own development machine is to open two web browsers or two web browser windows at the same time. When you look at CH11EX2.html in Google Chrome using this method, you should see something that looks like Figure 11-5.

ElectroServer chat demo on the canvas with JavaScript API
Figure 11-5. ElectroServer chat demo on the canvas with JavaScript API

Further Explorations with ElectroServer

Displaying text on HTML5 Canvas is interesting, but as we have shown you in this book, you can do much more. Let’s add some graphics to the previous demo. We have added a second application for you to peruse, named CH11EX3.html. This application adds the bouncing ball demo app from Chapter 5 to the chat application we just created. It allows chatters to “send” bouncing balls to each other by clicking on the Canvas.

The heart of the app is simply another use of the EsObject instance from the chat application, which is created when the user clicks on the canvas. This EsObject instance adds information about a ball that one user created for the others in the room:

function eventMouseUp(event) {
   var mouseX;
   var mouseY;
   if (event.layerX ||  event.layerX == 0) { // Firefox
      mouseX = event.layerX ;
      mouseY = event.layerY;
   } else if (event.offsetX || event.offsetX == 0) { // Opera
      mouseX = event.offsetX;
      mouseY = event.offsetY;
   }
   ballcounter++;
   var maxSize = 8;
   var minSize = 5;
   var maxSpeed = maxSize+5;
   var tempRadius = Math.floor(Math.random()*maxSize)+minSize;
   var tempX = mouseX;
   var tempY = mouseY;
   var tempSpeed = maxSpeed-tempRadius;
   var tempAngle = Math.floor(Math.random()*360);
   var tempRadians = tempAngle * Math.PI/ 180;
   var tempvelocityx = Math.cos(tempRadians) * tempSpeed;
   var tempvelocityy = Math.sin(tempRadians) * tempSpeed;
   var pmr = new PublicMessageRequest();
   pmr.message = "";
   pmr.roomId = _room.id;
   pmr.zoneId = _room.zoneId;
   var esob = new ElectroServer.EsObject();
   esob.setFloat("tempX",tempX );
   esob.setFloat("tempY",tempY );
   esob.setFloat("tempRadius",tempRadius );
   esob.setFloat("tempSpeed",tempSpeed );
   esob.setFloat("tempAngle",tempAngle );
   esob.setFloat("velocityx",tempvelocityx );
   esob.setFloat("velocityy",tempvelocityy );
   esob.setString("usercolor",usercolor );
   esob.setString("ballname",username+ballcounter);
   esob.setString("type", "newball");
   pmr.esObject = esob;
   es.engine.send(pmr);
   statusMessages.push("send ball");

   }

When a user connected in the same room receives this public message, we handle the newball event in a similar manner to how we handled the chat text, by using the onPublicMessageEvent() function. When the function sees an event with the type newball, it calls createNetBall(). The createNetBall() function creates ball objects to bounce around the canvas, much like the ones we created in Chapter 5:

function onPublicMessageEvent(event) {
   statusMessages.push("message received");
   var esob = event.esObject;
   if (esob.getString("type") == "chatmessage") {
    chatMessages.push(event.userName + ":" + esob.getString("message"));
   } else if (esob.getString("type") == "newball") {
      statusMessages.push("create ball")
      createNetBall(esob.getFloat("tempX"),esob.getFloat("tempY"),
          esob.getFloat("tempSpeed"),esob.getFloat("tempAngle"),
          esob.getFloat("tempRadius"),esob.getFloat("velocityx"),
          esob.getFloat("velocityy"),event.userName,esob.getString("usercolor"),
          esob.getString("ballname") );
   }

}

function createNetBall(tempX,tempY,tempSpeed,tempAngle,tempRadius,tempvelocityx,
                       tempvelocityy, user, usercolor, ballname) {

    tempBall = {x:tempX,y:tempY,radius:tempRadius, speed:tempSpeed,
        angle:tempAngle,
        velocityx:tempvelocityx, velocityy:tempvelocityy,nextx:tempX,
        nexty:tempY,
        mass:tempRadius, usercolor:usercolor, ballname:ballname}
    balls.push(tempBall);
    }

Figure 11-6 shows what this demo looks like when users click the mouse button to send balls to other users. The colors of the balls are assigned randomly. You can see the full set of code for this example in CH11EX3.html.

ElectroServer chat ball demo
Figure 11-6. ElectroServer chat ball demo

This Is Just the Tip of the Iceberg

There is much more you can do with ElectroServer than what we showed you in this chapter. Sending and receiving PublicMessage events can get you only so far when designing multiuser/multiplayer applications.

To start designing multiplayer applications seriously, you will need to delve into the extension and plug-in architecture of ElectroServer, as well as explore plug-in events, which are used to communicate to the server portion of an application. We suggest you check out http://www.electrotank.com/es5.html for more information about the socket server. You can also read ActionScript for Multiplayer Games and Virtual Worlds by Jobe Makar (New Riders). Even though it centers on Flash and an earlier version of ElectroServer, the architectural information about designing apps for a socket server is well worth your time. You can check out the current ElectroServer JavaScript Client API.

At the same time, ElectroServer can be used with technologies other than Canvas (such as Flash, iOS, and so on), so Canvas will be able to communicate with other socket servers via JavaScript and WebSockets. We chose to base this example on ElectroServer because it allowed us to create a full application for you to test and work through. Other libraries and tools are bound to appear very soon that can work with Canvas—for example, the SmartFox server, which now supports WebSockets and JavaScript without add-ons.

Creating a Simple Object Framework for the Canvas

As you have seen throughout this book, you can easily create a lot of code when working with the HTML5 Canvas. The fact is, large applications can get out of hand very easily if you put all of your JavaScript into the main .html file. However, to become efficient when developing applications for the HTML5 Canvas, you will need to develop a framework for your applications. There are many freely available frameworks that exist right now for you to use (for example, Impact.js, Easel.js), but we are going to focus on creating our own small framework for Canvas application development.

In this section, we will create a drag-and drop-application. You will click on colored “bulbs” and decorate a Christmas tree, shown in Figure 11-7. This might seem like a simple application, but it will require us to create a system that recognizes mouse clicks, dragging items, and keeping track of an infinite number of objects.

Drag-and-drop application example
Figure 11-7. Drag-and-drop application example

Creating the Drag-and-Drop Application

When creating our drag-and-drop application, we need to accomplish the following:

  • We will create objects that can be clicked on to decorate a Christmas tree and can be dragged, dropped, and dragged again.

  • We need to make the Canvas think it works in “retained mode” so that we can keep track of multiple objects. To do this, we need to create a “display list” of objects.

  • We need to add the ability for these Canvas objects to listen to “events,” and we need to have a way for mouse events to be “broadcast” events to objects that need to “hear” them.

  • We want to change the mouse pointer to a hand cursor when it is over objects that can be clicked to make the application act like it might in Flash or Silverlight.

  • The application will actually be a “click and stick” version of drag and drop. This means that when you click on an item, it sticks to the mouse until you click the mouse again.

Application Design

To create our framework, we will be creating objects and resource files that will help us design our application. Here is a brief run-down of what we will be creating:

EventDispatcher.js

The base class for objects that need to broadcast events to other objects

Ornament.js

A class that represents the draggable objects in the application

DisplayList.js

A class that represents the “retained mode” that we will simulate for our application

GameUtilities.js

A recourse file filled with nifty functions that we can reuse

DragAndDrop.js

The main application class

DragAndDrop.html

The HTML file that pulls all of our code together

You can find these files in the Chapter 11 /draganddrop folder in the code distribution.

EventDispatcher.js

The first thing we need to do is to create a way for our JavaScript object to subscribe to and dispatch events. Standard DOM objects can have event listeners to listen for events—for example:

theCanvas.addEventListener("mouseup",onMouseUp, false);

However, Canvas images and drawing paths cannot create events because they are not kept in a retained mode. Our task is to create an event dispatcher object in JavaScript that we can use as the base class for other objects. We will use the EventDispatcher object as the base class for our Ornament class, so when an ornament is clicked, we can dispatch an event and the subscriber to that event can take some action.

EventDispatcher.js needs the following three methods:

addEventListener()

Allows us to add a listener (subscriber) for a particular event

dispatch()

Allows us to send events to listeners

removeEventListener()

Allows us to remove a listener when it is no longer needed

By defining all the preceding methods as properties of the EventDispatcher prototype (EventDispatcher.prototype.addEventListener()), another class will be able to use this class as a base class and inherit all of them.

//Adapted from code Copyright (c) 2010 Nicholas C. Zakas. All rights reserved.
//MIT License


function EventDispatcher(){
    this._listeners = {};
}
EventDispatcher.prototype.addEventListener = function(type, listener){
        if (typeof this._listeners[type] == "undefined"){
            this._listeners[type] = [];
        }
        this._listeners[type].push(listener);
 }
EventDispatcher.prototype.dispatch = function(event){
        if (typeof event == "string"){
            event = { type: event };
        }
        if (!event.target){
            event.target = this;
        }
        if (!event.type){  //false
            throw new Error("Event object missing 'type' property.");
        }
        if (this._listeners[event.type] instanceof Array){
            var listeners = this._listeners[event.type];
            for (var i=0, len=listeners.length; i < len; i++){
                listeners[i].call(this, event);
            }
        }
    }
EventDispatcher.prototype.removeEve ntListener = function(type, listener){
        if (this._listeners[type] instanceof Array){
            var listeners = this._listeners[type];
            for (var i=0, len=listeners.length;
                 i < len; i++){
                if (listeners[i] === listener){
                    listeners.splice(i, 1);
                    break;
                }
            }
        }

}

Ornament.js

The Ornament class defined in Ornamant.js will use EventDispatcher as its’ base class. Instances of Ornament will represent the bulbs we create and then drag and drop onto our Christmas tree.

To inherit all the methods and properties from EventListener, we need to create an object and then set the prototype of the object to EventDispatcher. We also need to set the constructor to be the Ornament function that holds all of the other functions in the Ornament class. It will look something like this:

function Ornament(color,height,width,context) {
...(all code goes here)
}
Ornament.prototype = new EventDispatcher();
Ornament.prototype.constructor = Ornament;

Because the Ornament class is the heart of this application, it contains many essential properties that form the basis of how this application will function:

bulbColor

Color of bulb (red, green, blue, yellow, orange, purple).

file

Filename of bulb image to load. We generate this name when we know the color of the bulb.

height

Height of bulb image.

width

Width of bulb image.

x

x position of bulb.

y

y position of bulb.

context

Canvas context.

loaded

Boolean value; set when bulb image has loaded.

image

Image to load.

EVENT_CLICKED

Event to dispatch when clicked.

type

“Factory” or “copy.” (Factory bulbs are the ones you click on to make a copy.)

dragging

Boolean value: Is the bulb being dragged? This is essential information for a drag-and-drop application.

Here is the code to create the previous variables:

this.bulbColor = color;
this.file = "bulb_"+ this.bulbColor + ".gif";
this.height = height;
this.width = width;
this.x = 0;
this.y = 0;
this.context = context;
this.loaded = false;
this.image = null;
this.EVENT_CLICKED = "clicked";
this.type = "factory";
this.dragging = false;

When a user clicks on an instance of Ornament, the mouseUp() method is called (by way of DisplayList, but we will get to that in the next section) and we dispatch an event to subscribers to say the bulb has been clicked. In this app, the only subscribers will be an instance of the DragAndDrop class, but in theory, there could be many more.

this.onMouseUp = function (mouseX,mouseY) {
   this.dispatch(this.EVENT_CLICKED);
}

Instead of drawing everything in a main drawScreen() function of DragAndDrop.js, as we have done throughout the rest of this book, our display objects (like Ornament) will have their own draw() functions. This function draws the bulb image at the specified x and y location at the size of width and height.

This helps keep the draw() function in the main app class as simple as possible:

this.draw = function() {
this.context.drawImage(this.image,this.x,this.y, this.width,this.height);
    }

The last thing we do in our class is call the loadImage() function, which loads the image file associated with the file property:

this.loadImage = function (file) {
        this.image = new Image();
        this.image.onload = this.imageLoaded;
        this.image.src = file;
    }

    this.imageLoaded = function() {
        this.loaded = true;
    }



    this.loadImage(this.file);

You can see the final code listing in Ornament.js in the code distribution.

DisplayList.js

DisplayList is a JavaScript object that will hold a list of items we are displaying on the canvas. It will send mouse click events to the items in the list when a user clicks on them on the canvas. It will function as our “retained mode” for this application.

DisplayList has two properties:

objectList (array)

A list of the items to display.

theCanvas

Reference to the Canvas context. We need this so that we can find the proper mouse x and y coordinates when a user clicks on an item in the display list.

The addChild() function of DisplayList adds an object to the objectList array by pushing it into the array. All the items in objectList will have their draw() functions called when the displayList draw() function is called:

this.addChild = function(child) {
    this.objectList.push(child);
}

The removeChild() function finds the first instance of the object in the display list passed as a parameter and then removes it from objectList. We do this using the array.indexOf() method, which finds the first instance of an object in an array and removes it:

this.removeChild = function(child) {
var removeIndex = null;
    removeIndex = this.objectList.indexOf(child,0);
    if (removeIndex != null) {
        this.objectList.splice(removeIndex,1);
    }
}

The draw() function of DisplayList loops through all the objects in objectList and calls their draw() functions:

this.draw = function() {
    for (var i = 0; i < this.objectList.length; i++) {
         tempObject = this.objectList[i];
         tempObject.draw();
      }
}

The mouseUp function finds the current x and y position of the mouse pointer in a similar fashion to how we have done it previously in this book. (See Chapter 6.) Then, using a “hit test Point” collision detection (again, as we saw in Chapter 6), we check to see whether the mouse was clicked on any of the items in objectList. If so, it calls that object’s mouseUp() function:

this.onMouseUp = function(event) {
    var x;
    var y;
    if (event.pageX || event.pageY) {
        x = event.pageX;
        y = event.pageY;
    } else {
     x = e.clientX + document.body.scrollLeft +
           document.documentElement.scrollLeft;
     y = e.clientY + document.body.scrollTop +
           document.documentElement.scrollTop;
    }
    x -= this.theCanvas.offsetLeft;
    y -= this.theCanvas.offsetTop;

    var mouseX=x;
    var mouseY=y;

    for (i=0; i< this.objectList.length; i++) {
        var to = this.objectList[i];
        if ( (mouseY >= to.y) && (mouseY <= to.y+to.height)
                && (mouseX >= to.x) && (mouseX <=
             to.x+to.width) ) {
             to.onMouseUp(mouseX,mouseY);
            }
        }

    }

You can see the final code listing in DisplayList.js in the code distribution.

GameUtilities.js

GameUtilities.js is where we will put our debugger function and our check for canvasSupport(). In the future, you could plan to put any utility functions in here that do not need to exist in a class.

var Debugger = function () { };
Debugger.log = function (message) {
    try {
        console.log(message);
    } catch (exception) {
        return;
    }
}


function canvasSupport () {
    return Modernizr.canvas;
}

DragAndDrop.js

DragAndDrop.js is the main class for the drag-and-drop application. It acts as the controller for all the other code and classes we have already created.

The first things we do in DragAndDrop.js are as follows:

  1. Check for Canvas support using the new function in GameUtilities.js.

  2. Get a reference to the Canvas context (theCanvas).

We would begin to define this class as follows:

function DragAndDrop() {

    if (!canvasSupport()) {
        return;
        }

    var theCanvas =  document.getElementById("canvasOne");
    var context = theCanvas.getContext("2d");

    ...(code goes here)...
}

Next, we begin to create properties that we will use to create the application:

var backGround;
var bulbColors = new Array ("red","blue","green","yellow","orange","pink",
                            "purple");
var bulbs;
backGround

Holds the background image (a black field with snow and a Christmas tree)

bulbColors

An array of color values we will use when placing bulbs on the canvas

bulbs

An array to hold the bulbs that we are manipulating on the canvas

Now we define some variable that we will use to place objects on the canvas. These variables will be used to place the factory bulbs. Factory bulbs are the ones the user clicks on to create new bulbs to drag and drop. Now they could be defined as const, but we elected to use var. This is because Internet Explorer does not support the JavaScript const keyword.

var BULB_START_X = 40;
var BULB_START_Y = 50;
var BULB_Y_SPACING = 10;
var BULB_WIDTH = 25;
var BULB_HEIGHT = 25;

Next we create an array to hold to hold the factory bulbs:

var clickBulbs;

The following two variables are used to limit the number of clicks the application responds to. clickWait is the number of calls to gameLoop() to wait until we allow the user to click again. The value of clickWaitedFrames is set to 0 when the user clicks a bulb, so the process can restart. If you don’t have some kind of limit to the number of mouse clicks you listen for, you can get into a situation where objects never get dragged and dropped, because as soon as you click, the event fires multiple times.

var clickWait = 5;
var clickWaitedFrames = 5;

Next we create an instance of DisplayList passing a reference to the Canvas context. This will hold all the Ornament objects we display on the canvas.

var displayList = new DisplayList(theCanvas);

We also need to create listeners for mousemove and mouseup, so we use the events to click and/or drag bulbs on the screen:

theCanvas.addEventListener("mouseup",onMouseUp, false);
theCanvas.addEventListener("mousemove",onMouseMove, false);

Next we initialize the arrays for bulbs and clickBulbs and load the background image:

bulbs = new Array();
clickBulbs = new Array();
backGround = new Image();
backGround.src = "background.gif";

The factory bulbs are the ones we click on the canvas to create the bulbs that are dragged and dropped. To create them, we loop through all the colors in the bulbColors array, creating a new Ornament instance for each color. We then set the type property to factory, place it on the canvas using our placement variables (x,y), and then add it to the clickBulbs array and add it to the instance of displayList:

for (var i=0;i < bulbColors.length; i++) {
    var tempBulb = new     Ornament(bulbColors[i],BULB_WIDTH,
        BULB_HEIGHT,context);
    tempBulb.addEventListener(
        tempBulb.EVENT_CLICKED ,
        onBulbClicked);
    tempBulb.x = BULB_START_X;
    tempBulb.y = BULB_START_Y +
        i*BULB_Y_SPACING +i*BULB_HEIGHT;
    tempBulb.type = "factory";
    clickBulbs.push(tempBulb);
    displayList.addChild(tempBulb);
}

Next we create our game loop. We create a setTimeout loop that calls draw() every 20 milliseconds. This is also where we update clickWaitedFrames to test whether we will accept another mouse click:

function gameLoop() {
    window.setTimeout(gameLoop, 20);
    clickWaitedFrames++;
    draw();
}

Our draw() function is slimmed down considerably from others we have created previously in this book. First, it draws a background image, and then it calls displayList.draw() to draw all objects in displayList. For this simple display list to work, you need to add objects to the list in the opposite order that you want them layered because the later ones will be drawn on top of the earlier ones:

function  draw () {
    context.drawImage(backGround,0,0);
    displayList.draw();
}

The onBulbClicked() function is the heart of the DragAndDrop class. This function does several things:

  1. It tests to see whether a click is valid by checking clickWaitedFrames against clickWait.

  2. If a click is valid, it tests to see whether we have clicked on a factory bulb (so that we can make a new one) or whether it is a draggable bulb instance and sets clickWaitedFrames to 0 so that the app will wait a few frames until another click is valid.

  3. We find the instance of Ornament that was clicked on by using the event.target property. It will be a reference to the object that dispatched the event.

  4. If it is a factory bulb and if we are not currently dragging any bulbs, we create a new instance of Ornament and start dragging it. We set its type to copy, which means it is draggable.

  5. If it is a draggable instance of Ornament, we check to see whether we are currently dragging it. If so, we drop it by setting the dragging property to false. If not and if we are not dragging another bulb, we start dragging the new one by setting its dragging property to true.

The following code matches the previous description:

function onBulbClicked(event) {
    if (clickWaitedFrames >= clickWait) {
        clickWaitedFrames = 0;
        var clickedBulb = event.target;
        if ( clickedBulb.type  == "factory" && !currentlyDragging()) {
            var tempBulb = new Ornament(clickedBulb.bulbColor,BULB_WIDTH,
                                        BULB_HEIGHT,context);
            tempBulb.addEventListener(tempBulb.EVENT_CLICKED , onBulbClicked);
            tempBulb.y =  clickedBulb.y+10;
            tempBulb.x =  clickedBulb.x+10;
            tempBulb.type = "copy";
            tempBulb.dragging = true;
            bulbs.push(tempBulb);
            displayList.addChild(tempBulb);

        } else {
            if (clickedBulb.dragging) {
                clickedBulb.dragging = false;
            } else {
                if (!currentlyDragging()) {
                    clickedBulb.dragging = true;
                }
            }

        }
    }

}

Now we need to create the function to test whether the bulb being dragged is the same one we are testing in onCBulbClicked(). To do this, we simply loop through the bulbs array and see whether any bulb has its dragging property set to true:

function currentlyDragging() {
    isDragging = false
    for (var i =0; i < bulbs.length; i++) {
        if (bulbs[i].dragging) {
            isDragging = true;
        }
    }
    return isDragging;
}

When the user clicks (a mouseup event is fired on the Canvas DOM object), we want to send a message to the display list so that we can check to see whether any of the objects in the list have been clicked:

function onMouseUp(event) {
    displayList.onMouseUp(event);
}

When a mousemove event is fired on the Canvas DOM object, we want to do two things:

  1. Move the bulb that is currently being dragged to be under the mouse pointer by setting its x and y properties to the x and y location of the mouse.

  2. Check to see whether the mouse is over any clickable objects, and if so, change the look of the pointer to “hand” (to signify a button), using CSS. We do this by looping through all of the Ornament objects (both factory and copy ones) and checking a hit test point collision detection routine to see whether the mouse is over any of them. If it is over one, we set the style of the mouse to “pointer” (by setting the cursor variable). If not, we set it to “default”. Then we update the style like this:

    theCanvas.style.cursor = cursor;
    function onMouseMove(event) {
    
        var x;
            var y;
        if (event.pageX || event.pageY) {
              x = event.pageX;
              y = event.pageY;
        } else {
             x = e.clientX + document.body.scrollLeft +
               document.documentElement.scrollLeft;
              y = e.clientY + document.body.scrollTop +
               document.documentElement.scrollTop;
        }
        x -= theCanvas.offsetLeft;
        y -= theCanvas.offsetTop;
    
        var mouseX=x;
        var mouseY=y;
        for (var i =0; i < bulbs.length; i++) {
    
            if (bulbs[i].dragging) {
                bulbs[i].x = mouseX - BULB_WIDTH/2;
                bulbs[i].y = mouseY - BULB_HEIGHT/2;
            }
        }
    
        var cursor ="default";
        for (i=0; i< bulbs.length; i++) {
            var tp = bulbs[i];
            if ( (mouseY >= tp.y) && (mouseY <= tp.y+tp.height) &&
                 (mouseX >= tp.x)
                 && (mouseX <= tp.x+tp.width) ) {
                    cursor = "pointer";
            }
        }
    
        for (i=0; i< clickBulbs.length; i++) {
            var tp = clickBulbs[i];
            if ( (mouseY >= tp.y) && (mouseY <= tp.y+tp.height) &&
                 (mouseX >= tp.x)
                    && (mouseX <= tp.x+tp.width) ) {
                cursor = "pointer";
            }
        }
        theCanvas.style.cursor = cursor;
    }

DragAndDrop.html

Because we have moved almost all the code out of the HTML file, here is our “almost” bare-bones HTML file that starts the drag-and-drop application.

We need to include all the files we just created:

<script type="text/javascript" src="EventDispatcher.js"></script>
<script type="text/javascript" src="DisplayList.js"></script>
<script type="text/javascript" src="GameUtilities.js"></script>
<script type="text/javascript" src="DragAndDrop.js"></script>
<script type="text/javascript" src="Ornament.js"></script>
<script type="text/javascript" src="modernizr.js"></script>

We need to create the canvas in HTML. For this application, we will center it on the screen:

<div align="center">
<canvas id="canvasOne" width="640" height="480" style="cursor: default;">
 Your browser does not support the HTML 5 Canvas.
</canvas>
</div>

Finally, we need to start the app when the window has loaded:

<script type="text/javascript">
window.addEventListener("load", eventWindowLoaded, false);
function eventWindowLoaded () {
    DragAndDrop();
}
</script>

We have now created a very simple object-oriented structure for a Canvas application. With this application, we have attempted to solve some glaring application development issues that occur when creating Canvas applications. We have created a way to subscribe to and broadcast events from logical objects on the canvas, a way to find and click on individual objects on the canvas, and we keep track of those objects using a display list that simulates “retained mode.” While there are many ways this object model could be improved, we feel that this is a good starting point for your future endeavors with the HTML5 Canvas.

You can test this example by finding dragandrop.html in the code distribution (in the Chapter 11 /draganddrop folder) and opening it in your web browser.

Windows 8 Apps and the HTML5 Canvas

One very interesting development that occurred as this book was going to press was the release of Windows 8. Windows 8 offers some very interesting ways for developers to create applications for the operating system and for the Windows Store. One of those methods is to package HTML5 using Visual Studio 2012.

To demonstrate how easy it is to create an HTML5 Canvas application for Windows 8, we will take the previous drag-and-drop example and show you the changes that are required to get it running under Windows 8 as a bare-bones application.

The first thing you need to do is download Visual Studio Express for Windows 8 (if you don’t already have Visual Studio 2012 installed).

Next, you want to create a new Blank App project using the JavaScript template. (See Figure 11-8.)

Creating a new JavaScript project in Visual Studio 2012
Figure 11-8. Creating a new JavaScript project in Visual Studio 2012

Next, you need to add all the files we created for the drag-and-drop example to the new project. Copy the files to the solution directory Visual Studio created for you. (This is usually in documents\visual studio 2012\projects\[project name]\[project name].)

After you do this, right-click in the Solution Explorer and select Add->Existing Item. Choose all the files you just copied to the directory. (See Figure 11-9.)

Add drag-and-drop files to the project
Figure 11-9. Add drag-and-drop files to the project

Now you are ready to edit the files and get the app running. We are going to describe the quickest route to having a running application.

Start with opening default.html. You need to copy all the JavaScript included (except for modernizr.js) from draganddrop.html to default.html. You also need to copy the HTML tags from draganddrop.html (<div> and <canvas>) and put them into the body of default.html. Leave everything else intact. The final file should look like this:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>draganddroptest</title>
    <!-- WinJS references -->
    <link href="//Microsoft.WinJS.1.0/css/ui-dark.css" rel="stylesheet" />
    <script src="//Microsoft.WinJS.1.0/js/base.js"></script>
    <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>

    <!-- draganddroptest references -->
    <link href="/css/default.css" rel="stylesheet" />
    <script src="/js/default.js"></script>

    <script type="text/javascript" src="EventDispatcher.js"></script>
    <script type="text/javascript" src="DisplayList.js"></script>
    <script type="text/javascript" src="GameUtilities.js"></script>
    <script type="text/javascript" src="DragAndDrop.js"></script>
    <script type="text/javascript" src="Ornament.js"></script>
</head>
<body>
<div align="center">
<canvas id="canvasOne" width="640" height="480" style="cursor: default;">
 Your browser does not support the HTML 5 Canvas.
</canvas>
</div>
</body>
</html>

Next, we need to update default.js. This time, we will add the call to DragAndDrop() that is in draganddrop.js so that the application will start when Windows 8 is ready. Add the call in the section with the following comment:

   // TODO: This application has been newly launched. Initialize
   // your application here.

Leave everything else untouched. This will make sure our app starts as soon as Windows 8 is ready for our program to run.

// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;

    app.onactivated = function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !==
                activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
                DragAndDrop();
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }
            args.setPromise(WinJS.UI.processAll());
        }
    };

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. You might use the
        // WinJS.Application.sessionState object, which is automatically
        // saved and restored across suspension. If you need to complete an
        // asynchronous operation before your application is suspended, call
        // args.setPromise().
    };

    app.start();
})();

Finally, edit GameUtilities.js and change canvasSupport() to always return true. We do this because the Canvas will always be available, and we removed the reference to moderizr.js in default.html.

function canvasSupport () {
    return true;
}

Believe it or not, we are now ready to test the app. Click on the green arrow on the menu bar to test. (See Figure 11-10.) Be sure to have Local Machine selected from the drop-down menu.

Test application in Visual Studio
Figure 11-10. Test application in Visual Studio

When you test the app, you should see what is shown in Figure 11-11.

Drag and drop running as a Windows 8 application
Figure 11-11. Drag and drop running as a Windows 8 application

To get back to Visual Studio, roll the mouse pointer to the upper-left corner of the screen, and click on the thumbnail of the Visual Studio interface. You can close the app by doing the same thing while in Visual Studio, right-clicking on the drag-and-drop app, and choosing close.

You can find all the code for this application in the Chapter 11 windows8 folder in the code distribution.

From there you should be able to take your HTML5 Canvas apps and Visual Studio 12 and start making all kinds of stuff for Windows 8.

If you are wondering why we did not cover Windows Phone, it’s because at this moment there is no support for HTML5 to create apps for the Windows Phone using Visual Studio (although it is supported in the Internet Explorer 10 web browser). However, as we went to press, there was an effort underway in the Phonegap/Cordova community to create templates for the Windows Phone. When that happens, you can test your HTML5 Canvas apps on that platform too.

What’s Next in HTML5.1 and Canvas Level 2?

At the end of 2012, the W3C specs for HTML5.1 and Canvas Level 2 were unleashed into the world. While many of these features are a long way away from being supported in the browser, we thought we would highlight some of the most interesting new functions that will arrive for Canvas developers in the next few years.

HTML5.1 Canvas Context

In HTML5.1, the Canvas context will include some methods beyond toDataURL() and getContext().

supportsContext()

This method will allow a developer to test for support for different Canvas contexts in a web browser. In this book, we used the 2D context most of the time, but we added “experimental-wbgl” in this chapter, or “moz-3d.” In the future, the new Canvas context will be registered at this site.

toDataURLHD(), toBlob(), toBlobHD()

If you recall from earlier in the book, toDataURL() allows a developer to export a base64 encoded string that represents the Canvas display, which can then be converted to an image. This image is restricted to 96 dpi. In HTML5.1, this ability will be expanded to include several new variations:

toDataURLHD()

Returns canvas data at native resolution instead of 96 dpi

toBlob()

Returns data as a blob at 96 dpi instead of a base64 encoded string

toBlobHD()

Returns data as a blob at native resolution

Canvas Level-2

The next version of the Canvas, dubbed “Level 2,” is slated to have changes and updates to the API. The new features and functionality are in a constant state of flux. It appears that most of these new features are being added so that the Canvas can be more DOM accessible. At press time, it appeared that some of the following would make it into the final specification:

  • A way to define “hit regions” for mouse events instead of listening across the entire Canvas.

  • The TextMetrics object returned from context.measureText will include many more properties, including bounding box and height information.

  • New ways to create and use patterns from images and image data.

  • New Path objects that can have text applied.

For more information about the Canvas updates in HTML5.1, check out this site.

For more information about Canvas Level 2, check out this site.

Conclusion

Over the past 11 chapters, you have been immersed in the world of HTML5 Canvas. We have given you dozens of examples and applications to work from and through so that you can start building your own creations. From simple text displays to high-performance games, we have shown you many ways to bring some of the magic of previous RIA (Rich Internet Application) technologies into the plug-in-less browser experience.

We offered many strategies for integrating Canvas with other HTML5 technologies, as well as techniques for handling text, displaying graphics, scrolling bitmaps, creating animation, detecting multiple types of collisions, embedding and manipulating video, playing music, handling sound effects, creating user interfaces, optimizing code, and preparing apps for the mobile web and Windows 8. We even introduced you to the future of 3D and multiuser applications directly in the web browser and showed you how to get started creating an object model for the HTML5 Canvas.

However, the true future is up to you. HTML5 and Canvas are dynamic topics that are still in a rapid state of change and adoption. While this book is a good starting point, you will need to keep abreast of new changes to the technology. Visit our website for news and updates on HTML5 Canvas.

O’Reilly also has several books that you might find useful, including:

If you are interested in learning how some of the game-development techniques described in this book (as well as many others) can be applied to Flash, check out our other most recent book, The Essential Guide to Flash Games (friendsofED).

There is a real paradigm shift occurring right now on the Web. For most of the first decade of the 21st century, Java, Flash, Silverlight, and other plug-in RIA technologies dominated application development and design. At the time, there appeared to be no better solution for the development of rich applications in a web browser than to bolt on technology that was not native to the browser.

The emergence of the “connected apps” culture is changing this. Every platform—from tablets and phones to TVs, e-readers to tablets, wireless printers to desktop PCs—is targeted for web-enabled applications sold or distributed through an app store. In many ways, these apps are replacing RIA applications or, at the very least, offering a compelling new platform for their development and distribution.

Where RIA technologies of the past—like Java, Flash, and Silverlight—could target nearly all web browsers and PCs, they are having trouble finding a true foothold in the area of connected apps (especially on platforms where they are restricted from running, like iOS). This is where HTML5 Canvas can really make a difference. With true cross-platform execution, applications run in the web browser can be made available to the widest audience possible. Soon these applications will be enhanced with 3D graphics and have the ability to communicate with one another via technologies like the ElectroServer socket server. One can envision a day in the near future where technology platforms fade away, and the web-connected app world simply works, regardless of screen or location. This is the promise of HTML5—especially HTML5 Canvas. So, now that you have the tools to begin, what do you plan to build?