2D bin packing algorithm usefull for sprite-generators or texture atlas
npm install boxpacking
var boxpacking = require('boxpacking');
var blocks = [
{ width: 100, height: 100 },
{ width: 50, height: 50 },
{ width: 50, height: 50 },
{ width: 50, height: 50 },
{ width: 50, height: 50 },
{ width: 50, height: 50 }
];
var pack = boxpacking(blocks);
This will return into "pack" variable an object which contains a list of blocks with coordinates.
Result max width.
Result max height.
{
list: [
{ width: 100, height: 100, x: 0, y: 0 },
{ width: 50, height: 50, x: 100, y: 0 },
{ width: 50, height: 50, x: 100, y: 50 },
{ width: 50, height: 50, x: 0, y: 100 },
{ width: 50, height: 50, x: 50, y: 100 },
{ width: 50, height: 50, x: 100, y: 100 }
],
excludedBlocks: [],
width: 150,
height: 150
}
An array of blocks
An array of blocks which excluded from result. It can happen when both maxWidth
and maxHeight
options are passed and
algorithm couldn't fit some blocks.
Result width.
Result height.