-
Notifications
You must be signed in to change notification settings - Fork 171
/
pencil-handler.js
69 lines (51 loc) · 1.96 KB
/
pencil-handler.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
var pencilHandler = {
ismousedown: false,
prevX: 0,
prevY: 0,
mousedown: function(e) {
var x = e.pageX - canvas.offsetLeft,
y = e.pageY - canvas.offsetTop;
var t = this;
t.prevX = x;
t.prevY = y;
t.ismousedown = true;
// make sure that pencil is drawing shapes even
// if mouse is down but mouse isn't moving
tempContext.lineCap = 'round';
pencilDrawHelper.pencil(tempContext, [t.prevX, t.prevY, x, y]);
points[points.length] = ['pencil', [t.prevX, t.prevY, x, y], pencilDrawHelper.getOptions(), 'start'];
t.prevX = x;
t.prevY = y;
},
mouseup: function(e) {
var x = e.pageX - canvas.offsetLeft,
y = e.pageY - canvas.offsetTop;
var t = this;
if (t.ismousedown) {
tempContext.lineCap = 'round';
pencilDrawHelper.pencil(tempContext, [t.prevX, t.prevY, x, y]);
points[points.length] = ['pencil', [t.prevX, t.prevY, x, y], pencilDrawHelper.getOptions(), 'end'];
t.prevX = x;
t.prevY = y;
}
this.ismousedown = false;
},
mousemove: function(e) {
var x = e.pageX - canvas.offsetLeft,
y = e.pageY - canvas.offsetTop;
var t = this;
if (t.ismousedown) {
tempContext.lineCap = 'round';
pencilDrawHelper.pencil(tempContext, [t.prevX, t.prevY, x, y]);
points[points.length] = ['pencil', [t.prevX, t.prevY, x, y], pencilDrawHelper.getOptions()];
t.prevX = x;
t.prevY = y;
}
}
}
var pencilLineWidth = document.getElementById('pencil-stroke-style').value,
pencilStrokeStyle = '#' + document.getElementById('pencil-fill-style').value;
var pencilDrawHelper = clone(drawHelper);
pencilDrawHelper.getOptions = function() {
return [pencilLineWidth, pencilStrokeStyle, fillStyle, globalAlpha, globalCompositeOperation, lineCap, lineJoin, font];
}