TExcelTAD
Home › 2D Games with JavaScript & Canvas › Framework or Raw Canvas?

Framework or Raw Canvas?

Everything in this course so far is raw canvas: you wrote the loop, the input handling, the collisions and the drawing yourself. That is the best way to learn, because nothing is hidden. For a bigger project, a game framework can save weeks. This lesson compares the options and shows how the concepts you already know map onto them, so picking one up later is quick.

What a framework gives you

Feature Raw canvas (this course) A 2D framework
Rendering fillRect, drawImage on a 2D context Usually WebGL with sprite batching (thousands of sprites at 60 FPS), Canvas 2D fallback
Game loop Your run(update, draw) Built in, with delta time and often a fixed step for physics
Assets new Image(), your own loading code Loader with progress, sprite sheets, atlases, audio, fonts, tilemaps
Scenes Your own state machine Scene manager (boot, menu, game, pause)
Input keydown + a Set Keyboard, pointer, touch and gamepad unified
Physics AABB functions you wrote Arcade/AABB physics, sometimes Box2D- or Matter-style rigid bodies
Camera ctx.translate Cameras with follow, deadzone, bounds, shake, zoom
Audio Web Audio by hand Sound manager, unlocking after the first gesture, sprites, volume groups
Extras Write them Tweens, particles, tilemap loaders (Tiled, LDtk), text, scaling modes

The main options

Raw canvas Phaser 3 PixiJS Kaplay
What it is The browser API Complete game framework Fast 2D renderer Small, friendly game library (Kaboom's successor)
Includes physics, scenes, audio No Yes (Arcade + Matter physics) No: rendering, input events, assets; add the rest Yes (simple physics, scenes, audio)
Size (minified, roughly) 0 KB ~1 MB a few hundred KB, tree-shakable a few hundred KB
Learning curve Low API, but you build everything Medium: big API, lots of examples Medium: you design the game structure Low: very readable, great for jams
Best for Learning, tiny games, full control, odd rendering Complete 2D games of almost any genre Custom engines, heavy visuals, interactive sites Prototypes, game jams, teaching, small games

Other good options: Excalibur (TypeScript-first engine), melonJS, LittleJS (tiny, fast), and for 3D Three.js and Babylon.js (see the Three.js course in this track). Engines with a web export, such as Godot, Unity and Defold, are worth a look if you also want desktop and console builds.

How to decide

  1. Learning or a game jam under 48 hours? Raw canvas or Kaplay. You know the whole code and nothing surprises you.
  2. A complete 2D game with levels, physics, audio and menus? Phaser. It has solved the boring problems (loading, scaling, audio unlocking, tilemaps).
  3. Lots of sprites or effects, or your own engine structure? PixiJS as the renderer, plus your own loop, physics and scenes.
  4. Desktop, mobile and console too? A full engine (Godot, Unity) with a web export.
  5. Still unsure? Build the core loop in raw canvas first. If you start writing a sprite batcher, a texture atlas packer or a scene graph, that is the moment to switch.

Tip: Whatever you choose, the ideas from this course carry over: fixed-step updates, AABB collisions, coyote time, pooling, tweens, camera follow, saving. Frameworks only change the names.

Your concepts, their names

This course Phaser 3 PixiJS v8 Kaplay
run(update, draw) Scene.update(time, delta) app.ticker.add(fn) onUpdate(fn) / dt()
Game states this.scene.start('Game') Containers you show and hide scene('game', fn) + go('game')
keys.has('ArrowLeft') cursors.left.isDown DOM events (your own keys set) isKeyDown('left') / onKeyDown
Sprite drawn with drawImage this.add.sprite(x, y, 'key') new Sprite(texture) add([sprite('key'), pos(x, y)])
AABB collision this.physics.add.collider(a, b) Your own code area() + onCollide('tag', fn)
Gravity + jumping Arcade body setVelocityY, body.blocked.down Your own code body() + jump() + isGrounded()
Camera follow this.cameras.main.startFollow(player) Move a Container setCamPos(player.pos)
Tweens this.tweens.add({...}) Your own code or a tween library tween(from, to, time, setter, easing)
Particles this.add.particles(...) ParticleContainer particles() component

A scene manager in plain JavaScript

Frameworks call the same lifecycle methods on scenes that you can write yourself. Here is the skeleton, driven by a fake clock so the output is deterministic:

js · try it yourself
class Scene {
  constructor(name) { this.name = name; }
  create() {}
  update(dt) {}
}

class Game {
  constructor(scenes) {
    this.scenes = Object.fromEntries(scenes.map((s) => [s.name, s]));
    this.current = null;
    this.log = [];
  }
  start(name, data = {}) {
    this.current = this.scenes[name];
    this.current.game = this;
    this.log.push(`start ${name}`);
    this.current.create(data);
  }
  step(dt) { this.current.update(dt); }
}

class Title extends Scene {
  create() { this.t = 0; }
  update(dt) { this.t += dt; if (this.t >= 0.5) this.game.start('play', { lives: 3 }); }
}
class Play extends Scene {
  create({ lives }) { this.lives = lives; this.game.log.push(`  lives = ${lives}`); }
  update() {
    this.lives--;
    this.game.log.push(`  lost a life, ${this.lives} left`);
    if (this.lives === 0) this.game.start('over');
  }
}
class Over extends Scene {
  create() { this.game.log.push('  game over screen'); }
}

const game = new Game([new Title('title'), new Play('play'), new Over('over')]);
game.start('title');
for (let i = 0; i < 6; i++) game.step(0.25);
console.log(game.log.join('\n'));
start title
start play
  lives = 3
  lost a life, 2 left
  lost a life, 1 left
  lost a life, 0 left
start over
  game over screen

Phaser's Scene class has the same shape (init, preload, create, update), and this.scene.start('play', data) passes data just like start here.

Test yourself

What is PixiJS mainly?

  1. A full game engine with physics and scenes
  2. A fast 2D renderer; you add game structure and physics yourself
  3. A level editor
  4. A sound library

Which is a good signal that it is time to move from raw canvas to a framework?

  1. You need fillRect
  2. You find yourself writing a sprite batcher, atlas loader and scene graph
  3. Your game has a score
  4. You use requestAnimationFrame

In Kaplay, what does the course's AABB collision code correspond to?

  1. scene()
  2. The area() component with onCollide
  3. go()
  4. dt()