-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.js
87 lines (81 loc) · 2.87 KB
/
draw.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
// canavas board -> left point kitna aage hai
let boardLeft = canvasBoard.getBoundingClientRect().left;
let boardTop = canvasBoard.getBoundingClientRect().top;
let iX, iY, fX, fY;
let drawingMode = false;
//undo redo
// let undo = document.querySelector("#undo");
// let redo = document.querySelector("#redo");
// let undoRedoTracker = []; //records actions performed on canvas
// let track = 0; //redo=track++, undo=track--
body.addEventListener("mousedown", function (e) {
iX = e.clientX - boardLeft
iY = e.clientY - boardTop
if (cTool == "pencil" || cTool == "eraser") {
drawingMode = true;
tool.beginPath();
tool.moveTo(iX, iY);
}
})
// undo.addEventListener("click", (e) => {
// if(track > 0) track--; //invalid if we reach -1 index on array
// // //track action
// let trackObj = {
// trackValue: track,
// undoRedoTracker
// }
// undoRedoCanvas(trackObj);
// })
// redo.addEventListener("click", (e) => {
// if(track < undoRedoTracker.length-1) track++; //invalid, exceeds array length
// // //track action
// let trackObj = {
// trackValue: track,
// undoRedoTracker
// }
// undoRedoCanvas(trackObj);
// })
// function undoRedoCanvas(trackObj){
// track = trackObj.trackValue;
// undoRedoTracker = trackObj.undoRedoTracker;
// let url = undoRedoTracker[track];
// let img = new Image(); //new image reference element
// img.src = url;
// img.onload = (e) => {
// tool.drawImage(img, 0, 0, canvasBoard.width, canvasBoard.height);
// }
// }
body.addEventListener("mouseup", function (e) {
if (cTool == "pencil" || cTool == "eraser") {
drawingMode = false;
} else if (cTool == "rect" || cTool == "line") {
// rect, line
fX = e.clientX - boardLeft;;
fY = e.clientY - boardTop;
let width = fX - iX;
let height = fY - iY;
if (cTool == "rect") {
tool.strokeRect(iX, iY, width, height);
} else if (cTool == "line") {
tool.beginPath();
tool.moveTo(iX, iY);
tool.lineTo(fX, fY);
tool.stroke();
console.log("line tool is pending")
}
}
//undo redo
// let url = canvasBoard.toDataURL();
// undoRedoTracker.push(url);
// track = undoRedoTracker.length-1;
})
body.addEventListener("mousemove", function (e) {
if (drawingMode == false)
return;
fX = e.clientX - boardLeft;
fY = e.clientY - boardTop;
tool.lineTo(fX, fY);
tool.stroke();
iX = fX;
iY = fY;
})