-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathspritezero
More file actions
executable file
·75 lines (62 loc) · 1.74 KB
/
spritezero
File metadata and controls
executable file
·75 lines (62 loc) · 1.74 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
#!/usr/bin/env node
var spritezero = require('@mapbox/spritezero');
var fs = require('fs');
var path = require('path');
var glob = require('glob');
var argv = require('minimist')(process.argv.slice(2), {
default: { _: ['.', '.'] },
boolean: ['retina', 'unique', 'h', 'help']
});
function showHelp() {
console.log(`
spritezero
Generate sprite sheets for maps and the web using SVG files as input
spritezero [output filename] [input directory]
--retina shorthand for --ratio=2
--ratio=[n] pixel ratio
--unique map identical images to multiple names
`);
}
if (argv.help || argv._.length < 2) {
showHelp();
/* istanbul ignore next */
process.exit(1);
}
var ratio = 1;
var unique = false;
if (argv.retina) {
ratio = 2;
} else if (argv.ratio) {
ratio = parseFloat(argv.ratio);
}
if (argv.unique) {
unique = true;
}
var outfile = argv._[0];
var input = argv._[1];
function sortById(a, b) {
return b.id < a.id;
}
const buffers = glob
.sync(input + '/*.svg')
.map((f) => ({
svg: fs.readFileSync(f),
id: path.basename(f).replace('.svg', '')
}))
.sort(sortById);
var genLayout = unique ? spritezero.generateLayoutUnique : spritezero.generateLayout;
genLayout({ imgs: buffers, pixelRatio: ratio, format: true }, saveLayout);
genLayout({ imgs: buffers, pixelRatio: ratio, format: false }, saveImage);
function saveLayout(err, formattedLayout) {
if (err) throw err;
fs.writeFileSync(outfile + '.json', JSON.stringify(formattedLayout, undefined, ' '));
}
function saveImage(err, layout) {
if (err) throw err;
spritezero.generateImage(layout, function (err, image) {
if (err) throw err;
fs.writeFileSync(outfile + '.png', image, function (err) {
if (err) throw err;
});
});
}