-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
229 lines (200 loc) · 8.38 KB
/
index.html
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
<html>
<head>
<title>Signature Pad</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g=" crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script type="text/javascript">
var canvas, context;
var paint, drawingAreaX, drawingAreaY, drawingAreaWidth, drawingAreaHeight;
var clickX = [],
clickY = [],
clickDrag = [];
var curSize = "normal";
var radius;
$(document).ready(function() {
canvas = document.createElement("canvas");
canvas.width = $(window).width();
canvas.height = $(window).height() / 2;
canvas.id = "canvas";
context = canvas.getContext('2d');
$('#divCanvas').append(canvas);
createEvents();
drawingAreaWidth = canvas.width;
drawingAreaHeight = canvas.height;
drawBorder();
});
var drawBorder = function() {
context.beginPath();
context.rect(0, 0, canvas.width, canvas.height);
context.closePath();
context.fillStyle = '#333333';
context.fill();
context.beginPath();
context.rect(2, 2, canvas.width - 4, canvas.height - 4);
context.closePath();
context.fillStyle = 'white';
context.fill();
};
var redraw = function() {
clearCanvas();
// Keep the drawing in the drawing area
context.save();
context.beginPath();
context.rect(0, 0, drawingAreaWidth, drawingAreaHeight);
context.clip();
// For each point drawn
for (i = 0; i < clickX.length; i += 1) {
// Set the drawing radius
/*switch (clickSize[i]) {
case "small":
radius = 2;
break;
case "normal":
radius = 5;
break;
case "large":
radius = 10;
break;
case "huge":
radius = 20;
break;
default:
break;
}*/
radius = 2;
// Set the drawing path
context.beginPath();
// If dragging then draw a line between the two points
if (clickDrag[i] && i) {
context.moveTo(clickX[i - 1], clickY[i - 1]);
} else {
// The x position is moved over one pixel so a circle even if not dragging
context.moveTo(clickX[i] - 1, clickY[i]);
}
context.lineTo(clickX[i], clickY[i]);
// Set the drawing color
/*if (clickTool[i] === "eraser") {
//context.globalCompositeOperation = "destination-out"; // To erase instead of draw over with white
context.strokeStyle = 'white';
} else {
//context.globalCompositeOperation = "source-over"; // To erase instead of draw over with white
context.strokeStyle = clickColor[i];
}*/
context.strokeStyle = "black";
context.lineCap = "round";
context.lineJoin = "round";
context.lineWidth = radius;
context.stroke();
}
context.closePath();
//context.globalCompositeOperation = "source-over";// To erase instead of draw over with white
context.restore();
context.globalAlpha = 1; // No IE support
// Draw the outline image
//context.drawImage(outlineImage, drawingAreaX, drawingAreaY, drawingAreaWidth, drawingAreaHeight);
};
var clearCanvas = function() {
context.clearRect(0, 0, canvas.width, canvas.height);
drawBorder();
};
var resetSignature = function() {
clearCanvas();
clearArray(clickDrag);
clearArray(clickX);
clearArray(clickY);
$("#divSignature").empty();
};
var clearArray = function(arr) {
while (arr.length > 0) {
arr.pop();
}
};
var addClick = function(x, y, dragging) {
clickX.push(x);
clickY.push(y);
//clickTool.push(curTool);
//clickColor.push(curColor);
//clickSize.push(curSize);
clickDrag.push(dragging);
};
var createEvents = function() {
var press = function(e) {
mouseX = e.pageX; //- this.offsetLeft,
mouseY = e.pageY; // - this.offsetTop;
paint = true;
addClick(mouseX, mouseY, false);
redraw();
},
drag = function(e) {
if (paint) {
addClick(e.pageX, e.pageY, true);
redraw();
}
// Prevent the whole page from dragging if on mobile
//e.preventDefault();
//alert("dragged");
},
release = function() {
paint = false;
redraw();
},
cancel = function() {
paint = false;
},
touchPress = function(e) {
press(e.targetTouches[0]);
},
touchDrag = function(e) {
drag(e.targetTouches[0]);
};
// Add mouse event listeners to canvas element
canvas.addEventListener("mousedown", press, false);
canvas.addEventListener("mousemove", drag, false);
canvas.addEventListener("mouseup", release);
canvas.addEventListener("mouseout", cancel, false);
// Add touch event listeners to canvas element
canvas.addEventListener("touchstart", touchPress, false);
canvas.addEventListener("touchmove", touchDrag, false);
canvas.addEventListener("touchend", release, false);
canvas.addEventListener("touchcancel", cancel, false);
};
var printLocData = function() {
console.log("ClickX clickY clickDrag");
var printData = "ClickX clickY clickDrag\n"
for (i = 0; i < clickX.length; i++) {
console.log(clickX[i] + " " + clickY[i] + " " + clickDrag[i]);
printData = printData + clickX[i] + " " + clickY[i] + " " + clickDrag[i] + "\n"
}
$("#divLog").text(printData);
};
var captureData = function() {
var img = document.createElement("img");
img.src = canvas.toDataURL();
$("#divSignature").empty();
$("#divSignature").append(img);
};
</script>
</head>
<body>
<nav class="navbar navbar-default navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Signature Pad</a>
</div>
</div>
</nav>
<div id='divCanvas'></div>
<div>
<input type="submit" value="Clear" onclick="resetSignature()" class="btn btn-primary btn-lg">
<!--<input type="submit" value="Done" onclick="printLocData()" class="btn btn-primary btn-lg">-->
<input type="submit" value="Capture" onclick="captureData()" class="btn btn-default btn-lg">
</div>
<div id="divLog"></div>
<div id="divSignature"></div>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!-- BoldChat Live Chat Button HTML v1.20 (Type=Web,ChatButton=VFIW Chat Button 1,ChatWindow=VFIW Chat Window 1,Website=Customs,Department=Government) -->
<div style="text-align: center; white-space: nowrap;">
</body>
</html>