0.0.18 • Published 7 months ago

ase-parser v0.0.18

Weekly downloads
4
License
MIT
Repository
github
Last release
7 months ago

npm npm install size

ase-parser

Parse Aseprite files with Node.js, no external dependencies.

Install

To install for use:

npm i ase-parse

Instructions

You'll probably want to get the Buffer of the Aseprite file in whatever way you feel like. For the example, we'll just use fs.readFileSync(). But you can get it from a network request, etc.

const Aseprite = require('ase-parser');
const fs = require('fs');

const buff = fs.readFileSync('./somefile.aseprite');
const aseFile = new Aseprite(buff, 'somefile.aseprite');

aseFile.parse();
console.log(aseFile.numFrames);

After parsing, you can get the data and do something like generate an image with the sharp npm lib, which accepts raw pixel data and supports image composition.

Here is a more advanced example generating a png image of the first frame using the sharp lib.

const Aseprite = require('ase-parser');
const fs = require('fs');
const sharp = require('sharp');

async function makePNG() {
  const buff = fs.readFileSync('./my_chocobo.aseprite');
  const ase = new Aseprite(buff, 'my_chocobo.aseprite');
  
  ase.parse();
  // Create a blank png image buffer that's the same size as the Aseprite sprite (only make the promise because we'll use Promise.all a little later)
  const bgPromise = sharp({create: {
    width: ase.width,
    height: ase.height,
    channels: 4,
    background: {r: 0, g: 0, b: 0, alpha: 0}
  }}).png().toBuffer();
  
  // Get the cels for the first frame
  const cels = ase.frames[0].cels
    // copy the array
    .map(a => a)
    .sort((a, b) => {
      const orderA = a.layerIndex + a.zIndex;
      const orderB = b.layerIndex + b.zIndex;
      // sort by order, then by zIndex
      return orderA - orderB || a.zIndex - b.zIndex;
    })
  
  // Create png image buffers per cel to create an image of the first frame (creating the Promises to be used)
  const otherPromises = cels.map(cel => {
    return sharp(cel.rawCelData, {raw: {width: cel.w, height: cel.h, channels: 4}}).png().toBuffer();
  });
  
  // Run the promises all at once to get the buffers for the base image and the cels to combine
  const [ bg, ...others ] = await Promise.all([bgPromise, ...otherPromises]).catch(console.log);
  
  // take the first image and add on the png buffers on top of it (the cels should be in order from bottom to top from the parse)
  const finalBuff = await sharp(bg)
    .composite(others.map((img, index) => ({
      input: img,
      top: cels[index].ypos,
      left: cels[index].xpos
    })))
    .png()
    .toBuffer();
  // saves the file as a png with the buffer from sharp.composite
  fs.writeFileSync(ase.name.replace('.aseprite', '.png'), finalBuff);
}

makePNG();

Aseprite Functions

constructor

Parameters:

  • buffer: Expects a Node.js Buffer of the Aseprite file.
  • name: Expects a string that's the name of the Aseprite file, including the extension.

    Returns:

  • Aseprite: Returns Aseprite.

    Example:

    const Aseprite = require('ase-parser');
    const fs = require('fs');

const buff = fs.readFileSync('./somefile.aseprite'); const aseFile = new Aseprite(buff, 'somefile.aseprite');

### `parse`
Description:  
Parses the Aseprite file and populates the `Aseprite` class with the information from the file.

Parameters:
 * none

 Returns:
 * none

Example:
 ```js
const Aseprite = require('ase-parser');
const fs = require('fs');

const buff = fs.readFileSync('./somefile.aseprite');
const aseFile = new Aseprite(buff, 'somefile.aseprite');

aseFile.parse();

Aseprite Object

FieldTypeDescription
framesarray of frame objectsframes
layersarray of layer objectslayers
fileSizeintegersize of the file (in bytes)
numFramesintegernumber of frames the Aseprite file has
widthintegerwidth (in pixels)
heightintegerheight (in pixels)
colorDepthintegercolor depth (in bits per pixel)
paletteIndexintegerposition of the indexed color based on the palette
numColorsintegernumber of colors
pixelRatiostringwidth:height
namestringname of the file
tagsarry of tag objectstags
colorProfilecolorProfile objectColor profile
palettepalette objectPalette
tilesetsarray of tileset objectsTileset
slicesarray of slice objectsInfo on slices

Frame Object

FieldTypeDescription
bytesInFrameintegersize (in bytes)
frameDurationintegerduration (in ms)
celsarray of cel objectscels

Layer Object

FieldTypeDescription
flagslayer flagsflags for the layer translated into a map
typeintegertype
layerChildLevelintegerlayer child level
opacityintegeropacity (0-255)
tilesetIndex?integerthe tileset id, if applicable
namestringname of layer

Tag Object

FieldTypeDescription
fromintegerfirst frame index
tointegerlast frame index
animDirectionstringForward, Reverse, Ping-pong or Ping-pong Reverse
repeatintegerrepeat animation N times
colorstringhex color of the tag (no # included)
namestringname

Color Profile Object

FieldTypeDescription
typestringNone, sRGB or ICC
flagintegerfixed gamma flag
fGammaintegerfixed gamma
icc?bufferICC profile data

Palette Object

FieldTypeDescription
paletteSizeintegernumber of colors
firstColorintegerindex of the first color
lastColorintegerindex of the last color
colorsarray of color objectscolors
index?integerposition of the indexed color based on the palette

Tileset Object

FieldTypeDescription
idintegertileset id number
tileCountintegernumber of tiles
tileWidthintegerpixel width of each tile
tileHeightintegerpixel height ofeach tile
namestringname
externalFile?tileset external file objectexternal file linkage info, if applicable
rawTilesetData?Bufferraw pixel data for tiles, if applicable

Tileset External File Object

FieldTypeDescription
idintegerid of the external file
tilesetIdintegerid of the tileset in the external file

Cel Object

FieldTypeDescription
layerIndexintegerindex of the layer associated
xposintegerx position of the cel compared to the sprite
yposintegery position of the cel compared to the sprite
opacityintegeropacity (0-255)
celTypeintegerinternally used
zIndexintegershow this cel N layers later/back
wintegerwidth (in pixels)
hintegerheight (in pixels)
tilemapMetadata?tilemap metadata objecttilemap metadata, if applicable
rawCelDataBufferraw cel pixel data

Tilemap Metadata Object

FieldTypeDescription
bitsPerTileintegernumber of bits used to represent each tile (usually 32)
bitmaskForTileIdintegerwhich bit(s) represent the tile ID
bitmaskForXFlipintegerwhich bit(s) indicate X-axis flip
bitmaskForYFlipintegerwhich bit(s) indicate Y-axis flip
bitmaskFor90CWRotationintegerwhich bit(s) indicate 90-degree clockwise rotation flip

Color Object

FieldTypeDescription
redintegerred value (0-255)
greenintegergreen value (0-255)
blueintegerblue value (0-255)
alphaintegeralpha value (0-255)
namestring'none' or the actual color name if it has one

Slice Object

FieldTypeDescription
flagsintegerFlags set
keysarray of SliceKey objectsArray of keys and their values
namestringName of the slice

SliceKey Object

FieldTypeDescription
frameNumberintegerFrame number that the slice is from
xintegerX position of the slice
yintegerY position of the slice
widthintegerWidth of the slice
heightintegerHeight of the slice
patch?patch objectPatch info on the slice
pivot?pivot objectPivot info on the slice

Patch Object

FieldTypeDescription
xintegerX postion of the patch
yintegerY position of the patch
widthintegerWidth of the patch
heightintegerHeight of the patch

Pivot Object

FieldTypeDescription
xintegerX position of the pivot
yintegerY position of the pivot

Layer Flags Object

This object is a utility object that will have ALL fields present. If a field is "on" it will be true, if the field is "off" it will be false.

FieldDescription
visibleWhether or not the layer is visible
editableWhether or not the layer is editable
lockMovementWhether or not the layer is a background layer
preferLinkedCelsWhether or not the layer prefers linked cels
collapsedGroupWhether or not the layer group should be displayed collapsed
referenceWhether or not the layer is a reference layer

Further Info

If you would like to read up on the Aseprite file spec: Spec

0.0.17

7 months ago

0.0.18

7 months ago

0.0.16

1 year ago

0.0.15

2 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago