-
Notifications
You must be signed in to change notification settings - Fork 2
/
tools.js
64 lines (56 loc) · 1.7 KB
/
tools.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
let undo = document.querySelector("#undo");
let redo = document.querySelector("#redo");
undo.addEventListener("click", undoLine);
redo.addEventListener("click", redoLine);
function undoLine() {
if (linesDB.length) {
let undoLine = linesDB.pop();
redoLinesDB.push(undoLine);
// clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawLinesFromDB();
}
}
function redoLine() {
if (redoLinesDB.length) {
let currentLineWidth = ctx.lineWidth;
let currentStrokeStyle = ctx.strokeStyle;
let redoLine = redoLinesDB.pop();
for (let i = 0; i < redoLine.length; i++) {
let pointObject = redoLine[i];
if (pointObject.type == "md") {
ctx.lineWidth = pointObject.lineWidth;
ctx.strokeStyle = pointObject.strokeStyle;
ctx.beginPath();
ctx.moveTo(pointObject.x, pointObject.y);
} else {
ctx.lineTo(pointObject.x, pointObject.y);
ctx.stroke();
}
}
linesDB.push(redoLine);
ctx.lineWidth = currentLineWidth;
ctx.strokeStyle = currentStrokeStyle;
}
}
function drawLinesFromDB() {
let currentLineWidth = ctx.lineWidth;
let currentStrokeStyle = ctx.strokeStyle;
for (let i = 0; i < linesDB.length; i++) {
let line = linesDB[i];
for (let i = 0; i < line.length; i++) {
let pointObject = line[i];
if (pointObject.type == "md") {
ctx.lineWidth = pointObject.lineWidth;
ctx.strokeStyle = pointObject.strokeStyle;
ctx.beginPath();
ctx.moveTo(pointObject.x, pointObject.y);
} else {
ctx.lineTo(pointObject.x, pointObject.y);
ctx.stroke();
}
}
}
ctx.lineWidth = currentLineWidth;
ctx.strokeStyle = currentStrokeStyle;
}