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

Code before:
      // 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,
      scenePointer.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;
      }
    }

    const elements = this.scene.getElements().map((ele) => {
      const id =
        isBoundToContainer(ele) && idsToUpdate.includes(ele.containerId)
          ? ele.containerId
          : ele.id;
      if (idsToUpdate.includes(id)) {
        if (event.altKey) {
          if (pointerDownState.elementIdsToErase[id] === false) {
            return newElementWith(ele, {
              opacity: this.state.currentItemOpacity,
            });
          }
        } else {
          return newElementWith(ele, { opacity: 20 });
        }
      }
      return ele;
    });

    this.scene.replaceAllElements(elements);

    pointerDownState.lastCoords.x = scenePointer.x;
    pointerDownState.lastCoords.y = scenePointer.y;
  };

Review comment (suggestion):
let's factor this into a function (even in the same scope so it can share context variables) and reuse for both here and the loop

Code after:
      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;
      }

      // Calculate next point in the line at a distance of sampling interval
      samplingInterval = Math.min(samplingInterval + threshold, distance);

      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 elements = this.scene.getElements().map((ele) => {
      const id =
        isBoundToContainer(ele) && idsToUpdate.includes(ele.containerId)
          ? ele.containerId
          : ele.id;
      if (idsToUpdate.includes(id)) {
        if (event.altKey) {
          if (pointerDownState.elementIdsToErase[id] === false) {
            return newElementWith(ele, {
              opacity: this.state.currentItemOpacity,
            });
          }
        } else {
          return newElementWith(ele, { opacity: 20 });
        }
      }
      return ele;
    });

    this.scene.replaceAllElements(elements);

    pointerDownState.lastCoords.x = scenePointer.x;
    pointerDownState.lastCoords.y = scenePointer.y;
  };