1 | # ase-parser
|
2 | Parse Aseprite files with Node.js, no external dependencies.
|
3 |
|
4 | ### Install
|
5 | To install for use:
|
6 | ```
|
7 | npm i ase-parse
|
8 | ```
|
9 |
|
10 | ## Instructions
|
11 | 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.
|
12 |
|
13 | ```js
|
14 | const Aseprite = require('ase-parser');
|
15 | const fs = require('fs');
|
16 |
|
17 | const buff = fs.readFileSync('./somefile.aseprite');
|
18 | const aseFile = new Aseprite(buff, 'somefile.aseprite');
|
19 |
|
20 | aseFile.parse();
|
21 | console.log(aseFile.numFrames);
|
22 | ```
|
23 |
|
24 | After parsing, you can get the data and do something like generate an image with the [`sharp`](https://www.npmjs.com/package/sharp) npm lib, which accepts raw pixel data and supports image composition.
|
25 |
|
26 | Here is a more advanced example generating a `png` image of the first frame using the [`sharp`](https://www.npmjs.com/package/sharp) lib.
|
27 |
|
28 | ```js
|
29 | const Aseprite = require('ase-parser');
|
30 | const fs = require('fs');
|
31 | const sharp = require('sharp');
|
32 |
|
33 | async function makePNG() {
|
34 | const buff = fs.readFileSync('./my_chocobo.aseprite');
|
35 | const ase = new Aseprite(buff, 'my_chocobo.aseprite');
|
36 |
|
37 | ase.parse();
|
38 | // 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)
|
39 | const bgPromise = sharp({create: {
|
40 | width: ase.width,
|
41 | height: ase.height,
|
42 | channels: 4,
|
43 | background: {r: 0, g: 0, b: 0, alpha: 0}
|
44 | }}).png().toBuffer();
|
45 |
|
46 | // Get the cels for the first frame
|
47 | const cels = ase.frames[0].cels;
|
48 |
|
49 | // Create png image buffers per cel to create an image of the first frame (creating the Promises to be used)
|
50 | const otherPromises = cels.map(cel => {
|
51 | return sharp(cel.rawCelData, {raw: {width: cel.w, height: cel.h, channels: 4}}).png().toBuffer();
|
52 | });
|
53 |
|
54 | // Run the promises all at once to get the buffers for the base image and the cels to combine
|
55 | const [ bg, ...others ] = await Promise.all([bgPromise, ...otherPromises]).catch(console.log);
|
56 |
|
57 | // 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)
|
58 | const finalBuff = await sharp(bg)
|
59 | .composite(others.map((img, index) => ({
|
60 | input: img,
|
61 | top: cels[index].ypos,
|
62 | left: cels[index].xpos
|
63 | })))
|
64 | .png()
|
65 | .toBuffer();
|
66 | // saves the file as a png with the buffer from sharp.composite
|
67 | fs.writeFileSync(ase.name.replace('.aseprite', '.png'), finalBuff);
|
68 | }
|
69 |
|
70 | makePNG();
|
71 |
|
72 | ```
|
73 |
|
74 | ## Aseprite Functions
|
75 |
|
76 | ### `constructor`
|
77 | Parameters:
|
78 | * `buffer`: Expects a Node.js Buffer of the `Aseprite` file.
|
79 | * `name`: Expects a string that's the name of the `Aseprite` file, including the extension.
|
80 |
|
81 | Returns:
|
82 | * `Aseprite`: Returns [Aseprite](#aseprite-object).
|
83 |
|
84 | Example:
|
85 | ```js
|
86 | const Aseprite = require('ase-parser');
|
87 | const fs = require('fs');
|
88 |
|
89 | const buff = fs.readFileSync('./somefile.aseprite');
|
90 | const aseFile = new Aseprite(buff, 'somefile.aseprite');
|
91 | ```
|
92 |
|
93 | ### `parse`
|
94 | Description:
|
95 | Parses the Aseprite file and populates the `Aseprite` class with the information from the file.
|
96 |
|
97 | Parameters:
|
98 | * none
|
99 |
|
100 | Returns:
|
101 | * none
|
102 |
|
103 | Example:
|
104 | ```js
|
105 | const Aseprite = require('ase-parser');
|
106 | const fs = require('fs');
|
107 |
|
108 | const buff = fs.readFileSync('./somefile.aseprite');
|
109 | const aseFile = new Aseprite(buff, 'somefile.aseprite');
|
110 |
|
111 | aseFile.parse();
|
112 | ```
|
113 |
|
114 |
|
115 |
|
116 | ## Aseprite Object
|
117 | | Field | Type | Description |
|
118 | |--------------|------------------------|----------------------------------------|
|
119 | | frames | array of [frame](#frame-object) objects | frames |
|
120 | | layers | array of [layer](#layer-object) objects | layers |
|
121 | | fileSize | integer | size of the file (in bytes) |
|
122 | | numFrames | integer | number of frames the Aseprite file has |
|
123 | | width | integer | width (in pixels) |
|
124 | | height | integer | height (in pixels) |
|
125 | | colorDepth | integer | color depth (in bits per pixel) |
|
126 | | paletteIndex | integer | position of the indexed color based on the palette |
|
127 | | numColors | integer | number of colors |
|
128 | | pixelRatio | string | width:height |
|
129 | | name | string | name of the file |
|
130 | | tags | arry of [tag](#tag-object) objects | tags |
|
131 | | colorProfile | [colorProfile](#color-profile-object) object | Color profile |
|
132 | | palette | [palette](#palette-object) object | Palette |
|
133 |
|
134 | ## Frame Object
|
135 | | Field | Type | Description |
|
136 | |---------------|-----------------------|------------------|
|
137 | | bytesInFrame | integer | size (in bytes) |
|
138 | | frameDuration | integer | duration (in ms) |
|
139 | | cels | array of [cel](#cel-object) objects | cels |
|
140 |
|
141 | ## Layer Object
|
142 | | Field | Type | Description |
|
143 | |-----------------|---------|---------------------|
|
144 | | flags | integer | flags for the layer |
|
145 | | type | integer | type |
|
146 | | layerChildLevel | integer | layer child level |
|
147 | | opacity | integer | opacity (0-255) |
|
148 | | name | string | name of layer |
|
149 |
|
150 |
|
151 | ## Tag Object
|
152 | | Field | Type | Description |
|
153 | |---------------|---------|----------------------------------------|
|
154 | | from | integer | first frame index |
|
155 | | to | integer | last frame index |
|
156 | | animDirection | string | `Forward`, `Reverse` or `Ping-pong` |
|
157 | | color | string | hex color of the tag (no `#` included) |
|
158 | | name | string | name |
|
159 |
|
160 | ## Color Profile Object
|
161 | | Field | Type | Description |
|
162 | |--------|---------|-------------------------|
|
163 | | type | string | `None`, `sRGB` or `ICC` |
|
164 | | flag | integer | fixed gamma flag |
|
165 | | fGamma | integer | fixed gamma |
|
166 | | icc? | buffer | ICC profile data |
|
167 |
|
168 | ## Palette Object
|
169 | | Field | Type | Description |
|
170 | |-------------|------------------------|--------------------------|
|
171 | | paletteSize | integer | number of colors |
|
172 | | firstColor | integer | index of the first color |
|
173 | | lastColor | integer | index of the last color |
|
174 | | colors | array of [color](#color-object) objects | colors |
|
175 | | index? | integer | position of the indexed color based on the palette |
|
176 |
|
177 | ## Cel Object
|
178 | | Field | Type | Description |
|
179 | |------------|---------|----------------------------------------------|
|
180 | | layerIndex | integer | index of the layer associated |
|
181 | | xpos | integer | x position of the cel compared to the sprite |
|
182 | | ypos | integer | y position of the cel compared to the sprite |
|
183 | | opacity | integer | opacity (0-255) |
|
184 | | celType | integer | internally used |
|
185 | | w | integer | width (in pixels) |
|
186 | | h | integer | height (in pixels) |
|
187 | | rawCelData | Buffer | raw cel pixel data |
|
188 |
|
189 | ## Color Object
|
190 | | Field | Type | Description |
|
191 | |-------|---------|-----------------------------------------------|
|
192 | | red | integer | red value (0-255) |
|
193 | | green | integer | green value (0-255) |
|
194 | | blue | integer | blue value (0-255) |
|
195 | | alpha | integer | alpha value (0-255) |
|
196 | | name | string | 'none' or the actual color name if it has one |
|
197 |
|
198 | # Further Info
|
199 |
|
200 | If you would like to read up on the Aseprite file spec: [Spec](https://github.com/aseprite/aseprite/blob/master/docs/ase-file-specs.md) |
\ | No newline at end of file |