-
Notifications
You must be signed in to change notification settings - Fork 161
/
MaxRectsPacker.js
74 lines (57 loc) · 1.89 KB
/
MaxRectsPacker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
let MaxRectsPackerEngine = require("maxrects-packer").MaxRectsPacker;
import Packer from "./Packer";
import Rect from "../math/Rect";
const METHOD = {
Smart: "Smart",
Square: "Square",
SmartSquare: "SmartSquare"
};
class MaxRectsPacker extends Packer {
constructor(width, height, allowRotate=false) {
super();
this.binWidth = width;
this.binHeight = height;
this.allowRotate = allowRotate;
}
pack(data, method) {
let options = {
smart: (method === METHOD.Smart || method === METHOD.SmartSquare),
pot: false,
square: (method === METHOD.Square || method === METHOD.SmartSquare)
};
let packer = new MaxRectsPackerEngine(this.binWidth, this.binHeight, 0, options);
let input = [];
for(let item of data) {
input.push({width: item.frame.w, height: item.frame.h, data: item});
}
packer.addArray(input);
let bin = packer.bins[0];
let rects = bin.rects;
let res = [];
for(let item of rects) {
item.data.frame.x = item.x;
item.data.frame.y = item.y;
res.push(item.data);
}
return res;
}
static get type() {
return "MaxRectsPacker";
}
static get methods() {
return METHOD;
}
static getMethodProps(id='') {
switch(id) {
case METHOD.Smart:
return {name: "Smart", description: ""};
case METHOD.Square:
return {name: "Square", description: ""};
case METHOD.SmartSquare:
return {name: "Smart square", description: ""};
default:
throw Error("Unknown method " + id);
}
}
}
export default MaxRectsPacker;