Sergidev commited on
Commit
0670eb7
·
verified ·
1 Parent(s): 4302572

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +44 -46
static/script.js CHANGED
@@ -1,5 +1,7 @@
1
- const canvas = document.getElementById("whiteboard");
2
- const ctx = canvas.getContext("2d");
 
 
3
  const colorPicker = document.getElementById("color-picker");
4
  const sizeSlider = document.getElementById("size-slider");
5
  const clearBtn = document.getElementById("clear-btn");
@@ -7,8 +9,18 @@ const clearBtn = document.getElementById("clear-btn");
7
  let isDrawing = false;
8
  let currentPath = [];
9
 
10
- canvas.width = window.innerWidth;
11
- canvas.height = window.innerHeight - 50;
 
 
 
 
 
 
 
 
 
 
12
 
13
  const ws = new WebSocket(`ws://${window.location.host}/ws`);
14
 
@@ -17,46 +29,46 @@ ws.onmessage = (event) => {
17
  if (message.Update) {
18
  redrawWhiteboard(message.Update);
19
  } else if (message.Clear) {
20
- ctx.clearRect(0, 0, canvas.width, canvas.height);
21
  }
22
  };
23
 
24
  function redrawWhiteboard(actions) {
25
- ctx.clearRect(0, 0, canvas.width, canvas.height);
26
  for (const action of actions) {
27
- drawPath(action);
28
  }
29
  }
30
 
31
- function drawPath(action) {
32
- ctx.beginPath();
33
- ctx.strokeStyle = action.color;
34
- ctx.lineWidth = action.size;
35
- ctx.lineCap = "round";
36
- ctx.lineJoin = "round";
37
  for (let i = 0; i < action.points.length; i++) {
38
  const point = action.points[i];
39
  if (i === 0) {
40
- ctx.moveTo(point.x, point.y);
41
  } else {
42
- ctx.lineTo(point.x, point.y);
43
  }
44
  }
45
- ctx.stroke();
46
  }
47
 
48
- canvas.addEventListener("mousedown", startDrawing);
49
- canvas.addEventListener("mousemove", draw);
50
- canvas.addEventListener("mouseup", stopDrawing);
51
- canvas.addEventListener("mouseout", stopDrawing);
52
 
53
  function startDrawing(e) {
54
  isDrawing = true;
55
  currentPath = [];
56
  const point = getPoint(e);
57
  currentPath.push(point);
58
- ctx.beginPath();
59
- ctx.moveTo(point.x, point.y);
60
  }
61
 
62
  function draw(e) {
@@ -65,27 +77,12 @@ function draw(e) {
65
  const point = getPoint(e);
66
  currentPath.push(point);
67
 
68
- // Clear the canvas and redraw the current path
69
- ctx.clearRect(0, 0, canvas.width, canvas.height);
70
- redrawWhiteboard([]); // Redraw all previous actions
71
- drawCurrentPath();
72
- }
73
-
74
- function drawCurrentPath() {
75
- ctx.beginPath();
76
- ctx.strokeStyle = colorPicker.value;
77
- ctx.lineWidth = sizeSlider.value;
78
- ctx.lineCap = "round";
79
- ctx.lineJoin = "round";
80
- for (let i = 0; i < currentPath.length; i++) {
81
- const point = currentPath[i];
82
- if (i === 0) {
83
- ctx.moveTo(point.x, point.y);
84
- } else {
85
- ctx.lineTo(point.x, point.y);
86
- }
87
- }
88
- ctx.stroke();
89
  }
90
 
91
  function stopDrawing() {
@@ -99,11 +96,13 @@ function stopDrawing() {
99
  };
100
 
101
  ws.send(JSON.stringify({ Draw: action }));
 
 
102
  currentPath = [];
103
  }
104
 
105
  function getPoint(e) {
106
- const rect = canvas.getBoundingClientRect();
107
  return {
108
  x: e.clientX - rect.left,
109
  y: e.clientY - rect.top,
@@ -115,7 +114,6 @@ clearBtn.addEventListener("click", () => {
115
  });
116
 
117
  window.addEventListener("resize", () => {
118
- canvas.width = window.innerWidth;
119
- canvas.height = window.innerHeight - 50;
120
  redrawWhiteboard([]);
121
  });
 
1
+ const backgroundCanvas = document.getElementById("background-canvas");
2
+ const drawingCanvas = document.getElementById("drawing-canvas");
3
+ const bgCtx = backgroundCanvas.getContext("2d");
4
+ const drawCtx = drawingCanvas.getContext("2d");
5
  const colorPicker = document.getElementById("color-picker");
6
  const sizeSlider = document.getElementById("size-slider");
7
  const clearBtn = document.getElementById("clear-btn");
 
9
  let isDrawing = false;
10
  let currentPath = [];
11
 
12
+ function resizeCanvases() {
13
+ const container = document.getElementById("canvas-container");
14
+ const width = container.clientWidth;
15
+ const height = container.clientHeight;
16
+
17
+ backgroundCanvas.width = width;
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
 
 
29
  if (message.Update) {
30
  redrawWhiteboard(message.Update);
31
  } else if (message.Clear) {
32
+ bgCtx.clearRect(0, 0, backgroundCanvas.width, backgroundCanvas.height);
33
  }
34
  };
35
 
36
  function redrawWhiteboard(actions) {
37
+ bgCtx.clearRect(0, 0, backgroundCanvas.width, backgroundCanvas.height);
38
  for (const action of actions) {
39
+ drawPath(bgCtx, action);
40
  }
41
  }
42
 
43
+ function drawPath(context, action) {
44
+ context.beginPath();
45
+ context.strokeStyle = action.color;
46
+ context.lineWidth = action.size;
47
+ context.lineCap = "round";
48
+ context.lineJoin = "round";
49
  for (let i = 0; i < action.points.length; i++) {
50
  const point = action.points[i];
51
  if (i === 0) {
52
+ context.moveTo(point.x, point.y);
53
  } else {
54
+ context.lineTo(point.x, point.y);
55
  }
56
  }
57
+ context.stroke();
58
  }
59
 
60
+ drawingCanvas.addEventListener("mousedown", startDrawing);
61
+ drawingCanvas.addEventListener("mousemove", draw);
62
+ drawingCanvas.addEventListener("mouseup", stopDrawing);
63
+ drawingCanvas.addEventListener("mouseout", stopDrawing);
64
 
65
  function startDrawing(e) {
66
  isDrawing = true;
67
  currentPath = [];
68
  const point = getPoint(e);
69
  currentPath.push(point);
70
+ drawCtx.beginPath();
71
+ drawCtx.moveTo(point.x, point.y);
72
  }
73
 
74
  function draw(e) {
 
77
  const point = getPoint(e);
78
  currentPath.push(point);
79
 
80
+ drawCtx.clearRect(0, 0, drawingCanvas.width, drawingCanvas.height);
81
+ drawPath(drawCtx, {
82
+ color: colorPicker.value,
83
+ size: parseFloat(sizeSlider.value),
84
+ points: currentPath,
85
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  }
87
 
88
  function stopDrawing() {
 
96
  };
97
 
98
  ws.send(JSON.stringify({ Draw: action }));
99
+ drawCtx.clearRect(0, 0, drawingCanvas.width, drawingCanvas.height);
100
+ drawPath(bgCtx, action);
101
  currentPath = [];
102
  }
103
 
104
  function getPoint(e) {
105
+ const rect = drawingCanvas.getBoundingClientRect();
106
  return {
107
  x: e.clientX - rect.left,
108
  y: e.clientY - rect.top,
 
114
  });
115
 
116
  window.addEventListener("resize", () => {
117
+ resizeCanvases();
 
118
  redrawWhiteboard([]);
119
  });