# 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.

```ts playground
//+ import { Game } from "@debuffjp/core";
//+ import { Particles } from "@debuffjp/juice";
//+ const canvas = document.querySelector("#game")!;
//+ const game = new Game({ canvas, width: 320, height: 180, background: "#11140d" });
//+ // heroFrame: buff, drawn into a small canvas so this example needs no image file.
//+ const heroFrame = document.createElement("canvas");
//+ heroFrame.width = 24; heroFrame.height = 22;
//+ const g = heroFrame.getContext("2d")!;
//+ g.fillStyle = "#cffb2d"; g.beginPath(); g.roundRect(0, 0, 24, 22, 7); g.fill();
//+ g.fillStyle = "#11140d"; g.fillRect(14, 6, 3, 6); g.fillRect(19, 6, 3, 6);
//+ const groundY = 150;
//+ const hero = { x: 160, y: groundY, vx: 60, facingLeft: false, hurt: 0 };
//+ const particles = new Particles(() => game.random.next());
//+ let score = 0;
//+ let timer = 0;
//+ game.add({ id: "play", update(dt) {
//+ hero.x += hero.vx * dt;
//+ if (hero.x  280) hero.vx = -hero.vx;
//+ hero.facingLeft = hero.vx  1.5) {
//+ timer = 0; score++; hero.hurt = 1; game.camera.shake(0.3);
//+ particles.emit({ x: hero.x, y: hero.y - 12, count: 14, colors: ["#cffb2d", "#38bdf8", "#f472b6"], shape: "circle", size: [2, 5], gravity: 300 });
//+ }
//+ particles.update(dt);
//+ },
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" });
}
//+ }).go("play");
//+ game.start();
```

## 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](https://github.com/debuffjp/spryte). On the [renderer benchmark](/benchmarks), `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](/benchmarks)).

## Choosing a backend

```ts
new Game({ canvas, width, height, renderer: "auto" }); // default: WebGL2, else Canvas 2D
new Game({ canvas, width, height, renderer: "2d" }); // force Canvas 2D
```

`game.draw.kind` tells you which one is active. On Canvas 2D, `tint` applies only its alpha.

## Blending in linear light

```ts
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](/spryte/getting-started#create-a-renderer) 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.