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
| Type | Kind | Description |
|---|---|---|
AsepriteOptions | type | Configures a sheet read from an Aseprite JSON export. |
Builder | record | Builds a sheet one frame, tag, and slice at a time. |
Direction | type | Selects how a tag walks its span. |
GridOptions | type | Configures a sheet cut into a uniform grid. |
PivotValue | struct | A sprite's anchor as fractions of its frame. |
Rect | type | Defines one frame's pixel rectangle and duration. |
RectsOptions | type | Configures a sheet cut into explicitly listed rectangles. |
Sheet | record | Represents an image divided into frames. |
Slice | type | Represents a named region that moves across frames. |
SliceKey | type | Defines where a slice sits from one frame onward and which points it carries. |
Tag | type | Defines an inclusive frame span and its playback direction. |
Functions
| Function | Kind | Description |
|---|---|---|
build | function | Creates a builder for a sheet the other constructors cannot describe. |
byId | function | Returns the sheet a registration index names. |
byName | function | Returns the sheet a name names. |
fromAseprite | function | Creates a sheet from an Aseprite JSON export. |
grid | function | Creates a sheet whose frames form a uniform grid. |
rects | function | Creates a sheet from explicitly listed frame rectangles. |
replace | function | Folds a freshly built sheet into the one already registered under its name, keeping the old sheet's id. |
revision | function | Returns how many times any sheet's frames have changed. |
Values
| Value | Kind | Description |
|---|---|---|
DEFAULT_DURATION | variable | The milliseconds a frame is held when nothing says otherwise, which is what Aseprite writes for a frame nobody retimed. |
Pivot | variable | Anchors 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 ...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.
Members
| Name | Kind | Description |
|---|---|---|
sheet | field | |
tags | field | |
slices | field | |
frame | method | |
tag | method | |
slice | method | |
sliceKeys | method | |
finish | method |
framemethod#
frame: function(self: Builder, x: number, y: number, w: number, h: number, duration: number?): BuilderArguments
| Name | Type | Description |
|---|---|---|
self | Builder | |
x | number | |
y | number | |
w | number | |
h | number | |
duration | number? |
Returns
| Type | Description |
|---|---|
Builder |
tagmethod#
tag: function(self: Builder, name: string, from: integer, to: integer, direction: Direction?): BuilderArguments
| Name | Type | Description |
|---|---|---|
self | Builder | |
name | string | |
from | integer | |
to | integer | |
direction | Direction? |
Returns
| Type | Description |
|---|---|
Builder |
slicemethod#
slice: function(
self: Builder,
name: string,
x: number,
y: number,
w: number,
h: number,
pivotX: number?,
pivotY: number?
): BuilderArguments
| Name | Type | Description |
|---|---|---|
self | Builder | |
name | string | |
x | number | |
y | number | |
w | number | |
h | number | |
pivotX | number? | |
pivotY | number? |
Returns
| Type | Description |
|---|---|
Builder |
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 ...A sprite's anchor as fractions of its frame. A bound slice follows its authored keys during GPU animation without updating the component.
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 ...Represents an image divided into frames.
Members
| Name | Kind | Description |
|---|---|---|
name | field | Read-only. |
id | field | Read-only. |
count | field | Read-only. |
imageWidth | field | Read-only. |
imageHeight | field | Read-only. |
rects | field | Engine-owned. |
durations | field | Engine-owned. |
uvs | field | Engine-owned. |
tagFrom | field | |
tagTo | field | |
tagDirection | field | |
tagNames | field | |
tagIds | field | |
tagOrder | field | Engine-owned. |
tagTiming | field | Engine-owned. |
tagCycle | field | |
sliceList | field | |
sliceIds | field | |
image | field | |
originU | field | |
originV | field | |
scaleU | field | |
scaleV | field | |
rect | method | |
duration | method | |
uv | method | |
hasTag | method | |
tag | method | |
tagId | method | |
tagName | method | |
tagCount | method | |
cycle | method | |
frameAt | method | |
pivot | method | |
slice | method | |
sliceId | method | |
sliceName | method | |
sliceKeyAt | method | |
pivotOf | method | |
boundImage | method | |
bind | method | |
sprite | method |
idfield#
id: integerRead-only. Reports the registration index an Animation carries. Construction assigns an id once and never reuses it.
countfield#
count: integerRead-only. Reports the number of frames, which is the largest index rect, uv, and sprite accept.
imageWidthfield#
imageWidth: numberRead-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.
rectsfield#
rects: {number}Engine-owned. Stores each frame's pixel rectangle, four numbers a frame. Ordinary game code should call rect.
durationsfield#
durations: {number}Engine-owned. Stores each frame's duration in seconds. Construction converts the authored milliseconds once so playback never divides.
tagOrderfield#
tagOrder: {[integer]: {integer}}Engine-owned. Stores the frame shown at each step of a tag's cycle, which is what collapses the three directions into one lookup: the direction decides the order once, when the sheet is built.
tagTimingfield#
tagTiming: {[integer]: {number}}Engine-owned. Stores the elapsed seconds each step of a tag's cycle ends on.
rectmethod#
rect: function(self: Sheet, frame: integer): (number, number, number, number)Arguments
| Name | Type | Description |
|---|---|---|
self | Sheet | |
frame | integer |
Returns
| Type | Description |
|---|---|
number | |
number | |
number | |
number |
durationmethod#
duration: function(self: Sheet, frame: integer): numberArguments
| Name | Type | Description |
|---|---|---|
self | Sheet | |
frame | integer |
Returns
| Type | Description |
|---|---|
number |
uvmethod#
uv: function(self: Sheet, frame: integer): (number, number, number, number)Arguments
| Name | Type | Description |
|---|---|---|
self | Sheet | |
frame | integer |
Returns
| Type | Description |
|---|---|
number | |
number | |
number | |
number |
tagmethod#
Arguments
| Name | Type | Description |
|---|---|---|
self | Sheet | |
name | string |
Returns
| Type | Description |
|---|---|
integer | |
integer | |
Direction |
tagNamemethod#
tagName: function(self: Sheet, id: integer): stringArguments
| Name | Type | Description |
|---|---|---|
self | Sheet | |
id | integer |
Returns
| Type | Description |
|---|---|
string |
tagCountmethod#
tagCount: function(self: Sheet): integerArguments
| Name | Type | Description |
|---|---|---|
self | Sheet |
Returns
| Type | Description |
|---|---|
integer |
cyclemethod#
cycle: function(self: Sheet, id: integer): numberArguments
| Name | Type | Description |
|---|---|---|
self | Sheet | |
id | integer |
Returns
| Type | Description |
|---|---|
number |
frameAtmethod#
Arguments
| Name | Type | Description |
|---|---|---|
self | Sheet | |
id | integer | |
time | number |
Returns
| Type | Description |
|---|---|
integer |
pivotmethod#
pivot: function(
self: Sheet,
name: string,
frame: integer?
): components.FFIInstance<PivotValue, components.FFIComponent<PivotValue>>Arguments
| Name | Type | Description |
|---|---|---|
self | Sheet | |
name | string | |
frame | integer? |
Returns
| Type | Description |
|---|---|
components.FFIInstance<PivotValue, components.FFIComponent<PivotValue>> |
sliceNamemethod#
sliceName: function(self: Sheet, id: integer): stringArguments
| Name | Type | Description |
|---|---|---|
self | Sheet | |
id | integer |
Returns
| Type | Description |
|---|---|
string |
sliceKeyAtmethod#
Arguments
| Name | Type | Description |
|---|---|---|
self | Sheet | |
id | integer | |
frame | integer |
Returns
| Type | Description |
|---|---|
SliceKey? |
pivotOfmethod#
pivotOf: function(self: Sheet, id: integer, frame: integer): (number, number)Arguments
| Name | Type | Description |
|---|---|---|
self | Sheet | |
id | integer | |
frame | integer |
Returns
| Type | Description |
|---|---|
number | |
number |
boundImagemethod#
boundImage: function(self: Sheet): integerArguments
| Name | Type | Description |
|---|---|---|
self | Sheet |
Returns
| Type | Description |
|---|---|
integer |
bindmethod#
bind: function(self: Sheet, image: integer, u0: number?, v0: number?, u1: number?, v1: number?): SheetArguments
| Name | Type | Description |
|---|---|---|
self | Sheet | |
image | integer | |
u0 | number? | |
v0 | number? | |
u1 | number? | |
v1 | number? |
Returns
| Type | Description |
|---|---|
Sheet |
spritemethod#
sprite: function(
self: Sheet,
frame: integer?
): components.FFIInstance<rendercomponents.Sprite, components.FFIComponent<rendercomponents.Sprite>>Arguments
| Name | Type | Description |
|---|---|---|
self | Sheet | |
frame | integer? |
Returns
| Type | Description |
|---|---|
components.FFIInstance<rendercomponents.Sprite, components.FFIComponent<rendercomponents.Sprite>> |
Slicetype#
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#
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
| Name | Type | Description |
|---|---|---|
name | string | the name to register the finished sheet under |
imageWidth | number | the width in pixels of the image the frames are cut from |
imageHeight | number | the height in pixels of that image |
Returns
| Type | Description |
|---|---|
Builder | a builder whose methods chain |
Raises
| Type | Condition |
|---|---|
string | 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
| Name | Type | Description |
|---|---|---|
id | integer | an |
Returns
| Type | Description |
|---|---|
Sheet? | the sheet, or nil when nothing was built under the id |
byNamefunction#
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
| Name | Type | Description |
|---|---|---|
name | string | the name a sheet was built under |
Returns
| Type | Description |
|---|---|
Sheet? | the sheet most recently registered under the name, or nil |
fromAsepritefunction#
function fromAseprite(options: AsepriteOptions): SheetCreates 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
| Name | Type | Description |
|---|---|---|
options | AsepriteOptions | 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
| Type | Description |
|---|---|
Sheet | the finished sheet, already registered |
Raises
| Type | Condition |
|---|---|
string | 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): SheetCreates 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
| Name | Type | Description |
|---|---|---|
options | GridOptions | the image size, the cell size, and the optional grid shape, tags, and slices |
Returns
| Type | Description |
|---|---|
Sheet | the finished sheet, already registered under its name and carrying an |
Raises
| Type | Condition |
|---|---|
string | on a missing name, a non-positive image or frame size, a grid that fits no cells, or a |
rectsfunction#
function rects(options: RectsOptions): SheetCreates 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
| Name | Type | Description |
|---|---|---|
options | RectsOptions | the image size, the frame list, and the optional tags and slices |
Returns
| Type | Description |
|---|---|
Sheet | the finished sheet, already registered under its name and carrying an |
Raises
| Type | Condition |
|---|---|
string | on a missing name, a non-positive image size, an empty frame list, or a frame with no positive size |
replacefunction#
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
| Name | Type | Description |
|---|---|---|
built | Sheet | a sheet from any constructor here, registered moments ago under a name something else already held |
Returns
| Type | Description |
|---|---|
Sheet? | the live sheet, or nil and the refusal reason |
string? |
revisionfunction#
function revision(): integerReturns 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
| Type | Description |
|---|---|
integer | a count that only ever rises |
Values#
DEFAULT_DURATIONvariable#
const DEFAULT_DURATION: numberThe milliseconds a frame is held when nothing says otherwise, which is what Aseprite writes for a frame nobody retimed.
Pivotvariable#
const PivotAnchors a sprite directly, or follows a sheet slice. Snapshots store names.