To use MaxRectsPacker, import the class, initialize it with bin constraints and options, and then add rectangles using addArray or add.
Important Migration Notes:
- Since v2.0.0, use
import { MaxRectsPacker } from 'maxrects-packer' or require('maxrects-packer').MaxRectsPacker. - Since v2.1.0, you can pass any object with
width and height properties (no need for a specific schema). - The
Rectangle constructor order is (width, height, x, y, rotated).
let MaxRectsPacker = require("maxrects-packer").MaxRectsPacker;
const options = {
smart: true,
pot: true,
square: false,
allowRotation: true,
tag: false,
border: 5
};
// width, height, padding, options
let packer = new MaxRectsPacker(1024, 1024, 2, options);
let input = [
{width: 600, height: 20, name: "tree"},
{width: 600, height: 20, name: "flower"},
{width: 1000, height: 1000, name: "background"}
];
packer.addArray(input);
// Access the resulting bins
packer.bins.forEach(bin => {
console.log(bin.rects);
});