Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

Architecting a 60FPS Real-Time Multiplayer Stickman Battle Arena with HTML5 Canvas & WebSockets: The Engineering of Paper War

Architecting a 60FPS Real-Time Multiplayer Stickman Battle Arena with HTML5 Canvas & WebSockets: The Engineering of Paper War Building real-time multiplayer action games inside a standard web browser without third-part

Architecting a 60FPS Real-Time Multiplayer Stickman Battle Arena with HTML5 Canvas & WebSockets: The Engineering of Paper War

Building real-time multiplayer action games inside a standard web browser without third-party plugins, Unity exports, or heavy WebGL engines presents a fascinating set of architectural constraints. When our engineering team at UCDREAMS set out to build Paper War — an instant-play multiplayer stickman battle arena on our Games Arcade Hub — we set three strict engineering benchmarks:

  1. Rock-solid 60 FPS rendering across mobile and desktop browsers using pure HTML5 Canvas 2D.
  2. Sub-30ms client-side input responsiveness with server reconciliation and linear interpolation (LERP).
  3. Zero downloads, zero sign-ups, and instant room matchmaking via lightweight WebSockets.

Here is an architectural walkthrough of how we solved spatial partitioning, authoritative server state synchronization, and render loop optimization.

1. High-Performance Canvas 2D Render Loop & Sub-Pixel Interpolation

Modern desktop and mobile screens run at varying refresh rates (60Hz, 120Hz, 144Hz). Decoupling the game physics simulation loop from the canvas rendering loop is critical to prevent game speed fluctuations.

We maintain a fixed physics tick rate (e.g., 60 ticks per second / 16.66ms per tick) while rendering dynamically on requestAnimationFrame:

let lastFrameTime = performance.now();
const FIXED_STEP = 1000 / 60; // 16.66ms
let accumulator = 0;

function gameLoop(now) {
  const delta = Math.min(now - lastFrameTime, 100); // Clamp delta to avoid spiral of death
  lastFrameTime = now;
  accumulator += delta;

  // Fixed physics updates
  while (accumulator >= FIXED_STEP) {
    updatePhysics(FIXED_STEP);
    accumulator -= FIXED_STEP;
  }

  // Alpha represents how far we are between the previous and current physics frame
  const alpha = accumulator / FIXED_STEP;
  renderCanvas(alpha);

  requestAnimationFrame(gameLoop);
}

By rendering entities at their interpolated positions:
$$Position_{render} = Position_{prev} \times (1 - \alpha) + Position_{curr} \times \alpha$$
characters and projectiles glide seamlessly across the canvas without micro-stuttering.

2. Spatial Hash Grid Partitioning for O(1) Collision Detection

In a fast-paced stickman battle with dozens of active projectiles (arrows, spell blasts, melee swings) and player hitboxes, naive pairwise collision checking scales at O(N^2). At 100 active entities, that requires nearly 5,000 distance calculations 60 times per second.

To achieve O(1) collision performance, we built a 2D Spatial Hash Grid:

class SpatialGrid {
  constructor(cellSize, worldWidth, worldHeight) {
    this.cellSize = cellSize;
    this.cols = Math.ceil(worldWidth / cellSize);
    this.rows = Math.ceil(worldHeight / cellSize);
    this.cells = new Map();
  }

  _hashKey(col, row) {
    return `${col}_${row}`;
  }

  insert(entity) {
    const col = Math.floor(entity.x / this.cellSize);
    const row = Math.floor(entity.y / this.cellSize);
    const key = this._hashKey(col, row);

    if (!this.cells.has(key)) {
      this.cells.set(key, []);
    }
    this.cells.get(key).push(entity);
  }

  getPotentialColliders(entity) {
    const minCol = Math.max(0, Math.floor((entity.x - entity.radius) / this.cellSize));
    const maxCol = Math.min(this.cols - 1, Math.floor((entity.x + entity.radius) / this.cellSize));
    const minRow = Math.max(0, Math.floor((entity.y - entity.radius) / this.cellSize));
    const maxRow = Math.min(this.rows - 1, Math.floor((entity.y + entity.radius) / this.cellSize));

    const candidates = [];
    for (let c = minCol; c <= maxCol; c++) {
      for (let r = minRow; r <= maxRow; r++) {
        const cell = this.cells.get(this._hashKey(c, r));
        if (cell) {
          for (let i = 0; i < cell.length; i++) {
            if (cell[i].id !== entity.id) {
              candidates.push(cell[i]);
            }
          }
        }
      }
    }
    return candidates;
  }

  clear() {
    this.cells.clear();
  }
}

Instead of testing against the entire arena, each projectile only tests against entities sharing its immediate grid bucket and surrounding neighbors.

3. Authoritative WebSocket State Sync & Delta Compression

To eliminate cheating and maintain fairness:

  1. Client sends input vectors only: [angle, velocity, action, sequenceId].
  2. Server processes authoritative state: Server calculates velocity, resolves collisions, and broadcasts delta snapshots at 30Hz.
  3. Delta Compression: The server only broadcasts entities that moved, changed health, or fired, dramatically reducing WebSocket payload sizes from kilobytes to ~80 bytes per frame.

4. Play Live on UCDREAMS Games Arcade

Experience the real-time canvas engine in action:
👉 Play Paper War Multiplayer Stickman Arena

👉 Play Ludo Classic Multiplayer Arcade

👉 Play Click War Multiplayer Pixel Frog

👉 Explore UCDREAMS Games Arcade

Explore our 100% free, client-side developer and productivity utilities at UCDREAMS Tools Hub (including our Free In-Browser Background Remover, WebRTC FileShareOnline, and IG Media & Social Downloader). Job hunting in tech? Check out our 100% free job application autofill Chrome extension: ApplyPilot.

5. Need Custom Web, App, or Multiplayer Game Engineering?

Building low-latency real-time applications requires specialized full-stack architecture, clean network protocols, and rigorous optimization. At UCDREAMS, our senior engineers design and build:

  • High-performance real-time multiplayer WebRTC & WebSocket platforms.
  • Bespoke web applications (React, Django REST, Next.js, Node.js).
  • Figma-to-WordPress conversions and custom Elementor architectures scoring 95+ on Google PageSpeed.
  • Edge AI, WebAssembly, and zero-server client-side web tools.

Ready to bring your digital product to life with senior engineering rigor?

👉 Get in Touch with UCDREAMS Senior Engineering Team

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.