Repository: excalidraw/excalidraw
File: src/components/App.tsx (TypeScript)

Code before:
      ) {
        setCursor(this.canvas, CURSOR_TYPE.MOVE);
      } else {
        setCursor(this.canvas, CURSOR_TYPE.AUTO);
      }
    }
  };

  private handleEraser = (
    event: PointerEvent,
    pointerDownState: PointerDownState,
    scenePointer: { x: number; y: number },
  ) => {
    const idsToUpdate: Array<string> = [];

    const distance = distance2d(
      pointerDownState.lastCoords.x,
      pointerDownState.lastCoords.y,
      scenePointer.x,
      scenePointer.y,
    );
    const threshold = 10 / this.state.zoom.value;
    const point = pointerDownState.lastCoords;
    let samplingInterval = 0;
    while (samplingInterval < distance) {
      const hitElement = this.getElementAtPosition(point.x, point.y);
      if (hitElement) {
        idsToUpdate.push(hitElement.id);
        if (event.altKey) {
          if (pointerDownState.elementIdsToErase[hitElement.id]) {
            pointerDownState.elementIdsToErase[hitElement.id] = false;
          }
        } else {
          pointerDownState.elementIdsToErase[hitElement.id] = true;
        }
      }

      // Calculate next point in the line at a distance of sampling interval
      samplingInterval += threshold;

      const distanceRatio = samplingInterval / distance;
      const nextX =
        (1 - distanceRatio) * point.x + distanceRatio * scenePointer.x;
      const nextY =
        (1 - distanceRatio) * point.y + distanceRatio * scenePointer.y;
      point.x = nextX;
      point.y = nextY;
    }

    const hitElement = this.getElementAtPosition(
      scenePointer.x,

Review comment (suggestion):
let's use all elements under cursor (`getElementsAtPosition()`), not just the top-most. This will fix two major cases:

1) elements covering each other

![eraser_zindex](https://user-images.githubusercontent.com/5153846/157744134-6e4127ff-49f5-4e1b-bbb0-9133b2487ad7.gif)

2) elements (lines etc.) very close to each other but not covering each other, especially when moving the cursor fast so that there aren't many samples which to hitbox
 

Code after:
        !event[KEYS.CTRL_OR_CMD] &&
        (hitElement ||
          this.isHittingCommonBoundingBoxOfSelectedElements(
            scenePointer,
            selectedElements,
          ))
      ) {
        setCursor(this.canvas, CURSOR_TYPE.MOVE);
      } else {
        setCursor(this.canvas, CURSOR_TYPE.AUTO);
      }
    }
  };

  private handleEraser = (
    event: PointerEvent,
    pointerDownState: PointerDownState,
    scenePointer: { x: number; y: number },
  ) => {
    const updateElementIds = (elements: ExcalidrawElement[]) => {
      elements.forEach((element) => {
        idsToUpdate.push(element.id);
        if (event.altKey) {
          if (pointerDownState.elementIdsToErase[element.id]) {
            pointerDownState.elementIdsToErase[element.id] = false;
          }
        } else {
          pointerDownState.elementIdsToErase[element.id] = true;
        }
      });
    };

    const idsToUpdate: Array<string> = [];

    const distance = distance2d(
      pointerDownState.lastCoords.x,
      pointerDownState.lastCoords.y,
      scenePointer.x,
      scenePointer.y,
    );
    const threshold = 10 / this.state.zoom.value;
    const point = { ...pointerDownState.lastCoords };
    let samplingInterval = 0;
    while (samplingInterval <= distance) {
      const hitElements = this.getElementsAtPosition(point.x, point.y);
      updateElementIds(hitElements);

      // Exit since we reached current point
      if (samplingInterval === distance) {
        break;
      }