Modules: GameJs Reference
This module holds the essentialYour game should provide callbacks for
Surfaceclasses as well as static methods for preloading assets.
gamejs.ready()is maybe the most important as it kickstarts your app.
gamejs.onEventto handle events. To get called continuously, provide a callback to
Methods to create, access and manipulate the display Surface.
gamejs.display.setMode()understands three flags:
- gamejs.display.POINTERLOCK (implies FULLSCREEN)
// disable smoothing gamejs.display.setMode([800, 600], gamejs.display.DISABLESMOOTHING); // disable smoothing and fullscreen gamejs.display.setMode([800, 600], gamejs.display.DISABLESMOOTHING | gamejs.display.FULLSCREEN);
DOM node ids accessed by this module:
- #gjs-canvas - the display surface
- #gjs-loader - loading bar
- #gjs-fullscreen-toggle a clickable element to enable fullscreen
- #gjs-canvas-wrapper this wrapper is added when in fullscreen mode
setMode()is called with the fullscreen flag then the fullscreen mode can be enabled by the player by clicking on the DOM element with id "gjs-fullscreen-toggle". Browser security requires that a user enables fullscreen with a "gesture" (e.g., clicking a button) and we can not enable fullscreen in code.
Fullscreen mode can be exited by many keys, e.g., anything window manager related (ALT-TAB) or ESC. A lot of keys will trigger a browser information popup explaining how fullscreen mode can be exited.
The following keys are "whitelisted" in fullscreen mode and will not trigger such a browser popup:
- left arrow, right arrow, up arrow, down arrow
- shift, control, alt
- page up, page down
- home, end, tab, meta
Utilities for drawing geometrical objects to Surfaces. If you want to put images on the screen see gamejs/image.
There are several ways to specify colors. Whenever the docs says "valid #RGB string" you can pass in any of the following formats.
Mouse and keyboard events.
All events have a type identifier. This event type is in between the values of NOEVENT and NUMEVENTS. Each event has a constant in
gamejs.event.*All user defined events can have the value of USEREVENT or higher. Make sure your custom event ids* follow this system.
Methods for creating Font objects which can render text to a Surface.
Make synchronous http requests to your game's serverside component.
If you configure a ajax base URL you can make http requests to your server using those functions. The most high-level functions are
Load images as Surfaces.
Sounds & Images are loaded relative to your game's html page (the html which includes the GameJs code) or relative to the property
window.$g.resourceBaseHrefif it is set.
Image masks. Usefull for pixel perfect collision detection.
Playing sounds with the html5 audio tag. Audio files must be preloaded with the usual
gamejs.preload()function. Ogg, wav and webm supported.
Sounds & Images are loaded relative to './'.
A noise generator comparable to Perlin noise, which is useful for generating procedural content.
Spritethe basic building block for any game and
SpriteGroups, which are an efficient way for doing collision detection between groups as well as drawing layered groups of objects on the screen.
Fast pixel access.
Only used by GameJs internally to provide a game loop.
This is a loader for the general purpose tile map editor "Tiled".
This module can load all ".tmx" files even if additionally base64 encoded (can be configured in Tiled).
This module loads the whole map definition, including the TileSets with all necessary images. For an example on how to render a map loaded with this module, see
You will typically create a Map instance with
Map(url)and deal with the layers, tilesets, etc. through the Map instance instead of loading & creating them yourself.
Only orthogonol maps are supported (no isometric maps).
Rotate and scale Surfaces.
Workers are useful to relieve your GameJs application from code which might take long to run. Either expensive algorithms, which might get called every now and then (e.g., path-finding) or another logic being run continously within the rendering loop (e.g., physics engine).
A Worker is like a seperate GameJs application being executed - another
main.jswith its own
gamejs.ready(). The Worker's most important feature is that code executing within it does not block the rendering code. The Worker's greatest limitation is that you can only communicate with it through text messages.
examples/workersdirectory for a running example.
Provides facilities for parsing an xml String.
You will typically get a
gamejs.xml.Documentinstance by loading the data with one of the two static
Document.fromUrl(url). Querying for
children()will return a new
gamejs.xml.Documentmatching your result (or null).
value()to get the data stored in the XML Document.
AStar Path finding algorithm
findRoute(map, from, to, [timeout])function to get the linked list leading
toanother on the given
The map must implement interface
gamejs.pathfinding.Map. This class really holds an example implementation & data for you to study. If you understand what this calls provides, you understand this module.
Optionally, the search is cancelled after
If there is no route
Utility functions for working with Obiects
Base64 encode / decode
Matrix manipulation, used by GameJs itself. You probably do not need this unless you manipulate a Context's transformation matrix yourself.
Utility functions for working with Objects
A seedable random-number generator.
A generator is initialized by GameJs and can be used with the static functions (choose, integer, vector,...).
You can re-initialize this generator with a different seed by calling
gamejs.utils.prng.init(seed)after which the static functions in this module will use the new seed.
Utilies for URI handling.