@ibiz-template/vue3-util 0.7.41-alpha.2 → 0.7.41-alpha.21
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.
- package/dist/index.min.css +1 -1
- package/dist/index.system.min.js +1 -1
- package/es/common/badge/badge.css +1 -1
- package/es/common/control-base/control-base.css +1 -1
- package/es/common/control-base/control-base.d.ts +1 -0
- package/es/common/control-base/control-base.d.ts.map +1 -1
- package/es/common/control-base/control-base.mjs +38 -4
- package/es/common/custom-render/custom-render.d.ts.map +1 -1
- package/es/common/custom-render/custom-render.mjs +2 -2
- package/es/common/index.d.ts +1 -0
- package/es/common/index.d.ts.map +1 -1
- package/es/common/index.mjs +1 -0
- package/es/common/signature-pad/signature-pad.css +1 -0
- package/es/common/signature-pad/signature-pad.d.ts +19 -0
- package/es/common/signature-pad/signature-pad.d.ts.map +1 -0
- package/es/common/signature-pad/signature-pad.mjs +165 -0
- package/es/common/signature-pad/util/bezier.d.ts +58 -0
- package/es/common/signature-pad/util/bezier.d.ts.map +1 -0
- package/es/common/signature-pad/util/bezier.mjs +109 -0
- package/es/common/signature-pad/util/point.d.ts +55 -0
- package/es/common/signature-pad/util/point.d.ts.map +1 -0
- package/es/common/signature-pad/util/point.mjs +51 -0
- package/es/common/signature-pad/util/signature_pad.d.ts +593 -0
- package/es/common/signature-pad/util/signature_pad.d.ts.map +1 -0
- package/es/common/signature-pad/util/signature_pad.mjs +1018 -0
- package/es/common/view-shell/view-shell.d.ts +8 -1
- package/es/common/view-shell/view-shell.d.ts.map +1 -1
- package/es/common/view-shell/view-shell.mjs +13 -2
- package/es/control/panel/panel/panel.d.ts.map +1 -1
- package/es/control/panel/panel/panel.mjs +12 -1
- package/es/control/panel/view-layout-panel/view-layout-panel.d.ts +1 -1
- package/es/control/panel/view-layout-panel/view-layout-panel.d.ts.map +1 -1
- package/es/control/panel/view-layout-panel/view-layout-panel.mjs +12 -1
- package/es/index.mjs +3 -1
- package/es/locale/en/index.d.ts +1 -0
- package/es/locale/en/index.d.ts.map +1 -1
- package/es/locale/en/index.mjs +2 -1
- package/es/locale/zh-CN/index.d.ts +1 -0
- package/es/locale/zh-CN/index.d.ts.map +1 -1
- package/es/locale/zh-CN/index.mjs +2 -1
- package/es/panel-component/multi-data-container/multi-data-container.controller.d.ts.map +1 -1
- package/es/panel-component/multi-data-container/multi-data-container.controller.mjs +10 -3
- package/es/panel-component/multi-data-container/multi-data-container.d.ts +1 -1
- package/es/panel-component/multi-data-container/multi-data-container.d.ts.map +1 -1
- package/es/panel-component/multi-data-container-raw/multi-data-container-raw.d.ts +1 -1
- package/es/panel-component/multi-data-container-raw/multi-data-container-raw.d.ts.map +1 -1
- package/es/panel-component/nav-pos/nav-pos.controller.d.ts +1 -1
- package/es/panel-component/nav-pos/nav-pos.d.ts +1 -1
- package/es/panel-component/panel-container/panel-container.d.ts.map +1 -1
- package/es/panel-component/panel-container/panel-container.mjs +30 -2
- package/es/panel-component/panel-field/panel-field.controller.mjs +1 -1
- package/es/panel-component/panel-tab-page/panel-tab-page.d.ts +1 -1
- package/es/panel-component/single-data-container/single-data-container.d.ts +1 -1
- package/es/panel-component/single-data-container/single-data-container.d.ts.map +1 -1
- package/es/panel-component/teleport-placeholder/teleport-placeholder.d.ts +1 -0
- package/es/panel-component/teleport-placeholder/teleport-placeholder.d.ts.map +1 -1
- package/es/panel-component/teleport-placeholder/teleport-placeholder.state.d.ts +1 -1
- package/es/panel-component/teleport-placeholder/teleport-placeholder.state.mjs +1 -1
- package/es/plugin/plugin-factory/plugin-factory.d.ts +13 -12
- package/es/plugin/plugin-factory/plugin-factory.d.ts.map +1 -1
- package/es/plugin/plugin-factory/plugin-factory.mjs +24 -18
- package/es/use/index.d.ts +1 -0
- package/es/use/index.d.ts.map +1 -1
- package/es/use/index.mjs +3 -1
- package/es/use/popover/popover.d.ts +7 -0
- package/es/use/popover/popover.d.ts.map +1 -1
- package/es/use/popover/popover.mjs +9 -2
- package/es/use/storage/index.d.ts +4 -6
- package/es/use/storage/index.d.ts.map +1 -1
- package/es/use/storage/index.mjs +16 -11
- package/es/use/vue-use/computed-async.d.ts +51 -0
- package/es/use/vue-use/computed-async.d.ts.map +1 -0
- package/es/use/vue-use/computed-async.mjs +66 -0
- package/es/use/vue-use/index.d.ts +2 -0
- package/es/use/vue-use/index.d.ts.map +1 -0
- package/es/use/vue-use/index.mjs +3 -0
- package/es/util/install.d.ts.map +1 -1
- package/es/util/overlay-container/overlay-container.d.ts +1 -1
- package/es/util/overlay-container/overlay-container.d.ts.map +1 -1
- package/es/util/overlay-container/overlay-container.mjs +7 -2
- package/es/util/overlay-view-util/overlay-view-util.d.ts +2 -2
- package/es/util/overlay-view-util/overlay-view-util.d.ts.map +1 -1
- package/es/util/overlay-view-util/overlay-view-util.mjs +2 -1
- package/es/util/render/render.d.ts.map +1 -1
- package/es/util/render/render.mjs +4 -1
- package/es/util/route/route.d.ts.map +1 -1
- package/es/util/route/route.mjs +18 -3
- package/es/view/common/index.d.ts.map +1 -1
- package/es/view/common/index.mjs +2 -0
- package/es/view/common/view.d.ts.map +1 -1
- package/es/view/common/view.mjs +14 -1
- package/es/view/md-custom-view/md-custom-view.provider.d.ts +13 -0
- package/es/view/md-custom-view/md-custom-view.provider.d.ts.map +1 -0
- package/es/view/md-custom-view/md-custom-view.provider.mjs +13 -0
- package/lib/common/badge/badge.css +1 -1
- package/lib/common/control-base/control-base.cjs +37 -3
- package/lib/common/control-base/control-base.css +1 -1
- package/lib/common/custom-render/custom-render.cjs +1 -1
- package/lib/common/index.cjs +2 -0
- package/lib/common/signature-pad/signature-pad.cjs +167 -0
- package/lib/common/signature-pad/signature-pad.css +1 -0
- package/lib/common/signature-pad/util/bezier.cjs +111 -0
- package/lib/common/signature-pad/util/point.cjs +53 -0
- package/lib/common/signature-pad/util/signature_pad.cjs +1022 -0
- package/lib/common/view-shell/view-shell.cjs +13 -2
- package/lib/control/panel/panel/panel.cjs +12 -1
- package/lib/control/panel/view-layout-panel/view-layout-panel.cjs +12 -1
- package/lib/index.cjs +5 -0
- package/lib/locale/en/index.cjs +2 -1
- package/lib/locale/zh-CN/index.cjs +2 -1
- package/lib/panel-component/multi-data-container/multi-data-container.controller.cjs +10 -3
- package/lib/panel-component/panel-container/panel-container.cjs +29 -1
- package/lib/panel-component/panel-field/panel-field.controller.cjs +1 -1
- package/lib/panel-component/teleport-placeholder/teleport-placeholder.state.cjs +1 -1
- package/lib/plugin/plugin-factory/plugin-factory.cjs +24 -18
- package/lib/use/index.cjs +4 -0
- package/lib/use/popover/popover.cjs +8 -0
- package/lib/use/storage/index.cjs +16 -11
- package/lib/use/vue-use/computed-async.cjs +68 -0
- package/lib/use/vue-use/index.cjs +7 -0
- package/lib/util/overlay-container/overlay-container.cjs +7 -2
- package/lib/util/overlay-view-util/overlay-view-util.cjs +2 -1
- package/lib/util/render/render.cjs +4 -1
- package/lib/util/route/route.cjs +18 -3
- package/lib/view/common/index.cjs +2 -0
- package/lib/view/common/view.cjs +13 -0
- package/lib/view/md-custom-view/md-custom-view.provider.cjs +15 -0
- package/package.json +7 -7
|
@@ -0,0 +1,1022 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var lodashEs = require('lodash-es');
|
|
6
|
+
var bezier = require('./bezier.cjs');
|
|
7
|
+
var point = require('./point.cjs');
|
|
8
|
+
|
|
9
|
+
"use strict";
|
|
10
|
+
class SignaturePad {
|
|
11
|
+
constructor(canvas, options = {}) {
|
|
12
|
+
this.canvas = canvas;
|
|
13
|
+
/**
|
|
14
|
+
* @description 是否正在绘制中(笔触未抬起)
|
|
15
|
+
* @private
|
|
16
|
+
* @memberof SignaturePad
|
|
17
|
+
*/
|
|
18
|
+
this._drawingStroke = false;
|
|
19
|
+
/**
|
|
20
|
+
* @description 签名是否为空(未绘制任何内容)
|
|
21
|
+
* @private
|
|
22
|
+
* @memberof SignaturePad
|
|
23
|
+
*/
|
|
24
|
+
this._isEmpty = true;
|
|
25
|
+
/**
|
|
26
|
+
* @description 是否重新绘制过签名(用于判断是否从数据恢复过签名)
|
|
27
|
+
* @private
|
|
28
|
+
* @memberof SignaturePad
|
|
29
|
+
*/
|
|
30
|
+
this._isRedrawn = false;
|
|
31
|
+
/**
|
|
32
|
+
* @description 上一次从DataURL加载的签名图片地址,用于撤销操作时恢复
|
|
33
|
+
* @private
|
|
34
|
+
* @memberof SignaturePad
|
|
35
|
+
*/
|
|
36
|
+
this._oldFromDataURL = "";
|
|
37
|
+
/**
|
|
38
|
+
* @description 存储最近的点(最多4个),用于生成新的贝塞尔曲线
|
|
39
|
+
* @private
|
|
40
|
+
* @type {Point[]}
|
|
41
|
+
* @memberof SignaturePad
|
|
42
|
+
*/
|
|
43
|
+
this._lastPoints = [];
|
|
44
|
+
/**
|
|
45
|
+
* @description 存储所有签名数据(按轨迹分组,每组包含一段连续绘制的点及样式配置)
|
|
46
|
+
* @private
|
|
47
|
+
* @type {IPointGroup[]}
|
|
48
|
+
* @memberof SignaturePad
|
|
49
|
+
*/
|
|
50
|
+
this._data = [];
|
|
51
|
+
/**
|
|
52
|
+
* @description 上一次计算的绘制速度(用于平滑线条宽度变化)
|
|
53
|
+
* @private
|
|
54
|
+
* @memberof SignaturePad
|
|
55
|
+
*/
|
|
56
|
+
this._lastVelocity = 0;
|
|
57
|
+
/**
|
|
58
|
+
* @description 上一次绘制的线条宽度(用于平滑过渡到当前宽度)
|
|
59
|
+
* @private
|
|
60
|
+
* @memberof SignaturePad
|
|
61
|
+
*/
|
|
62
|
+
this._lastWidth = 0;
|
|
63
|
+
var _a, _b, _c;
|
|
64
|
+
this.velocityFilterWeight = options.velocityFilterWeight || 0.7;
|
|
65
|
+
this.minWidth = options.minWidth || 2;
|
|
66
|
+
this.maxWidth = options.maxWidth || 2;
|
|
67
|
+
this.throttle = (_a = options.throttle) != null ? _a : 16;
|
|
68
|
+
this.minDistance = (_b = options.minDistance) != null ? _b : 5;
|
|
69
|
+
this.dotSize = options.dotSize || 0;
|
|
70
|
+
this.penColor = options.penColor || "black";
|
|
71
|
+
this.backgroundColor = options.backgroundColor || "rgba(0,0,0,0)";
|
|
72
|
+
this.compositeOperation = options.compositeOperation || "source-over";
|
|
73
|
+
this.canvasContextOptions = (_c = options.canvasContextOptions) != null ? _c : {};
|
|
74
|
+
this._strokeMoveUpdate = this.throttle ? lodashEs.throttle(SignaturePad.prototype._strokeUpdate, this.throttle) : SignaturePad.prototype._strokeUpdate;
|
|
75
|
+
this._handleMouseDown = this._handleMouseDown.bind(this);
|
|
76
|
+
this._handleMouseMove = this._handleMouseMove.bind(this);
|
|
77
|
+
this._handleMouseUp = this._handleMouseUp.bind(this);
|
|
78
|
+
this._handleTouchStart = this._handleTouchStart.bind(this);
|
|
79
|
+
this._handleTouchMove = this._handleTouchMove.bind(this);
|
|
80
|
+
this._handleTouchEnd = this._handleTouchEnd.bind(this);
|
|
81
|
+
this._handlePointerDown = this._handlePointerDown.bind(this);
|
|
82
|
+
this._handlePointerMove = this._handlePointerMove.bind(this);
|
|
83
|
+
this._handlePointerUp = this._handlePointerUp.bind(this);
|
|
84
|
+
this._ctx = canvas.getContext(
|
|
85
|
+
"2d",
|
|
86
|
+
this.canvasContextOptions
|
|
87
|
+
);
|
|
88
|
+
this.clear();
|
|
89
|
+
this.on();
|
|
90
|
+
}
|
|
91
|
+
clear() {
|
|
92
|
+
const { _ctx: ctx, canvas } = this;
|
|
93
|
+
ctx.fillStyle = this.backgroundColor;
|
|
94
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
95
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
96
|
+
this._data = [];
|
|
97
|
+
this._reset(this._getPointGroupOptions());
|
|
98
|
+
this._isEmpty = true;
|
|
99
|
+
this._strokePointerId = void 0;
|
|
100
|
+
this._oldFromDataURL = "";
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 从数据URL加载图像并绘制到签名画布上
|
|
104
|
+
* 支持对图像进行旋转、缩放和偏移等处理,适用于恢复保存的签名图像
|
|
105
|
+
* @param {string} dataUrl - 图像的DataURL(如base64编码的图片数据)
|
|
106
|
+
* @param {Object} [options={}] - 加载配置选项
|
|
107
|
+
* @returns {Promise<void>} - 加载完成的Promise(成功时resolve,失败时reject)
|
|
108
|
+
*/
|
|
109
|
+
async fromDataURL(dataUrl, options = {}) {
|
|
110
|
+
let _tempDataUrl = dataUrl;
|
|
111
|
+
if (lodashEs.isNumber(options.rotation) && options.rotation !== 0) {
|
|
112
|
+
_tempDataUrl = await this.rotateDataURL(
|
|
113
|
+
_tempDataUrl,
|
|
114
|
+
options.rotation,
|
|
115
|
+
// 顺时针旋转指定角度
|
|
116
|
+
"image/png"
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
return new Promise((resolve, reject) => {
|
|
120
|
+
const image = new Image();
|
|
121
|
+
const ratio = options.ratio || window.devicePixelRatio || 1;
|
|
122
|
+
const width = options.width || this.canvas.width / ratio;
|
|
123
|
+
const height = options.height || this.canvas.height / ratio;
|
|
124
|
+
const xOffset = options.xOffset || 0;
|
|
125
|
+
const yOffset = options.yOffset || 0;
|
|
126
|
+
this._reset(this._getPointGroupOptions());
|
|
127
|
+
image.onload = () => {
|
|
128
|
+
this._ctx.drawImage(image, xOffset, yOffset, width, height);
|
|
129
|
+
resolve();
|
|
130
|
+
};
|
|
131
|
+
image.onerror = (error) => {
|
|
132
|
+
reject(error);
|
|
133
|
+
};
|
|
134
|
+
image.crossOrigin = "anonymous";
|
|
135
|
+
image.src = _tempDataUrl;
|
|
136
|
+
this._oldFromDataURL = _tempDataUrl;
|
|
137
|
+
this._isEmpty = false;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* @description 将已有的 DataURL 旋转指定角度后,返回新的 DataURL
|
|
142
|
+
* @private
|
|
143
|
+
* @param {string} dataUrl - 原始图片的DataURL
|
|
144
|
+
* @param {number} rotation - 旋转角度(度数,顺时针),默认0
|
|
145
|
+
* @param {string} type - 图片格式,默认'image/png'
|
|
146
|
+
* @param {number} [encoderOptions] - 图片质量(0-1),仅适用于jpeg等格式
|
|
147
|
+
* @returns {*} {Promise<string>}
|
|
148
|
+
* @memberof SignaturePad
|
|
149
|
+
*/
|
|
150
|
+
rotateDataURL(dataUrl, rotation = 0, type = "image/png", encoderOptions) {
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
const image = new Image();
|
|
153
|
+
image.onload = () => {
|
|
154
|
+
const radians = rotation * Math.PI / 180;
|
|
155
|
+
const width = image.width;
|
|
156
|
+
const height = image.height;
|
|
157
|
+
const tmpCanvas = document.createElement("canvas");
|
|
158
|
+
const ctx = tmpCanvas.getContext("2d");
|
|
159
|
+
if (rotation % 180 === 0) {
|
|
160
|
+
tmpCanvas.width = width;
|
|
161
|
+
tmpCanvas.height = height;
|
|
162
|
+
} else {
|
|
163
|
+
tmpCanvas.width = height;
|
|
164
|
+
tmpCanvas.height = width;
|
|
165
|
+
}
|
|
166
|
+
ctx.translate(tmpCanvas.width / 2, tmpCanvas.height / 2);
|
|
167
|
+
ctx.rotate(radians);
|
|
168
|
+
ctx.drawImage(image, -width / 2, -height / 2);
|
|
169
|
+
resolve(tmpCanvas.toDataURL(type, encoderOptions));
|
|
170
|
+
};
|
|
171
|
+
image.onerror = reject;
|
|
172
|
+
image.crossOrigin = "anonymous";
|
|
173
|
+
image.src = dataUrl;
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* @description 撤销上一步
|
|
178
|
+
* @returns {*} {void}
|
|
179
|
+
* @memberof SignaturePad
|
|
180
|
+
*/
|
|
181
|
+
undoLastStep() {
|
|
182
|
+
const tempData = [...this._data];
|
|
183
|
+
const tempFromDataURL = this._oldFromDataURL;
|
|
184
|
+
this.clear();
|
|
185
|
+
this._isRedrawn = true;
|
|
186
|
+
if (tempData.length === 0) {
|
|
187
|
+
this._oldFromDataURL = "";
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
tempData.pop();
|
|
191
|
+
this.fromDataURL(tempFromDataURL);
|
|
192
|
+
this.fromData(tempData, { clear: false });
|
|
193
|
+
this._isEmpty = tempData.length <= 0;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* @description 封装获取当前旋转方位的方法
|
|
197
|
+
* @returns {*} {IData} 包含屏幕状态、旋转角度和方向的对象
|
|
198
|
+
* @memberof SignaturePad
|
|
199
|
+
*/
|
|
200
|
+
getCurrentOrientation() {
|
|
201
|
+
const { screen } = window;
|
|
202
|
+
const screenOrientation = screen.orientation || screen.mozOrientation || screen.msOrientation;
|
|
203
|
+
let angle = 0;
|
|
204
|
+
if (screenOrientation) {
|
|
205
|
+
angle = screenOrientation.angle;
|
|
206
|
+
}
|
|
207
|
+
const orientationText = {
|
|
208
|
+
"0": "DEFAULT",
|
|
209
|
+
"90": "Rotated right",
|
|
210
|
+
"-90": "Rotated left",
|
|
211
|
+
"180": "Upside down"
|
|
212
|
+
};
|
|
213
|
+
const isLandscape = window.innerWidth > window.innerHeight;
|
|
214
|
+
const status = isLandscape ? "\u6A2A\u5C4F" : "\u7AD6\u5C4F";
|
|
215
|
+
const orientation = orientationText["".concat(angle)] || "\u672A\u77E5 (".concat(angle, "\xB0)");
|
|
216
|
+
return {
|
|
217
|
+
isLandscape,
|
|
218
|
+
status,
|
|
219
|
+
angle,
|
|
220
|
+
orientation
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* @description 将签名图像作为数据 URL 返回
|
|
225
|
+
* @param {string} [type='image/png']
|
|
226
|
+
* @param {{
|
|
227
|
+
* rotation?: number;
|
|
228
|
+
* quality?: number;
|
|
229
|
+
* encoderOptions?: IToSVGOptions;
|
|
230
|
+
* }} [options={}]
|
|
231
|
+
* @returns {*} {string}
|
|
232
|
+
* @memberof SignaturePad
|
|
233
|
+
*/
|
|
234
|
+
toDataURL(type = "image/png", options = {}) {
|
|
235
|
+
switch (type) {
|
|
236
|
+
case "image/svg+xml":
|
|
237
|
+
if (lodashEs.isObject(options == null ? void 0 : options.encoderOptions)) {
|
|
238
|
+
options.encoderOptions = void 0;
|
|
239
|
+
}
|
|
240
|
+
return "data:image/svg+xml;base64,".concat(btoa(
|
|
241
|
+
this.toSVG(options == null ? void 0 : options.encoderOptions)
|
|
242
|
+
));
|
|
243
|
+
default:
|
|
244
|
+
return this.toCanvasDataURL(
|
|
245
|
+
type,
|
|
246
|
+
options.encoderOptions,
|
|
247
|
+
options.rotation
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* @description 导出为 Canvas DataURL(支持旋转)
|
|
253
|
+
* @private
|
|
254
|
+
* @param {string} [type='image/png'] 图片格式
|
|
255
|
+
* @param {number} [encoderOptions] 图片质量(0-1)
|
|
256
|
+
* @param {number} [rotation] 旋转角度(度数)
|
|
257
|
+
* @returns {*} {string}
|
|
258
|
+
* @memberof SignaturePad
|
|
259
|
+
*/
|
|
260
|
+
toCanvasDataURL(type = "image/png", encoderOptions, rotation) {
|
|
261
|
+
if (typeof encoderOptions !== "number") {
|
|
262
|
+
encoderOptions = void 0;
|
|
263
|
+
}
|
|
264
|
+
if (lodashEs.isNumber(rotation) && rotation !== 0) {
|
|
265
|
+
const radians = rotation * Math.PI / 180;
|
|
266
|
+
const tmpCanvas = document.createElement("canvas");
|
|
267
|
+
const ctx = tmpCanvas.getContext("2d");
|
|
268
|
+
const width = this.canvas.width;
|
|
269
|
+
const height = this.canvas.height;
|
|
270
|
+
if (rotation % 180 === 0) {
|
|
271
|
+
tmpCanvas.width = width;
|
|
272
|
+
tmpCanvas.height = height;
|
|
273
|
+
} else {
|
|
274
|
+
tmpCanvas.width = height;
|
|
275
|
+
tmpCanvas.height = width;
|
|
276
|
+
}
|
|
277
|
+
ctx.translate(tmpCanvas.width / 2, tmpCanvas.height / 2);
|
|
278
|
+
ctx.rotate(radians);
|
|
279
|
+
ctx.drawImage(this.canvas, -width / 2, -height / 2);
|
|
280
|
+
return tmpCanvas.toDataURL(type, encoderOptions);
|
|
281
|
+
}
|
|
282
|
+
return this.canvas.toDataURL(type, encoderOptions);
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* @description 启用签名功能的事件监听,配置画布样式以禁用默认的触摸行为(如平移、缩放),并根据设备类型绑定相应的事件处理器
|
|
286
|
+
* @memberof SignaturePad
|
|
287
|
+
*/
|
|
288
|
+
on() {
|
|
289
|
+
this.canvas.style.touchAction = "none";
|
|
290
|
+
this.canvas.style.msTouchAction = "none";
|
|
291
|
+
this.canvas.style.userSelect = "none";
|
|
292
|
+
const isIOS = /Macintosh/.test(navigator.userAgent) && "ontouchstart" in document;
|
|
293
|
+
if (window.PointerEvent && !isIOS) {
|
|
294
|
+
this._handlePointerEvents();
|
|
295
|
+
} else {
|
|
296
|
+
this._handleMouseEvents();
|
|
297
|
+
if ("ontouchstart" in window) {
|
|
298
|
+
this._handleTouchEvents();
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* @description 禁用签名功能的事件监听,恢复画布默认样式(允许平移、缩放等),并移除所有已绑定的事件处理器
|
|
304
|
+
* @memberof SignaturePad
|
|
305
|
+
*/
|
|
306
|
+
off() {
|
|
307
|
+
this.canvas.style.touchAction = "auto";
|
|
308
|
+
this.canvas.style.msTouchAction = "auto";
|
|
309
|
+
this.canvas.style.userSelect = "auto";
|
|
310
|
+
this.canvas.removeEventListener("pointerdown", this._handlePointerDown);
|
|
311
|
+
this.canvas.removeEventListener("mousedown", this._handleMouseDown);
|
|
312
|
+
this.canvas.removeEventListener("touchstart", this._handleTouchStart);
|
|
313
|
+
this._removeMoveUpEventListeners();
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* @description 获取事件监听器的工具函数(适配不同文档上下文的窗口)
|
|
317
|
+
* @private
|
|
318
|
+
* @returns {*} 包含addEventListener和removeEventListener的对象
|
|
319
|
+
* @memberof SignaturePad
|
|
320
|
+
*/
|
|
321
|
+
_getListenerFunctions() {
|
|
322
|
+
var _a;
|
|
323
|
+
const canvasWindow = window.document === this.canvas.ownerDocument ? window : (_a = this.canvas.ownerDocument.defaultView) != null ? _a : this.canvas.ownerDocument;
|
|
324
|
+
return {
|
|
325
|
+
addEventListener: canvasWindow.addEventListener.bind(
|
|
326
|
+
canvasWindow
|
|
327
|
+
),
|
|
328
|
+
removeEventListener: canvasWindow.removeEventListener.bind(
|
|
329
|
+
canvasWindow
|
|
330
|
+
)
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* @description 移除所有移动和抬起事件的监听器(清理事件绑定)
|
|
335
|
+
* @private
|
|
336
|
+
* @memberof SignaturePad
|
|
337
|
+
*/
|
|
338
|
+
_removeMoveUpEventListeners() {
|
|
339
|
+
const { removeEventListener } = this._getListenerFunctions();
|
|
340
|
+
removeEventListener("pointermove", this._handlePointerMove);
|
|
341
|
+
removeEventListener("pointerup", this._handlePointerUp);
|
|
342
|
+
removeEventListener("mousemove", this._handleMouseMove);
|
|
343
|
+
removeEventListener("mouseup", this._handleMouseUp);
|
|
344
|
+
removeEventListener("touchmove", this._handleTouchMove);
|
|
345
|
+
removeEventListener("touchend", this._handleTouchEnd);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* @description 判断签名画布是否为空(未绘制任何内容)
|
|
349
|
+
* @returns {*} {boolean}
|
|
350
|
+
* @memberof SignaturePad
|
|
351
|
+
*/
|
|
352
|
+
isEmpty() {
|
|
353
|
+
return this._isEmpty;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* @description 判断签名是否经过重新绘制(如从数据恢复签名、执行撤销后重新渲染等场景)
|
|
357
|
+
* @returns {*} {boolean}
|
|
358
|
+
* @memberof SignaturePad
|
|
359
|
+
*/
|
|
360
|
+
isRedrawn() {
|
|
361
|
+
return this._isRedrawn;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* @description 从点组数据加载并渲染签名,可根据配置决定是否先清空现有签名,再基于传入的点组数据重新绘制曲线和点
|
|
365
|
+
* @param {IPointGroup[]} pointGroups - 签名点组数据数组,每个点组包含一段签名的点集合及对应的样式配置
|
|
366
|
+
* @param {IFromDataOptions} [options={ clear: true }] - 加载配置项,默认清空现有签名
|
|
367
|
+
* @memberof SignaturePad
|
|
368
|
+
*/
|
|
369
|
+
fromData(pointGroups, { clear = true } = {}) {
|
|
370
|
+
if (clear) {
|
|
371
|
+
this.clear();
|
|
372
|
+
}
|
|
373
|
+
this._fromData(
|
|
374
|
+
pointGroups,
|
|
375
|
+
this._drawCurve.bind(this),
|
|
376
|
+
// 绑定当前实例上下文的曲线绘制方法
|
|
377
|
+
this._drawDot.bind(this)
|
|
378
|
+
// 绑定当前实例上下文的点绘制方法
|
|
379
|
+
);
|
|
380
|
+
this._data = this._data.concat(pointGroups);
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* @description 导出当前签名的点组数据,用于保存签名原始数据,后续可通过fromData方法恢复签名
|
|
384
|
+
* @returns {*} {IPointGroup[]}
|
|
385
|
+
* @memberof SignaturePad
|
|
386
|
+
*/
|
|
387
|
+
toData() {
|
|
388
|
+
return this._data;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* @description 判断鼠标左键是否按下(支持判断是否仅左键按下)
|
|
392
|
+
* @private
|
|
393
|
+
* @param {MouseEvent} event - 鼠标事件对象
|
|
394
|
+
* @param {boolean} [only] - 是否要求仅左键按下
|
|
395
|
+
* @returns {*} {boolean}
|
|
396
|
+
* @memberof SignaturePad
|
|
397
|
+
*/
|
|
398
|
+
_isLeftButtonPressed(event, only) {
|
|
399
|
+
if (only) {
|
|
400
|
+
return event.buttons === 1;
|
|
401
|
+
}
|
|
402
|
+
return (event.buttons & 1) === 1;
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* @description 将鼠标/指针事件转换为签名事件对象
|
|
406
|
+
* @private
|
|
407
|
+
* @param {(MouseEvent | PointerEvent)} event - 原始事件对象
|
|
408
|
+
* @returns {*} {ISignatureEvent}
|
|
409
|
+
* @memberof SignaturePad
|
|
410
|
+
*/
|
|
411
|
+
_pointerEventToSignatureEvent(event) {
|
|
412
|
+
return {
|
|
413
|
+
event,
|
|
414
|
+
type: event.type,
|
|
415
|
+
x: event.clientX,
|
|
416
|
+
y: event.clientY,
|
|
417
|
+
pressure: "pressure" in event ? event.pressure : 0
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* @description 将触摸事件转换为签名事件对象
|
|
422
|
+
* @private
|
|
423
|
+
* @param {TouchEvent} event - 原始触摸事件
|
|
424
|
+
* @returns {*} {ISignatureEvent}
|
|
425
|
+
* @memberof SignaturePad
|
|
426
|
+
*/
|
|
427
|
+
_touchEventToSignatureEvent(event) {
|
|
428
|
+
const touch = event.changedTouches[0];
|
|
429
|
+
return {
|
|
430
|
+
event,
|
|
431
|
+
type: event.type,
|
|
432
|
+
x: touch.clientX,
|
|
433
|
+
y: touch.clientY,
|
|
434
|
+
pressure: touch.force
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* @description 处理鼠标按下事件,当左键按下且未正在绘制时,开始新的笔触
|
|
439
|
+
* @private
|
|
440
|
+
* @param {MouseEvent} event
|
|
441
|
+
* @returns {*} {void}
|
|
442
|
+
* @memberof SignaturePad
|
|
443
|
+
*/
|
|
444
|
+
_handleMouseDown(event) {
|
|
445
|
+
if (!this._isLeftButtonPressed(event, true) || this._drawingStroke) {
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
this._strokeBegin(this._pointerEventToSignatureEvent(event));
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* @description 处理鼠标移动事件,当左键持续按下且正在绘制时,更新笔触;否则结束笔触
|
|
452
|
+
* @private
|
|
453
|
+
* @param {MouseEvent} event
|
|
454
|
+
* @returns {*} {void}
|
|
455
|
+
* @memberof SignaturePad
|
|
456
|
+
*/
|
|
457
|
+
_handleMouseMove(event) {
|
|
458
|
+
if (!this._isLeftButtonPressed(event, true) || !this._drawingStroke) {
|
|
459
|
+
this._strokeEnd(this._pointerEventToSignatureEvent(event), false);
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
this._strokeMoveUpdate(this._pointerEventToSignatureEvent(event));
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* @description 处理鼠标抬起事件,当左键抬起时,结束当前笔触
|
|
466
|
+
* @private
|
|
467
|
+
* @param {MouseEvent} event
|
|
468
|
+
* @returns {*} {void}
|
|
469
|
+
* @memberof SignaturePad
|
|
470
|
+
*/
|
|
471
|
+
_handleMouseUp(event) {
|
|
472
|
+
if (this._isLeftButtonPressed(event)) {
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
this._strokeEnd(this._pointerEventToSignatureEvent(event));
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* @description 处理触摸开始事件,当单点触摸且不在绘制状态时,开始新的笔触,并阻止页面滚动
|
|
479
|
+
* @private
|
|
480
|
+
* @param {TouchEvent} event
|
|
481
|
+
* @returns {*} {void}
|
|
482
|
+
* @memberof SignaturePad
|
|
483
|
+
*/
|
|
484
|
+
_handleTouchStart(event) {
|
|
485
|
+
if (event.targetTouches.length !== 1 || this._drawingStroke) {
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
if (event.cancelable) {
|
|
489
|
+
event.preventDefault();
|
|
490
|
+
}
|
|
491
|
+
this._strokeBegin(this._touchEventToSignatureEvent(event));
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* @description 处理触摸移动事件,当单点触摸且正在绘制时,更新笔触;否则结束笔触,并阻止页面滚动
|
|
495
|
+
* @private
|
|
496
|
+
* @param {TouchEvent} event
|
|
497
|
+
* @returns {*} {void}
|
|
498
|
+
* @memberof SignaturePad
|
|
499
|
+
*/
|
|
500
|
+
_handleTouchMove(event) {
|
|
501
|
+
if (event.targetTouches.length !== 1) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
if (event.cancelable) {
|
|
505
|
+
event.preventDefault();
|
|
506
|
+
}
|
|
507
|
+
if (!this._drawingStroke) {
|
|
508
|
+
this._strokeEnd(this._touchEventToSignatureEvent(event), false);
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
this._strokeMoveUpdate(this._touchEventToSignatureEvent(event));
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* @description 处理触摸结束事件,当所有触摸点离开时,结束当前笔触,并阻止默认行为
|
|
515
|
+
* @private
|
|
516
|
+
* @param {TouchEvent} event
|
|
517
|
+
* @returns {*} {void}
|
|
518
|
+
* @memberof SignaturePad
|
|
519
|
+
*/
|
|
520
|
+
_handleTouchEnd(event) {
|
|
521
|
+
if (event.targetTouches.length !== 0) {
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
if (event.cancelable) {
|
|
525
|
+
event.preventDefault();
|
|
526
|
+
}
|
|
527
|
+
this._strokeEnd(this._touchEventToSignatureEvent(event));
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* @description 获取指针事件的唯一ID(优先使用persistentDeviceId,兼容旧设备用pointerId)
|
|
531
|
+
* @private
|
|
532
|
+
* @param {PointerEvent} event
|
|
533
|
+
* @returns {*} {number}
|
|
534
|
+
* @memberof SignaturePad
|
|
535
|
+
*/
|
|
536
|
+
_getPointerId(event) {
|
|
537
|
+
return (event == null ? void 0 : event.persistentDeviceId) || event.pointerId;
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* @description 判断当前指针事件的ID是否为当前活跃的笔触ID
|
|
541
|
+
* @private
|
|
542
|
+
* @param {PointerEvent} event - 指针事件对象
|
|
543
|
+
* @param {boolean} [allowUndefined] - 是否允许当前无活跃ID(初始状态)
|
|
544
|
+
* @returns {*} {boolean}
|
|
545
|
+
* @memberof SignaturePad
|
|
546
|
+
*/
|
|
547
|
+
_allowPointerId(event, allowUndefined = false) {
|
|
548
|
+
if (typeof this._strokePointerId === "undefined") {
|
|
549
|
+
return allowUndefined;
|
|
550
|
+
}
|
|
551
|
+
return this._getPointerId(event) === this._strokePointerId;
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* @description 处理指针按下事件(兼容鼠标、触摸等多种输入设备)
|
|
555
|
+
* @private
|
|
556
|
+
* @param {PointerEvent} event
|
|
557
|
+
* @returns {*} {void}
|
|
558
|
+
* @memberof SignaturePad
|
|
559
|
+
*/
|
|
560
|
+
_handlePointerDown(event) {
|
|
561
|
+
if (this._drawingStroke || !this._isLeftButtonPressed(event) || !this._allowPointerId(event, true)) {
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
this._strokePointerId = this._getPointerId(event);
|
|
565
|
+
event.preventDefault();
|
|
566
|
+
this._strokeBegin(this._pointerEventToSignatureEvent(event));
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* @description 处理指针移动事件,当指针ID有效、左键持续按下且正在绘制时,更新笔触;否则结束笔触
|
|
570
|
+
* @private
|
|
571
|
+
* @param {PointerEvent} event
|
|
572
|
+
* @returns {*} {void}
|
|
573
|
+
* @memberof SignaturePad
|
|
574
|
+
*/
|
|
575
|
+
_handlePointerMove(event) {
|
|
576
|
+
if (!this._allowPointerId(event)) {
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
if (!this._isLeftButtonPressed(event, true) || !this._drawingStroke) {
|
|
580
|
+
this._strokeEnd(this._pointerEventToSignatureEvent(event), false);
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
event.preventDefault();
|
|
584
|
+
this._strokeMoveUpdate(this._pointerEventToSignatureEvent(event));
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* @description 处理指针抬起事件,当左键抬起且指针ID有效时,结束当前笔触
|
|
588
|
+
* @private
|
|
589
|
+
* @param {PointerEvent} event
|
|
590
|
+
* @returns {*} {void}
|
|
591
|
+
* @memberof SignaturePad
|
|
592
|
+
*/
|
|
593
|
+
_handlePointerUp(event) {
|
|
594
|
+
if (this._isLeftButtonPressed(event) || !this._allowPointerId(event)) {
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
event.preventDefault();
|
|
598
|
+
this._strokeEnd(this._pointerEventToSignatureEvent(event));
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* @description 获取点组的样式配置(优先使用点组自身配置,否则用全局配置)
|
|
602
|
+
* @private
|
|
603
|
+
* @param {IPointGroup} [group] - 点组对象(可选)
|
|
604
|
+
* @returns {*} {IPointGroupOptions}
|
|
605
|
+
* @memberof SignaturePad
|
|
606
|
+
*/
|
|
607
|
+
_getPointGroupOptions(group) {
|
|
608
|
+
return {
|
|
609
|
+
penColor: group && "penColor" in group ? group.penColor : this.penColor,
|
|
610
|
+
dotSize: group && "dotSize" in group ? group.dotSize : this.dotSize,
|
|
611
|
+
minWidth: group && "minWidth" in group ? group.minWidth : this.minWidth,
|
|
612
|
+
maxWidth: group && "maxWidth" in group ? group.maxWidth : this.maxWidth,
|
|
613
|
+
velocityFilterWeight: group && "velocityFilterWeight" in group ? group.velocityFilterWeight : this.velocityFilterWeight,
|
|
614
|
+
compositeOperation: group && "compositeOperation" in group ? group.compositeOperation : this.compositeOperation
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* @description 开始绘制笔触(初始化事件监听和绘制状态)
|
|
619
|
+
* @private
|
|
620
|
+
* @param {ISignatureEvent} event - 签名事件对象
|
|
621
|
+
* @memberof SignaturePad
|
|
622
|
+
*/
|
|
623
|
+
_strokeBegin(event) {
|
|
624
|
+
const { addEventListener } = this._getListenerFunctions();
|
|
625
|
+
switch (event.event.type) {
|
|
626
|
+
case "mousedown":
|
|
627
|
+
addEventListener("mousemove", this._handleMouseMove, {
|
|
628
|
+
passive: false
|
|
629
|
+
});
|
|
630
|
+
addEventListener("mouseup", this._handleMouseUp, { passive: false });
|
|
631
|
+
break;
|
|
632
|
+
case "touchstart":
|
|
633
|
+
addEventListener("touchmove", this._handleTouchMove, {
|
|
634
|
+
passive: false
|
|
635
|
+
});
|
|
636
|
+
addEventListener("touchend", this._handleTouchEnd, { passive: false });
|
|
637
|
+
break;
|
|
638
|
+
case "pointerdown":
|
|
639
|
+
addEventListener("pointermove", this._handlePointerMove, {
|
|
640
|
+
passive: false
|
|
641
|
+
});
|
|
642
|
+
addEventListener("pointerup", this._handlePointerUp, {
|
|
643
|
+
passive: false
|
|
644
|
+
});
|
|
645
|
+
break;
|
|
646
|
+
default:
|
|
647
|
+
}
|
|
648
|
+
this._drawingStroke = true;
|
|
649
|
+
const pointGroupOptions = this._getPointGroupOptions();
|
|
650
|
+
const newPointGroup = {
|
|
651
|
+
...pointGroupOptions,
|
|
652
|
+
points: []
|
|
653
|
+
};
|
|
654
|
+
this._data.push(newPointGroup);
|
|
655
|
+
this._reset(pointGroupOptions);
|
|
656
|
+
this._strokeUpdate(event);
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* @description 更新绘制(处理新点并绘制曲线/点)
|
|
660
|
+
* @private
|
|
661
|
+
* @param {ISignatureEvent} event
|
|
662
|
+
* @returns {*} {void}
|
|
663
|
+
* @memberof SignaturePad
|
|
664
|
+
*/
|
|
665
|
+
_strokeUpdate(event) {
|
|
666
|
+
if (!this._drawingStroke) {
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
if (this._data.length === 0) {
|
|
670
|
+
this._strokeBegin(event);
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
const point = this._createPoint(event.x, event.y, event.pressure);
|
|
674
|
+
const lastPointGroup = this._data[this._data.length - 1];
|
|
675
|
+
const lastPoints = lastPointGroup.points;
|
|
676
|
+
const lastPoint = lastPoints.length > 0 && lastPoints[lastPoints.length - 1];
|
|
677
|
+
const isLastPointTooClose = lastPoint ? point.distanceTo(lastPoint) <= this.minDistance : false;
|
|
678
|
+
const pointGroupOptions = this._getPointGroupOptions(lastPointGroup);
|
|
679
|
+
if (!lastPoint || !(lastPoint && isLastPointTooClose)) {
|
|
680
|
+
const curve = this._addPoint(point, pointGroupOptions);
|
|
681
|
+
if (!lastPoint) {
|
|
682
|
+
this._drawDot(point, pointGroupOptions);
|
|
683
|
+
} else if (curve) {
|
|
684
|
+
this._drawCurve(curve, pointGroupOptions);
|
|
685
|
+
}
|
|
686
|
+
lastPoints.push({
|
|
687
|
+
time: point.time,
|
|
688
|
+
x: point.x,
|
|
689
|
+
y: point.y,
|
|
690
|
+
pressure: point.pressure
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* @description 结束绘制笔触(清理事件监听和绘制状态)
|
|
696
|
+
* @private
|
|
697
|
+
* @param {ISignatureEvent} event - 签名事件对象
|
|
698
|
+
* @param {boolean} [shouldUpdate=true] - 是否在结束前更新绘制
|
|
699
|
+
* @returns {*} {void}
|
|
700
|
+
* @memberof SignaturePad
|
|
701
|
+
*/
|
|
702
|
+
_strokeEnd(event, shouldUpdate = true) {
|
|
703
|
+
this._removeMoveUpEventListeners();
|
|
704
|
+
if (!this._drawingStroke) {
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
if (shouldUpdate) {
|
|
708
|
+
this._strokeUpdate(event);
|
|
709
|
+
}
|
|
710
|
+
this._drawingStroke = false;
|
|
711
|
+
this._strokePointerId = void 0;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* @description 初始化指针事件处理(绑定指针按下事件)
|
|
715
|
+
* @private
|
|
716
|
+
* @memberof SignaturePad
|
|
717
|
+
*/
|
|
718
|
+
_handlePointerEvents() {
|
|
719
|
+
this._drawingStroke = false;
|
|
720
|
+
this.canvas.addEventListener("pointerdown", this._handlePointerDown, {
|
|
721
|
+
passive: false
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* @description 初始化鼠标事件处理(绑定鼠标按下事件)
|
|
726
|
+
* @private
|
|
727
|
+
* @memberof SignaturePad
|
|
728
|
+
*/
|
|
729
|
+
_handleMouseEvents() {
|
|
730
|
+
this._drawingStroke = false;
|
|
731
|
+
this.canvas.addEventListener("mousedown", this._handleMouseDown, {
|
|
732
|
+
passive: false
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
/**
|
|
736
|
+
* @description 初始化触摸事件处理(绑定触摸开始事件)
|
|
737
|
+
* @private
|
|
738
|
+
* @memberof SignaturePad
|
|
739
|
+
*/
|
|
740
|
+
_handleTouchEvents() {
|
|
741
|
+
this.canvas.addEventListener("touchstart", this._handleTouchStart, {
|
|
742
|
+
passive: false
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* @description 重置绘制状态(清空最近点、重置速度和宽度等)
|
|
747
|
+
* @private
|
|
748
|
+
* @param {IPointGroupOptions} options - 点组样式配置
|
|
749
|
+
* @memberof SignaturePad
|
|
750
|
+
*/
|
|
751
|
+
_reset(options) {
|
|
752
|
+
this._lastPoints = [];
|
|
753
|
+
this._lastVelocity = 0;
|
|
754
|
+
this._lastWidth = (options.minWidth + options.maxWidth) / 2;
|
|
755
|
+
this._ctx.fillStyle = options.penColor;
|
|
756
|
+
this._ctx.globalCompositeOperation = options.compositeOperation;
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* @description 创建点对象(转换坐标为画布相对坐标)
|
|
760
|
+
* @private
|
|
761
|
+
* @param {number} x - 原始X坐标(相对于视口)
|
|
762
|
+
* @param {number} y - 原始Y坐标(相对于视口)
|
|
763
|
+
* @param {number} pressure - 压力值
|
|
764
|
+
* @returns {*} {Point}
|
|
765
|
+
* @memberof SignaturePad
|
|
766
|
+
*/
|
|
767
|
+
_createPoint(x, y, pressure) {
|
|
768
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
769
|
+
return new point.Point(
|
|
770
|
+
x - rect.left,
|
|
771
|
+
y - rect.top,
|
|
772
|
+
pressure,
|
|
773
|
+
(/* @__PURE__ */ new Date()).getTime()
|
|
774
|
+
);
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* @description 添加点到最近点列表,并在点足够时生成贝塞尔曲线
|
|
778
|
+
* @private
|
|
779
|
+
* @param {Point} point - 新点
|
|
780
|
+
* @param {IPointGroupOptions} options - 样式配置
|
|
781
|
+
* @returns {*} {(Bezier | null)}
|
|
782
|
+
* @memberof SignaturePad
|
|
783
|
+
*/
|
|
784
|
+
_addPoint(point, options) {
|
|
785
|
+
const { _lastPoints } = this;
|
|
786
|
+
_lastPoints.push(point);
|
|
787
|
+
if (_lastPoints.length > 2) {
|
|
788
|
+
if (_lastPoints.length === 3) {
|
|
789
|
+
_lastPoints.unshift(_lastPoints[0]);
|
|
790
|
+
}
|
|
791
|
+
const widths = this._calculateCurveWidths(
|
|
792
|
+
_lastPoints[1],
|
|
793
|
+
_lastPoints[2],
|
|
794
|
+
options
|
|
795
|
+
);
|
|
796
|
+
const curve = bezier.Bezier.fromPoints(_lastPoints, widths);
|
|
797
|
+
_lastPoints.shift();
|
|
798
|
+
return curve;
|
|
799
|
+
}
|
|
800
|
+
return null;
|
|
801
|
+
}
|
|
802
|
+
/**
|
|
803
|
+
* @description 计算曲线的起始和结束宽度(基于速度动态调整)
|
|
804
|
+
* @private
|
|
805
|
+
* @param {Point} startPoint - 曲线起点
|
|
806
|
+
* @param {Point} endPoint - 曲线终点
|
|
807
|
+
* @param {IPointGroupOptions} options - 样式配置
|
|
808
|
+
* @returns {*} {{ start: number; end: number }}
|
|
809
|
+
* @memberof SignaturePad
|
|
810
|
+
*/
|
|
811
|
+
_calculateCurveWidths(startPoint, endPoint, options) {
|
|
812
|
+
const velocity = options.velocityFilterWeight * endPoint.velocityFrom(startPoint) + (1 - options.velocityFilterWeight) * this._lastVelocity;
|
|
813
|
+
const newWidth = this._strokeWidth(velocity, options);
|
|
814
|
+
const widths = {
|
|
815
|
+
end: newWidth,
|
|
816
|
+
start: this._lastWidth
|
|
817
|
+
};
|
|
818
|
+
this._lastVelocity = velocity;
|
|
819
|
+
this._lastWidth = newWidth;
|
|
820
|
+
return widths;
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* @description 根据速度计算线条宽度(速度越快,宽度越接近最小宽度)
|
|
824
|
+
* @private
|
|
825
|
+
* @param {number} velocity - 绘制速度
|
|
826
|
+
* @param {IPointGroupOptions} options - 样式配置
|
|
827
|
+
* @returns {*} {number}
|
|
828
|
+
* @memberof SignaturePad
|
|
829
|
+
*/
|
|
830
|
+
_strokeWidth(velocity, options) {
|
|
831
|
+
return Math.max(options.maxWidth / (velocity + 1), options.minWidth);
|
|
832
|
+
}
|
|
833
|
+
/**
|
|
834
|
+
* @description 绘制曲线片段(以点为中心的圆,用于模拟线条)
|
|
835
|
+
* @private
|
|
836
|
+
* @param {number} x - 片段X坐标
|
|
837
|
+
* @param {number} y - 片段Y坐标
|
|
838
|
+
* @param {number} width - 片段宽度(圆的半径)
|
|
839
|
+
* @memberof SignaturePad
|
|
840
|
+
*/
|
|
841
|
+
_drawCurveSegment(x, y, width) {
|
|
842
|
+
const ctx = this._ctx;
|
|
843
|
+
ctx.moveTo(x, y);
|
|
844
|
+
ctx.arc(x, y, width, 0, 2 * Math.PI, false);
|
|
845
|
+
this._isEmpty = false;
|
|
846
|
+
this._isRedrawn = true;
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* @description 绘制贝塞尔曲线(通过分段绘制多个圆模拟平滑线条)
|
|
850
|
+
* @private
|
|
851
|
+
* @param {Bezier} curve - 贝塞尔曲线对象
|
|
852
|
+
* @param {IPointGroupOptions} options - 样式配置
|
|
853
|
+
* @memberof SignaturePad
|
|
854
|
+
*/
|
|
855
|
+
_drawCurve(curve, options) {
|
|
856
|
+
const ctx = this._ctx;
|
|
857
|
+
const widthDelta = curve.endWidth - curve.startWidth;
|
|
858
|
+
const drawSteps = Math.ceil(curve.length()) * 2;
|
|
859
|
+
ctx.beginPath();
|
|
860
|
+
ctx.fillStyle = options.penColor;
|
|
861
|
+
for (let i = 0; i < drawSteps; i += 1) {
|
|
862
|
+
const t = i / drawSteps;
|
|
863
|
+
const tt = t * t;
|
|
864
|
+
const ttt = tt * t;
|
|
865
|
+
const u = 1 - t;
|
|
866
|
+
const uu = u * u;
|
|
867
|
+
const uuu = uu * u;
|
|
868
|
+
let x = uuu * curve.startPoint.x;
|
|
869
|
+
x += 3 * uu * t * curve.control1.x;
|
|
870
|
+
x += 3 * u * tt * curve.control2.x;
|
|
871
|
+
x += ttt * curve.endPoint.x;
|
|
872
|
+
let y = uuu * curve.startPoint.y;
|
|
873
|
+
y += 3 * uu * t * curve.control1.y;
|
|
874
|
+
y += 3 * u * tt * curve.control2.y;
|
|
875
|
+
y += ttt * curve.endPoint.y;
|
|
876
|
+
const width = Math.min(
|
|
877
|
+
curve.startWidth + ttt * widthDelta,
|
|
878
|
+
options.maxWidth
|
|
879
|
+
);
|
|
880
|
+
this._drawCurveSegment(x, y, width);
|
|
881
|
+
}
|
|
882
|
+
ctx.closePath();
|
|
883
|
+
ctx.fill();
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* @description 绘制点(用于笔触起始或单点点击)
|
|
887
|
+
* @private
|
|
888
|
+
* @param {IBasicPoint} point - 点对象
|
|
889
|
+
* @param {IPointGroupOptions} options - 样式配置
|
|
890
|
+
* @memberof SignaturePad
|
|
891
|
+
*/
|
|
892
|
+
_drawDot(point, options) {
|
|
893
|
+
const ctx = this._ctx;
|
|
894
|
+
const width = options.dotSize > 0 ? options.dotSize : (options.minWidth + options.maxWidth) / 2;
|
|
895
|
+
ctx.beginPath();
|
|
896
|
+
this._drawCurveSegment(point.x, point.y, width);
|
|
897
|
+
ctx.closePath();
|
|
898
|
+
ctx.fillStyle = options.penColor;
|
|
899
|
+
ctx.fill();
|
|
900
|
+
}
|
|
901
|
+
/**
|
|
902
|
+
* @description 从点组数据绘制签名(用于从保存的数据恢复签名)
|
|
903
|
+
* @private
|
|
904
|
+
* @param {IPointGroup[]} pointGroups - 点组数据数组
|
|
905
|
+
* @param {Function} drawCurve - 绘制曲线的函数
|
|
906
|
+
* @param {Function} drawDot - 绘制点的函数
|
|
907
|
+
* @memberof SignaturePad
|
|
908
|
+
*/
|
|
909
|
+
_fromData(pointGroups, drawCurve, drawDot) {
|
|
910
|
+
for (const group of pointGroups) {
|
|
911
|
+
const { points } = group;
|
|
912
|
+
const pointGroupOptions = this._getPointGroupOptions(group);
|
|
913
|
+
if (points.length > 1) {
|
|
914
|
+
for (let j = 0; j < points.length; j += 1) {
|
|
915
|
+
const basicPoint = points[j];
|
|
916
|
+
const point$1 = new point.Point(
|
|
917
|
+
basicPoint.x,
|
|
918
|
+
basicPoint.y,
|
|
919
|
+
basicPoint.pressure,
|
|
920
|
+
basicPoint.time
|
|
921
|
+
);
|
|
922
|
+
if (j === 0) {
|
|
923
|
+
this._reset(pointGroupOptions);
|
|
924
|
+
}
|
|
925
|
+
const curve = this._addPoint(point$1, pointGroupOptions);
|
|
926
|
+
if (curve) {
|
|
927
|
+
drawCurve(curve, pointGroupOptions);
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
} else {
|
|
931
|
+
this._reset(pointGroupOptions);
|
|
932
|
+
drawDot(points[0], pointGroupOptions);
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
/**
|
|
937
|
+
* @description 返回 svg 字符串而不转换为 base64
|
|
938
|
+
* @param {IToSVGOptions} [{ includeBackgroundColor = false }={}] includeBackgroundColor值为true时将背景颜色添加到 SVG 输出
|
|
939
|
+
* @returns {*} {string}
|
|
940
|
+
* @memberof SignaturePad
|
|
941
|
+
*/
|
|
942
|
+
toSVG({ includeBackgroundColor = false } = {}) {
|
|
943
|
+
const pointGroups = this._data;
|
|
944
|
+
const ratio = Math.max(window.devicePixelRatio || 1, 1);
|
|
945
|
+
const minX = 0;
|
|
946
|
+
const minY = 0;
|
|
947
|
+
const maxX = this.canvas.width / ratio;
|
|
948
|
+
const maxY = this.canvas.height / ratio;
|
|
949
|
+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
950
|
+
svg.setAttribute("xmlns", "http://www.w3.org/2000/svg");
|
|
951
|
+
svg.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
|
|
952
|
+
svg.setAttribute("viewBox", "".concat(minX, " ").concat(minY, " ").concat(maxX, " ").concat(maxY));
|
|
953
|
+
svg.setAttribute("width", maxX.toString());
|
|
954
|
+
svg.setAttribute("height", maxY.toString());
|
|
955
|
+
if (includeBackgroundColor && this.backgroundColor) {
|
|
956
|
+
const rect = document.createElement("rect");
|
|
957
|
+
rect.setAttribute("width", "100%");
|
|
958
|
+
rect.setAttribute("height", "100%");
|
|
959
|
+
rect.setAttribute("fill", this.backgroundColor);
|
|
960
|
+
svg.appendChild(rect);
|
|
961
|
+
}
|
|
962
|
+
this._fromData(
|
|
963
|
+
pointGroups,
|
|
964
|
+
(curve, { penColor }) => {
|
|
965
|
+
const path = document.createElement("path");
|
|
966
|
+
if (!Number.isNaN(curve.control1.x) && !Number.isNaN(curve.control1.y) && !Number.isNaN(curve.control2.x) && !Number.isNaN(curve.control2.y)) {
|
|
967
|
+
const attr = "M ".concat(curve.startPoint.x.toFixed(3), ",").concat(curve.startPoint.y.toFixed(
|
|
968
|
+
3
|
|
969
|
+
), " ") + "C ".concat(curve.control1.x.toFixed(3), ",").concat(curve.control1.y.toFixed(3), " ") + "".concat(curve.control2.x.toFixed(3), ",").concat(curve.control2.y.toFixed(3), " ") + "".concat(curve.endPoint.x.toFixed(3), ",").concat(curve.endPoint.y.toFixed(3));
|
|
970
|
+
path.setAttribute("d", attr);
|
|
971
|
+
path.setAttribute("stroke-width", (curve.endWidth * 2.25).toFixed(3));
|
|
972
|
+
path.setAttribute("stroke", penColor);
|
|
973
|
+
path.setAttribute("fill", "none");
|
|
974
|
+
path.setAttribute("stroke-linecap", "round");
|
|
975
|
+
svg.appendChild(path);
|
|
976
|
+
}
|
|
977
|
+
},
|
|
978
|
+
(point, { penColor, dotSize, minWidth, maxWidth }) => {
|
|
979
|
+
const circle = document.createElement("circle");
|
|
980
|
+
const size = dotSize > 0 ? dotSize : (minWidth + maxWidth) / 2;
|
|
981
|
+
circle.setAttribute("r", size.toString());
|
|
982
|
+
circle.setAttribute("cx", point.x.toString());
|
|
983
|
+
circle.setAttribute("cy", point.y.toString());
|
|
984
|
+
circle.setAttribute("fill", penColor);
|
|
985
|
+
svg.appendChild(circle);
|
|
986
|
+
}
|
|
987
|
+
);
|
|
988
|
+
return svg.outerHTML;
|
|
989
|
+
}
|
|
990
|
+
/**
|
|
991
|
+
* @description 将Blob对象转换为DataURL
|
|
992
|
+
* @param {Blob} blob - 要转换的Blob对象
|
|
993
|
+
* @returns {*} {Promise<string>}
|
|
994
|
+
* @memberof SignaturePad
|
|
995
|
+
*/
|
|
996
|
+
blobToDataURL(blob) {
|
|
997
|
+
return new Promise((resolve, reject) => {
|
|
998
|
+
const reader = new FileReader();
|
|
999
|
+
reader.onload = () => {
|
|
1000
|
+
resolve(reader.result);
|
|
1001
|
+
};
|
|
1002
|
+
reader.onerror = () => {
|
|
1003
|
+
reject(new Error("\u65E0\u6CD5\u8F6C\u6362Blob\u4E3ADataURL"));
|
|
1004
|
+
};
|
|
1005
|
+
reader.readAsDataURL(blob);
|
|
1006
|
+
});
|
|
1007
|
+
}
|
|
1008
|
+
/**
|
|
1009
|
+
* @description 处理图片加载并计算加载时间(通过回调通知完成状态)
|
|
1010
|
+
* @param {string} imageUrl
|
|
1011
|
+
* @param {() => void} _callBack
|
|
1012
|
+
* @memberof SignaturePad
|
|
1013
|
+
*/
|
|
1014
|
+
loadImage(imageUrl, _callBack) {
|
|
1015
|
+
const img = new Image();
|
|
1016
|
+
img.onload = () => _callBack();
|
|
1017
|
+
img.onerror = () => _callBack;
|
|
1018
|
+
img.src = imageUrl;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
exports.default = SignaturePad;
|