-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathspriter.js
144 lines (112 loc) · 4.01 KB
/
spriter.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var fs = require('fs'),
util = require('util'),
path = require('path'),
mkdirp = require('mkdirp'),
Canvas = require('canvas'),
Image = Canvas.Image,
Futures = require('futures'),
Join = Futures.join;
var maxSpriteWidth = 2048;
function spriter(options) {
var sourceDirectories = options.sourceDirectories,
imageDestination = options.imageDestination || '.',
dataDestination = options.dataDestination || '.';
var fileLists = sourceDirectories.reduce(function(lists, directory) {
lists[directory] = fs.readdirSync(directory);
return lists;
}, {});
var rowData = {},
// Load all the images
join = loadImages(fileLists,rowData);
// Wait for the all images to load
join.when(function() {
// Get the dimensions of the output sprite map
var dimensions = calculateSize(rowData),
canvas = new Canvas(dimensions.width, dimensions.height);
// Draw the images to the canvas and return the JSON data
var jsonOutput = drawImages(rowData,canvas);
// Create non-existent target directories recursively, if needed
mkdirp.sync(path.resolve(imageDestination));
mkdirp.sync(path.resolve(dataDestination));
// Write out both the sprites.png and sprites.json files
var imageTarget = path.resolve(imageDestination,"sprites.png"),
dataTarget = path.resolve(dataDestination,"sprites.json");
fs.writeFileSync(imageTarget,canvas.toBuffer());
fs.writeFileSync(dataTarget,JSON.stringify(jsonOutput));
util.print("Wrote sprites.png and sprites.json\n");
});
}
function loadImages(fileLists,rowData) {
var fileRegex = /^(.*?)([0-9]+)\.[a-zA-Z]{3}$/,
join = Join();
Object.keys(fileLists).forEach(function(directory) {
var files = fileLists[directory];
for(var i=0;i<files.length;i++) {
(function(file) {
var results = file.match(fileRegex),
img = new Image();
if(results) {
var rowName = results[1],
fileNum = parseInt(results[2],10);
img.onload = join.add();
img.onerror = function() {
util.print("Error loading: " + file + "\n"); process.exit(1);
}
img.src = path.resolve(directory, file);
rowData[rowName] = rowData[rowName] || [];
rowData[rowName].push([fileNum,img]);
}
})(files[i]);
}
});
return join;
}
function calculateSize(rowData) {
var maxWidth = 0,
totalHeight = 0;
for(var spriteName in rowData) {
// Order by ascending number
var row = rowData[spriteName],
firstImage = row[0][1],
width = firstImage.width * row.length,
rows = 1;
if(width > maxSpriteWidth) {
rows = Math.ceil(width / maxSpriteWidth);
width = maxSpriteWidth;
}
maxWidth = Math.max(width,maxWidth);
totalHeight += firstImage.height * rows + 1;
}
return { width: maxWidth, height: totalHeight };
}
function drawImages(rowData,canvas) {
var ctx = canvas.getContext('2d'),
curY = 0,
jsonOutput = {};
for(var spriteName in rowData) {
// Order by ascending number
var row = rowData[spriteName].sort(function(a,b) { return a[0] - b[0]; }),
firstImage = row[0][1],
imageWidth = firstImage.width,
rowHeight = firstImage.height,
rowWidth = Math.min(imageWidth * row.length, maxSpriteWidth),
cols = Math.floor(rowWidth / imageWidth),
rows = Math.ceil(row.length / cols);
console.log(spriteName + " " + cols + " " + rows);
curY++;
jsonOutput[spriteName] = { sx: 0, sy: curY, cols: cols,
tilew: imageWidth, tileh: rowHeight,
frames: row.length };
for(var i =0;i<rows;i++) {
for(var k=0;k<cols;k++) {
if(row[k+i*cols]) {
ctx.drawImage(row[k + i*cols ][1],k*imageWidth,curY);
}
}
curY += Math.ceil(rowHeight);
}
}
return jsonOutput;
}
// Make the spriter method available
module.exports = spriter;