HTML 计算网页的PPI

HTML 计算网页的PPI

vscode上安装live server插件,可以实时看网页预览

有个疑问: 鸿蒙density是按照类别写死的吗,手机520dpi 折叠屏426dpi 平板360dpi

html 复制代码
<html lang="en" data - overlayscrollbars - initialize>
  <header></header>
  <body>
    <div>
      <div id="messageID">heheh</div>
    </div>
    <script>
      function getdpi2() {
        const dpr = window.devicePixelRatio || 1;
        const measureDiv = document.createElement("inchUnitId");
        measureDiv.style.cssText = `
                width: 1in;
                height: 1in;
                border - color: blue;
                position: absolute;
                top: 0;
                left: 0;
                display: flex;
            `;
        document.body.appendChild(measureDiv);
        window.getComputedStyle(measureDiv).width;
        window.getComputedStyle(measureDiv).height;

        const cssWidth = measureDiv.offsetWidth;
        const cssHeight = measureDiv.offsetHeight;
        const physicalWidth = cssWidth / dpr;
        const physicalHeight = cssHeight / dpr;
        const dpx = Math.round(physicalWidth);
        const dpy = Math.round(physicalHeight);

        const screenWidth = window.screen.width * dpr;
        const screenHeight = window.screen.height * dpr;

        const screenRatio = (screenWidth / screenHeight).toFixed(4);
        const ppi = (
          Math.sqrt(Math.pow(physicalWidth, 2) + Math.pow(physicalHeight, 2)) /
          Math.sqrt(2)
        ).toFixed(4);
        const vppi = (ppi / 160).toFixed(4);

        // 手机ppi 应为 528dpi
        const phonePPI =
          Math.sqrt(Math.pow(1280, 2) + Math.pow(2720, 2)) / 6.82;
        const phoneVPPX = (phonePPI / 160).toFixed(4);

        // 平板ppi应为 366dpi
        const tablePPI =
          Math.sqrt(Math.pow(2560, 2) + Math.pow(1600, 2)) / 10.8;
        const tableVPPX = (tablePPI / 160).toFixed(4);

        // foldablePPI 应为 224dpi
        const foldablePPI =
          Math.sqrt(Math.pow(2224, 2) + Math.pow(2406, 2)) / 7.85;
        const foldableVPPX = (foldablePPI / 160).toFixed(4);

        const messageID = document.getElementById("messageID");
        messageID.innerHTML = `
                <p>dpx:${dpx}</p>                                                                                                                                      
                <p>dpy:${dpy}</p>
                <p>ppi:${ppi}</p>
                <p>phonePPI:${phonePPI}  phoneVPPX:${phoneVPPX}</p>
                <p>tablePPI:${tablePPI}  tableVPPX:${tableVPPX}</p>
                <p>foldablePPI:${foldablePPI}  foldableVPPX:${foldableVPPX}</p>
                <p>vppx:${vppi}</p>
                <p>devicePixelRatio:${devicePixelRatio}</p>
                <p>physicalWidth:${physicalWidth}</p>
                <p>physicalHeight:${physicalHeight}</p>
                <p>window.screen.width:${window.screen.width}</p>
                <p>window.screen.height:${window.screen.height}</p>
            `;
        document.body.removeChild(measureDiv);
      }

      setInterval(() => {
        getdpi2();
      }, 200);
    </script>
  </body>
</html>
相关推荐
巴巴_羊35 分钟前
xss和csrf
前端·xss·csrf
华子w90892585939 分钟前
基于 Python Web 应用框架 Django 的在线小说阅读平台设计与实现
前端·python·django
黑客飓风1 小时前
JavaScript性能优化实战
开发语言·javascript·性能优化
烛阴1 小时前
让你的Python并发飞起来:多线程开发实用技巧大全
前端·python
旺代1 小时前
Vue3中的v-model、computed、watch
前端
excel2 小时前
微信小程序鉴权登录详解 —— 基于 wx.login 与后端 openid 换取流程
前端
Gazer_S2 小时前
【前端隐蔽 Bug 深度剖析:SVG 组件复用中的 ID 冲突陷阱】
前端·bug
蓝婷儿3 小时前
每天一个前端小知识 Day 7 - 现代前端工程化与构建工具体系
前端
mfxcyh4 小时前
npm下载离线依赖包
前端·npm·node.js
waterHBO4 小时前
01 ( chrome 浏览器插件, 立马翻译), 设计
前端·chrome