ShelfPack([w], [h], [options])

index.js

Create a new ShelfPack bin allocator.

Uses the Shelf Best Height Fit algorithm from http://clb.demon.fi/files/RectangleBinPack.pdf

Parameters

  • number = w (default 64) :

    Initial width of the sprite

  • number = h (default 64) :

    Initial width of the sprite

  • Object = options :
    • boolean = options.autoResize (default false)

      If true, the sprite will automatically grow

Examples

var sprite = new ShelfPack(64, 64, { autoResize: false });

Instance members

#clear

Clear the sprite.

clear

index.js

Clear the sprite.

Examples

sprite.clear();
#pack(bins, [options])

Batch pack multiple bins into the sprite.

pack(bins, [options])

index.js

Batch pack multiple bins into the sprite.

Parameters

  • Array bins :

    Array of requested bins - each object should have width, height (or w, h) properties

  • Object = options :
    • boolean = options.inPlace (default false)

      If true, the supplied bin objects will be updated inplace with x and y properties

Returns

Array :

Array of allocated bins - each bin is an object with x, y, w, h properties

Examples

var bins = [
    { id: 'a', width: 12, height: 12 },
    { id: 'b', width: 12, height: 16 },
    { id: 'c', width: 12, height: 24 }
];
var results = sprite.pack(bins, { inPlace: false });
#packOne(w, h)

Pack a single bin into the sprite.

packOne(w, h)

index.js

Pack a single bin into the sprite.

Parameters

  • number w :

    Width of the bin to allocate

  • number h :

    Height of the bin to allocate

Returns

Object :

Allocated bin object with x, y, w, h properties, or null if allocation failed

Examples

var results = sprite.packOne(12, 16);
#resize(w, h)

Resize the sprite. The resize will fail if the requested dimensions are smaller than the current sprite dimensions.

resize(w, h)

index.js

Resize the sprite. The resize will fail if the requested dimensions are smaller than the current sprite dimensions.

Parameters

  • number w :

    Requested new sprite width

  • number h :

    Requested new sprite height

Returns

boolean :

true if resize succeeded, false if failed

Examples

sprite.resize(256, 256);