ztxkutils 2.10.66-37 → 2.10.66-39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/print.js +30 -21
  2. package/package.json +1 -1
package/dist/print.js CHANGED
@@ -59,29 +59,36 @@ function printCurrentDom(_a) {
59
59
  window.print();
60
60
  }
61
61
  function getTextDimensions(text, fontSize) {
62
- // 创建一个临时元素
63
- var tempElement = document.createElement('div');
64
- // 设置元素内容为传入的文本
65
- tempElement.textContent = text;
66
- // 设置样式使其不可见且不影响页面布局
67
- tempElement.style.position = 'absolute';
68
- tempElement.style.visibility = 'hidden'; // 元素不可见但仍占据空间
69
- tempElement.style.opacity = '0'; // 完全透明
70
- tempElement.style.pointerEvents = 'none'; // 不响应鼠标事件
71
- tempElement.style.whiteSpace = 'nowrap'; // 防止文本换行影响宽度测量
72
- tempElement.style.fontSize = fontSize;
73
- tempElement.style.fontFamily = '微软雅黑';
74
- tempElement.style.padding = '40px 10px';
75
- // 插入到body中
76
- document.body.appendChild(tempElement);
77
- // 获取元素的宽高
78
- var width = tempElement.offsetWidth;
79
- var height = tempElement.offsetHeight;
80
- // 从DOM中移除元素
81
- document.body.removeChild(tempElement);
62
+ var width, height;
63
+ try {
64
+ // 创建一个临时元素
65
+ var tempElement = document.createElement('div');
66
+ // 设置元素内容为传入的文本
67
+ tempElement.textContent = text;
68
+ // 设置样式使其不可见且不影响页面布局
69
+ tempElement.style.position = 'absolute';
70
+ tempElement.style.visibility = 'hidden'; // 元素不可见但仍占据空间
71
+ tempElement.style.opacity = '0'; // 完全透明
72
+ tempElement.style.pointerEvents = 'none'; // 不响应鼠标事件
73
+ tempElement.style.whiteSpace = 'nowrap'; // 防止文本换行影响宽度测量
74
+ tempElement.style.fontSize = fontSize;
75
+ tempElement.style.fontFamily = '微软雅黑';
76
+ tempElement.style.padding = '50px 10px';
77
+ // 插入到body中
78
+ document.body.appendChild(tempElement);
79
+ // 获取元素的宽高
80
+ width = tempElement.offsetWidth;
81
+ height = tempElement.offsetHeight;
82
+ // 从DOM中移除元素
83
+ document.body.removeChild(tempElement);
84
+ }
85
+ catch (err) {
86
+ width = text.length * 22;
87
+ height = text.length * 9;
88
+ }
82
89
  // 返回测量结果
83
90
  return {
84
- width: width > 440 ? 440 : width < 240 ? 240 : width,
91
+ width: width > 520 ? 520 : width < 240 ? 240 : width,
85
92
  height: height > 200 ? 200 : height < 100 ? 100 : height,
86
93
  };
87
94
  }
@@ -107,11 +114,13 @@ function createWaterHandle(obj) {
107
114
  var canvas = document.createElement('canvas');
108
115
  // 如何计算水印的大小。创建一个元素插入到body中,获取到宽高后移除
109
116
  var _b = getTextDimensions(waterText, fontSize || '18px'), textWidth = _b.width, textHeight = _b.height;
117
+ // 之前的宽度是文本长度 * 22
110
118
  var defaultCanvasWidth = waterText
111
119
  ? waterText.length > 10
112
120
  ? textWidth
113
121
  : 240
114
122
  : 240;
123
+ // 之前的高度是文本长度 * 9
115
124
  var defaultCanvasHeight = waterText
116
125
  ? waterText.length > 10
117
126
  ? textHeight
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ztxkutils",
3
- "version": "2.10.66-37",
3
+ "version": "2.10.66-39",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",