SignaturePad.vue
6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<template>
<view class="signature-container">
<view class="signature-header">
<text class="signature-title">会员签字</text>
<view class="signature-actions">
<button @click="clearSignature" class="btn-clear">清除</button>
<button @click="confirmSignature" class="btn-confirm">确认</button>
</view>
</view>
<!-- -->
<view class="signature-pad" id="signaturePad">
<canvas
canvas-id="signature-canvas"
style="width: 100%; height: 100%;"
@touchstart="startDrawing"
@touchmove="draw"
@touchend="stopDrawing"
class="signature-canvas"
></canvas>
</view>
<view class="signature-tips">
<text class="tips-text">请在上方区域进行签字</text>
</view>
</view>
</template>
<script>
export default {
name: 'SignaturePad',
props: {
width: {
type: Number,
default: 300
},
height: {
type: Number,
default: 150
},
lineWidth: {
type: Number,
default: 2
},
strokeColor: {
type: String,
default: '#000000'
}
},
data() {
return {
canvasWidth: 300,
canvasHeight: 150,
isDrawing: false,
lastX: 0,
lastY: 0,
ctx: null,
hasSignature: false,
// 存储所有绘制的路径点
paths: []
}
},
mounted() {
this.initCanvas();
},
methods: {
// 初始化画布
initCanvas() {
// this.canvasWidth = this.width;
// this.canvasHeight = this.height;
// 获取signature-pad的宽高
const query = uni.createSelectorQuery().in(this);
// 选择需要查询的元素(支持类名、ID等选择器)
query.select('#signaturePad')
.boundingClientRect(data => {
if (data) {
this.canvasWidth = data.width;
this.canvasHeight = data.height;
} else {
console.log('未找到元素');
}
})
.exec(); // 执行查询
// console.error(this.canvasWidth, this.canvasHeight);
this.$nextTick(() => {
this.ctx = uni.createCanvasContext('signature-canvas', this);
this.drawBackground();
});
},
// 绘制背景
drawBackground() {
if (!this.ctx) return;
// 清除画布
this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight);
// 设置背景色
this.ctx.fillStyle = '#ffffff';
this.ctx.fillRect(0, 0, this.canvasWidth, this.canvasHeight);
// 绘制边框
this.ctx.strokeStyle = '#e0e0e0';
this.ctx.lineWidth = 2;
this.ctx.strokeRect(0, 0, this.canvasWidth, this.canvasHeight);
// 绘制提示文字
this.ctx.fillStyle = '#999';
this.ctx.font = '16px Arial';
// this.ctx.fillText('请在此区域签字', 20, 30);
// 重新绘制所有已保存的路径
this.redrawPaths();
// 显示
this.ctx.draw();
},
// 重新绘制所有路径
redrawPaths() {
if (!this.ctx || this.paths.length === 0) return;
this.ctx.strokeStyle = this.strokeColor;
this.ctx.lineWidth = this.lineWidth;
this.ctx.lineCap = 'round';
this.ctx.lineJoin = 'round';
this.paths.forEach(path => {
if (path.length < 2) return;
this.ctx.beginPath();
this.ctx.moveTo(path[0].x, path[0].y);
for (let i = 1; i < path.length; i++) {
this.ctx.lineTo(path[i].x, path[i].y);
}
this.ctx.stroke();
});
},
// 开始绘制
startDrawing(e) {
this.isDrawing = true;
this.hasSignature = true;
const touch = e.touches[0];
const x = touch.x;
const y = touch.y;
// 开始新的路径
this.paths.push([{ x, y }]);
this.lastX = x;
this.lastY = y;
},
// 绘制
draw(e) {
if (!this.isDrawing) return;
const touch = e.touches[0];
const x = touch.x;
const y = touch.y;
// 添加点到当前路径
this.paths[this.paths.length - 1].push({ x, y });
// 重新绘制整个画布
this.drawBackground();
this.lastX = x;
this.lastY = y;
},
// 停止绘制
stopDrawing() {
this.isDrawing = false;
},
// 清除签字
clearSignature() {
this.paths = [];
this.hasSignature = false;
this.drawBackground();
this.$emit('clear');
},
// 确认签字
confirmSignature() {
if (!this.hasSignature) {
uni.showToast({
title: '请先进行签字',
icon: 'none'
});
return;
}
this.getSignatureData().then(dataUrl => {
this.$emit('confirm', {
dataUrl: dataUrl,
hasSignature: this.hasSignature
});
}).catch(error => {
console.error('获取签字数据失败:', error);
uni.showToast({
title: '获取签字数据失败',
icon: 'none'
});
});
},
// 获取签字数据
getSignatureData() {
return new Promise((resolve, reject) => {
uni.canvasToTempFilePath({
canvasId: 'signature-canvas',
success: (res) => {
// 在H5环境下,直接使用tempFilePath
// #ifdef H5
const dataUrl = res.tempFilePath;
resolve(dataUrl);
// #endif
// 在微信小程序环境下,需要读取文件
// #ifdef MP-WEIXIN
uni.getFileSystemManager().readFile({
filePath: res.tempFilePath,
encoding: 'base64',
success: (fileRes) => {
const dataUrl = `data:image/png;base64,${fileRes.data}`;
resolve(dataUrl);
},
fail: reject
});
// #endif
},
fail: reject
}, this);
});
},
// 设置签字数据(用于编辑模式)
setSignatureData(dataUrl) {
if (!dataUrl || !this.ctx) return;
// 如果有签字数据,设置hasSignature为true
this.hasSignature = true;
// 这里可以添加从dataUrl恢复签字的逻辑
// 暂时简化处理
}
}
}
</script>
<style scoped>
.signature-container {
background: #fff;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
border: 3rpx solid #c8e6c9;
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.signature-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 16px;
background: linear-gradient(135deg, #e8f5e9 0%, #b2dfdb 100%);
border-bottom: 1px solid #c8e6c9;
}
.signature-title {
font-size: 16px;
font-weight: 500;
color: #2e7d32;
}
.signature-actions {
display: flex;
gap: 8px;
}
.btn-clear, .btn-confirm {
padding: 6px 12px;
border: none;
border-radius: 4px;
font-size: 14px;
cursor: pointer;
transition: all 0.2s;
}
.btn-clear {
background: #f9fff9;
color: #6a9c6a;
border: 1px solid #c8e6c9;
}
.btn-clear:hover {
background: #e8f5e9;
}
.btn-confirm {
background: linear-gradient(120deg, #43e97b 0%, #38f9d7 100%);
color: #fff;
}
.btn-confirm:hover {
background: linear-gradient(120deg, #38d96b 0%, #2ee5c7 100%);
}
.signature-pad {
/* padding: 16px; */
display: flex;
justify-content: center;
flex: 1;
box-sizing: border-box;
width: 100%;
height: 100%;
}
.signature-canvas {
/* border: 1px solid #c8e6c9; */
border-radius: 4px;
background: #fff;
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
}
.signature-tips {
padding: 8px 16px;
background: #f9fff9;
text-align: center;
border-top: 1px solid #c8e6c9;
}
.tips-text {
font-size: 12px;
color: #6a9c6a;
}
</style>