-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
40 lines (32 loc) · 1.17 KB
/
utils.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
function round(x){
return Math.round(x*100)/100;
}
function getDimensions(id){
let image = document.getElementById(id)
let ratio = window.devicePixelRatio
let screenWidth = window.innerWidth
let screenWidthEm = Math.round(window.innerWidth / parseFloat(
getComputedStyle(
document.querySelector('body')
)['font-size'])
)
if(image){
return `logical dimensions: ${round(image.width)} x ${round(image.height)}<br>
physical dimensions: ${round(ratio*image.width)} x ${round(ratio*image.height)}<br>
device pixel ratio: ${round(ratio)}`
} else {
return `viewport width: ${round(screenWidth)} (${round(screenWidthEm)}em)<br>
physical viewport width: ${round(ratio*screenWidth)}`
}
}
function updateDimensions(){
const images = ['screen', 'hero', 'card1', 'card2', 'card3', 'card4']
images.forEach(image => {
let dimensions = document.getElementById('dimensions-' + image)
if(dimensions){
dimensions.innerHTML = getDimensions(image)
}
})
}
window.addEventListener('load', updateDimensions);
window.addEventListener('resize', updateDimensions);