Skip to content

Instantly share code, notes, and snippets.

@millermedeiros
Created November 4, 2011 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save millermedeiros/1340046 to your computer and use it in GitHub Desktop.
Save millermedeiros/1340046 to your computer and use it in GitHub Desktop.
node.js CLI to compress TexturePacker JSON-Array data
#!/usr/bin/env node
//npm install commander
/**
* Command line tool to convert TexturePacker JSON-Array data into
* optimized SpriteAnim.js JSON data file.
* @author Miller Medeiros
* @version 0.1.0 (2011/10/18)
*/
var program = require('commander'),
fs = require('fs'),
path = require('path');
// ---
function list(val){
return val.split(',');
}
program
.version('0.1.0')
.description('Convert TexturePacker JSON-Array data into SpriteAnim.js data file.')
.option('-i, --input <dir>', 'Specify input dir.')
.option('-o, --output <dir>', 'Specify output dir.')
.option('-f, --files [files]', 'Specify input json files (comma separated), if empty it will optimize all the ".json" files inside the input dir.', list)
.option('-H, --hard', 'Overwrite existing files.')
.option('-p, --image-path <path>', 'Base image path.')
.parse(process.argv);
//input and output are required
if(! program.input || ! program.output){
program.parse([process.argv[0], process.argv[1], '-h']);
process.exit(0);
}
// ---
var inputFiles;
if(! path.existsSync(program.output) ){
fs.mkdirSync(program.output, '0777');
}
if(program.files){
inputFiles = program.files;
} else {
inputFiles = fs.readdirSync(program.input);
inputFiles = inputFiles.filter(function(fname){
return (/\.json$/).test(fname);
});
}
if(! inputFiles.length){
console.log('\n ERROR: no JSON files found inside input directory.\n');
process.exit(1);
}
console.log('\n Converting %s file%s... ', inputFiles.length, (inputFiles.length > 1)? 's' : '');
inputFiles.forEach(processFile);
console.log('');
// ---
function processFile(fname){
var fpath = path.join(program.input, fname),
content = fs.readFileSync(fpath, "utf-8"),
sourceData = JSON.parse(content),
outputData = {
spriteSheet : path.join(program.imagePath, sourceData.meta.image),
frameSize : sourceData.frames[0].sourceSize,
frames : sourceData.frames.map(normalizeFrameData)
};
var outputPath = path.join(program.output, fname);
if(program.hard || ! path.existsSync(outputPath)){
fs.writeFileSync(outputPath, JSON.stringify(outputData), 'utf-8');
console.log(' - '+ fpath +' -> '+outputPath);
} else {
console.log('\n ERROR: "'+ outputPath +'" already exists, use \'--hard\' to overwrite existing files.\n');
process.exit(1);
}
}
function normalizeFrameData(data){
return {
x : data.frame.x,
y : data.frame.y,
w : data.frame.w,
h : data.frame.h,
t : data.spriteSourceSize.y,
l : data.spriteSourceSize.x
};
}
@millermedeiros
Copy link
Author

used to convert TexturePacker JSON-Array data into a compressed JSON e.g.:

{
  "spriteSheet" : "imgs/sprites/zombie.png",
  "frameSize" : {
    "w" : 131,
    "h" : 257
  },
  "frames" : [
    {"x":0,"y":0,"w":125,"h":257,"t":0,"l":5},
    {"x":125,"y":0,"w":125,"h":257,"t":0,"l":5},
    {"x":250,"y":0,"w":125,"h":257,"t":0,"l":5},
    {"x":375,"y":0,"w":127,"h":257,"t":0,"l":4},
    {"x":502,"y":0,"w":125,"h":257,"t":0,"l":5},
    {"x":627,"y":0,"w":107,"h":257,"t":0,"l":23},
    {"x":734,"y":0,"w":107,"h":257,"t":0,"l":23},
    ...
  ]
}

I'm planning to release a library that reads this kind of data and plays sprite sheet animation. Already coded it but not sure if I will open source it and when I will do it (spends too much time writing docs, creating repository, false bug reports, etc...)

Some of the frames data is only needed if each sprite have a different size.. source code should be easy to tweak for your need.

PS: output won't have line breaks..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment