Overview & Quick Start
A web browser is built to display documents and buttons—not 60-frames-per-second video games.
Maverick gives you a blank digital painting board (an HTML5 <canvas>) and a heartbeat
(a game loop) so you can build 2D and 2.5D games without fighting browser quirks or UI frameworks like React.
onUpdate),
and then it paints the new picture onto the screen (onDraw).
Without a Game Engine
If you use raw browser timers like setInterval or requestAnimationFrame by hand,
a player on a 144Hz monitor runs twice as fast as someone on a 60Hz laptop. Switching browser tabs causes
huge time jumps that teleport characters through walls, blurry pixels appear on Retina screens, and leaving a page
leaks keyboard listeners.
Why Maverick Built Game
Game locks your game's math to a steady fixed-step clock (so gameplay speed is identical
on every monitor), automatically scales crisp pixels for high-DPI screens (DPR scaling),
caps lag spikes so characters don't teleport, and cleans up every listener when you call unmount().
1. Install
npm install maverick
2. Your First Game & The 4 Lifecycle Hooks
To make a game, you create a class that extends Game. Maverick calls four special methods
(called lifecycle hooks) at the exact right moments—just like a theater stage manager:
onReady()— Opening night: runs once when the canvas is mounted. Load images and spawn your player here.onUpdate(timeDelta)— Behind the scenes: runs every tick to move characters and check rules.timeDeltais the time passed in seconds (e.g.0.0167), so speed is measured in pixels per second, not frames.onDraw(timeDelta, perf)— On stage: wipes the old frame (Viewport.clear()) and paints the new one.onDisengaged()— Curtain call: runs when the game is unmounted so nothing keeps running in the background.
import { Game, type SystemPerformance } from 'maverick';
class HelloGame extends Game {
onReady(): void {
this.Logger.info('Game ready!');
}
onDisengaged(): void {
this.Logger.info('Cleaned up!');
}
onUpdate(_timeDelta: number): void {
// Fixed-step simulation logic (in seconds)
}
onDraw(_timeDelta: number, perf: SystemPerformance): void {
this.Viewport.clear();
this.Viewport.drawText(
`Hello Maverick (${perf.fps} FPS)`,
{ x: 24, y: 32 },
this.Fonts.get('default')
);
}
}
const root = document.getElementById('game-root');
if (root) {
const game = new HelloGame({
name: 'HelloWorld',
viewport: { width: 480, height: 320, fps: 60, bgColor: '#0f172a', showPerfStats: true },
});
game.mount(root);
}
game.pause() to pause/unpause simulation,
or game.unmount() to stop the loop, release DOM listeners, and remove any auto-created canvas.
HelloGame Lifecycle3. Core Managers on Game
| Property | Responsibility |
|---|---|
game.Viewport | Canvas 2D context, DPR scaling, Camera2D, drawing, wrapped text |
game.Elements | Flat GameElement registry (for single-scene games) |
game.Scenes | Multi-scene switching (SceneManager) |
game.Sprites | Image loading (preload) and spritesheet sub-rectangles |
game.Input | Action-mapped keyboard & mouse input |
game.UI | Retained in-canvas UI widgets (UIButton, UIPanel, etc.) |
game.Sounds | Web Audio buses (master, sfx, music) |
game.Timers / game.Tweens | Pause-aware timers and property tweening |