2021-02-06 13:32:38 +11:00
|
|
|
import React, { useEffect, useState } from "react";
|
2020-05-31 16:25:05 +10:00
|
|
|
import useImage from "use-image";
|
|
|
|
|
|
2021-03-12 17:35:26 +11:00
|
|
|
import { useImageSource } from "../../contexts/ImageSourceContext";
|
|
|
|
|
|
2020-05-31 16:25:05 +10:00
|
|
|
import { mapSources as defaultMapSources } from "../../maps";
|
|
|
|
|
|
2020-07-13 17:44:22 +10:00
|
|
|
import { getImageLightness } from "../../helpers/image";
|
2020-05-31 16:25:05 +10:00
|
|
|
|
2020-10-17 10:45:59 +11:00
|
|
|
import Grid from "../Grid";
|
|
|
|
|
|
2021-02-09 16:58:14 +11:00
|
|
|
function MapGrid({ map }) {
|
2020-10-02 11:15:20 +10:00
|
|
|
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]];
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-12 17:35:26 +11:00
|
|
|
const mapSource = useImageSource(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]);
|
|
|
|
|
|
2021-02-09 16:58:14 +11:00
|
|
|
return <Grid stroke={isImageLight ? "black" : "white"} />;
|
2020-05-31 16:25:05 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MapGrid;
|