Repository: excalidraw/excalidraw
File: packages/excalidraw/eraser/index.ts (TypeScript)

Code before:
    this.elementsToErase.clear();
    this.groupsToErase.clear();
  }
}

const eraserTest = (
  pathSegment: LineSegment<GlobalPoint>,
  element: ExcalidrawElement,
  elementsMap: ElementsMap,
): boolean => {
  const lastPoint = pathSegment[1];
  const threshold = 1;

  const bounds = [
    lastPoint[0] - threshold,
    lastPoint[1] - threshold,
    lastPoint[0] + threshold,
    lastPoint[1] + threshold,
  ] as Bounds;

  const elementBounds = getElementBounds(element, elementsMap);

  if (!doBoundsIntersect(bounds, elementBounds)) {
    return false;
  }

  if (
    shouldTestInside(element) &&
    isPointInElement(lastPoint, element, elementsMap)
  ) {
    return true;
  }

  // for freedraw-points, lines and arrows
  if (
    isFreeDrawElement(element) ||
    isArrowElement(element) ||
    (isLineElement(element) && !element.polygon)
  ) {
    const tolerance =
      element.type === "freedraw"
        ? element.strokeWidth * 2.125
        : element.strokeWidth / 2;
    return distanceToElement(element, elementsMap, lastPoint) <= tolerance;
  }

  const boundTextElement = getBoundTextElement(element, elementsMap);

  return (
    intersectElementWithLineSegment(element, elementsMap, pathSegment, 0, true)
      .length > 0 ||

Review comment (suggestion):
This will create false positives if the eraser is moving extremely fast and when this functino is called the two line segment endpoints are far from the item you want to delete. Instead you have to create the bounding box for the path segment for the eraser trail, then do an intersection test against the element bounding box.

Code after:
        }
      }
    }

    return Array.from(this.elementsToErase);
  }

  endPath(): void {
    super.endPath();
    super.clearTrails();
    this.elementsToErase.clear();
    this.groupsToErase.clear();
  }
}

const eraserTest = (
  pathSegment: LineSegment<GlobalPoint>,
  element: ExcalidrawElement,
  elementsMap: ElementsMap,
  zoom: number,
): boolean => {
  const lastPoint = pathSegment[1];

  // PERF: Do a quick bounds intersection test first because it's cheap
  const threshold = isFreeDrawElement(element) ? 15 : element.strokeWidth / 2;
  const segmentBounds = [
    Math.min(pathSegment[0][0], pathSegment[1][0]) - threshold,
    Math.min(pathSegment[0][1], pathSegment[1][1]) - threshold,
    Math.max(pathSegment[0][0], pathSegment[1][0]) + threshold,
    Math.max(pathSegment[0][1], pathSegment[1][1]) + threshold,
  ] as Bounds;
  const origElementBounds = getElementBounds(element, elementsMap);
  const elementBounds: Bounds = [
    origElementBounds[0] - threshold,
    origElementBounds[1] - threshold,
    origElementBounds[2] + threshold,
    origElementBounds[3] + threshold,
  ];

  if (!doBoundsIntersect(segmentBounds, elementBounds)) {
    return false;
  }

  // There are shapes where the inner area should trigger erasing
  // even though the eraser path segment doesn't intersect with or
  // get close to the shape's stroke
  if (
    shouldTestInside(element) &&
    isPointInElement(lastPoint, element, elementsMap)
  ) {
    return true;