Optimise grid and interaction context

This commit is contained in:
Mitchell McCaffrey 2021-03-12 11:02:58 +11:00
parent 01d738b9c6
commit e6fd10a904
14 changed files with 406 additions and 135 deletions

View File

@ -4,7 +4,12 @@ import useImage from "use-image";
import Vector2 from "../helpers/Vector2"; import Vector2 from "../helpers/Vector2";
import { useGrid } from "../contexts/GridContext"; import {
useGrid,
useGridPixelSize,
useGridOffset,
useGridCellPixelSize,
} from "../contexts/GridContext";
import squarePatternDark from "../images/SquarePatternDark.png"; import squarePatternDark from "../images/SquarePatternDark.png";
import squarePatternLight from "../images/SquarePatternLight.png"; import squarePatternLight from "../images/SquarePatternLight.png";
@ -12,7 +17,11 @@ import hexPatternDark from "../images/HexPatternDark.png";
import hexPatternLight from "../images/HexPatternLight.png"; import hexPatternLight from "../images/HexPatternLight.png";
function Grid({ stroke }) { function Grid({ stroke }) {
const { grid, gridPixelSize, gridOffset, gridCellPixelSize } = useGrid(); const grid = useGrid();
const gridPixelSize = useGridPixelSize();
const gridOffset = useGridOffset();
const gridCellPixelSize = useGridCellPixelSize();
let imageSource; let imageSource;
if (grid.type === "square") { if (grid.type === "square") {
if (stroke === "black") { if (stroke === "black") {

View File

@ -2,9 +2,17 @@ import React, { useState, useEffect } from "react";
import shortid from "shortid"; import shortid from "shortid";
import { Group, Line, Rect, Circle } from "react-konva"; import { Group, Line, Rect, Circle } from "react-konva";
import { useMapInteraction } from "../../contexts/MapInteractionContext"; import {
useDebouncedStageScale,
useMapWidth,
useMapHeight,
useInteractionEmitter,
} from "../../contexts/MapInteractionContext";
import { useMapStage } from "../../contexts/MapStageContext"; import { useMapStage } from "../../contexts/MapStageContext";
import { useGrid } from "../../contexts/GridContext"; import {
useGridCellNormalizedSize,
useGridStrokeWidth,
} from "../../contexts/GridContext";
import Vector2 from "../../helpers/Vector2"; import Vector2 from "../../helpers/Vector2";
import { import {
@ -25,13 +33,14 @@ function MapDrawing({
active, active,
toolSettings, toolSettings,
}) { }) {
const { const stageScale = useDebouncedStageScale();
stageScale, const mapWidth = useMapWidth();
mapWidth, const mapHeight = useMapHeight();
mapHeight, const interactionEmitter = useInteractionEmitter();
interactionEmitter,
} = useMapInteraction(); const gridCellNormalizedSize = useGridCellNormalizedSize();
const { gridCellNormalizedSize, gridStrokeWidth } = useGrid(); const gridStrokeWidth = useGridStrokeWidth();
const mapStageRef = useMapStage(); const mapStageRef = useMapStage();
const [drawingShape, setDrawingShape] = useState(null); const [drawingShape, setDrawingShape] = useState(null);
const [isBrushDown, setIsBrushDown] = useState(false); const [isBrushDown, setIsBrushDown] = useState(false);

View File

@ -5,9 +5,21 @@ import useImage from "use-image";
import diagonalPattern from "../../images/DiagonalPattern.png"; import diagonalPattern from "../../images/DiagonalPattern.png";
import { useMapInteraction } from "../../contexts/MapInteractionContext"; import {
useDebouncedStageScale,
useMapWidth,
useMapHeight,
useInteractionEmitter,
} from "../../contexts/MapInteractionContext";
import { useMapStage } from "../../contexts/MapStageContext"; import { useMapStage } from "../../contexts/MapStageContext";
import { useGrid } from "../../contexts/GridContext"; import {
useGrid,
useGridCellPixelSize,
useGridCellNormalizedSize,
useGridStrokeWidth,
useGridCellPixelOffset,
useGridOffset,
} from "../../contexts/GridContext";
import { useKeyboard } from "../../contexts/KeyboardContext"; import { useKeyboard } from "../../contexts/KeyboardContext";
import Vector2 from "../../helpers/Vector2"; import Vector2 from "../../helpers/Vector2";
@ -41,20 +53,18 @@ function MapFog({
toolSettings, toolSettings,
editable, editable,
}) { }) {
const { const stageScale = useDebouncedStageScale();
stageScale, const mapWidth = useMapWidth();
mapWidth, const mapHeight = useMapHeight();
mapHeight, const interactionEmitter = useInteractionEmitter();
interactionEmitter,
} = useMapInteraction(); const grid = useGrid();
const { const gridCellNormalizedSize = useGridCellNormalizedSize();
grid, const gridCellPixelSize = useGridCellPixelSize();
gridCellNormalizedSize, const gridStrokeWidth = useGridStrokeWidth();
gridCellPixelSize, const gridCellPixelOffset = useGridCellPixelOffset();
gridStrokeWidth, const gridOffset = useGridOffset();
gridCellPixelOffset,
gridOffset,
} = useGrid();
const [gridSnappingSensitivity] = useSetting("map.gridSnappingSensitivity"); const [gridSnappingSensitivity] = useSetting("map.gridSnappingSensitivity");
const mapStageRef = useMapStage(); const mapStageRef = useMapStage();

View File

@ -1,18 +1,21 @@
import React, { useRef } from "react"; import React, { useRef } from "react";
import { Group, Circle, Rect } from "react-konva"; import { Group, Circle, Rect } from "react-konva";
import { useMapInteraction } from "../../contexts/MapInteractionContext"; import {
useDebouncedStageScale,
useMapWidth,
useMapHeight,
useSetPreventMapInteraction,
} from "../../contexts/MapInteractionContext";
import { useKeyboard } from "../../contexts/KeyboardContext"; import { useKeyboard } from "../../contexts/KeyboardContext";
import Vector2 from "../../helpers/Vector2"; import Vector2 from "../../helpers/Vector2";
function MapGridEditor({ map, onGridChange }) { function MapGridEditor({ map, onGridChange }) {
const { const stageScale = useDebouncedStageScale();
mapWidth, const mapWidth = useMapWidth();
mapHeight, const mapHeight = useMapHeight();
stageScale, const setPreventMapInteraction = useSetPreventMapInteraction();
setPreventMapInteraction,
} = useMapInteraction();
const mapSize = { x: mapWidth, y: mapHeight }; const mapSize = { x: mapWidth, y: mapHeight };

View File

@ -1,9 +1,20 @@
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import { Group, Line, Text, Label, Tag } from "react-konva"; import { Group, Line, Text, Label, Tag } from "react-konva";
import { useMapInteraction } from "../../contexts/MapInteractionContext"; import {
useDebouncedStageScale,
useMapWidth,
useMapHeight,
useInteractionEmitter,
} from "../../contexts/MapInteractionContext";
import { useMapStage } from "../../contexts/MapStageContext"; import { useMapStage } from "../../contexts/MapStageContext";
import { useGrid } from "../../contexts/GridContext"; import {
useGrid,
useGridCellPixelSize,
useGridCellNormalizedSize,
useGridStrokeWidth,
useGridOffset,
} from "../../contexts/GridContext";
import { import {
getDefaultShapeData, getDefaultShapeData,
@ -16,19 +27,17 @@ import { parseGridScale, gridDistance } from "../../helpers/grid";
import useGridSnapping from "../../hooks/useGridSnapping"; import useGridSnapping from "../../hooks/useGridSnapping";
function MapMeasure({ map, active }) { function MapMeasure({ map, active }) {
const { const stageScale = useDebouncedStageScale();
stageScale, const mapWidth = useMapWidth();
mapWidth, const mapHeight = useMapHeight();
mapHeight, const interactionEmitter = useInteractionEmitter();
interactionEmitter,
} = useMapInteraction(); const grid = useGrid();
const { const gridCellNormalizedSize = useGridCellNormalizedSize();
grid, const gridCellPixelSize = useGridCellPixelSize();
gridCellNormalizedSize, const gridStrokeWidth = useGridStrokeWidth();
gridStrokeWidth, const gridOffset = useGridOffset();
gridCellPixelSize,
gridOffset,
} = useGrid();
const mapStageRef = useMapStage(); const mapStageRef = useMapStage();
const [drawingShapeData, setDrawingShapeData] = useState(null); const [drawingShapeData, setDrawingShapeData] = useState(null);
const [isBrushDown, setIsBrushDown] = useState(false); const [isBrushDown, setIsBrushDown] = useState(false);

View File

@ -2,7 +2,7 @@ import React, { useState, useEffect, useRef } from "react";
import shortid from "shortid"; import shortid from "shortid";
import { Group } from "react-konva"; import { Group } from "react-konva";
import { useMapInteraction } from "../../contexts/MapInteractionContext"; import { useInteractionEmitter } from "../../contexts/MapInteractionContext";
import { useMapStage } from "../../contexts/MapStageContext"; import { useMapStage } from "../../contexts/MapStageContext";
import { useAuth } from "../../contexts/AuthContext"; import { useAuth } from "../../contexts/AuthContext";
@ -27,7 +27,7 @@ function MapNotes({
onNoteDragEnd, onNoteDragEnd,
fadeOnHover, fadeOnHover,
}) { }) {
const { interactionEmitter } = useMapInteraction(); const interactionEmitter = useInteractionEmitter();
const { userId } = useAuth(); const { userId } = useAuth();
const mapStageRef = useMapStage(); const mapStageRef = useMapStage();
const [isBrushDown, setIsBrushDown] = useState(false); const [isBrushDown, setIsBrushDown] = useState(false);

View File

@ -1,9 +1,13 @@
import React, { useEffect } from "react"; import React, { useEffect } from "react";
import { Group } from "react-konva"; import { Group } from "react-konva";
import { useMapInteraction } from "../../contexts/MapInteractionContext"; import {
useMapWidth,
useMapHeight,
useInteractionEmitter,
} from "../../contexts/MapInteractionContext";
import { useMapStage } from "../../contexts/MapStageContext"; import { useMapStage } from "../../contexts/MapStageContext";
import { useGrid } from "../../contexts/GridContext"; import { useGridStrokeWidth } from "../../contexts/GridContext";
import { import {
getRelativePointerPositionNormalized, getRelativePointerPositionNormalized,
@ -22,8 +26,10 @@ function MapPointer({
visible, visible,
color, color,
}) { }) {
const { mapWidth, mapHeight, interactionEmitter } = useMapInteraction(); const mapWidth = useMapWidth();
const { gridStrokeWidth } = useGrid(); const mapHeight = useMapHeight();
const interactionEmitter = useInteractionEmitter();
const gridStrokeWidth = useGridStrokeWidth();
const mapStageRef = useMapStage(); const mapStageRef = useMapStage();
useEffect(() => { useEffect(() => {

View File

@ -0,0 +1,5 @@
import React from "react";
function MapTest() {}
export default MapTest;

View File

@ -10,8 +10,13 @@ import usePrevious from "../../hooks/usePrevious";
import useGridSnapping from "../../hooks/useGridSnapping"; import useGridSnapping from "../../hooks/useGridSnapping";
import { useAuth } from "../../contexts/AuthContext"; import { useAuth } from "../../contexts/AuthContext";
import { useMapInteraction } from "../../contexts/MapInteractionContext"; import {
import { useGrid } from "../../contexts/GridContext"; useSetPreventMapInteraction,
useMapWidth,
useMapHeight,
useDebouncedStageScale,
} from "../../contexts/MapInteractionContext";
import { useGridCellPixelSize } from "../../contexts/GridContext";
import TokenStatus from "../token/TokenStatus"; import TokenStatus from "../token/TokenStatus";
import TokenLabel from "../token/TokenLabel"; import TokenLabel from "../token/TokenLabel";
@ -31,13 +36,13 @@ function MapToken({
map, map,
}) { }) {
const { userId } = useAuth(); const { userId } = useAuth();
const {
setPreventMapInteraction, const stageScale = useDebouncedStageScale();
mapWidth, const mapWidth = useMapWidth();
mapHeight, const mapHeight = useMapHeight();
stageScale, const setPreventMapInteraction = useSetPreventMapInteraction();
} = useMapInteraction();
const { gridCellPixelSize } = useGrid(); const gridCellPixelSize = useGridCellPixelSize();
const tokenSource = useDataSource(token, tokenSources, unknownSource); const tokenSource = useDataSource(token, tokenSources, unknownSource);
const [tokenSourceImage, tokenSourceStatus] = useImage(tokenSource); const [tokenSourceImage, tokenSourceStatus] = useImage(tokenSource);

View File

@ -3,8 +3,12 @@ import { Rect, Text } from "react-konva";
import { useSpring, animated } from "react-spring/konva"; import { useSpring, animated } from "react-spring/konva";
import { useAuth } from "../../contexts/AuthContext"; import { useAuth } from "../../contexts/AuthContext";
import { useMapInteraction } from "../../contexts/MapInteractionContext"; import {
import { useGrid } from "../../contexts/GridContext"; useSetPreventMapInteraction,
useMapWidth,
useMapHeight,
} from "../../contexts/MapInteractionContext";
import { useGridCellPixelSize } from "../../contexts/GridContext";
import colors from "../../helpers/colors"; import colors from "../../helpers/colors";
@ -24,8 +28,12 @@ function Note({
fadeOnHover, fadeOnHover,
}) { }) {
const { userId } = useAuth(); const { userId } = useAuth();
const { mapWidth, mapHeight, setPreventMapInteraction } = useMapInteraction();
const { gridCellPixelSize } = useGrid(); const mapWidth = useMapWidth();
const mapHeight = useMapHeight();
const setPreventMapInteraction = useSetPreventMapInteraction();
const gridCellPixelSize = useGridCellPixelSize();
const minCellSize = Math.min( const minCellSize = Math.min(
gridCellPixelSize.width, gridCellPixelSize.width,

View File

@ -1,7 +1,10 @@
import React from "react"; import React from "react";
import { useAuth } from "../../contexts/AuthContext"; import { useAuth } from "../../contexts/AuthContext";
import { useMapInteraction } from "../../contexts/MapInteractionContext"; import {
useMapWidth,
useMapHeight,
} from "../../contexts/MapInteractionContext";
import DragOverlay from "../DragOverlay"; import DragOverlay from "../DragOverlay";
@ -15,7 +18,9 @@ function TokenDragOverlay({
mapState, mapState,
}) { }) {
const { userId } = useAuth(); const { userId } = useAuth();
const { mapWidth, mapHeight } = useMapInteraction();
const mapWidth = useMapWidth();
const mapHeight = useMapHeight();
function handleTokenRemove() { function handleTokenRemove() {
// Handle other tokens when a vehicle gets deleted // Handle other tokens when a vehicle gets deleted

View File

@ -1,4 +1,4 @@
import React, { useContext } from "react"; import React, { useContext, useState, useEffect } from "react";
import Vector2 from "../helpers/Vector2"; import Vector2 from "../helpers/Vector2";
import Size from "../helpers/Size"; import Size from "../helpers/Size";
@ -37,55 +37,103 @@ const defaultValue = {
gridCellPixelOffset: new Vector2(0, 0), gridCellPixelOffset: new Vector2(0, 0),
}; };
const GridContext = React.createContext(defaultValue); const GridContext = React.createContext(defaultValue.grid);
const GridPixelSizeContext = React.createContext(defaultValue.gridPixelSize);
const GridCellPixelSizeContext = React.createContext(
defaultValue.gridCellPixelSize
);
const GridCellNormalizedSizeContext = React.createContext(
defaultValue.gridCellNormalizedSize
);
const GridOffsetContext = React.createContext(defaultValue.gridOffset);
const GridStrokeWidthContext = React.createContext(
defaultValue.gridStrokeWidth
);
const GridCellPixelOffsetContext = React.createContext(
defaultValue.gridCellPixelOffset
);
const defaultStrokeWidth = 1 / 10; const defaultStrokeWidth = 1 / 10;
export function GridProvider({ grid, width, height, children }) { export function GridProvider({ grid: inputGrid, width, height, children }) {
let grid = inputGrid;
if (!grid?.size.x || !grid?.size.y) { if (!grid?.size.x || !grid?.size.y) {
return ( grid = defaultValue.grid;
<GridContext.Provider value={defaultValue}>
{children}
</GridContext.Provider>
);
} }
const gridPixelSize = getGridPixelSize(grid, width, height); const [gridPixelSize, setGridPixelSize] = useState(
const gridCellPixelSize = getCellPixelSize( defaultValue.gridCellPixelSize
);
const [gridCellPixelSize, setGridCellPixelSize] = useState(
defaultValue.gridCellPixelSize
);
const [gridCellNormalizedSize, setGridCellNormalizedSize] = useState(
defaultValue.gridCellNormalizedSize
);
const [gridOffset, setGridOffset] = useState(defaultValue.gridOffset);
const [gridStrokeWidth, setGridStrokeWidth] = useState(
defaultValue.gridStrokeWidth
);
const [gridCellPixelOffset, setGridCellPixelOffset] = useState(
defaultValue.gridCellPixelOffset
);
useEffect(() => {
const _gridPixelSize = getGridPixelSize(grid, width, height);
const _gridCellPixelSize = getCellPixelSize(
grid, grid,
gridPixelSize.width, _gridPixelSize.width,
gridPixelSize.height _gridPixelSize.height
); );
const gridCellNormalizedSize = new Size( const _gridCellNormalizedSize = new Size(
gridCellPixelSize.width / width, _gridCellPixelSize.width / width,
gridCellPixelSize.height / height _gridCellPixelSize.height / height
); );
const gridOffset = Vector2.multiply(grid.inset.topLeft, { const _gridOffset = Vector2.multiply(grid.inset.topLeft, {
x: width, x: width,
y: height, y: height,
}); });
const gridStrokeWidth = const _gridStrokeWidth =
(gridCellPixelSize.width < gridCellPixelSize.height (_gridCellPixelSize.width < _gridCellPixelSize.height
? gridCellPixelSize.width ? _gridCellPixelSize.width
: gridCellPixelSize.height) * defaultStrokeWidth; : _gridCellPixelSize.height) * defaultStrokeWidth;
let gridCellPixelOffset = { x: 0, y: 0 }; let _gridCellPixelOffset = { x: 0, y: 0 };
// Move hex tiles to top left // Move hex tiles to top left
if (grid.type === "hexVertical" || grid.type === "hexHorizontal") { if (grid.type === "hexVertical" || grid.type === "hexHorizontal") {
gridCellPixelOffset = Vector2.multiply(gridCellPixelSize, 0.5); _gridCellPixelOffset = Vector2.multiply(_gridCellPixelSize, 0.5);
} }
const value = { setGridPixelSize(_gridPixelSize);
grid, setGridCellPixelSize(_gridCellPixelSize);
gridPixelSize, setGridCellNormalizedSize(_gridCellNormalizedSize);
gridCellPixelSize, setGridOffset(_gridOffset);
gridCellNormalizedSize, setGridStrokeWidth(_gridStrokeWidth);
gridOffset, setGridCellPixelOffset(_gridCellPixelOffset);
gridStrokeWidth, }, [grid, width, height]);
gridCellPixelOffset,
};
return <GridContext.Provider value={value}>{children}</GridContext.Provider>; return (
<GridContext.Provider value={grid}>
<GridPixelSizeContext.Provider value={gridPixelSize}>
<GridCellPixelSizeContext.Provider value={gridCellPixelSize}>
<GridCellNormalizedSizeContext.Provider
value={gridCellNormalizedSize}
>
<GridOffsetContext.Provider value={gridOffset}>
<GridStrokeWidthContext.Provider value={gridStrokeWidth}>
<GridCellPixelOffsetContext.Provider
value={gridCellPixelOffset}
>
{children}
</GridCellPixelOffsetContext.Provider>
</GridStrokeWidthContext.Provider>
</GridOffsetContext.Provider>
</GridCellNormalizedSizeContext.Provider>
</GridCellPixelSizeContext.Provider>
</GridPixelSizeContext.Provider>
</GridContext.Provider>
);
} }
export function useGrid() { export function useGrid() {
@ -96,4 +144,54 @@ export function useGrid() {
return context; return context;
} }
export default GridContext; export function useGridPixelSize() {
const context = useContext(GridPixelSizeContext);
if (context === undefined) {
throw new Error("useGridPixelSize must be used within a GridProvider");
}
return context;
}
export function useGridCellPixelSize() {
const context = useContext(GridCellPixelSizeContext);
if (context === undefined) {
throw new Error("useGridCellPixelSize must be used within a GridProvider");
}
return context;
}
export function useGridCellNormalizedSize() {
const context = useContext(GridCellNormalizedSizeContext);
if (context === undefined) {
throw new Error(
"useGridCellNormalizedSize must be used within a GridProvider"
);
}
return context;
}
export function useGridOffset() {
const context = useContext(GridOffsetContext);
if (context === undefined) {
throw new Error("useGridOffset must be used within a GridProvider");
}
return context;
}
export function useGridStrokeWidth() {
const context = useContext(GridStrokeWidthContext);
if (context === undefined) {
throw new Error("useGridStrokeWidth must be used within a GridProvider");
}
return context;
}
export function useGridCellPixelOffset() {
const context = useContext(GridCellPixelOffsetContext);
if (context === undefined) {
throw new Error(
"useGridCellPixelOffset must be used within a GridProvider"
);
}
return context;
}

View File

@ -1,24 +1,125 @@
import React, { useContext } from "react"; import React, { useContext } from "react";
import useDebounce from "../hooks/useDebounce";
const MapInteractionContext = React.createContext({ const StageScaleContext = React.createContext();
stageScale: 1, const DebouncedStageScaleContext = React.createContext();
stageWidth: 1, const StageWidthContext = React.createContext();
stageHeight: 1, const StageHeightContext = React.createContext();
setPreventMapInteraction: () => {}, const SetPreventMapInteractionContext = React.createContext();
mapWidth: 1, const MapWidthContext = React.createContext();
mapHeight: 1, const MapHeightContext = React.createContext();
interactionEmitter: null, const InteractionEmitterContext = React.createContext();
});
export const MapInteractionProvider = MapInteractionContext.Provider;
export function useMapInteraction() { export function MapInteractionProvider({ value, children }) {
const context = useContext(MapInteractionContext); const {
stageScale,
stageWidth,
stageHeight,
setPreventMapInteraction,
mapWidth,
mapHeight,
interactionEmitter,
} = value;
const debouncedStageScale = useDebounce(stageScale, 200);
return (
<InteractionEmitterContext.Provider value={interactionEmitter}>
<SetPreventMapInteractionContext.Provider
value={setPreventMapInteraction}
>
<StageWidthContext.Provider value={stageWidth}>
<StageHeightContext.Provider value={stageHeight}>
<MapWidthContext.Provider value={mapWidth}>
<MapHeightContext.Provider value={mapHeight}>
<StageScaleContext.Provider value={stageScale}>
<DebouncedStageScaleContext.Provider
value={debouncedStageScale}
>
{children}
</DebouncedStageScaleContext.Provider>
</StageScaleContext.Provider>
</MapHeightContext.Provider>
</MapWidthContext.Provider>
</StageHeightContext.Provider>
</StageWidthContext.Provider>
</SetPreventMapInteractionContext.Provider>
</InteractionEmitterContext.Provider>
);
}
export function useInteractionEmitter() {
const context = useContext(InteractionEmitterContext);
if (context === undefined) { if (context === undefined) {
throw new Error( throw new Error(
"useMapInteraction must be used within a MapInteractionProvider" "useInteractionEmitter must be used within a MapInteractionProvider"
); );
} }
return context; return context;
} }
export default MapInteractionContext; export function useSetPreventMapInteraction() {
const context = useContext(SetPreventMapInteractionContext);
if (context === undefined) {
throw new Error(
"useSetPreventMapInteraction must be used within a MapInteractionProvider"
);
}
return context;
}
export function useStageWidth() {
const context = useContext(StageWidthContext);
if (context === undefined) {
throw new Error(
"useStageWidth must be used within a MapInteractionProvider"
);
}
return context;
}
export function useStageHeight() {
const context = useContext(StageHeightContext);
if (context === undefined) {
throw new Error(
"useStageHeight must be used within a MapInteractionProvider"
);
}
return context;
}
export function useMapWidth() {
const context = useContext(MapWidthContext);
if (context === undefined) {
throw new Error("useMapWidth must be used within a MapInteractionProvider");
}
return context;
}
export function useMapHeight() {
const context = useContext(MapHeightContext);
if (context === undefined) {
throw new Error(
"useMapHeight must be used within a MapInteractionProvider"
);
}
return context;
}
export function useStageScale() {
const context = useContext(StageScaleContext);
if (context === undefined) {
throw new Error(
"useStageScale must be used within a MapInteractionProvider"
);
}
return context;
}
export function useDebouncedStageScale() {
const context = useContext(DebouncedStageScaleContext);
if (context === undefined) {
throw new Error(
"useDebouncedStageScale must be used within a MapInteractionProvider"
);
}
return context;
}

View File

@ -7,7 +7,12 @@ import {
import useSetting from "./useSetting"; import useSetting from "./useSetting";
import { useGrid } from "../contexts/GridContext"; import {
useGrid,
useGridOffset,
useGridCellPixelSize,
useGridCellPixelOffset,
} from "../contexts/GridContext";
/** /**
* Returns a function that when called will snap a node to the current grid * Returns a function that when called will snap a node to the current grid
@ -19,12 +24,10 @@ function useGridSnapping(snappingSensitivity) {
); );
snappingSensitivity = snappingSensitivity || defaultSnappingSensitivity; snappingSensitivity = snappingSensitivity || defaultSnappingSensitivity;
const { const grid = useGrid();
grid, const gridOffset = useGridOffset();
gridOffset, const gridCellPixelSize = useGridCellPixelSize();
gridCellPixelSize, const gridCellPixelOffset = useGridCellPixelOffset();
gridCellPixelOffset,
} = useGrid();
/** /**
* @param {Vector2} node The node to snap * @param {Vector2} node The node to snap