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.
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.