Skip to content

Options ​

js
const packer = new MaxRectsPacker(maxWidth, maxHeight, padding = 0, options = {});

maxWidth and maxHeight are the bounds a normal bin stays inside. The placeholder bin an oversized rect gets is that rect's own size and can pass them — see oversized rects. padding keeps placed rects at least that many pixels apart, and is added to the footprint of each rect when the packer looks for free space. Omitting it or passing undefined uses 0. Numeric NaN, Infinity and -Infinity throw from the constructor; other JavaScript values retain the existing coercion behaviour, including string concatenation. If that arithmetic produces a non-finite bin size, placement is refused instead of retried indefinitely — see padding troubleshooting. options is a partial IOption:

OptionDefaultEffect
smarttrueGrow a bin to the smallest size that still fits, instead of jumping to maxWidth/maxHeight.
pottrueRound a grown bin up to the next power of two.
squarefalseKeep every normal bin square.
allowRotationfalseAllow 90° rotation while packing.
tagfalseGroup rects by tag — rect.data.tag, or a tag property on the rect itself.
exclusiveTagtrueWith tag, keep one tag per bin and label it with that tag. Set it to false to let several tags share a bin without an automatic bin label.
border0Atlas edge spacing: free space is kept border pixels away from a bin's edges.
logicMAX_EDGEHow free space is scored — see packing logic.

Options are copied by the packer and handed to each bin it creates, so a bin keeps the options it was created with even if you mutate the object you passed in afterwards.

Packing logic ​

options.logic picks the score findNode() uses to choose among the free rectangles a bin keeps:

ValueNameChooses
0PACKING_LOGIC.MAX_AREAThe free space with the smallest loss of area — good for tight, mixed sizes.
1PACKING_LOGIC.MAX_EDGEThe free space with the smallest loss of either width or height (the default).
2PACKING_LOGIC.FILL_WIDTHFills the current row width-first before starting the next row.
js
import { MaxRectsPacker, PACKING_LOGIC } from "maxrects-packer";

const packer = new MaxRectsPacker(1024, 1024, 0, { logic: PACKING_LOGIC.FILL_WIDTH });

FILL_WIDTH reports a usable bin.height only with { pot: false, square: false }, and pairs well with allowRotation: true: the other two settings round the grown bin up, so the height stops tracking what was actually filled.

Sizing ​

With the defaults (smart: true, pot: true) a bin starts small and grows only as far as the rect being placed requires, then rounds up to a power of two — so a packer declared as 4096 × 4096 that only ever sees 100×100 rects produces a 128×128 bin rather than a 4096×4096 one. square: true additionally takes the larger of the two dimensions, and border is subtracted from the usable area on every side.