@npm_lx/signature-pad-for-vue3 0.0.1
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/README.md +124 -0
- package/dist/signature-pad.css +1 -0
- package/dist/signature-pad.js +597 -0
- package/dist/signature-pad.umd.cjs +1 -0
- package/dist/vite.svg +1 -0
- package/package.json +25 -0
package/README.md
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# @lx/signature-pad-for-vue3
|
|
2
|
+
|
|
3
|
+
一个基于[signature_pad](https://github.com/szimek/signature_pad)的签名板Vue3组件,支持自定义样式和多种导出格式,简单易用,可用于用户签名、合同签名等场景,可随时更换背景图片。
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## 安装
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install @lx/signature-pad-for-vue3
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## 组件预览
|
|
13
|
+
```bash
|
|
14
|
+
pnpm install
|
|
15
|
+
pnpm run dev
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 基本使用
|
|
19
|
+
|
|
20
|
+
```vue
|
|
21
|
+
<template>
|
|
22
|
+
<div class="signature-container">
|
|
23
|
+
<SignaturePad
|
|
24
|
+
ref="signaturePadRef"
|
|
25
|
+
:penMinWidth="2"
|
|
26
|
+
:penMaxWidth="5"
|
|
27
|
+
:penColor="'#000000'"
|
|
28
|
+
:backgroundColor="'#F3F3F4'"
|
|
29
|
+
@beginStroke="handleBeginStroke"
|
|
30
|
+
@endStroke="handleEndStroke"
|
|
31
|
+
/>
|
|
32
|
+
<div class="signature-actions">
|
|
33
|
+
<button @click="clearSignature">清除</button>
|
|
34
|
+
<button @click="saveSignature">保存</button>
|
|
35
|
+
<button @click="exportImage">导出图片</button>
|
|
36
|
+
</div>
|
|
37
|
+
</div>
|
|
38
|
+
</template>
|
|
39
|
+
|
|
40
|
+
<script setup>
|
|
41
|
+
import { ref } from 'vue'
|
|
42
|
+
import SignaturePad from '@lx/signature-pad-for-vue3'
|
|
43
|
+
|
|
44
|
+
const signaturePadRef = ref(null)
|
|
45
|
+
|
|
46
|
+
const handleBeginStroke = () => {
|
|
47
|
+
console.log('开始签名')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const handleEndStroke = () => {
|
|
51
|
+
console.log('结束签名')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const clearSignature = () => {
|
|
55
|
+
signaturePadRef.value.clear()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const saveSignature = () => {
|
|
59
|
+
const base64Data = signaturePadRef.value.getBase64Data()
|
|
60
|
+
if (base64Data) {
|
|
61
|
+
console.log('签名数据:', base64Data)
|
|
62
|
+
// 可以将base64Data发送到服务器保存
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const exportImage = () => {
|
|
67
|
+
signaturePadRef.value.getImageFile()
|
|
68
|
+
}
|
|
69
|
+
</script>
|
|
70
|
+
|
|
71
|
+
<style scoped>
|
|
72
|
+
.signature-container {
|
|
73
|
+
width: 400px;
|
|
74
|
+
height: 300px;
|
|
75
|
+
border: 1px solid #ccc;
|
|
76
|
+
margin: 0 auto;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.signature-actions {
|
|
80
|
+
margin-top: 20px;
|
|
81
|
+
text-align: center;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
button {
|
|
85
|
+
margin: 0 10px;
|
|
86
|
+
padding: 8px 16px;
|
|
87
|
+
cursor: pointer;
|
|
88
|
+
}
|
|
89
|
+
</style>
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## 配置项
|
|
93
|
+
|
|
94
|
+
| 参数名 | 类型 | 默认值 | 描述 |
|
|
95
|
+
| --- | --- | --- | --- |
|
|
96
|
+
| penMinWidth | Number | 2 | 笔画的最小宽度 |
|
|
97
|
+
| penMaxWidth | Number | 2 | 笔画的最大宽度 |
|
|
98
|
+
| penColor | String | '#000000' | 笔画的颜色 |
|
|
99
|
+
| backgroundColor | String | '#F3F3F4' | 画板的背景颜色 |
|
|
100
|
+
| bgImageUrl | String | '' | 背景图片的URL(用于回显签名) |
|
|
101
|
+
|
|
102
|
+
## 方法
|
|
103
|
+
|
|
104
|
+
| 方法名 | 参数 | 返回值 | 描述 |
|
|
105
|
+
| --- | --- | --- | --- |
|
|
106
|
+
| clear | isClearBg: Boolean (默认: false) | 无 | 清空画板,isClearBg为true时同时清除背景图片 |
|
|
107
|
+
| getBase64Data | format: String (默认: 'png'), quality: Number (默认: 0.95) | String | 获取签名的Base64数据 |
|
|
108
|
+
| getImageFile | format: String (默认: 'png'), quality: Number (默认: 0.95) | 无 | 导出签名为图片文件 |
|
|
109
|
+
| setBgImage | url: String | 无 | 设置背景图片 |
|
|
110
|
+
| isCanvasEmpty | 无 | Boolean | 检查画板是否为空 |
|
|
111
|
+
|
|
112
|
+
## 事件
|
|
113
|
+
|
|
114
|
+
| 事件名 | 说明 |
|
|
115
|
+
| --- | --- |
|
|
116
|
+
| beginStroke | 开始签名时触发 |
|
|
117
|
+
| endStroke | 结束签名时触发 |
|
|
118
|
+
|
|
119
|
+
## 注意事项
|
|
120
|
+
|
|
121
|
+
1. 组件需要一个有固定宽高的容器来显示
|
|
122
|
+
2. 使用ref来调用组件的方法
|
|
123
|
+
3. 使用背景图片需要来源支持跨域设置
|
|
124
|
+
4. 导出的图片格式支持png、jpg等常见格式
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
.signature-canvas-container[data-v-745eff61]{width:100%;height:100%;display:flex;flex-direction:column}
|
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
import { watch as k, ref as y, onMounted as O, nextTick as I, onUnmounted as A, createElementBlock as F, openBlock as R, createElementVNode as B } from "vue";
|
|
2
|
+
var b = class {
|
|
3
|
+
x;
|
|
4
|
+
y;
|
|
5
|
+
pressure;
|
|
6
|
+
time;
|
|
7
|
+
constructor(d, t, e, i) {
|
|
8
|
+
if (isNaN(d) || isNaN(t))
|
|
9
|
+
throw new Error(`Point is invalid: (${d}, ${t})`);
|
|
10
|
+
this.x = +d, this.y = +t, this.pressure = e || 0, this.time = i || Date.now();
|
|
11
|
+
}
|
|
12
|
+
distanceTo(d) {
|
|
13
|
+
return Math.sqrt(
|
|
14
|
+
Math.pow(this.x - d.x, 2) + Math.pow(this.y - d.y, 2)
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
equals(d) {
|
|
18
|
+
return this.x === d.x && this.y === d.y && this.pressure === d.pressure && this.time === d.time;
|
|
19
|
+
}
|
|
20
|
+
velocityFrom(d) {
|
|
21
|
+
return this.time !== d.time ? this.distanceTo(d) / (this.time - d.time) : 0;
|
|
22
|
+
}
|
|
23
|
+
}, $ = class W {
|
|
24
|
+
constructor(t, e, i, n, a, r) {
|
|
25
|
+
this.startPoint = t, this.control2 = e, this.control1 = i, this.endPoint = n, this.startWidth = a, this.endWidth = r;
|
|
26
|
+
}
|
|
27
|
+
static fromPoints(t, e) {
|
|
28
|
+
const i = this.calculateControlPoints(t[0], t[1], t[2]).c2, n = this.calculateControlPoints(t[1], t[2], t[3]).c1;
|
|
29
|
+
return new W(t[1], i, n, t[2], e.start, e.end);
|
|
30
|
+
}
|
|
31
|
+
static calculateControlPoints(t, e, i) {
|
|
32
|
+
const n = t.x - e.x, a = t.y - e.y, r = e.x - i.x, o = e.y - i.y, c = { x: (t.x + e.x) / 2, y: (t.y + e.y) / 2 }, h = { x: (e.x + i.x) / 2, y: (e.y + i.y) / 2 }, s = Math.sqrt(n * n + a * a), u = Math.sqrt(r * r + o * o), v = c.x - h.x, p = c.y - h.y, f = s + u == 0 ? 0 : u / (s + u), _ = { x: h.x + v * f, y: h.y + p * f }, x = e.x - _.x, P = e.y - _.y;
|
|
33
|
+
return {
|
|
34
|
+
c1: new b(c.x + x, c.y + P),
|
|
35
|
+
c2: new b(h.x + x, h.y + P)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
// Returns approximated length. Code taken from https://www.lemoda.net/maths/bezier-length/index.html.
|
|
39
|
+
length() {
|
|
40
|
+
let e = 0, i, n;
|
|
41
|
+
for (let a = 0; a <= 10; a += 1) {
|
|
42
|
+
const r = a / 10, o = this.point(
|
|
43
|
+
r,
|
|
44
|
+
this.startPoint.x,
|
|
45
|
+
this.control1.x,
|
|
46
|
+
this.control2.x,
|
|
47
|
+
this.endPoint.x
|
|
48
|
+
), c = this.point(
|
|
49
|
+
r,
|
|
50
|
+
this.startPoint.y,
|
|
51
|
+
this.control1.y,
|
|
52
|
+
this.control2.y,
|
|
53
|
+
this.endPoint.y
|
|
54
|
+
);
|
|
55
|
+
if (a > 0) {
|
|
56
|
+
const h = o - i, s = c - n;
|
|
57
|
+
e += Math.sqrt(h * h + s * s);
|
|
58
|
+
}
|
|
59
|
+
i = o, n = c;
|
|
60
|
+
}
|
|
61
|
+
return e;
|
|
62
|
+
}
|
|
63
|
+
// Calculate parametric value of x or y given t and the four point coordinates of a cubic bezier curve.
|
|
64
|
+
point(t, e, i, n, a) {
|
|
65
|
+
return e * (1 - t) * (1 - t) * (1 - t) + 3 * i * (1 - t) * (1 - t) * t + 3 * n * (1 - t) * t * t + a * t * t * t;
|
|
66
|
+
}
|
|
67
|
+
}, N = class {
|
|
68
|
+
/* tslint:disable: variable-name */
|
|
69
|
+
_et;
|
|
70
|
+
/* tslint:enable: variable-name */
|
|
71
|
+
constructor() {
|
|
72
|
+
try {
|
|
73
|
+
this._et = new EventTarget();
|
|
74
|
+
} catch {
|
|
75
|
+
this._et = document;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
addEventListener(d, t, e) {
|
|
79
|
+
this._et.addEventListener(d, t, e);
|
|
80
|
+
}
|
|
81
|
+
dispatchEvent(d) {
|
|
82
|
+
return this._et.dispatchEvent(d);
|
|
83
|
+
}
|
|
84
|
+
removeEventListener(d, t, e) {
|
|
85
|
+
this._et.removeEventListener(d, t, e);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
function z(d, t = 250) {
|
|
89
|
+
let e = 0, i = null, n, a, r;
|
|
90
|
+
const o = () => {
|
|
91
|
+
e = Date.now(), i = null, n = d.apply(a, r), i || (a = null, r = []);
|
|
92
|
+
};
|
|
93
|
+
return function(...h) {
|
|
94
|
+
const s = Date.now(), u = t - (s - e);
|
|
95
|
+
return a = this, r = h, u <= 0 || u > t ? (i && (clearTimeout(i), i = null), e = s, n = d.apply(a, r), i || (a = null, r = [])) : i || (i = window.setTimeout(o, u)), n;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
var G = class C extends N {
|
|
99
|
+
/* tslint:enable: variable-name */
|
|
100
|
+
constructor(t, e = {}) {
|
|
101
|
+
super(), this.canvas = t, this.velocityFilterWeight = e.velocityFilterWeight || 0.7, this.minWidth = e.minWidth || 0.5, this.maxWidth = e.maxWidth || 2.5, this.throttle = e.throttle ?? 16, this.minDistance = e.minDistance ?? 5, this.dotSize = e.dotSize || 0, this.penColor = e.penColor || "black", this.backgroundColor = e.backgroundColor || "rgba(0,0,0,0)", this.compositeOperation = e.compositeOperation || "source-over", this.canvasContextOptions = e.canvasContextOptions ?? {}, this._strokeMoveUpdate = this.throttle ? z(C.prototype._strokeUpdate, this.throttle) : C.prototype._strokeUpdate, this._handleMouseDown = this._handleMouseDown.bind(this), this._handleMouseMove = this._handleMouseMove.bind(this), this._handleMouseUp = this._handleMouseUp.bind(this), this._handleTouchStart = this._handleTouchStart.bind(this), this._handleTouchMove = this._handleTouchMove.bind(this), this._handleTouchEnd = this._handleTouchEnd.bind(this), this._handlePointerDown = this._handlePointerDown.bind(this), this._handlePointerMove = this._handlePointerMove.bind(this), this._handlePointerUp = this._handlePointerUp.bind(this), this._handlePointerCancel = this._handlePointerCancel.bind(this), this._handleTouchCancel = this._handleTouchCancel.bind(this), this._ctx = t.getContext(
|
|
102
|
+
"2d",
|
|
103
|
+
this.canvasContextOptions
|
|
104
|
+
), this.clear(), this.on();
|
|
105
|
+
}
|
|
106
|
+
// Public stuff
|
|
107
|
+
dotSize;
|
|
108
|
+
minWidth;
|
|
109
|
+
maxWidth;
|
|
110
|
+
penColor;
|
|
111
|
+
minDistance;
|
|
112
|
+
velocityFilterWeight;
|
|
113
|
+
compositeOperation;
|
|
114
|
+
backgroundColor;
|
|
115
|
+
throttle;
|
|
116
|
+
canvasContextOptions;
|
|
117
|
+
// Private stuff
|
|
118
|
+
/* tslint:disable: variable-name */
|
|
119
|
+
_ctx;
|
|
120
|
+
_drawingStroke = !1;
|
|
121
|
+
_isEmpty = !0;
|
|
122
|
+
_dataUrl;
|
|
123
|
+
_dataUrlOptions;
|
|
124
|
+
_lastPoints = [];
|
|
125
|
+
// Stores up to 4 most recent points; used to generate a new curve
|
|
126
|
+
_data = [];
|
|
127
|
+
// Stores all points in groups (one group per line or dot)
|
|
128
|
+
_lastVelocity = 0;
|
|
129
|
+
_lastWidth = 0;
|
|
130
|
+
_strokeMoveUpdate;
|
|
131
|
+
_strokePointerId;
|
|
132
|
+
clear() {
|
|
133
|
+
const { _ctx: t, canvas: e } = this;
|
|
134
|
+
t.fillStyle = this.backgroundColor, t.clearRect(0, 0, e.width, e.height), t.fillRect(0, 0, e.width, e.height), this._data = [], this._reset(this._getPointGroupOptions()), this._isEmpty = !0, this._dataUrl = void 0, this._dataUrlOptions = void 0, this._strokePointerId = void 0;
|
|
135
|
+
}
|
|
136
|
+
redraw() {
|
|
137
|
+
const t = this._data, e = this._dataUrl, i = this._dataUrlOptions;
|
|
138
|
+
this.clear(), e && this.fromDataURL(e, i), this.fromData(t, { clear: !1 });
|
|
139
|
+
}
|
|
140
|
+
fromDataURL(t, e = {}) {
|
|
141
|
+
return new Promise((i, n) => {
|
|
142
|
+
const a = new Image(), r = e.ratio || window.devicePixelRatio || 1, o = e.width || this.canvas.width / r, c = e.height || this.canvas.height / r, h = e.xOffset || 0, s = e.yOffset || 0;
|
|
143
|
+
this._reset(this._getPointGroupOptions()), a.onload = () => {
|
|
144
|
+
this._ctx.drawImage(a, h, s, o, c), i();
|
|
145
|
+
}, a.onerror = (u) => {
|
|
146
|
+
n(u);
|
|
147
|
+
}, a.crossOrigin = "anonymous", a.src = t, this._isEmpty = !1, this._dataUrl = t, this._dataUrlOptions = { ...e };
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
toDataURL(t = "image/png", e) {
|
|
151
|
+
return t === "image/svg+xml" ? (typeof e != "object" && (e = void 0), `data:image/svg+xml;base64,${btoa(
|
|
152
|
+
this.toSVG(e)
|
|
153
|
+
)}`) : (typeof e != "number" && (e = void 0), this.canvas.toDataURL(t, e));
|
|
154
|
+
}
|
|
155
|
+
on() {
|
|
156
|
+
this.canvas.style.touchAction = "none", this.canvas.style.msTouchAction = "none", this.canvas.style.userSelect = "none", this.canvas.style.webkitUserSelect = "none";
|
|
157
|
+
const t = /Macintosh/.test(navigator.userAgent) && "ontouchstart" in document;
|
|
158
|
+
window.PointerEvent && !t ? this._handlePointerEvents() : (this._handleMouseEvents(), "ontouchstart" in window && this._handleTouchEvents());
|
|
159
|
+
}
|
|
160
|
+
off() {
|
|
161
|
+
this.canvas.style.touchAction = "auto", this.canvas.style.msTouchAction = "auto", this.canvas.style.userSelect = "auto", this.canvas.style.webkitUserSelect = "auto", this.canvas.removeEventListener("pointerdown", this._handlePointerDown), this.canvas.removeEventListener("mousedown", this._handleMouseDown), this.canvas.removeEventListener("touchstart", this._handleTouchStart), this._removeMoveUpEventListeners();
|
|
162
|
+
}
|
|
163
|
+
_getListenerFunctions() {
|
|
164
|
+
const t = window.document === this.canvas.ownerDocument ? window : this.canvas.ownerDocument.defaultView ?? this.canvas.ownerDocument;
|
|
165
|
+
return {
|
|
166
|
+
addEventListener: t.addEventListener.bind(
|
|
167
|
+
t
|
|
168
|
+
),
|
|
169
|
+
removeEventListener: t.removeEventListener.bind(
|
|
170
|
+
t
|
|
171
|
+
)
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
_removeMoveUpEventListeners() {
|
|
175
|
+
const { removeEventListener: t } = this._getListenerFunctions();
|
|
176
|
+
t("pointermove", this._handlePointerMove), t("pointerup", this._handlePointerUp), t("pointercancel", this._handlePointerCancel), t("mousemove", this._handleMouseMove), t("mouseup", this._handleMouseUp), t("touchmove", this._handleTouchMove), t("touchend", this._handleTouchEnd), t("touchcancel", this._handleTouchCancel);
|
|
177
|
+
}
|
|
178
|
+
isEmpty() {
|
|
179
|
+
return this._isEmpty;
|
|
180
|
+
}
|
|
181
|
+
fromData(t, { clear: e = !0 } = {}) {
|
|
182
|
+
e && this.clear(), this._fromData(
|
|
183
|
+
t,
|
|
184
|
+
this._drawCurve.bind(this),
|
|
185
|
+
this._drawDot.bind(this)
|
|
186
|
+
), this._data = this._data.concat(t);
|
|
187
|
+
}
|
|
188
|
+
toData() {
|
|
189
|
+
return this._data;
|
|
190
|
+
}
|
|
191
|
+
_isLeftButtonPressed(t, e) {
|
|
192
|
+
return e ? t.buttons === 1 : (t.buttons & 1) === 1;
|
|
193
|
+
}
|
|
194
|
+
_pointerEventToSignatureEvent(t) {
|
|
195
|
+
return {
|
|
196
|
+
event: t,
|
|
197
|
+
type: t.type,
|
|
198
|
+
x: t.clientX,
|
|
199
|
+
y: t.clientY,
|
|
200
|
+
pressure: "pressure" in t ? t.pressure : 0
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
_touchEventToSignatureEvent(t) {
|
|
204
|
+
const e = t.changedTouches[0];
|
|
205
|
+
return {
|
|
206
|
+
event: t,
|
|
207
|
+
type: t.type,
|
|
208
|
+
x: e.clientX,
|
|
209
|
+
y: e.clientY,
|
|
210
|
+
pressure: e.force
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
// Event handlers
|
|
214
|
+
_handleMouseDown(t) {
|
|
215
|
+
!this._isLeftButtonPressed(t, !0) || this._drawingStroke || this._strokeBegin(this._pointerEventToSignatureEvent(t));
|
|
216
|
+
}
|
|
217
|
+
_handleMouseMove(t) {
|
|
218
|
+
if (!this._isLeftButtonPressed(t, !0) || !this._drawingStroke) {
|
|
219
|
+
this._strokeEnd(this._pointerEventToSignatureEvent(t), !1);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
this._strokeMoveUpdate(this._pointerEventToSignatureEvent(t));
|
|
223
|
+
}
|
|
224
|
+
_handleMouseUp(t) {
|
|
225
|
+
this._isLeftButtonPressed(t) || this._strokeEnd(this._pointerEventToSignatureEvent(t));
|
|
226
|
+
}
|
|
227
|
+
_handleTouchStart(t) {
|
|
228
|
+
t.targetTouches.length !== 1 || this._drawingStroke || (t.cancelable && t.preventDefault(), this._strokeBegin(this._touchEventToSignatureEvent(t)));
|
|
229
|
+
}
|
|
230
|
+
_handleTouchMove(t) {
|
|
231
|
+
if (t.targetTouches.length === 1) {
|
|
232
|
+
if (t.cancelable && t.preventDefault(), !this._drawingStroke) {
|
|
233
|
+
this._strokeEnd(this._touchEventToSignatureEvent(t), !1);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
this._strokeMoveUpdate(this._touchEventToSignatureEvent(t));
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
_handleTouchEnd(t) {
|
|
240
|
+
t.targetTouches.length === 0 && (t.cancelable && t.preventDefault(), this._strokeEnd(this._touchEventToSignatureEvent(t)));
|
|
241
|
+
}
|
|
242
|
+
_handlePointerCancel(t) {
|
|
243
|
+
this._allowPointerId(t) && (t.preventDefault(), this._strokeEnd(this._pointerEventToSignatureEvent(t), !1));
|
|
244
|
+
}
|
|
245
|
+
_handleTouchCancel(t) {
|
|
246
|
+
t.cancelable && t.preventDefault(), this._strokeEnd(this._touchEventToSignatureEvent(t), !1);
|
|
247
|
+
}
|
|
248
|
+
_getPointerId(t) {
|
|
249
|
+
return t.persistentDeviceId || t.pointerId;
|
|
250
|
+
}
|
|
251
|
+
_allowPointerId(t, e = !1) {
|
|
252
|
+
return typeof this._strokePointerId > "u" ? e : this._getPointerId(t) === this._strokePointerId;
|
|
253
|
+
}
|
|
254
|
+
_handlePointerDown(t) {
|
|
255
|
+
this._drawingStroke || !this._isLeftButtonPressed(t) || !this._allowPointerId(t, !0) || (this._strokePointerId = this._getPointerId(t), t.preventDefault(), this._strokeBegin(this._pointerEventToSignatureEvent(t)));
|
|
256
|
+
}
|
|
257
|
+
_handlePointerMove(t) {
|
|
258
|
+
if (this._allowPointerId(t)) {
|
|
259
|
+
if (!this._isLeftButtonPressed(t, !0) || !this._drawingStroke) {
|
|
260
|
+
this._strokeEnd(this._pointerEventToSignatureEvent(t), !1);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
t.preventDefault(), this._strokeMoveUpdate(this._pointerEventToSignatureEvent(t));
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
_handlePointerUp(t) {
|
|
267
|
+
this._isLeftButtonPressed(t) || !this._allowPointerId(t) || (t.preventDefault(), this._strokeEnd(this._pointerEventToSignatureEvent(t)));
|
|
268
|
+
}
|
|
269
|
+
_getPointGroupOptions(t) {
|
|
270
|
+
return {
|
|
271
|
+
penColor: t && "penColor" in t ? t.penColor : this.penColor,
|
|
272
|
+
dotSize: t && "dotSize" in t ? t.dotSize : this.dotSize,
|
|
273
|
+
minWidth: t && "minWidth" in t ? t.minWidth : this.minWidth,
|
|
274
|
+
maxWidth: t && "maxWidth" in t ? t.maxWidth : this.maxWidth,
|
|
275
|
+
velocityFilterWeight: t && "velocityFilterWeight" in t ? t.velocityFilterWeight : this.velocityFilterWeight,
|
|
276
|
+
compositeOperation: t && "compositeOperation" in t ? t.compositeOperation : this.compositeOperation
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
// Private methods
|
|
280
|
+
_strokeBegin(t) {
|
|
281
|
+
if (!this.dispatchEvent(
|
|
282
|
+
new CustomEvent("beginStroke", { detail: t, cancelable: !0 })
|
|
283
|
+
))
|
|
284
|
+
return;
|
|
285
|
+
const { addEventListener: i } = this._getListenerFunctions();
|
|
286
|
+
switch (t.event.type) {
|
|
287
|
+
case "mousedown":
|
|
288
|
+
i("mousemove", this._handleMouseMove, {
|
|
289
|
+
passive: !1
|
|
290
|
+
}), i("mouseup", this._handleMouseUp, { passive: !1 });
|
|
291
|
+
break;
|
|
292
|
+
case "touchstart":
|
|
293
|
+
i("touchmove", this._handleTouchMove, {
|
|
294
|
+
passive: !1
|
|
295
|
+
}), i("touchend", this._handleTouchEnd, { passive: !1 }), i("touchcancel", this._handleTouchCancel, { passive: !1 });
|
|
296
|
+
break;
|
|
297
|
+
case "pointerdown":
|
|
298
|
+
i("pointermove", this._handlePointerMove, {
|
|
299
|
+
passive: !1
|
|
300
|
+
}), i("pointerup", this._handlePointerUp, {
|
|
301
|
+
passive: !1
|
|
302
|
+
}), i("pointercancel", this._handlePointerCancel, {
|
|
303
|
+
passive: !1
|
|
304
|
+
});
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
this._drawingStroke = !0;
|
|
308
|
+
const n = this._getPointGroupOptions(), a = {
|
|
309
|
+
...n,
|
|
310
|
+
points: []
|
|
311
|
+
};
|
|
312
|
+
this._data.push(a), this._reset(n), this._strokeUpdate(t);
|
|
313
|
+
}
|
|
314
|
+
_strokeUpdate(t) {
|
|
315
|
+
if (!this._drawingStroke)
|
|
316
|
+
return;
|
|
317
|
+
if (this._data.length === 0) {
|
|
318
|
+
this._strokeBegin(t);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
this.dispatchEvent(
|
|
322
|
+
new CustomEvent("beforeUpdateStroke", { detail: t })
|
|
323
|
+
);
|
|
324
|
+
const e = this._createPoint(t.x, t.y, t.pressure), i = this._data[this._data.length - 1], n = i.points, a = n.length > 0 && n[n.length - 1], r = a ? e.distanceTo(a) <= this.minDistance : !1, o = this._getPointGroupOptions(i);
|
|
325
|
+
if (!a || !(a && r)) {
|
|
326
|
+
const c = this._addPoint(e, o);
|
|
327
|
+
a ? c && this._drawCurve(c, o) : this._drawDot(e, o), n.push({
|
|
328
|
+
time: e.time,
|
|
329
|
+
x: e.x,
|
|
330
|
+
y: e.y,
|
|
331
|
+
pressure: e.pressure
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
this.dispatchEvent(new CustomEvent("afterUpdateStroke", { detail: t }));
|
|
335
|
+
}
|
|
336
|
+
_strokeEnd(t, e = !0) {
|
|
337
|
+
this._removeMoveUpEventListeners(), this._drawingStroke && (e && this._strokeUpdate(t), this._drawingStroke = !1, this._strokePointerId = void 0, this.dispatchEvent(new CustomEvent("endStroke", { detail: t })));
|
|
338
|
+
}
|
|
339
|
+
_handlePointerEvents() {
|
|
340
|
+
this._drawingStroke = !1, this.canvas.addEventListener("pointerdown", this._handlePointerDown, {
|
|
341
|
+
passive: !1
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
_handleMouseEvents() {
|
|
345
|
+
this._drawingStroke = !1, this.canvas.addEventListener("mousedown", this._handleMouseDown, {
|
|
346
|
+
passive: !1
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
_handleTouchEvents() {
|
|
350
|
+
this.canvas.addEventListener("touchstart", this._handleTouchStart, {
|
|
351
|
+
passive: !1
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
// Called when a new line is started
|
|
355
|
+
_reset(t) {
|
|
356
|
+
this._lastPoints = [], this._lastVelocity = 0, this._lastWidth = (t.minWidth + t.maxWidth) / 2, this._ctx.fillStyle = t.penColor, this._ctx.globalCompositeOperation = t.compositeOperation;
|
|
357
|
+
}
|
|
358
|
+
_createPoint(t, e, i) {
|
|
359
|
+
const n = this.canvas.getBoundingClientRect();
|
|
360
|
+
return new b(
|
|
361
|
+
t - n.left,
|
|
362
|
+
e - n.top,
|
|
363
|
+
i,
|
|
364
|
+
(/* @__PURE__ */ new Date()).getTime()
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
// Add point to _lastPoints array and generate a new curve if there are enough points (i.e. 3)
|
|
368
|
+
_addPoint(t, e) {
|
|
369
|
+
const { _lastPoints: i } = this;
|
|
370
|
+
if (i.push(t), i.length > 2) {
|
|
371
|
+
i.length === 3 && i.unshift(i[0]);
|
|
372
|
+
const n = this._calculateCurveWidths(
|
|
373
|
+
i[1],
|
|
374
|
+
i[2],
|
|
375
|
+
e
|
|
376
|
+
), a = $.fromPoints(i, n);
|
|
377
|
+
return i.shift(), a;
|
|
378
|
+
}
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
_calculateCurveWidths(t, e, i) {
|
|
382
|
+
const n = i.velocityFilterWeight * e.velocityFrom(t) + (1 - i.velocityFilterWeight) * this._lastVelocity, a = this._strokeWidth(n, i), r = {
|
|
383
|
+
end: a,
|
|
384
|
+
start: this._lastWidth
|
|
385
|
+
};
|
|
386
|
+
return this._lastVelocity = n, this._lastWidth = a, r;
|
|
387
|
+
}
|
|
388
|
+
_strokeWidth(t, e) {
|
|
389
|
+
return Math.max(e.maxWidth / (t + 1), e.minWidth);
|
|
390
|
+
}
|
|
391
|
+
_drawCurveSegment(t, e, i) {
|
|
392
|
+
const n = this._ctx;
|
|
393
|
+
n.moveTo(t, e), n.arc(t, e, i, 0, 2 * Math.PI, !1), this._isEmpty = !1;
|
|
394
|
+
}
|
|
395
|
+
_drawCurve(t, e) {
|
|
396
|
+
const i = this._ctx, n = t.endWidth - t.startWidth, a = Math.ceil(t.length()) * 2;
|
|
397
|
+
i.beginPath(), i.fillStyle = e.penColor;
|
|
398
|
+
for (let r = 0; r < a; r += 1) {
|
|
399
|
+
const o = r / a, c = o * o, h = c * o, s = 1 - o, u = s * s, v = u * s;
|
|
400
|
+
let p = v * t.startPoint.x;
|
|
401
|
+
p += 3 * u * o * t.control1.x, p += 3 * s * c * t.control2.x, p += h * t.endPoint.x;
|
|
402
|
+
let f = v * t.startPoint.y;
|
|
403
|
+
f += 3 * u * o * t.control1.y, f += 3 * s * c * t.control2.y, f += h * t.endPoint.y;
|
|
404
|
+
const _ = Math.min(
|
|
405
|
+
t.startWidth + h * n,
|
|
406
|
+
e.maxWidth
|
|
407
|
+
);
|
|
408
|
+
this._drawCurveSegment(p, f, _);
|
|
409
|
+
}
|
|
410
|
+
i.closePath(), i.fill();
|
|
411
|
+
}
|
|
412
|
+
_drawDot(t, e) {
|
|
413
|
+
const i = this._ctx, n = e.dotSize > 0 ? e.dotSize : (e.minWidth + e.maxWidth) / 2;
|
|
414
|
+
i.beginPath(), this._drawCurveSegment(t.x, t.y, n), i.closePath(), i.fillStyle = e.penColor, i.fill();
|
|
415
|
+
}
|
|
416
|
+
_fromData(t, e, i) {
|
|
417
|
+
for (const n of t) {
|
|
418
|
+
const { points: a } = n, r = this._getPointGroupOptions(n);
|
|
419
|
+
if (a.length > 1)
|
|
420
|
+
for (let o = 0; o < a.length; o += 1) {
|
|
421
|
+
const c = a[o], h = new b(
|
|
422
|
+
c.x,
|
|
423
|
+
c.y,
|
|
424
|
+
c.pressure,
|
|
425
|
+
c.time
|
|
426
|
+
);
|
|
427
|
+
o === 0 && this._reset(r);
|
|
428
|
+
const s = this._addPoint(h, r);
|
|
429
|
+
s && e(s, r);
|
|
430
|
+
}
|
|
431
|
+
else
|
|
432
|
+
this._reset(r), i(a[0], r);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
toSVG({ includeBackgroundColor: t = !1, includeDataUrl: e = !1 } = {}) {
|
|
436
|
+
const i = this._data, n = Math.max(window.devicePixelRatio || 1, 1), a = 0, r = 0, o = this.canvas.width / n, c = this.canvas.height / n, h = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
437
|
+
if (h.setAttribute("xmlns", "http://www.w3.org/2000/svg"), h.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink"), h.setAttribute("viewBox", `${a} ${r} ${o} ${c}`), h.setAttribute("width", o.toString()), h.setAttribute("height", c.toString()), t && this.backgroundColor) {
|
|
438
|
+
const s = document.createElement("rect");
|
|
439
|
+
s.setAttribute("width", "100%"), s.setAttribute("height", "100%"), s.setAttribute("fill", this.backgroundColor), h.appendChild(s);
|
|
440
|
+
}
|
|
441
|
+
if (e && this._dataUrl) {
|
|
442
|
+
const s = this._dataUrlOptions?.ratio || window.devicePixelRatio || 1, u = this._dataUrlOptions?.width || this.canvas.width / s, v = this._dataUrlOptions?.height || this.canvas.height / s, p = this._dataUrlOptions?.xOffset || 0, f = this._dataUrlOptions?.yOffset || 0, _ = document.createElement("image");
|
|
443
|
+
_.setAttribute("x", p.toString()), _.setAttribute("y", f.toString()), _.setAttribute("width", u.toString()), _.setAttribute("height", v.toString()), _.setAttribute("preserveAspectRatio", "none"), _.setAttribute("href", this._dataUrl), h.appendChild(_);
|
|
444
|
+
}
|
|
445
|
+
return this._fromData(
|
|
446
|
+
i,
|
|
447
|
+
(s, { penColor: u }) => {
|
|
448
|
+
const v = document.createElement("path");
|
|
449
|
+
if (!isNaN(s.control1.x) && !isNaN(s.control1.y) && !isNaN(s.control2.x) && !isNaN(s.control2.y)) {
|
|
450
|
+
const p = `M ${s.startPoint.x.toFixed(3)},${s.startPoint.y.toFixed(
|
|
451
|
+
3
|
|
452
|
+
)} C ${s.control1.x.toFixed(3)},${s.control1.y.toFixed(3)} ${s.control2.x.toFixed(3)},${s.control2.y.toFixed(3)} ${s.endPoint.x.toFixed(3)},${s.endPoint.y.toFixed(3)}`;
|
|
453
|
+
v.setAttribute("d", p), v.setAttribute("stroke-width", (s.endWidth * 2.25).toFixed(3)), v.setAttribute("stroke", u), v.setAttribute("fill", "none"), v.setAttribute("stroke-linecap", "round"), h.appendChild(v);
|
|
454
|
+
}
|
|
455
|
+
},
|
|
456
|
+
(s, { penColor: u, dotSize: v, minWidth: p, maxWidth: f }) => {
|
|
457
|
+
const _ = document.createElement("circle"), x = v > 0 ? v : (p + f) / 2;
|
|
458
|
+
_.setAttribute("r", x.toString()), _.setAttribute("cx", s.x.toString()), _.setAttribute("cy", s.y.toString()), _.setAttribute("fill", u), h.appendChild(_);
|
|
459
|
+
}
|
|
460
|
+
), h.outerHTML;
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
const V = (d, t) => {
|
|
464
|
+
const e = d.__vccOpts || d;
|
|
465
|
+
for (const [i, n] of t)
|
|
466
|
+
e[i] = n;
|
|
467
|
+
return e;
|
|
468
|
+
}, q = {
|
|
469
|
+
__name: "index",
|
|
470
|
+
props: {
|
|
471
|
+
// 笔画最小宽度
|
|
472
|
+
penMinWidth: {
|
|
473
|
+
type: Number,
|
|
474
|
+
default: 2
|
|
475
|
+
},
|
|
476
|
+
// 笔画最大宽度
|
|
477
|
+
penMaxWidth: {
|
|
478
|
+
type: Number,
|
|
479
|
+
default: 2
|
|
480
|
+
},
|
|
481
|
+
// 笔画颜色
|
|
482
|
+
penColor: {
|
|
483
|
+
type: String,
|
|
484
|
+
default: "#000000"
|
|
485
|
+
},
|
|
486
|
+
// 背景色
|
|
487
|
+
backgroundColor: {
|
|
488
|
+
type: String,
|
|
489
|
+
default: "#F3F3F4"
|
|
490
|
+
},
|
|
491
|
+
// 是否有背景图(用于回显)
|
|
492
|
+
bgImageUrl: {
|
|
493
|
+
type: String,
|
|
494
|
+
default: ""
|
|
495
|
+
}
|
|
496
|
+
},
|
|
497
|
+
emits: ["beginStroke", "endStroke"],
|
|
498
|
+
setup(d, { expose: t, emit: e }) {
|
|
499
|
+
const i = e, n = d;
|
|
500
|
+
k(
|
|
501
|
+
() => n.bgImageUrl,
|
|
502
|
+
() => {
|
|
503
|
+
v();
|
|
504
|
+
}
|
|
505
|
+
), k(
|
|
506
|
+
() => n.penColor,
|
|
507
|
+
() => {
|
|
508
|
+
o.value && (o.value.penColor = n.penColor);
|
|
509
|
+
}
|
|
510
|
+
), k(
|
|
511
|
+
() => n.penMinWidth,
|
|
512
|
+
(l) => {
|
|
513
|
+
o.value && (o.value.minWidth = l);
|
|
514
|
+
}
|
|
515
|
+
), k(
|
|
516
|
+
() => n.penMaxWidth,
|
|
517
|
+
(l) => {
|
|
518
|
+
o.value && (o.value.maxWidth = l);
|
|
519
|
+
}
|
|
520
|
+
);
|
|
521
|
+
const a = y(null), r = y(null), o = y(null), c = y(null), h = y(!0), s = y(null);
|
|
522
|
+
let u = window.devicePixelRatio || 1;
|
|
523
|
+
const v = () => {
|
|
524
|
+
if (n.bgImageUrl) {
|
|
525
|
+
const l = new Image();
|
|
526
|
+
l.crossOrigin = "anonymous", l.onload = () => {
|
|
527
|
+
c.value = l, o.value && (o.value.clear(), s.value && o.value.fromData(s.value));
|
|
528
|
+
}, l.src = n.bgImageUrl;
|
|
529
|
+
} else
|
|
530
|
+
c.value = null, o.value && (o.value.clear(), s.value && o.value.fromData(s.value));
|
|
531
|
+
}, p = () => {
|
|
532
|
+
if (!r.value || !a.value) return;
|
|
533
|
+
const l = r.value, m = a.value, g = m.clientWidth, S = m.clientHeight;
|
|
534
|
+
l.width = g * u, l.height = S * u, l.style.width = `${g}px`, l.style.height = `${S}px`;
|
|
535
|
+
const E = l.getContext("2d");
|
|
536
|
+
E.scale(u, u);
|
|
537
|
+
const w = new G(l, {
|
|
538
|
+
minWidth: n.penMinWidth,
|
|
539
|
+
maxWidth: n.penMaxWidth,
|
|
540
|
+
penColor: n.penColor,
|
|
541
|
+
backgroundColor: n.backgroundColor
|
|
542
|
+
});
|
|
543
|
+
f(w), _(w);
|
|
544
|
+
const D = w.clear;
|
|
545
|
+
w.clear = (L) => {
|
|
546
|
+
D.call(w), E.clearRect(0, 0, l.width, l.height), c.value && !L ? E.drawImage(c.value, 0, 0, g, S) : (E.fillStyle = n.backgroundColor, E.fillRect(0, 0, g, S));
|
|
547
|
+
}, o.value = w, w.clear(), s.value && w.fromData(s.value);
|
|
548
|
+
}, f = (l) => {
|
|
549
|
+
l.removeEventListener("beginStroke"), l.removeEventListener("endStroke");
|
|
550
|
+
}, _ = (l) => {
|
|
551
|
+
l.addEventListener("beginStroke", () => {
|
|
552
|
+
i("beginStroke");
|
|
553
|
+
}), l.addEventListener("endStroke", () => {
|
|
554
|
+
i("endStroke");
|
|
555
|
+
});
|
|
556
|
+
}, x = (l = !1) => {
|
|
557
|
+
o.value?.clear(l), s.value = null, h.value = !0;
|
|
558
|
+
}, P = (l = "png", m = 0.95) => o.value && !o.value.isEmpty() ? o.value.toDataURL(`image/${l}`, m) : null, T = (l = "png", m = 0.95) => {
|
|
559
|
+
const g = document.createElement("a");
|
|
560
|
+
g.href = o.value.toDataURL(`image/${l}`, m), g.download = `signature.${l}`, g.click();
|
|
561
|
+
}, U = (l) => {
|
|
562
|
+
const m = new Image();
|
|
563
|
+
m.crossOrigin = "anonymous", m.onload = () => {
|
|
564
|
+
c.value = m, o.value && (o.value.clear(), s.value && o.value.fromData(s.value));
|
|
565
|
+
}, m.src = l;
|
|
566
|
+
}, M = () => {
|
|
567
|
+
u = window.devicePixelRatio || 1, o.value && (s.value = o.value.toData()), p();
|
|
568
|
+
};
|
|
569
|
+
return O(() => {
|
|
570
|
+
I(() => {
|
|
571
|
+
p(), v(), window.addEventListener("resize", M);
|
|
572
|
+
});
|
|
573
|
+
}), A(() => {
|
|
574
|
+
window.removeEventListener("resize", M);
|
|
575
|
+
}), t({
|
|
576
|
+
clear: x,
|
|
577
|
+
getBase64Data: P,
|
|
578
|
+
getImageFile: T,
|
|
579
|
+
setBgImage: U,
|
|
580
|
+
isCanvasEmpty: () => (h.value = o.value.isEmpty(), h.value),
|
|
581
|
+
signaturePadRef: o
|
|
582
|
+
}), (l, m) => (R(), F("div", {
|
|
583
|
+
class: "signature-canvas-container",
|
|
584
|
+
ref_key: "containerRef",
|
|
585
|
+
ref: a
|
|
586
|
+
}, [
|
|
587
|
+
B("canvas", {
|
|
588
|
+
ref_key: "canvasRef",
|
|
589
|
+
ref: r,
|
|
590
|
+
class: "signature-canvas-pad"
|
|
591
|
+
}, null, 512)
|
|
592
|
+
], 512));
|
|
593
|
+
}
|
|
594
|
+
}, j = /* @__PURE__ */ V(q, [["__scopeId", "data-v-745eff61"]]);
|
|
595
|
+
export {
|
|
596
|
+
j as default
|
|
597
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(_,w){typeof exports=="object"&&typeof module<"u"?module.exports=w(require("vue")):typeof define=="function"&&define.amd?define(["vue"],w):(_=typeof globalThis<"u"?globalThis:_||self,_.SignaturePad=w(_.Vue))})(this,(function(_){"use strict";var w=class{x;y;pressure;time;constructor(d,t,e,i){if(isNaN(d)||isNaN(t))throw new Error(`Point is invalid: (${d}, ${t})`);this.x=+d,this.y=+t,this.pressure=e||0,this.time=i||Date.now()}distanceTo(d){return Math.sqrt(Math.pow(this.x-d.x,2)+Math.pow(this.y-d.y,2))}equals(d){return this.x===d.x&&this.y===d.y&&this.pressure===d.pressure&&this.time===d.time}velocityFrom(d){return this.time!==d.time?this.distanceTo(d)/(this.time-d.time):0}},T=class M{constructor(t,e,i,n,a,r){this.startPoint=t,this.control2=e,this.control1=i,this.endPoint=n,this.startWidth=a,this.endWidth=r}static fromPoints(t,e){const i=this.calculateControlPoints(t[0],t[1],t[2]).c2,n=this.calculateControlPoints(t[1],t[2],t[3]).c1;return new M(t[1],i,n,t[2],e.start,e.end)}static calculateControlPoints(t,e,i){const n=t.x-e.x,a=t.y-e.y,r=e.x-i.x,o=e.y-i.y,c={x:(t.x+e.x)/2,y:(t.y+e.y)/2},h={x:(e.x+i.x)/2,y:(e.y+i.y)/2},s=Math.sqrt(n*n+a*a),u=Math.sqrt(r*r+o*o),p=c.x-h.x,v=c.y-h.y,m=s+u==0?0:u/(s+u),f={x:h.x+p*m,y:h.y+v*m},E=e.x-f.x,S=e.y-f.y;return{c1:new w(c.x+E,c.y+S),c2:new w(h.x+E,h.y+S)}}length(){let e=0,i,n;for(let a=0;a<=10;a+=1){const r=a/10,o=this.point(r,this.startPoint.x,this.control1.x,this.control2.x,this.endPoint.x),c=this.point(r,this.startPoint.y,this.control1.y,this.control2.y,this.endPoint.y);if(a>0){const h=o-i,s=c-n;e+=Math.sqrt(h*h+s*s)}i=o,n=c}return e}point(t,e,i,n,a){return e*(1-t)*(1-t)*(1-t)+3*i*(1-t)*(1-t)*t+3*n*(1-t)*t*t+a*t*t*t}},W=class{_et;constructor(){try{this._et=new EventTarget}catch{this._et=document}}addEventListener(d,t,e){this._et.addEventListener(d,t,e)}dispatchEvent(d){return this._et.dispatchEvent(d)}removeEventListener(d,t,e){this._et.removeEventListener(d,t,e)}};function U(d,t=250){let e=0,i=null,n,a,r;const o=()=>{e=Date.now(),i=null,n=d.apply(a,r),i||(a=null,r=[])};return function(...h){const s=Date.now(),u=t-(s-e);return a=this,r=h,u<=0||u>t?(i&&(clearTimeout(i),i=null),e=s,n=d.apply(a,r),i||(a=null,r=[])):i||(i=window.setTimeout(o,u)),n}}var D=class b extends W{constructor(t,e={}){super(),this.canvas=t,this.velocityFilterWeight=e.velocityFilterWeight||.7,this.minWidth=e.minWidth||.5,this.maxWidth=e.maxWidth||2.5,this.throttle=e.throttle??16,this.minDistance=e.minDistance??5,this.dotSize=e.dotSize||0,this.penColor=e.penColor||"black",this.backgroundColor=e.backgroundColor||"rgba(0,0,0,0)",this.compositeOperation=e.compositeOperation||"source-over",this.canvasContextOptions=e.canvasContextOptions??{},this._strokeMoveUpdate=this.throttle?U(b.prototype._strokeUpdate,this.throttle):b.prototype._strokeUpdate,this._handleMouseDown=this._handleMouseDown.bind(this),this._handleMouseMove=this._handleMouseMove.bind(this),this._handleMouseUp=this._handleMouseUp.bind(this),this._handleTouchStart=this._handleTouchStart.bind(this),this._handleTouchMove=this._handleTouchMove.bind(this),this._handleTouchEnd=this._handleTouchEnd.bind(this),this._handlePointerDown=this._handlePointerDown.bind(this),this._handlePointerMove=this._handlePointerMove.bind(this),this._handlePointerUp=this._handlePointerUp.bind(this),this._handlePointerCancel=this._handlePointerCancel.bind(this),this._handleTouchCancel=this._handleTouchCancel.bind(this),this._ctx=t.getContext("2d",this.canvasContextOptions),this.clear(),this.on()}dotSize;minWidth;maxWidth;penColor;minDistance;velocityFilterWeight;compositeOperation;backgroundColor;throttle;canvasContextOptions;_ctx;_drawingStroke=!1;_isEmpty=!0;_dataUrl;_dataUrlOptions;_lastPoints=[];_data=[];_lastVelocity=0;_lastWidth=0;_strokeMoveUpdate;_strokePointerId;clear(){const{_ctx:t,canvas:e}=this;t.fillStyle=this.backgroundColor,t.clearRect(0,0,e.width,e.height),t.fillRect(0,0,e.width,e.height),this._data=[],this._reset(this._getPointGroupOptions()),this._isEmpty=!0,this._dataUrl=void 0,this._dataUrlOptions=void 0,this._strokePointerId=void 0}redraw(){const t=this._data,e=this._dataUrl,i=this._dataUrlOptions;this.clear(),e&&this.fromDataURL(e,i),this.fromData(t,{clear:!1})}fromDataURL(t,e={}){return new Promise((i,n)=>{const a=new Image,r=e.ratio||window.devicePixelRatio||1,o=e.width||this.canvas.width/r,c=e.height||this.canvas.height/r,h=e.xOffset||0,s=e.yOffset||0;this._reset(this._getPointGroupOptions()),a.onload=()=>{this._ctx.drawImage(a,h,s,o,c),i()},a.onerror=u=>{n(u)},a.crossOrigin="anonymous",a.src=t,this._isEmpty=!1,this._dataUrl=t,this._dataUrlOptions={...e}})}toDataURL(t="image/png",e){return t==="image/svg+xml"?(typeof e!="object"&&(e=void 0),`data:image/svg+xml;base64,${btoa(this.toSVG(e))}`):(typeof e!="number"&&(e=void 0),this.canvas.toDataURL(t,e))}on(){this.canvas.style.touchAction="none",this.canvas.style.msTouchAction="none",this.canvas.style.userSelect="none",this.canvas.style.webkitUserSelect="none";const t=/Macintosh/.test(navigator.userAgent)&&"ontouchstart"in document;window.PointerEvent&&!t?this._handlePointerEvents():(this._handleMouseEvents(),"ontouchstart"in window&&this._handleTouchEvents())}off(){this.canvas.style.touchAction="auto",this.canvas.style.msTouchAction="auto",this.canvas.style.userSelect="auto",this.canvas.style.webkitUserSelect="auto",this.canvas.removeEventListener("pointerdown",this._handlePointerDown),this.canvas.removeEventListener("mousedown",this._handleMouseDown),this.canvas.removeEventListener("touchstart",this._handleTouchStart),this._removeMoveUpEventListeners()}_getListenerFunctions(){const t=window.document===this.canvas.ownerDocument?window:this.canvas.ownerDocument.defaultView??this.canvas.ownerDocument;return{addEventListener:t.addEventListener.bind(t),removeEventListener:t.removeEventListener.bind(t)}}_removeMoveUpEventListeners(){const{removeEventListener:t}=this._getListenerFunctions();t("pointermove",this._handlePointerMove),t("pointerup",this._handlePointerUp),t("pointercancel",this._handlePointerCancel),t("mousemove",this._handleMouseMove),t("mouseup",this._handleMouseUp),t("touchmove",this._handleTouchMove),t("touchend",this._handleTouchEnd),t("touchcancel",this._handleTouchCancel)}isEmpty(){return this._isEmpty}fromData(t,{clear:e=!0}={}){e&&this.clear(),this._fromData(t,this._drawCurve.bind(this),this._drawDot.bind(this)),this._data=this._data.concat(t)}toData(){return this._data}_isLeftButtonPressed(t,e){return e?t.buttons===1:(t.buttons&1)===1}_pointerEventToSignatureEvent(t){return{event:t,type:t.type,x:t.clientX,y:t.clientY,pressure:"pressure"in t?t.pressure:0}}_touchEventToSignatureEvent(t){const e=t.changedTouches[0];return{event:t,type:t.type,x:e.clientX,y:e.clientY,pressure:e.force}}_handleMouseDown(t){!this._isLeftButtonPressed(t,!0)||this._drawingStroke||this._strokeBegin(this._pointerEventToSignatureEvent(t))}_handleMouseMove(t){if(!this._isLeftButtonPressed(t,!0)||!this._drawingStroke){this._strokeEnd(this._pointerEventToSignatureEvent(t),!1);return}this._strokeMoveUpdate(this._pointerEventToSignatureEvent(t))}_handleMouseUp(t){this._isLeftButtonPressed(t)||this._strokeEnd(this._pointerEventToSignatureEvent(t))}_handleTouchStart(t){t.targetTouches.length!==1||this._drawingStroke||(t.cancelable&&t.preventDefault(),this._strokeBegin(this._touchEventToSignatureEvent(t)))}_handleTouchMove(t){if(t.targetTouches.length===1){if(t.cancelable&&t.preventDefault(),!this._drawingStroke){this._strokeEnd(this._touchEventToSignatureEvent(t),!1);return}this._strokeMoveUpdate(this._touchEventToSignatureEvent(t))}}_handleTouchEnd(t){t.targetTouches.length===0&&(t.cancelable&&t.preventDefault(),this._strokeEnd(this._touchEventToSignatureEvent(t)))}_handlePointerCancel(t){this._allowPointerId(t)&&(t.preventDefault(),this._strokeEnd(this._pointerEventToSignatureEvent(t),!1))}_handleTouchCancel(t){t.cancelable&&t.preventDefault(),this._strokeEnd(this._touchEventToSignatureEvent(t),!1)}_getPointerId(t){return t.persistentDeviceId||t.pointerId}_allowPointerId(t,e=!1){return typeof this._strokePointerId>"u"?e:this._getPointerId(t)===this._strokePointerId}_handlePointerDown(t){this._drawingStroke||!this._isLeftButtonPressed(t)||!this._allowPointerId(t,!0)||(this._strokePointerId=this._getPointerId(t),t.preventDefault(),this._strokeBegin(this._pointerEventToSignatureEvent(t)))}_handlePointerMove(t){if(this._allowPointerId(t)){if(!this._isLeftButtonPressed(t,!0)||!this._drawingStroke){this._strokeEnd(this._pointerEventToSignatureEvent(t),!1);return}t.preventDefault(),this._strokeMoveUpdate(this._pointerEventToSignatureEvent(t))}}_handlePointerUp(t){this._isLeftButtonPressed(t)||!this._allowPointerId(t)||(t.preventDefault(),this._strokeEnd(this._pointerEventToSignatureEvent(t)))}_getPointGroupOptions(t){return{penColor:t&&"penColor"in t?t.penColor:this.penColor,dotSize:t&&"dotSize"in t?t.dotSize:this.dotSize,minWidth:t&&"minWidth"in t?t.minWidth:this.minWidth,maxWidth:t&&"maxWidth"in t?t.maxWidth:this.maxWidth,velocityFilterWeight:t&&"velocityFilterWeight"in t?t.velocityFilterWeight:this.velocityFilterWeight,compositeOperation:t&&"compositeOperation"in t?t.compositeOperation:this.compositeOperation}}_strokeBegin(t){if(!this.dispatchEvent(new CustomEvent("beginStroke",{detail:t,cancelable:!0})))return;const{addEventListener:i}=this._getListenerFunctions();switch(t.event.type){case"mousedown":i("mousemove",this._handleMouseMove,{passive:!1}),i("mouseup",this._handleMouseUp,{passive:!1});break;case"touchstart":i("touchmove",this._handleTouchMove,{passive:!1}),i("touchend",this._handleTouchEnd,{passive:!1}),i("touchcancel",this._handleTouchCancel,{passive:!1});break;case"pointerdown":i("pointermove",this._handlePointerMove,{passive:!1}),i("pointerup",this._handlePointerUp,{passive:!1}),i("pointercancel",this._handlePointerCancel,{passive:!1});break}this._drawingStroke=!0;const n=this._getPointGroupOptions(),a={...n,points:[]};this._data.push(a),this._reset(n),this._strokeUpdate(t)}_strokeUpdate(t){if(!this._drawingStroke)return;if(this._data.length===0){this._strokeBegin(t);return}this.dispatchEvent(new CustomEvent("beforeUpdateStroke",{detail:t}));const e=this._createPoint(t.x,t.y,t.pressure),i=this._data[this._data.length-1],n=i.points,a=n.length>0&&n[n.length-1],r=a?e.distanceTo(a)<=this.minDistance:!1,o=this._getPointGroupOptions(i);if(!a||!(a&&r)){const c=this._addPoint(e,o);a?c&&this._drawCurve(c,o):this._drawDot(e,o),n.push({time:e.time,x:e.x,y:e.y,pressure:e.pressure})}this.dispatchEvent(new CustomEvent("afterUpdateStroke",{detail:t}))}_strokeEnd(t,e=!0){this._removeMoveUpEventListeners(),this._drawingStroke&&(e&&this._strokeUpdate(t),this._drawingStroke=!1,this._strokePointerId=void 0,this.dispatchEvent(new CustomEvent("endStroke",{detail:t})))}_handlePointerEvents(){this._drawingStroke=!1,this.canvas.addEventListener("pointerdown",this._handlePointerDown,{passive:!1})}_handleMouseEvents(){this._drawingStroke=!1,this.canvas.addEventListener("mousedown",this._handleMouseDown,{passive:!1})}_handleTouchEvents(){this.canvas.addEventListener("touchstart",this._handleTouchStart,{passive:!1})}_reset(t){this._lastPoints=[],this._lastVelocity=0,this._lastWidth=(t.minWidth+t.maxWidth)/2,this._ctx.fillStyle=t.penColor,this._ctx.globalCompositeOperation=t.compositeOperation}_createPoint(t,e,i){const n=this.canvas.getBoundingClientRect();return new w(t-n.left,e-n.top,i,new Date().getTime())}_addPoint(t,e){const{_lastPoints:i}=this;if(i.push(t),i.length>2){i.length===3&&i.unshift(i[0]);const n=this._calculateCurveWidths(i[1],i[2],e),a=T.fromPoints(i,n);return i.shift(),a}return null}_calculateCurveWidths(t,e,i){const n=i.velocityFilterWeight*e.velocityFrom(t)+(1-i.velocityFilterWeight)*this._lastVelocity,a=this._strokeWidth(n,i),r={end:a,start:this._lastWidth};return this._lastVelocity=n,this._lastWidth=a,r}_strokeWidth(t,e){return Math.max(e.maxWidth/(t+1),e.minWidth)}_drawCurveSegment(t,e,i){const n=this._ctx;n.moveTo(t,e),n.arc(t,e,i,0,2*Math.PI,!1),this._isEmpty=!1}_drawCurve(t,e){const i=this._ctx,n=t.endWidth-t.startWidth,a=Math.ceil(t.length())*2;i.beginPath(),i.fillStyle=e.penColor;for(let r=0;r<a;r+=1){const o=r/a,c=o*o,h=c*o,s=1-o,u=s*s,p=u*s;let v=p*t.startPoint.x;v+=3*u*o*t.control1.x,v+=3*s*c*t.control2.x,v+=h*t.endPoint.x;let m=p*t.startPoint.y;m+=3*u*o*t.control1.y,m+=3*s*c*t.control2.y,m+=h*t.endPoint.y;const f=Math.min(t.startWidth+h*n,e.maxWidth);this._drawCurveSegment(v,m,f)}i.closePath(),i.fill()}_drawDot(t,e){const i=this._ctx,n=e.dotSize>0?e.dotSize:(e.minWidth+e.maxWidth)/2;i.beginPath(),this._drawCurveSegment(t.x,t.y,n),i.closePath(),i.fillStyle=e.penColor,i.fill()}_fromData(t,e,i){for(const n of t){const{points:a}=n,r=this._getPointGroupOptions(n);if(a.length>1)for(let o=0;o<a.length;o+=1){const c=a[o],h=new w(c.x,c.y,c.pressure,c.time);o===0&&this._reset(r);const s=this._addPoint(h,r);s&&e(s,r)}else this._reset(r),i(a[0],r)}}toSVG({includeBackgroundColor:t=!1,includeDataUrl:e=!1}={}){const i=this._data,n=Math.max(window.devicePixelRatio||1,1),a=0,r=0,o=this.canvas.width/n,c=this.canvas.height/n,h=document.createElementNS("http://www.w3.org/2000/svg","svg");if(h.setAttribute("xmlns","http://www.w3.org/2000/svg"),h.setAttribute("xmlns:xlink","http://www.w3.org/1999/xlink"),h.setAttribute("viewBox",`${a} ${r} ${o} ${c}`),h.setAttribute("width",o.toString()),h.setAttribute("height",c.toString()),t&&this.backgroundColor){const s=document.createElement("rect");s.setAttribute("width","100%"),s.setAttribute("height","100%"),s.setAttribute("fill",this.backgroundColor),h.appendChild(s)}if(e&&this._dataUrl){const s=this._dataUrlOptions?.ratio||window.devicePixelRatio||1,u=this._dataUrlOptions?.width||this.canvas.width/s,p=this._dataUrlOptions?.height||this.canvas.height/s,v=this._dataUrlOptions?.xOffset||0,m=this._dataUrlOptions?.yOffset||0,f=document.createElement("image");f.setAttribute("x",v.toString()),f.setAttribute("y",m.toString()),f.setAttribute("width",u.toString()),f.setAttribute("height",p.toString()),f.setAttribute("preserveAspectRatio","none"),f.setAttribute("href",this._dataUrl),h.appendChild(f)}return this._fromData(i,(s,{penColor:u})=>{const p=document.createElement("path");if(!isNaN(s.control1.x)&&!isNaN(s.control1.y)&&!isNaN(s.control2.x)&&!isNaN(s.control2.y)){const v=`M ${s.startPoint.x.toFixed(3)},${s.startPoint.y.toFixed(3)} C ${s.control1.x.toFixed(3)},${s.control1.y.toFixed(3)} ${s.control2.x.toFixed(3)},${s.control2.y.toFixed(3)} ${s.endPoint.x.toFixed(3)},${s.endPoint.y.toFixed(3)}`;p.setAttribute("d",v),p.setAttribute("stroke-width",(s.endWidth*2.25).toFixed(3)),p.setAttribute("stroke",u),p.setAttribute("fill","none"),p.setAttribute("stroke-linecap","round"),h.appendChild(p)}},(s,{penColor:u,dotSize:p,minWidth:v,maxWidth:m})=>{const f=document.createElement("circle"),E=p>0?p:(v+m)/2;f.setAttribute("r",E.toString()),f.setAttribute("cx",s.x.toString()),f.setAttribute("cy",s.y.toString()),f.setAttribute("fill",u),h.appendChild(f)}),h.outerHTML}};return((d,t)=>{const e=d.__vccOpts||d;for(const[i,n]of t)e[i]=n;return e})({__name:"index",props:{penMinWidth:{type:Number,default:2},penMaxWidth:{type:Number,default:2},penColor:{type:String,default:"#000000"},backgroundColor:{type:String,default:"#F3F3F4"},bgImageUrl:{type:String,default:""}},emits:["beginStroke","endStroke"],setup(d,{expose:t,emit:e}){const i=e,n=d;_.watch(()=>n.bgImageUrl,()=>{p()}),_.watch(()=>n.penColor,()=>{o.value&&(o.value.penColor=n.penColor)}),_.watch(()=>n.penMinWidth,l=>{o.value&&(o.value.minWidth=l)}),_.watch(()=>n.penMaxWidth,l=>{o.value&&(o.value.maxWidth=l)});const a=_.ref(null),r=_.ref(null),o=_.ref(null),c=_.ref(null),h=_.ref(!0),s=_.ref(null);let u=window.devicePixelRatio||1;const p=()=>{if(n.bgImageUrl){const l=new Image;l.crossOrigin="anonymous",l.onload=()=>{c.value=l,o.value&&(o.value.clear(),s.value&&o.value.fromData(s.value))},l.src=n.bgImageUrl}else c.value=null,o.value&&(o.value.clear(),s.value&&o.value.fromData(s.value))},v=()=>{if(!r.value||!a.value)return;const l=r.value,g=a.value,x=g.clientWidth,k=g.clientHeight;l.width=x*u,l.height=k*u,l.style.width=`${x}px`,l.style.height=`${k}px`;const P=l.getContext("2d");P.scale(u,u);const y=new D(l,{minWidth:n.penMinWidth,maxWidth:n.penMaxWidth,penColor:n.penColor,backgroundColor:n.backgroundColor});m(y),f(y);const I=y.clear;y.clear=A=>{I.call(y),P.clearRect(0,0,l.width,l.height),c.value&&!A?P.drawImage(c.value,0,0,x,k):(P.fillStyle=n.backgroundColor,P.fillRect(0,0,x,k))},o.value=y,y.clear(),s.value&&y.fromData(s.value)},m=l=>{l.removeEventListener("beginStroke"),l.removeEventListener("endStroke")},f=l=>{l.addEventListener("beginStroke",()=>{i("beginStroke")}),l.addEventListener("endStroke",()=>{i("endStroke")})},E=(l=!1)=>{o.value?.clear(l),s.value=null,h.value=!0},S=(l="png",g=.95)=>o.value&&!o.value.isEmpty()?o.value.toDataURL(`image/${l}`,g):null,L=(l="png",g=.95)=>{const x=document.createElement("a");x.href=o.value.toDataURL(`image/${l}`,g),x.download=`signature.${l}`,x.click()},O=l=>{const g=new Image;g.crossOrigin="anonymous",g.onload=()=>{c.value=g,o.value&&(o.value.clear(),s.value&&o.value.fromData(s.value))},g.src=l},C=()=>{u=window.devicePixelRatio||1,o.value&&(s.value=o.value.toData()),v()};return _.onMounted(()=>{_.nextTick(()=>{v(),p(),window.addEventListener("resize",C)})}),_.onUnmounted(()=>{window.removeEventListener("resize",C)}),t({clear:E,getBase64Data:S,getImageFile:L,setBgImage:O,isCanvasEmpty:()=>(h.value=o.value.isEmpty(),h.value),signaturePadRef:o}),(l,g)=>(_.openBlock(),_.createElementBlock("div",{class:"signature-canvas-container",ref_key:"containerRef",ref:a},[_.createElementVNode("canvas",{ref_key:"canvasRef",ref:r,class:"signature-canvas-pad"},null,512)],512))}},[["__scopeId","data-v-745eff61"]])}));
|
package/dist/vite.svg
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@npm_lx/signature-pad-for-vue3",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"private": false,
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite",
|
|
8
|
+
"build": "vite build",
|
|
9
|
+
"preview": "vite preview"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"signature_pad": "^5.1.3"
|
|
13
|
+
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"vue": "^3.0.0"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"main": "dist/signature-pad.js",
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@vitejs/plugin-vue": "^6.0.1",
|
|
23
|
+
"vite": "^7.2.4"
|
|
24
|
+
}
|
|
25
|
+
}
|