So I have some free time this winter break and want to build a simple 2d HTML5 canvas game engine. Mostly a physics engine that will dictate the way objects move and interact(collisions, etc). I made a basic game here:


and would like to make more, and thought that this would be a good reason to make a simple framework for this stuff. Here are some questions:

  • Does the scripting language have to be Javascript? What about Ruby? I will probably write it with jQuery because of the selecting powers, but I'm curious either way.

  • Are there any great guides you guys know of? I want a fast guide that will help me bust out this engine sometime in the next 2 weeks, hopefully sooner.

  • What are some good conventions I should be aware of?

  • What's the best way to get sound? At the moment I'm using something like this:

    var audioElement = document.createElement('audio'); audioElement.setAttribute('src', 'paddle_col.wav'); audioElement.load();

I'm interested in making this engine lightweight and extremely efficient, I will do whatever it takes to get great speeds and processing power. I know this question is fairly vague, but I just need a push in the right direction.

Thanks :)

  • \$\begingroup\$ Browsers can only interprete Javascript. When you want to use another language, you need to use a transcompilation tool to turn it into Javascript code so that the browser can understand it. There are languages like Coffescript or Google Dart which were designed explicitely for that. \$\endgroup\$ – Philipp Dec 18 '12 at 9:52
  • \$\begingroup\$ These are actually four unrelated questions and not one. You should maybe ask them as separate questions. \$\endgroup\$ – Philipp Dec 18 '12 at 9:53

Q. Does the scripting language have to be Javascript?

A. Yes. There are other somewhat supported scripting languages, but JavaScript is by far the easiest to get into, most widely supported, and most widely documented. In terms of JS toolkits available, jQuery is certainly an option. So is Dojo Toolkit; with its AMD design scheme, it's (arguably) the better choice if you're looking to develop long-term. It's the toolkit I decided to go to when developing my own game engine, TGE.

Q: Are there any great guides you guys know of?

A: Yes! The Mozilla Developer Network is a great guide on all things HTML5. It will provide you with the basic skills necessary to manipulate HTML5 DOM elements. However, game development theory is still king; you'll at least need to understand things like separating the physics and animation loops, sprite animation, scene management (yes, PONG has a scene, and there are objects in it which need management). Then there are paradigms like event-driven (vs. function-driven) programming, callbacks, the execution context stack, and so on. I'd suggest reading through The Good Parts, The Definitive Guide (if you've got time), and Patterns.

Q: What are some good conventions I should be aware of?

A: I chose the "do everything in JavaScript" path, and avoid authoring DOM elements in HTML where possible. I'm not sure if this is highly desirable, but it makes the most sense to me to keep as much in JS where possible. YMMV

Breaking down your game into Modules ("singleton"-esque interface-style, uh, interfaces like Graphics and Network and Audio) and Factories (which produce Objects like Sprites and Bullets and ParticleEmitters and PowerUps) is crucial for debugging, and important for organizing your code, especially as your project grows. A toolkit like Dojo (and to some extent, RequireJS) trumps jQuery in terms of usefulness in this regard, which is why I highly tout it.

Develop using Google Chrome, and use Chrome Developer Tools (CDT) to debug your application. Seriously. Firefox+Firebug is nice, but Chrome+CDT beats the pants off it.

Use requestAnimationFrame in your render loop.

Write your game such that it will function in all browsers which support the bare minimum elements you're planning on using. That does not mean supporting IE6, but it does mean making sure your game functions in IE9/10, Firefox, Chrome, and possibly Safari and Opera. You'll learn a lot on making your game compatible with different browsers. Don't waste your time on backward compatibility (or "progressive enhancement"), as people who are using old browser (aside from, say, your parents) won't even be looking at your game.

Trailing comma of death is your enemy.

If you're developing in Windows, Notepad++ is a great editor, and XAMPP is a great web-server stack.

If you're going to eventually write some server-side code, PHP, C#, are great options for server-side scripting languages. Other very popular languages include Ruby, but my experience is limited to the two former, so I won't comment more than that.

Q: What's the best way to get sound?

A: Support OGG and MP3, and you'll cover the bases you need to enable playback in the Big Three browsers. Create your audio tags once at application startup, keep the audio elements around a long time, and reuse them as often as you can, to avoid manipulating the DOM excessively.

Good luck, and happy JavaScripting!

| improve this answer | |
  • \$\begingroup\$ Thanks, I've started development, I'll be referring back to this every now and then. \$\endgroup\$ – Scriptonaut Dec 30 '12 at 20:27

The scripting language has to be Javascript or something which compiles into it. I recommend that you do use Javascript. There are a lot of problems with Javascript - such as its lack of a single standard way of "doing things". Defining classes, inheritance, modularity etc, all have multiple approaches which different authors do differently (or even a mixture).

However, if you don't use Javascript, you'll have to use a language which compiles into JS, and debugging becomes more difficult (because the in-browser debuggers can't see your original source code and therefore won't be source-level debuggers). It is likely to be difficult to debug any Ruby code which is compiled to Javascript.

I recommend you don't use JQuery because I don't like it, and it provides few useful features for game developers. Most games will use only a few DOM elements and they're unlikely to change much (if you mostly use a canvas).

As far as audio is concerned - I think you're best to test it on your intended target platforms and choose the approach which best works for you. Things change quickly, and when your game is ready for audio, there may be better platform support for different APIs.

| improve this answer | |
  • 1
    \$\begingroup\$ I've built the graphical user interface of my game in HTML and I found jQuery to be really helpful for that. One very nice feature is the drag&drop functionality of jQueryUI. \$\endgroup\$ – Philipp Dec 18 '12 at 22:43
  • \$\begingroup\$ @Philipp Great if it works for you. I still feel that it's more suitable for the majority of games to just create a canvas rather than using discrete HTML elements. \$\endgroup\$ – MarkR Dec 20 '12 at 13:47
  • \$\begingroup\$ Well, it depends on what requirements you have for your GUI. When you just need to draw the score value, doing it on the canvas would be sufficient. But in my case I require an advanced GUI with moveable and resizeable windows, scrollable text boxes, text markup, drag&drop between windows, text inputs and many more. I could have implemented those myself in canvas, but why should I when I can get them for free from the HTML engine of the browser and jQuery? \$\endgroup\$ – Philipp Dec 20 '12 at 14:19

I'm not attempting a complete answer, since there are already two that cover the bases rather well. I'll just add the following:

  • jQuery is not a scripting language. It's a JavaScript framework/library for easing DOM manipulation and some other things, but it plays no big part when drawing things to canvas. And you want to be drawing things to canvas, maybe even with WebGL, (instead of DOM manipulation) if you want to get good performance (although CSS3D could potentially perform better than 2d canvas on some platforms). In addition, if you want to "do whatever it takes to get great speeds and processing power" you don't want to add another processing layer (jQuery) between the calls to DOM API.
  • With regards to sound effects, you want to preload them and have multiple copies of each so that you can have the same sound playing multiple times at the same time (think of a machine gun that makes the shooting sound again before the previous bullet has yet finished playing).
  • Game engines themselves are useless. You should be making a game. Only by actually creating a complete game you will know what is actually required from the engine. Then make another. After that you will notice which parts are reusable, which need refactoring etc. and the engine starts to take form. Afterwards, you can also show that your engine was capable of making these games, which will be a huge marketing point if you ever want anybody else using the engine.
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.