2020-04-18 18:54:13 +10:00
|
|
|
import React, { useRef, useEffect, useState } from "react";
|
2020-04-18 19:09:48 +10:00
|
|
|
import simplify from "simplify-js";
|
2020-04-18 18:11:21 +10:00
|
|
|
|
|
|
|
|
function MapDrawing({ width, height }) {
|
2020-04-18 18:54:13 +10:00
|
|
|
const canvasRef = useRef();
|
2020-04-18 18:11:21 +10:00
|
|
|
const containerRef = useRef();
|
|
|
|
|
|
2020-04-18 18:54:13 +10:00
|
|
|
const [shapes, setShapes] = useState([]);
|
2020-04-18 18:11:21 +10:00
|
|
|
|
|
|
|
|
function getMousePosition(event) {
|
|
|
|
|
const container = containerRef.current;
|
|
|
|
|
if (container) {
|
|
|
|
|
const containerRect = container.getBoundingClientRect();
|
|
|
|
|
const x = (event.clientX - containerRect.x) / containerRect.width;
|
|
|
|
|
const y = (event.clientY - containerRect.y) / containerRect.height;
|
2020-04-18 19:09:48 +10:00
|
|
|
return { x, y };
|
2020-04-18 18:11:21 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 18:54:13 +10:00
|
|
|
const [isMouseDown, setIsMouseDown] = useState(false);
|
2020-04-18 18:11:21 +10:00
|
|
|
function handleMouseDown(event) {
|
2020-04-18 18:54:13 +10:00
|
|
|
setIsMouseDown(true);
|
|
|
|
|
const position = getMousePosition(event);
|
|
|
|
|
setShapes((prevShapes) => [...prevShapes, { points: [position] }]);
|
2020-04-18 18:11:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleMouseMove(event) {
|
2020-04-18 18:54:13 +10:00
|
|
|
if (isMouseDown) {
|
|
|
|
|
const position = getMousePosition(event);
|
|
|
|
|
setShapes((prevShapes) => {
|
|
|
|
|
const currentShape = prevShapes.slice(-1)[0];
|
|
|
|
|
const otherShapes = prevShapes.slice(0, -1);
|
|
|
|
|
return [...otherShapes, { points: [...currentShape.points, position] }];
|
|
|
|
|
});
|
2020-04-18 18:11:21 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleMouseUp(event) {
|
2020-04-18 18:54:13 +10:00
|
|
|
setIsMouseDown(false);
|
2020-04-18 19:09:48 +10:00
|
|
|
setShapes((prevShapes) => {
|
|
|
|
|
const currentShape = prevShapes.slice(-1)[0];
|
|
|
|
|
const otherShapes = prevShapes.slice(0, -1);
|
|
|
|
|
const simplified = simplify(currentShape.points, 0.001);
|
|
|
|
|
return [...otherShapes, { points: simplified }];
|
|
|
|
|
});
|
2020-04-18 18:11:21 +10:00
|
|
|
}
|
|
|
|
|
|
2020-04-18 18:54:13 +10:00
|
|
|
useEffect(() => {
|
|
|
|
|
const canvas = canvasRef.current;
|
|
|
|
|
if (canvas) {
|
|
|
|
|
const context = canvas.getContext("2d");
|
|
|
|
|
|
|
|
|
|
context.clearRect(0, 0, width, height);
|
|
|
|
|
for (let shape of shapes) {
|
|
|
|
|
context.beginPath();
|
2020-04-18 19:09:48 +10:00
|
|
|
context.moveTo(shape.points[0].x * width, shape.points[0].y * height);
|
2020-04-18 18:54:13 +10:00
|
|
|
for (let point of shape.points.slice(1)) {
|
2020-04-18 19:09:48 +10:00
|
|
|
context.lineTo(point.x * width, point.y * height);
|
2020-04-18 18:54:13 +10:00
|
|
|
}
|
|
|
|
|
context.closePath();
|
|
|
|
|
context.stroke();
|
|
|
|
|
context.fill();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [shapes, width, height]);
|
|
|
|
|
|
2020-04-18 18:11:21 +10:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
style={{ position: "absolute", top: 0, left: 0, right: 0, bottom: 0 }}
|
|
|
|
|
ref={containerRef}
|
|
|
|
|
onMouseDown={handleMouseDown}
|
|
|
|
|
onMouseMove={handleMouseMove}
|
|
|
|
|
onMouseUp={handleMouseUp}
|
2020-04-18 18:54:13 +10:00
|
|
|
>
|
|
|
|
|
<canvas
|
|
|
|
|
ref={canvasRef}
|
|
|
|
|
width={width}
|
|
|
|
|
height={height}
|
|
|
|
|
style={{ width: "100%", height: "100%" }}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2020-04-18 18:11:21 +10:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MapDrawing;
|