spritesmith
Advanced tools
Comparing version 0.4.1 to 0.5.0
{ | ||
"name": "spritesmith", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Utility that takes images and creates a spritesheet with JSON sprite data", | ||
@@ -5,0 +5,0 @@ "main": "src/smith.js", |
@@ -15,5 +15,2 @@ // Load in underscore and common test | ||
this.options = options; | ||
// Bump the timeout for extreme tests | ||
this.timeout(20000); | ||
}, | ||
@@ -20,0 +17,0 @@ 'when processed via spritesmith': ['using phantomjs engine', '_when processed via spritesmith'], |
@@ -67,2 +67,15 @@ var assert = require('assert'), | ||
function createImage(file, cb) { | ||
// Call createImages with an array | ||
createImages([file], function (err, dimensionArr) { | ||
// Fallback dimensionArr | ||
dimensionArr = dimensionArr || []; | ||
// Pluck out dimension and callback | ||
cb(err, dimensionArr[0]); | ||
}); | ||
} | ||
engine.createImage = createImage; | ||
// Mass image creation | ||
function createImages(files, cb) { | ||
// In series | ||
@@ -72,18 +85,19 @@ async.waterfall([ | ||
function getImgSize (cb) { | ||
exec('phantomjs ' + __dirname + '/phantomjs/stats.js ' + file, cb); | ||
// Stringify and escape (for CLI quote issues) | ||
var filesStr = JSON.stringify(files), | ||
encodedFilesStr = encodeURIComponent(filesStr); | ||
// Call the stats phantomjs | ||
exec('phantomjs ' + __dirname + '/phantomjs/stats.js ' + encodedFilesStr, cb); | ||
}, | ||
function saveImgSize (stdout, stderr, cb) { | ||
// Parse the output | ||
var dimensions = JSON.parse(stdout); | ||
var dimensionArr = JSON.parse(stdout); | ||
// Adjust the dimensions off of `px` | ||
dimensions.height = +(dimensions.height.replace('px', '')); | ||
dimensions.width = +(dimensions.width.replace('px', '')); | ||
// Callback with the dimensions | ||
cb(null, dimensions); | ||
cb(null, dimensionArr); | ||
} | ||
], cb); | ||
} | ||
engine.createImage = createImage; | ||
engine.createImages = createImages; | ||
@@ -90,0 +104,0 @@ // Function to add new exporters |
// Load in modules | ||
var system = require('system'), | ||
webpage = require('webpage'); | ||
fs = require('fs'), | ||
webpage = require('webpage'), | ||
_ = require('./nimble.min.js'); | ||
// Grab the arguments | ||
var args = system.args, | ||
img = args[1]; | ||
encodedFilesStr = args[1]; | ||
// If there is no image, throw an error | ||
if (!img) { | ||
throw new Error('No image specified to grab stats from.'); | ||
if (!encodedFilesStr) { | ||
throw new Error('No images specified to grab stats from.'); | ||
} | ||
// Load in the image | ||
// DEV: If this fails, use data/html | ||
var page = webpage.create(); | ||
page.open(img, function (status) { | ||
// Pluck out the image dimensions | ||
var dimensions = page.evaluate(function () { | ||
// Grab the image | ||
var img = document.getElementsByTagName('img')[0]; | ||
// Parse the image paths | ||
var imgsStr = decodeURIComponent(encodedFilesStr), | ||
imgs = JSON.parse(imgsStr); | ||
// Get the dimensions of the image | ||
var style = window.getComputedStyle(img), | ||
dimensions = { | ||
width: style.width, | ||
height: style.height | ||
}; | ||
return dimensions; | ||
// In parallel | ||
_.map(imgs, function getStats (img, cb) { | ||
// Load in the image | ||
// DEV: If this fails, use data/html | ||
var page = webpage.create(); | ||
page.open(img, function (status) { | ||
// Pluck out the image dimensions | ||
var dimensions = page.evaluate(function () { | ||
// Grab the image | ||
var img = document.getElementsByTagName('img')[0]; | ||
// Get the dimensions of the image | ||
var style = window.getComputedStyle(img), | ||
dimensions = { | ||
width: style.width, | ||
height: style.height | ||
}; | ||
return dimensions; | ||
}); | ||
// Adjust the dimensions off of `px` | ||
dimensions.height = +(dimensions.height.replace('px', '')); | ||
dimensions.width = +(dimensions.width.replace('px', '')); | ||
// Callback with the dimensions | ||
cb(null, dimensions); | ||
}); | ||
}, function handleStats (err, dimensionArr) { | ||
// Stringify and emit the dimensions | ||
var retStr = JSON.stringify(dimensions, null, 4); | ||
var retStr = JSON.stringify(dimensionArr); | ||
console.log(retStr); | ||
@@ -39,1 +55,2 @@ | ||
}); | ||
@@ -5,2 +5,8 @@ var async = require('async'); | ||
} | ||
// ANTI-PATTERN: 2 parameters are non-extensible objects | ||
function saveImagePath(img, file) { | ||
img._filepath = file; | ||
} | ||
EngineSmith.prototype = { | ||
@@ -16,3 +22,3 @@ // Create an image from a file via the engine | ||
// Save the buffer path to the image | ||
img._filepath = file; | ||
saveImagePath(img, file); | ||
@@ -26,6 +32,28 @@ // Callback with the image | ||
'createImages': function (files, cb) { | ||
// Map the files into their image counterparts | ||
// DEV: Magic number of 10 to prevent file descriptor overuse | ||
// This does not affect perf -- 12 seconds with 300, 11.5 with 10 for 2000 images (derp) | ||
async.mapLimit(files, 10, this.createImage.bind(this), cb); | ||
// If there is a engine.createImages method, use it | ||
var engine = this.engine; | ||
if (engine.createImages) { | ||
async.waterfall([ | ||
// Mass create images | ||
function engineCreateImages (cb) { | ||
engine.createImages(files, cb); | ||
}, | ||
// Save the image paths | ||
function savePaths (imgs, cb) { | ||
// Iterate over the images and save their paths | ||
imgs.forEach(function (img, i) { | ||
saveImagePath(img, files[i]); | ||
}); | ||
// Callback with the images | ||
cb(null, imgs); | ||
} | ||
], cb); | ||
} else { | ||
// Otherwise, map each of the images individually | ||
// Map the files into their image counterparts | ||
// DEV: Magic number of 10 to prevent file descriptor overuse | ||
// This does not affect perf -- 12 seconds with 300, 11.5 with 10 for 2000 images (derp) | ||
async.mapLimit(files, 10, this.createImage.bind(this), cb); | ||
} | ||
}, | ||
@@ -32,0 +60,0 @@ // Helper to create canvas via engine |
1370965
1075
8