index.js 1.5 KB

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