Class: Game

Game(canvas, tempCanvas)

Game class. It manages the game state and elements within it.

Constructor

new Game(canvas, tempCanvas)

Constructor of the Game class

Parameters:
Name Type Description
canvas HTMLCanvasElement

Canvas on which the game is played

tempCanvas HTMLCanvasElement

(optional) Canvas used for click detection of elements

Properties:
Name Type Description
elements Array.<GameElement>

Array of GameElement objects

grids Array.<GameGrid>

Array of grids

selectedElement GameElement

Element that is being dragged or held

selectedDelta Point

Distance from mouse to center of dragged element

pressedKeys Array.<string>

Array of currently pressed keys

shared Object

Last recorded mouse position and temporary context shared with elements

canvas HTMLCanvasElement

HTML canvas on which the game is played

context CanvasRenderingContext2D

Rendering context for the canvas

onClear Array.<function()>

What happens on clear() in addition to removing elements

onClick Array.<function()>

Array of functions triggered on click

onMove Array.<function()>

Array of functions triggered on drag

onMouseUp Array.<function()>

Array of functions triggered on finish dragging

Source:

Methods

addElement(element, sort)

Adds element to the array of elements

Parameters:
Name Type Default Description
element GameElement

Added element

sort boolean true

Passing false improves the speed of adding elements, but requires later sorting (by level) for correct display

Source:

addElements(…elements)

Adds multiple elements to game

Parameters:
Name Type Attributes Description
elements GameElement <repeatable>
Source:

addOnClearListener(callback)

Additional functions to call on clear

Parameters:
Name Type Description
callback

function to call on clear

Source:

addOnMouseDownListener(callback)

Adds a listener to the array onClickCallbacks

Parameters:
Name Type Description
callback function

function to be called

Source:

addOnMouseMoveListener(callback)

Adds a listener to the array of listeners for onDrag

Parameters:
Name Type Description
callback function

function to be called

Source:

addOnMouseUpListener(callback)

Adds a listener to the array of listeners for onFinishDragging

Parameters:
Name Type Description
callback function

function to be called

Source:

animate()

Starts the animation loop

Source:

changeLevelOfElement(element, newLevel)

Changes the level of element and sorts elements by level

Parameters:
Name Type Description
element GameElement

Element with level to be changed

newLevel number

New level value of the element

Source:

checkCollisions(element) → {Array.<GameElement>}

Returns an array of elements that collide with input element

Parameters:
Name Type Description
element GameElement

Element for which the collisions are checked

Source:
Returns:

Array of elements that collide with input element

Type
Array.<GameElement>

clear()

Resets game to initial state

Source:

copyElement(element, newName) → {GameElement|GameCanvas|GameButton|GameComposite|GameTextInput}

Calls the copy function of element and adds it to game

Parameters:
Name Type Description
element GameElement | GameCanvas | GameButton | GameComposite | GameTextInput

Element to copy

newName string

Name of the newly created copy

Source:
Returns:

New instance

Type
GameElement | GameCanvas | GameButton | GameComposite | GameTextInput

createButton(attrs) → {GameButton}

Creates, adds and returns a button element

Parameters:
Name Type Description
attrs Object

Element attributes

Source:
Returns:

New instance

Type
GameButton

createCanvas(attrs) → {GameCanvas}

Creates, adds and returns a canvas element

Parameters:
Name Type Description
attrs Object

Element attributes

Source:
Returns:

New instance

Type
GameCanvas

createComposite(attrs) → {GameComposite}

Creates, adds and returns a blank composite object at (0,0)

Parameters:
Name Type Description
attrs Object

Element attributes

Source:
Returns:

New instance

Type
GameComposite

createElement(attrs) → {GameElement}

Creates, adds and returns a blank element

Parameters:
Name Type Description
attrs Object

Element attributes

Source:
Returns:

New instance

Type
GameElement

createGrid(dx, dy, width, height, cols, rows) → {GameGrid}

Creates,adds and returns a new grid instance

Parameters:
Name Type Default Description
dx number 0

Deviation from right

dy number 0

Deviation from top

width number

Width of the grid

height number

Height of the grid

cols number

Number of columns

rows number

Number of rows

Source:
Returns:

New instance

Type
GameGrid

createRangeSlider(attrs) → {GameRangeSlider}

Creates, adds and returns a range slider element

Parameters:
Name Type Description
attrs Object

Element attributes

Source:
Returns:

New instance

Type
GameRangeSlider

createTextInput(attrs) → {GameTextInput}

Creates, adds and returns a text input element

Parameters:
Name Type Description
attrs Object

Element attributes

Source:
Returns:

New instance

Type
GameTextInput

getCenter() → {Point}

Returns center of the game canvas

Source:
Returns:

center of canvas

Type
Point

getElementAtPos(position) → {null|GameElement}

Returns element on position or null

Parameters:
Name Type Description
position Point

Searched position

Source:
Returns:

Element at position or null

Type
null | GameElement

getElementByName(name) → {GameElement}

Returns element with matching name or throws an error

Parameters:
Name Type Description
name string

Name of searched element

Source:
Returns:

Found element

Type
GameElement

getMousePos(event) → {Point}

Sets current mouse position in the shared object and returns it

Parameters:
Name Type Description
event MouseEvent | TouchEvent

Mouse event passed

Source:
Returns:

Current mouse position

Type
Point

highestLevel() → {number}

Returns the highest current level value

Source:
Returns:

Currently highest level

Type
number

lowestLevel() → {number}

Returns the lowest current level value

Source:
Returns:

Currently lowest level

Type
number

moveElementToBottom(element)

Moves element to bottom

Parameters:
Name Type Description
element GameElement
Source:

moveElementToTop(element)

Moves element to top

Parameters:
Name Type Description
element GameElement
Source:

moveToTopWhenDragging(elementArray)

Keeps dragged element on top of group Adds or replaces the listener for starting dragging

Parameters:
Name Type Description
elementArray Array.<GameElement>

Array of elements in a group

Source:

popElementByName(name) → {GameElement}

Removes element from elements array and returns it, or throws an error

Parameters:
Name Type Description
name string

Name of element to be removed

Source:
Returns:

Removed element

Type
GameElement

removeElement(element)

Removes element from elements array

Parameters:
Name Type Description
element GameElement

Element instance

Source:

removeElements(…elements)

Removes multiple elements from elements array

Parameters:
Name Type Attributes Description
elements GameElement <repeatable>

Element instances

Source:

removeOnMouseDownListener(callback)

Removes listener for the onClick event

Parameters:
Name Type Description
callback function

function you want to remove

Source:

removeOnMouseMoveListener(callback)

Removes listener for the onDrag event

Parameters:
Name Type Description
callback function

function you want to remove

Source:

removeOnMouseUpListener(callback)

Removes listener for the onFinishDragging event

Parameters:
Name Type Description
callback function

function you want to remove

Source:

screenShot()

Downloads a screenshot of the game area

Source:

stopAnimation()

Stops the animation loop

Source:

updateLevels()

Sorts array of elements by level

Source: