• Stars
    star
    628
  • Rank 71,541 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A javascript binary tree based algorithm for 2d bin-packing suitable for generating CSS sprites

Binary Tree Algorithm for 2D Bin Packing

This project is a javascript experiment to write a binary tree based bin packing algorithm that is suitable for generating CSS sprites.

Demo

Clone this repository

git clone https://github.com/jakesgordon/bin-packing

View the index.html file in your favorite browser for examples of algorithm in use with lots of configurable options.

Usage

If you want to use this in your own javascript projects, you need something like this:

<script src='packer.growing.js'></script>
<script>
  var packer = new Packer(1000, 1000);   // or:  new GrowingPacker();
  var blocks = [
    { w: 100, h: 100 },
    { w: 100, h: 100 },
    { w:  80, h:  80 },
    { w:  80, h:  80 },
    etc
    etc
  ];

  blocks.sort(function(a,b) { return (b.h < a.h); }); // sort inputs for best results
  packer.fit(blocks);

  for(var n = 0 ; n < blocks.length ; n++) {
    var block = blocks[n];
    if (block.fit) {
      DrawRectangle(block.fit.x, block.fit.y, block.w, block.h);
    }
  }
</script>

See source code comments for more details.

License

See LICENSE file.

Contact

If you have any ideas, feedback, requests or bug reports, you can reach me at [email protected], or via my website: Code inComplete

More Repositories

1

javascript-state-machine

A javascript finite state machine library
JavaScript
8,690
star
2

javascript-racer

How to build an outrun-style racing game in javascript
HTML
1,033
star
3

sprite-factory

Automatic CSS Sprite Generator
Ruby
620
star
4

javascript-tetris

A simple javascript tetris game
HTML
586
star
5

javascript-pong

A javascript version of the classic 1972 arcade game Pong!
JavaScript
202
star
6

javascript-tiny-platformer

A very minimal javascript platform game
JavaScript
172
star
7

javascript-gauntlet

An HTML5 Gauntlet-style game
JavaScript
155
star
8

javascript-audio-fx

A Simple HTML5 Audio Helper Library
JavaScript
111
star
9

javascript-boulderdash

An HTML5 implementation of the c64 classic Boulderdash game
JavaScript
103
star
10

javascript-snakes

An HTML5 Snakes Game
HTML
95
star
11

javascript-tower-platformer

A prototype for an html5 rotating tower platformer game
JavaScript
86
star
12

javascript-breakout

An HTML5 canvas implementation of breakout
JavaScript
72
star
13

rack-rabbit

A Unicorn-style forking server for hosting rabbitMQ consumers as load balanced Rack apps
Ruby
62
star
14

javascript-starfield

An old school 2d starfield using HTML5 <canvas>
JavaScript
46
star
15

javascript-delta

An HTML5 javascript shoot em up in the style of the old c64 classic game "Delta"
JavaScript
43
star
16

ruby-sample-daemon

A simple example of daemonizing a long running Ruby process
Ruby
13
star
17

tetris-shoes

A Tetris game in Ruby Shoes
Ruby
5
star
18

javascript-stutter

Can anyone make this minimalist canvas animation smooth ?
JavaScript
3
star
19

dotfiles

Personal dotfiles
Vim Script
2
star
20

sprite-factory-and-rails31

Experimenting with sprite-factory and the rails 3.1 asset pipeline
Ruby
2
star
21

snowpack-coverage-bug

Reproducing a snowpack/web-test-runner coverage issue
JavaScript
1
star
22

unified-assets

Easily unify and minify static javascript and css assets
Ruby
1
star
23

vue-template-brunch

Adds support to Brunch for pre-compiling standalone (NOT single-file) vue templates.
JavaScript
1
star
24

ruby-serializer

Declarative serialization of PORO's to JSON
Ruby
1
star