-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
112 lines (90 loc) · 2.01 KB
/
script.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
let canvas = document.querySelector("#canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight - 100;
window.addEventListener("resize", function () {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight - 100;
drawLinesFromDB();
});
// a context object which provides fun for 2d drawing
let ctx = canvas.getContext("2d");
let linesDB = [];
let redoLinesDB = [];
let isPenDown = false;
let line = [];
canvas.addEventListener("mousedown", function (e) {
if (redoLinesDB.length) {
redoLinesDB = [];
}
isPenDown = true;
let x = e.clientX;
let y = e.clientY - 100;
ctx.beginPath();
ctx.moveTo(x, y);
let pointObject = {
x: x,
y: y,
type: "md",
lineWidth: ctx.lineWidth,
strokeStyle: ctx.strokeStyle,
};
line.push(pointObject);
});
canvas.addEventListener("touchstart", function (e) {
if (redoLinesDB.length) {
redoLinesDB = [];
}
isPenDown = true;
let x = e.touches[0].clientX;
let y = e.touches[0].clientY-100;
ctx.beginPath();
ctx.moveTo(x, y);
let pointObject = {
x: x,
y: y,
type: "md",
lineWidth: ctx.lineWidth,
strokeStyle: ctx.strokeStyle,
};
line.push(pointObject);
});
canvas.addEventListener("mousemove", function (e) {
if (isPenDown) {
let x = e.clientX;
let y = e.clientY - 100;
ctx.lineTo(x, y);
ctx.stroke();
let pointObject = {
x: x,
y: y,
type: "mm",
};
line.push(pointObject);
}
});
canvas.addEventListener("touchmove", function (e) {
if (isPenDown) {
var x = e.touches[0].clientX;
var y = e.touches[0].clientY-100;
ctx.lineTo(x, y);
ctx.stroke();
let pointObject = {
x: x,
y: y,
type: "mm",
};
line.push(pointObject);
}
});
canvas.addEventListener("mouseup", function (e) {
isPenDown = false;
linesDB.push(line);
line = [];
console.log(linesDB);
});
canvas.addEventListener("touchend", function (e) {
isPenDown = false;
linesDB.push(line);
line = [];
//console.log(linesDB);
});