Drawing
Scenes draw through draw, an immediate-mode API in logical units. It runs on a batched WebGL2 renderer and falls back to Canvas 2D automatically where WebGL2 isn't available. Your code is the same either way.
render(draw) {
draw.withCamera(game.camera, () => {
draw.sprite(heroFrame, hero.x, hero.y, { origin: [0.5, 1], flipX: hero.facingLeft, flash: hero.hurt });
draw.rect(0, groundY, 320, 8, "#2f3a1f");
particles.draw(draw);
});
draw.text(`score ${score}`, 4, 4, { font: "8px monospace", color: "#cffb2d" });
}API
| Call | Draws |
|---|---|
sprite(frame | image, x, y, options?) | An image or sprite-sheet frame. Options: origin, scaleX, scaleY, rotation, flipX, flipY, alpha, tint, flash, smooth |
rect(x, y, w, h, color) | Filled rectangle |
circle(x, y, radius, color) | Filled, antialiased circle |
line(x1, y1, x2, y2, width, color) | Line |
text(string, x, y, options?) | Cached, crisp text. Options: font (default debuff sans; 8px monospace before 0.6), color, align, baseline, alpha |
push() / pop(), translate, scale, rotate | Transform stack |
withCamera(camera, fn) | Run fn in the camera's view |
clip(x, y, w, h, fn) | Draw only inside a rectangle (UI panels, lists, minimaps); nests |
alpha, blend("normal" | "add") | Global alpha and blend mode |
raw | { kind: "webgl2", gl } or { kind: "2d", ctx } escape hatch |
Colours are hex strings ("#cffb2d", "#cffb2d80"), any CSS colour, or numbers 0xRRGGBBAA.
Why it's fast
Every call becomes one instance in a GPU buffer. Up to 8 textures are bound per batch, so sprites, text and particles interleave without breaking batches. The hot path doesn't allocate.
debuff draws through spryte. On the renderer benchmark, draw.sprite is about 2.6-3.5x faster than PixiJS Sprite and holds about 100,000 sprites at 60 fps. For particles, bullets and swarms, draw.sprites(frame, positions, count, options, each?) draws a whole Float32Array of positions in one call, with optional per-copy scale, rotation and colour. It holds about 800,000 sprites at 60 fps, and with every property changing per particle it's 4.2x faster than PixiJS's ParticleContainer. That's one laptop GPU, so treat these as indicative (method and caveats).
Choosing a backend
new Game({ canvas, width, height, renderer: "auto" }); // default: WebGL2, else Canvas 2D
new Game({ canvas, width, height, renderer: "2d" }); // force Canvas 2Dgame.draw.kind tells you which one is active. On Canvas 2D, tint applies only its alpha.
Blending in linear light
new Game({ canvas, width, height, blendSpace: "linear" });Since 0.34, blendSpace reaches spryte's renderer, so colours mix in linear light: glows add up like light and light-on-dark text keeps its weight. The default "srgb" mixes as CSS and Canvas 2D do. See spryte's renderer options for the cost and for marking data images. Before 0.34, Game passed only renderer and smooth, so a debuff game couldn't turn it on.