unitChange.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // px <-> mm 单位互相转换
  2. /**
  3. * 获取DPI 每英寸像素点
  4. * @returns {Array}
  5. */
  6. let conversion_getDPI = function() {
  7. var DPI = 0;
  8. if (window.screen.deviceXDPI) {
  9. DPI = window.screen.deviceXDPI;
  10. } else {
  11. var tmpNode = document.createElement("DIV");
  12. tmpNode.style.cssText =
  13. "width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden";
  14. document.body.appendChild(tmpNode);
  15. DPI = parseInt(tmpNode.offsetWidth);
  16. tmpNode.parentNode.removeChild(tmpNode);
  17. }
  18. return DPI;
  19. };
  20. // 1 英寸=25.4 毫米
  21. /**
  22. * px转换为mm
  23. * @param value
  24. * @returns {number}
  25. */
  26. let px2mm = function(value) {
  27. var inch = value / conversion_getDPI();
  28. var c_value = inch * 25.4;
  29. // console.log(c_value);
  30. return c_value;
  31. };
  32. /**
  33. * mm转换为px
  34. * @param value
  35. * @returns {number}
  36. */
  37. let mm2px = function(value) {
  38. var inch = value / 25.4;
  39. var c_value = inch * conversion_getDPI();
  40. // console.log(c_value);
  41. return c_value;
  42. };
  43. // export default {
  44. // mm2px,
  45. // px2mm
  46. // };