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;