-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspritemap.js
More file actions
75 lines (66 loc) · 2.92 KB
/
spritemap.js
File metadata and controls
75 lines (66 loc) · 2.92 KB
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
const gm = require('gm');
const fse = require('fs-extra');
const path = require('path');
module.exports = (sheets, cache = '.spritemap-cache/') => {
fse.ensureDir(cache).then(() =>
sheets.forEach((configPath) => fse.readJson(configPath).then((data) => {
console.log('Reading ' + configPath);
const basePath = path.dirname(configPath);
const inputPath = path.join(basePath, data.input.path);
const inputExt = data.input.extension || 'png';
const { x: basisX, y: basisY } = data.basis;
const { x: gridX, y: gridY } = data.grid;
const sprites = data.sprites;
const blocks = data.blocks;
const tempPath = path.join(cache, data.output.path);
console.log('Writing ' + tempPath);
const ox = basisX * gridX;
const oy = basisY * gridY;
const getValues = (data) => {
const { x: gx, y: gy, src } = data;
const gw = data.w || 1;
const gh = data.h || 1;
const px = gx * (basisX + 0);
const py = gy * (basisY + 0);
const pw = gw * basisX;
const ph = gh * basisY;
const imgSrc = `${src}.${inputExt}`;
const srcPath = path.join(inputPath, imgSrc);
const tmpPath = path.join(cache, imgSrc);
return {
gx, gy, gw, gh, px, py, pw, ph, imgSrc, srcPath, tmpPath,
};
}
const bufferX = sprites.map(v => (v.w || 1) * basisX).reduce((a, b) => a > b ? a : b, 0);
const bufferY = sprites.map(v => (v.h || 1) * basisY).reduce((a, b) => a > b ? a : b, 0);
console.log('bufferX: '+ bufferX + ' bufferY: ' + bufferY); // Needs a buffer zone because region().draw() overrites from 0,0 for some reason
const serialStitch = (array, buffer, position) => {
if (position === array.length) {
gm(buffer)
.crop(ox, oy, bufferX, bufferY)
.write(tempPath, err => {
if (err) console.log('no write: '+ tempPath + `\n${err}`);
});
} else {
const { gx, gy, gw, gh, px, py, pw, ph, imgSrc, srcPath, tmpPath } = getValues(array[position]);
if (!fse.existsSync(srcPath)) {
console.log('DNE:', srcPath, '->', tempPath);
serialStitch(array, buffer, position + 1);
} else {
gm(buffer)
.region(pw, ph, px + bufferX, py + bufferY)
.draw(`image Over ${0},${0} ${pw},${ph} ${srcPath}`)
.region(ox + bufferX, oy + bufferY, -px - bufferX, -py - bufferY) // required because gm draw bleeds one column to the right and the left-most pixel below
.toBuffer('PNG', (err, buf) => {
serialStitch(array, buf, position + 1); // TODO convert to actual iterative
});
}
}
};
gm(ox + bufferX, oy + bufferY, "transparent")
.toBuffer('png', (err, buffer) => {
if (err) console.log(err);
serialStitch(sprites, buffer, 0);
});
})));
}