# Class: Draw

Defined in: spryte/dist/draw.d.ts:74

Immediate-mode drawing in logical units, the same API on WebGL2 and Canvas 2D (ADR 0007).
Everything batches automatically; call order is draw order.

## Properties

### alpha

> **alpha**: `number`

Defined in: spryte/dist/draw.d.ts:82

Global alpha multiplier for subsequent draws.

***

### font

> **font**: `string` \| [`BitmapFont`](BitmapFont.md) \| (() => `string` \| [`BitmapFont`](BitmapFont.md))

Defined in: spryte/dist/draw.d.ts:87

Font for `text()` and `measure()` when none is given: a CSS font, a `BitmapFont`, or a function returning
one (called on first use, so a font built from a canvas costs nothing until text is drawn).

***

### quads

> **quads**: `number`

Defined in: spryte/dist/draw.d.ts:89

Quads (sprites, rects, glyph runs…) submitted this frame. Reset each frame; for diagnostics.

***

### ready

> **ready**: `Promise`\<`void`\>

Defined in: spryte/dist/draw.d.ts:147

Resolves once the backend can draw. Immediate on WebGL2; on the Canvas 2D fallback, once its chunk has
loaded (frames before that draw nothing). Rejects if the fallback fails to load.

***

### smooth

> **smooth**: `boolean`

Defined in: spryte/dist/draw.d.ts:78

Default filtering for images (false for pixel art).

## Accessors

### compressedFormats

#### Get Signature

> **get** **compressedFormats**(): `ReadonlySet`\<[`CompressedFormat`](../type-aliases/CompressedFormat.md)\>

Defined in: spryte/dist/draw.d.ts:140

GPU compression families this renderer can draw ("bc7", "etc2", "astc4x4"), so a game can pick which
`CompressedTexture` file to load. Empty on Canvas 2D. Since 0.17.

##### Returns

`ReadonlySet`\<[`CompressedFormat`](../type-aliases/CompressedFormat.md)\>

***

### contextLost

#### Get Signature

> **get** **contextLost**(): `boolean`

Defined in: spryte/dist/draw.d.ts:277

True while the WebGL context is lost (e.g. a backgrounded mobile tab): frames draw nothing until it's restored.

##### Returns

`boolean`

***

### drawCalls

#### Get Signature

> **get** **drawCalls**(): `number`

Defined in: spryte/dist/draw.d.ts:142

Draw calls this frame: batched GPU calls on WebGL2, one per quad on Canvas 2D.

##### Returns

`number`

***

### kind

#### Get Signature

> **get** **kind**(): `"2d"` \| `"webgl2"`

Defined in: spryte/dist/draw.d.ts:135

Which backend is active.

##### Returns

`"2d"` \| `"webgl2"`

***

### raw

#### Get Signature

> **get** **raw**(): [`RawContext`](../type-aliases/RawContext.md)

Defined in: spryte/dist/draw.d.ts:149

Raw `WebGL2RenderingContext` or `CanvasRenderingContext2D`. Mixing raw calls with `draw` calls requires care.

##### Returns

[`RawContext`](../type-aliases/RawContext.md)

## Methods

### begin()

> **begin**(`clear`, `width`, `height`, `scale`): `void`

Defined in: spryte/dist/draw.d.ts:287

Start a frame: clear to `clear` (null draws over what's there: an overlay, a second pass, or a frame another
engine began), at a logical size of `width × height` units with `scale` backing pixels per unit (2 on a 2×
screen, or the canvas's pixel width / `width`). Resets the transform, alpha, blend and clips.

#### Parameters

##### clear

[`Color`](../type-aliases/Color.md) \| `null`

##### width

`number`

##### height

`number`

##### scale

`number`

#### Returns

`void`

***

### blend()

> **blend**(`mode`): `void`

Defined in: spryte/dist/draw.d.ts:170

#### Parameters

##### mode

[`BlendMode`](../type-aliases/BlendMode.md)

#### Returns

`void`

***

### circle()

> **circle**(`x`, `y`, `radius`, `color`): `void`

Defined in: spryte/dist/draw.d.ts:173

Filled circle (antialiased).

#### Parameters

##### x

`number`

##### y

`number`

##### radius

`number`

##### color

[`Color`](../type-aliases/Color.md)

#### Returns

`void`

***

### clip()

> **clip**(`x`, `y`, `w`, `h`, `fn`): `void`

Defined in: spryte/dist/draw.d.ts:165

Run `fn` with drawing restricted to a rectangle (in current units), then restore. Nested clips
intersect. Clips are axis-aligned on screen: under rotation the rectangle's screen bounds are used.

#### Parameters

##### x

`number`

##### y

`number`

##### w

`number`

##### h

`number`

##### fn

() => `void`

#### Returns

`void`

***

### createShader()

> **createShader**(`source`): [`Shader`](../interfaces/Shader.md)

Defined in: spryte/dist/draw.d.ts:268

A custom fragment effect: see `Shader`. Compiled on first use, so errors surface at the first `withShader` draw.

#### Parameters

##### source

`string`

#### Returns

[`Shader`](../interfaces/Shader.md)

***

### createTarget()

> **createTarget**(`width`, `height`, `options?`): [`RenderTarget`](../../../spryte/classes/RenderTarget.md)

Defined in: spryte/dist/draw.d.ts:248

An offscreen image of `width × height` pixels to draw into with `into()`, then draw like any image. Pass
`{ data: true }` for a target that holds data rather than colour (velocities, a simulation field): under
`blendSpace: "linear"` colour targets are stored as sRGB, data targets keep their values as written. Pass
`{ format: "rgba16f" }` for values above 1 (since 0.13); check `target.format`, since it falls back to `rgba8`
where the GPU can't render to half floats.

#### Parameters

##### width

`number`

##### height

`number`

##### options?

[`TargetOptions`](../../../spryte/interfaces/TargetOptions.md) & `object`

#### Returns

[`RenderTarget`](../../../spryte/classes/RenderTarget.md)

***

### dispose()

> **dispose**(): `void`

Defined in: spryte/dist/draw.d.ts:293

Release GPU resources and listeners. Using this `Draw` afterwards throws; disposing again does nothing.

#### Returns

`void`

***

### disposeTarget()

> **disposeTarget**(`target`): `void`

Defined in: spryte/dist/draw.d.ts:275

Free a target's GPU memory. Drawing it afterwards recreates it, blank.

#### Parameters

##### target

[`RenderTarget`](../../../spryte/classes/RenderTarget.md)

#### Returns

`void`

***

### end()

> **end**(): `void`

Defined in: spryte/dist/draw.d.ts:291

Finish the frame: submits everything drawn since `begin()`.

#### Returns

`void`

***

### into()

> **into**(`target`, `fn`, `clear?`): `void`

Defined in: spryte/dist/draw.d.ts:266

Run `fn` with drawing going into `target` instead of the canvas, then switch back. Inside, units are the
target's pixels from its top-left; the transform, clips and global alpha start fresh and are restored
afterwards. `clear` fills the target first (default transparent); pass `null` to draw over what's there,
for trails and accumulation. Nests.

```ts
const glow = draw.createTarget(160, 90);
draw.into(glow, () => draw.sprites(spark, sparks, n, { tint: "#ffd36b" }));
draw.blend("add");
draw.sprite(glow, 160, 90, { scaleX: 2, scaleY: 2, smooth: true });
```

#### Parameters

##### target

[`RenderTarget`](../../../spryte/classes/RenderTarget.md)

##### fn

() => `void`

##### clear?

[`Color`](../type-aliases/Color.md) \| `null`

#### Returns

`void`

***

### invalidate()

> **invalidate**(`src`): `void`

Defined in: spryte/dist/draw.d.ts:279

Re-upload a source whose pixels changed (e.g. a canvas you draw into).

#### Parameters

##### src

[`TextureSource`](../type-aliases/TextureSource.md)

#### Returns

`void`

***

### line()

> **line**(`x1`, `y1`, `x2`, `y2`, `width`, `color`): `void`

Defined in: spryte/dist/draw.d.ts:174

#### Parameters

##### x1

`number`

##### y1

`number`

##### x2

`number`

##### y2

`number`

##### width

`number`

##### color

[`Color`](../type-aliases/Color.md)

#### Returns

`void`

***

### measure()

> **measure**(`text`, `font?`): `object`

Defined in: spryte/dist/draw.d.ts:236

The logical box `text()` fills for this string and font, so layouts can align and centre without
guessing. Same cache as `text()`, so measuring then drawing rasterises once.

#### Parameters

##### text

`string`

##### font?

`string` \| [`BitmapFont`](BitmapFont.md)

#### Returns

`object`

##### h

> **h**: `number`

##### w

> **w**: `number`

***

### pop()

> **pop**(): `void`

Defined in: spryte/dist/draw.d.ts:151

#### Returns

`void`

***

### push()

> **push**(): `void`

Defined in: spryte/dist/draw.d.ts:150

#### Returns

`void`

***

### record()

> **record**(`fn`): [`Layer`](../../../spryte/classes/Layer.md)

Defined in: spryte/dist/draw.d.ts:189

Record the sprites `fn` draws into a `Layer`, without drawing them, to draw them all again each frame with
`replay()`. Use it for what doesn't change: a tilemap, a background. Inside `fn`, draw in the layer's own
coordinates with `draw.sprite` (and `push`, `translate`, `pop`); other drawing calls throw. `draw.alpha`
isn't recorded: it applies when the layer is replayed.

```ts
const ground = draw.record((d) => {
  for (const t of map.tiles) d.sprite(tiles[t.id], t.x * 16, t.y * 16, { origin: [0, 0] });
});
// each frame:
draw.withCamera(camera, () => draw.replay(ground));
```

#### Parameters

##### fn

(`draw`) => `void`

#### Returns

[`Layer`](../../../spryte/classes/Layer.md)

***

### rect()

> **rect**(`x`, `y`, `w`, `h`, `color`): `void`

Defined in: spryte/dist/draw.d.ts:171

#### Parameters

##### x

`number`

##### y

`number`

##### w

`number`

##### h

`number`

##### color

[`Color`](../type-aliases/Color.md)

#### Returns

`void`

***

### replay()

> **replay**(`layer`): `void`

Defined in: spryte/dist/draw.d.ts:195

Draw a layer from `record()` under the current transform and `draw.alpha`, as if its sprites were drawn
here one by one. On WebGL2 that's one draw call per eight images it uses; on Canvas 2D, and under a custom
shader or `draw.alpha` below 1, its sprites are drawn again the usual way (the same pixels, without the saving).

#### Parameters

##### layer

[`Layer`](../../../spryte/classes/Layer.md)

#### Returns

`void`

***

### resize()

> **resize**(`w`, `h`): `void`

Defined in: spryte/dist/draw.d.ts:296

Tell spryte the canvas's backing size changed: call it with `canvas.width, canvas.height` after setting them.

#### Parameters

##### w

`number`

##### h

`number`

#### Returns

`void`

***

### rotate()

> **rotate**(`radians`): `void`

Defined in: spryte/dist/draw.d.ts:160

#### Parameters

##### radians

`number`

#### Returns

`void`

***

### scale()

> **scale**(`x`, `y?`): `void`

Defined in: spryte/dist/draw.d.ts:159

#### Parameters

##### x

`number`

##### y?

`number`

#### Returns

`void`

***

### sprite()

> **sprite**(`frame`, `x`, `y`, `o?`): `void`

Defined in: spryte/dist/draw.d.ts:197

Draw an image frame (or a whole image) at `x, y`, anchored by `origin`. Allocation-free hot path.

#### Parameters

##### frame

[`Frame`](../interfaces/Frame.md) \| [`TextureSource`](../type-aliases/TextureSource.md)

##### x

`number`

##### y

`number`

##### o?

[`SpriteDrawOptions`](../interfaces/SpriteDrawOptions.md)

#### Returns

`void`

***

### sprites()

> **sprites**(`frame`, `positions`, `count?`, `o?`, `each?`): `void`

Defined in: spryte/dist/draw.d.ts:222

Draw one image frame at many positions in a single call: particles, bullets, swarms, starfields.
`positions` holds x, y pairs (a `Float32Array` you reuse is ideal); `count` defaults to all of them.
Every copy shares `options` (rotation, scale, tint, alpha…), so per-copy work is just the position.
Much faster than calling `sprite()` in a loop, with no per-sprite allocation.

```ts
const pos = new Float32Array(2 * MAX);
// ...update pos...
draw.sprites(spark, pos, live, { tint: "#ffd36b", alpha: 0.8 });
```

Per-copy scale, rotation, colour and frame go in `each`. For a frame per copy, pass the frames as an
array and index it with `each.frame`:

```ts
draw.sprites(itemFrames, pos, n, {}, { frame: itemKind });
```

Arrays are read during the call and not kept. An array viewing wasm memory detaches when that memory grows, and
a detached array has length 0, so nothing draws: recreate the view after growth, or each frame.

#### Parameters

##### frame

[`Frame`](../interfaces/Frame.md) \| [`TextureSource`](../type-aliases/TextureSource.md) \| readonly [`Frame`](../interfaces/Frame.md)[]

##### positions

`ArrayLike`\<`number`\>

##### count?

`number`

##### o?

[`SpriteDrawOptions`](../interfaces/SpriteDrawOptions.md)

##### each?

[`SpritesEach`](../../../spryte/interfaces/SpritesEach.md)

#### Returns

`void`

***

### supportsTarget()

> **supportsTarget**(`format`): `boolean`

Defined in: spryte/dist/draw.d.ts:252

Whether `createTarget` can give you `format` here, without making one. Since 0.13.

#### Parameters

##### format

[`TargetFormat`](../../../spryte/type-aliases/TargetFormat.md)

#### Returns

`boolean`

***

### text()

> **text**(`text`, `x`, `y`, `o?`): `void`

Defined in: spryte/dist/draw.d.ts:231

Text, rasterised once and cached as a texture (crisp at the current render scale).

#### Parameters

##### text

`string`

##### x

`number`

##### y

`number`

##### o?

[`TextDrawOptions`](../interfaces/TextDrawOptions.md)

#### Returns

`void`

***

### transform()

> **transform**(`a`, `b`, `c`, `d`, `e`, `f`): `void`

Defined in: spryte/dist/draw.d.ts:158

Multiply a matrix `[a, b, c, d, e, f]` (x' = a·x + c·y + e, y' = b·x + d·y + f, the Canvas order) onto the
current transform, the way `translate`, `scale` and `rotate` do. For a camera or parent transform another
engine computed, which can include skew that those three can't express.

#### Parameters

##### a

`number`

##### b

`number`

##### c

`number`

##### d

`number`

##### e

`number`

##### f

`number`

#### Returns

`void`

***

### translate()

> **translate**(`x`, `y`): `void`

Defined in: spryte/dist/draw.d.ts:152

#### Parameters

##### x

`number`

##### y

`number`

#### Returns

`void`

***

### withCamera()

> **withCamera**(`camera`, `fn`): `void`

Defined in: spryte/dist/draw.d.ts:167

Run `fn` with the camera's view applied, then restore.

#### Parameters

##### camera

###### view

() => [`Matrix`](../type-aliases/Matrix.md)

##### fn

() => `void`

#### Returns

`void`

***

### withShader()

> **withShader**(`shader`, `fn`): `void`

Defined in: spryte/dist/draw.d.ts:273

Run `fn` with everything drawn through `shader`, except the vector shapes and `mesh`, which draw without it. Then
restore the previous one. Nests. WebGL2 only (see `Shader`).

#### Parameters

##### shader

[`Shader`](../interfaces/Shader.md)

##### fn

() => `void`

#### Returns

`void`
