sailei1 / blog

1 stars 0 forks source link

屏幕dpi 运算 #62

Closed sailei1 closed 5 years ago

sailei1 commented 5 years ago

dpi: 每英寸多少点,ppi是 Pixel per inch,每英寸像素数

      // i 英寸 x 屏幕宽 y 屏幕高     
       function getDPI(i, x, y) {
    var d = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2))
    if (i > 0){
        return d/i;
    }
    return 0;
}

function ES_round(v, accuracy) {
    if (isNaN(accuracy)){
        accuracy = 0;
     }
    return Math.round(v * Math.pow(10, accuracy)) / Math.pow(10, accuracy);
}

var dpi = ES_round(getDPI(inch, screen.width, screen.height), 2);