PaulMartrenchar commited on
Commit
cc49c95
·
1 Parent(s): 5a82009

Fix bug of duplicate circles

Browse files
Files changed (1) hide show
  1. index.html +34 -23
index.html CHANGED
@@ -14,6 +14,7 @@
14
  display: flex;
15
  justify-content: center;
16
  align-items: center;
 
17
  }
18
 
19
  #canvas-container {
@@ -25,7 +26,7 @@
25
  #drawing-canvas {
26
  width: 100%;
27
  height: 100%;
28
- z-index:10;
29
  }
30
 
31
  #text-container {
@@ -36,6 +37,7 @@
36
  text-align: center;
37
  color: white;
38
  pointer-events: none;
 
39
  }
40
  </style>
41
  </head>
@@ -51,10 +53,10 @@
51
  document.addEventListener('DOMContentLoaded', () => {
52
  const canvas = document.getElementById('drawing-canvas');
53
  const ctx = canvas.getContext('2d');
54
- const statusText = document.getElementById('status-text');
55
  let circles = [];
56
  let timer = null;
57
  let animationId = null;
 
58
 
59
  // Set canvas dimensions to full window size
60
  function resizeCanvas() {
@@ -80,6 +82,7 @@
80
  // Clear all circles
81
  circles = [winnerCircle];
82
  winnerCircle.color = '#228B22';
 
83
  }
84
 
85
  function draw() {
@@ -90,7 +93,7 @@
90
  // Draw all circles
91
  circles.forEach(circle => {
92
  ctx.beginPath();
93
- ctx.arc(circle.x, circle.y, 120, 0, Math.PI * 2); // Radius of 50
94
  ctx.fillStyle = circle.color || '#fff';
95
  ctx.fill();
96
  ctx.closePath();
@@ -99,44 +102,52 @@
99
  animationId = requestAnimationFrame(draw);
100
  }
101
 
102
- function addCircle(x, y, id) {
103
- circles.push({ id, x, y, color: '#fff' });
104
- resetTimer();
105
- }
 
106
 
107
- function updateCircle(id, x, y) {
108
- const circle = circles.find(c => c.id === id);
109
- if (circle) {
110
- circle.x = x;
111
- circle.y = y;
112
  }
 
 
113
  }
114
 
115
  function removeCircle(id) {
116
  circles = circles.filter(c => c.id !== id);
 
 
 
117
  resetTimer();
118
  }
119
 
120
  canvas.addEventListener('touchstart', (event) => {
121
- event.preventDefault();
122
- const touches = event.touches;
123
- for (let i = 0; i < touches.length; i++) {
124
- const touch = touches[i];
125
- addCircle(touch.clientX, touch.clientY, touch.identifier);
126
  }
 
 
127
  });
128
 
129
  canvas.addEventListener('touchmove', (event) => {
130
- event.preventDefault();
131
- const touches = event.touches;
132
- for (let i = 0; i < touches.length; i++) {
133
- const touch = touches[i];
134
- updateCircle(touch.identifier, touch.clientX, touch.clientY);
135
  }
 
 
136
  });
137
 
138
  canvas.addEventListener('touchend', (event) => {
139
- event.preventDefault();
 
 
 
140
  const touches = event.changedTouches;
141
  for (let i = 0; i < touches.length; i++) {
142
  const touch = touches[i];
 
14
  display: flex;
15
  justify-content: center;
16
  align-items: center;
17
+ background-color: #add8e6; /* Light blue background */
18
  }
19
 
20
  #canvas-container {
 
26
  #drawing-canvas {
27
  width: 100%;
28
  height: 100%;
29
+ z-index: 10;
30
  }
31
 
32
  #text-container {
 
37
  text-align: center;
38
  color: white;
39
  pointer-events: none;
40
+ z-index: 20; /* Ensure text is above the canvas */
41
  }
42
  </style>
43
  </head>
 
53
  document.addEventListener('DOMContentLoaded', () => {
54
  const canvas = document.getElementById('drawing-canvas');
55
  const ctx = canvas.getContext('2d');
 
56
  let circles = [];
57
  let timer = null;
58
  let animationId = null;
59
+ let winnerDecided = false;
60
 
61
  // Set canvas dimensions to full window size
62
  function resizeCanvas() {
 
82
  // Clear all circles
83
  circles = [winnerCircle];
84
  winnerCircle.color = '#228B22';
85
+ winnerDecided = true;
86
  }
87
 
88
  function draw() {
 
93
  // Draw all circles
94
  circles.forEach(circle => {
95
  ctx.beginPath();
96
+ ctx.arc(circle.x, circle.y, 120, 0, Math.PI * 2); // Radius of 120
97
  ctx.fillStyle = circle.color || '#fff';
98
  ctx.fill();
99
  ctx.closePath();
 
102
  animationId = requestAnimationFrame(draw);
103
  }
104
 
105
+ function updateCircles(touches) {
106
+ if (winnerDecided) return;
107
+
108
+ // Clear existing circles
109
+ circles = [];
110
 
111
+ // Add circles for current touches
112
+ for (let i = 0; i < touches.length; i++) {
113
+ const touch = touches[i];
114
+ circles.push({ id: touch.identifier, x: touch.clientX, y: touch.clientY, color: '#fff' });
 
115
  }
116
+
117
+ resetTimer();
118
  }
119
 
120
  function removeCircle(id) {
121
  circles = circles.filter(c => c.id !== id);
122
+ if (circles.length === 0 && winnerDecided) {
123
+ winnerDecided = false;
124
+ }
125
  resetTimer();
126
  }
127
 
128
  canvas.addEventListener('touchstart', (event) => {
129
+ if (winnerDecided) {
130
+ event.preventDefault();
131
+ return;
 
 
132
  }
133
+ const touches = event.touches;
134
+ updateCircles(touches);
135
  });
136
 
137
  canvas.addEventListener('touchmove', (event) => {
138
+ if (winnerDecided) {
139
+ event.preventDefault();
140
+ return;
 
 
141
  }
142
+ const touches = event.touches;
143
+ updateCircles(touches);
144
  });
145
 
146
  canvas.addEventListener('touchend', (event) => {
147
+ if (winnerDecided) {
148
+ event.preventDefault();
149
+ return;
150
+ }
151
  const touches = event.changedTouches;
152
  for (let i = 0; i < touches.length; i++) {
153
  const touch = touches[i];