-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
34 lines (25 loc) · 906 Bytes
/
index.js
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
"use strict";
var GrowingPacker = require('./packer.growing.js');
module.exports = function(items, options) {
options = options || {};
var packer = new GrowingPacker();
var inPlace = options.inPlace || false;
// Clone the items.
var newItems = items.map(function(item) { return inPlace ? item : { width: item.width, height: item.height, item: item }; });
newItems = newItems.sort(function(a, b) {
// TODO: check that each actually HAS a width and a height.
// Sort based on the size (area) of each block.
return (b.width * b.height) - (a.width * a.height);
});
packer.fit(newItems);
var w = newItems.reduce(function(curr, item) { return Math.max(curr, item.x + item.width); }, 0);
var h = newItems.reduce(function(curr, item) { return Math.max(curr, item.y + item.height); }, 0);
var ret = {
width: w,
height: h
};
if (!inPlace) {
ret.items = newItems;
}
return ret;
};