@fogrexon/glyph-arena-draw

Minimal 2D canvas drawing helpers. See also package README.

Exports

Generated from the package's source when the docs are built — do not edit this table by hand.

NameKind
CreateDrawOptionstype
Croptype
Drawtype
DrawContexttype
MeasureTextOptionstype
Projectiontype
RectOptionstype
SpriteOptionstype
TextMetricsSizetype
TextOptionstype
ViewMatrixtype
createDrawfunction

createDraw({ context })

const draw = createDraw({ context }); // CanvasRenderingContext2D or OffscreenCanvasRenderingContext2D

draw.clear();           // or draw.clear("#112233")
draw.sprite({ image, x, y, width?, height?, crop?, alpha? });

Methods

SpriteOptions

FieldTypeDescription
imageCanvasImageSourcesource image
xnumberdestination x
ynumberdestination y
width?numberdestination width
height?numberdestination height
crop?Cropsource rectangle
alpha?numberopacity, clamped to 0..1

Crop

FieldTypeDescription
xnumbersource x
ynumbersource y
widthnumbersource width
heightnumbersource height

Drawing behavior

Views

setView takes a camera matrix, a Projection, or null. The last call wins, so a HUD drawn after setView(null) needs no save/restore.

draw.setView(camera.view(width, height));       // a Camera, when the game has one
draw.rect({ x: player.x, y: player.y, w: 20, h: 28, fill: "#fff" });

draw.setView({ scale: 1, offsetX: -cameraX, offsetY: 0, round: true }); // a flat scrolling world
draw.rect({ x: tileX, y: tileY, w: 32, h: 32, fill: "#4a5468" });

draw.setView(null);                             // screen coordinates again
draw.text({ text: "SCORE 0", x: 12, y: 12, size: 16, fill: "#fff" });

Projection

FieldTypeDescription
scalenumber1 draws at native size, 2 doubles it
offsetXnumberscreen pixels; -100 puts world x = 0 at screen x = -100
offsetYnumberscreen pixels
round?booleanrounds the offset to whole pixels, once

A projection is the matrix [scale, 0, 0, scale, offsetX, offsetY].

round rounds the offset, never a point. Two tiles at 0.4 and 0.6 stay 0.2 apart; rounding each point instead puts them on the same edge, and a wall built from tiles then shows a seam at every join. A game that scrolls rounds its camera once and subtracts it — which is what this option is.

Rectangles and text

The native calls are not an optimisation detail: on a fractional coordinate a path fill differs from fillRect by 2 pixels and a path stroke from strokeRect by 12 (measured in Chromium). A game moving from hand-written ctx.fillRect to draw.rect keeps its edges exactly.