Online Book Reader

Home Category

HTML5 Canvas [112]

By Root 6503 0

var rows = 4;

var cols = 4;

var xPad = 10;

var yPad = 10;

var startXOffset = 10;

var startYOffset = 10;

var partWidth = videoElement.width/cols;

var partHeight = videoElement.height/rows;

//320×240

partWidth = 80;

partHeight = 60;

var board = new Array();

//Initialize Board

for (var i = 0; i < cols; i++) {

board[i] = new Array();

for (var j =0; j < rows; j++) {

board[i][j] = { finalCol:i,finalRow:j,selected:false };

}

}

board = randomizeBoard(board);

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

setInterval(drawScreen, 33);

}

Your browser does not support HTML5 Canvas.

Creating Video Controls on the Canvas


One obvious use of the HTML5 Canvas video display functionality is to create custom video controls to play, pause, stop, etc. You may have already noticed that when a video is rendered on the canvas, it does not retain any of the HTML5 video controls. If you want to create controls on the canvas, you need to make them yourself. Thankfully, we have already learned most everything we need to do this—now we just have to put it all together.

Creating video buttons


We are going to use some video control buttons that were created specifically for this example. Figure 6-11 shows a tile sheet that consists of off and on states for play, pause, and stop. The top row images are the on state; the bottom row images are the off state.

Figure 6-11. Video control button tile sheet

NOTE

We don’t use the off state of the stop button in this application, but we included it in case you—the amazing reader and programmer that you are—want to use it later.

We will load this image dynamically onto the canvas, and then place each 32×32 button onto the canvas individually. We use the width and height to calculate which part of the image to display as a control.

Preloading the buttons


The first thing we need to do is preload the button tile sheet. Since we are already testing for the video to preload before we display the canvas, we need a slightly new strategy to preload multiple objects. For this example, we will use a counter variable named loadCount that we will increment each time we detect that an item has loaded. In conjunction with that variable, we will create another named itemsToLoad, which will hold the number of things we are preloading. For this app that number is two: the video and the tile sheet. These two variables are created outside of all functions at the top of our JavaScript:

var loadCount = 0;

var itemsToLoad = 2;

Along with videoElement and videoDiv, we also create another new variable, buttonSheet. This is a reference to the image we load that holds the graphical buttons we will use for the video player interface:

var videoElement;

var videoDiv;

var buttonSheet

We now must make some updates to our standard eventWindowLoaded() function that we have used for most of this chapter. First, we are going to change the canplay event handler for the video to a new function, itemLoaded:

videoElement.addEventListener("canplay",itemLoaded,false);

We used the canplay event instead of canplaythrough because, most of the time, a user wants to start watching a video as soon as enough data has been buffered to play, and not after the entire video has loaded.

Next, we need to load our tile sheet. We create a new Image object and set the src property to videobuttons.png, which is the file shown in Figure 6-11. We also set its onload event handler to itemLoaded, just like the video:

buttonSheet = new Image();

buttonSheet.src = "videobuttons.png";

buttonSheet.onload = itemLoaded;

}

Finally, we create the itemLoaded() event handler function. When this function is called, we increment the loadCount variable and test it against the itemsToLoad variable.

NOTE

loadCount should never be greater than itemsToLoad if your application is running correctly. However, we find it safer to

Return Main Page Previous Page Next Page

®Online Book Reader