Creates a new GameCanvas instance.
The Canvas Element OR the ID of the canvas element we will render the game in.
Optional configuration object
Optionalautoresize?: booleanWhether to resize the game canvas to the DOM canvas automatically (defaults to true)
Optionalsize?: SizeOptional size for canvas. Otherwise size is taken from explicitly set width/height OR from the element's size on the page.
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
Check if the game is currently running.
Whether the game is running
Get current canvas size
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)
Replace a drawing by id
Restore a previously removed drawing (start drawing again).
drawing ID to restore (start drawing again).
GameCanvas sets up a canvas for creating a simple game in.
Hand it the id of the canvas you want to use from your HTML document.
It returns an object for registering functions to do the drawing and for registering callbacks to handle events on the canvas.
Example: <caption>Make a simple spinner using only the addDrawing method.</caption>
Example: <caption>Create a game where clicking makes balls drop.</caption>
Memberof
SimpleCanvas