Modules: GameJs Reference
-
gamejs
This module holds the essential
Your game should provide callbacks forRectandSurfaceclasses 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 togamejs.onTick. -
gamejs/callback
-
gamejs/display
Methods to create, access and manipulate the display Surface.
Flags
gamejs.display.setMode()understands three flags:- gamejs.display.FULLSCREEN
- gamejs.display.DISABLE_SMOOTHING
- 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
Fullscreen mode
When
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
- space
- shift, control, alt
- page up, page down
- home, end, tab, meta
-
gamejs/draw
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.
-
gamejs/event
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. -
gamejs/font
Methods for creating Font objects which can render text to a Surface.
-
gamejs/http
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()andsave()which take and return a JavaScript object, which they will send to / recieve from the server-side in JSON format. -
gamejs/image
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. -
gamejs/mask
Image masks. Usefull for pixel perfect collision detection.
-
gamejs/mixer
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 './'.
-
gamejs/noise
A noise generator comparable to Perlin noise, which is useful for generating procedural content.
-
gamejs/sprite
Provides
Spritethe basic building block for any game andSpriteGroups, which are an efficient way for doing collision detection between groups as well as drawing layered groups of objects on the screen. -
gamejs/surfacearray
Fast pixel access.
-
gamejs/time
Only used by GameJs internally to provide a game loop.
-
gamejs/tmx
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
examples/tiledmap.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).
-
gamejs/transform
Rotate and scale Surfaces.
-
gamejs/worker
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 owngamejs.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.See the
examples/workersdirectory for a running example. -
gamejs/xml
Provides facilities for parsing an xml String.
You will typically get a
gamejs.xml.Documentinstance by loading the data with one of the two staticDocument.fromString(string)orDocument.fromUrl(url). Querying forelements(name)orchildren()will return a newgamejs.xml.Documentmatching your result (or null).Use
attributes(name)andvalue()to get the data stored in the XML Document. -
gamejs/pathfinding/astar
AStar Path finding algorithm
Use the
findRoute(map, from, to, [timeout])function to get the linked list leadingfroma pointtoanother on the givenmap.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
timeoutin millseconds.If there is no route
nullis returned. -
gamejs/utils/arrays
Utility functions for working with Obiects
-
gamejs/utils/base64
Base64 encode / decode
-
gamejs/utils/binaryheap
-
gamejs/utils/math
-
gamejs/utils/matrix
Matrix manipulation, used by GameJs itself. You probably do not need this unless you manipulate a Context's transformation matrix yourself.
-
gamejs/utils/objects
Utility functions for working with Objects
-
gamejs/utils/prng
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. -
gamejs/utils/strings
-
gamejs/utils/uri
Utilies for URI handling.
-
gamejs/utils/vectors