Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Game (Canvas) - UI Techniques?

I'm in the process of building a JavaScript / HTML5 game (using Canvas) for mobile (Android / iPhone/ WebOS) with PhoneGap. I'm currently trying to design out how the UI and playing board should be built and how they should interact but I'm not sure what the best solution is. Here's what I can think of -

  1. Build the UI right into the canvas using things like drawImage and fillText
  2. Build parts of the UI outside of the canvas using regular DOM objects and then float a div over the canvas when UI elements need to overlap the playing board canvas.

Are there any other possible techniques I can use for building the game UI that I haven't thought of? Also, which of these would be considered the "standard" way (I know HTML5 games are not very popular so there probably isn't a "standard" way yet)? And finally, which way would YOU recommend / use?

Many thanks in advance!

EDIT

I've moved this question over to gamedev.stackoverflow.com. You can find the new question here: https://gamedev.stackexchange.com/questions/7090/html5-game-canvas-ui-techniques/7115#7115

like image 420
Jason L. Avatar asked Jan 04 '11 23:01

Jason L.


People also ask

Is HTML Canvas good for games?

HTML CanvasThe <canvas> element is perfect for making games in HTML. The <canvas> element offers all the functionality you need for making games.

What can you do with HTML5 canvas?

According to the HTML5 specification, the CANVAS element is: “...a resolution-dependent bitmap canvas, which can be used for rendering graphs, game graphics, art, or other visual images on the fly.” The CANVAS element lets you draw graphs, graphics, games, art, and other visuals right on the web page in real-time.

Is HTML5 good for games?

HTML 5 is favored by web developers and game development companies alike. It provides many cutting-edge features, like 2D and 3D graphics, audio APIs, offline asset storage, and combined support for the most popular web browsers.

How HTML5 games are made?

HTML5 Game Development Uses WebGL & Canvas: Both WebGL and Canvas are core technologies used in HTML5 for creating complex games suited for browsers. With Canvas, game developers can draw shapes and convert them into 3D images. Such images can be directly added to the library, ready for use.


2 Answers

You can do it a million ways. However you feel most comfortable and your engineers feel most confident.

If you're looking for inspiration or a code example, here's one way that I do it. I have a function that repeatedly draws a menu until a button is pressed. When the button is pressed, the game loads and the old menu click event listeners are removed and new game click event listeners are added. I also end the old draw loop of the menu and start a new game draw loop. Here's some selected snippets to give you the idea of how its done:

Game.prototype.loadMenu = function() {
  var game = this;
  var can = this.canvas;

  // now we can use the mouse for the menu
  can.addEventListener('click', game.menuClickEvent, false);
  can.addEventListener('touchstart', game.menuClickEvent, false);

  // draw menu
  this.loop = setInterval(function() { game.drawMenu() }, 30);
};

Game.prototype.drawMenu = function() {
  // ... draw the menu
}

Game.prototype.loadLevel = function(levelstring) {
  // unload menu
  var can = this.canvas;
  var game = this;
  can.removeEventListener('click', game.menuClickEvent, false);
  can.removeEventListener('touchstart', game.menuClickEvent, false);

  if (this.loop) clearInterval(this.loop);

  // ... other level init stuff


  // now we can press keys for the game
  //can.addEventListener('click', game.gameClickEvent, false);
  can.addEventListener('touchstart', game.gameClickEvent, false);
  can.addEventListener('keydown', game.gameKeyDownEvent, false);

  this.loop = setInterval(function() { game.tick() }, 30);
}

// called from tick()
Game.prototype.draw = function(advanceFrame) {
  // ...
}

This way I'm able to separate out game drawing and game events from menu drawing and menu events. It also gives me leeway to use game/animation elements in my menus should I want to make them look real pretty.

(I posted this at the twin gamedev discussion too)

like image 178
Simon Sarris Avatar answered Oct 30 '22 23:10

Simon Sarris


I do not think that there is a "standard" for this. It highly depends on your UI. I think using the DOM elements is better in most cases, since you do not need to build all of the UI components, events, etc. yourself. They can be styled with CSS to achieve the desired look. If this is not enough, you'll probably need to build the interface elements yourself, but you should make sure that this is really needed. It is probably a huge amount of work to roll your own solution.

like image 34
jwueller Avatar answered Oct 30 '22 21:10

jwueller