Code Monkey home page Code Monkey logo

pack's Introduction

pack

lightweight rectangle packer

View the demo!

boxes

pack is a box packing algorithm 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

npm

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:

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 - for donating the package name

license

MIT © Brandon Semilla

pack's People

Contributors

dependabot[bot] avatar semibran avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pack's Issues

demo

Would be neat to have an interactive example on gh-pages to demonstrate how the algorithm works:thinking:

better-looking results

The algorithm already minimizes whitespace, but I'm currently searching for a heuristic that prefers layouts that place whitespace closer to the bottom-right corner than leaving them strewn arbitrarily throughout the configuration.

An example of a "bad" configuration (generated automatically):

bad boxes

a "good" configuration (tweaked manually):

good boxes

The second one is easily the more compact revision. Unfortunately, calculating a score based on whitespace "position" is a lot more difficult than it appears. I've tried sorting by a lot of different positional shenanigans with the boxes (note that this algorithm isn't strictly bound to a grid) but no satisfactory results have come up yet.

optimization

There are actually plenty of good box packing algorithms out there, but I made my own version without much regard for optimization since I had planned on using it for a spritesheet generator anyway. Surprisingly, I've found that arranging a mere six boxes creates upwards of 1000 permutations on average and already takes a few seconds to load. Seven boxes made Chromium lock up entirely in my own experience and required a system restart.

I haven't done benchmarks yet (which is another issue entirely), but I'm assuming there's a large performance bottleneck stemming from recursion and the subsequent mass object creation that's going on.

Basically, I'd love to create an imperative version of this algorithm or at least find some loopholes to keep memory usage to a bare minimum.

3D and beyond

Vectors are currently modeled as arrays in the current implementation, adding an extra layer of versatility. The current algorithm should be expanded to cover any dimension.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.