All files / src/components/world/hooks useRectVisibleInWorldViewport.ts

0% Statements 0/21
0% Branches 0/10
0% Functions 0/2
0% Lines 0/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                                                                                                             
import { useAppSelector } from "store/hooks";
 
const VIEW_MARGIN = 400;
 
interface UseRectVisibleInWorldViewportArgs {
  x: number;
  y: number;
  width: number;
  height: number;
}
 
export const useRectVisibleInWorldViewport = ({
  x,
  y,
  width,
  height,
}: UseRectVisibleInWorldViewportArgs) => {
  return useAppSelector((state) => {
    Iif (width <= 0 || height <= 0) {
      return false;
    }
 
    const zoomRatio = state.editor.zoom / 100;
 
    Iif (zoomRatio <= 0) {
      return false;
    }
 
    const worldScrollX = state.editor.worldScrollX;
    const worldScrollY = state.editor.worldScrollY;
    const worldViewWidth = state.editor.worldViewWidth;
    const worldViewHeight = state.editor.worldViewHeight;
    const sidebarWidth = state.editor.worldSidebarWidth;
    const navigatorWidth = state.project.present.settings.showNavigator
      ? state.editor.navigatorSidebarWidth
      : 0;
 
    const viewBoundsX = (worldScrollX - VIEW_MARGIN) / zoomRatio;
    const viewBoundsY = (worldScrollY - VIEW_MARGIN) / zoomRatio;
 
    const viewBoundsWidth =
      (worldViewWidth - sidebarWidth - navigatorWidth + VIEW_MARGIN * 2) /
      zoomRatio;
 
    const viewBoundsHeight = (worldViewHeight + VIEW_MARGIN * 2) / zoomRatio;
 
    return (
      x + width > viewBoundsX &&
      x < viewBoundsX + viewBoundsWidth &&
      y + height > viewBoundsY &&
      y < viewBoundsY + viewBoundsHeight
    );
  });
};