1
0
Fork 0
Browse Source

refactor: move variables to global scope to rAF them

Signed-off-by: André Jaenisch <andre.jaenisch@posteo.de>
combat-scorched-earth-from-outer-space
André Jaenisch 11 months ago
parent
commit
59586dc468
Signed by: ryuno-ki
GPG Key ID: 5A668E771F1ED854
  1. 24
      src/js/app.js
  2. 1
      types/app.d.ts

24
src/js/app.js

@ -4,6 +4,12 @@ import { Vec2 } from './vector.js'
/** @typedef {import('./shape').Shape} Shape */
// Global on purpose
/** @type {Shape} */
let astronaut
/** @type {CanvasRenderingContext2D | null} */
let context
/**
* Entry point into the game.
*
@ -16,15 +22,15 @@ export function app () {
return
}
const context = /** @type {HTMLCanvasElement} */(game).getContext('2d')
context = /** @type {HTMLCanvasElement} */(game).getContext('2d')
if (!context) {
console.error('Could not start game!')
return
}
const astronaut = makeAstronaut()
drawShape(context, astronaut)
astronaut = makeAstronaut()
tick()
}
/**
@ -53,3 +59,15 @@ function makeAstronaut () {
return shape
}
/* TODO: Add some paint-FPS to UI */
// See https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame#notes
function tick () {
// Impossible state
if (!context) {
return
}
drawShape(context, astronaut)
window.requestAnimationFrame(tick)
}

1
types/app.d.ts vendored

@ -1,4 +1,3 @@
/** @typedef {import('./shape').Shape} Shape */
/**
* Entry point into the game.
*

Loading…
Cancel
Save