tecs.gfx.sheet

A sprite sheet divides one image into frames, tags, and slices.

Aseprite supplies the model: each frame carries its own duration, tags play inclusive spans in a direction, and slices carry rectangles, nine-slice centers, and pivots that may move between frames. fromAseprite reads its JSON export into the same interface grid, rects, and build produce.

const hero = tecs.gfx.sheet.grid({
    name = "game.hero",
    imageWidth = 256,
    imageHeight = 32,
    frameWidth = 32,
    frameHeight = 32,
    tags = {idle = {from = 1, to = 4}, run = {from = 5, to = 8}},
})

hero:bind(tecs.gfx.images.id("sprites/hero"))

world:spawn(
    tecs.ecs.Transform2D(64, 64, 0, 1, 0, 32, 32),
    tecs.gfx.Tint(1, 1, 1, 1),
    hero:sprite(),
    tecs.gfx.Renderable2D
)

Frames count from one in sheet order. Tag zero is the whole sheet playing forward, which is what an animation naming no tag plays.

bind resolves the sheet's pixel rectangles against a tecs.gfx.images id, so sprite and uv then answer the region a Sprite samples. A sheet is usable before that: until it is bound, a frame's region is its plain fraction of the image.

A sheet name is a snapshot compatibility surface. Every entity playing the sheet shares its registered frame, tag, slice, and timing data.

Pivots#

pivotOf answers a slice's pivot as a fraction of its frame, which is what an attachment point on a hand, a muzzle, or a pair of feet is measured in. No component carries one: a frame packet places a quad by its center, so a pivot is a number a game reads rather than something the renderer applies. The render lane owns adding a quad origin, and a pivot becomes a component when it does.

Module contents

Types

TypeKindDescription
AsepriteOptionstypeConfigures a sheet read from an Aseprite JSON export.
BuilderrecordBuilds a sheet one frame, tag, and slice at a time.
DirectiontypeSelects how a tag walks its span.
GridOptionstypeConfigures a sheet cut into a uniform grid.
PivotValuestructA sprite's anchor as fractions of its frame.
RecttypeDefines one frame's pixel rectangle and duration.
RectsOptionstypeConfigures a sheet cut into explicitly listed rectangles.
SheetrecordRepresents an image divided into frames.
SlicetypeRepresents a named region that moves across frames.
SliceKeytypeDefines where a slice sits from one frame onward and which points it carries.
TagtypeDefines an inclusive frame span and its playback direction.

Functions

FunctionKindDescription
buildfunctionCreates a builder for a sheet the other constructors cannot describe.
byIdfunctionReturns the sheet a registration index names.
byNamefunctionReturns the sheet a name names.
fromAsepritefunctionCreates a sheet from an Aseprite JSON export.
gridfunctionCreates a sheet whose frames form a uniform grid.
rectsfunctionCreates a sheet from explicitly listed frame rectangles.
replacefunctionFolds a freshly built sheet into the one already registered under its name, keeping the old sheet's id.
revisionfunctionReturns how many times any sheet's frames have changed.

Values

ValueKindDescription
DEFAULT_DURATIONvariableThe milliseconds a frame is held when nothing says otherwise, which is what Aseprite writes for a frame nobody retimed.
PivotvariableAnchors a sprite directly, or follows a sheet slice.

Types#

AsepriteOptionstype#

type AsepriteOptions = {
    name: string?,
    json: any
}

Configures a sheet read from an Aseprite JSON export.

Builderrecord#

record Builder

    frame: function(self: Builder, x: number, y: number, w: number, h: number, duration: number?): Builder
    tag: function(self: Builder, name: string, from: integer, to: integer, direction: Direction?): Builder
    slice: function(
        self: Builder,
        name: string,
        x: number,
        y: number,
        w: number,
        h: number,
        pivotX: number?,
        pivotY: number?
    ): Builder
    sliceKeys: function(self: Builder, name: string, data: string?, keys: {SliceKey}): Builder
    finish: function(self: Builder): Sheet
end

Builds a sheet one frame, tag, and slice at a time.

What every constructor here goes through, and what an importer for a format nothing else reads writes into.

Methods

frame#
frame: function(self: Builder, x: number, y: number, w: number, h: number, duration: number?): Builder

Appends a frame to the sheet under construction.

Arguments
NameTypeDescription
selfBuilder
xnumber
ynumber
wnumber
hnumber
durationnumber?
Returns
TypeDescription
Builder
Raises
  • when the frame has no positive size

tag#
tag: function(self: Builder, name: string, from: integer, to: integer, direction: Direction?): Builder

Names an inclusive frame span and its playback direction.

Arguments
NameTypeDescription
selfBuilder
namestring
frominteger
tointeger
directionDirection?
Returns
TypeDescription
Builder
Raises
  • when the name is empty

slice#
slice: function(
    self: Builder,
    name: string,
    x: number,
    y: number,
    w: number,
    h: number,
    pivotX: number?,
    pivotY: number?
): Builder

Adds a fixed slice carrying one pivot.

Arguments
NameTypeDescription
selfBuilder
namestring
xnumber
ynumber
wnumber
hnumber
pivotXnumber?
pivotYnumber?
Returns
TypeDescription
Builder
sliceKeys#
sliceKeys: function(self: Builder, name: string, data: string?, keys: {SliceKey}): Builder

Adds a slice from explicit keys, which is what an importer writes.

Arguments
NameTypeDescription
selfBuilder
namestring
datastring?
keys{SliceKey}
Returns
TypeDescription
Builder
finish#
finish: function(self: Builder): Sheet

Registers and returns the finished sheet.

Arguments
NameTypeDescription
selfBuilder
Returns
TypeDescription
Sheet
Raises
  • when the sheet has no frames, when a tag's span falls outside them, or when a slice has no name or no keys

Directiontype#

type Direction = "forward" | "reverse" | "pingpong"

Selects how a tag walks its span.

pingpong plays forward and then back without repeating either end, so a three-frame tag is 1, 2, 3, 2 and then round again.

These identifiers reach an Aseprite export and configuration a developer writes, so they are a compatibility surface.

GridOptionstype#

type GridOptions = {
    name: string,
    imageWidth: number,
    imageHeight: number,
    frameWidth: number,
    frameHeight: number,
    margin: number?,
    spacing: number?,
    columns: integer?,
    rows: integer?,
    count: integer?,
    duration: number?,
    tags: {[string]: Tag}?,
    slices: {Slice}?
}

Configures a sheet cut into a uniform grid.

PivotValuestruct#

struct PivotValue
    x: number
    y: number
    sheet: integer
    slice: integer
end
@derive(nupp.derive.Debug, nupp.derive.Serde)

A sprite's anchor as fractions of its frame. A bound slice follows its authored keys during GPU animation without updating the component.

Fields

x#
x: number

Caller-writable horizontal anchor; one half selects the center.

y#
y: number

Caller-writable vertical anchor; one half selects the center.

sheet#
sheet: integer

Caller-writable sheet id, or zero for a direct anchor.

slice#
slice: integer

Caller-writable slice id, or zero for a direct anchor.

Recttype#

type Rect = {
    x: number?,
    y: number?,
    w: number,
    h: number,
    duration: number?
}

Defines one frame's pixel rectangle and duration.

x and y default to zero, and duration to DEFAULT_DURATION milliseconds. These keys are the sheet format's own and are a compatibility surface.

RectsOptionstype#

type RectsOptions = {
    name: string,
    imageWidth: number,
    imageHeight: number,
    frames: {Rect},
    tags: {[string]: Tag}?,
    slices: {Slice}?
}

Configures a sheet cut into explicitly listed rectangles.

Sheetrecord#

record Sheet
    name: string
    id: integer
    count: integer
    imageWidth: number
    imageHeight: number

    rect: function(self: Sheet, frame: integer): (number, number, number, number)
    duration: function(self: Sheet, frame: integer): number
    uv: function(self: Sheet, frame: integer): (number, number, number, number)
    hasTag: function(self: Sheet, name: string): boolean
    tag: function(self: Sheet, name: string): (integer, integer, Direction)
    tagId: function(self: Sheet, name: string?): integer
    tagName: function(self: Sheet, id: integer): string
    tagCount: function(self: Sheet): integer
    cycle: function(self: Sheet, id: integer): number
    frameAt: function(self: Sheet, id: integer, time: number): integer
    pivot: function(
        self: Sheet,
        name: string,
        frame: integer?
    ): components.FFIInstance<PivotValue, components.FFIComponent<PivotValue>>
    slice: function(self: Sheet, name: string): Slice?
    sliceId: function(self: Sheet, name: string?): integer
    sliceName: function(self: Sheet, id: integer): string
    sliceKeyAt: function(self: Sheet, id: integer, frame: integer): SliceKey?
    pivotOf: function(self: Sheet, id: integer, frame: integer): (number, number)
    boundImage: function(self: Sheet): integer
    bind: function(self: Sheet, image: integer, u0: number?, v0: number?, u1: number?, v1: number?): Sheet
    sprite: function(
        self: Sheet,
        frame: integer?
    ): components.FFIInstance<rendercomponents.Sprite, components.FFIComponent<rendercomponents.Sprite>>
end

Represents an image divided into frames.

Methods

rect#
rect: function(self: Sheet, frame: integer): (number, number, number, number)

Returns a frame's pixel rectangle.

Arguments
NameTypeDescription
selfSheet
frameinteger
Returns
TypeDescription
number
number
number
number
Raises
  • when the frame is outside the sheet, because that is a sheet and an animation disagreeing rather than something to paper over

duration#
duration: function(self: Sheet, frame: integer): number

Returns how long a frame stays visible.

Arguments
NameTypeDescription
selfSheet
frameinteger
Returns
TypeDescription
number
Raises
  • when the frame is outside the sheet

uv#
uv: function(self: Sheet, frame: integer): (number, number, number, number)

Returns a frame's region.

Fractions of the image before bind, and of the bound image's region after it, which is what a Sprite samples.

Arguments
NameTypeDescription
selfSheet
frameinteger
Returns
TypeDescription
number
number
number
number
Raises
  • when the frame is outside the sheet

hasTag#
hasTag: function(self: Sheet, name: string): boolean

Returns whether the sheet carries a tag.

Arguments
NameTypeDescription
selfSheet
namestring
Returns
TypeDescription
boolean
tag#
tag: function(self: Sheet, name: string): (integer, integer, Direction)

Returns a named tag's span and direction.

Arguments
NameTypeDescription
selfSheet
namestring
Returns
TypeDescription
integer
integer
Direction
Raises
  • when the sheet carries no such tag, because the alternative is an animation silently playing the whole sheet on a typo

tagId#
tagId: function(self: Sheet, name: string?): integer

Returns the index a tag name represents, or zero when the sheet has none.

Zero reads as the whole sheet rather than as nothing, so an animation naming no tag plays every frame in order.

A name the sheet does not carry is reported at error level under the tecs.gfx logger and then treated as the whole sheet, because zero is a plausible wrong answer rather than a visible failure. The report names the sheet, the name asked for, and the tags the sheet does carry, once per sheet and name however often the name is asked. Call hasTag instead when a name's absence is expected and ordinary.

Arguments
NameTypeDescription
selfSheet
namestring?
Returns
TypeDescription
integer
tagName#
tagName: function(self: Sheet, id: integer): string

Returns the name a tag index represents.

Arguments
NameTypeDescription
selfSheet
idinteger
Returns
TypeDescription
string
tagCount#
tagCount: function(self: Sheet): integer

Returns how many named tags the sheet carries.

Arguments
NameTypeDescription
selfSheet
Returns
TypeDescription
integer
cycle#
cycle: function(self: Sheet, id: integer): number

Returns the duration of one tag cycle.

Arguments
NameTypeDescription
selfSheet
idinteger
Returns
TypeDescription
number
frameAt#
frameAt: function(self: Sheet, id: integer, time: number): integer

Returns the frame a tag shows at a point in its cycle.

Arguments
NameTypeDescription
selfSheet
idinteger
timenumber
Returns
TypeDescription
integer
pivot#
pivot: function(
    self: Sheet,
    name: string,
    frame: integer?
): components.FFIInstance<PivotValue, components.FFIComponent<PivotValue>>

Creates an anchor that follows a named slice.

Arguments
NameTypeDescription
selfSheet
namestring
frameinteger?
Returns
TypeDescription
components.FFIInstance<PivotValue, components.FFIComponent<PivotValue>>
slice#
slice: function(self: Sheet, name: string): Slice?

Returns a slice by name.

Arguments
NameTypeDescription
selfSheet
namestring
Returns
TypeDescription
Slice?
sliceId#
sliceId: function(self: Sheet, name: string?): integer

Returns the index a slice name represents, or zero when the sheet has none.

Arguments
NameTypeDescription
selfSheet
namestring?
Returns
TypeDescription
integer
sliceName#
sliceName: function(self: Sheet, id: integer): string

Returns the name a slice index represents.

Arguments
NameTypeDescription
selfSheet
idinteger
Returns
TypeDescription
string
sliceKeyAt#
sliceKeyAt: function(self: Sheet, id: integer, frame: integer): SliceKey?

Returns the slice key in effect on a frame.

A slice holds a key until the next one, so this answers the last key at or before the frame rather than only an exact match.

Arguments
NameTypeDescription
selfSheet
idinteger
frameinteger
Returns
TypeDescription
SliceKey?
pivotOf#
pivotOf: function(self: Sheet, id: integer, frame: integer): (number, number)

Returns a slice's pivot as a fraction of its frame.

Aseprite writes a pivot in the slice's own pixels, so this adds the slice's origin and divides by the frame, which is the number a quad wants: nothing downstream has to know the sheet's pixel sizes.

A slice with a center but no pivot answers the center's middle, and a slice with neither answers the middle of its own rectangle. Zero, a slice the sheet does not carry, and a frame it has no key for all answer the middle of the frame, which is where a quad sits with no pivot at all.

Arguments
NameTypeDescription
selfSheet
idinteger
frameinteger
Returns
TypeDescription
number
number
boundImage#
boundImage: function(self: Sheet): integer

Returns the image this sheet was last bound to.

Arguments
NameTypeDescription
selfSheet
Returns
TypeDescription
integer
bind#
bind: function(self: Sheet, image: integer, u0: number?, v0: number?, u1: number?, v1: number?): Sheet

Resolves the sheet's frames against an image.

The optional region names the part of the image the sheet occupies, which is what an atlas packer places it at, and defaults to the whole image. Frame rectangles are scaled into it, so passing a region smaller than the sheet describes places the frames inside that region rather than across the image.

Binding again rescales from the pixel rectangles rather than from the previous result, so re-registering an image accumulates no scaling error. Entities already carrying regions from an earlier bind keep them: a rebind updates no existing Sprite.

Arguments
NameTypeDescription
selfSheet
imageinteger
u0number?
v0number?
u1number?
v1number?
Returns
TypeDescription
Sheet
sprite#
sprite: function(
    self: Sheet,
    frame: integer?
): components.FFIInstance<rendercomponents.Sprite, components.FFIComponent<rendercomponents.Sprite>>

Creates a sprite showing one frame, ready to spawn.

Meaningful after bind, since before it the sheet names no image and the quad samples the backend's white fallback.

Arguments
NameTypeDescription
selfSheet
frameinteger?
Returns
TypeDescription
components.FFIInstance<rendercomponents.Sprite, components.FFIComponent<rendercomponents.Sprite>>
Raises
  • when the frame is outside the sheet

Fields

name#
name: string

Read-only. Reports the registered name a snapshot stores.

id#
id: integer

Read-only. Reports the registration index an Animation carries. Construction assigns an id once and never reuses it.

count#
count: integer

Read-only. Reports the number of frames, which is the largest index rect, uv, and sprite accept.

imageWidth#
imageWidth: number

Read-only. Reports the source image width in pixels. Frame rectangles are measured in it, so binding to an image of another size places frames wrongly.

imageHeight#
imageHeight: number

Read-only. Reports the source image height in pixels.

Slicetype#

type Slice = {
    name: string,
    data: string?,
    keys: {SliceKey}
}

Represents a named region that moves across frames.

Aseprite's slices carry hitboxes, attachment points, and nine-patch borders. Tecs reads pivots out of them; everything else is the game's to use. These keys are the sheet format's own and are a compatibility surface.

SliceKeytype#

type SliceKey = {
    frame: integer?,
    x: number?,
    y: number?,
    w: number?,
    h: number?,
    centerX: number?,
    centerY: number?,
    centerW: number?,
    centerH: number?,
    pivotX: number?,
    pivotY: number?
}

Defines where a slice sits from one frame onward and which points it carries.

A slice holds a key until the next one, so a slice that never moves is one key at frame one. A nil centerX means the slice names no nine-slice center, and a nil pivotX means it names no pivot. These keys are the sheet format's own and are a compatibility surface.

Tagtype#

type Tag = {
    from: integer,
    to: integer,
    direction: Direction?
}

Defines an inclusive frame span and its playback direction.

Aseprite calls this a frame tag, and a tag's name is what an animation asks for. direction defaults to "forward". These keys are the sheet format's own and are a compatibility surface.

Functions#

buildfunction#

function build(name: string, imageWidth: number, imageHeight: number): Builder

Creates a builder for a sheet the other constructors cannot describe.

The model is what the builder writes, so an atlas from any tool reaches the same sheet an Aseprite export does. Frames, tags, and slices are added in any order, and finish registers the result.

Arguments

NameTypeDescription
namestring

the name to register the finished sheet under

imageWidthnumber

the width in pixels of the image the frames are cut from

imageHeightnumber

the height in pixels of that image

Returns

TypeDescription
Builder

a builder whose methods chain

Raises

  • when the name is empty or either dimension is not positive

byIdfunction#

function byId(id: integer): Sheet?

Returns the sheet a registration index names.

Arguments

NameTypeDescription
idinteger

an id from a sheet this process built; ids are assigned in build order, so one is meaningful only within a run

Returns

TypeDescription
Sheet?

the sheet, or nil when nothing was built under the id

byNamefunction#

function byName(name: string): Sheet?

Returns the sheet a name names.

Building a second sheet under a taken name replaces what this returns, so a reload points new entities at the new sheet. Entities already carrying the old id keep drawing the old one, which is what stops a reload pulling a frame out from under them.

Arguments

NameTypeDescription
namestring

the name a sheet was built under

Returns

TypeDescription
Sheet?

the sheet most recently registered under the name, or nil

fromAsepritefunction#

function fromAseprite(options: AsepriteOptions): Sheet

Creates a sheet from an Aseprite JSON export.

One reader in front of the model rather than a second model: frames, their durations, frame tags with their directions, and slices with their keys all land in the sheet the builder writes.

The reader accepts Aseprite's array layout and its object layout, sorting the latter by frame name. It ignores spriteSourceSize, so export with trimming off.

Arguments

NameTypeDescription
optionsAsepriteOptions

the export as JSON text or an already-decoded table, and the name to register it under; the name defaults to the export's image name

Returns

TypeDescription
Sheet

the finished sheet, already registered

Raises

  • when the export is not readable, when it names no image and the caller supplied no name, or when a frame carries no rectangle

gridfunction#

function grid(options: GridOptions): Sheet

Creates a sheet whose frames form a uniform grid.

Margin surrounds the grid and spacing separates the cells, so a cell's left edge is margin + column * (frameWidth + spacing). Both default to zero, which is an image cut with nothing between its cells. Frames come out in row-major order.

Arguments

NameTypeDescription
optionsGridOptions

the image size, the cell size, and the optional grid shape, tags, and slices

Returns

TypeDescription
Sheet

the finished sheet, already registered under its name and carrying an id

Raises

  • on a missing name, a non-positive image or frame size, a grid that fits no cells, or a count past what the grid holds

rectsfunction#

function rects(options: RectsOptions): Sheet

Creates a sheet from explicitly listed frame rectangles.

For an image no grid describes: frames of differing sizes, or an atlas whose cells a packing tool placed. Rectangles are not checked against the image, so one running off the edge samples whatever the image holds there.

Arguments

NameTypeDescription
optionsRectsOptions

the image size, the frame list, and the optional tags and slices

Returns

TypeDescription
Sheet

the finished sheet, already registered under its name and carrying an id

Raises

  • on a missing name, a non-positive image size, an empty frame list, or a frame with no positive size

replacefunction#

function replace(built: Sheet): Sheet?, string?

Folds a freshly built sheet into the one already registered under its name, keeping the old sheet's id.

The reverse of what building under a taken name does, and it exists because a reload wants the reverse. Building again answers new entities with the new sheet and leaves every entity already playing on the old one, which is right for two sheets that happen to share a name and wrong for one file that was re-exported. This overwrites in place instead, so an Animation holding the id continues and shows the new frames on its next step without the world changing.

The refusals exist because a component holds an index into this sheet, so anything this refuses is a change that would renumber one. Tag ids follow the tag names in sorted order and slice ids follow the slices, so adding, removing, or renaming either is a restart. Frames are free to change: an entity carries a tag and a time, and each step resolves its frame from the cycle.

Replacement keeps the live sheet's bind and rescales the new frames against it, so a caller need not bind again.

The sheet handed in is spent. Its id resolves to the live sheet rather than to itself, because the two share their frame tables once the fold is done and only one of them may be bound.

Arguments

NameTypeDescription
builtSheet

a sheet from any constructor here, registered moments ago under a name something else already held

Returns

TypeDescription
Sheet?

the live sheet, or nil and the refusal reason

string?

revisionfunction#

function revision(): integer

Returns how many times any sheet's frames have changed.

Bumped by registration and by bind, both of which move where a frame's region points. Anything holding a copy of those regions compares this against what it copied at rather than being told, which keeps the dependency running one way: a sheet knows nothing about who read it.

Returns

TypeDescription
integer

a count that only ever rises

Values#

DEFAULT_DURATIONvariable#

const DEFAULT_DURATION: number

The milliseconds a frame is held when nothing says otherwise, which is what Aseprite writes for a frame nobody retimed.

Pivotvariable#

const Pivot

Anchors a sprite directly, or follows a sheet slice. Snapshots store names.