From e5747235b46da202e3d95a3237eef9c376bcbe6a Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Tue, 3 Aug 2021 08:31:40 +1000 Subject: [PATCH] Refactor ruler out of measure tool --- src/components/konva/Ruler.tsx | 69 +++++++++++++++++++++++++ src/components/tools/MeasureTool.tsx | 77 +++++----------------------- 2 files changed, 83 insertions(+), 63 deletions(-) create mode 100644 src/components/konva/Ruler.tsx diff --git a/src/components/konva/Ruler.tsx b/src/components/konva/Ruler.tsx new file mode 100644 index 0000000..5b93b4d --- /dev/null +++ b/src/components/konva/Ruler.tsx @@ -0,0 +1,69 @@ +import { Group, Label, Line, Tag, Text } from "react-konva"; + +import { useGridStrokeWidth } from "../../contexts/GridContext"; +import { + useDebouncedStageScale, + useMapHeight, + useMapWidth, +} from "../../contexts/MapInteractionContext"; + +import { scaleAndFlattenPoints } from "../../helpers/konva"; +import Vector2 from "../../helpers/Vector2"; + +import { GridScale } from "../../types/Grid"; + +type RulerProps = { + points: Vector2[]; + scale: GridScale; + length: number; +}; + +function Ruler({ points, scale, length }: RulerProps) { + const stageScale = useDebouncedStageScale(); + const mapWidth = useMapWidth(); + const mapHeight = useMapHeight(); + const mapSize = new Vector2(mapWidth, mapHeight); + + const gridStrokeWidth = useGridStrokeWidth(); + + const linePoints = scaleAndFlattenPoints(points, mapSize); + + const lineCenter = Vector2.multiply(Vector2.centroid(points), mapSize); + + return ( + + + + + + ); +} + +export default Ruler; diff --git a/src/components/tools/MeasureTool.tsx b/src/components/tools/MeasureTool.tsx index 3c73dc1..7e352b3 100644 --- a/src/components/tools/MeasureTool.tsx +++ b/src/components/tools/MeasureTool.tsx @@ -1,18 +1,12 @@ import { useState, useEffect } from "react"; -import { Group, Line, Text, Label, Tag } from "react-konva"; +import { Group } from "react-konva"; -import { - useDebouncedStageScale, - useMapWidth, - useMapHeight, - useInteractionEmitter, -} from "../../contexts/MapInteractionContext"; +import { useInteractionEmitter } from "../../contexts/MapInteractionContext"; import { useMapStage } from "../../contexts/MapStageContext"; import { useGrid, useGridCellPixelSize, useGridCellNormalizedSize, - useGridStrokeWidth, useGridOffset, } from "../../contexts/GridContext"; @@ -24,6 +18,8 @@ import Vector2 from "../../helpers/Vector2"; import { getRelativePointerPosition } from "../../helpers/konva"; import { parseGridScale, gridDistance } from "../../helpers/grid"; +import Ruler from "../konva/Ruler"; + import useGridSnapping from "../../hooks/useGridSnapping"; import { Map } from "../../types/Map"; import { PointsData } from "../../types/Drawing"; @@ -36,15 +32,11 @@ type MapMeasureProps = { type MeasureData = { length: number; points: Vector2[] }; function MeasureTool({ map, active }: MapMeasureProps) { - const stageScale = useDebouncedStageScale(); - const mapWidth = useMapWidth(); - const mapHeight = useMapHeight(); const interactionEmitter = useInteractionEmitter(); const grid = useGrid(); const gridCellNormalizedSize = useGridCellNormalizedSize(); const gridCellPixelSize = useGridCellPixelSize(); - const gridStrokeWidth = useGridStrokeWidth(); const gridOffset = useGridOffset(); const mapStageRef = useMapStage(); @@ -147,58 +139,17 @@ function MeasureTool({ map, active }: MapMeasureProps) { }; }); - function renderShape(shapeData: MeasureData) { - const linePoints = shapeData.points.reduce( - (acc: number[], point) => [ - ...acc, - point.x * mapWidth, - point.y * mapHeight, - ], - [] - ); - - const lineCenter = Vector2.multiply( - Vector2.divide(Vector2.add(shapeData.points[0], shapeData.points[1]), 2), - { x: mapWidth, y: mapHeight } - ); - - return ( - - + {drawingShapeData && ( + - - - - ); - } - - return {drawingShapeData && renderShape(drawingShapeData)}; + )} + + ); } export default MeasureTool;