Just starting out with coding and working on a game for a school project. The idea is to have random circles or "targets" appear on the screen, and the user has to click them. I've been struggling with keeping the "un-clicked" circles on the canvas when a new circle is clicked. Looking for some guidance on how to achieve this. Any simple tips would be greatly appreciated!
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var radiustracker = [];
var xpos = [];
var ypos = [];
var radius;
var x;
var y;
var color = 'blue';
ctx.fillStyle = 'lightblue';
ctx.fillRect(0, 0, canvas.width, canvas.height);
function randomize() {
radius = Math.floor(Math.random() * 25) + 10;
x = Math.floor(Math.random() * 600) + 50;
y = Math.floor(Math.random() * 400) + 50;
radiustracker.push(radius);
xpos.push(x);
ypos.push(y);
drawCircle(x, y, radius);
}
function drawCircle(x, y, radius) {
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
}
function clickCircle(xmus, ymus) {
for (var i = 0; i < xpos.length; i++) {
var distance =
Math.sqrt(
((xmus - xpos[i]) * (xmus - xpos[i])) +
((ymus - ypos[i]) * (ymus - ypos[i]))
);
console.log(distance);
if (distance < radiustracker[i]) {
radiustracker[i] = 0;
ctx.fillStyle = 'lightblue';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
}
}
var intervall = setInterval(randomize, 1000);
canvas.addEventListener('click', (event) => {
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
clickCircle(x, y);
});
#canvas {
display:inline;
margins:auto;
}
<body>
<canvas id="canvas" height="500" width="700" ></canvas>
</body>