This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
util.js
151 lines (138 loc) · 3.3 KB
/
util.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (c) 2015-2016, Salesforce.com, Inc.
* All rights reserved.
* Licensed under the MIT license.
* For full license text, see LICENSE.md file in the repo root or
* https://opensource.org/licenses/MIT
*/
'use strict';
class BBox {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.fill();
}
fill() {
this.x2 = this.x + this.w;
this.y2 = this.y + this.h;
this.cx = this.x + this.w / 2;
this.cy = this.y + this.h / 2;
return this;
}
padLabs(amount) {
return new BBox(this.x + amount, this.y, this.w - amount, this.h);
}
padL(frac) {
return this.padLabs(this.w * frac);
}
padRabs(amount) {
return new BBox(this.x, this.y, this.w - amount, this.h);
}
padR(frac) {
return this.padRabs(this.w * frac);
}
padTabs(amount) {
return new BBox(this.x, this.y + amount, this.w, this.h - amount);
}
padT(frac) {
return this.padTabs(this.h * frac);
}
padBabs(amount) {
return new BBox(this.x, this.y, this.w, this.h - amount);
}
padB(frac) {
return this.padBabs(this.h * frac);
}
padLRabs(amount) {
return new BBox(this.x + amount, this.y, this.w - 2 * amount, this.h);
}
padLR(frac) {
return this.padLRabs(this.w * frac);
}
padTBabs(amount) {
return new BBox(this.x, this.y + amount, this.w, this.h - 2 * amount);
}
padTB(frac) {
return this.padTBabs(this.h * frac);
}
pad(frac) {
return this.padLR(frac).padTB(frac);
}
setX(amount) {
return new BBox(amount, this.y, this.w, this.h);
}
setY(amount) {
return new BBox(this.x, amount, this.w, this.h);
}
setW(amount) {
return new BBox(this.x, this.y, amount, this.h);
}
setWR(amount) {
return new BBox(this.x + this.w - amount, this.y, amount, this.h);
}
scaleW(frac) {
return this.setW(this.w * frac);
}
scaleWR(frac) {
return this.setWR(this.w * frac);
}
setH(amount) {
return new BBox(this.x, this.y, this.w, amount);
}
setHB(amount) {
return new BBox(this.x, this.y + this.h - amount, this.w, amount);
}
scaleH(frac) {
return this.setH(this.h * frac);
}
scaleHB(frac) {
return this.setHB(this.h * frac);
}
squareMin() {
let wh = Math.min(this.w, this.h);
return new BBox(this.x, this.y, wh, wh);
}
squareMax() {
let wh = Math.max(this.w, this.h);
return new BBox(this.x, this.y, wh, wh);
}
sliceH(pieces, start, stop) {
if (stop === undefined) {
stop = start;
}
let pieceWidth = this.w / pieces;
return new BBox(this.x + pieceWidth * start, this.y, pieceWidth * (stop + 1 - start), this.h);
}
sliceV(pieces, start, stop) {
if (stop === undefined) {
stop = start;
}
let pieceHeight = this.h / pieces;
return new BBox(this.x, this.y + pieceHeight * start, this.w, pieceHeight * (stop + 1 - start));
}
maxW(amount) {
return this.setW(Math.min(this.w, amount));
}
maxH(amount) {
return this.setH(Math.min(this.h, amount));
}
}
let fillBBox = (bbox) => {
bbox.x2 = bbox.x + bbox.w;
bbox.y2 = bbox.y + bbox.h;
bbox.cx = bbox.x + bbox.w / 2;
bbox.cy = bbox.y + bbox.h / 2;
return bbox;
};
let fontSize = bbox => {
return Math.min(
bbox.x2 - bbox.x,
bbox.y2 - bbox.y);
};
module.exports = {
fillBBox: fillBBox,
fontSize: fontSize,
BBox: BBox,
};