Comparing version 1.0.2 to 2.0.0
75
index.js
@@ -1,16 +0,9 @@ | ||
(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) { | ||
export default function potpack(boxes) { | ||
// calculate total box area and maximum box width | ||
var area = 0; | ||
var maxWidth = 0; | ||
let area = 0; | ||
let maxWidth = 0; | ||
for (var i$1 = 0, list = boxes; i$1 < list.length; i$1 += 1) { | ||
var box = list[i$1]; | ||
for (const box of boxes) { | ||
area += box.w * box.h; | ||
@@ -21,23 +14,21 @@ maxWidth = Math.max(maxWidth, box.w); | ||
// sort the boxes for insertion by height, descending | ||
boxes.sort(function (a, b) { return b.h - a.h; }); | ||
boxes.sort((a, b) => 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); | ||
const 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}]; | ||
const spaces = [{x: 0, y: 0, w: startWidth, h: Infinity}]; | ||
var width = 0; | ||
var height = 0; | ||
let width = 0; | ||
let height = 0; | ||
for (var i$2 = 0, list$1 = boxes; i$2 < list$1.length; i$2 += 1) { | ||
for (const box of boxes) { | ||
// look through spaces backwards so that we check smaller spaces first | ||
var box$1 = list$1[i$2]; | ||
for (let i = spaces.length - 1; i >= 0; i--) { | ||
const space = spaces[i]; | ||
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; } | ||
if (box.w > space.w || box.h > space.h) continue; | ||
@@ -50,14 +41,14 @@ // found the space; add the box to its top-left corner | ||
// |_______________| | ||
box$1.x = space.x; | ||
box$1.y = space.y; | ||
box.x = space.x; | ||
box.y = space.y; | ||
height = Math.max(height, box$1.y + box$1.h); | ||
width = Math.max(width, box$1.x + box$1.w); | ||
height = Math.max(height, box.y + box.h); | ||
width = Math.max(width, box.x + box.w); | ||
if (box$1.w === space.w && box$1.h === space.h) { | ||
if (box.w === space.w && box.h === space.h) { | ||
// space matches the box exactly; remove it | ||
var last = spaces.pop(); | ||
if (i < spaces.length) { spaces[i] = last; } | ||
const last = spaces.pop(); | ||
if (i < spaces.length) spaces[i] = last; | ||
} else if (box$1.h === space.h) { | ||
} else if (box.h === space.h) { | ||
// space matches the box height; update it accordingly | ||
@@ -67,6 +58,6 @@ // |-------|---------------| | ||
// |_______|_______________| | ||
space.x += box$1.w; | ||
space.w -= box$1.w; | ||
space.x += box.w; | ||
space.w -= box.w; | ||
} else if (box$1.w === space.w) { | ||
} else if (box.w === space.w) { | ||
// space matches the box width; update it accordingly | ||
@@ -78,4 +69,4 @@ // |---------------| | ||
// |_______________| | ||
space.y += box$1.h; | ||
space.h -= box$1.h; | ||
space.y += box.h; | ||
space.h -= box.h; | ||
@@ -90,9 +81,9 @@ } else { | ||
spaces.push({ | ||
x: space.x + box$1.w, | ||
x: space.x + box.w, | ||
y: space.y, | ||
w: space.w - box$1.w, | ||
h: box$1.h | ||
w: space.w - box.w, | ||
h: box.h | ||
}); | ||
space.y += box$1.h; | ||
space.h -= box$1.h; | ||
space.y += box.h; | ||
space.h -= box.h; | ||
} | ||
@@ -109,5 +100,1 @@ break; | ||
} | ||
return potpack; | ||
})); |
{ | ||
"name": "potpack", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "A tiny library for packing 2D rectangles (for sprite layouts)", | ||
"main": "index", | ||
"module": "index.mjs", | ||
"unpkg": "index.js", | ||
"jsdelivr": "index.js", | ||
"main": "index.js", | ||
"type": "module", | ||
"exports": "./index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.mjs", | ||
"index.js", | ||
@@ -16,8 +14,4 @@ "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" | ||
"pretest": "eslint *.js", | ||
"test": "node test.js" | ||
}, | ||
@@ -46,10 +40,7 @@ "eslintConfig": { | ||
"@mapbox/shelf-pack": "^3.2.0", | ||
"@rollup/plugin-buble": "^0.21.3", | ||
"bin-pack": "^1.0.2", | ||
"eslint": "^8.0.1", | ||
"eslint": "^8.25.0", | ||
"eslint-config-mourner": "^3.0.0", | ||
"esm": "^3.2.25", | ||
"rollup": "^2.58.0", | ||
"tape": "^5.3.1" | ||
"tape": "^5.6.1" | ||
} | ||
} |
@@ -38,16 +38,17 @@ # potpack | ||
Install with NPM (`npm install potpack`) or Yarn (`yarn add potpack`), then: | ||
Install with NPM: `npm install potpack`. | ||
```js | ||
// import as an ES module | ||
import potpack from 'potpack'; | ||
Potpack is provided as a ES module, so it's only supported on modern browsers, excluding IE: | ||
// or require in Node / Browserify | ||
const potpack = require('potpack'); | ||
```html | ||
<script type="module"> | ||
import potpack from 'https://cdn.skypack.dev/potpack'; | ||
... | ||
</script> | ||
``` | ||
Or use a browser build directly: | ||
In Node, you can't use `require` — only `import` in ESM-capable versions (v12.15+): | ||
```html | ||
<script src="https://unpkg.com/potpack@1.0.1/index.js"></script> | ||
```js | ||
import potpack from 'potpack'; | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5
54
Yes
7114
5
114