How to avoid Overlapping of random numbers? On clicking button click me! , It will generate random numbers but there are some numbers which are overlapping how to avoid this…and how to rotate the
circle I tried to rotate the circle using moz webkit animation but it didn’t work for me.. Please answer How to do that?
const circle = document.querySelector(".circle");
const addNumBtn = document.querySelector('#addNumBtn');
// we set the width of the circle element in JS and pass
// it to CSS using a css variable and the root element
const circleWidth = 650;
document.documentElement.style.setProperty('--circleWidth', `${circleWidth}px`);
// NOTE: '~~' -> is quicker form of writing Math.floor()
// runs faster on most browsers.
function randomPos(min, max) {
return ~~(Math.random() * (max - min + 1) + min)
}
// return a random value between 1 and 50
function randomValue(){
return ~~(Math.random() * 50) + 1;
}
function randomColor() {
let r = ~~(Math.random() * 255) + 1;
let g = ~~(Math.random() * 255) + 1;
let b = ~~(Math.random() * 255) + 1;
return `rgba(${r},${g},${b})`
}
function randomSize() {
let size = ~~(Math.random() * 30) + 8;
return `${size}px`
}
// add a helper function to find the center of the
// circle and calculate distance to the edge of circle
// x and y are the randomly generated points
// cx, cy are center of circle x and y repsectively
// rad is circle radius
function pointInCircle(x, y, cx, cy, rad) {
const distancesquared = (x - cx) * (x - cx) + (y - cy) * (y - cy);
return distancesquared <= rad * rad;
}
// you can remove valArray array if you do not want the 50 numbers to not be repeated
const valArray = [];
function randNum() {
// add a counter
let count = 1;
// while loop to check if the counter has reached the target 50
while (count <= 50) {
// set the randomly generated val
let val = randomValue();
// random size = string with px added
let randSize = randomSize();
// random size = the exact number => typeof number
let posSize = randSize.split('px')[0];
// an array to hold our randomly positioned number values
// uses helper function randonPos
// pass in the 1 and the circle width minus the font size
let ran = [randomPos(1, circleWidth - posSize), randomPos(1, circleWidth - posSize)];
// conditional to check bounds of circle using helper function
// we pass in the ran array, center points and radius
// we add buffers to accommodate for the font size
// you can remove !valArray.includes(val) if you do not want the 50 numbers to not be repeated
if (pointInCircle(ran[0], ran[1], circleWidth/2-posSize, circleWidth/2-posSize, circleWidth / 2 - posSize) && !valArray.includes(val)) {
// you can remove valArray.push(val); if you do not want the 50 numbers to not be repeated
valArray.push(val);
// if the conditional returns true,
// create the element, style and append
let randnum = document.createElement("p");
randnum.classList.add('numStyle');
randnum.style.color = randomColor();
randnum.style.fontSize = randSize;
randnum.textContent = val;
randnum.style.position = 'absolute';
randnum.style.top = `${ran[0]}px`;
randnum.style.left = `${ran[1]}px`;
circle.append(randnum);
// iterate the counter
count++;
}
}
}
addNumBtn.addEventListener('click', randNum)
html {
--circle-width: 300px;
}
.circle {
aspect-ratio: 1;
width: var(--circleWidth);
background-color: black;
border-radius: 50%;
position: relative;
overflow: hidden;
padding: .5rem;
-moz-animation: spinoffPulse 1s infinite linear;
-webkit-animation: spinoffPulse 1s infinite linear;
}
@-moz-keyframes spinoffPulse {
0% { -moz-transform:rotate(0deg); }
100% { -moz-transform:rotate(360deg); }
}
@-webkit-keyframes spinoffPulse {
0% { -webkit-transform:rotate(0deg); }
100% { -webkit-transform:rotate(360deg); }
}
#addNumBtn {
position: absolute;
top: 0;
left: 0;
}
.numStyle {
font-family: "roboto", sans-serif;
}
<div id="container"></div>
<div class="circle">
</div>
<button id="addNumBtn">Click Me!</button>