-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheSignature.js
162 lines (132 loc) · 3.91 KB
/
eSignature.js
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
// stroke weight
var penSize = 3;
// drawing vectors
var present;
var past;
// drawing boolean
var isPressed = false;
// check if canvas is clear
var isClear = true;
var p5_1 = new p5(function(sketch) {
sketch.setup = function() {
var canv = sketch.createCanvas(sketch.windowWidth / 4, sketch.windowHeight / 5);
canv.parent('#eSignatureCanvas');
sketch.background(255);
sketch.border();
};
sketch.draw = function() {
// while pen is pressed
if (isPressed == true) {
// store previous location
past = present;
// store new location
present = sketch.createVector(sketch.mouseX, sketch.mouseY);
if (past) {
// new line connecting new and previous location
sketch.line(past.x, past.y, present.x, present.y);
}
// lock scrolling
$('body').bind('mousewheel touchmove', lockScroll);
// when screen release clear past & present
} else {
// reset
present = null;
past = null;
// enable scrolling
$('body').unbind('mousewheel touchmove', lockScroll);
}
};
// lock window scrolling
function lockScroll(e) {
e.preventDefault();
}
sketch.border = function() {
var spacing = 2;
// top left corner
var tl = {
x: 0,
y: 0
}
// bottom left corner
var bl = {
x: 0,
y: sketch.height - spacing
}
// top right corner
var tr = {
x: sketch.width - spacing,
y: 0
}
// bottom right corner
var br = {
x: sketch.width - spacing,
y: sketch.height - spacing
}
// border stroke weight
sketch.strokeWeight(1);
// draw border
sketch.line(tl.x, tl.y, tr.x, tr.y);
sketch.line(tl.x, tl.y, bl.x, bl.y);
sketch.line(tr.x, tr.y, br.x, br.y);
sketch.line(bl.x, bl.y, br.x, br.y);
// pen size
sketch.strokeWeight(penSize);
};
// event on screen touch
sketch.touchStarted = function() {
if (
sketch.mouseX > 0 &&
sketch.mouseX < sketch.width &&
sketch.mouseY > 0 &&
sketch.mouseY < sketch.height
) {
isPressed = true;
}
};
// event on screen release
sketch.touchEnded = function() {
isPressed = false;
if (
sketch.mouseX > 0 &&
sketch.mouseX < sketch.width &&
sketch.mouseY > 0 &&
sketch.mouseY < sketch.height
) {
isClear = false;
}
}
// generate random string of length
sketch.randomString = function(len) {
var alphabet = "abcdefghijklmnopqrstuvwxyz";
var ret = "";
for (var i = 0; i < len; i++) {
ret += alphabet[sketch.floor(sketch.random(0, alphabet.length))];
}
return ret;
}
// clear image background
sketch.clearEvent = function() {
sketch.background(255);
sketch.border();
isClear = true;
}
});
// save image to server
function saveEvent() {
if (isClear === true) {
alert("Signature is not signed");
} else {
var canvas = $('canvas')[0];
var data = canvas.toDataURL('image/png').replace(/data:image\/png;base64,/, '');
var iname = 'signature_' + randomString(5) + '.png';
$.ajax({
type: "POST",
url: "php-src/upload.php",
data: {
iname: iname,
data: data
}
}).done(function(e) {
});
}
}