https://github.com/bramp/binpack
Simple library for 2D bin packing, as used for texture atlases.
https://github.com/bramp/binpack
Last synced: over 1 year ago
JSON representation
Simple library for 2D bin packing, as used for texture atlases.
- Host: GitHub
- URL: https://github.com/bramp/binpack
- Owner: bramp
- License: bsd-2-clause
- Created: 2024-02-20T18:51:16.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-21T07:02:15.000Z (over 2 years ago)
- Last Synced: 2025-03-25T18:51:07.421Z (over 1 year ago)
- Language: Dart
- Size: 105 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Simple library for 2D bin packing, as used for texture atlases.
[](https://pub.dev/packages/binpack)
[](https://pub.dev/publishers/bramp.net/packages)
[](https://github.com/bramp/binpack/actions/workflows/dart.yml)
## Features
Takes a list of rectangles and packs them into a larger rectangle, with the goal
of minimizing the larger rectangle's size.
Uses the algorithm described at https://github.com/TeamHypersomnia/rectpack2D
## Usage
```dart
import 'package:binpack/binpack.dart';
// Create a list of key and rectanges.
// The key can be anything specific to your application.
final rects = [
('image1.png', Rectangle(0, 0, 100, 200)), // Image 100x200 pixels
('image2.png', Rectangle(0, 0, 32, 64)),
('image3.png', Rectangle(0, 0, 128, 100)),
('image4.png', Rectangle(0, 0, 300, 400)),
];
// Target the max size 4096 x 4096
final results = Binpacker(4096, 4096).pack(rects);
// The rectangles are now packed and their positions available:
print(results.placements);
```
## Example
Packing 168 rects (from a game) into 4060x4065, with 95.7% efficiency.
