javascript获得屏幕的DPI
有两种办法获取屏幕的DPI
export function getDPI() {
const div = document.createElement('div')
div.style.cssText = 'height: 1in; left: -100%; position: absolute; top: -100%; width: 1in;'
document.body.appendChild(div)
const devicePixelRatio = window.devicePixelRatio || 1,
dpi = div.offsetWidth * devicePixelRatio;
console.log('dpi_x, dpi_y', dpi);
return dpi
}
function findFirstPositive (f,b=1,d=(e,g,c)=>g<e?-1:0<f(c=e+g>>>1)?c==e||0>=f(c-1)?c:d(e,c-1):d(c+1,g)) {
for (;0>=f(b);b<<=1);return d(b>>>1,b)|0
}
var dpi = findFirstPositive(x => matchMedia(`(max-resolution: ${x}dpi)`).matches)
console.log(dpi)