<html>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<body>

纸张宽度(毫米mm):<input type="text" id="width" value="10"> <span id="width_px"></span>
<br>纸张宽度(毫米cm):<input type="text" id="height" value="10"> <span id="height_px"></span>
<br><input type="button" value="计算" οnclick="compute(1)">
<input type="button" value="计算And画格子" οnclick="compute(2)">
<input type="button" value="一键生成多个格子(生成以后可以打印出来在纸上用尺子量一下对不对)" οnclick="make()">
<br><br>
<div id="testDiv">
<div id="testDiv1"></div>
</div>
</body>
<script>
function make(){
document.getElementById("width").value=10;
compute(2);
document.getElementById("width").value=20;
compute(2);
document.getElementById("width").value=30;
compute(2);
document.getElementById("width").value=40;
compute(2);
document.getElementById("width").value=60;
compute(2);
document.getElementById("width").value=80;
compute(2);
document.getElementById("width").value=100;
compute(2);
document.getElementById("width").value=150;
compute(2);
}

function compute(t){
var width=document.getElementById("width").value;
var height=document.getElementById("height").value;
console.log("width:"+width)
console.log("height:"+height)
var width_px=cm2px(width);
var height_px=cm2px(height);
console.log("width_px:"+width_px)
console.log("height:"+height)
document.getElementById("width_px").innerHTML=width_px+" px";
document.getElementById("height_px").innerHTML=height_px+" px";

if(t==2){
var tmpNode = document.createElement("DIV");
tmpNode.setAttribute('style', 'width:'+width_px+'px;height:'+height_px+'px;border:solid 1px #000;text-align:center');
tmpNode.innerHTML=width+"*"+height+"("+width_px+"*"+height_px+")";
document.getElementById("testDiv").appendChild(tmpNode);
}else{
document.getElementById("testDiv1").setAttribute('style', 'width:'+width_px+'px;height:'+height_px+'px;border:solid 1px #000');
}

}
//根据毫米算DPI
function cm2px(cm) {
var dpi = getDPI();
var pixel = parseFloat(cm) / 25.4 * dpi[0]; //只计算x轴的dPI
return (parseInt(pixel))
}

function getDPI() {
var arrDPI = new Array();
if (window.screen.deviceXDPI != undefined) {//ie 9
arrDPI[0] = window.screen.deviceXDPI;
arrDPI[1] = window.screen.deviceYDPI;
}else {//chrome firefox
var tmpNode = document.createElement("DIV");
tmpNode.style.cssText = "width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden";
document.body.appendChild(tmpNode);
arrDPI[0] = parseInt(tmpNode.offsetWidth);
arrDPI[1] = parseInt(tmpNode.offsetHeight);
tmpNode.parentNode.removeChild(tmpNode);

}
return arrDPI;
}
console.log("dpi:"+getDPI());
</script>
</html>

转载于:https://www.cnblogs.com/q149072205/p/9714664.html

js根据毫米/厘米算像素px相关推荐

  1. 常见图片尺寸大小(厘米 cm,象素 px)

    一英寸=72pt(点)=96px(像素) 身份证(驾照)照片:22*32 mm (小1寸) 小1寸: 27*38 mm 1寸:25*38 mm 普通护照照片:33*48mm(大1寸) 大1寸:40*5 ...

  2. css像素px,物理像素(pt),设备像素比(dpr),1px边框问题,viewport自适应

    关于 css像素px,物理像素(pt),设备像素比(dpr),1px边框问题,viewport自适应等问题. 目录 **关于 css像素px,物理像素(pt),设备像素比(dpr),1px边框问题,v ...

  3. java font 像素_字体的FontSize(pt)和像素(px)的关系

    上位机字体的通用设置是使用FontSize进行设置,可是实际上FontSize 为8 时,不是代表字体像素高度为8. 英文字体的bai1磅,相当于1/72 英寸,约等于1/2.8mm. 12PT的字打 ...

  4. 厘米与像素的单位转换

    1厘米等于多少个像素 客户要求地图显示的时候显示的是一比多少的地图,于是会设计到这个问题. 网上的答案都是这个问题是错误的之类的答案,或者就是1厘米等于25像素. 我知道这个问题和屏幕的尺寸有关,因为 ...

  5. 字体的大小(pt)和像素(px)如何转换?

    px:相对长度单位.像素(Pixel). pt:绝对长度单位.点(Point). 1in = 2.54cm = 25.4 mm = 72pt = 6pc具体换算是: Points Pixels Ems ...

  6. [转载] 厘米转像素

    厘米(CM): 分辨率(DPI): 像素(PX): 打印常识:A4纸张在显示器上应该要多少像素? A4纸的尺寸是210mm×297mm,也就是21.0cm×29.7cm,而1英寸=2.54cm,如果屏 ...

  7. java px pt转化_字体的大小(pt)和像素(px)如何转换?

    px:相对长度单位.像素(Pixel). pt:绝对长度单位.点(Point). 1in = 2.54cm = 25.4 mm = 72pt = 6pc 具体换算是: Points     Pixel ...

  8. 转换数字为像素px单位

    /*转换数字为像素px单位*/ private PX=size=>parseFloat(size.toString())+"px";

  9. html像素和em转换,初识HTML(5)+CSS(3)-2020升级版 - 像素px:相对单位em:就是本元素给定字...

    像素px:相对单位 em:就是本元素给定字体的 font-size 值,如果元素的 font-size 为 14px ,那么 1em = 14px 百分比:font-size的百分比 初识HTML(5 ...

最新文章

  1. nagios监控远程端口
  2. java new url 带密码_获取密码重置URL
  3. java反射机制原理详解_java反射机制的详细讲解
  4. RPA或成为日本大银行“瘦身”潮的催化剂
  5. matlab中云模型,云模型简介与个人理解matlab程序.doc
  6. 软件开发 thoughtworks 技术面_【软件开发】10月29日比赛详细预告
  7. python 当前目录_Python中的搜索路径顺序
  8. [Ext JS]5.8 Group Grid-分组网格
  9. Web 服务器错误代码
  10. Xlim函数--Matplotlib
  11. 【JanusGraph入门】(第一天,下载项目)
  12. AMOS分析技术:验证性因子分析介绍;信度与效度指标详解
  13. 163-H桥电机驱动电路
  14. python - 文件打包发布流程
  15. AutoSAR Layered Software Architecture 分层软件体系架构(初级)
  16. python 海龟 速度_Python中的高级turtle(海龟)作图
  17. 深度势能 deep potential
  18. LoraWAN源码学习(1)--通道选择详解
  19. 视频教程-Java工程师必学系列课程之4--《Java Swing》视频课程-Java
  20. 中国石油大学《计算机网络课程设计》第三次在线作业

热门文章

  1. elasticsearch常用命令
  2. Java概述、环境变量、注释、关键字、标识符、常量
  3. iOS------App之间传递数据的几种方式
  4. VC++的windows服务
  5. 02 List、Tuple、Dict、Set
  6. Mysql-my-innodb-heavy-4G.cnf配置文件注解
  7. 静态代理设计与动态代理设计
  8. C#获取刚插入的数据的id
  9. 哲学到编程:思想的实例化
  10. Yii2.0 ActiveForm Input Fields