-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmspaint.js
76 lines (65 loc) · 1.91 KB
/
mspaint.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
var mspaint = {
sketchSelector: '',
paintSelector: '',
paintContext: null,
currentIcon: null,
start: function(selector1, selector2) {
this.sketchSelector = selector1;
this.paintSelector = selector2;
var canvas = document.querySelector(this.paintSelector);
this.paintContext = canvas.getContext('2d');
var sketch = document.querySelector(this.sketchSelector);
var sketch_style = getComputedStyle(sketch);
canvas.width = parseInt(sketch_style.getPropertyValue('width'));
canvas.height = parseInt(sketch_style.getPropertyValue('height'));
var mouse = {
x: 0, y: 0,
getX: function() {
return this.x - 65
},
getY: function() {
return this.y - 55
}
};
/* Drawing on Paint App */
this.setLineWidth(5);
this.setLineCap('round');
this.setLineJoin('round');
this.setColor('black');
/* Mouse Capturing Work */
var machine = this;
canvas.addEventListener('mousemove', function(e) {
mouse.x = e.pageX - this.offsetLeft;
mouse.y = e.pageY - this.offsetTop;
}, false);
canvas.addEventListener('mousedown', function(e) {
machine.paintContext.beginPath();
machine.paintContext.moveTo(mouse.getX(), mouse.getY());
//initial dot
mouse.x += 0.1;
mouse.y += -0.1;
onPaint();
canvas.addEventListener('mousemove', onPaint, false);
}, false);
canvas.addEventListener('mouseup', function() {
canvas.removeEventListener('mousemove', onPaint, false);
}, false);
var onPaint = function() {
machine.paintContext.lineTo(mouse.getX(), mouse.getY());
machine.paintContext.stroke();
};
},
setLineWidth: function(lineWidth) {
this.paintContext.lineWidth = lineWidth;
},
setLineCap: function(lineCap) {
this.paintContext.lineCap = lineCap;
},
setLineJoin: function(lineJoin) {
this.paintContext.lineJoin= lineJoin;
},
setColor: function(color) {
this.currentIcon.style.background = '#' + color;
this.paintContext.strokeStyle = '#' + color;
},
}