add measurement check shortcut ctrl+left mouse button. when you press ctrl+ left mouse…
- User
add measurement check shortcut ctrl+left mouse button. when you press ctrl+ left mouse button, a status bar displays the number inbetween any number of points selected distance. actual line shouldn't be drawn on grid when a measurement tool shortcut is activated through ctrl+left mouse button. if adding shortcut is impossible, then add some button near grid ui adjustment control elements to trigger that measurement tool on/off to show the numbers of points in distance mesured in status bar. output only full code.: const canvas = document.getElementById('canvas'); const statusBar = document.getElementById('distanceText'); const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth; canvas.height = window.innerHeight;
let grid = []; let gridSize = 120; let gridSpacing = 10;
function updateGrid() { grid = []; for (let x = -gridSize; x <= gridSize; x += gridSpacing) { for (let y = -gridSize; y <= gridSize; y += gridSpacing) { grid.push({ x, y }); } } }
function updateCanvasSize() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }
function updateUI() { const gridSizeInput = document.getElementById('gridSize'); const gridSpacingInput = document.getElementById('gridSpacing');
gridSizeInput.value = gridSize; gridSpacingInput.value = gridSpacing; }
function drawLine(line) { ctx.beginPath(); for (let i = 0; i < line.length - 1; i++) { const projectedStart = project(line[i]); const projectedEnd = project(line[i + 1]);
ctx.moveTo(projectedStart.x, projectedStart.y); ctx.lineTo(projectedEnd.x, projectedEnd.y); }
ctx.strokeStyle = 'rgba(25,200,25,0.2)'; ctx.lineWidth = 2; ctx.stroke(); }
function calculateGridCenter(grid) { let totalX = 0; let totalY = 0; const pointsCount = grid.length;
for (const point of grid) { totalX += point.x; totalY += point.y; }
return { x: totalX / pointsCount, y: totalY / pointsCount }; }
const clickedPoints = []; let lastClickedPoint = null; let activeLine = null; const lines = [];
function project(point) { const { x, y } = point;
const scale = 1; const newX = x * scale + canvas.width / 2; const newY = y * scale + canvas.height / 2;
return { x: newX, y: newY, scale }; }
function findClosestPoint(point) { const threshold = 20; let minDist = Infinity; let minIndex = -1;
for (let i = 0; i < grid.length; i++) { const projected = project(grid[i]); const dx = point.x - projected.x; const dy = point.y - projected.y; const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < minDist && dist <= threshold) { minDist = dist; minIndex = i; } }
return minIndex !== -1 ? grid[minIndex] : null; }
let mouseDown = false; let dragStart = null; let startPoint = null;
canvas.addEventListener('mousedown', (e) => { dragStart = { x: e.clientX, y: e.clientY };
- Assistant