@codexo/exojs-aseprite 0.16.2 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -27,7 +27,7 @@ export declare class AsepriteSheet {
|
|
|
27
27
|
/**
|
|
28
28
|
* Animation clips derived from the Aseprite `frameTags` metadata.
|
|
29
29
|
* Each clip's frames are live references into {@link spritesheet.frames};
|
|
30
|
-
* they are cloned automatically when passed to {@link AnimatedSprite.
|
|
30
|
+
* they are cloned automatically when passed to {@link AnimatedSprite.addClip}.
|
|
31
31
|
*/
|
|
32
32
|
readonly clips: ReadonlyMap<string, AnimatedSpriteClipDefinition>;
|
|
33
33
|
/**
|
|
@@ -51,12 +51,6 @@ export declare class AsepriteSheet {
|
|
|
51
51
|
* unique within an Aseprite document, so keying by name loses nothing.
|
|
52
52
|
*/
|
|
53
53
|
readonly layers: ReadonlyMap<string, AsepriteLayer>;
|
|
54
|
-
/**
|
|
55
|
-
* @internal - use {@link AsepriteSheet.parse} to create instances.
|
|
56
|
-
* The public modifier is required for the Loader's `AssetConstructor` token
|
|
57
|
-
* contract; users should call `parse()` instead of constructing directly.
|
|
58
|
-
*/
|
|
59
|
-
constructor(spritesheet: Spritesheet, clips: ReadonlyMap<string, AnimatedSpriteClipDefinition>, slices: ReadonlyMap<string, AsepriteSlice>, layers?: ReadonlyMap<string, AsepriteLayer>);
|
|
60
54
|
/**
|
|
61
55
|
* Parse a raw {@link AsepriteData} document and the already-loaded
|
|
62
56
|
* {@link Texture} into an {@link AsepriteSheet}.
|
|
@@ -88,7 +82,7 @@ export declare class AsepriteSheet {
|
|
|
88
82
|
/**
|
|
89
83
|
* Create an {@link AnimatedSprite} with all frame-tag clips pre-defined.
|
|
90
84
|
*
|
|
91
|
-
* Each clip is registered via {@link AnimatedSprite.
|
|
85
|
+
* Each clip is registered via {@link AnimatedSprite.addClip}, which
|
|
92
86
|
* clones the frame {@link Rectangle}s so the sprite owns its own copies.
|
|
93
87
|
* Call {@link AnimatedSprite.play} with a tag name to start playback.
|
|
94
88
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AsepriteSheet.d.ts","sourceRoot":"","sources":["../../src/AsepriteSheet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,4BAA4B,EAAE,WAAW,EAAE,KAAK,OAAO,
|
|
1
|
+
{"version":3,"file":"AsepriteSheet.d.ts","sourceRoot":"","sources":["../../src/AsepriteSheet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,4BAA4B,EAAE,WAAW,EAAE,KAAK,OAAO,EAAQ,MAAM,eAAe,CAAC;AAEnH,OAAO,EAAE,KAAK,YAAY,EAAiD,KAAK,aAAa,EAAE,KAAK,aAAa,EAAuB,MAAM,gBAAgB,CAAC;AA2G/J;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,aAAa;IACxB,iFAAiF;IACjF,SAAgB,WAAW,EAAE,WAAW,CAAC;IAEzC;;;;OAIG;IACH,SAAgB,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,4BAA4B,CAAC,CAAC;IAEzE;;;;;;;OAOG;IACH,SAAgB,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IAE3D;;;;;;;;;;OAUG;IACH,SAAgB,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IAmB3D;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;WACW,KAAK,CAAC,IAAI,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,GAAG,aAAa;IAiFxE;;;;;;OAMG;IACI,oBAAoB,IAAI,cAAc;IAU7C,kFAAkF;IAC3E,OAAO,IAAI,IAAI;CAGvB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isAsepriteArrayData } from "./AsepriteData.js";
|
|
2
|
-
import { AnimatedSprite, Spritesheet } from "@codexo/exojs";
|
|
2
|
+
import { AnimatedSprite, Spritesheet, Time } from "@codexo/exojs";
|
|
3
3
|
|
|
4
4
|
//#region src/AsepriteSheet.ts
|
|
5
5
|
/**
|
|
@@ -98,7 +98,7 @@ var AsepriteSheet = class AsepriteSheet {
|
|
|
98
98
|
/**
|
|
99
99
|
* Animation clips derived from the Aseprite `frameTags` metadata.
|
|
100
100
|
* Each clip's frames are live references into {@link spritesheet.frames};
|
|
101
|
-
* they are cloned automatically when passed to {@link AnimatedSprite.
|
|
101
|
+
* they are cloned automatically when passed to {@link AnimatedSprite.addClip}.
|
|
102
102
|
*/
|
|
103
103
|
clips;
|
|
104
104
|
/**
|
|
@@ -173,8 +173,7 @@ var AsepriteSheet = class AsepriteSheet {
|
|
|
173
173
|
if (frames.length === 0) continue;
|
|
174
174
|
const repeat = tag.repeat !== void 0 ? Number(tag.repeat) : -1;
|
|
175
175
|
const fps = avgFps(taggedFrames);
|
|
176
|
-
const
|
|
177
|
-
const frameDurations = taggedFrames.map(({ frameData }) => frameData.duration > 0 ? frameData.duration : avgDurationFallback);
|
|
176
|
+
const frameDurations = taggedFrames.map(({ frameData }) => Time.seconds(frameData.duration > 0 ? frameData.duration / 1e3 : 1 / fps));
|
|
178
177
|
const frameOffsets = taggedFrames.some(({ frameData }) => frameData.trimmed) ? taggedFrames.map(({ frameData }) => {
|
|
179
178
|
const { x, y } = frameData.spriteSourceSize;
|
|
180
179
|
return {
|
|
@@ -199,13 +198,13 @@ var AsepriteSheet = class AsepriteSheet {
|
|
|
199
198
|
/**
|
|
200
199
|
* Create an {@link AnimatedSprite} with all frame-tag clips pre-defined.
|
|
201
200
|
*
|
|
202
|
-
* Each clip is registered via {@link AnimatedSprite.
|
|
201
|
+
* Each clip is registered via {@link AnimatedSprite.addClip}, which
|
|
203
202
|
* clones the frame {@link Rectangle}s so the sprite owns its own copies.
|
|
204
203
|
* Call {@link AnimatedSprite.play} with a tag name to start playback.
|
|
205
204
|
*/
|
|
206
205
|
createAnimatedSprite() {
|
|
207
206
|
const sprite = new AnimatedSprite(this.spritesheet.texture);
|
|
208
|
-
for (const [name, clip] of this.clips) sprite.
|
|
207
|
+
for (const [name, clip] of this.clips) sprite.addClip(name, clip);
|
|
209
208
|
return sprite;
|
|
210
209
|
}
|
|
211
210
|
/** Destroy the underlying {@link Spritesheet} and release its frame resources. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AsepriteSheet.js","names":[],"sources":["../../src/AsepriteSheet.ts"],"sourcesContent":["import { AnimatedSprite, type AnimatedSpriteClipDefinition, Spritesheet, type Texture } from '@codexo/exojs';\n\nimport { type AsepriteData, type AsepriteFrameData, type AsepriteFrameTag, type AsepriteLayer, type AsepriteSlice, isAsepriteArrayData } from './AsepriteData';\n\n/**\n * Normalises an {@link AsepriteData} document into an ordered array of\n * {@link AsepriteFrameData} entries regardless of whether the JSON was\n * produced in array or hash mode.\n */\nconst normaliseFrames = (data: AsepriteData): AsepriteFrameData[] => {\n if (isAsepriteArrayData(data)) {\n return [...data.frames];\n }\n\n return Object.values(data.frames);\n};\n\n/**\n * Expands a frame tag's inclusive `[from, to]` range into the ordered\n * sequence of frame indices it actually plays, according to its\n * {@link AsepriteDirection}. Indices are not bounds-checked against the\n * frame array here; callers filter out-of-range entries separately.\n *\n * - `forward`: `[from, from+1, ..., to]`.\n * - `reverse`: `[to, to-1, ..., from]`.\n * - `pingpong`: a forward pass followed by a backward pass that excludes\n * both endpoints, e.g. `[0,1,2]` becomes `[0,1,2,1]`.\n * - `pingpong_reverse`: the mirrored shape, starting from `to`.\n * - A single-frame tag (`from === to`) always yields just that one frame.\n */\nconst expandFrameIndices = (tag: AsepriteFrameTag): number[] => {\n const { from, to } = tag;\n\n if (from === to) {\n return [from];\n }\n\n const indices: number[] = [];\n\n switch (tag.direction) {\n case 'reverse':\n for (let i = to; i >= from; i--) indices.push(i);\n break;\n\n case 'pingpong':\n for (let i = from; i <= to; i++) indices.push(i);\n for (let i = to - 1; i > from; i--) indices.push(i);\n break;\n\n case 'pingpong_reverse':\n for (let i = to; i >= from; i--) indices.push(i);\n for (let i = from + 1; i < to; i++) indices.push(i);\n break;\n\n case 'forward':\n default:\n for (let i = from; i <= to; i++) indices.push(i);\n break;\n }\n\n return indices;\n};\n\n/**\n * Calculates the average frames-per-second for a sequence of frame indices,\n * based on the per-frame `duration` field (milliseconds per frame) exported\n * by Aseprite. Every occurrence of an index counts toward the average - for\n * ping-pong sequences that means repeated (bounced) frames are weighted twice.\n * Falls back to `12` fps when all durations are zero or the sequence is empty.\n */\nconst avgFps = (frames: TaggedFrame[]): number => {\n if (frames.length === 0) {\n return 12;\n }\n\n const totalMs = frames.reduce((sum, { frameData }) => sum + frameData.duration, 0);\n const avgMs = totalMs / frames.length;\n\n return avgMs > 0 ? 1000 / avgMs : 12;\n};\n\n/**\n * A frame a tag actually resolved to, paired with the index it came from.\n * Resolving index and data together is what keeps every per-frame array below\n * (durations, offsets, spritesheet lookups) aligned without re-indexing.\n */\ninterface TaggedFrame {\n index: number;\n frameData: AsepriteFrameData;\n}\n\n/**\n * Resolves a tag's frame indices against the frame array, dropping any that\n * fall outside it - Aseprite exports can reference frames a later edit removed.\n */\nconst resolveTaggedFrames = (frameArray: AsepriteFrameData[], indices: number[]): TaggedFrame[] => {\n const resolved: TaggedFrame[] = [];\n\n for (const index of indices) {\n const frameData = frameArray[index];\n\n if (frameData !== undefined) {\n resolved.push({ index, frameData });\n }\n }\n\n return resolved;\n};\n\n/**\n * Parsed representation of an Aseprite JSON sprite sheet export.\n *\n * `AsepriteSheet.parse(data, texture)` converts the raw JSON document into:\n * - A {@link Spritesheet} whose frames correspond to the Aseprite frame array\n * (keyed by zero-based index string: `\"0\"`, `\"1\"`, ...).\n * - A `clips` map of {@link AnimatedSpriteClipDefinition} entries built from\n * `meta.frameTags`, one per named tag.\n * - The `slices` and `layers` metadata maps, carried through verbatim.\n *\n * Call {@link createAnimatedSprite} to obtain a ready-to-use\n * {@link AnimatedSprite} with all clips pre-registered.\n *\n * @example\n * ```ts\n * const sheet = await loader.load(Asset.type('asepriteSheet', 'hero.aseprite.json'));\n * const sprite = sheet.createAnimatedSprite();\n * sprite.play('run');\n * scene.addChild(sprite);\n * ```\n */\nexport class AsepriteSheet {\n /** The underlying {@link Spritesheet} whose frames are keyed by index string. */\n public readonly spritesheet: Spritesheet;\n\n /**\n * Animation clips derived from the Aseprite `frameTags` metadata.\n * Each clip's frames are live references into {@link spritesheet.frames};\n * they are cloned automatically when passed to {@link AnimatedSprite.defineClip}.\n */\n public readonly clips: ReadonlyMap<string, AnimatedSpriteClipDefinition>;\n\n /**\n * Named slices from the Aseprite `meta.slices` metadata, keyed by slice\n * name. Slices describe editor-defined regions - hitboxes, nine-patch\n * borders, UI anchor points - that aren't part of the frame/animation\n * data itself. Each {@link AsepriteSlice} carries one {@link AsepriteSliceKey}\n * per frame at which its bounds change; consumers resolve the applicable\n * key for a given frame index themselves.\n */\n public readonly slices: ReadonlyMap<string, AsepriteSlice>;\n\n /**\n * Layers from the Aseprite `meta.layers` metadata, keyed by layer name and\n * in export order (bottom-most first). Aseprite packs the sheet already\n * composited, so these are descriptive rather than renderable: they tell a\n * consumer which layers went into a frame and how - opacity, blend mode,\n * editor user data - and {@link AsepriteLayer.group} names the enclosing\n * group layer, so the flat map still encodes the layer tree.\n *\n * Empty when the export carries no `meta.layers` block. Layer names are\n * unique within an Aseprite document, so keying by name loses nothing.\n */\n public readonly layers: ReadonlyMap<string, AsepriteLayer>;\n\n /**\n * @internal - use {@link AsepriteSheet.parse} to create instances.\n * The public modifier is required for the Loader's `AssetConstructor` token\n * contract; users should call `parse()` instead of constructing directly.\n */\n public constructor(\n spritesheet: Spritesheet,\n clips: ReadonlyMap<string, AnimatedSpriteClipDefinition>,\n slices: ReadonlyMap<string, AsepriteSlice>,\n layers: ReadonlyMap<string, AsepriteLayer> = new Map(),\n ) {\n this.spritesheet = spritesheet;\n this.clips = clips;\n this.slices = slices;\n this.layers = layers;\n }\n\n /**\n * Parse a raw {@link AsepriteData} document and the already-loaded\n * {@link Texture} into an {@link AsepriteSheet}.\n *\n * Supports both Aseprite array mode and hash mode. Frame indices from\n * `frameTags` are resolved against the ordered frame array; out-of-range\n * indices are silently skipped.\n *\n * A tag's `direction` determines the expanded frame sequence fed into the\n * clip - `forward` and `reverse` play the `[from, to]` range in order or\n * in reverse, while `pingpong`/`pingpong_reverse` append a backward pass\n * (excluding both endpoints) so the bounce plays back correctly on the\n * engine's forward-only {@link AnimatedSprite} playback. The tag's\n * `repeat` field maps directly onto {@link AnimatedSpriteClipDefinition.repeat}:\n * absent means the clip loops indefinitely (`repeat: -1`); a numeric\n * string (`\"1\"`, `\"2\"`, ...) means it plays exactly that many full cycles\n * before stopping.\n *\n * Each clip's `frameDurations` carries the real per-frame `duration` from\n * the export (falling back to the tag's average when a frame's duration is\n * non-positive), so uneven hold-frames survive into playback instead of\n * being flattened to a uniform fps. `frameOffsets` carries each frame's\n * `spriteSourceSize` `{x,y}` - its trimmed content's offset within the\n * untrimmed canvas - whenever any frame in the tag is trimmed, so frames\n * trimmed by different amounts stay anchored instead of jittering; it's\n * omitted entirely for tags with no trimmed frames.\n */\n public static parse(data: AsepriteData, texture: Texture): AsepriteSheet {\n const frameArray = normaliseFrames(data);\n\n // Build SpritesheetData: frame names are zero-based index strings.\n const spritesheetFrames: Record<string, { frame: { x: number; y: number; w: number; h: number } }> = {};\n\n for (const [i, frameData] of frameArray.entries()) {\n spritesheetFrames[String(i)] = { frame: frameData.frame };\n }\n\n const spritesheet = new Spritesheet(texture, { frames: spritesheetFrames });\n\n // Build clips from frameTags, resolving frame indices into Rectangles.\n const clips = new Map<string, AnimatedSpriteClipDefinition>();\n const frameTags = data.meta.frameTags ?? [];\n\n for (const tag of frameTags) {\n // Out-of-range indices are silently skipped; `taggedFrames` parallels\n // `frames` exactly, so it's the basis for every other per-frame array\n // (durations, offsets) built below.\n const taggedFrames = resolveTaggedFrames(frameArray, expandFrameIndices(tag));\n const frames = taggedFrames.map(({ index }) => spritesheet.getFrame(String(index)));\n\n if (frames.length === 0) {\n continue;\n }\n\n // Aseprite's `tag.repeat` (a numeric string, `'1'` through any N) maps\n // directly onto the engine's `repeat` count. Absent means the tag\n // loops indefinitely, the engine's `-1` sentinel.\n const repeat = tag.repeat !== undefined ? Number(tag.repeat) : -1;\n const fps = avgFps(taggedFrames);\n\n // Per-frame hold duration (Aseprite \"duration\"), so uneven hold-frames\n // (e.g. a lingering idle frame) survive into playback instead of being\n // flattened to the tag's average fps. A non-positive duration (same\n // degenerate case `avgFps` guards against) falls back to the average.\n const avgDurationFallback = 1000 / fps;\n const frameDurations = taggedFrames.map(({ frameData }) => (frameData.duration > 0 ? frameData.duration : avgDurationFallback));\n\n // Per-frame trim offset (Aseprite \"spriteSourceSize\"), so frames trimmed\n // by different amounts stay anchored to the same point in the untrimmed\n // canvas instead of jittering frame to frame. Omitted entirely when no\n // frame in the tag is trimmed, to avoid noise on untrimmed sheets.\n const anyTrimmed = taggedFrames.some(({ frameData }) => frameData.trimmed);\n const frameOffsets = anyTrimmed\n ? taggedFrames.map(({ frameData }) => {\n const { x, y } = frameData.spriteSourceSize;\n\n return { x, y };\n })\n : undefined;\n\n clips.set(tag.name, {\n fps,\n frames,\n repeat,\n frameDurations,\n ...(frameOffsets ? { frameOffsets } : {}),\n });\n }\n\n // Build the slices map from meta.slices, keyed by slice name.\n const slices = new Map<string, AsepriteSlice>();\n\n for (const slice of data.meta.slices ?? []) {\n slices.set(slice.name, slice);\n }\n\n // Same for meta.layers - insertion order is the export order, so the map\n // doubles as the ordered layer list.\n const layers = new Map<string, AsepriteLayer>();\n\n for (const layer of data.meta.layers ?? []) {\n layers.set(layer.name, layer);\n }\n\n return new AsepriteSheet(spritesheet, clips, slices, layers);\n }\n\n /**\n * Create an {@link AnimatedSprite} with all frame-tag clips pre-defined.\n *\n * Each clip is registered via {@link AnimatedSprite.defineClip}, which\n * clones the frame {@link Rectangle}s so the sprite owns its own copies.\n * Call {@link AnimatedSprite.play} with a tag name to start playback.\n */\n public createAnimatedSprite(): AnimatedSprite {\n const sprite = new AnimatedSprite(this.spritesheet.texture);\n\n for (const [name, clip] of this.clips) {\n sprite.defineClip(name, clip);\n }\n\n return sprite;\n }\n\n /** Destroy the underlying {@link Spritesheet} and release its frame resources. */\n public destroy(): void {\n this.spritesheet.destroy();\n }\n}\n"],"mappings":";;;;;;;;;AASA,MAAM,mBAAmB,SAA4C;CACnE,IAAI,oBAAoB,IAAI,GAC1B,OAAO,CAAC,GAAG,KAAK,MAAM;CAGxB,OAAO,OAAO,OAAO,KAAK,MAAM;AAClC;;;;;;;;;;;;;;AAeA,MAAM,sBAAsB,QAAoC;CAC9D,MAAM,EAAE,MAAM,OAAO;CAErB,IAAI,SAAS,IACX,OAAO,CAAC,IAAI;CAGd,MAAM,UAAoB,CAAC;CAE3B,QAAQ,IAAI,WAAZ;EACE,KAAK;GACH,KAAK,IAAI,IAAI,IAAI,KAAK,MAAM,KAAK,QAAQ,KAAK,CAAC;GAC/C;EAEF,KAAK;GACH,KAAK,IAAI,IAAI,MAAM,KAAK,IAAI,KAAK,QAAQ,KAAK,CAAC;GAC/C,KAAK,IAAI,IAAI,KAAK,GAAG,IAAI,MAAM,KAAK,QAAQ,KAAK,CAAC;GAClD;EAEF,KAAK;GACH,KAAK,IAAI,IAAI,IAAI,KAAK,MAAM,KAAK,QAAQ,KAAK,CAAC;GAC/C,KAAK,IAAI,IAAI,OAAO,GAAG,IAAI,IAAI,KAAK,QAAQ,KAAK,CAAC;GAClD;EAGF,SACE,KAAK,IAAI,IAAI,MAAM,KAAK,IAAI,KAAK,QAAQ,KAAK,CAAC;CAEnD;CAEA,OAAO;AACT;;;;;;;;AASA,MAAM,UAAU,WAAkC;CAChD,IAAI,OAAO,WAAW,GACpB,OAAO;CAIT,MAAM,QADU,OAAO,QAAQ,KAAK,EAAE,gBAAgB,MAAM,UAAU,UAAU,CAC5D,IAAI,OAAO;CAE/B,OAAO,QAAQ,IAAI,MAAO,QAAQ;AACpC;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UAAU,WAAW,IAAI,UAAU,WAAW,mBAAoB;GAO9H,MAAM,eADa,aAAa,MAAM,EAAE,gBAAgB,UAAU,OACpC,IAC1B,aAAa,KAAK,EAAE,gBAAgB;IAClC,MAAM,EAAE,GAAG,MAAM,UAAU;IAE3B,OAAO;KAAE;KAAG;IAAE;GAChB,CAAC,IACD;GAEJ,MAAM,IAAI,IAAI,MAAM;IAClB;IACA;IACA;IACA;IACA,GAAI,eAAe,EAAE,aAAa,IAAI,CAAC;GACzC,CAAC;EACH;EAGA,MAAM,yBAAS,IAAI,IAA2B;EAE9C,KAAK,MAAM,SAAS,KAAK,KAAK,UAAU,CAAC,GACvC,OAAO,IAAI,MAAM,MAAM,KAAK;EAK9B,MAAM,yBAAS,IAAI,IAA2B;EAE9C,KAAK,MAAM,SAAS,KAAK,KAAK,UAAU,CAAC,GACvC,OAAO,IAAI,MAAM,MAAM,KAAK;EAG9B,OAAO,IAAI,cAAc,aAAa,OAAO,QAAQ,MAAM;CAC7D;;;;;;;;CASA,AAAO,uBAAuC;EAC5C,MAAM,SAAS,IAAI,eAAe,KAAK,YAAY,OAAO;EAE1D,KAAK,MAAM,CAAC,MAAM,SAAS,KAAK,OAC9B,OAAO,WAAW,MAAM,IAAI;EAG9B,OAAO;CACT;;CAGA,AAAO,UAAgB;EACrB,KAAK,YAAY,QAAQ;CAC3B;AACF"}
|
|
1
|
+
{"version":3,"file":"AsepriteSheet.js","names":[],"sources":["../../src/AsepriteSheet.ts"],"sourcesContent":["import { AnimatedSprite, type AnimatedSpriteClipDefinition, Spritesheet, type Texture, Time } from '@codexo/exojs';\n\nimport { type AsepriteData, type AsepriteFrameData, type AsepriteFrameTag, type AsepriteLayer, type AsepriteSlice, isAsepriteArrayData } from './AsepriteData';\n\n/**\n * Normalises an {@link AsepriteData} document into an ordered array of\n * {@link AsepriteFrameData} entries regardless of whether the JSON was\n * produced in array or hash mode.\n */\nconst normaliseFrames = (data: AsepriteData): AsepriteFrameData[] => {\n if (isAsepriteArrayData(data)) {\n return [...data.frames];\n }\n\n return Object.values(data.frames);\n};\n\n/**\n * Expands a frame tag's inclusive `[from, to]` range into the ordered\n * sequence of frame indices it actually plays, according to its\n * {@link AsepriteDirection}. Indices are not bounds-checked against the\n * frame array here; callers filter out-of-range entries separately.\n *\n * - `forward`: `[from, from+1, ..., to]`.\n * - `reverse`: `[to, to-1, ..., from]`.\n * - `pingpong`: a forward pass followed by a backward pass that excludes\n * both endpoints, e.g. `[0,1,2]` becomes `[0,1,2,1]`.\n * - `pingpong_reverse`: the mirrored shape, starting from `to`.\n * - A single-frame tag (`from === to`) always yields just that one frame.\n */\nconst expandFrameIndices = (tag: AsepriteFrameTag): number[] => {\n const { from, to } = tag;\n\n if (from === to) {\n return [from];\n }\n\n const indices: number[] = [];\n\n switch (tag.direction) {\n case 'reverse':\n for (let i = to; i >= from; i--) indices.push(i);\n break;\n\n case 'pingpong':\n for (let i = from; i <= to; i++) indices.push(i);\n for (let i = to - 1; i > from; i--) indices.push(i);\n break;\n\n case 'pingpong_reverse':\n for (let i = to; i >= from; i--) indices.push(i);\n for (let i = from + 1; i < to; i++) indices.push(i);\n break;\n\n case 'forward':\n default:\n for (let i = from; i <= to; i++) indices.push(i);\n break;\n }\n\n return indices;\n};\n\n/**\n * Calculates the average frames-per-second for a sequence of frame indices,\n * based on the per-frame `duration` field (milliseconds per frame) exported\n * by Aseprite. Every occurrence of an index counts toward the average - for\n * ping-pong sequences that means repeated (bounced) frames are weighted twice.\n * Falls back to `12` fps when all durations are zero or the sequence is empty.\n */\nconst avgFps = (frames: TaggedFrame[]): number => {\n if (frames.length === 0) {\n return 12;\n }\n\n const totalMs = frames.reduce((sum, { frameData }) => sum + frameData.duration, 0);\n const avgMs = totalMs / frames.length;\n\n return avgMs > 0 ? 1000 / avgMs : 12;\n};\n\n/**\n * A frame a tag actually resolved to, paired with the index it came from.\n * Resolving index and data together is what keeps every per-frame array below\n * (durations, offsets, spritesheet lookups) aligned without re-indexing.\n */\ninterface TaggedFrame {\n index: number;\n frameData: AsepriteFrameData;\n}\n\n/**\n * Resolves a tag's frame indices against the frame array, dropping any that\n * fall outside it - Aseprite exports can reference frames a later edit removed.\n */\nconst resolveTaggedFrames = (frameArray: AsepriteFrameData[], indices: number[]): TaggedFrame[] => {\n const resolved: TaggedFrame[] = [];\n\n for (const index of indices) {\n const frameData = frameArray[index];\n\n if (frameData !== undefined) {\n resolved.push({ index, frameData });\n }\n }\n\n return resolved;\n};\n\n/**\n * Parsed representation of an Aseprite JSON sprite sheet export.\n *\n * `AsepriteSheet.parse(data, texture)` converts the raw JSON document into:\n * - A {@link Spritesheet} whose frames correspond to the Aseprite frame array\n * (keyed by zero-based index string: `\"0\"`, `\"1\"`, ...).\n * - A `clips` map of {@link AnimatedSpriteClipDefinition} entries built from\n * `meta.frameTags`, one per named tag.\n * - The `slices` and `layers` metadata maps, carried through verbatim.\n *\n * Call {@link createAnimatedSprite} to obtain a ready-to-use\n * {@link AnimatedSprite} with all clips pre-registered.\n *\n * @example\n * ```ts\n * const sheet = await loader.load(Asset.type('asepriteSheet', 'hero.aseprite.json'));\n * const sprite = sheet.createAnimatedSprite();\n * sprite.play('run');\n * scene.addChild(sprite);\n * ```\n */\nexport class AsepriteSheet {\n /** The underlying {@link Spritesheet} whose frames are keyed by index string. */\n public readonly spritesheet: Spritesheet;\n\n /**\n * Animation clips derived from the Aseprite `frameTags` metadata.\n * Each clip's frames are live references into {@link spritesheet.frames};\n * they are cloned automatically when passed to {@link AnimatedSprite.addClip}.\n */\n public readonly clips: ReadonlyMap<string, AnimatedSpriteClipDefinition>;\n\n /**\n * Named slices from the Aseprite `meta.slices` metadata, keyed by slice\n * name. Slices describe editor-defined regions - hitboxes, nine-patch\n * borders, UI anchor points - that aren't part of the frame/animation\n * data itself. Each {@link AsepriteSlice} carries one {@link AsepriteSliceKey}\n * per frame at which its bounds change; consumers resolve the applicable\n * key for a given frame index themselves.\n */\n public readonly slices: ReadonlyMap<string, AsepriteSlice>;\n\n /**\n * Layers from the Aseprite `meta.layers` metadata, keyed by layer name and\n * in export order (bottom-most first). Aseprite packs the sheet already\n * composited, so these are descriptive rather than renderable: they tell a\n * consumer which layers went into a frame and how - opacity, blend mode,\n * editor user data - and {@link AsepriteLayer.group} names the enclosing\n * group layer, so the flat map still encodes the layer tree.\n *\n * Empty when the export carries no `meta.layers` block. Layer names are\n * unique within an Aseprite document, so keying by name loses nothing.\n */\n public readonly layers: ReadonlyMap<string, AsepriteLayer>;\n\n /**\n * @internal - use {@link AsepriteSheet.parse} to create instances.\n * The public modifier is required for the Loader's `AssetConstructor` token\n * contract; users should call `parse()` instead of constructing directly.\n */\n public constructor(\n spritesheet: Spritesheet,\n clips: ReadonlyMap<string, AnimatedSpriteClipDefinition>,\n slices: ReadonlyMap<string, AsepriteSlice>,\n layers: ReadonlyMap<string, AsepriteLayer> = new Map(),\n ) {\n this.spritesheet = spritesheet;\n this.clips = clips;\n this.slices = slices;\n this.layers = layers;\n }\n\n /**\n * Parse a raw {@link AsepriteData} document and the already-loaded\n * {@link Texture} into an {@link AsepriteSheet}.\n *\n * Supports both Aseprite array mode and hash mode. Frame indices from\n * `frameTags` are resolved against the ordered frame array; out-of-range\n * indices are silently skipped.\n *\n * A tag's `direction` determines the expanded frame sequence fed into the\n * clip - `forward` and `reverse` play the `[from, to]` range in order or\n * in reverse, while `pingpong`/`pingpong_reverse` append a backward pass\n * (excluding both endpoints) so the bounce plays back correctly on the\n * engine's forward-only {@link AnimatedSprite} playback. The tag's\n * `repeat` field maps directly onto {@link AnimatedSpriteClipDefinition.repeat}:\n * absent means the clip loops indefinitely (`repeat: -1`); a numeric\n * string (`\"1\"`, `\"2\"`, ...) means it plays exactly that many full cycles\n * before stopping.\n *\n * Each clip's `frameDurations` carries the real per-frame `duration` from\n * the export (falling back to the tag's average when a frame's duration is\n * non-positive), so uneven hold-frames survive into playback instead of\n * being flattened to a uniform fps. `frameOffsets` carries each frame's\n * `spriteSourceSize` `{x,y}` - its trimmed content's offset within the\n * untrimmed canvas - whenever any frame in the tag is trimmed, so frames\n * trimmed by different amounts stay anchored instead of jittering; it's\n * omitted entirely for tags with no trimmed frames.\n */\n public static parse(data: AsepriteData, texture: Texture): AsepriteSheet {\n const frameArray = normaliseFrames(data);\n\n // Build SpritesheetData: frame names are zero-based index strings.\n const spritesheetFrames: Record<string, { frame: { x: number; y: number; w: number; h: number } }> = {};\n\n for (const [i, frameData] of frameArray.entries()) {\n spritesheetFrames[String(i)] = { frame: frameData.frame };\n }\n\n const spritesheet = new Spritesheet(texture, { frames: spritesheetFrames });\n\n // Build clips from frameTags, resolving frame indices into Rectangles.\n const clips = new Map<string, AnimatedSpriteClipDefinition>();\n const frameTags = data.meta.frameTags ?? [];\n\n for (const tag of frameTags) {\n // Out-of-range indices are silently skipped; `taggedFrames` parallels\n // `frames` exactly, so it's the basis for every other per-frame array\n // (durations, offsets) built below.\n const taggedFrames = resolveTaggedFrames(frameArray, expandFrameIndices(tag));\n const frames = taggedFrames.map(({ index }) => spritesheet.getFrame(String(index)));\n\n if (frames.length === 0) {\n continue;\n }\n\n // Aseprite's `tag.repeat` (a numeric string, `'1'` through any N) maps\n // directly onto the engine's `repeat` count. Absent means the tag\n // loops indefinitely, the engine's `-1` sentinel.\n const repeat = tag.repeat !== undefined ? Number(tag.repeat) : -1;\n const fps = avgFps(taggedFrames);\n\n // Per-frame hold duration (Aseprite \"duration\"), so uneven hold-frames\n // (e.g. a lingering idle frame) survive into playback instead of being\n // flattened to the tag's average fps. A non-positive duration (same\n // degenerate case `avgFps` guards against) falls back to the average -\n // computed as `1 / fps` directly, not `(1000 / fps) / 1000`, so it is\n // bit-for-bit the reciprocal of the fps this clip actually carries.\n const frameDurations = taggedFrames.map(({ frameData }) => Time.seconds(frameData.duration > 0 ? frameData.duration / 1000 : 1 / fps));\n\n // Per-frame trim offset (Aseprite \"spriteSourceSize\"), so frames trimmed\n // by different amounts stay anchored to the same point in the untrimmed\n // canvas instead of jittering frame to frame. Omitted entirely when no\n // frame in the tag is trimmed, to avoid noise on untrimmed sheets.\n const anyTrimmed = taggedFrames.some(({ frameData }) => frameData.trimmed);\n const frameOffsets = anyTrimmed\n ? taggedFrames.map(({ frameData }) => {\n const { x, y } = frameData.spriteSourceSize;\n\n return { x, y };\n })\n : undefined;\n\n clips.set(tag.name, {\n fps,\n frames,\n repeat,\n frameDurations,\n ...(frameOffsets ? { frameOffsets } : {}),\n });\n }\n\n // Build the slices map from meta.slices, keyed by slice name.\n const slices = new Map<string, AsepriteSlice>();\n\n for (const slice of data.meta.slices ?? []) {\n slices.set(slice.name, slice);\n }\n\n // Same for meta.layers - insertion order is the export order, so the map\n // doubles as the ordered layer list.\n const layers = new Map<string, AsepriteLayer>();\n\n for (const layer of data.meta.layers ?? []) {\n layers.set(layer.name, layer);\n }\n\n return new AsepriteSheet(spritesheet, clips, slices, layers);\n }\n\n /**\n * Create an {@link AnimatedSprite} with all frame-tag clips pre-defined.\n *\n * Each clip is registered via {@link AnimatedSprite.addClip}, which\n * clones the frame {@link Rectangle}s so the sprite owns its own copies.\n * Call {@link AnimatedSprite.play} with a tag name to start playback.\n */\n public createAnimatedSprite(): AnimatedSprite {\n const sprite = new AnimatedSprite(this.spritesheet.texture);\n\n for (const [name, clip] of this.clips) {\n sprite.addClip(name, clip);\n }\n\n return sprite;\n }\n\n /** Destroy the underlying {@link Spritesheet} and release its frame resources. */\n public destroy(): void {\n this.spritesheet.destroy();\n }\n}\n"],"mappings":";;;;;;;;;AASA,MAAM,mBAAmB,SAA4C;CACnE,IAAI,oBAAoB,IAAI,GAC1B,OAAO,CAAC,GAAG,KAAK,MAAM;CAGxB,OAAO,OAAO,OAAO,KAAK,MAAM;AAClC;;;;;;;;;;;;;;AAeA,MAAM,sBAAsB,QAAoC;CAC9D,MAAM,EAAE,MAAM,OAAO;CAErB,IAAI,SAAS,IACX,OAAO,CAAC,IAAI;CAGd,MAAM,UAAoB,CAAC;CAE3B,QAAQ,IAAI,WAAZ;EACE,KAAK;GACH,KAA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MAAM,KAAK;EAG9B,OAAO,IAAI,cAAc,aAAa,OAAO,QAAQ,MAAM;CAC7D;;;;;;;;CASA,AAAO,uBAAuC;EAC5C,MAAM,SAAS,IAAI,eAAe,KAAK,YAAY,OAAO;EAE1D,KAAK,MAAM,CAAC,MAAM,SAAS,KAAK,OAC9B,OAAO,QAAQ,MAAM,IAAI;EAG3B,OAAO;CACT;;CAGA,AAAO,UAAgB;EACrB,KAAK,YAAY,QAAQ;CAC3B;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codexo/exojs-aseprite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "Aseprite sprite sheet asset extension for ExoJS.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
"LICENSE"
|
|
27
27
|
],
|
|
28
28
|
"peerDependencies": {
|
|
29
|
-
"@codexo/exojs": "0.
|
|
29
|
+
"@codexo/exojs": "0.17.x"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@codexo/exojs
|
|
33
|
-
"@codexo/exojs": "0.
|
|
32
|
+
"@codexo/exojs": "0.17.0",
|
|
33
|
+
"@codexo/exojs-config": "0.0.0"
|
|
34
34
|
},
|
|
35
35
|
"license": "MIT",
|
|
36
36
|
"publishConfig": {
|