Create a new ShelfPack bin allocator.
Uses the Shelf Best Height Fit algorithm from http://clb.demon.fi/files/RectangleBinPack.pdf
var sprite = new ShelfPack(64, 64, { autoResize: false });
Batch pack multiple bins into the sprite.
Array<Bin>
:
Array of allocated Bins - each Bin is an object with
id
,
x
,
y
,
w
,
h
properties
var bins = [
{ id: 1, w: 12, h: 12 },
{ id: 2, w: 12, h: 16 },
{ id: 3, w: 12, h: 24 }
];
var results = sprite.pack(bins, { inPlace: false });
Pack a single bin into the sprite.
Each bin will have a unique identitifer.
If no identifier is supplied in the id
parameter, one will be created.
Note: The supplied id
is used as an object index, so numeric values are fastest!
Bins are automatically refcounted (i.e. a newly packed Bin will have a refcount of 1).
When a bin is no longer needed, use the ShelfPack.unref
function to mark it
as unused. When a Bin's refcount decrements to 0, the Bin will be marked
as free and its space may be reused by the packing code.
Bin
:
Bin object with
id
,
x
,
y
,
w
,
h
properties, or
null
if allocation failed
var results = sprite.packOne(12, 16, 'a');
Shrink the width/height of the sprite to the bare minimum. Since shelf-pack doubles first width, then height when running out of shelf space this can result in fairly large unused space both in width and height if that happens towards the end of bin packing.
Clear the sprite. Resets everything and resets statistics.
sprite.clear();
Create a new Bin object.
(number)
Left coordinate of the bin
(number)
Top coordinate of the bin
(number)
Width of the bin
(number)
Height of the bin
var bin = new Bin('a', 0, 0, 12, 16);