1. potpack
A tiny library for packing 2D rectangles (for sprite layouts)
potpack
Package: potpack
Created by: mapbox
Last modified: Mon, 17 Oct 2022 10:01:17 GMT
Version: 2.0.0
License: ISC
Downloads: 4,897,195
Repository: https://github.com/mapbox/potpack

Install

npm install potpack
yarn add potpack

potpack

A tiny JavaScript library for packing 2D rectangles into a near-square container,
which is useful for generating CSS sprites and WebGL textures. Similar to shelf-pack,
but static (you can't add items once a layout is generated), and aims for maximal space utilization.

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

Demo

Example usage

 import potpack from 'potpack';

const boxes = [
    {w: 300, h: 50},
    {w: 100, h: 200},
    ...
];

const {w, h, fill} = potpack(boxes);
// w and h are resulting container's width and height;
// fill is the space utilization value (0 to 1), higher is better

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

Install

Install with NPM: npm install potpack.

Potpack is provided as a ES module, so it's only supported on modern browsers, excluding IE:

 <script type="module">
import potpack from 'https://cdn.skypack.dev/potpack';
...
</script>

In Node, you can't use require — only import in ESM-capable versions (v12.15+):

 import potpack from 'potpack';

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code