All files / src/components/world IsoGridOverlay.tsx

100% Statements 17/17
100% Branches 0/0
100% Functions 1/1
100% Lines 15/15

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 56 57 58 59 60 61 62 63 64 65 66 671x 1x                                         1x   1x 1x   1x 1x 1x 2x 2x 2x 2x                       1x                                   1x   1x  
import React, { memo } from "react";
import {
  isoToScreen,
  isoDiamondPoints,
  isoOriginX,
  isoOriginY,
} from "shared/lib/entities/isoUtils";
 
interface IsoGridOverlayProps {
  /** Scene width in grid tiles. */
  mapWidth: number;
  /** Scene height in grid tiles. */
  mapHeight: number;
  /** Full rendered scene size, including any background letterboxing. */
  canvasWidth: number;
  canvasHeight: number;
}
 
/**
 * SVG overlay that draws the isometric diamond grid for a scene in the editor.
 * Rendered as a transparent overlay on top of the background image.
 */
const IsoGridOverlay = memo(
  ({ mapWidth, mapHeight, canvasWidth, canvasHeight }: IsoGridOverlayProps) => {
    const originX = isoOriginX(mapWidth, mapHeight, canvasWidth);
    const originY = isoOriginY(mapWidth, mapHeight, canvasHeight);
 
    const polygons: JSX.Element[] = [];
    for (let ty = 0; ty < mapHeight; ty++) {
      for (let tx = 0; tx < mapWidth; tx++) {
        const { x, y } = isoToScreen(tx, ty);
        const sx = originX + x;
        const sy = originY + y;
        polygons.push(
          <polygon
            key={`${tx}-${ty}`}
            points={isoDiamondPoints(sx, sy)}
            fill="none"
            stroke="rgba(100, 180, 255, 0.35)"
            strokeWidth={0.5}
          />,
        );
      }
    }
 
    return (
      <svg
        style={{
          position: "absolute",
          top: 0,
          left: 0,
          width: canvasWidth,
          height: canvasHeight,
          pointerEvents: "none",
          overflow: "visible",
        }}
      >
        {polygons}
      </svg>
    );
  },
);
 
IsoGridOverlay.displayName = "IsoGridOverlay";
 
export default IsoGridOverlay;