29 lines
578 B
JavaScript
29 lines
578 B
JavaScript
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);
|
|
});
|