Add and return a bottom bar for UI components. If a bottom bar already exists, returns the existing one.
Syntactic sugar for addHandler('click',h).
A function to handle click events
ID that can be used to remove handler with removeClickHandler
let xpos = 100;
let ypos = 100;
// Register a handler to update our variable each time
// there is a click.
game.addClickHandler(
function ({x,y}) {
// set variables...
xpos = x;
ypos = y;
}
)
// Now create a drawing that uses the variable we set.
game.addDrawing(
function ({ctx}) {ctx.fillRect(xpos,ypos,30,30)}
)
Add a drawing to our drawing queue (it will remain until we remove it).
draw function OR an object with a draw callback method
ID that can be used in removeDrawing callback to remove drawing.
game.addDrawing(
function ({ctx,elapsed}) {
ctx.beginPath();
ctx.moveTo(200,200);
ctx.lineTo(100,200+Math.sin(elapsed/10)*200);
ctx.stroke();
}
);
game.addDrawing(
{ x : 0,
y : 0,
w : 100,
h : 100,
draw ({ctx,stepTime,width,height}) {
this.x += stepTime/20;
this.y += stepTime/20;
if (this.x > width) { this.x = 0 }
if (this.y > height) { this.y = 0 }
ctx.fillRect(this.x,this.y,this.w,this.h)
},
}
);
Register a handler h for eventType
type of event to handle
handler function
ID that can be used to remove handler with removeHandler
Register a handler h for eventType
type of event to handle
handler function
ID that can be used to remove handler with removeHandler
Register a handler h for eventType
type of event to handle
handler function
ID that can be used to remove handler with removeHandler
Register a handler h for resize
Add and return a top bar for UI components. If a top bar already exists, returns the existing one.
Destroy the interface and clean up DOM elements
Show a simple dialog with a message
Optionalmessage: stringOptionalonClose: () => voidGet the bottom bar if it exists
Get the main container element
Get the current game state
Check if the game is currently running.
Whether the game is running
Get current canvas size
Get the top bar if it exists
Pause the game
Remove the bottom bar
Syntactic sugar for removeHandler('click',h)
Remove a drawing by its ID.
drawing ID to remove (return value from addDrawing).
Remove handler for eventType.
Syntactic sugar for removeHandler('resize',h)
Remove the top bar
Replace a drawing by id
Reset the game (alias for stop)
Restore a previously removed drawing (start drawing again).
drawing ID to restore (start drawing again).
Resume the game
Start the game (override parent to track state)
Stop the game completely
GameInterface extends GameCanvas functionality with UI components.
This class provides a complete game interface with a top bar, canvas, and bottom bar. You can use it to add UI controls such as buttons, sliders, and number inputs to your game layout.
Example: Add a top bar with a title
See also: