How to make a tiny browser game

A simple recipe using HTML, CSS and JavaScript — no frameworks.

⏱️ ~5 min read
GatorNor illustration

Base structure

Start with a canvas or a simple <div>-based scene. Canvas is usually easiest for animation.

<canvas id="game" width="640" height="360"></canvas>
<script>
const cvs = document.getElementById('game');
const ctx = cvs.getContext('2d');
let t=0;
function loop(){
  t++;
  ctx.clearRect(0,0,cvs.width,cvs.height);
  // draw a simple shape
  ctx.fillStyle = '#9146FF';
  ctx.fillRect(50 + Math.sin(t/20)*20, 150, 40, 40);
  requestAnimationFrame(loop);
}
loop();
</script>

Game logic

Create a game state: position, velocity, score, enemies. Update every frame.

const player = {x:50, y:160, w:40, h:40, vy:0};
const gravity = 0.6;
const keys = new Set();

function update(){
  player.vy += gravity;
  player.y += player.vy;
  if(player.y + player.h >= 320){ // ground
    player.y = 320 - player.h;
    player.vy = 0;
  }
}

function draw(){
  const {width, height} = cvs;
  ctx.fillStyle = '#171a21';
  ctx.fillRect(0,0,width,height);
  ctx.fillStyle = '#9146FF';
  ctx.fillRect(player.x, player.y, player.w, player.h);
}

Controls

Add keyboard input for jump, movement and pause.

addEventListener('keydown', e => keys.add(e.key));
addEventListener('keyup',   e => keys.delete(e.key));

function handleInput(){
  if(keys.has(' ') && player.vy === 0){ player.vy = -10; } // jump
}

Polish & tips

🎵 Audio

Use short wav/ogg files. Trigger sounds on collisions and scoring.

📱 Scaling

Scale the canvas to screen size, but keep a fixed internal resolution.

💾 Save

Store highscores in localStorage.

⚙️ FPS

Use requestAnimationFrame and delta time for smoothness.