-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppi.js
82 lines (74 loc) · 3.98 KB
/
ppi.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
// Identifies the correct screen density (i.e., PPI) -- DO NOT CHANGE!
// https://gist.github.com/scryptonite/5242987
function Display(facts, screen) { this.poll(facts, screen); }
Display.prototype.poll = function(facts, screen) {
screen = screen || window.screen;
this.facts = facts || this.facts || {};
this.screen = {};
this.screen.ppi = (screen.devicePixelRatio || devicePixelRatio) * 100;
this.screen.width = screen.width;
this.screen.height = screen.height;
this.screen.diagonal = Math.sqrt(Math.pow(this.screen.width, 2) + Math.pow(this.screen.height, 2));
this.guess = {};
this.guess.ppi = function() {
try {
var el = document.createElement("div");
el.style.width = "72pt";
document.body.appendChild(el);
var _ppi = (this.screen.ppi / 100) * (el.offsetWidth || 96);
document.body.removeChild(el);
return _ppi;
} catch (e) {
return 96;
}
}.bind(this)();
this.guess.width = this.screen.width / this.guess.ppi;
this.guess.height = this.screen.height / this.guess.ppi;
this.guess.diagonal = Math.sqrt(Math.pow(this.guess.width, 2) + Math.pow(this.guess.height, 2));
if ("diagonal" in this.facts) {
this.facts.width =
Math.sqrt(Math.pow(this.facts.diagonal, 2) -
Math.pow(this.facts.diagonal, 2) * Math.pow(this.screen.height / this.screen.width, 2));
this.facts.height = this.facts.width * (this.screen.height / this.screen.width);
this.facts.diagonal = this.facts.diagonal + 0;
this.facts.ppi = Math.round(this.screen.width /
Math.sqrt(Math.pow(this.facts.diagonal, 2) *
(Math.pow(this.facts.width, 2) /
(Math.pow(this.facts.width, 2) + Math.pow(this.facts.height, 2)))));
} else if ("width" in this.facts) {
this.facts.width = this.facts.width + 0;
this.facts.height = (this.screen.height / this.screen.width) * this.facts.width;
this.facts.diagonal = Math.sqrt(Math.pow(this.facts.width, 2) + Math.pow(this.facts.height, 2));
this.facts.ppi = Math.round(this.screen.width /
Math.sqrt(Math.pow(this.facts.diagonal, 2) *
(Math.pow(this.facts.width, 2) /
(Math.pow(this.facts.width, 2) + Math.pow(this.facts.height, 2)))));
} else if ("height" in this.facts) {
this.facts.width = (this.screen.width / this.screen.height) * this.facts.height;
this.facts.height = this.facts.height + 0;
this.facts.diagonal = Math.sqrt(Math.pow(this.facts.width, 2) + Math.pow(this.facts.height, 2));
this.facts.ppi = Math.round(this.screen.width /
Math.sqrt(Math.pow(this.facts.diagonal, 2) *
(Math.pow(this.facts.width, 2) /
(Math.pow(this.facts.width, 2) + Math.pow(this.facts.height, 2)))));
} else if ("ppi" in this.facts) {
this.facts.width = this.screen.width / this.facts.ppi;
this.facts.height = this.screen.height / this.facts.ppi;
this.facts.diagonal = Math.sqrt(Math.pow(this.facts.width, 2) + Math.pow(this.facts.height, 2));
this.facts.ppi = this.facts.ppi + 0;
} else {
this.facts.width = this.guess.width;
this.facts.height = this.guess.height;
this.facts.diagonal = this.guess.diagonal;
this.facts.ppi = this.guess.height;
}
this.width = this.facts.width || this.guess.width;
this.height = this.facts.height || this.guess.width;
this.diagonal = this.facts.diagonal || this.guess.diagonal;
this.ppi = this.facts.ppi || this.guess.ppi;
if (!this.facts.__halt) {
this.poll({ppi : this.ppi, __halt : true}, screen);
}
delete this.facts.__halt;
return this;
};