Getting started
spryte draws 2D graphics on any canvas in immediate mode: each frame you call begin(), draw in paint order, then call end(). It uses WebGL2 and falls back to Canvas 2D on its own. This page covers everything a standalone setup needs.
Install
npm install spryteCreate a renderer
import { createRenderer } from "spryte";
const canvas = document.querySelector("canvas")!;
// smooth: false for pixel art
const draw = createRenderer(canvas, { smooth: false });
// instant on WebGL2; waits for the fallback otherwise
await draw.ready;
console.log(draw.kind); // "webgl2", or "2d" on the fallbackcreateRenderer(canvas, options?) takes three options:
| Option | Default | |
|---|---|---|
backend | "auto" | "auto" uses WebGL2 and falls back to Canvas 2D. "webgl2" throws if WebGL2 is unavailable. "2d" always uses Canvas 2D. |
smooth | true | Default image filtering. Use false for pixel art. Each sprite can override it. |
blendSpace | "srgb" | Where colours mix. "srgb" mixes sRGB bytes, as CSS and Canvas 2D do, so anti-aliased edges come out dark: dark text on light paper looks heavier, light text on dark paper thinner. "linear" (since 0.13) mixes in linear light, so swapping the colours keeps the weight and glows add up like light. It costs one extra full-screen pass at end() (0.05 to 0.13 ms per full-HD frame on a desktop GPU). Mark data images such as MSDF atlases with asData(image) before their first draw (since 0.16, asData(image, { premultiply: false }) also uploads straight alpha, under either blend space, so values packed into RGBA survive where alpha is below 255; it works for an <img>, since a canvas already holds premultiplied pixels and an ImageBitmap keeps the premultiplyAlpha it was made with), and make data targets with { data: true }. Shaders get spryte_toLinear(rgb) for their colour uniforms (a no-op under "srgb"), and SPRYTE_LINEAR is defined in linear mode. WebGL2 only. |
transparent | false | A see-through canvas. Clear with an alpha colour such as "#0000" to draw over the page or another canvas. Opaque is slightly cheaper for the compositor. |
draw.ready and the fallback
The Canvas 2D backend is a separate chunk, so WebGL2 browsers never download it. On the fallback, frames draw nothing until that chunk arrives (a few milliseconds). await draw.ready waits for it. On WebGL2 it resolves immediately, and it rejects if the fallback fails to load.
Size the canvas
You draw in logical units you choose, and you own the canvas's backing size. After changing canvas.width or canvas.height, call draw.resize():
function resize(): number {
const dpr = devicePixelRatio || 1;
const w = Math.round(canvas.clientWidth * dpr);
const h = Math.round(canvas.clientHeight * dpr);
if (canvas.width !== w || canvas.height !== h) {
canvas.width = w;
canvas.height = h;
draw.resize(w, h);
}
return dpr;
}The frame loop
function frame(time: number) {
const dpr = resize();
const w = canvas.clientWidth;
const h = canvas.clientHeight;
// clear colour, logical width and height, backing pixels per unit
draw.begin("#111", w, h, dpr);
draw.rect(10, 10, 40, 20, "#cffb2d");
draw.sprite(hero, w / 2, h / 2, { rotation: time / 1000 });
draw.text(`${draw.kind}: ${draw.drawCalls} draw calls`, 8, 40, { font: "14px sans-serif" });
draw.end();
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);begin(clear, width, height, scale) clears the canvas and sets the logical size. scale is backing pixels per logical unit, usually canvas.width / width (CSS scale × devicePixelRatio). It also sets the resolution text is rasterised at. begin() resets the transform stack, clips, global alpha, blend mode and shader, so each frame starts clean. end() submits the frame.
For a fixed resolution, draw at 320x180 logical units with scale = canvas.width / 320. Pixel art covers that setup.
Load images before drawing them
spryte draws HTMLImageElement, HTMLCanvasElement, OffscreenCanvas and ImageBitmap objects, plus its own render targets. Each is uploaded to the GPU the first time you draw it and cached per object. Make sure the pixels have loaded first:
async function load(url: string): Promise<HTMLImageElement> {
const img = new Image();
img.src = url;
await img.decode();
return img;
}
const hero = await load("/hero.png");
// or: const hero = await createImageBitmap(await (await fetch("/hero.png")).blob());If you change the pixels of a canvas you draw from, call draw.invalidate(thatCanvas) so spryte re-uploads it. spryte never owns your images: their textures are dropped by invalidate() or dispose().
Context loss
On phones, the browser can take the GPU away from a backgrounded tab. spryte handles it. While draw.contextLost is true, drawing is skipped. When the context is restored, GPU objects are recreated and images re-upload on first use, with nothing for you to do. The only thing that doesn't come back is the contents of render targets: redraw those each frame, or when contextLost turns false.
Clean up
draw.dispose(); // frees every GPU object and listenerUsing a disposed Draw throws. Disposing twice does nothing.
Diagnostics
draw.kind:"webgl2"or"2d".draw.quads: quads submitted this frame.draw.drawCalls: GPU draw calls this frame on WebGL2, one per quad on Canvas 2D.draw.raw:{ kind: "webgl2", gl }or{ kind: "2d", ctx }. If you mix raw calls withdrawcalls, restore any state you change.
GPU time
When a frame runs slow, gpuTimer tells you whether the GPU is the limit (draw less, or smaller) or your own code is (simulate less). Since spryte 0.8.0.
import { gpuTimer } from "spryte";
const gpu = gpuTimer(draw);
function frame() {
gpu.begin();
draw.begin("#000", 320, 180, scale);
// ...draw...
draw.end();
gpu.end();
draw.text(gpu.ms === undefined ? "GPU: n/a" : `GPU ${gpu.ms.toFixed(1)} ms`, 8, 56);
}gpu.msis smoothed and arrives a few frames late. Reading it never stalls the GPU.- It stays
undefinedwhere the GPU can't be timed: on the Canvas 2D fallback, and in browsers withoutEXT_disjoint_timer_query_webgl2, which in practice means everything except Chromium on desktop. Treat it as a development tool, not something to show players. - Only one GPU timer can run at a time. Inside another engine that already times its frame, or alongside a profiler, frames are skipped.
For a sense of scale: in the flow demo on a desktop GPU, a million sprites a frame took 1.5 to 7.3 ms of GPU time against 20 to 36 ms of JavaScript, so the simulation, not the drawing, set the frame rate.
Next: Sprites