-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinput.js
77 lines (64 loc) · 2.3 KB
/
input.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
function drawEvents(tool) {
document.addEventListener("mousedown", event => {
if(event.clientX < cBounds.x || event.clientX > cBounds.x + canvas.width || event.clientY < cBounds.y || event.clientY > cBounds.y + canvas.height) return;
if(event.button == 0){
tool.startDraw = true;
tool.isDrawing = true;
tool.endDrawing=false;
}
});
document.addEventListener("mousemove", event => {
tool.x = event.clientX - cBounds.x;
tool.y = event.clientY - cBounds.y;
});
document.addEventListener("mouseup", event => {
tool.endDrawing = true;
});
canvas.oncontextmenu = function (e) {
e.preventDefault();
};
}
// Tools handler
function toolsEvent(tool) {
document.getElementById("brushSize").oninput = function (event) {
tool.stroke = event.target.value;
document.getElementById("brushSizeValue").innerHTML = event.target.value;
};
document.getElementById("brushColor").oninput = function (event) {
tool.color = event.target.value
};
}
function programEvents(program){
document.getElementById("clear").addEventListener("click", function () {
program.clearCanvas = true;
});
document.getElementById("download").addEventListener("click", function () {
program.downloadCanvas();
});
document.getElementById("brush").addEventListener("click", function () {
program.selectTool("brush");
selectToolUI("brush");
});
document.getElementById("pen").addEventListener("click", function () {
program.selectTool("pen");
selectToolUI("pen");
});
document.getElementById("eraser").addEventListener("click", function () {
program.selectTool("eraser");
selectToolUI("eraser");
});
document.getElementById("square").addEventListener("click", function () {
program.selectTool("square");
selectToolUI("square");
});
document.getElementById("squareFill").addEventListener("click", function () {
program.selectTool("squareFill");
selectToolUI("squareFill");
});
}
function selectToolUI(newTool){
tools.forEach(tool => {
document.getElementById(tool).classList.remove("selected");
});
document.getElementById(newTool).classList.add("selected");
}