Fix bug with zooming while on the drawing tool on a touch device

This commit is contained in:
Mitchell McCaffrey 2020-06-22 09:22:20 +10:00
parent ccc0c2b89d
commit d677f3623f
2 changed files with 26 additions and 6 deletions

View File

@ -344,6 +344,7 @@ function Map({
</> </>
} }
selectedToolId={selectedToolId} selectedToolId={selectedToolId}
onSelectedToolChange={setSelectedToolId}
> >
{mapGrid} {mapGrid}
{mapDrawing} {mapDrawing}

View File

@ -21,7 +21,13 @@ const touchZoomSpeed = 0.005;
const minZoom = 0.1; const minZoom = 0.1;
const maxZoom = 5; const maxZoom = 5;
function MapInteraction({ map, children, controls, selectedToolId }) { function MapInteraction({
map,
children,
controls,
selectedToolId,
onSelectedToolChange,
}) {
const mapSource = useDataSource(map, defaultMapSources); const mapSource = useDataSource(map, defaultMapSources);
const [mapSourceImage] = useImage(mapSource); const [mapSourceImage] = useImage(mapSource);
@ -77,15 +83,16 @@ function MapInteraction({ map, children, controls, selectedToolId }) {
const pinchPreviousDistanceRef = useRef(); const pinchPreviousDistanceRef = useRef();
const pinchPreviousOriginRef = useRef(); const pinchPreviousOriginRef = useRef();
const isInteractingCanvas = useRef(false); const isInteractingWithCanvas = useRef(false);
const previousSelectedToolRef = useRef(selectedToolId);
const bind = useGesture({ const bind = useGesture({
onWheelStart: ({ event }) => { onWheelStart: ({ event }) => {
isInteractingCanvas.current = isInteractingWithCanvas.current =
event.target === mapLayerRef.current.getCanvas()._canvas; event.target === mapLayerRef.current.getCanvas()._canvas;
}, },
onWheel: ({ delta }) => { onWheel: ({ delta }) => {
if (preventMapInteraction || !isInteractingCanvas.current) { if (preventMapInteraction || !isInteractingWithCanvas.current) {
return; return;
} }
const newScale = Math.min( const newScale = Math.min(
@ -94,6 +101,11 @@ function MapInteraction({ map, children, controls, selectedToolId }) {
); );
setStageScale(newScale); setStageScale(newScale);
}, },
onPinchStart: () => {
// Change to pan tool when pinching and zooming
previousSelectedToolRef.current = selectedToolId;
onSelectedToolChange("pan");
},
onPinch: ({ da, origin, first }) => { onPinch: ({ da, origin, first }) => {
const [distance] = da; const [distance] = da;
const [originX, originY] = origin; const [originX, originY] = origin;
@ -127,12 +139,19 @@ function MapInteraction({ map, children, controls, selectedToolId }) {
pinchPreviousDistanceRef.current = distance; pinchPreviousDistanceRef.current = distance;
pinchPreviousOriginRef.current = { x: originX, y: originY }; pinchPreviousOriginRef.current = { x: originX, y: originY };
}, },
onPinchEnd: () => {
onSelectedToolChange(previousSelectedToolRef.current);
},
onDragStart: ({ event }) => { onDragStart: ({ event }) => {
isInteractingCanvas.current = isInteractingWithCanvas.current =
event.target === mapLayerRef.current.getCanvas()._canvas; event.target === mapLayerRef.current.getCanvas()._canvas;
}, },
onDrag: ({ delta, xy, first, last, pinching }) => { onDrag: ({ delta, xy, first, last, pinching }) => {
if (preventMapInteraction || pinching || !isInteractingCanvas.current) { if (
preventMapInteraction ||
pinching ||
!isInteractingWithCanvas.current
) {
return; return;
} }