agentleFS
omspradippatilomspradippatil/Claude-Skills/.claude/skills/web-gamedev/SKILL.md

web-gamedev

Skill3 starsChanged 10 days agoMITView on GitHub

Activate this skill for HTML5 canvas games, game loops, physics engines (Matter.js), sprite rendering, or interactive browser gaming experiences. Use when building 2D games, physics simulations, or real-time canvas architectures.

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 2 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Never update physics with variable delta time (dt), which causes unstable physics calculations, clipping through walls, and …

    line 29
  2. Very specific:

    Always scale the canvas buffer by window.devicePixelRatio while keeping CSS dimensions fixed to eliminate blurry graphics:

    line 61
  3. Distinctive:

    Never instantiate short-lived objects (bullets, damage numbers, particle effects) in the inner game loop.

    line 81
  4. Distinctive:

    Use collision categories (bitmasks) to prevent friendly fire or unwanted collisions between specific entity groups:

    line 108

≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Web Game Development & 2D Canvas Engineering

Scope: High-performance HTML5 2D Canvas, deterministic fixed-timestep game loops, physics simulation with Matter.js, sprite sheets, input handling (keyboard, touch, gamepad), spatial partitioning, and audio management with Web Audio API.


When to Activate

  • Designing and implementing 2D browser games or physics simulations
  • Integrating Matter.js or custom rigid-body physics
  • Structuring game architecture (Entity-Component-System or Object-Oriented state machines)
  • Managing canvas animation loops, delta timing, and frame budget
  • Handling responsive multi-touch, keyboard, and gamepad input
  • Optimizing canvas render performance and eliminating frame drops

1. Deterministic Game Loop & Frame Timing

The Fixed Timestep Loop ("Fix Your Timestep")

Never update physics with variable delta time (dt), which causes unstable physics calculations, clipping through walls, and non-deterministic behavior. Decouple simulation updates from display rendering:

let lastTime = performance.now();
let accumulator = 0;
const TIMESTEP = 1000 / 60; // 60 updates per second (16.67ms)

function gameLoop(currentTime) {
  const frameTime = Math.min(currentTime - lastTime, 250); // Cap spiral of death
  lastTime = currentTime;
  accumulator += frameTime;

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

  // Render with interpolation between physics frames
  const alpha = accumulator / TIMESTEP;
  render(alpha);

  requestAnimationFrame(gameLoop);
}
requestAnimationFrame(gameLoop);

2. 2D Canvas Performance Architecture

High-DPI Display Setup

Always scale the canvas buffer by window.devicePixelRatio while keeping CSS dimensions fixed to eliminate blurry graphics:

function resizeCanvas(canvas, width, height) {
  const dpr = Math.min(window.devicePixelRatio || 1, 2);
  canvas.width = width * dpr;
  canvas.height = height * dpr;
  canvas.style.width = `${width}px`;
  canvas.style.height = `${height}px`;

  const ctx = canvas.getContext('2d');
  ctx.scale(dpr, dpr);
  return ctx;
}

Offscreen Canvas & Pre-Rendering

  • Pre-render complex static backgrounds or procedural textures onto an offscreen canvas (new OffscreenCanvas() or an in-memory <canvas>).
  • Draw the pre-rendered canvas in a single fast ctx.drawImage(offscreenCanvas, 0, 0) call rather than redrawing individual primitives every frame.

Object Pooling

Never instantiate short-lived objects (bullets, damage numbers, particle effects) in the inner game loop. Memory allocation triggers JavaScript Garbage Collection pauses that cause visible stutter. Pre-allocate an array of reusable objects and toggle their active state.


3. Physics with Matter.js

Rigid Body Simulation

import Matter from 'matter-js';

const { Engine, Render, Runner, Bodies, Composite } = Matter;

const engine = Engine.create({
  gravity: { x: 0, y: 1 }
});

// Create boundaries
const ground = Bodies.rectangle(400, 590, 810, 60, { isStatic: true });
const player = Bodies.circle(100, 100, 20, {
  restitution: 0.5,
  friction: 0.05
});

Composite.add(engine.world, [ground, player]);

Collision Handling & Filtering

Use collision categories (bitmasks) to prevent friendly fire or unwanted collisions between specific entity groups:

const CATEGORY_PLAYER = 0x0001;
const CATEGORY_ENEMY  = 0x0002;
const CATEGORY_BULLET = 0x0004;

// Player bullet collides with enemies, but not the player
playerBullet.collisionFilter = {
  category: CATEGORY_BULLET,
  mask: CATEGORY_ENEMY
};

4. Multi-Input & Audio Architecture

Input Management

  • Implement a unified Input Manager that abstracts raw events (keydown, touchstart, pointerdown) into semantic game actions (JUMP, ATTACK, MOVE_LEFT).
  • Prevent default browser behaviors for game keys (Arrow keys, Spacebar) only when the game canvas has active focus.

Web Audio API

  • Modern browsers block audio autoplay until the user interacts with the page. Initialize the AudioContext only after the first user click or keypress:
    let audioCtx = null;
    function ensureAudioContext() {
      if (!audioCtx) {
        audioCtx = new (window.AudioContext || window.webkitAudioContext)();
      }
      if (audioCtx.state === 'suspended') {
        audioCtx.resume();
      }
    }

5. Verification Checklist

  • Game maintains a rock-solid 60fps on standard desktop and mobile hardware.
  • Physics simulation is deterministic and does not clip through boundaries at low frame rates.
  • Memory footprint is flat over extended gameplay (verified zero GC spikes via object pooling).
  • Canvas is sharp on Retina / High-DPI screens without blurriness.
  • Audio respects browser autoplay policies and supports mute toggling.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

Reviews can't be read right now.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.