-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
111 lines (88 loc) · 2.93 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import domready from "domready";
import Game from "./game.js";
import pkg from "./package.json";
console.log("Super Metronome Hero v" + pkg.version);
domready(() => {
const game = new Game();
const engine = game.engine;
engine.screen.canvas.style.position = "absolute";
// Work around Firefox not supporting image-rendering: pixelated
// See https://github.com/excaliburjs/Excalibur/issues/1676
if (engine.canvas.style.imageRendering === "") {
engine.canvas.style.imageRendering = "crisp-edges";
}
const scale = (): void => {
const scaleFactor = Math.floor(
Math.min(window.innerWidth / game.width, window.innerHeight / game.height)
);
const scaledWidth = game.width * scaleFactor;
const scaledHeight = game.height * scaleFactor;
engine.screen.viewport = {width: scaledWidth, height: scaledHeight};
engine.screen.applyResolutionAndViewport();
engine.screen.canvas.tabIndex = 0;
engine.screen.canvas.style.left = `${Math.floor(
(window.innerWidth - scaledWidth) * 0.5
)}px`;
engine.screen.canvas.style.top = `${Math.floor(
(window.innerHeight - scaledHeight) * 0.5
)}px`;
};
const onKey = (event: KeyboardEvent): void => {
engine.screen.canvas.focus();
switch (event.code) {
case "ArrowUp":
case "ArrowDown":
case "ArrowLeft":
case "ArrowRight":
case "KeyX":
case "Space":
case "Enter":
case "NumpadEnter":
event.preventDefault();
}
};
let clicked = false;
const onClick = (): void => {
clicked = true;
hidePointer();
game.active = true;
};
let pointerTimeout: number | null = null;
const onMouseMove = (): void => {
showPointer();
if (pointerTimeout != null) {
clearTimeout(pointerTimeout);
}
pointerTimeout = window.setTimeout(() => {
if (game.active) {
hidePointer();
}
}, 500);
};
const onFocus = (): void => {
if (clicked) {
hidePointer();
game.active = true;
}
};
const onBlur = (): void => {
showPointer();
game.active = false;
};
const hidePointer = (): void => {
engine.canvas.style.cursor = "none";
};
const showPointer = (): void => {
engine.canvas.style.cursor = "auto";
};
scale();
window.addEventListener("resize", scale);
window.addEventListener("keydown", onKey);
window.addEventListener("keypress", onKey);
window.addEventListener("keyup", onKey);
window.addEventListener("click", onClick, true);
window.addEventListener("mousemove", onMouseMove, true);
window.addEventListener("focus", onFocus, true);
window.addEventListener("blur", onBlur, true);
game.start();
});