{"_id":"@mapbox/shelf-pack","_rev":"402700","name":"@mapbox/shelf-pack","description":"A 2D rectangular bin packing data structure that uses the Shelf Best Height Fit heuristic","dist-tags":{"latest":"3.2.0"},"maintainers":[{"name":"mapbox-machine-user","email":"accounts+npm-mapbox-machine-user@mapbox.com"},{"name":"mapbox-npm","email":"accounts+npmjs@mapbox.com"},{"name":"mapbox-npm-01","email":"accounts+npmjs-01@mapbox.com"},{"name":"mapbox-npm-02","email":"accounts+npmjs-02@mapbox.com"},{"name":"mapbox-npm-03","email":"accounts+npmjs-03@mapbox.com"},{"name":"mapbox-npm-04","email":"accounts+npmjs-04@mapbox.com"},{"name":"mapbox-npm-08","email":"accounts+npmjs-08@mapbox.com"},{"name":"mapbox-npm-09","email":"accounts+npmjs-09@mapbox.com"},{"name":"mapbox-npm-ci","email":"accounts+npmjs-npm-ci@mapbox.com"}],"time":{"modified":"2021-08-04T04:36:55.000Z","created":"2017-02-13T04:39:14.361Z","3.2.0":"2018-07-13T14:20:41.104Z","3.1.0":"2017-08-25T05:07:53.296Z","3.0.0":"2017-02-13T04:39:14.361Z"},"users":{},"author":{"name":"Bryan Housel","email":"bryan@mapbox.com"},"repository":{"type":"git","url":"git+https://github.com/mapbox/shelf-pack.git"},"versions":{"3.2.0":{"name":"@mapbox/shelf-pack","description":"A 2D rectangular bin packing data structure that uses the Shelf Best Height Fit heuristic","version":"3.2.0","main":"index.js","module":"index.mjs","license":"ISC","author":{"name":"Bryan Housel","email":"bryan@mapbox.com"},"repository":{"type":"git","url":"git+https://github.com/mapbox/shelf-pack.git"},"keywords":["bin packing","sprite"],"devDependencies":{"benchmark":"^2.1.0","bin-pack":"1.0.2","coveralls":"^3.0.0","documentation":"4.0.0-beta5","eslint":"^5.0.0","rollup":"0.60.0","tap":"^12.0.0"},"engines":{"node":">=6.0.0"},"scripts":{"bench":"npm run build && node bench/bench.js","build":"rollup -f umd -n ShelfPack index.mjs --no-indent --no-strict -o index.js","docs":"documentation build index.mjs --lint --github --format html --output docs/","lint":"eslint index.mjs test/ bench/","test":"npm run build && npm run lint && tap --cov test/*.js"},"gitHead":"aff2193ba439937aedaf38f97f2f26a8bc133453","bugs":{"url":"https://github.com/mapbox/shelf-pack/issues"},"homepage":"https://github.com/mapbox/shelf-pack#readme","_id":"@mapbox/shelf-pack@3.2.0","_npmVersion":"6.1.0","_nodeVersion":"6.7.0","_npmUser":{"name":"mapbox-npm-04","email":"accounts+npmjs-04@mapbox.com"},"dist":{"shasum":"df3630ecce8c042817c9a365b88078412963de64","size":9364,"noattachment":false,"key":"/@mapbox/shelf-pack/-/@mapbox/shelf-pack-3.2.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@mapbox/shelf-pack/download/@mapbox/shelf-pack-3.2.0.tgz"},"maintainers":[{"name":"mapbox-machine-user","email":"accounts+npm-mapbox-machine-user@mapbox.com"},{"name":"mapbox-npm","email":"accounts+npmjs@mapbox.com"},{"name":"mapbox-npm-01","email":"accounts+npmjs-01@mapbox.com"},{"name":"mapbox-npm-02","email":"accounts+npmjs-02@mapbox.com"},{"name":"mapbox-npm-03","email":"accounts+npmjs-03@mapbox.com"},{"name":"mapbox-npm-04","email":"accounts+npmjs-04@mapbox.com"},{"name":"mapbox-npm-08","email":"accounts+npmjs-08@mapbox.com"},{"name":"mapbox-npm-09","email":"accounts+npmjs-09@mapbox.com"},{"name":"mapbox-npm-ci","email":"accounts+npmjs-npm-ci@mapbox.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/shelf-pack_3.2.0_1531491640990_0.2681459632204286"},"_hasShrinkwrap":false,"publish_time":1531491641104,"_cnpm_publish_time":1531491641104},"3.1.0":{"name":"@mapbox/shelf-pack","description":"A 2D rectangular bin packing data structure that uses the Shelf Best Height Fit heuristic","version":"3.1.0","main":"index.umd.js","jsnext:main":"index.js","license":"ISC","author":{"name":"Bryan Housel","email":"bryan@mapbox.com"},"repository":{"type":"git","url":"git+https://github.com/mapbox/shelf-pack.git"},"keywords":["bin packing","sprite"],"devDependencies":{"benchmark":"^2.1.0","bin-pack":"1.0.2","coveralls":"^2.11.12","documentation":"4.0.0-beta5","eslint":"^3.8.0","rollup":"0.41.4","tap":"^10.1.0"},"engines":{"node":">=4.0.0"},"scripts":{"bench":"npm run build && node bench/bench.js","build":"rollup -f umd -n ShelfPack index.js --no-indent --no-strict -o index.umd.js","docs":"documentation build index.js --lint --github --format html --output docs/","lint":"eslint index.js test/ bench/","test":"npm run build && npm run lint && tap --cov test/*.js"},"gitHead":"0df4c218bfef9ba059012e83a45f6418d566366c","bugs":{"url":"https://github.com/mapbox/shelf-pack/issues"},"homepage":"https://github.com/mapbox/shelf-pack#readme","_id":"@mapbox/shelf-pack@3.1.0","_shasum":"1edea9c0bf6715b217171ba60646c201af520f6a","_from":".","_npmVersion":"3.10.3","_nodeVersion":"6.7.0","_npmUser":{"name":"bhousel","email":"bryan@mapbox.com"},"dist":{"shasum":"1edea9c0bf6715b217171ba60646c201af520f6a","size":9380,"noattachment":false,"key":"/@mapbox/shelf-pack/-/@mapbox/shelf-pack-3.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@mapbox/shelf-pack/download/@mapbox/shelf-pack-3.1.0.tgz"},"maintainers":[{"name":"mapbox-machine-user","email":"accounts+npm-mapbox-machine-user@mapbox.com"},{"name":"mapbox-npm","email":"accounts+npmjs@mapbox.com"},{"name":"mapbox-npm-01","email":"accounts+npmjs-01@mapbox.com"},{"name":"mapbox-npm-02","email":"accounts+npmjs-02@mapbox.com"},{"name":"mapbox-npm-03","email":"accounts+npmjs-03@mapbox.com"},{"name":"mapbox-npm-04","email":"accounts+npmjs-04@mapbox.com"},{"name":"mapbox-npm-08","email":"accounts+npmjs-08@mapbox.com"},{"name":"mapbox-npm-09","email":"accounts+npmjs-09@mapbox.com"},{"name":"mapbox-npm-ci","email":"accounts+npmjs-npm-ci@mapbox.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/shelf-pack-3.1.0.tgz_1503637672176_0.6718796011991799"},"directories":{},"publish_time":1503637673296,"_cnpm_publish_time":1503637673296,"_hasShrinkwrap":false},"3.0.0":{"name":"@mapbox/shelf-pack","description":"A 2D rectangular bin packing data structure that uses the Shelf Best Height Fit heuristic","version":"3.0.0","main":"index.umd.js","jsnext:main":"index.js","license":"ISC","author":{"name":"Bryan Housel","email":"bryan@mapbox.com"},"repository":{"type":"git","url":"git+https://github.com/mapbox/shelf-pack.git"},"keywords":["bin packing","sprite"],"devDependencies":{"benchmark":"^2.1.0","bin-pack":"1.0.2","coveralls":"^2.11.12","documentation":"4.0.0-beta5","eslint":"^3.8.0","greenkeeper-postpublish":"^1.0.1","rollup":"0.41.4","tap":"^10.1.0"},"engines":{"node":">=4.0.0"},"scripts":{"bench":"npm run build && node bench/bench.js","build":"rollup -f umd -n ShelfPack index.js --no-indent --no-strict -o index.umd.js","docs":"documentation build index.js --lint --github --format html --output docs/","lint":"eslint index.js test/ bench/","test":"npm run build && npm run lint && tap --cov test/*.js","postpublish":"greenkeeper-postpublish"},"gitHead":"685083057b15f4e9dd26b7ee631d98ee66dadc11","bugs":{"url":"https://github.com/mapbox/shelf-pack/issues"},"homepage":"https://github.com/mapbox/shelf-pack#readme","_id":"@mapbox/shelf-pack@3.0.0","_shasum":"44e284c8336eeda1e9dbbb1d61954c70e26e5766","_from":".","_npmVersion":"3.10.3","_nodeVersion":"6.7.0","_npmUser":{"name":"bhousel","email":"bryan@mapbox.com"},"dist":{"shasum":"44e284c8336eeda1e9dbbb1d61954c70e26e5766","size":9285,"noattachment":false,"key":"/@mapbox/shelf-pack/-/@mapbox/shelf-pack-3.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@mapbox/shelf-pack/download/@mapbox/shelf-pack-3.0.0.tgz"},"maintainers":[{"name":"mapbox-machine-user","email":"accounts+npm-mapbox-machine-user@mapbox.com"},{"name":"mapbox-npm","email":"accounts+npmjs@mapbox.com"},{"name":"mapbox-npm-01","email":"accounts+npmjs-01@mapbox.com"},{"name":"mapbox-npm-02","email":"accounts+npmjs-02@mapbox.com"},{"name":"mapbox-npm-03","email":"accounts+npmjs-03@mapbox.com"},{"name":"mapbox-npm-04","email":"accounts+npmjs-04@mapbox.com"},{"name":"mapbox-npm-08","email":"accounts+npmjs-08@mapbox.com"},{"name":"mapbox-npm-09","email":"accounts+npmjs-09@mapbox.com"},{"name":"mapbox-npm-ci","email":"accounts+npmjs-npm-ci@mapbox.com"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/shelf-pack-3.0.0.tgz_1486960752384_0.3332601219881326"},"directories":{},"publish_time":1486960754361,"_cnpm_publish_time":1486960754361,"_hasShrinkwrap":false}},"readme":"[![npm version](https://badge.fury.io/js/%40mapbox%2Fshelf-pack.svg)](https://badge.fury.io/js/%40mapbox%2Fshelf-pack)\n[![Build Status](https://secure.travis-ci.org/mapbox/shelf-pack.svg)](http://travis-ci.org/mapbox/shelf-pack)\n[![Coverage Status](https://coveralls.io/repos/github/mapbox/shelf-pack/badge.svg?branch=master)](https://coveralls.io/github/mapbox/shelf-pack?branch=master)\n\n## shelf-pack\n\nA 2D rectangular [bin packing](https://en.wikipedia.org/wiki/Bin_packing_problem)\ndata structure that uses the Shelf Best Height Fit heuristic.\n\n\n### What is it?\n\n`shelf-pack` is a library for packing little rectangles into a big rectangle.  This sounds simple enough,\nbut finding an optimal packing is a problem with [NP-Complete](https://en.wikipedia.org/wiki/NP-completeness)\ncomplexity.  One useful application of bin packing is to assemble icons or glyphs into a sprite texture.\n\nThere are many ways to approach the bin packing problem, but `shelf-pack` uses the Shelf Best\nHeight Fit heuristic.  It works by dividing the total space into \"shelves\", each with a certain height.\nThe allocator packs rectangles onto whichever shelf minimizes the amount of wasted vertical space.\n\n`shelf-pack` is simple, fast, and works best when the rectangles have similar heights (icons and glyphs\nare like this).  It is not a generalized bin packer, and can potentially waste a lot of space if the\nrectangles vary significantly in height.\n\n\n### How fast is it?\n\nReally fast!  `shelf-pack` is several orders of magnitude faster than the more general\n[`bin-pack`](https://www.npmjs.com/package/bin-pack) library.\n\n```bash\n> npm run bench\n\nShelfPack single allocate fixed size bins x 1,610 ops/sec ±1.21% (90 runs sampled)\nShelfPack single allocate random width bins x 1,475 ops/sec ±1.00% (89 runs sampled)\nShelfPack single allocate random height bins x 1,458 ops/sec ±1.00% (90 runs sampled)\nShelfPack single allocate random height and width bins x 1,346 ops/sec ±0.96% (89 runs sampled)\nShelfPack batch allocate fixed size bins x 1,522 ops/sec ±1.06% (86 runs sampled)\nShelfPack batch allocate random width bins x 1,427 ops/sec ±1.06% (89 runs sampled)\nShelfPack batch allocate random height bins x 1,350 ops/sec ±1.63% (90 runs sampled)\nShelfPack batch allocate random height and width bins x 1,257 ops/sec ±1.02% (89 runs sampled)\nBinPack batch allocate fixed size bins x 2.21 ops/sec ±6.60% (10 runs sampled)\nBinPack batch allocate random width bins x 0.50 ops/sec ±2.25% (6 runs sampled)\nBinPack batch allocate random height bins x 0.51 ops/sec ±1.97% (6 runs sampled)\nBinPack batch allocate random height and width bins x 0.51 ops/sec ±1.37% (6 runs sampled)\n```\n\n\n### Usage\n\n#### Basic Usage\n\n```js\nvar ShelfPack = require('@mapbox/shelf-pack');\n\n// Initialize the sprite with a width and height..\nvar sprite = new ShelfPack(64, 64);\n\n// Pack bins one at a time..\nfor (var i = 0; i < 5; i++) {\n    // packOne() accepts parameters: `width`, `height`, `id`\n    // and returns a single allocated Bin object..\n    // `id` is optional - if you skip it, shelf-pack will make up a number for you..\n    // (Protip: numeric ids are much faster than string ids)\n\n    var bin = sprite.packOne(32, 32);\n    console.log(bin || 'out of space');\n}\n\n/* output:\nBin { id: 1, x: 0, y: 0, w: 32, h: 32, maxw: 32, maxh: 32, refcount: 1 }\nBin { id: 2, x: 32, y: 0, w: 32, h: 32, maxw: 32, maxh: 32, refcount: 1 }\nBin { id: 3, x: 0, y: 32, w: 32, h: 32, maxw: 32, maxh: 32, refcount: 1 }\nBin { id: 4, x: 32, y: 32, w: 32, h: 32, maxw: 32, maxh: 32, refcount: 1 }\nout of space\n*/\n\n// Clear sprite and start over..\nsprite.clear();\n\n// Or, resize sprite by passing larger dimensions..\nsprite.resize(128, 128);   // width, height\n\n```\n\n\n#### Batch packing\n\n```js\nvar ShelfPack = require('@mapbox/shelf-pack');\n\n// If you don't want to think about the size of the sprite,\n// the `autoResize` option will allow it to grow as needed..\nvar sprite = new ShelfPack(10, 10, { autoResize: true });\n\n// Bins can be allocated in batches..\n// Each requested bin should have `w`, `h` (or `width`, `height`) properties..\nvar requests = [\n    { id: 'a', width: 10, height: 10 },\n    { id: 'b', width: 10, height: 12 },\n    { id: 'c', w: 10, h: 12 },\n    { id: 'd', w: 10, h: 10 }\n];\n\n// pack() returns an Array of packed Bin objects..\nvar results = sprite.pack(requests);\n\nresults.forEach(function(bin) {\n    console.log(bin);\n});\n\n/* output:\nBin { id: 'a', x: 0, y: 0, w: 10, h: 10, maxw: 10, maxh: 10, refcount: 1 }\nBin { id: 'b', x: 0, y: 10, w: 10, h: 12, maxw: 10, maxh: 12, refcount: 1 }\nBin { id: 'c', x: 10, y: 10, w: 10, h: 12, maxw: 10, maxh: 12, refcount: 1 }\nBin { id: 'd', x: 10, y: 0, w: 10, h: 10, maxw: 10, maxh: 10, refcount: 1 }\n*/\n\n// If you don't mind letting ShelfPack modify your objects,\n// the `inPlace` option will decorate your bin objects with `x` and `y` properties.\n// Fancy!\nvar myBins = [\n    { id: 'e', width: 12, height: 24 },\n    { id: 'f', width: 12, height: 12 },\n    { id: 'g', w: 10, h: 10 }\n];\n\nsprite.pack(myBins, { inPlace: true });\nmyBins.forEach(function(bin) {\n    console.log(bin);\n});\n\n/* output:\n{ id: 'e', width: 12, height: 24, x: 0, y: 22 }\n{ id: 'f', width: 12, height: 12, x: 20, y: 10 }\n{ id: 'g', w: 10, h: 10, x: 20, y: 0 }\n*/\n\n```\n\n#### Reference Counting\n\n```js\nvar ShelfPack = require('@mapbox/shelf-pack');\n\n// Initialize the sprite with a width and height..\nvar sprite = new ShelfPack(64, 64);\n\n// Allocated bins are automatically reference counted.\n// They start out having a refcount of 1.\n[100, 101, 102].forEach(function(id) {\n    var bin = sprite.packOne(16, 16, id);\n    console.log(bin);\n});\n\n/* output:\nBin { id: 100, x: 0, y: 0, w: 16, h: 16, maxw: 16, maxh: 16, refcount: 1 }\nBin { id: 101, x: 16, y: 0, w: 16, h: 16, maxw: 16, maxh: 16, refcount: 1 }\nBin { id: 102, x: 32, y: 0, w: 16, h: 16, maxw: 16, maxh: 16, refcount: 1 }\n*/\n\n// If you try to pack the same id again, shelf-pack will not re-pack it.\n// Instead, it will increment the reference count automatically..\nvar bin102 = sprite.packOne(16, 16, 102);\nconsole.log(bin102);\n\n/* output:\nBin { id: 102, x: 32, y: 0, w: 16, h: 16, maxw: 16, maxh: 16, refcount: 2 }\n*/\n\n// You can also manually increment the reference count..\nvar bin101 = sprite.getBin(101);\nsprite.ref(bin101);\nconsole.log(bin101);\n\n/* output:\nBin { id: 101, x: 16, y: 0, w: 16, h: 16, maxw: 16, maxh: 16, refcount: 2 }\n*/\n\n// ...and decrement it!\nvar bin100 = sprite.getBin(100);\nsprite.unref(bin100);\nconsole.log(bin100);\n\n/* output:\nBin { id: 100, x: 0, y: 0, w: 16, h: 16, maxw: 16, maxh: 16, refcount: 0 }\n*/\n\n// Bins with a refcount of 0 are considered free space.\n// Next time a bin is packed, shelf-back tries to reuse free space first.\n// See how Bin 103 gets allocated at [0,0] - Bin 100's old spot!\nvar bin103 = sprite.packOne(16, 15, 103);\nconsole.log(bin103);\n\n/* output:\nBin { id: 103, x: 0, y: 0, w: 16, h: 15, maxw: 16, maxh: 16, refcount: 1 }\n*/\n\n// Bin 103 may be smaller (16x15) but it knows 16x16 was its original size.\n// If that space becomes free again, a 16x16 bin will still fit there.\nsprite.unref(bin103)\nvar bin104 = sprite.packOne(16, 16, 104);\nconsole.log(bin104);\n\n/* output:\nBin { id: 104, x: 0, y: 0, w: 16, h: 16, maxw: 16, maxh: 16, refcount: 1 }\n*/\n\n```\n\n\n### Documentation\n\nComplete API documentation is here:  http://mapbox.github.io/shelf-pack/\n\n\n### See also\n\nJ. Jylänky, \"A Thousand Ways to Pack the Bin - A Practical\nApproach to Two-Dimensional Rectangle Bin Packing,\"\nhttp://clb.demon.fi/files/RectangleBinPack.pdf, 2010\n","_attachments":{},"homepage":"https://github.com/mapbox/shelf-pack#readme","bugs":{"url":"https://github.com/mapbox/shelf-pack/issues"},"license":"ISC"}