PaulMartrenchar commited on
Commit
55cdcd7
·
1 Parent(s): d564cda

Improve the canvas

Browse files
Files changed (1) hide show
  1. index.html +65 -55
index.html CHANGED
@@ -17,43 +17,53 @@
17
  background-color: lightblue;
18
  }
19
 
20
- #canvas {
 
21
  width: 100%;
22
  height: 100%;
23
- display: flex;
24
- flex-direction: column;
25
- justify-content: center;
26
- align-items: center;
27
- position: relative;
28
- background-color: lightblue;
29
  }
30
 
31
- h1, p {
32
- color: white;
33
- text-align: center;
 
34
  }
35
 
36
- .circle {
37
  position: absolute;
38
- width: 50px;
39
- height: 50px;
40
- background-color: white;
41
- border-radius: 50%;
42
  transform: translate(-50%, -50%);
 
 
43
  pointer-events: none;
44
  }
45
  </style>
46
  </head>
47
  <body>
48
- <div id="canvas">
49
- <h1>Randomizer</h1>
50
- <p>put your finger to decide</p>
 
 
 
51
  </div>
52
  <script>
53
  document.addEventListener('DOMContentLoaded', () => {
54
- const canvas = document.getElementById('canvas');
 
55
  let circles = [];
56
  let timer = null;
 
 
 
 
 
 
 
 
 
 
57
 
58
  function resetTimer() {
59
  if (timer) {
@@ -68,31 +78,43 @@
68
  const winnerIndex = Math.floor(Math.random() * circles.length);
69
  const winnerCircle = circles[winnerIndex];
70
 
71
- // Remove all circles
 
 
 
 
 
 
 
 
 
72
  circles.forEach(circle => {
73
- if (circle.element && circle.element.parentNode) {
74
- circle.element.parentNode.removeChild(circle.element);
75
- }
 
 
76
  });
77
 
78
- // Change the winner's circle to green
79
- if (winnerCircle.element) {
80
- winnerCircle.element.style.backgroundColor = 'green';
81
- canvas.appendChild(winnerCircle.element);
82
- }
83
 
84
- circles = [winnerCircle];
 
 
85
  }
86
 
87
- function createCircle(x, y, id) {
88
- const circle = document.createElement('div');
89
- circle.className = 'circle';
90
- circle.style.left = `${x}px`;
91
- circle.style.top = `${y}px`;
92
- circle.id = `circle-${id}`;
93
- canvas.appendChild(circle);
94
 
95
- return circle;
 
 
96
  }
97
 
98
  canvas.addEventListener('touchstart', (event) => {
@@ -100,10 +122,8 @@
100
  const touches = event.touches;
101
  for (let i = 0; i < touches.length; i++) {
102
  const touch = touches[i];
103
- const circle = createCircle(touch.clientX, touch.clientY, touch.identifier);
104
- circles.push({ id: touch.identifier, element: circle, x: touch.clientX, y: touch.clientY });
105
  }
106
- resetTimer();
107
  });
108
 
109
  canvas.addEventListener('touchmove', (event) => {
@@ -111,13 +131,7 @@
111
  const touches = event.touches;
112
  for (let i = 0; i < touches.length; i++) {
113
  const touch = touches[i];
114
- const circleIndex = circles.findIndex(c => c.id === touch.identifier);
115
- if (circleIndex !== -1) {
116
- circles[circleIndex].x = touch.clientX;
117
- circles[circleIndex].y = touch.clientY;
118
- circles[circleIndex].element.style.left = `${touch.clientX}px`;
119
- circles[circleIndex].element.style.top = `${touch.clientY}px`;
120
- }
121
  }
122
  });
123
 
@@ -126,16 +140,12 @@
126
  const touches = event.changedTouches;
127
  for (let i = 0; i < touches.length; i++) {
128
  const touch = touches[i];
129
- const circleIndex = circles.findIndex(c => c.id === touch.identifier);
130
- if (circleIndex !== -1) {
131
- if (circles[circleIndex].element && circles[circleIndex].element.parentNode) {
132
- circles[circleIndex].element.parentNode.removeChild(circles[circleIndex].element);
133
- }
134
- circles.splice(circleIndex, 1);
135
- }
136
  }
137
- resetTimer();
138
  });
 
 
 
139
  });
140
  </script>
141
  </body>
 
17
  background-color: lightblue;
18
  }
19
 
20
+ #canvas-container {
21
+ position: relative;
22
  width: 100%;
23
  height: 100%;
 
 
 
 
 
 
24
  }
25
 
26
+ #drawing-canvas {
27
+ width: 100%;
28
+ height: 100%;
29
+ background-color: lightblue;
30
  }
31
 
32
+ #text-container {
33
  position: absolute;
34
+ top: 50%;
35
+ left: 50%;
 
 
36
  transform: translate(-50%, -50%);
37
+ text-align: center;
38
+ color: white;
39
  pointer-events: none;
40
  }
41
  </style>
42
  </head>
43
  <body>
44
+ <div id="canvas-container">
45
+ <canvas id="drawing-canvas"></canvas>
46
+ <div id="text-container">
47
+ <h1>Randomizer</h1>
48
+ <p>put your finger to decide</p>
49
+ </div>
50
  </div>
51
  <script>
52
  document.addEventListener('DOMContentLoaded', () => {
53
+ const canvas = document.getElementById('drawing-canvas');
54
+ const ctx = canvas.getContext('2d');
55
  let circles = [];
56
  let timer = null;
57
+ let animationId = null;
58
+
59
+ // Set canvas dimensions to full window size
60
+ function resizeCanvas() {
61
+ canvas.width = window.innerWidth;
62
+ canvas.height = window.innerHeight;
63
+ }
64
+
65
+ window.addEventListener('resize', resizeCanvas);
66
+ resizeCanvas();
67
 
68
  function resetTimer() {
69
  if (timer) {
 
78
  const winnerIndex = Math.floor(Math.random() * circles.length);
79
  const winnerCircle = circles[winnerIndex];
80
 
81
+ // Clear all circles
82
+ circles = [winnerCircle];
83
+ winnerCircle.color = 'green';
84
+ }
85
+
86
+ function draw() {
87
+ // Clear the canvas
88
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
89
+
90
+ // Draw all circles
91
  circles.forEach(circle => {
92
+ ctx.beginPath();
93
+ ctx.arc(circle.x, circle.y, 50, 0, 2 * Math.PI); // Increased radius to 50
94
+ ctx.fillStyle = circle.color || 'white';
95
+ ctx.fill();
96
+ ctx.closePath();
97
  });
98
 
99
+ animationId = requestAnimationFrame(draw);
100
+ }
 
 
 
101
 
102
+ function addCircle(x, y, id) {
103
+ circles.push({ id, x, y, color: 'white' });
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) => {
 
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) => {
 
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
 
 
140
  const touches = event.changedTouches;
141
  for (let i = 0; i < touches.length; i++) {
142
  const touch = touches[i];
143
+ removeCircle(touch.identifier);
 
 
 
 
 
 
144
  }
 
145
  });
146
+
147
+ // Start the drawing loop
148
+ draw();
149
  });
150
  </script>
151
  </body>