-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspritesheet.js
73 lines (64 loc) · 2.18 KB
/
spritesheet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const fs = require('fs');
const path = require('path');
const spritesmith = require('spritesmith');
const sourcePath = path.resolve(__dirname, 'src', 'spritesheets');
const outputDir = path.resolve(__dirname, 'dist', 'spritesheets');
fs.readdir(sourcePath, (err, files) => {
if (err) {
console.error('Error reading spritesheets directory:', err);
return;
}
files.forEach(folder => {
const folderPath = path.join(sourcePath, folder);
if (fs.statSync(folderPath).isDirectory()) {
const images = fs.readdirSync(folderPath)
.filter(file => file.endsWith('.png'))
.map(file => path.join(folderPath, file));
spritesmith.run({ src: images }, (err, result) => {
if (err) {
console.error('Error generating spritesheet:', err);
return;
}
const spritesheetPath = path.join(outputDir, `${folder}.png`);
const jsonPath = path.join(outputDir, `${folder}.json`);
const frames = {};
Object.keys(result.coordinates).forEach(imagePath => {
const baseName = path.basename(imagePath, path.extname(imagePath));
frames[baseName] = {
frame: {
x: result.coordinates[imagePath].x,
y: result.coordinates[imagePath].y,
w: result.coordinates[imagePath].width,
h: result.coordinates[imagePath].height
},
rotated: false,
trimmed: false,
spriteSourceSize: {
x: 0,
y: 0,
w: result.coordinates[imagePath].width,
h: result.coordinates[imagePath].height
},
sourceSize: {
w: result.coordinates[imagePath].width,
h: result.coordinates[imagePath].height
}
};
});
const meta = {
image: `${folder}.png`,
size: {
w: result.properties.width,
h: result.properties.height
}
};
const jsonData = {
frames: frames,
meta: meta
};
fs.writeFileSync(jsonPath, JSON.stringify(jsonData));
fs.writeFileSync(spritesheetPath, result.image);
});
}
});
});