Files
grungnet/src/components/MapDrawing.js

232 lines
6.3 KiB
JavaScript
Raw Normal View History

import React, { useRef, useEffect, useState } from "react";
import simplify from "simplify-js";
import shortid from "shortid";
2020-04-18 18:11:21 +10:00
import colors from "../helpers/colors";
2020-04-20 15:17:56 +10:00
import { snapPositionToGrid } from "../helpers/shared";
import { pointsToGesture, gestureToData } from "../helpers/gestures";
function MapDrawing({
width,
height,
selectedTool,
shapes,
onShapeAdd,
onShapeRemove,
brushColor,
2020-04-20 15:17:56 +10:00
useGridSnapping,
gridSize,
useBrushBlending,
useBrushGesture,
}) {
const canvasRef = useRef();
2020-04-18 18:11:21 +10:00
const containerRef = useRef();
2020-04-19 17:39:26 +10:00
const [brushPoints, setBrushPoints] = useState([]);
const [isDrawing, setIsDrawing] = useState(false);
const [pointerPosition, setPointerPosition] = useState({ x: -1, y: -1 });
// Reset pointer position when tool changes
useEffect(() => {
setPointerPosition({ x: -1, y: -1 });
}, [selectedTool]);
function getRelativePointerPosition(event) {
2020-04-18 18:11:21 +10:00
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;
return { x, y };
2020-04-18 18:11:21 +10:00
}
}
2020-04-19 17:39:26 +10:00
function handleStart(event) {
if (event.touches && event.touches.length !== 1) {
setIsDrawing(false);
setBrushPoints([]);
return;
}
const pointer = event.touches ? event.touches[0] : event;
const position = getRelativePointerPosition(pointer);
setPointerPosition(position);
setIsDrawing(true);
2020-04-19 00:24:06 +10:00
if (selectedTool === "brush") {
2020-04-20 15:17:56 +10:00
const brushPosition = useGridSnapping
? snapPositionToGrid(position, gridSize)
: position;
setBrushPoints([brushPosition]);
2020-04-19 00:24:06 +10:00
}
2020-04-18 18:11:21 +10:00
}
2020-04-19 17:39:26 +10:00
function handleMove(event) {
if (event.touches && event.touches.length !== 1) {
return;
}
const pointer = event.touches ? event.touches[0] : event;
const position = getRelativePointerPosition(pointer);
2020-04-19 00:24:06 +10:00
if (selectedTool === "erase") {
2020-04-19 17:39:26 +10:00
setPointerPosition(position);
2020-04-19 00:24:06 +10:00
}
2020-04-19 17:39:26 +10:00
if (isDrawing && selectedTool === "brush") {
setPointerPosition(position);
2020-04-20 15:17:56 +10:00
const brushPosition = useGridSnapping
? snapPositionToGrid(position, gridSize)
: position;
setBrushPoints((prevPoints) => {
if (prevPoints[prevPoints.length - 1] === brushPosition) {
return prevPoints;
}
return [...prevPoints, brushPosition];
});
2020-04-18 18:11:21 +10:00
}
}
2020-04-19 17:39:26 +10:00
function handleStop(event) {
if (event.touches && event.touches.length !== 0) {
return;
}
setIsDrawing(false);
2020-04-19 00:24:06 +10:00
if (selectedTool === "brush") {
if (brushPoints.length > 1) {
2020-04-19 17:39:26 +10:00
const simplifiedPoints = simplify(brushPoints, 0.001);
const type = useBrushGesture
? pointsToGesture(simplifiedPoints)
: "path";
const data =
type === "path"
? { points: simplifiedPoints }
: gestureToData(simplifiedPoints, type);
onShapeAdd({
type,
data,
id: shortid.generate(),
color: brushColor,
blend: useBrushBlending,
});
2020-04-19 17:39:26 +10:00
setBrushPoints([]);
}
}
if (selectedTool === "erase" && hoveredShapeRef.current) {
onShapeRemove(hoveredShapeRef.current.id);
2020-04-19 00:24:06 +10:00
}
2020-04-18 18:11:21 +10:00
}
const hoveredShapeRef = useRef(null);
useEffect(() => {
function pointsToPath(points) {
const path = new Path2D();
path.moveTo(points[0].x * width, points[0].y * height);
for (let point of points.slice(1)) {
path.lineTo(point.x * width, point.y * height);
}
path.closePath();
return path;
}
function circleToPath(x, y, radius) {
const path = new Path2D();
const minSide = width < height ? width : height;
path.arc(x * width, y * height, radius * minSide, 0, 2 * Math.PI, true);
return path;
}
function rectangleToPath(x, y, w, h) {
const path = new Path2D();
path.rect(x * width, y * height, w * width, h * height);
return path;
}
function shapeToPath(shape) {
const data = shape.data;
if (shape.type === "path") {
return pointsToPath(data.points);
} else if (shape.type === "circle") {
return circleToPath(data.x, data.y, data.radius);
} else if (shape.type === "rectangle") {
return rectangleToPath(data.x, data.y, data.width, data.height);
} else if (shape.type === "triangle") {
return pointsToPath(data.points);
}
}
function drawPath(path, color, blend, context) {
context.globalAlpha = blend ? 0.5 : 1.0;
context.fillStyle = color;
context.strokeStyle = color;
context.stroke(path);
context.fill(path);
}
const canvas = canvasRef.current;
if (canvas) {
const context = canvas.getContext("2d");
context.clearRect(0, 0, width, height);
let hoveredShape = null;
for (let shape of shapes) {
const path = shapeToPath(shape);
// Detect hover
2020-04-19 00:24:06 +10:00
if (selectedTool === "erase") {
if (
context.isPointInPath(
path,
2020-04-19 17:39:26 +10:00
pointerPosition.x * width,
pointerPosition.y * height
2020-04-19 00:24:06 +10:00
)
) {
hoveredShape = shape;
2020-04-19 00:24:06 +10:00
}
}
drawPath(path, colors[shape.color], shape.blend, context);
2020-04-19 00:24:06 +10:00
}
if (selectedTool === "brush" && brushPoints.length > 0) {
const path = pointsToPath(brushPoints);
drawPath(path, colors[brushColor], useBrushBlending, context);
}
if (hoveredShape) {
const path = shapeToPath(hoveredShape);
drawPath(path, "#BB99FF", true, context);
}
hoveredShapeRef.current = hoveredShape;
}
}, [
shapes,
width,
height,
2020-04-19 17:39:26 +10:00
pointerPosition,
isDrawing,
selectedTool,
brushPoints,
brushColor,
useBrushGesture,
useBrushBlending,
]);
2020-04-18 18:11:21 +10:00
return (
<div
style={{ position: "absolute", top: 0, left: 0, right: 0, bottom: 0 }}
ref={containerRef}
2020-04-19 17:39:26 +10:00
onMouseDown={handleStart}
onMouseMove={handleMove}
onMouseUp={handleStop}
onTouchStart={handleStart}
onTouchMove={handleMove}
onTouchEnd={handleStop}
>
<canvas
ref={canvasRef}
width={width}
height={height}
style={{ width: "100%", height: "100%" }}
/>
</div>
2020-04-18 18:11:21 +10:00
);
}
export default MapDrawing;