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 |
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 |
addElements(…elements)
Adds multiple elements to game
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
elements |
GameElement |
<repeatable> |
addOnClearListener(callback)
Additional functions to call on clear
Parameters:
Name | Type | Description |
---|---|---|
callback |
function to call on clear |
addOnMouseDownListener(callback)
Adds a listener to the array onClickCallbacks
Parameters:
Name | Type | Description |
---|---|---|
callback |
function | function to be called |
addOnMouseMoveListener(callback)
Adds a listener to the array of listeners for onDrag
Parameters:
Name | Type | Description |
---|---|---|
callback |
function | function to be called |
addOnMouseUpListener(callback)
Adds a listener to the array of listeners for onFinishDragging
Parameters:
Name | Type | Description |
---|---|---|
callback |
function | function to be called |
animate()
Starts the animation loop
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 |
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 |
Returns:
Array of elements that collide with input element
- Type
- Array.<GameElement>
clear()
Resets game to initial state
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 |
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 |
Returns:
New instance
- Type
- GameButton
createCanvas(attrs) → {GameCanvas}
Creates, adds and returns a canvas element
Parameters:
Name | Type | Description |
---|---|---|
attrs |
Object | Element attributes |
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 |
Returns:
New instance
- Type
- GameComposite
createElement(attrs) → {GameElement}
Creates, adds and returns a blank element
Parameters:
Name | Type | Description |
---|---|---|
attrs |
Object | Element attributes |
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 |
Returns:
New instance
- Type
- GameGrid
createRangeSlider(attrs) → {GameRangeSlider}
Creates, adds and returns a range slider element
Parameters:
Name | Type | Description |
---|---|---|
attrs |
Object | Element attributes |
Returns:
New instance
- Type
- GameRangeSlider
createTextInput(attrs) → {GameTextInput}
Creates, adds and returns a text input element
Parameters:
Name | Type | Description |
---|---|---|
attrs |
Object | Element attributes |
Returns:
New instance
- Type
- GameTextInput
getCenter() → {Point}
Returns center of the game canvas
Returns:
center of canvas
- Type
- Point
getElementAtPos(position) → {null|GameElement}
Returns element on position or null
Parameters:
Name | Type | Description |
---|---|---|
position |
Point | Searched position |
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 |
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 |
Returns:
Current mouse position
- Type
- Point
highestLevel() → {number}
Returns the highest current level value
Returns:
Currently highest level
- Type
- number
lowestLevel() → {number}
Returns the lowest current level value
Returns:
Currently lowest level
- Type
- number
moveElementToBottom(element)
Moves element to bottom
Parameters:
Name | Type | Description |
---|---|---|
element |
GameElement |
moveElementToTop(element)
Moves element to top
Parameters:
Name | Type | Description |
---|---|---|
element |
GameElement |
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 |
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 |
Returns:
Removed element
- Type
- GameElement
removeElement(element)
Removes element from elements array
Parameters:
Name | Type | Description |
---|---|---|
element |
GameElement | Element instance |
removeElements(…elements)
Removes multiple elements from elements array
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
elements |
GameElement |
<repeatable> |
Element instances |
removeOnMouseDownListener(callback)
Removes listener for the onClick event
Parameters:
Name | Type | Description |
---|---|---|
callback |
function | function you want to remove |
removeOnMouseMoveListener(callback)
Removes listener for the onDrag event
Parameters:
Name | Type | Description |
---|---|---|
callback |
function | function you want to remove |
removeOnMouseUpListener(callback)
Removes listener for the onFinishDragging event
Parameters:
Name | Type | Description |
---|---|---|
callback |
function | function you want to remove |
screenShot()
Downloads a screenshot of the game area
stopAnimation()
Stops the animation loop
updateLevels()
Sorts array of elements by level