https://github.com/semibran/pack
:package: greedy rectangle packing algorithm
https://github.com/semibran/pack
bin-packing combinatorial-optimization rectangle-packing
Last synced: 7 months ago
JSON representation
:package: greedy rectangle packing algorithm
- Host: GitHub
- URL: https://github.com/semibran/pack
- Owner: semibran
- License: mit
- Created: 2018-01-29T07:06:37.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-08-29T06:31:03.000Z (about 2 years ago)
- Last Synced: 2024-10-20T12:44:46.427Z (12 months ago)
- Topics: bin-packing, combinatorial-optimization, rectangle-packing
- Language: JavaScript
- Homepage: https://semibran.github.io/pack
- Size: 339 KB
- Stars: 37
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# pack
> lightweight rectangle packer[View the demo!](https://semibran.github.io/pack)

`pack` is a [box packing algorithm](https://en.wikipedia.org/wiki/Bin_packing_problem) which attempts to determine the most compact configuration for a set of rectangles. To this end, it employs the first-fit decreasing (FFD) algorithm to place the boxes, using the length of the layout's shortest side length as a heuristic.
Unlike traditional bin packing algorithms, the boxes here are placed in a single container that grows appropriately as they are packed inside. This property makes it ideal for assembling spritesheets given a list of images.
## usage
[](https://www.npmjs.com/package/pack "View npm package")### `pack(sizes) -> layout`
Arranges a list of `{ width, height }` pairs into the most compact `layout` possible. The result will be reminiscent of the following data structure:
```js
var layout = {
width: 13,
height: 8,
rects: [
{ x: 0, y: 0, width: 5, height: 3 },
{ x: 0, y: 3, width: 6, height: 5 }
{ x: 6, y: 0, width: 5, height: 4 },
{ x: 11, y: 0, width: 2, height: 4 },
{ x: 6, y: 4, width: 7, height: 4 },
{ x: 5, y: 0, width: 1, height: 3 },
]
}
```## kudos
* [leo](https://github.com/leo) - for donating the package name## license
[MIT](https://opensource.org/licenses/MIT) © [Brandon Semilla](https://git.io/semibran)