mv playground src

This commit is contained in:
sfja 2025-10-10 18:01:15 +02:00
parent 5ae84ad471
commit ea22b2de6a
7 changed files with 78 additions and 0 deletions

24
src/code_stopper.js Normal file
View File

@ -0,0 +1,24 @@
export class CodeStopper {
constructor() {
this.abortController = new AbortController();
}
start() {
this.abortController = new AbortController();
}
stop() {
this.abortController.abort();
}
isStopped() {
return this.abortController.signal.aborted;
}
addListener(fn) {
this.abortController.signal
.addEventListener("abort", () => {
fn();
});
}
}

54
src/gamelib.js Normal file
View File

@ -0,0 +1,54 @@
export class Gamelib {
constructor(console, codeStopper, canvasElement) {
this.console = console;
this.codeStopper = codeStopper;
this.canvas = canvasElement;
this.width = 480;
this.height = 360;
this.canvas.width = this.width;
this.canvas.height = this.height;
this.cx = this.canvas.getContext("2d");
this.cx.imageSmootingEnabled = false;
}
println(msg) {
this.console.log(msg);
}
startGameLoop(loopFunction) {
let before = Date.now();
const loopInterval = setInterval(() => {
const now = Date.now();
const deltaT = (now - before) / 1000;
before = now;
loopFunction(deltaT);
}, 16);
if (this.codeStopper.isStopped()) {
clearInterval(loopInterval);
}
this.codeStopper.addListener(() => {
clearInterval(loopInterval);
});
}
rgb(red, green, blue) {
return `rgb(${red}, ${green}, ${blue})`;
}
clear(color) {
const cx = this.cx;
cx.fillStyle = color;
cx.fillRect(0, 0, this.width, this.height);
}
drawRect(x, y, width, height, color) {
const cx = this.cx;
cx.fillStyle = color;
cx.fillRect(x, y, width, height);
}
}