Files
grungnet/src/components/map/MapGrid.js

62 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-05-31 16:25:05 +10:00
import React, { useContext, useEffect, useState } from "react";
import { Line, Group } from "react-konva";
import useImage from "use-image";
import MapInteractionContext from "../../contexts/MapInteractionContext";
import useDataSource from "../../helpers/useDataSource";
import { mapSources as defaultMapSources } from "../../maps";
import { getStrokeWidth } from "../../helpers/drawing";
2020-07-13 17:44:22 +10:00
import { getImageLightness } from "../../helpers/image";
2020-05-31 16:25:05 +10:00
function MapGrid({ map, gridSize }) {
const mapSource = useDataSource(map, defaultMapSources);
const [mapImage, mapLoadingStatus] = useImage(mapSource);
const gridX = map && map.gridX;
const gridY = map && map.gridY;
const { mapWidth, mapHeight } = useContext(MapInteractionContext);
const lineSpacingX = mapWidth / gridX;
const lineSpacingY = mapHeight / gridY;
const [isImageLight, setIsImageLight] = useState(true);
// When the map changes find the average lightness of its pixels
useEffect(() => {
if (mapLoadingStatus === "loaded") {
2020-07-13 17:44:22 +10:00
setIsImageLight(getImageLightness(mapImage));
2020-05-31 16:25:05 +10:00
}
}, [mapImage, mapLoadingStatus]);
const lines = [];
for (let x = 1; x < gridX; x++) {
lines.push(
<Line
key={`grid_x_${x}`}
points={[x * lineSpacingX, 0, x * lineSpacingX, mapHeight]}
stroke={isImageLight ? "black" : "white"}
strokeWidth={getStrokeWidth(0.1, gridSize, mapWidth, mapHeight)}
2020-06-28 15:44:53 +10:00
opacity={0.5}
2020-05-31 16:25:05 +10:00
/>
);
}
for (let y = 1; y < gridY; y++) {
lines.push(
<Line
key={`grid_y_${y}`}
points={[0, y * lineSpacingY, mapWidth, y * lineSpacingY]}
stroke={isImageLight ? "black" : "white"}
strokeWidth={getStrokeWidth(0.1, gridSize, mapWidth, mapHeight)}
2020-06-28 15:44:53 +10:00
opacity={0.5}
2020-05-31 16:25:05 +10:00
/>
);
}
return <Group>{lines}</Group>;
}
export default MapGrid;