Favivon - Correção

This commit is contained in:
2026-05-30 19:59:39 -03:00
parent 76ddaa815d
commit d7dfd221f0
32859 changed files with 5459654 additions and 404 deletions
+15
View File
@@ -0,0 +1,15 @@
ISC License
Copyright (c) 2018, Mapbox
Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
+52
View File
@@ -0,0 +1,52 @@
# 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](https://github.com/mapbox/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](https://github.com/TeamHypersomnia/rectpack2D) and
[bin-pack](https://github.com/bryanburgers/bin-pack),
which are in turn based on
[this article by Blackpawn](http://blackpawn.com/texts/lightmaps/default.html).
## [Demo](https://mapbox.github.io/potpack/)
## Example usage
```js
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`) or Yarn (`yarn add potpack`), then:
```js
// import as an ES module
import potpack from 'potpack';
// or require in Node / Browserify
const potpack = require('potpack');
```
Or use a browser build directly:
```html
<script src="https://unpkg.com/potpack@1.0.1/index.js"></script>
```
+39
View File
@@ -0,0 +1,39 @@
declare module "potpack" {
export interface PotpackBox {
w: number;
h: number;
/**
* X coordinate in the resulting container.
*/
x?: number;
/**
* Y coordinate in the resulting container.
*/
y?: number;
}
interface PotpackStats {
/**
* Width of the resulting container.
*/
w: number;
/**
* Height of the resulting container.
*/
h: number;
/**
* The space utilization value (0 to 1). Higher is better.
*/
fill: number;
}
/**
* Packs 2D rectangles into a near-square container.
*
* Mutates the {@link boxes} array: it's sorted by height,
* and box objects are augmented with `x`, `y` coordinates.
*/
const potpack: (boxes: PotpackBox[]) => PotpackStats;
export default potpack;
}
+107
View File
@@ -0,0 +1,107 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.potpack = factory());
})(this, (function () { 'use strict';
function potpack(boxes) {
// calculate total box area and maximum box width
var area = 0;
var maxWidth = 0;
for (var i$1 = 0, list = boxes; i$1 < list.length; i$1 += 1) {
var box = list[i$1];
area += box.w * box.h;
maxWidth = Math.max(maxWidth, box.w);
}
// sort the boxes for insertion by height, descending
boxes.sort(function (a, b) { return b.h - a.h; });
// aim for a squarish resulting container,
// slightly adjusted for sub-100% space utilization
var startWidth = Math.max(Math.ceil(Math.sqrt(area / 0.95)), maxWidth);
// start with a single empty space, unbounded at the bottom
var spaces = [{x: 0, y: 0, w: startWidth, h: Infinity}];
var width = 0;
var height = 0;
for (var i$2 = 0, list$1 = boxes; i$2 < list$1.length; i$2 += 1) {
// look through spaces backwards so that we check smaller spaces first
var box$1 = list$1[i$2];
for (var i = spaces.length - 1; i >= 0; i--) {
var space = spaces[i];
// look for empty spaces that can accommodate the current box
if (box$1.w > space.w || box$1.h > space.h) { continue; }
// found the space; add the box to its top-left corner
// |-------|-------|
// | box | |
// |_______| |
// | space |
// |_______________|
box$1.x = space.x;
box$1.y = space.y;
height = Math.max(height, box$1.y + box$1.h);
width = Math.max(width, box$1.x + box$1.w);
if (box$1.w === space.w && box$1.h === space.h) {
// space matches the box exactly; remove it
var last = spaces.pop();
if (i < spaces.length) { spaces[i] = last; }
} else if (box$1.h === space.h) {
// space matches the box height; update it accordingly
// |-------|---------------|
// | box | updated space |
// |_______|_______________|
space.x += box$1.w;
space.w -= box$1.w;
} else if (box$1.w === space.w) {
// space matches the box width; update it accordingly
// |---------------|
// | box |
// |_______________|
// | updated space |
// |_______________|
space.y += box$1.h;
space.h -= box$1.h;
} else {
// otherwise the box splits the space into two spaces
// |-------|-----------|
// | box | new space |
// |_______|___________|
// | updated space |
// |___________________|
spaces.push({
x: space.x + box$1.w,
y: space.y,
w: space.w - box$1.w,
h: box$1.h
});
space.y += box$1.h;
space.h -= box$1.h;
}
break;
}
}
return {
w: width, // container width
h: height, // container height
fill: (area / (width * height)) || 0 // space utilization
};
}
return potpack;
}));
+94
View File
@@ -0,0 +1,94 @@
export default function potpack(boxes) {
// calculate total box area and maximum box width
let area = 0;
let maxWidth = 0;
for (const box of boxes) {
area += box.w * box.h;
maxWidth = Math.max(maxWidth, box.w);
}
// sort the boxes for insertion by height, descending
boxes.sort((a, b) => b.h - a.h);
// aim for a squarish resulting container,
// slightly adjusted for sub-100% space utilization
const startWidth = Math.max(Math.ceil(Math.sqrt(area / 0.95)), maxWidth);
// start with a single empty space, unbounded at the bottom
const spaces = [{x: 0, y: 0, w: startWidth, h: Infinity}];
let width = 0;
let height = 0;
for (const box of boxes) {
// look through spaces backwards so that we check smaller spaces first
for (let i = spaces.length - 1; i >= 0; i--) {
const space = spaces[i];
// look for empty spaces that can accommodate the current box
if (box.w > space.w || box.h > space.h) continue;
// found the space; add the box to its top-left corner
// |-------|-------|
// | box | |
// |_______| |
// | space |
// |_______________|
box.x = space.x;
box.y = space.y;
height = Math.max(height, box.y + box.h);
width = Math.max(width, box.x + box.w);
if (box.w === space.w && box.h === space.h) {
// space matches the box exactly; remove it
const last = spaces.pop();
if (i < spaces.length) spaces[i] = last;
} else if (box.h === space.h) {
// space matches the box height; update it accordingly
// |-------|---------------|
// | box | updated space |
// |_______|_______________|
space.x += box.w;
space.w -= box.w;
} else if (box.w === space.w) {
// space matches the box width; update it accordingly
// |---------------|
// | box |
// |_______________|
// | updated space |
// |_______________|
space.y += box.h;
space.h -= box.h;
} else {
// otherwise the box splits the space into two spaces
// |-------|-----------|
// | box | new space |
// |_______|___________|
// | updated space |
// |___________________|
spaces.push({
x: space.x + box.w,
y: space.y,
w: space.w - box.w,
h: box.h
});
space.y += box.h;
space.h -= box.h;
}
break;
}
}
return {
w: width, // container width
h: height, // container height
fill: (area / (width * height)) || 0 // space utilization
};
}
+53
View File
@@ -0,0 +1,53 @@
{
"name": "potpack",
"version": "1.0.2",
"description": "A tiny library for packing 2D rectangles (for sprite layouts)",
"main": "index",
"module": "index.mjs",
"unpkg": "index.js",
"jsdelivr": "index.js",
"types": "index.d.ts",
"files": [
"index.mjs",
"index.js",
"index.d.ts"
],
"scripts": {
"pretest": "eslint index.mjs test.mjs bench.mjs",
"test": "node -r esm test.mjs",
"bench": "node -r esm bench.mjs",
"build": "rollup -c",
"start": "rollup -w",
"prepublishOnly": "npm run build"
},
"eslintConfig": {
"extends": "mourner"
},
"repository": {
"type": "git",
"url": "git+https://github.com/mapbox/potpack.git"
},
"keywords": [
"algorithms",
"sprites",
"bin packing",
"geometry",
"rectangles"
],
"author": "Vladimir Agafonkin",
"license": "ISC",
"bugs": {
"url": "https://github.com/mapbox/potpack/issues"
},
"homepage": "https://mapbox.github.io/potpack/",
"devDependencies": {
"@mapbox/shelf-pack": "^3.2.0",
"@rollup/plugin-buble": "^0.21.3",
"bin-pack": "^1.0.2",
"eslint": "^8.0.1",
"eslint-config-mourner": "^3.0.0",
"esm": "^3.2.25",
"rollup": "^2.58.0",
"tape": "^5.3.1"
}
}