Online Book Reader

Home Category

HTML5 Canvas [6]

By Root 6359 0
a listener. Events for existing objects like window are already defined.

Event handler function: eventWindowLoaded()

Call this function when the event occurs. In our code, we will then call the canvasApp() function, which will start our main application execution.

useCapture: true or false

This sets the function to capture this type of event before it propagates lower in the DOM tree of objects. We will always set this to false.

Below is the final code we will use to test to see whether the window has loaded:

window.addEventListener("load", eventWindowLoaded, false);

function eventWindowLoaded () {

canvasApp();

}

Alternatively, you can set up an event listener for the load event in a number of other ways:

window.onload = function()

{

canvasApp();

}

or:

window.onload = canvasApp();

We will use the first method throughout this book.

Encapsulating Your JavaScript Code for Canvas


Now that we have created a way to test to see whether the HTML page has loaded, we can start creating our JavaScript application. Because JavaScript runs in an HTML page, it could be running with other JavaScript applications and code simultaneously. Usually, this does not cause any problems. However, there is a chance that your code might have variables or functions that conflict with other JavaScript code on the HTML page.

Canvas applications are a bit different from other apps that run in the web browser. Because Canvas executes its display in a defined region of the screen, its functionality is most likely self-contained, so it should not interfere with the rest of the page, and vice versa. You might also want to put multiple Canvas apps on the same page, so there must be some kind of separation of JavaScript when defining the code.

To avoid this issue, you can encapsulate your variables and functions by placing them inside another function. Functions in JavaScript are objects themselves, and objects in JavaScript can have both properties and methods. By placing a function inside another function, you are making the second function local in scope to the first function.

In our example, we are going to have the canvasApp() function that is called from the window load event contain our entire Canvas application. This “Hello World!” example will have one function named drawScreen(). As soon as canvasApp() is called, we will call drawScreen() immediately to draw our “Hello World!” text.

The drawScreen() function is now local to canvasApp(). Any variables or functions we create in canvasApp() will be local to drawScreen(), but not to the rest of the HTML page or other JavaScript applications that might be running.

Here is the sample code for how we will encapsulate functions and code for our Canvas applications:

function canvasApp() {

drawScreen();

...

function drawScreen() {

...

}

}

Adding Canvas to the HTML Page


In the section of the HTML page, add a tag using code such as the following:

Your browser does not support HTML5 Canvas.

Now, let’s break this down to understand what we are doing. The tag has three main attributes. In HTML, attributes are set within pointy brackets of an HTML tag. The three attributes we need to set are:

id

The id is the name we will use to reference this tag in our JavaScript code. canvasOne is the name we will use.

width

The width, in pixels, of the canvas. The width will be 500 pixels.

height

The height, in pixels, of the canvas. The height will be 300 pixels.

NOTE

HTML5 elements, including canvas, have many more attributes: tabindex, title, class, accesskey, dir, draggable, hidden, etc.

Between the opening and closing tags, you can put text that will be displayed if the browser executing the HTML page does not support Canvas. For our Canvas applications, we will use the text “Your browser does not support HTML5 Canvas.” However, you can adjust this text to say anything.

Using document to reference the canvas element in JavaScript


We will

Return Main Page Previous Page Next Page

®Online Book Reader