-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhiteboard_safari.js
52 lines (45 loc) · 1.38 KB
/
whiteboard_safari.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
const canvas = document.getElementById("whiteboard");
const ctx = canvas.getContext("2d");
let drawing = false;
const getCoordinates = (e) => {
if (e.touches || e.changedTouches) {
const touch = (e.touches && e.touches[0]) || e.changedtouches[0];
const rect = canvas.getBoundingClientRect();
return {x : touch.clientX - rect.left, y : touch.clientY - rect.top};
}
return {x : e.offsetX, y: e.offsetY};
};
const startDrawing = (e) => {
drawing = true;
const {x, y} = getCoordinates(e);
ctx.beginPath();
ctx.moveTo(x, y);
}
const draw = (e) => {
if (!drawing) return;
const {x, y} = getCoordinates(e);
ctx.lineTo(x, y);
ctx.strokeStyle = "black"; // Set stroke color
ctx.lineWidth = 1; // Set line width
ctx.stroke();
}
const stopDrawing = () => {
drawing = false;
ctx.closePath();
}
//Mouse Events
canvas.addEventListener("mousedown", startDrawing);
canvas.addEventListener("mousemove", draw);
canvas.addEventListener("mouseup", stopDrawing);
canvas.addEventListener("mouseout", stopDrawing);
//Touch Events
canvas.addEventListener("touchstart", (e) => {
e.preventDefault(); // prevent scrolling
startDrawing(e);
});
canvas.addEventListener("touchmove", (e) => {
e.preventDefault();
draw(e);
});
canvas.addEventListener("touchend", stopDrawing);
canvas.addEventListener("touchcancel", stopDrawing);