forked from apg/timer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtextfit.js
More file actions
27 lines (26 loc) · 757 Bytes
/
textfit.js
File metadata and controls
27 lines (26 loc) · 757 Bytes
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
/**
* (C) 2007 Andrew Gwozdziewycz <apgwoz@gmail.com>
* VERSION: 0.1, Released under an MIT License.
* URL: http://www.apgwoz.com/textfit/
**/
function textFit(node, minfs, maxfs, width, height) {
var fits = function(sz) {
node.style.fontSize = sz + 'px';
return node.offsetWidth <= width && node.offsetHeight <= height;
};
var hfs = maxfs - minfs;
var tst = minfs + Math.ceil(hfs / 2);
var opsz = node.style.fontSize;
while ((hfs/2) >= 1) {
if (fits(tst)) {
minfs = tst;
}
else {
maxfs = tst;
}
hfs = maxfs - minfs;
tst = minfs + Math.ceil(hfs/2);
}
opsz = fits(maxfs) ? maxfs: minfs;
node.style.fontSize = opsz + 'px';
}