forked from LeaVerou/dpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpi.js
163 lines (128 loc) · 3.34 KB
/
dpi.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
152
153
154
155
156
157
158
159
160
161
162
163
var hashRegex = /^#(\d+)[x×](\d+)(@(\d*\.?\d+)["″])?|(\d*\.?\d+)["″]$/;
var dppx = window.devicePixelRatio ||
(window.matchMedia && window.matchMedia("(min-resolution: 2dppx), (-webkit-min-device-pixel-ratio: 1.5),(-moz-min-device-pixel-ratio: 1.5),(min-device-pixel-ratio: 1.5)").matches? 2 : 1) ||
1;
width.value = screen.width * dppx;
height.value = screen.height * dppx;
var output = $('output');
function calcDpi(w, h, d, opt) {
// Calculate PPI/DPI
w>0 || (w=1);
h>0 || (h=1);
opt || (opt='d');
var dpi = (opt=='d' ? Math.sqrt(w*w + h*h) : opt=='w' ? w : h) / d;
return dpi>0 ? Math.round(dpi) : 0;
}
function update() {
var w = width.value,
h = height.value;
result.textContent = calcDpi(w, h, physical.value, dimension.value);
// Size the output to have the same aspect ratio as the screen
var ratio = w/h;
output.style.minWidth = result.parentNode.offsetWidth;
if (ratio > 1) {
output.style.width = '';
}
else {
output.style.width = '10em';
}
output.style.height = output.offsetWidth / ratio + 'px';
}
dimension.onchange = update;
$$('fieldset input').forEach(function(element) {
(element.oninput = function () {
this.style.width = this.value.length * .7 + 'em';
this.style.width = this.value.length + 'ch';
update();
}).call(element);
});
$$('#resolutions a, #diagonals a').forEach(function(a) {
a.href = '#' + a.textContent;
});
$u.xhr({
url: 'screens.json',
callback: function (xhr) {
window.Devices = JSON.parse(xhr.responseText);
var fragment = document.createDocumentFragment();
Devices.forEach(function (device) {
device.ppi || ( device.ppi = calcDpi(device.w, device.h, device.d) );
deviceRow(device, fragment);
});
var tbody = $('table tbody', devices);
tbody.innerHTML = '';
tbody.appendChild(fragment);
}
});
function deviceRow(device, fragment) {
return $u.element.create('tr', {
contents: [
{
tag: 'th',
contents: {
tag: 'a',
properties: {
href: '#' + device.w + '×' + device.h + '@' + device.d + '″'
},
contents: device.name
}
}, {
tag: 'td',
contents: device.d + '″'
}, {
tag: 'td',
contents: device.w + '×' + device.h
}, {
tag: 'td',
contents: device.ppi
}, {
tag: 'td',
contents: device.dppx || 1
}
],
inside: fragment
});
}
(window.onhashchange = function() {
var hash = decodeURIComponent(location.hash);
if (hashRegex.test(hash)) {
var matches = hash.match(hashRegex);
if (matches[1]) {
width.value = matches[1]
width.oninput();
}
if (matches[2]) {
height.value = matches[2];
height.oninput();
}
if (matches[3] || matches[5]) {
if (matches[3]) {
physical.value = matches[4];
}
if (matches[5]) {
physical.value = matches[5];
}
dimension.value = 'd';
physical.oninput();
}
}
})();
search.oninput = function() {
if (!window.Devices) {
return;
}
var term = this.value;
var fragment = document.createDocumentFragment(),
results = 0;
Devices.forEach(function (device) {
for (var i in device) {
if ((device[i] + '').toLowerCase().indexOf(term.toLowerCase()) > -1) {
deviceRow(device, fragment);
results++;
return;
}
}
});
var tbody = $('table tbody', devices);
tbody.innerHTML = results? '' : '<tr><td colspan="4">No results</td></tr>';
tbody.appendChild(fragment);
};