-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.js
129 lines (111 loc) · 3.22 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
122
123
124
125
126
127
128
129
let canvas = document.querySelector("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let pencilColor = document.querySelectorAll(".pencil-color");
let pencilWidthElem = document.querySelector(".pencil-width");
let eraserWidthElem = document.querySelector(".eraser-width");
let download = document.querySelector(".download");
let redo = document.querySelector(".redo");
let undo = document.querySelector(".undo");
let penColor = "red";
let eraserColor = "white";
let penWidth = pencilWidthElem.value;
let eraserWidth = eraserWidthElem.value;
let undoRedoTracker = []; //Data
let track = 0; // Represent which action from tracker array
let mouseDown = false;
// API
let tool = canvas.getContext("2d");
tool.strokeStyle = penColor;
tool.lineWidth = penWidth;
// mousedown -> start new path, mousemove -> path fill (graphics)
canvas.addEventListener("mousedown", (e) => {
mouseDown = true;
beginPath({
x:e.clientX,
y:e.clientY
})
})
canvas.addEventListener("mousemove", (e) => {
if(mouseDown) drawStroke({
x:e.clientX,
y:e.clientY,
color: eraserFlag ? eraserColor : penColor,
width: eraserFlag ? eraserWidth : penWidth
})
})
canvas.addEventListener("mouseup", (e) => {
mouseDown = false;
let url = canvas.toDataURL();
undoRedoTracker.push(url);
track = undoRedoTracker.length-1;
})
undo.addEventListener("click", (e) => {
if (track > 0) track--;
// track action
let trackObj = {
trackValue: track,
undoRedoTracker
}
undoRedoCanvas(trackObj);
})
redo.addEventListener("click", (e) => {
if (track < undoRedoTracker.length-1) track++;
// 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, canvas.width, canvas.height);
}
}
function beginPath(strokeObj) {
tool.beginPath();
tool.moveTo(strokeObj.x, strokeObj.y);
}
function drawStroke(strokeObj) {
tool.strokeStyle = strokeObj.color;
tool.lineWidth = strokeObj.width;
tool.lineTo(strokeObj.x, strokeObj.y);
tool.stroke();
}
pencilColor.forEach((colorElem) => {
colorElem.addEventListener("click", (e) => {
let color = colorElem.classList[0];
penColor = color;
tool.strokeStyle = penColor;
})
})
pencilWidthElem.addEventListener("change", (e) => {
penWidth = pencilWidthElem.value;
tool.lineWidth = penWidth;
})
eraserWidthElem.addEventListener("change", (e) => {
eraserWidth = eraserWidthElem.value;
tool.lineWidth = eraserWidth;
})
eraser.addEventListener("click", (e) => {
if (eraserFlag) {
tool.strokeStyle = eraserColor;
tool.lineWidth = eraserWidth;
} else {
tool.strokeStyle = penColor;
tool.lineWidth = penWidth;
}
})
download.addEventListener("click", (e) => {
let url = canvas.toDataURL();
let a = document.createElement("a");
a.href = url;
a.download = "board.jpg";
a.click();
})