signature.js
7.25 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
/**
* @param {*} params {}
* /**
* whiteBord 白板盒子canvas id名
* popverBox 确认签字以后想要隐藏掉的盒子
* againWrite 重写按钮id名
* sureWrite 确定按钮id名
* mobileOri 移动端方向
* canvasWidth 画布宽
* canvasHeight 画布高
* writeColor 轨迹颜色
* clearWriteCallback 重写后的回调函数
* sureWriteCallback 确定签名之后的回调函数
* @returns 签名的图片:格式base64
*/
let mobileOri = null;
let canvasWidth = null;
let canvasHeight = null;
let whiteBord = null;
let writeColor = "#000";
let hascontent = false;
let whiteBoardDom = null;
let clientWidth = document.documentElement.clientWidth;
let points = [];
export function signature(params) {
console.log("signature");
let {
againWrite,
sureWrite,
clearWriteCallback,
sureWriteCallback,
} = params;
whiteBord = params.whiteBord;
whiteBoardDom = document.getElementById(whiteBord);
let againWriteBtn = document.getElementById(againWrite);
let sureWriteBtn = document.getElementById(sureWrite);
let devicePixelRatio = window.devicePixelRatio;
console.log(whiteBoardDom);
canvasWidth = whiteBoardDom.offsetWidth;
canvasHeight = whiteBoardDom.offsetHeight;
let writeCanvas;
let imageBase64='';
againWriteBtn.addEventListener("click", function () {
drawClear();
})
sureWriteBtn.addEventListener('click', function () {
savePict();
})
function WriteFont() {
let obj = {
canvas: whiteBoardDom,
context: whiteBoardDom.getContext("2d"),
isWrite: false, //是否开始
lastPoint: {}, //上次点
writeWidth: 4, //初始轨迹宽度
lastWriteTime: 0, /* 上次写入时间 */
oldW: 4, /* 上次写入宽度 */
}
/**
* 轨迹宽度
*/
this.setLineWidth = function () {
obj.context.lineWidth = obj.writeWidth;
}
/**
* 绘制轨迹
*/
this.writing = function (point) {
points.push(point);
obj.context.beginPath();
if(mobileOri == "notlandscape") { /* 竖屏 */
obj.context.moveTo(obj.lastPoint.y, clientWidth - obj.lastPoint.x);
obj.context.lineTo(point.y, clientWidth - point.x);
}else if(mobileOri == "landscape") { /* 横屏 */
obj.context.moveTo(obj.lastPoint.x, obj.lastPoint.y);
obj.context.lineTo(point.x, point.y );
}else { /* pc端 */
let x = (points[points.length - 2].x + points[points.length - 1].x) / 2;
let y = (points[points.length - 2].y + points[points.length - 1].y) / 2;
if(points.length == 2) {
obj.context.moveTo(points[points.length - 2].x, points[points.length - 2].y);
obj.context.lineTo(x, y);
}else {
let lastX = (points[points.length - 3].x + points[points.length - 2].x) / 2;
let lastY = (points[points.length - 3].y + points[points.length - 2].y) / 2;
obj.context.moveTo(lastX, lastY);
obj.context.quadraticCurveTo(points[points.length - 2].x, points[points.length - 2].y, x, y); /* 贝塞尔曲线实现平滑效果 */
}
// obj.context.moveTo(obj.lastPoint.x, obj.lastPoint.y);
// obj.context.lineTo(point.x, point.y);
}
// 动态计算画笔宽度
let pow = Math.pow(point.x - obj.lastPoint.x, 2) + Math.pow(point.y - obj.lastPoint.y, 2)
let d = Math.sqrt(pow);
let nowTime = new Date().getTime();
let v = d / (nowTime - obj.lastWriteTime);
obj.lastWriteTime = nowTime;
let w = 2 / v;
let offset = 0.5;
if(w - obj.oldW > offset) {
w = obj.oldW + offset;
}
if(obj.oldW - w > offset) {
w = obj.oldW - offset;
}
if (w < 2) {
w = 2
}
if(w > 8) {
w = 8
}
obj.writeWidth = w;
obj.oldW = w;
this.setLineWidth();
obj.context.stroke();
points.slice(0, 1);
obj.lastPoint = point;
obj.context.closePath();
hascontent = true;
}
/**
* 轨迹样式
*/
this.writeContextStyle = function () {
obj.context.beginPath();
obj.context.strokeStyle = writeColor;
obj.context.lineCap = 'round';
obj.context.lineJoin = "round";
}
/**
* 写开始
*/
this.writeBegin = function (point) {
obj.isWrite = true;
obj.lastWriteTime = new Date().getTime();
obj.lastPoint = point;
points.push(point);
this.writeContextStyle();
}
/**
* 写结束
*/
this.writeEnd = function () {
obj.isWrite = false;
points = [];
}
/**
* 清空画板
*/
this.canvasClear = function () {
obj.context.save();
obj.context.strokeStyle = '#fff';
obj.context.clearRect(0, 0, canvasWidth, canvasHeight);
obj.context.restore();
}
/**
* 初始化画板
*/
this.canvasInit = function () {
whiteBoardDom.width = canvasWidth;
whiteBoardDom.height = canvasHeight;
}
let that = this
/**======================事件绑定===========================**/
// 移动端
whiteBoardDom.addEventListener('touchstart', function (e) {
// console.log("touchstart");
let point = {
x: e.touches[0].pageX,
y: e.touches[0].pageY
};
that.writeBegin(point);
that.writing(point);
}, {passive: false});
whiteBoardDom.addEventListener('touchmove', function (e) {
e.preventDefault();
if (obj.isWrite) {
let point = {
x: e.touches[0].pageX,
y: e.touches[0].pageY
};
that.writing(point);
}
}, {passive: false});
document.addEventListener('touchend', function (e) {
that.writeEnd();
}, {passive: false});
// pc端
whiteBoardDom.addEventListener('mousedown', function (e) {
let point = {
x: e.offsetX,
y: e.offsetY,
};
that.writeBegin(point);
that.writing(point);
}, {passive: false});
whiteBoardDom.addEventListener('mousemove', function (e) {
if (obj.isWrite) {
let point = {
x: e.offsetX,
y: e.offsetY
};
that.writing(point);
}
}, {passive: false});
document.addEventListener('mouseup', function (e) {
that.writeEnd();
}, {passive: false});
this.canvasInit();
this.canvasClear();
obj.control = {
clearCanvas: that.canvasClear
};
this.option = obj;
}
/**
* 初始化调用
* 设置参数
*/
writeCanvas = new WriteFont();
function savePict() {
if(hascontent) {
imageBase64 = whiteBoardDom.toDataURL("image/png"); //base64
writeCanvas.option.control.clearCanvas();
sureWriteCallback(imageBase64)
}else {
sureWriteCallback("empty")
}
hascontent = false;
}
function drawClear() {
writeCanvas.option.control.clearCanvas();
hascontent = false;
clearWriteCallback && clearWriteCallback();
}
}
export function changeOri(event, color) {
if(event == "changeColor") {
writeColor = color;
}else {
mobileOri = event;
clientWidth = document.documentElement.clientWidth;
canvasWidth = whiteBoardDom.offsetWidth;
canvasHeight = whiteBoardDom.offsetHeight;
// 重设画布宽高
whiteBoardDom.width = canvasWidth;
whiteBoardDom.height = canvasHeight;
// // 消除锯齿
// let width = canvasWidth,
// height = canvasHeight;
// let whiteBoardDom = document.getElementById(whiteBord);
// if (devicePixelRatio) {
// console.log("devicePixelRatio", devicePixelRatio);
// whiteBoardDom.style.width = width + "px";
// whiteBoardDom.style.height = height + "px";
// whiteBoardDom.height = height * devicePixelRatio;
// whiteBoardDom.width = width * devicePixelRatio;
// obj.context.scale(devicePixelRatio, devicePixelRatio);
// }
hascontent = false;
return canvasWidth + "-" + canvasHeight
}
}
export default {
signature,
changeOri,
}