Skip to content

Thrunium / Reference

Elem

HTML elements on top of the game canvas, such as buttons and text fields. They are positioned in game coordinates and use the game's font. Create the THRUNIUM.Game before creating elements, because they are added to the game's container.

Elem

new THRUNIUM.Elem.Elem(tag, x, y)

  • tag string

    Tag name of the element to create, such as 'div' or 'button'.

  • x number

    X coordinate of the element's top-left corner, in game coordinates.

  • y number

    Y coordinate of the element's top-left corner, in game coordinates.

Creates an HTML element with the tag tag and places it on the game at (x, y). It starts with a white background, black text, the game's font and rounded corners, and is added to THRUNIUM.Elem.elemsList. Create the THRUNIUM.Game first, because the element is added to the game's container. Must be called with new.

JavaScript
var game = new THRUNIUM.Game(400, 400);
var label = new THRUNIUM.Elem.Elem('div', 20, 20);
label.elem.textContent = 'Score: 0';

x: number

X coordinate of the element's top-left corner, in game coordinates. Read-only: use setPos() to move the element.

JavaScript
var game = new THRUNIUM.Game(400, 400);
var button = THRUNIUM.Elem.Button('Play', 100, 100);
button.x; // 100

y: number

Y coordinate of the element's top-left corner, in game coordinates. Read-only: use setPos() to move the element.

JavaScript
var game = new THRUNIUM.Game(400, 400);
var button = THRUNIUM.Elem.Button('Play', 100, 100);
button.y; // 100

elem: HTMLElement

The HTML element itself. Use it for anything the wrapper does not cover, such as its text.

JavaScript
var game = new THRUNIUM.Game(400, 400);
var label = new THRUNIUM.Elem.Elem('div', 20, 20);
label.elem.textContent = 'Score: 0';

style: Record<string, string>

The element's styles, as CSS property names in camelCase (backgroundColor) mapped to values. Change them with setStyle(), which also applies them; changing this object directly does not update the element.

JavaScript
var game = new THRUNIUM.Game(400, 400);
var button = THRUNIUM.Elem.Button('Play', 100, 100);
button.style.backgroundColor; // '#fff'

hoverStyle: Record<string, string>

The styles applied while the mouse is over the element, in the same form as style. It follows style, except for properties set with setHoverStyle(). Change it with setHoverStyle().

JavaScript
var game = new THRUNIUM.Game(400, 400);
var button = THRUNIUM.Elem.Button('Play', 100, 100);
button.setHoverStyle('backgroundColor', '#ddd');
button.hoverStyle.backgroundColor; // '#ddd'

setPos(x, y)

  • x number

    The new x coordinate.

  • y number

    The new y coordinate.

Moves the element so its top-left corner is at (x, y) in game coordinates.

JavaScript
var game = new THRUNIUM.Game(400, 400);
var button = THRUNIUM.Elem.Button('Play', 100, 100);
button.setPos(150, 200);

resize()

Re-applies the element's position and scale for the game's current fitScale. The game calls it for every element in THRUNIUM.Elem.elemsList when the window is resized, so you rarely need it. Since 1.1.0 it no longer leaks a global variable or throws in strict mode.

JavaScript
var game = new THRUNIUM.Game(400, 400);
var button = THRUNIUM.Elem.Button('Play', 100, 100);
button.resize();

addEventListener(type, fun, async?)

  • type string

    Name of the event, such as 'click'.

  • fun EventListenerOrEventListenerObject

    The function to call when the event happens; it gets the event.

  • async booleanoptional

    Passed on as the third argument of the DOM addEventListener (useCapture). Despite its name it has nothing to do with asynchronous code; leave it out.

Listens for a DOM event on the element, like the element's own addEventListener.

JavaScript
var game = new THRUNIUM.Game(400, 400);
var button = THRUNIUM.Elem.Button('Press me!', 100, 100);
button.addEventListener('click', function(e) {
	// button was clicked
});

updateStyle(styleList)

  • styleList Record<string, string>

    CSS property names in camelCase mapped to values.

Applies a set of styles to the element without storing them in style or hoverStyle. The element uses it internally to switch between style and hoverStyle; to change a style for good, use setStyle().

JavaScript
var game = new THRUNIUM.Game(400, 400);
var button = THRUNIUM.Elem.Button('Play', 100, 100);
button.updateStyle({ color: '#f00', fontSize: '20px' });

setStyle(name, value)

  • name string

    CSS property name in camelCase, such as 'backgroundColor'.

  • value string

    The CSS value, such as '#000'.

Sets one style of the element and applies it. Unless you have set a hover style for the same property with setHoverStyle(), the element keeps it while hovered too.

JavaScript
var game = new THRUNIUM.Game(400, 400);
var button = THRUNIUM.Elem.Button('Play', 100, 100);
button.setStyle('backgroundColor', '#000');
button.setStyle('color', '#fff');

setHoverStyle(name, value)

  • name string

    CSS property name in camelCase, such as 'backgroundColor'.

  • value string

    The CSS value to use while hovered.

Sets a style that applies only while the mouse is over the element; the normal style comes back when the mouse leaves. Since 1.1.0 hover styles work as described; 1.0.0 applied them the wrong way round.

JavaScript
var game = new THRUNIUM.Game(400, 400);
var button = THRUNIUM.Elem.Button('Play', 100, 100);
button.setHoverStyle('backgroundColor', '#ddd');

show()

Shows the element again after hide().

JavaScript
var game = new THRUNIUM.Game(400, 400);
var button = THRUNIUM.Elem.Button('Play', 100, 100);
button.hide();
button.show();

hide()

Hides the element. It stays in place, so show() brings it back.

JavaScript
var game = new THRUNIUM.Game(400, 400);
var button = THRUNIUM.Elem.Button('Play', 100, 100);
button.addEventListener('click', function() {
	button.hide();
});

General

elemsList: Elem.Elem[]

Every Elem.Elem created so far, including buttons, in creation order. The game uses it to reposition them when the window is resized. Inputs from Input() are not in it.

JavaScript
var game = new THRUNIUM.Game(400, 400);
THRUNIUM.Elem.Button('Play', 100, 100);
THRUNIUM.Elem.elemsList.length; // 1

Button(txt, x, y)

  • txt string

    The button's text.

  • x number

    X coordinate of the button's top-left corner, in game coordinates.

  • y number

    Y coordinate of the button's top-left corner, in game coordinates.

returns Elem.Elem

Creates a button with the text txt and places it on the game at (x, y). Returns an Elem.Elem, so all of its methods work on the button. Create the THRUNIUM.Game first. Call it without new.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.init();
var button = THRUNIUM.Elem.Button('Press me!', 100, 100);
button.setStyle('backgroundColor', '#000');
button.setStyle('color', '#fff');
button.addEventListener('click', function(e) {
	// button was clicked
});

Input(x, y, options)

  • x number

    Horizontal position of the field (see above).

  • y number

    Vertical position of the field (see above).

  • options { type?: string; placeholder?: string }

    Settings for the field.

  • options.type stringoptional

    The input's type, such as 'text', 'password' or 'number'. Default is 'text'.

  • options.placeholder stringoptional

    Hint text shown while the field is empty.

returns HTMLInputElement

Creates an <input> field on top of the game and returns it; read what the player typed from its value. It uses the game's font and textSize. Unlike Elem.Elem, it is a plain HTML element: it is not added to elemsList and does not move when the window is resized. It is placed x * game.scale * game.fitScale pixels from the left and y * game.scale * game.fitScale from the top of the game, so it matches game coordinates only when both scales are 1. Since 1.1.0 it creates an <input>; 1.0.0 created a <button>. Create the THRUNIUM.Game first. Call it without new.

JavaScript
var game = new THRUNIUM.Game(400, 400);
var nameField = THRUNIUM.Elem.Input(100, 100, { placeholder: 'Your name' });
nameField.addEventListener('change', function() {
	console.log(nameField.value);
});