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>
相关推荐
前端九哥2 分钟前
装个依赖把公司电脑干报废了?npm i 到底背着我干了啥?
前端·javascript
溪海莘8 分钟前
React入门:跟读官方快速入门教程(前端小白)
前端·react.js·前端框架
绝世唐门三哥27 分钟前
工具函数-精准判断美东交易时间
前端·javascript·vue.js
松涛和鸣31 分钟前
DAY52 7-Segment Display/GPIO/Buttons/Interrupts/Timers/PWM
c语言·数据库·单片机·sqlite·html
Moment32 分钟前
如何一次性生成 60 种语气表达?RWKV 模型告诉你答案 ❗❗❗
前端·后端·aigc
踢球的打工仔42 分钟前
typescript-null和undefined
前端·javascript·typescript
前端小蜗1 小时前
对不起,我很贱:老板还没催,我自己就统计《GitLab年度代码报告》
前端·javascript·人工智能
佛系打工仔1 小时前
绘制K线第三章:拖拽功能实现
android·前端·ios
cauyyl1 小时前
react 项目检查国际化配置脚本
前端·react.js·前端框架
康一夏1 小时前
React面试题,useRef和普通变量的区别
前端·javascript·react.js