Fixed erase for stroked paths

This commit is contained in:
Mitchell McCaffrey 2020-04-28 10:14:45 +10:00
parent 81f84d8a19
commit c3ed55e5e8

View File

@ -136,13 +136,6 @@ function MapDrawing({
setIsDrawing(false); setIsDrawing(false);
if (selectedTool === "brush") { if (selectedTool === "brush") {
if (drawingShape.data.points.length > 1) { if (drawingShape.data.points.length > 1) {
// const simplifiedPoints = simplify(
// drawingShape.data.points,
// getStrokeSize(drawingShape.strokeWidth, gridSize, 1, 1) * 0.1
// );
// const data = { points: simplifiedPoints };
// onShapeAdd({ ...drawingShape, data });
onShapeAdd(drawingShape); onShapeAdd(drawingShape);
} }
} else if (selectedTool === "shape") { } else if (selectedTool === "shape") {
@ -176,6 +169,9 @@ function MapDrawing({
}; };
}); });
/**
* Rendering
*/
const hoveredShapeRef = useRef(null); const hoveredShapeRef = useRef(null);
useEffect(() => { useEffect(() => {
function pointsToPath(points, close) { function pointsToPath(points, close) {
@ -232,6 +228,22 @@ function MapDrawing({
} }
} }
function isPathHovered(path, hasFill, context) {
if (hasFill) {
return context.isPointInPath(
path,
pointerPosition.x * width,
pointerPosition.y * height
);
} else {
return context.isPointInStroke(
path,
pointerPosition.x * width,
pointerPosition.y * height
);
}
}
const canvas = canvasRef.current; const canvas = canvasRef.current;
if (canvas) { if (canvas) {
const context = canvas.getContext("2d"); const context = canvas.getContext("2d");
@ -242,13 +254,7 @@ function MapDrawing({
const path = shapeToPath(shape); const path = shapeToPath(shape);
// Detect hover // Detect hover
if (selectedTool === "erase") { if (selectedTool === "erase") {
if ( if (isPathHovered(path, shapeHasFill(shape), context)) {
context.isPointInPath(
path,
pointerPosition.x * width,
pointerPosition.y * height
)
) {
hoveredShape = shape; hoveredShape = shape;
} }
} }
@ -275,7 +281,14 @@ function MapDrawing({
} }
if (hoveredShape) { if (hoveredShape) {
const path = shapeToPath(hoveredShape); const path = shapeToPath(hoveredShape);
drawPath(path, "#BB99FF", true, 1, true, context); drawPath(
path,
"#BB99FF",
shapeHasFill(hoveredShape),
hoveredShape.strokeWidth,
true,
context
);
} }
hoveredShapeRef.current = hoveredShape; hoveredShapeRef.current = hoveredShape;
} }