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

105 lines
2.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, strokeWidth }) {
2020-10-09 12:57:48 +11:00
const { mapWidth, mapHeight } = useContext(MapInteractionContext);
let mapSourceMap = map;
// Use lowest resolution for grid lightness
if (map && map.type === "file" && map.resolutions) {
const resolutionArray = Object.keys(map.resolutions);
if (resolutionArray.length > 0) {
mapSourceMap = map.resolutions[resolutionArray[0]];
}
}
const mapSource = useDataSource(mapSourceMap, defaultMapSources);
2020-05-31 16:25:05 +10:00
const [mapImage, mapLoadingStatus] = useImage(mapSource);
2020-10-09 12:57:48 +11:00
const [isImageLight, setIsImageLight] = useState(true);
// When the map changes find the average lightness of its pixels
useEffect(() => {
if (mapLoadingStatus === "loaded") {
setIsImageLight(getImageLightness(mapImage));
}
}, [mapImage, mapLoadingStatus]);
const gridX = map && map.grid.size.x;
const gridY = map && map.grid.size.y;
2020-10-09 12:57:48 +11:00
if (!gridX || !gridY) {
return null;
}
const gridSizeNormalized = {
2020-10-09 12:57:48 +11:00
x: 1 / gridX,
y: 1 / gridY,
};
2020-10-02 18:47:20 +10:00
const gridInset = map && map.grid.inset;
2020-05-31 16:25:05 +10:00
2020-10-02 18:47:20 +10:00
const insetWidth = (gridInset.bottomRight.x - gridInset.topLeft.x) * mapWidth;
const insetHeight =
(gridInset.bottomRight.y - gridInset.topLeft.y) * mapHeight;
const lineSpacingX = insetWidth / gridX;
const lineSpacingY = insetHeight / gridY;
const offsetX = gridInset.topLeft.x * mapWidth * -1;
const offsetY = gridInset.topLeft.y * mapHeight * -1;
2020-05-31 16:25:05 +10:00
const lines = [];
for (let x = 1; x < gridX; x++) {
lines.push(
<Line
key={`grid_x_${x}`}
2020-10-02 18:47:20 +10:00
points={[x * lineSpacingX, 0, x * lineSpacingX, insetHeight]}
2020-05-31 16:25:05 +10:00
stroke={isImageLight ? "black" : "white"}
strokeWidth={getStrokeWidth(
strokeWidth,
gridSizeNormalized,
mapWidth,
mapHeight
)}
2020-06-28 15:44:53 +10:00
opacity={0.5}
2020-10-02 18:47:20 +10:00
offsetX={offsetX}
offsetY={offsetY}
2020-05-31 16:25:05 +10:00
/>
);
}
for (let y = 1; y < gridY; y++) {
lines.push(
<Line
key={`grid_y_${y}`}
2020-10-02 18:47:20 +10:00
points={[0, y * lineSpacingY, insetWidth, y * lineSpacingY]}
2020-05-31 16:25:05 +10:00
stroke={isImageLight ? "black" : "white"}
strokeWidth={getStrokeWidth(
strokeWidth,
gridSizeNormalized,
mapWidth,
mapHeight
)}
2020-06-28 15:44:53 +10:00
opacity={0.5}
2020-10-02 18:47:20 +10:00
offsetX={offsetX}
offsetY={offsetY}
2020-05-31 16:25:05 +10:00
/>
);
}
return <Group>{lines}</Group>;
}
MapGrid.defaultProps = {
strokeWidth: 0.1,
};
2020-05-31 16:25:05 +10:00
export default MapGrid;