hxpotpack

This library is for sprite packing algorithm at runtime.
https://github.com/barisyild/hxpotpack/

To install, run:

haxelib install hxpotpack 1.0.3 

See using Haxelib in Haxelib documentation for more information.

README.md

hxpotpack

hxpotpack is the haxe port of the original potpack project.

A variation of algorithms used in rectpack2D and bin-pack, which are in turn based on this article by Blackpawn.

Ordered Example usage

import haxe.ds.Vector;
import hx.potpack.Potpack;
import hx.potpack.geom.PotpackRectangle; //or you can import "openfl.display.Rectangle" for openfl

class Test {
    static function main() {
        final boxes:Vector<PotpackRectangle> = new Vector<PotpackRectangle>(2);
        boxes.set(0, new PotpackRectangle(0, 0, 300, 50));
        boxes.set(1, new PotpackRectangle(0, 0, 100, 200));

        final data = Potpack.pack(boxes);
        trace('width: ${data.width}, height: ${data.height}, rect size: ${data.size} fill: ${data.fill}');

        // potpack mutates the boxes array: it's sorted by height,
        // and box objects are augmented with x, y coordinates:
        trace(boxes[0]); // {x: 0, y: 200, width: 300, height: 50}
        trace(boxes[1]); // {x: 0, y: 0, width: 100, height: 200}
    }
}

Unordered Example usage

import haxe.ds.Vector;
import hx.potpack.Potpack;
import hx.potpack.geom.PotpackRectangle; //or you can import "openfl.display.Rectangle" for openfl

class Test {
    static function main() {
        final boxes:Vector<PotpackRectangle> = new Vector<PotpackRectangle>(2);
        boxes.set(0, new PotpackRectangle(0, 0, 300, 50));
        boxes.set(1, new PotpackRectangle(0, 0, 100, 200));

        final data = Potpack.pack(boxes, false);
        trace('width: ${data.width}, height: ${data.height}, rect size: ${data.size} fill: ${data.fill}');

        // potpack mutates the boxes array: it's sorted by height,
        // and box objects are augmented with x, y coordinates:
        trace(boxes[0]); // {x: 0, y: 0, width: 100, height: 200}
        trace(boxes[1]); // {x: 0, y: 200, width: 300, height: 50}
    }
}

Compatibility

Compatible with all haxe projects.

There is an additional compatibility feature for OpenFL, you can use OpenFL Rectangles.

Install Release Version

Install with haxelib: haxelib install hxpotpack

Install Development Version

Install with haxelib: haxelib git hxpotpack https://github.com/barisyild/hxpotpack.git

Original project created by Mapbox

Contributors
barisyild
Version
1.0.3
Published
2 years ago
License
MIT

All libraries are free

Every month, more than a thousand developers use Haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on GitHub!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on GitHub

⌂