0.0.13 • Published 4 years ago

ase-parser-slices v0.0.13

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

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;
  
  // 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

Frame Object

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

Layer Object

FieldTypeDescription
flagsintegerflags for the layer
typeintegertype
layerChildLevelintegerlayer child level
opacityintegeropacity (0-255)
namestringname of layer

Tag Object

FieldTypeDescription
fromintegerfirst frame index
tointegerlast frame index
animDirectionstringForward, Reverse or Ping-pong
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

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
wintegerwidth (in pixels)
hintegerheight (in pixels)
rawCelDataBufferraw cel pixel data

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

Further Info

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

0.0.13

4 years ago

0.0.12

4 years ago