-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.js
121 lines (93 loc) · 2.47 KB
/
canvas.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
var myCanvas = ( id => {
// Get the canvas element from html
var canvas = document.getElementById(id);
// Set context to use properties
var context = canvas.getContext("2d");
// set canvas width and height to window size
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Initialize mouse pointer and brush size
var down = false;
var bs = 15;
// e is the event passed
var drawPoint = e => {
// If the mouse button is pressed
if(down == true) {
// Get actual x and y values
var x;
var y;
if (e.pageX || e.pageY) {
x = e.pageX;
y = e.pageY;
}
else {
x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
x -= canvas.offsetLeft;
y -= canvas.offsetTop;
//Draw a line to this point from whereever the canvas point was previously
context.lineTo(x, y);
context.lineWidth = bs*2;
context.stroke();
// Create a point
context.beginPath();
context.arc(x, y, bs, 0, 2*Math.PI);
// Fill the point
context.fill();
// Send the canvas context to the new point
context.beginPath();
context.moveTo(x, y);
}
}
var mouseup = e => {
// If mouse was clicked
if(down == true) {
// Unclick it
down=false;
// Refresh the context path
context.beginPath();
}
}
var mousedown = e => {
// If mouse was not clicked
if(down == false) {
// Click it
down = true;
// Draw the point
drawPoint(e);
}
}
// Set listeners
canvas.addEventListener('mousedown',mousedown);
canvas.addEventListener('mousemove',drawPoint);
// Mouse up from entire document should unclick
document.addEventListener('mouseup',mouseup);
// Function to set brush size
var setBrush = size => {
bs = size;
}
// Function to set color
var setcolor = color => {
context.globalCompositeOperation = "source-over"
context.fillStyle = color;
context.strokeStyle = color;
}
// Function to clear canvas
var clrcan = () => {
context.clearRect(0, 0, canvas.width, canvas.height);
}
var erase = () => {
context.globalCompositeOperation = "destination-out";
context.strokeStyle = "rgba(0,0,0,0)";
}
// Subscribe to canvas events that anyone can order
pubsub.on('clearCanvas', clrcan);
pubsub.on('changeBrush', setBrush);
pubsub.on('setColor', setcolor);
pubsub.on('erase', erase);
// Export the canvas object to access from outside
return {
canvas: canvas
}
})("myCanvas");