npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

efficient-rect-packer

v1.1.2

Published

An efficient 2D bin packing algorithm designed for optimal arrangement of rectangular items on fixed-size surfaces, ideal for applications like image layout on paper sheets, minimizing waste and maximizing space utilization.

Downloads

68

Readme

Efficient Rect Packer

Description

Efficient Rect Packer is a powerful and efficient 2D bin packing algorithm designed for the optimal arrangement of rectangular items within a fixed-size container or sheet. This library is particularly useful for a wide range of applications where efficient space utilization is key, such as in material cutting, shelf space allocation, or any scenario that requires packing a bunch of rectangles as efficiently as possible within a fixed size container. The algorithm used in this library is based on research by Mao Chen, which can be found here.

Features

  • Efficient Packing Algorithm: Efficiently packs rectangles inside a container of fixed dimensions.
  • Customizable Options: Supports padding, margins, and rotation settings for rectangles.
  • Easy to Use: Simple and intuitive API.

Installation

Install the package using npm:

npm install efficient-rect-packer

Or using yarn:

yard add efficient-rect-packer

Usage

Here's a quick example to get you started:

import { pack } from "efficient-rect-packer";

const rectangles = [
    { id: "rect1", w: 100, h: 200 },
    { id: "rect2", w: 150, h: 100 },
    // ... more rectangles ...
];

const containerSize = { w: 500, h: 500 };

const options = {
    padding: 10,
    margin: { top: 5, right: 5, bottom: 5, left: 5 },
    noRotation: false,
};

pack(rectangles, containerSize, options)
    .then((result) => {
        console.log(result);
    })
    .catch((error) => {
        console.error(error);
    });

Example Output

Given the above example, the output might look like this:

{
    "packed_rectangles": [
        { "id": "rect1", "w": 100, "h": 200, "x": 5, "y": 5, "rotated": false },
        {
            "id": "rect2",
            "w": 150,
            "h": 100,
            "x": 115,
            "y": 5,
            "rotated": true
        },
        {
            "id": "rect3",
            "w": 200,
            "h": 150,
            "x": 275,
            "y": 5,
            "rotated": false
        }
    ],
    "unpacked_rectangles": [],
    "isRemaining": false
}

Demo

A practical application of this library is an online tool for efficiently packing images onto A4-sized paper for printing. This tool helps to minimize the number of pages required to print all the photos, saving paper and reducing printing costs. You can check out the demo here.

API Reference

pack(rects, container_size, options)

  • rects: Array of UnpackedRect objects (each having id, w, h).
  • container_size: Dimension object specifying the width (w) and height (h) of the container.
  • options: (Optional) Options object.
    • padding: (Optional) Number specifying padding between rectangles.
    • margin: (Optional) Margin object specifying top, right, bottom, and left margins.
    • noRotation: (Optional) Boolean to disable rotation of rectangles.