code: function rectIntersect(r1, r2) {
      var right1 = r1.x + r1.width;
      var bottom1 = r1.y + r1.height;
      var right2 = r2.x + r2.width;
      var bottom2 = r2.y + r2.height;
  
      var x = Math.max(r1.x, r2.x);
      var y = Math.max(r1.y, r2.y);
      var w = Math.max(Math.min(right1, right2) - x, 0);
      var h = Math.max(Math.min(bottom1, bottom2) - y, 0);
      return {x: x, y: y, width: w, height: h};
  }
docs: Calculates the intersecting rectangle of two input rectangles.