{"id":13425997,"url":"https://github.com/mapbox/shelf-pack","last_synced_at":"2025-03-15T20:31:47.415Z","repository":{"id":57358233,"uuid":"51096304","full_name":"mapbox/shelf-pack","owner":"mapbox","description":"A 2D rectangular bin packing data structure that uses the Shelf Best Height Fit heuristic","archived":false,"fork":false,"pushed_at":"2018-12-26T20:35:12.000Z","size":1767,"stargazers_count":132,"open_issues_count":9,"forks_count":18,"subscribers_count":116,"default_branch":"master","last_synced_at":"2025-03-01T16:36:03.012Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mapbox.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-04T18:32:05.000Z","updated_at":"2025-02-21T21:20:20.000Z","dependencies_parsed_at":"2022-09-06T21:41:03.129Z","dependency_job_id":null,"html_url":"https://github.com/mapbox/shelf-pack","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mapbox%2Fshelf-pack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mapbox%2Fshelf-pack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mapbox%2Fshelf-pack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mapbox%2Fshelf-pack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mapbox","download_url":"https://codeload.github.com/mapbox/shelf-pack/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243200745,"owners_count":20252688,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T00:01:23.843Z","updated_at":"2025-03-15T20:31:42.374Z","avatar_url":"https://github.com/mapbox.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"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\u003e 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 \u003c 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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmapbox%2Fshelf-pack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmapbox%2Fshelf-pack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmapbox%2Fshelf-pack/lists"}