2020-05-25 18:56:46 +10:00
|
|
|
import React, { useRef, useEffect, useState, useContext } from "react";
|
2020-04-27 17:29:46 +10:00
|
|
|
import { Box } from "theme-ui";
|
2020-05-21 16:46:50 +10:00
|
|
|
import { useGesture } from "react-use-gesture";
|
|
|
|
|
import ReactResizeDetector from "react-resize-detector";
|
|
|
|
|
import useImage from "use-image";
|
|
|
|
|
import { Stage, Layer, Image } from "react-konva";
|
2020-06-24 09:27:20 +10:00
|
|
|
import { EventEmitter } from "events";
|
2020-04-27 17:29:46 +10:00
|
|
|
|
2020-05-21 16:46:50 +10:00
|
|
|
import usePreventOverscroll from "../../helpers/usePreventOverscroll";
|
|
|
|
|
import useDataSource from "../../helpers/useDataSource";
|
|
|
|
|
|
|
|
|
|
import { mapSources as defaultMapSources } from "../../maps";
|
2020-04-29 18:21:44 +10:00
|
|
|
|
2020-05-21 16:46:50 +10:00
|
|
|
import { MapInteractionProvider } from "../../contexts/MapInteractionContext";
|
2020-06-19 18:04:58 +10:00
|
|
|
import MapStageContext, {
|
|
|
|
|
MapStageProvider,
|
|
|
|
|
} from "../../contexts/MapStageContext";
|
2020-05-21 16:46:50 +10:00
|
|
|
import AuthContext from "../../contexts/AuthContext";
|
2020-04-30 22:08:03 +10:00
|
|
|
|
2020-05-29 07:36:11 +10:00
|
|
|
const wheelZoomSpeed = -0.001;
|
2020-05-25 17:26:36 +10:00
|
|
|
const touchZoomSpeed = 0.005;
|
2020-04-27 17:29:46 +10:00
|
|
|
const minZoom = 0.1;
|
|
|
|
|
const maxZoom = 5;
|
|
|
|
|
|
2020-06-22 09:22:20 +10:00
|
|
|
function MapInteraction({
|
|
|
|
|
map,
|
|
|
|
|
children,
|
|
|
|
|
controls,
|
|
|
|
|
selectedToolId,
|
|
|
|
|
onSelectedToolChange,
|
|
|
|
|
}) {
|
2020-05-21 16:46:50 +10:00
|
|
|
const mapSource = useDataSource(map, defaultMapSources);
|
|
|
|
|
const [mapSourceImage] = useImage(mapSource);
|
2020-04-27 17:29:46 +10:00
|
|
|
|
2020-05-21 16:46:50 +10:00
|
|
|
const [stageWidth, setStageWidth] = useState(1);
|
|
|
|
|
const [stageHeight, setStageHeight] = useState(1);
|
|
|
|
|
const [stageScale, setStageScale] = useState(1);
|
|
|
|
|
const [preventMapInteraction, setPreventMapInteraction] = useState(false);
|
2020-04-27 17:29:46 +10:00
|
|
|
|
2020-05-21 16:46:50 +10:00
|
|
|
const stageWidthRef = useRef(stageWidth);
|
|
|
|
|
const stageHeightRef = useRef(stageHeight);
|
2020-05-25 14:09:45 +10:00
|
|
|
// Avoid state udpates when panning the map by using a ref and updating the konva element directly
|
|
|
|
|
const stageTranslateRef = useRef({ x: 0, y: 0 });
|
2020-04-27 17:29:46 +10:00
|
|
|
|
2020-05-25 18:56:46 +10:00
|
|
|
// Reset transform when map changes
|
|
|
|
|
useEffect(() => {
|
2020-05-25 14:09:45 +10:00
|
|
|
const layer = mapLayerRef.current;
|
|
|
|
|
if (map && layer) {
|
2020-05-21 16:46:50 +10:00
|
|
|
const mapHeight = stageWidthRef.current * (map.height / map.width);
|
2020-05-25 14:09:45 +10:00
|
|
|
const newTranslate = {
|
|
|
|
|
x: 0,
|
|
|
|
|
y: -(mapHeight - stageHeightRef.current) / 2,
|
|
|
|
|
};
|
|
|
|
|
layer.x(newTranslate.x);
|
|
|
|
|
layer.y(newTranslate.y);
|
|
|
|
|
layer.draw();
|
|
|
|
|
stageTranslateRef.current = newTranslate;
|
2020-05-25 18:08:39 +10:00
|
|
|
|
|
|
|
|
setStageScale(1);
|
2020-05-21 16:46:50 +10:00
|
|
|
}
|
2020-04-27 17:29:46 +10:00
|
|
|
}, [map]);
|
|
|
|
|
|
2020-05-25 17:26:36 +10:00
|
|
|
const pinchPreviousDistanceRef = useRef();
|
|
|
|
|
const pinchPreviousOriginRef = useRef();
|
2020-06-22 09:22:20 +10:00
|
|
|
const isInteractingWithCanvas = useRef(false);
|
|
|
|
|
const previousSelectedToolRef = useRef(selectedToolId);
|
2020-05-25 17:26:36 +10:00
|
|
|
|
2020-06-24 09:27:20 +10:00
|
|
|
const [interactionEmitter] = useState(new EventEmitter());
|
|
|
|
|
|
2020-05-21 16:46:50 +10:00
|
|
|
const bind = useGesture({
|
2020-05-28 11:52:18 +10:00
|
|
|
onWheelStart: ({ event }) => {
|
2020-06-22 09:22:20 +10:00
|
|
|
isInteractingWithCanvas.current =
|
2020-05-28 11:52:18 +10:00
|
|
|
event.target === mapLayerRef.current.getCanvas()._canvas;
|
|
|
|
|
},
|
2020-05-21 16:46:50 +10:00
|
|
|
onWheel: ({ delta }) => {
|
2020-06-22 09:22:20 +10:00
|
|
|
if (preventMapInteraction || !isInteractingWithCanvas.current) {
|
2020-05-26 15:44:41 +10:00
|
|
|
return;
|
|
|
|
|
}
|
2020-05-21 16:46:50 +10:00
|
|
|
const newScale = Math.min(
|
2020-05-22 23:43:55 +10:00
|
|
|
Math.max(stageScale + delta[1] * wheelZoomSpeed, minZoom),
|
|
|
|
|
maxZoom
|
|
|
|
|
);
|
|
|
|
|
setStageScale(newScale);
|
|
|
|
|
},
|
2020-06-22 09:22:20 +10:00
|
|
|
onPinchStart: () => {
|
|
|
|
|
// Change to pan tool when pinching and zooming
|
|
|
|
|
previousSelectedToolRef.current = selectedToolId;
|
|
|
|
|
onSelectedToolChange("pan");
|
|
|
|
|
},
|
2020-05-25 17:26:36 +10:00
|
|
|
onPinch: ({ da, origin, first }) => {
|
|
|
|
|
const [distance] = da;
|
|
|
|
|
const [originX, originY] = origin;
|
|
|
|
|
if (first) {
|
|
|
|
|
pinchPreviousDistanceRef.current = distance;
|
|
|
|
|
pinchPreviousOriginRef.current = { x: originX, y: originY };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply scale
|
|
|
|
|
const distanceDelta = distance - pinchPreviousDistanceRef.current;
|
|
|
|
|
const originXDelta = originX - pinchPreviousOriginRef.current.x;
|
|
|
|
|
const originYDelta = originY - pinchPreviousOriginRef.current.y;
|
2020-05-22 23:43:55 +10:00
|
|
|
const newScale = Math.min(
|
2020-05-25 17:26:36 +10:00
|
|
|
Math.max(stageScale + distanceDelta * touchZoomSpeed, minZoom),
|
2020-05-21 16:46:50 +10:00
|
|
|
maxZoom
|
|
|
|
|
);
|
|
|
|
|
setStageScale(newScale);
|
2020-05-25 17:26:36 +10:00
|
|
|
|
|
|
|
|
// Apply translate
|
|
|
|
|
const stageTranslate = stageTranslateRef.current;
|
|
|
|
|
const layer = mapLayerRef.current;
|
|
|
|
|
const newTranslate = {
|
|
|
|
|
x: stageTranslate.x + originXDelta / newScale,
|
|
|
|
|
y: stageTranslate.y + originYDelta / newScale,
|
|
|
|
|
};
|
|
|
|
|
layer.x(newTranslate.x);
|
|
|
|
|
layer.y(newTranslate.y);
|
|
|
|
|
layer.draw();
|
|
|
|
|
stageTranslateRef.current = newTranslate;
|
|
|
|
|
|
|
|
|
|
pinchPreviousDistanceRef.current = distance;
|
|
|
|
|
pinchPreviousOriginRef.current = { x: originX, y: originY };
|
2020-05-21 16:46:50 +10:00
|
|
|
},
|
2020-06-22 09:22:20 +10:00
|
|
|
onPinchEnd: () => {
|
|
|
|
|
onSelectedToolChange(previousSelectedToolRef.current);
|
|
|
|
|
},
|
2020-05-27 15:26:42 +10:00
|
|
|
onDragStart: ({ event }) => {
|
2020-06-22 09:22:20 +10:00
|
|
|
isInteractingWithCanvas.current =
|
2020-05-27 15:26:42 +10:00
|
|
|
event.target === mapLayerRef.current.getCanvas()._canvas;
|
|
|
|
|
},
|
2020-05-25 17:26:36 +10:00
|
|
|
onDrag: ({ delta, xy, first, last, pinching }) => {
|
2020-06-22 09:22:20 +10:00
|
|
|
if (
|
|
|
|
|
preventMapInteraction ||
|
|
|
|
|
pinching ||
|
|
|
|
|
!isInteractingWithCanvas.current
|
|
|
|
|
) {
|
2020-05-22 13:47:11 +10:00
|
|
|
return;
|
|
|
|
|
}
|
2020-05-25 14:09:45 +10:00
|
|
|
|
2020-05-22 13:47:11 +10:00
|
|
|
const [dx, dy] = delta;
|
2020-05-25 14:09:45 +10:00
|
|
|
const stageTranslate = stageTranslateRef.current;
|
|
|
|
|
const layer = mapLayerRef.current;
|
2020-05-22 13:47:11 +10:00
|
|
|
if (selectedToolId === "pan") {
|
2020-05-21 16:46:50 +10:00
|
|
|
const newTranslate = {
|
2020-05-22 13:47:11 +10:00
|
|
|
x: stageTranslate.x + dx / stageScale,
|
|
|
|
|
y: stageTranslate.y + dy / stageScale,
|
2020-05-21 16:46:50 +10:00
|
|
|
};
|
2020-05-25 14:09:45 +10:00
|
|
|
layer.x(newTranslate.x);
|
|
|
|
|
layer.y(newTranslate.y);
|
|
|
|
|
layer.draw();
|
2020-05-21 16:46:50 +10:00
|
|
|
stageTranslateRef.current = newTranslate;
|
2020-05-25 15:07:12 +10:00
|
|
|
}
|
2020-06-24 09:27:20 +10:00
|
|
|
if (first) {
|
|
|
|
|
interactionEmitter.emit("dragStart");
|
|
|
|
|
} else if (last) {
|
|
|
|
|
interactionEmitter.emit("dragEnd");
|
|
|
|
|
} else {
|
|
|
|
|
interactionEmitter.emit("drag");
|
2020-05-21 16:46:50 +10:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function handleResize(width, height) {
|
|
|
|
|
setStageWidth(width);
|
|
|
|
|
setStageHeight(height);
|
|
|
|
|
stageWidthRef.current = width;
|
|
|
|
|
stageHeightRef.current = height;
|
|
|
|
|
}
|
2020-04-30 15:45:20 +10:00
|
|
|
|
2020-05-22 15:10:20 +10:00
|
|
|
function getCursorForTool(tool) {
|
|
|
|
|
switch (tool) {
|
|
|
|
|
case "pan":
|
|
|
|
|
return "move";
|
|
|
|
|
case "fog":
|
2020-06-21 11:01:03 +10:00
|
|
|
case "drawing":
|
2020-05-22 15:10:20 +10:00
|
|
|
return "crosshair";
|
|
|
|
|
default:
|
|
|
|
|
return "default";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-21 16:46:50 +10:00
|
|
|
const containerRef = useRef();
|
|
|
|
|
usePreventOverscroll(containerRef);
|
2020-04-27 17:29:46 +10:00
|
|
|
|
2020-05-21 16:46:50 +10:00
|
|
|
const mapWidth = stageWidth;
|
|
|
|
|
const mapHeight = map ? stageWidth * (map.height / map.width) : stageHeight;
|
2020-04-27 17:29:46 +10:00
|
|
|
|
2020-05-21 16:46:50 +10:00
|
|
|
const mapStageRef = useContext(MapStageContext);
|
2020-05-25 14:09:45 +10:00
|
|
|
const mapLayerRef = useRef();
|
2020-05-22 13:47:11 +10:00
|
|
|
const mapImageRef = useRef();
|
2020-04-27 17:29:46 +10:00
|
|
|
|
2020-05-21 16:46:50 +10:00
|
|
|
const auth = useContext(AuthContext);
|
2020-04-27 17:29:46 +10:00
|
|
|
|
2020-05-21 16:46:50 +10:00
|
|
|
const mapInteraction = {
|
|
|
|
|
stageScale,
|
|
|
|
|
stageWidth,
|
|
|
|
|
stageHeight,
|
|
|
|
|
setPreventMapInteraction,
|
|
|
|
|
mapWidth,
|
|
|
|
|
mapHeight,
|
2020-06-24 09:27:20 +10:00
|
|
|
interactionEmitter,
|
2020-05-21 16:46:50 +10:00
|
|
|
};
|
2020-04-27 17:29:46 +10:00
|
|
|
|
2020-06-19 18:04:58 +10:00
|
|
|
// Enable keyboard interaction for map stage container
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const container = mapStageRef.current.container();
|
|
|
|
|
container.tabIndex = 1;
|
|
|
|
|
container.style.outline = "none";
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, []);
|
|
|
|
|
|
2020-04-27 17:29:46 +10:00
|
|
|
return (
|
|
|
|
|
<Box
|
2020-05-22 15:10:20 +10:00
|
|
|
sx={{
|
|
|
|
|
flexGrow: 1,
|
|
|
|
|
position: "relative",
|
|
|
|
|
cursor: getCursorForTool(selectedToolId),
|
2020-05-22 23:43:55 +10:00
|
|
|
touchAction: "none",
|
2020-05-22 15:10:20 +10:00
|
|
|
}}
|
2020-05-21 16:46:50 +10:00
|
|
|
ref={containerRef}
|
|
|
|
|
{...bind()}
|
2020-04-27 17:29:46 +10:00
|
|
|
className="map"
|
|
|
|
|
>
|
2020-05-21 16:46:50 +10:00
|
|
|
<ReactResizeDetector handleWidth handleHeight onResize={handleResize}>
|
|
|
|
|
<Stage
|
|
|
|
|
width={stageWidth}
|
|
|
|
|
height={stageHeight}
|
|
|
|
|
scale={{ x: stageScale, y: stageScale }}
|
|
|
|
|
x={stageWidth / 2}
|
|
|
|
|
y={stageHeight / 2}
|
|
|
|
|
offset={{ x: stageWidth / 2, y: stageHeight / 2 }}
|
|
|
|
|
ref={mapStageRef}
|
|
|
|
|
>
|
2020-05-25 14:09:45 +10:00
|
|
|
<Layer ref={mapLayerRef}>
|
2020-05-21 16:46:50 +10:00
|
|
|
<Image
|
|
|
|
|
image={mapSourceImage}
|
|
|
|
|
width={mapWidth}
|
|
|
|
|
height={mapHeight}
|
|
|
|
|
id="mapImage"
|
2020-05-22 13:47:11 +10:00
|
|
|
ref={mapImageRef}
|
2020-05-18 21:52:46 +10:00
|
|
|
/>
|
2020-05-21 16:46:50 +10:00
|
|
|
{/* Forward auth context to konva elements */}
|
|
|
|
|
<AuthContext.Provider value={auth}>
|
|
|
|
|
<MapInteractionProvider value={mapInteraction}>
|
2020-06-19 18:04:58 +10:00
|
|
|
<MapStageProvider value={mapStageRef}>
|
|
|
|
|
{children}
|
|
|
|
|
</MapStageProvider>
|
2020-05-21 16:46:50 +10:00
|
|
|
</MapInteractionProvider>
|
|
|
|
|
</AuthContext.Provider>
|
|
|
|
|
</Layer>
|
|
|
|
|
</Stage>
|
|
|
|
|
</ReactResizeDetector>
|
|
|
|
|
<MapInteractionProvider value={mapInteraction}>
|
|
|
|
|
{controls}
|
|
|
|
|
</MapInteractionProvider>
|
2020-04-27 17:29:46 +10:00
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MapInteraction;
|