aboutsummaryrefslogtreecommitdiff
path: root/js/game.js
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-12-29 15:44:42 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-12-29 15:44:42 +0000
commit0a24e427bd19953d1f1675ff22ae80bec58f3ac0 (patch)
treefe764051388ade8e9715fc726c6ed95e3c52e964 /js/game.js
parent49d733c84dc142ca34aa4bec3a6f66203018d822 (diff)
downloadjs-canvas-2023-0a24e427bd19953d1f1675ff22ae80bec58f3ac0.tar.gz
js-canvas-2023-0a24e427bd19953d1f1675ff22ae80bec58f3ac0.zip
Move utility consts to the base class
Diffstat (limited to 'js/game.js')
-rw-r--r--js/game.js18
1 files changed, 11 insertions, 7 deletions
diff --git a/js/game.js b/js/game.js
index b45e905..069618c 100644
--- a/js/game.js
+++ b/js/game.js
@@ -1,3 +1,7 @@
+const floor = Math.floor;
+const max = Math.max;
+const min = Math.min;
+
class Game {
constructor(canvas, width, height) {
this.canvas = canvas;
@@ -79,14 +83,14 @@ class Game {
_draw() {}
resize() {
- const scale = Math.floor(window.innerHeight / this.canvas.height);
+ const scale = floor(window.innerHeight / this.canvas.height);
this.canvas.style["imageRendering"] = "pixelated";
this.canvas.style["transformOrigin"] = "top";
this.canvas.style["transform"] = `scale(${scale})`;
}
loop(now) {
- let dt = Math.min(1 / this.minFps, now - this.then);
+ let dt = min(1 / this.minFps, now - this.then);
this._update(dt);
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
@@ -165,16 +169,16 @@ class Game {
this.ctx.font = "8px monospace";
this.ctx.fillText(
"ERROR Loading Resources",
- Math.floor(this.ctx.canvas.width * 0.1),
- Math.floor(this.ctx.canvas.height / 2 - h - h / 2)
+ floor(this.ctx.canvas.width * 0.1),
+ floor(this.ctx.canvas.height / 2 - h - h / 2)
);
}
this.ctx.fillStyle = "rgb(128, 128, 128)";
- this.ctx.fillRect(Math.floor(this.ctx.canvas.width * 0.1), Math.floor(this.ctx.canvas.height / 2) - h, this.ctx.canvas.width - Math.floor(this.ctx.canvas.width * 0.2), h);
+ this.ctx.fillRect(floor(this.ctx.canvas.width * 0.1), floor(this.ctx.canvas.height / 2) - h, this.ctx.canvas.width - floor(this.ctx.canvas.width * 0.2), h);
this.ctx.fillStyle = "rgb(255, 255, 255)";
- this.ctx.fillRect(Math.floor(this.ctx.canvas.width * 0.1), Math.floor(this.ctx.canvas.height / 2) - h, (
- Math.floor(this.ctx.canvas.resSize * (this.ctx.canvas.width - Math.floor(this.ctx.canvas.width * 0.2)) / this.ctx.canvas.dataSize)
+ this.ctx.fillRect(floor(this.ctx.canvas.width * 0.1), floor(this.ctx.canvas.height / 2) - h, (
+ floor(this.ctx.canvas.resSize * (this.ctx.canvas.width - floor(this.ctx.canvas.width * 0.2)) / this.ctx.canvas.dataSize)
), h);
// we don't do this on update because we want