Game loop
This commit is contained in:
parent
3abb79af83
commit
ec9a4c24fc
18
src/game.js
18
src/game.js
@ -2,6 +2,24 @@ export default class Game {
|
||||
constructor(canvas) {
|
||||
this.canvas = canvas
|
||||
this.ctx = canvas.getContext("2d")
|
||||
this.timestamp = 0
|
||||
}
|
||||
|
||||
start() {
|
||||
requestAnimationFrame(this.loop.bind(this))
|
||||
}
|
||||
|
||||
loop(timestamp) {
|
||||
const dt = timestamp - this.timestamp
|
||||
this.timestamp = timestamp
|
||||
this.tick(dt)
|
||||
this.draw()
|
||||
|
||||
requestAnimationFrame(this.loop.bind(this))
|
||||
}
|
||||
|
||||
tick(dt) {
|
||||
console.log(dt)
|
||||
}
|
||||
|
||||
draw() {
|
||||
|
@ -4,5 +4,6 @@ document.addEventListener("DOMContentLoaded", e => {
|
||||
console.log("Hello, world!")
|
||||
const canvas = document.getElementById("game-canvas")
|
||||
const game = new Game(canvas)
|
||||
game.draw()
|
||||
|
||||
game.start()
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user