Tuesday, October 28th, 2008
GameJS: Canvas Game Library
<p>Tommy Maintz has created a fun project called GameJS a 2d game development framework using JavaScript and Canvas.The API
GameJS.framework.Game - This is the main game class you extend when creating the game. It has the following methods which you have to override: initialize, loadContent, update and draw. This is all very similar to the XNA framework. Tetris.js is an example of how to extend this class.
GameJS.content.ContentManager - This class handles the asynchronous loading of resources saving you some headaches. The following code loads a new texture which can be drawn in the games draw() method:
JAVASCRIPT:
loadContent: function() { // set the screen rect this.screenRect = new Rectangle( 0, 0, this.screenWidth, this.screenHeight ); // Create the background texture. The third argument argument // makes this texture is not redrawn every frame (gives // performance boost since the background doesn't change) this.backgroundTexture = new Texture( this.graphics, this.content.load('Textures/BackgroundTexture.png'), false );GameJS.graphics.GraphicsDevice - This file sets up the main canvas. It also manages the Z-buffering, and has a method that creates a new resource context which individual textures can use. You can easily change its properties. A simple example:
JAVASCRIPT:
constructor: function() { var ds = this.graphics.deviceSettings; ds.screenWidth = this.screenWidth; ds.screenHeight = this.screenHeight; ds.target = document.getElementById('tetris-container'); ds.fullScreen = false; ds.applyChanges();GameJS.graphics.SpriteBatch - This class acts as a wrapper around the canvas element. It has a draw method which allows you to draw a texture to the graphicsDevice. You can pass it a destination and source rectangle. Code example:
JAVASCRIPT:
draw: function(gameTime) { // clear the device this.graphics.graphicsDevice.clear(); var spriteBatch = new GameJS.graphics.SpriteBatch(this.graphics); spriteBatch.begin(); // Draw the background spriteBatch.draw(this.backgroundTexture, this.screenRect); // Draw the gamefield. spriteBatch.draw(this.gameFieldTexture, this.gameFieldRect);GameJS.input.Keyboard and GameJS.input.KeyboardState - These classes handle the keyboard input. Since JavaScript is an event based language and a game cycle is something continues, the framework takes care of storing the pressed keys. This looks something like the following:
JAVASCRIPT:
update: function(gameTime) { // Get the current input state. var keyboardState = this.keyboard.getState(); if(this.isGamePaused) { if (keyboardState.isKeyPressed(Keys.ENTER)) { this.isGamePaused = false; } }
You can't build a game frameworking without creating a game, and Tommy did the old favourite, creating Jetris as a clone of Tetris.

Related Content:











This is unbelievable. The minute after this post came online, my hosting companies server park completely died. I called them and they don’t know when it will be back up again…
A mirror can be found at http://www.ugame.net/gamejs/ for interested people.
nice idea! :)
This is excellent! The speed (fps) is astounding compared to similar libraries. I’ll definitely be looking into using this for a few upcoming projects of mine.
This is a *great* idea and what JS in the app level really should be used for (my opinion)…
.
Great work :)