-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsign.js
132 lines (114 loc) · 4.54 KB
/
sign.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
/*
* Signature Plugin
* HTML5 Canvas Jquery plugin
*
* Examples and documentation at: http://tiendasdigitales.net
*
* Copyright (c) 2018 Lucas Gabriel Martinez
*
* Version: 1.0.0 - 2018/07/24
* Requires: jQuery v1.3+
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
jQuery(document).ready(function(e) {
jQuery.fn.sign = function(options) {
var params = jQuery.fn.extend({
reset: options.resetButton ? options.resetButton : null,
width: options.width ? options.width : 500,
height: options.height ? options.height : 300,
lineWidth: options.lineWidth ? options.lineWidth : 10,
}, options);
var canvas = jQuery(this);
var lineWidth = params.lineWidth;
var context = canvas.get(0).getContext('2d');
context.lineJoin = context.lineCap = 'round';
var fixFingerPosition = 15;
canvas.attr("width",params.width);
canvas.attr("height", params.height);
var points = [];
var last = {x:null,y:null};
var holdClick = false;
var touch = function(e)
{
var touch = null;
if (e.type !== 'click' && e.type !== 'mousedown' && e.type !== 'mousemove') {
touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
} else {
touch = e;
}
return ({x: touch.pageX, y: touch.pageY});
}
var getMousePosition = function(canvas, evt)
{
var rect = canvas.get(0).getBoundingClientRect();
var pos = touch(evt);
return {
x: pos.x + rect.left - fixFingerPosition,
y: pos.y - rect.top - fixFingerPosition
};
}
var draw = function(ctx, x, y)
{
points.push({x: x, y: y, break: false});
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
var p1 = points[0];
var p2 = points[1];
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
for (var i = 1; i < points.length; i++) {
var midPoint = calculateMiddlePoint(p1, p2);
if (p1.break) {
ctx.moveTo(p2.x, p2.y);
} else {
ctx.quadraticCurveTo(p1.x, p1.y, midPoint.x, midPoint.y);
}
p1 = points[i];
p2 = points[i+1];
}
ctx.lineWidth = lineWidth;
ctx.lineTo(p1.x, p1.y);
ctx.stroke();
}
var calculateMiddlePoint = function(pointStart, pointEnd)
{
return {
x: pointStart.x + (pointEnd.x - pointStart.x) / 2 ,
y: pointStart.y + (pointEnd.y - pointStart.y) / 2
}
}
// Mouse & touch events
canvas.on('touchstart mousedown', function(e) {
holdClick = true;
var mousePosition = getMousePosition(canvas, e);
points.push({x: mousePosition.x, y: mousePosition.y, break: false});
return false;
}).on('touchmove mousemove', function(e)
{
if (holdClick) {
var mousePosition = getMousePosition(canvas, e);
draw(context, mousePosition.x, mousePosition.y);
}
return false;
}).on('touchend mouseup', function(e) {
e.preventDefault();
holdClick = false;
points[points.length - 1].break = true;
return false;
});
// Reset canvas
var reset = function()
{
context.clearRect(0, 0, canvas.width(), canvas.height());
points.length = 0;
}
if (params.reset !== null) {
params.reset.on('click touchend', function()
{
reset();
});
}
};
});