chore: scaffold Dino Land with canvas base game loop
This commit is contained in:
+28
@@ -0,0 +1,28 @@
|
||||
const canvas = document.getElementById('game');
|
||||
const ctx = canvas.getContext('2d');
|
||||
|
||||
const state = {
|
||||
running: true,
|
||||
lastTs: performance.now()
|
||||
};
|
||||
|
||||
function drawBackground() {
|
||||
ctx.fillStyle = '#88d34f';
|
||||
ctx.fillRect(0, 420, canvas.width, 120);
|
||||
}
|
||||
|
||||
function tick(ts) {
|
||||
if (!state.running) return;
|
||||
const dt = Math.min((ts - state.lastTs) / 1000, 0.05);
|
||||
state.lastTs = ts;
|
||||
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
drawBackground();
|
||||
|
||||
requestAnimationFrame(tick);
|
||||
}
|
||||
|
||||
requestAnimationFrame((ts) => {
|
||||
state.lastTs = ts;
|
||||
requestAnimationFrame(tick);
|
||||
});
|
||||
Reference in New Issue
Block a user