Sergidev commited on
Commit
85d3c15
·
verified ·
1 Parent(s): 6d45359

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +57 -6
static/script.js CHANGED
@@ -18,9 +18,14 @@ function resizeCanvases() {
18
  backgroundCanvas.height = height;
19
  drawingCanvas.width = width;
20
  drawingCanvas.height = height;
 
 
 
21
  }
22
 
 
23
  resizeCanvases();
 
24
 
25
  const ws = new WebSocket(`ws://${window.location.host}/ws`);
26
 
@@ -62,6 +67,11 @@ drawingCanvas.addEventListener("mousemove", draw);
62
  drawingCanvas.addEventListener("mouseup", stopDrawing);
63
  drawingCanvas.addEventListener("mouseout", stopDrawing);
64
 
 
 
 
 
 
65
  function startDrawing(e) {
66
  isDrawing = true;
67
  currentPath = [];
@@ -103,17 +113,58 @@ function stopDrawing() {
103
 
104
  function getPoint(e) {
105
  const rect = drawingCanvas.getBoundingClientRect();
 
 
106
  return {
107
- x: e.clientX - rect.left,
108
- y: e.clientY - rect.top,
109
  };
110
  }
111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  clearBtn.addEventListener("click", () => {
113
  ws.send(JSON.stringify({ Clear: null }));
114
  });
115
 
116
- window.addEventListener("resize", () => {
117
- resizeCanvases();
118
- redrawWhiteboard([]);
119
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  backgroundCanvas.height = height;
19
  drawingCanvas.width = width;
20
  drawingCanvas.height = height;
21
+
22
+ // Redraw the background canvas after resizing
23
+ redrawWhiteboard([]);
24
  }
25
 
26
+ // Call resizeCanvases initially and add event listener
27
  resizeCanvases();
28
+ window.addEventListener("resize", resizeCanvases);
29
 
30
  const ws = new WebSocket(`ws://${window.location.host}/ws`);
31
 
 
67
  drawingCanvas.addEventListener("mouseup", stopDrawing);
68
  drawingCanvas.addEventListener("mouseout", stopDrawing);
69
 
70
+ // Touch event listeners for mobile support
71
+ drawingCanvas.addEventListener("touchstart", handleTouchStart);
72
+ drawingCanvas.addEventListener("touchmove", handleTouchMove);
73
+ drawingCanvas.addEventListener("touchend", handleTouchEnd);
74
+
75
  function startDrawing(e) {
76
  isDrawing = true;
77
  currentPath = [];
 
113
 
114
  function getPoint(e) {
115
  const rect = drawingCanvas.getBoundingClientRect();
116
+ const x = e.clientX || e.touches[0].clientX;
117
+ const y = e.clientY || e.touches[0].clientY;
118
  return {
119
+ x: x - rect.left,
120
+ y: y - rect.top,
121
  };
122
  }
123
 
124
+ function handleTouchStart(e) {
125
+ e.preventDefault();
126
+ startDrawing(e.touches[0]);
127
+ }
128
+
129
+ function handleTouchMove(e) {
130
+ e.preventDefault();
131
+ draw(e.touches[0]);
132
+ }
133
+
134
+ function handleTouchEnd(e) {
135
+ e.preventDefault();
136
+ stopDrawing();
137
+ }
138
+
139
  clearBtn.addEventListener("click", () => {
140
  ws.send(JSON.stringify({ Clear: null }));
141
  });
142
 
143
+ // Prevent scrolling when touching the canvas
144
+ document.body.addEventListener(
145
+ "touchstart",
146
+ function (e) {
147
+ if (e.target == drawingCanvas) {
148
+ e.preventDefault();
149
+ }
150
+ },
151
+ { passive: false },
152
+ );
153
+ document.body.addEventListener(
154
+ "touchend",
155
+ function (e) {
156
+ if (e.target == drawingCanvas) {
157
+ e.preventDefault();
158
+ }
159
+ },
160
+ { passive: false },
161
+ );
162
+ document.body.addEventListener(
163
+ "touchmove",
164
+ function (e) {
165
+ if (e.target == drawingCanvas) {
166
+ e.preventDefault();
167
+ }
168
+ },
169
+ { passive: false },
170
+ );