This commit is contained in:
Jens Cornelsen
2024-12-09 16:23:45 +01:00
parent 0085a93f25
commit adc67c819a

View File

@@ -83,9 +83,9 @@ import Layout from "#layouts/layoutCAD.astro";
const lineLog = document.getElementById('line-log');
const pixelToMeter = 0.01; // Conversion factor: 1 pixel = 0.01 meter
const gridSize = 10; // Size of each grid cell in pixels
let lastEndPoint = null;
let lastStartPoint = null;
let capturedLines = [];
let lastEndPoint = null; // Tracks the end point of the last line
let firstStartPoint = null; // Tracks the start point of the first line
// Snap a coordinate to the nearest grid point
function snapToGrid(value) {
@@ -118,7 +118,7 @@ import Layout from "#layouts/layoutCAD.astro";
ctx.clearRect(0, 0, canvas.width, canvas.height);
capturedLines = [];
lastEndPoint = null;
lastStartPoint = null;
firstStartPoint = null;
drawGrid(); // Redraw the grid after clearing
updateLineLog();
}
@@ -149,20 +149,17 @@ import Layout from "#layouts/layoutCAD.astro";
// Determine the starting point for the new line
let startX, startY;
if (lastEndPoint && clickX === lastEndPoint.x && clickY === lastEndPoint.y) {
// Snap to the last endpoint
startX = lastEndPoint.x;
startY = lastEndPoint.y;
} else if (lastStartPoint && clickX === lastStartPoint.x && clickY === lastStartPoint.y) {
// Snap to the last starting point
startX = lastStartPoint.x;
startY = lastStartPoint.y;
} else if (!capturedLines.length) {
// First line can start anywhere
if (!lastEndPoint) {
// For the first line, set the start point anywhere
startX = clickX;
startY = clickY;
firstStartPoint = { x: startX, y: startY }; // Save the first start point
} else if (clickX === lastEndPoint.x && clickY === lastEndPoint.y) {
// For subsequent lines, start at the last endpoint
startX = lastEndPoint.x;
startY = lastEndPoint.y;
} else {
// If click doesn't match a valid connection, do nothing
// If not a valid start point, do nothing
return;
}
@@ -170,11 +167,21 @@ import Layout from "#layouts/layoutCAD.astro";
const endX = snapToGrid(e.offsetX);
const endY = snapToGrid(e.offsetY);
// The endpoint must be either:
// - A valid grid point (different from the start point)
// - The start point of the first line
const isValidEndPoint =
(endX !== startX || endY !== startY) &&
(endX === firstStartPoint?.x && endY === firstStartPoint?.y);
if (!isValidEndPoint) {
return;
}
// Draw the line
drawLine(startX, startY, endX, endY);
// Update the last start and end points
lastStartPoint = { x: startX, y: startY };
// Update the last endpoint
lastEndPoint = { x: endX, y: endY };
// Capture the line details
@@ -196,8 +203,4 @@ import Layout from "#layouts/layoutCAD.astro";
drawGrid(); // Draw the grid when the page loads
</script>
</Layout>