我是靠谱客的博主 忐忑吐司,这篇文章主要介绍vue手写签名(移动端竖屏),拿来即用,可转为横屏,现在分享给大家,希望可以做个参考。

一、vue手写签名(移动端竖屏)

复制代码
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
<template> <div class="canvas"> <div class="canvas_header">手写签名</div> <div class="canvas_canvas"> <canvas id="myCanvas" width="350" height="400"></canvas> </div> <div class="canvas_info"> <span @click="showColorDiv()" >*请在输入框写入您的姓名</span > </div> <div class="canvas_footer"> <div @click="clearArea()" class="canvas_footer_re">重写</div> <div @click="saveImage()" class="canvas_footer_sa">保存</div> <div @click="saveImageInfo()" class="canvas_footer_su">提交</div> </div> <!-- 颜色选择器 --> <div class="canvas_color" v-show="showColor == 2"> <div> <input v-model="strokeStyle" type="color" /> </div> </div> </div> </template> <script> export default { data() { return { touchPressed: false, ctx: null, strokeStyle: "#000000", //书写颜色 lineWidth: 4, //线条宽度 lastX: null, lastY: null, canvas: null, showColor: 1, //显示颜色 sColor: 0, //显示颜色 }; }, mounted() { this.$nextTick(() => { let canvas = document.getElementById("myCanvas"); this.canvas = canvas; this.ctx = canvas.getContext("2d"); // let winW = window.innerWidth; // let winH = window.innerHeight; // let winW = window.innerWidth; // let winH = window.innerHeight; // canvas.width = winW; // canvas.height = winH; this.Init(); }); }, methods: { Init() { // 移动前 this.canvas.addEventListener( "touchstart", (event) => { if (event.targetTouches.length == 1) { event.preventDefault(); // 阻止浏览器默认事件,重要 var touch = event.targetTouches[0]; this.touchPressed = true; this.draw( touch.pageX - this.canvas.offsetLeft, touch.pageY - this.canvas.offsetTop, false ); } }, false ); // 移动中 this.canvas.addEventListener( "touchmove", (event) => { if (event.targetTouches.length == 1) { event.preventDefault(); // 阻止浏览器默认事件,重要 var touch = event.targetTouches[0]; if (this.touchPressed) { this.draw( touch.pageX - this.canvas.offsetLeft, touch.pageY - this.canvas.offsetTop, true ); } } }, false ); // 移动结束 this.canvas.addEventListener( "touchend", (event) => { if (event.targetTouches.length == 1) { event.preventDefault(); // 阻止浏览器默认事件,防止手写的时候拖动屏幕,重要 this.touchPressed = false; } }, false ); }, draw(x, y, isDown) { let ctx = this.ctx; if (isDown) { ctx.beginPath(); ctx.strokeStyle = this.strokeStyle; ctx.lineWidth = this.lineWidth; ctx.lineJoin = "round"; ctx.moveTo(this.lastX, this.lastY); ctx.lineTo(x, y); ctx.closePath(); ctx.stroke(); } this.lastX = x; this.lastY = y; }, // 重写 clearArea() { this.ctx.setTransform(1, 0, 0, 1, 0, 0); this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height); }, // 保存本地 saveImage() { let a = document.createElement("a"); a.href = this.canvas.toDataURL(); a.download = "sign"; a.click(); //保存 }, // 保存服务器 saveImageInfo() { let b = this.canvas.toDataURL(); console.log(b, "bbb"); }, // 弹出颜色 showColorDiv() { return this.sColor += 1; if (this.sColor == 10) { this.showColor = 2; } }, //签完名的图片旋转处理 // src为你、base64编码;edg为角度,0-360;callback回调 rotateBase64Img(src, edg, callback) { var canvas = document.createElement("canvas"); var ctx = canvas.getContext("2d"); var imgW;//图片宽度 var imgH;//图片高度 var size;//canvas初始大小 if (edg % 90 != 0) { console.error("旋转角度必须是90的倍数!"); throw '旋转角度必须是90的倍数!'; } (edg < 0) && (edg = (edg % 360) + 360) const quadrant = (edg / 90) % 4; //旋转象限 const cutCoor = {sx: 0, sy: 0, ex: 0, ey: 0}; //裁剪坐标 var image = new Image(); image.crossOrigin = "anonymous" image.src = src; image.onload = function () { imgW = image.width; imgH = image.height; size = imgW > imgH ? imgW : imgH; canvas.width = size * 2; canvas.height = size * 2; switch (quadrant) { case 0: cutCoor.sx = size; cutCoor.sy = size; cutCoor.ex = size + imgW; cutCoor.ey = size + imgH; break; case 1: cutCoor.sx = size - imgH; cutCoor.sy = size; cutCoor.ex = size; cutCoor.ey = size + imgW; break; case 2: cutCoor.sx = size - imgW; cutCoor.sy = size - imgH; cutCoor.ex = size; cutCoor.ey = size; break; case 3: cutCoor.sx = size; cutCoor.sy = size - imgW; cutCoor.ex = size + imgH; cutCoor.ey = size + imgW; break; } ctx.translate(size, size); ctx.rotate(edg * Math.PI / 180); ctx.drawImage(image, 0, 0); var imgData = ctx.getImageData(cutCoor.sx, cutCoor.sy, cutCoor.ex, cutCoor.ey); if (quadrant % 2 == 0) { canvas.width = imgW; canvas.height = imgH; } else { canvas.width = imgH; canvas.height = imgW; } ctx.putImageData(imgData, 0, 0); callback(canvas.toDataURL()) }; } //end }, watch: { strokeStyle(newColor,oldColor) { console.log(newColor) }, }, }; </script> <style> .canvas_header { width: 100%; text-align: center; line-height: 50px; font-weight: 800; font-size: 20px; } .canvas_canvas { width: 350px; margin: 0 auto; border: 1px solid #aaa; } .canvas_info { margin: 10px 8%; font-size: 12px; } .canvas_footer { width: 80%; margin: 10px 10%; display: flex; justify-content: space-between; line-height: 40px; text-align: center; color: white; font-weight: 800; } .canvas_footer_re { width: 100px; background: #f11919; border-radius: 10px; } .canvas_footer_sa { width: 100px; background: #1989f1; border-radius: 10px; } .canvas_footer_su { width: 100px; background: #2dcb7a; border-radius: 10px; } </style>

最后

以上就是忐忑吐司最近收集整理的关于vue手写签名(移动端竖屏),拿来即用,可转为横屏的全部内容,更多相关vue手写签名(移动端竖屏)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部