Skip to content
Shubham JangleResources
JavaScript

Explore topics

Overlapping Circles

Watch the walkthrough
Open on Instagram

Exercise

Draw a circle at each click and show how many existing circles overlap it.

What the source implements

The source compares center distance with a 100 px diameter. Touching edges do not count. It mixes viewport client coordinates with absolute positioning, so scrolling can offset placement. Its random color expression can generate an invalid hex color. Align the coordinate system and generate six padded hexadecimal digits in a revised implementation.

Run and revise

Copy the entire fenced HTML into a file with the same name and open it in a browser. It includes the markup, styles, and JavaScript together. Check the console, keyboard behavior, and boundary cases. Remote APIs/assets and CDN scripts need network access.

Original repository source

The complete file below is preserved apart from line endings; the extensions above are not changes to this copied implementation.

10-overlapping-circles.html

Original file on GitHub

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,400;0,700;1,100&display=swap"
      rel="stylesheet"
    />
    <style>
      body {
        color: white;
        background-color: rgb(71, 79, 87);
        font-family: "Poppins", sans-serif;
        font-weight: 400;
        font-style: normal;
      }
      .circle {
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: bold;
        border: 1px solid slategray;
        border-radius: 50px;
        width: 100px;
        height: 100px;
      }
    </style>
  </head>
  <body>
    <h1>Click Anywhere to draw circles</h1>
  </body>
  <script>
    let coords = [];
    const diameter = 100;

    function findOverlaps(x1, y1) {
      let overlaps = 0;
      coords.forEach((item) => {
        const { x, y } = item;

        const distBetweenTwoCircle = Math.sqrt(
          Math.pow(x - x1, 2) + Math.pow(y - y1, 2)
        );

        if (distBetweenTwoCircle < diameter) overlaps++;
      });

      return overlaps;
    }

    function drawCircle(e) {
      const { clientX, clientY } = e;

      const overlaps = findOverlaps(clientX, clientY);
      coords.push({ x: clientX, y: clientY });

      const ele = document.createElement("div");
      ele.innerText = overlaps;
      ele.classList.add("circle");
      ele.style.position = "absolute";
      ele.style.top = `calc(${clientY}px - ${diameter / 2}px)`;
      ele.style.left = `calc(${clientX}px - ${diameter / 2}px)`;
      ele.style.backgroundColor = `#${Math.trunc(Math.random() * 1000000)}`;

      document.body.appendChild(ele);
    }

    document.addEventListener("click", drawCircle);
  </script>
</html>