Skip to content

Thrunium / Reference

Game

The core of Thrunium: a game owns a <canvas>, a 2d context, a game loop and the standard input events. Construct it with new THRUNIUM.Game(width, height), assign your assets, event handlers and states, then call init() and start(). Use it to draw on the canvas, load images and audio, and react to mouse, touch, keyboard and device events.

Constructor

new THRUNIUM.Game(width, height)

  • width number

    Width of the game in pixels.

  • height number

    Height of the game in pixels.

Creates a game: a <canvas> with its 2d context inside a container, the game loop and the standard events. The canvas is appended to the page and scaled to fit the window. Call init() and start() afterwards. Must be called with new.

JavaScript
var game = new THRUNIUM.Game(400, 400);

Variables

width: number

Width of the game in pixels, as passed to the constructor. Read-only: changing it does not resize the canvas.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.width; // 400

height: number

Height of the game in pixels, as passed to the constructor. Read-only: changing it does not resize the canvas.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.height; // 400

scale: number

Density of real canvas pixels per game pixel (device pixel ratio times fitScale). Adjusted automatically to keep the game sharp. Read-only.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.scale;

fitScale: number

How much the game is scaled to fit the browser window. Adjusted automatically when the window is resized. Read-only.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.fitScale;

canvas: HTMLCanvasElement

The game's <canvas> element. All normal canvas properties and methods are available on it.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.canvas.style.cursor = 'pointer';

ctx: CanvasRenderingContext2D

The canvas's 2d rendering context. All normal context methods are available on it. Canvas state such as lineWidth persists between draw calls until changed.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.ctx.globalAlpha = 0.5;

delta: number

Seconds elapsed since the previous frame. Updated every game loop; multiply movement by it to keep speed independent of frame rate.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.states['game'] = function() {
	playerX += 100 * game.delta; // 100 pixels per second
};

fps: number

Frames per second of the game loop. Updated every frame. Read-only: an overwritten value is replaced on the next frame.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.fps;

runSpeed: string | number

Current frames per second divided by 60, as a string with two decimals (for example "1.00"). Use it to correct game speed on devices that run below 60 fps. Updated every frame. Read-only.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.runSpeed;

lastRun: number | null

Unix timestamp in milliseconds of the last time the game loop ran, or null before the first frame. Updated every frame. Read-only.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.lastRun;

gameRunning: boolean

Whether the game loop is running. start() sets it to true and stop() to false; setting it to false yourself ends the loop after the current frame.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.gameRunning = false; // the loop stops

showFPS: boolean

Set to true to draw an FPS meter in the top-left corner. Default is false.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.showFPS = true;

namespace: string

Free-form name for the game. The engine does not use it, so it is available for your own bookkeeping, for example to prefix saved data. Default is an empty string.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.namespace = 'my-game';

mouseX: number

X coordinate of the pointer in game coordinates. Updated when the mouse moves or the screen is touched or clicked. Read-only.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.mouseX;

mouseY: number

Y coordinate of the pointer in game coordinates. Updated when the mouse moves or the screen is touched or clicked. Read-only.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.mouseY;

state: string

Name of the state the game loop is currently running (a key of states). It starts as the built-in loading screen, "startup", and switches to startState once everything has loaded. Assign a new name to change state.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.state = 'game'; // run game.states['game'] each frame

startState: string | null

Name of the state to run after the loading screen. Used once, so set it before init(). Default is null, so you have to set it.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.startState = 'menu';

states: Record<string, () => void>

Your game states: an object of functions keyed by name. The function named by state is called every frame. Change state to switch between them.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.states['game'] = function() {
	// your code, run every frame
};

backgroundColor: string

Background color drawn behind every frame. Accepts any CSS color (hex, rgb, rgba, hsl, hsla). Default is "#fff".

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.backgroundColor = '#eee';

assetsLoaded: boolean

Whether loadAssets() has run, so the entries of assets have been turned into loadable elements. Use loadedAssets to see how many have actually finished loading.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assetsLoaded; // false until init()

loadedAssets: number

Number of requested assets that have finished loading.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.loadedAssets;

assetsToLoad: number

Number of assets requested for loading. Set when loadAssets() runs; the loading screen is done when loadedAssets reaches it.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assetsToLoad;

font: string

Font family used by drawText. Default is "Arial".

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.font = 'Courier New';

textSize: string

Font size used by drawText, as a CSS size. Default is "30px".

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.textSize = '32px';

Events

click: (evt: Event) => void

Called when the user clicks the game, and when a touch ends on it (touchend calls click just before its own handler). It updates mouseX and mouseY first. Assign a function to handle it. Handlers are attached by init(), so assign them before calling it.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.click = function(e) {
	// your code here
};

mousedown: (evt: Event) => void

Called when the user presses a mouse button in the game. It updates mouseX and mouseY first. Assign a function to handle it. Handlers are attached by init(), so assign them before calling it.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.mousedown = function(e) {
	// your code here
};

mouseup: (evt: Event) => void

Called when the user releases a mouse button in the game. It updates mouseX and mouseY first. Assign a function to handle it. Handlers are attached by init(), so assign them before calling it.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.mouseup = function(e) {
	// your code here
};

mousemove: (evt: Event) => void

Called when the pointer moves over the game. It updates mouseX and mouseY first. Assign a function to handle it.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.mousemove = function(e) {
	// your code here
};

touch: (evt: Event) => void

Called when the user touches the game with a touchscreen. It updates mouseX and mouseY first. Assign a function to handle it. The handler is looked up when the event fires, so it can be assigned or changed at any time.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.touch = function(e) {
	// your code here
};

touchstart: (evt: Event) => void

Called when the user first touches the game with a touchscreen, after touch. It updates mouseX and mouseY first. Assign a function to handle it. The handler is looked up when the event fires, so it can be assigned or changed at any time.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.touchstart = function(e) {
	// your code here
};

touchend: (evt: Event) => void

Called when the user lifts the touch from a touchscreen, after click. It updates mouseX and mouseY first. Assign a function to handle it. The handler is looked up when the event fires, so it can be assigned or changed at any time.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.touchend = function(e) {
	// your code here
};

keydown: (evt: Event) => void

Called when the user presses a key. Assign a function to handle it; find key codes at keycode.info. Attached by init(), so assign it first.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.keydown = function(e) {
	// your code here
	console.log(e.keyCode, e.key);
};

keyup: (evt: Event) => void

Called when the user releases a key. Assign a function to handle it. Attached by init(), so assign it first.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.keyup = function(e) {
	// your code here
	console.log(e.keyCode, e.key);
};

deviceorientation: (evt: Event) => void

Called when the orientation of the device changes. Usually only available on phones.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.deviceorientation = function(e) {
	// your code here
};

devicemotion: (evt: Event) => void

Called when the device reports motion (acceleration or rotation). Usually only available on phones. Since 1.1.0 this handler is actually attached; 1.0.0 never called it.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.devicemotion = function(e) {
	// your code here
};

rezise: (evt: Event) => void

Called after the game has refitted itself when the window is resized. The name is misspelled on purpose and is kept for compatibility.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.rezise = function(e) {
	// your code here
};

Assets

assets: unknown[]

Assets to load on startup. Set it before init(). Each entry is either the path of an image or audio file, which is also the key the loaded asset is stored under, or an object { src, index, spriteMap? } with the file path, the key to store it under, and an optional spriteMap of { width, height } that loads an image as a Sprite sprite map. Supported images are jpg, jpeg, png, gif and xbm; audio is mp3, wav, ogg and aac. After init() the array is replaced by an object-like array keyed by each entry's index, holding the loaded element (or Sprite for sprite maps).

JavaScript
var game = new THRUNIUM.Game(400, 400);
// as a string: the path is also the key
game.assets = ['images/sprite.png'];
// the loading screen runs until every asset has loaded, then switches to startState
game.startState = 'game';
game.states['game'] = function() {
	game.drawImage(game.assets['images/sprite.png'], 0, 0);
};
game.init();
game.start();
JavaScript
var game = new THRUNIUM.Game(400, 400);
// as an object: choose the key, optionally load a sprite map
game.assets = [{
	src: 'images/spritemap.png',
	index: 'tiles',
	spriteMap: { width: 40, height: 40 }
}];
game.startState = 'game';
game.states['game'] = function() {
	// Sprite.draw(frame, x, y): frame 0 centred at (50, 50)
	game.assets['tiles'].draw(0, 50, 50);
};
game.init();
game.start();

loadAssets()

Creates an Image or Audio element for every entry of assets and starts loading it, then replaces assets with an object-like array keyed by each entry's index. init() calls it for you, so you rarely need it directly. Unknown entries and file types are logged to the console and skipped.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = ['sounds/jump.wav'];
game.loadAssets();
game.assets['sounds/jump.wav'].play();

Drawing

setFillColor(color)

  • color string

    Any CSS color: hex, rgb, rgba, hsl, hsla or a name.

Sets the fill color used by fill() and drawText.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.setFillColor('#f00');
game.drawRect(10, 10, 50, 50);
game.fill();

fill()

Fills the current path (for example from drawRect or drawCircle) with the fill color.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.setFillColor('#0a0');
game.drawCircle(100, 100, 20);
game.fill();

setStrokeColor(color)

  • color string

    Any CSS color: hex, rgb, rgba, hsl, hsla or a name.

Sets the stroke color used by stroke() and drawLine.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.setStrokeColor('#00f');
game.drawLine(0, 0, 100, 100);

stroke()

Strokes the outline of the current path with the stroke color.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.setStrokeColor('#000');
game.drawRect(10, 10, 50, 50);
game.stroke();

drawLine(x1, y1, x2, y2, width?)

  • x1 number

    X coordinate of the start.

  • y1 number

    Y coordinate of the start.

  • x2 number

    X coordinate of the end.

  • y2 number

    Y coordinate of the end.

  • width numberoptional

    Line width in pixels. Leave out to keep the current width.

Draws a line from (x1, y1) to (x2, y2) with the stroke color. When width is given it sets the canvas lineWidth; like all canvas state it then persists for later strokes until changed (since 1.1.0; 1.0.0 ignored width).

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.drawLine(100, 100, 200, 200);
game.drawLine(100, 200, 200, 100, 4); // 4 pixels wide, and later lines stay 4 wide

drawText(text, x, y)

  • text string

    The text to draw.

  • x number

    X coordinate of the text.

  • y number

    Y coordinate of the text.

Draws text at (x, y) using font and textSize and the fill color.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.drawText('Hello World!', 100, 100);

drawRect(x, y, width, height, radius?)

  • x number

    X coordinate of the left edge.

  • y number

    Y coordinate of the top edge.

  • width number

    Width of the rectangle.

  • height number

    Height of the rectangle.

  • radius numberoptional

    Corner radius. Leave out for square corners.

Traces a rectangle path, with rounded corners if radius is given. It only builds the path: call fill() or stroke() afterwards to draw it.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.drawRect(100, 100, 20, 20);
game.fill();

drawCircle(x, y, radius)

  • x number

    X coordinate of the center.

  • y number

    Y coordinate of the center.

  • radius number

    Radius of the circle.

Traces a circle path. It only builds the path: call fill() or stroke() afterwards to draw it.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.drawCircle(100, 100, 20);
game.fill();

drawImage(img, x, y, width?, height?)

  • img CanvasImageSource

    The image to draw, for example a loaded entry of assets.

  • x number

    X coordinate of the left edge.

  • y number

    Y coordinate of the top edge.

  • width number | nulloptional

    Width to draw at. Needs height as well; leave out for the natural size.

  • height number | nulloptional

    Height to draw at. Needs width as well; leave out for the natural size.

Draws an image at (x, y), scaled to width by height when both are given.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = ['image.png'];
game.init();
game.drawImage(game.assets['image.png'], 100, 100);
game.drawImage(game.assets['image.png'], 100, 100, 200, 200);

General

getMouseCords(evt)

  • evt Event

    The mouse or touch event to read the position from.

Updates mouseX and mouseY (in game coordinates) from a mouse or touch event. The engine calls it for you before the mouse and touch handlers run, so you only need it to read the position of an event you handle yourself. Since 1.1.0 it uses this game instead of a global variable named game, and handles touch-end events that have no touches.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.canvas.addEventListener('pointerdown', function(e) {
	game.getMouseCords(e);
	console.log(game.mouseX, game.mouseY);
});

contruct()

Creates the container and canvas and hooks the window resize event. The constructor calls it, so you do not need to. The name is misspelled on purpose and is kept for compatibility.

JavaScript
var game = new THRUNIUM.Game(400, 400);
// the constructor has already called game.contruct()

init()

Initializes the game: loads assets, fits the canvas to the window and attaches the event handlers as they are at this moment. Assign your handlers and assets before calling it, and call it before start().

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.startState = 'menu';
game.init();

start()

Starts the game loop. Each frame it clears the canvas, fills the background and runs the current state.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.init();
game.start();

stop()

Stops the game loop. Call start() to run it again.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.stop();