Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/math/size.js
4103 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview A utility class for representing two-dimensional sizes.
9
*/
10
11
12
goog.provide('goog.math.Size');
13
14
15
16
/**
17
* Class for representing sizes consisting of a width and height. Undefined
18
* width and height support is deprecated and results in compiler warning.
19
* @param {number} width Width.
20
* @param {number} height Height.
21
* @struct
22
* @constructor
23
*/
24
goog.math.Size = function(width, height) {
25
'use strict';
26
/**
27
* Width
28
* @type {number}
29
*/
30
this.width = width;
31
32
/**
33
* Height
34
* @type {number}
35
*/
36
this.height = height;
37
};
38
39
40
/**
41
* Compares sizes for equality.
42
* @param {goog.math.Size} a A Size.
43
* @param {goog.math.Size} b A Size.
44
* @return {boolean} True iff the sizes have equal widths and equal
45
* heights, or if both are null.
46
*/
47
goog.math.Size.equals = function(a, b) {
48
'use strict';
49
if (a == b) {
50
return true;
51
}
52
if (!a || !b) {
53
return false;
54
}
55
return a.width == b.width && a.height == b.height;
56
};
57
58
59
/**
60
* @return {!goog.math.Size} A new copy of the Size.
61
*/
62
goog.math.Size.prototype.clone = function() {
63
'use strict';
64
return new goog.math.Size(this.width, this.height);
65
};
66
67
68
if (goog.DEBUG) {
69
/**
70
* Returns a nice string representing size.
71
* @return {string} In the form (50 x 73).
72
* @override
73
*/
74
goog.math.Size.prototype.toString = function() {
75
'use strict';
76
return '(' + this.width + ' x ' + this.height + ')';
77
};
78
}
79
80
81
/**
82
* @return {number} The longer of the two dimensions in the size.
83
*/
84
goog.math.Size.prototype.getLongest = function() {
85
'use strict';
86
return Math.max(this.width, this.height);
87
};
88
89
90
/**
91
* @return {number} The shorter of the two dimensions in the size.
92
*/
93
goog.math.Size.prototype.getShortest = function() {
94
'use strict';
95
return Math.min(this.width, this.height);
96
};
97
98
99
/**
100
* @return {number} The area of the size (width * height).
101
*/
102
goog.math.Size.prototype.area = function() {
103
'use strict';
104
return this.width * this.height;
105
};
106
107
108
/**
109
* @return {number} The perimeter of the size (width + height) * 2.
110
*/
111
goog.math.Size.prototype.perimeter = function() {
112
'use strict';
113
return (this.width + this.height) * 2;
114
};
115
116
117
/**
118
* @return {number} The ratio of the size's width to its height.
119
*/
120
goog.math.Size.prototype.aspectRatio = function() {
121
'use strict';
122
return this.width / this.height;
123
};
124
125
126
/**
127
* @return {boolean} True if the size has zero area, false if both dimensions
128
* are non-zero numbers.
129
*/
130
goog.math.Size.prototype.isEmpty = function() {
131
'use strict';
132
return !this.area();
133
};
134
135
136
/**
137
* Clamps the width and height parameters upward to integer values.
138
* @return {!goog.math.Size} This size with ceil'd components.
139
*/
140
goog.math.Size.prototype.ceil = function() {
141
'use strict';
142
this.width = Math.ceil(this.width);
143
this.height = Math.ceil(this.height);
144
return this;
145
};
146
147
148
/**
149
* @param {!goog.math.Size} target The target size.
150
* @return {boolean} True if this Size is the same size or smaller than the
151
* target size in both dimensions.
152
*/
153
goog.math.Size.prototype.fitsInside = function(target) {
154
'use strict';
155
return this.width <= target.width && this.height <= target.height;
156
};
157
158
159
/**
160
* Clamps the width and height parameters downward to integer values.
161
* @return {!goog.math.Size} This size with floored components.
162
*/
163
goog.math.Size.prototype.floor = function() {
164
'use strict';
165
this.width = Math.floor(this.width);
166
this.height = Math.floor(this.height);
167
return this;
168
};
169
170
171
/**
172
* Rounds the width and height parameters to integer values.
173
* @return {!goog.math.Size} This size with rounded components.
174
*/
175
goog.math.Size.prototype.round = function() {
176
'use strict';
177
this.width = Math.round(this.width);
178
this.height = Math.round(this.height);
179
return this;
180
};
181
182
183
/**
184
* Scales this size by the given scale factors. The width and height are scaled
185
* by `sx` and `opt_sy` respectively. If `opt_sy` is not
186
* given, then `sx` is used for both the width and height.
187
* @param {number} sx The scale factor to use for the width.
188
* @param {number=} opt_sy The scale factor to use for the height.
189
* @return {!goog.math.Size} This Size object after scaling.
190
*/
191
goog.math.Size.prototype.scale = function(sx, opt_sy) {
192
'use strict';
193
const sy = (typeof opt_sy === 'number') ? opt_sy : sx;
194
this.width *= sx;
195
this.height *= sy;
196
return this;
197
};
198
199
200
/**
201
* Uniformly scales the size to perfectly cover the dimensions of a given size.
202
* If the size is already larger than the target, it will be scaled down to the
203
* minimum size at which it still covers the entire target. The original aspect
204
* ratio will be preserved.
205
*
206
* This function assumes that both Sizes contain strictly positive dimensions.
207
* @param {!goog.math.Size} target The target size.
208
* @return {!goog.math.Size} This Size object, after optional scaling.
209
*/
210
goog.math.Size.prototype.scaleToCover = function(target) {
211
'use strict';
212
const s = this.aspectRatio() <= target.aspectRatio() ?
213
target.width / this.width :
214
target.height / this.height;
215
216
return this.scale(s);
217
};
218
219
220
/**
221
* Uniformly scales the size to fit inside the dimensions of a given size. The
222
* original aspect ratio will be preserved.
223
*
224
* This function assumes that both Sizes contain strictly positive dimensions.
225
* @param {!goog.math.Size} target The target size.
226
* @return {!goog.math.Size} This Size object, after optional scaling.
227
*/
228
goog.math.Size.prototype.scaleToFit = function(target) {
229
'use strict';
230
const s = this.aspectRatio() > target.aspectRatio() ?
231
target.width / this.width :
232
target.height / this.height;
233
234
return this.scale(s);
235
};
236
237