index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Unit = void 0;
  4. let millimeterSize;
  5. const supportedUnits = {
  6. px(val) {
  7. return val;
  8. },
  9. mm(val) {
  10. return millimeterSize * val;
  11. },
  12. cm(val) {
  13. return millimeterSize * val * 10;
  14. },
  15. in(val) {
  16. return millimeterSize * val * 25.4;
  17. },
  18. pt(val) {
  19. return millimeterSize * ((25.4 * val) / 72);
  20. },
  21. pc(val) {
  22. return millimeterSize * ((25.4 * val) / 6);
  23. },
  24. };
  25. // eslint-disable-next-line
  26. var Unit;
  27. (function (Unit) {
  28. function measure(cssWidth, cssHeight, unit) {
  29. const div = document.createElement('div');
  30. const style = div.style;
  31. style.display = 'inline-block';
  32. style.position = 'absolute';
  33. style.left = '-15000px';
  34. style.top = '-15000px';
  35. style.width = cssWidth + (unit || 'px');
  36. style.height = cssHeight + (unit || 'px');
  37. document.body.appendChild(div);
  38. const rect = div.getBoundingClientRect();
  39. const size = {
  40. width: rect.width || 0,
  41. height: rect.height || 0,
  42. };
  43. document.body.removeChild(div);
  44. return size;
  45. }
  46. Unit.measure = measure;
  47. function toPx(val, unit) {
  48. if (millimeterSize == null) {
  49. millimeterSize = measure('1', '1', 'mm').width;
  50. }
  51. const convert = unit ? supportedUnits[unit] : null;
  52. if (convert) {
  53. return convert(val);
  54. }
  55. return val;
  56. }
  57. Unit.toPx = toPx;
  58. })(Unit = exports.Unit || (exports.Unit = {}));
  59. //# sourceMappingURL=index.js.map