Added touch support for map drawing

This commit is contained in:
Mitchell McCaffrey 2020-04-19 17:39:26 +10:00
parent 16cca15872
commit 04e4bf3f29

View File

@ -13,7 +13,16 @@ function MapDrawing({
const canvasRef = useRef(); const canvasRef = useRef();
const containerRef = useRef(); const containerRef = useRef();
function getMousePosition(event) { 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) {
const container = containerRef.current; const container = containerRef.current;
if (container) { if (container) {
const containerRect = container.getBoundingClientRect(); const containerRect = container.getBoundingClientRect();
@ -23,34 +32,47 @@ function MapDrawing({
} }
} }
const [brushPoints, setBrushPoints] = useState([]); function handleStart(event) {
const [isMouseDown, setIsMouseDown] = useState(false); if (event.touches && event.touches.length !== 1) {
function handleMouseDown(event) { setIsDrawing(false);
setIsMouseDown(true); setBrushPoints([]);
return;
}
const pointer = event.touches ? event.touches[0] : event;
const position = getRelativePointerPosition(pointer);
setPointerPosition(position);
setIsDrawing(true);
if (selectedTool === "brush") { if (selectedTool === "brush") {
const position = getMousePosition(event);
setBrushPoints([position]); setBrushPoints([position]);
} }
} }
const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 }); function handleMove(event) {
function handleMouseMove(event) { if (event.touches && event.touches.length !== 1) {
const position = getMousePosition(event); return;
if (selectedTool === "erase") {
setMousePosition(position);
} }
if (isMouseDown && selectedTool === "brush") { const pointer = event.touches ? event.touches[0] : event;
setMousePosition(position); const position = getRelativePointerPosition(pointer);
if (selectedTool === "erase") {
setPointerPosition(position);
}
if (isDrawing && selectedTool === "brush") {
setPointerPosition(position);
setBrushPoints((prevPoints) => [...prevPoints, position]); setBrushPoints((prevPoints) => [...prevPoints, position]);
} }
} }
function handleMouseUp(event) { function handleStop(event) {
setIsMouseDown(false); if (event.touches && event.touches.length !== 0) {
return;
}
setIsDrawing(false);
if (selectedTool === "brush") { if (selectedTool === "brush") {
const simplifiedPoints = simplify(brushPoints, 0.001); if (brushPoints.length > 0) {
onShapeAdd({ id: shortid.generate(), points: simplifiedPoints }); const simplifiedPoints = simplify(brushPoints, 0.001);
setBrushPoints([]); onShapeAdd({ id: shortid.generate(), points: simplifiedPoints });
setBrushPoints([]);
}
} }
if (selectedTool === "erase" && hoveredShapeRef.current) { if (selectedTool === "erase" && hoveredShapeRef.current) {
onShapeRemove(hoveredShapeRef.current.id); onShapeRemove(hoveredShapeRef.current.id);
@ -89,8 +111,8 @@ function MapDrawing({
if ( if (
context.isPointInPath( context.isPointInPath(
path, path,
mousePosition.x * width, pointerPosition.x * width,
mousePosition.y * height pointerPosition.y * height
) )
) { ) {
hoveredShape = shape; hoveredShape = shape;
@ -112,8 +134,8 @@ function MapDrawing({
shapes, shapes,
width, width,
height, height,
mousePosition, pointerPosition,
isMouseDown, isDrawing,
selectedTool, selectedTool,
brushPoints, brushPoints,
]); ]);
@ -122,9 +144,12 @@ function MapDrawing({
<div <div
style={{ position: "absolute", top: 0, left: 0, right: 0, bottom: 0 }} style={{ position: "absolute", top: 0, left: 0, right: 0, bottom: 0 }}
ref={containerRef} ref={containerRef}
onMouseDown={handleMouseDown} onMouseDown={handleStart}
onMouseMove={handleMouseMove} onMouseMove={handleMove}
onMouseUp={handleMouseUp} onMouseUp={handleStop}
onTouchStart={handleStart}
onTouchMove={handleMove}
onTouchEnd={handleStop}
> >
<canvas <canvas
ref={canvasRef} ref={canvasRef}