54 lines
1.3 KiB
JavaScript

const consoleCode = document.querySelector("#console-code");
export const width = 480;
export const height = 360;
const canvas = document.querySelector("canvas");
canvas.width = width;
canvas.height = height;
const cx = canvas.getContext("2d");
cx.imageSmoothingEnabled = false;
export function rgb(red, green, blue) {
return `rgb(${red}, ${green}, ${blue})`;
}
export function clear(color) {
cx.fillStyle = color;
cx.fillRect(0, 0, width, height);
}
export function drawRect(x, y, width, height, color) {
cx.fillStyle = color;
cx.fillRect(x, y, width, height);
}
export function println(msg) {
if (consoleCode) {
consoleCode.textContent += `${msg}\n`;
} else {
console.log(msg);
}
}
export function startGameLoop(loopFunction) {
let before = Date.now();
const loopInterval = setInterval(() => {
const now = Date.now();
const deltaT = (now - before) / 1000;
before = now;
loopFunction(deltaT);
}, 16);
const abortSignal = globalThis.libInternalAbortController.signal;
if (abortSignal) {
if (abortSignal.aborted) {
clearInterval(loopInterval);
}
abortSignal.addEventListener("abort", () => {
clearInterval(loopInterval);
});
}
}