Skip to content

Commit

Permalink
Switch to shelf-pack binpacker
Browse files Browse the repository at this point in the history
  • Loading branch information
bhousel committed Mar 30, 2016
1 parent 2a9ddd4 commit ae79318
Show file tree
Hide file tree
Showing 9 changed files with 2,104 additions and 2,108 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ and higher-dpi sprites from the same source.
### `generateLayout(imgs, ratio, format, callback)`

Pack a list of images with width and height into a sprite layout.
Uses bin-pack.
Uses [shelf-pack](https://github.com/mapbox/shelf-pack).

### Parameters

Expand Down
31 changes: 14 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var mapnik = require('mapnik');
var assert = require('assert');
var xtend = require('xtend');
var pack = require('shelf-pack');
var ShelfPack = require('shelf-pack');
var queue = require('queue-async');
var emptyPNG = new mapnik.Image(1, 1).encodeSync('png');
var sortBy = require('sort-by');
Expand All @@ -10,7 +10,7 @@ var heightAscThanNameComparator = sortBy('-height', 'id');

/**
* Pack a list of images with width and height into a sprite layout.
* Uses bin-pack.
* Uses shelf-pack.
* @param {Array<Object>} imgs array of `{ buffer: Buffer, id: String }`
* @param {number} pixelRatio ratio of a 72dpi screen pixel to the destination
* pixel density
Expand Down Expand Up @@ -41,24 +41,15 @@ function generateLayout(imgs, pixelRatio, format, callback) {

q.awaitAll(function(err, imagesWithSizes){
if (err) return callback(err);
var height = 1;
var width = 1;
var packing = new pack(width, height);

imagesWithSizes.forEach(function(image) {
var packed = packing.allocate(width, height);
if (packed.x === -1 && packed.y === -1) {
if (image.width + width > packing.width) width += image.width;
if (image.height + height > packing.height) height += image.height;
packing.resize(width, height);
packing.allocate(image.width, image.height);
}
});
imagesWithSizes.sort(heightAscThanNameComparator);

var obj = {};
var sprite = new ShelfPack(1, 1, { autoResize: true });
sprite.pack(imagesWithSizes, { inPlace: true });

if (format) {
packing.items.forEach(function(item) {
var obj = {};
imagesWithSizes.forEach(function(item) {
obj[item.id] = {
width: item.width,
height: item.height,
Expand All @@ -68,9 +59,15 @@ function generateLayout(imgs, pixelRatio, format, callback) {
};
});
return callback(null, obj);

} else {
return callback(null, packing);
return callback(null, {
width: sprite.w,
height: sprite.h,
items: imagesWithSizes
});
}

});
}
module.exports.generateLayout = generateLayout;
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
"author": "Tom MacWright",
"license": "ISC",
"dependencies": {
"bin-pack": "^1.0.1",
"mapnik": "~3.5.0",
"queue-async": "^1.0.7",
"shelf-pack": "0.0.1",
"shelf-pack": "1.0.0",
"sort-by": "^1.1.1",
"xtend": "^4.0.1"
},
Expand Down
Loading

0 comments on commit ae79318

Please sign in to comment.