Here is a little widget that I've made. I'm looking to refactor the way that I'm doing hit detection. Currently, every square on the screen finds the distance between itself and the cursor.Because there are quite a few of these squares, and that they are all being rendered 60 times per second. It's quite inefficient.The squares are all stored in a 2D array where squares2d represents rows, and squares2d[0 -> squares2d.length - 1] represents columns. Because the spacing between the squares is a constant 25px, I'm thinking that there is a better way that I can find what the mouse is close to. Maybe something like spatial partitioning?It performs pretty well, anyways. But for the sake of writing good code, I'm looking for advice.class Square {    constructor(x, y, size, xpos, ypos) {      this.x = x;      this.y = y;      this.size = size;      this.sr = 0;      this.color = '#222222';      this.forward = true;      this.xpos = xpos;      this.ypos = ypos;      this.maxSize = 15;    }    getDistance() {      const xMid = this.x + this.size / 2;      const yMid = this.y + this.size / 2;      return Math.sqrt( (( mouseX - xMid ) ** 2) +                        (( mouseY - yMid ) ** 2) );    }    draw () {      ctx.fillStyle = this.color;      const dist = this.getDistance();      if (dist / 2 < this.maxSize - 5 && mousein) {        this.size = dist / 2;        this.sr = 5; // initial energy to start waves      } else {        /* irrelivant */      }      ctx.fillRect(this.x - this.size / 2, this.y - this.size / 2, this.size, this.size);    }  }    function draw() {    stats.begin();    ctx.fillStyle = '#fb933b';    ctx.fillRect(0, 0, w, h);    if (update) { // resizing and resetting      /* also irrelivant */    }    for (let i = 0; i < squares2d.length; i++) {      for (let j = 0; j < squares2d[i].length; j++) {        squares2d[i][j].findShrinkRate();        squares2d[i][j].draw(); // here's the kicker      }    }    stats.end();  }  // this sets the framerate (60fps);  thread = setInterval(draw, 1000 / 60);
Detect objects that the mouse is close to
javascript;performance;canvas