1 小程序dpr 之坑 怎么计算真实dpr 信息?

739 阅读1分钟

小程序接口 wx.getSystemInfoSync(); 能获取到设备信息如下

{

     "model":"iPhone 6/7/8 Plus",

      "pixelRatio":3,

      "windowWidth":414,

      "windowHeight":672,

     "system":"iOS 10.0.1",

      "language":"zh_CN",

      "version":"8.0.5",

      "deviceOrientation":"portrait",

      "screenWidth":414,

       screenHeight":736,

       "SDKVersion":"2.21.4",

      "brand":"devtools",

      "fontSizeSetting":16,

      "benchmarkLevel":1,

       "batteryLevel":100,

       "statusBarHeight":20,

       "bluetoothEnabled":true,

       "locationEnabled":true,

       "wifiEnabled":true,

       "cameraAuthorized":true,

       "locationAuthorized":true,

       "microphoneAuthorized":true,

       "notificationAuthorized":true,

       "safeArea":{

           "top":20,
           "left":0,
           "right":414,
           "bottom":736,
           "width":414,
           "height":716

    },

   "platform":"devtools",

   "enableDebug":false,

   "devicePixelRatio":3

}

设备像素比 "pixelRatio":3 "devicePixelRatio":3 这个比值3 在页面布进行rpx换算的时候会发现是不能用的比如

image.png

布局width:100rpx;height:106rpx;在页面中实际大小 width:55px;height:58px 反推像素比100/55=1.81818181818 明显不是接口获取的到的3,接口害人不浅.实际计算为750/safeArea.width750/414=1.8115942029 这两个值已经很相近 直接保留两位小说 1.81参与计算误差在接受范围内.