jakes-gordon-growing-packer

0.1.0 • Public • Published

jakes-gordon-growing-packer

version npm version

Dependency Status devDependency Status

Home Page

A javascript binary tree based algorithm for 2d bin-packing

Purpose

  • Creating a spritesheet generator

Install

npm install jakes-gordon-growing-packer

How to use

See first the Original algorithm README

 
var GrowingPacker = require('jakes-gordon-growing-packer')
 
var packer = new GrowingPacker({
    //sortMethod: a sort method or a valid sort method name, default to maxside
 
    blocks: [
        { w: 100, h: 100 },
        { w: 100, h: 100 },
        { w:  80, h:  80 },
        { w:  80, h:  80 }
    ]
    /* blocks is optional and can be passed directly to the pack method */
});
 
var pack = packer.pack();
 
// or
 
var packer = new GrowingPacker();
var pack = packer.pack([
    { w: 100, h: 100 },
    { w: 100, h: 100 },
    { w:  80, h:  80 },
    { w:  80, h:  80 }
]);
 
// then
 
console.log(pack.width, pack.height);
 
var rectangles = pack.rectangles(); //note that the rectangles method modify your original blocks array
    
for(var i = 0, imax = rectangles.length ; i < imax ; i++) {
    var rect = rectangles[i];
 
    //do something like :
    //DrawRectangle(rect.x, rect.y, rect.w, rect.h);
}
 

Valid sort methods

  • random

    function random(a, b) {
        return Math.random() - 0.5;
    }
  • w

    function w(a, b) {
        return b.w - a.w;
    }
  • h

    function h(a, b) {
        return b.h - a.h;
    }
  • a

    function a(_a, b) {
        return b.area - _a.area;
    }
  • max

    function max(a, b) {
        return Math.max(b.w, b.h) - Math.max(a.w, a.h);
    }
  • min

    function min(a, b) {
        return Math.min(b.w, b.h) - Math.min(a.w, a.h);
    }
  • height

    function height(a, b) {
        return msort(a, b, ['h', 'w']);
    }
  • width

    function width(a, b) {
        return msort(a, b, ['w', 'h']);
    }
  • area

    function area(a, b) {
        return msort(a, b, ['a', 'h', 'w']);
    }
  • maxside

    function maxside(a, b) {
        return msort(a, b, ['max', 'min', 'h', 'w']);
    }

Credits

The packing algorithm is from here

I just wrap it in a custom API (including the sort methods proposed by Jake Gordon ) and make it accesible via npm since that issue is still opened.

Package Sidebar

Install

npm i jakes-gordon-growing-packer

Weekly Downloads

3

Version

0.1.0

License

MIT

Last publish

Collaborators

  • alexistessier