convert this code into a 2d drawing plane. make simple schematics editor with snapable…
- User
convert this code into a 2d drawing plane. make simple schematics editor with snapable properre grid. make some menu at top to set grid size in units and dimension in px em etc...:
canvas.width = window.innerWidth; canvas.height = window.innerHeight;
const rotationSpeed = 0.1; const gridSize = 50; const gridSpacing = 25; const zoomStep = 10; const minZoom = 100; const maxZoom = 2000; const centerFocus = 100;
let grid = []; for (let x = -gridSize; x <= gridSize; x += gridSpacing) { for (let y = -gridSize; y <= gridSize; y += gridSpacing) { for (let z = -gridSize; z <= gridSize; z += gridSpacing) { grid.push({ x, y, z }); } } }
let lastClickedPoint = null; const clickedPoints = []; let angleX = 95.03; let angleY = 119.99; let zoom = 100; let dragStart = null;
let offset = { x: gridSpacing / 25, y: gridSpacing / 25, z: 0 };
function rotate(point, angleX, angleY) { const { x, y, z } = point;
let centeredX = x - offset.x; let centeredY = y - offset.y; let centeredZ = z - offset.z;
const cosX = Math.cos(angleX); const sinX = Math.sin(angleX); const cosY = Math.cos(angleY); const sinY = Math.sin(angleY);
const newY = centeredY * cosX - centeredZ * sinX; const newZ = centeredY * sinX + centeredZ * cosX; const newX = centeredX * cosY - newZ * sinY; const newZ2 = centeredX * sinY + newZ * cosY;
return { x: newX + offset.x, y: newY + offset.y, z: newZ2 + offset.z }; }
function project(point, width, height) { const { x, y, z } = point;
const scale = zoom / (centerFocus + z); const newX = x * scale + width / 2; const newY = y * scale + 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(rotate(grid[i], angleX, angleY), canvas.width, canvas.height); 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 activeLine = null; const lines = [];
canvas.addEventListener("wheel", (e) => { e.preventDefault(); zoom += e.deltaY * -0.1 * zoomStep; zoom = Math.min(Math.max(zoom, minZoom), maxZoom); // Limit zoom to min and max thresholds });
canvas.addEventListener("mousedown", (e) => { dragStart = { x: e.clientX, y: e.clientY }; });
canvas.addEventListener("mousemove", (e) => { if (dragStart) { const deltaX = e.clientX - dragStart.x; const deltaY = e.clientY - dragStart.y; angleX += deltaY * 0.01; angleY += deltaX * 0.01; dragStart = { x: e.clientX, y: e.clientY }; } });
canvas.addEventListener("mouseup", (e) => { mouseDown = false; dragStart = null; if (activeLine !== null) { const canvasRect = canvas.getBoundingClientRect(); const mouseX = e.clientX - canvasRect.left; const mouseY = e.clientY - canvasRect.top; const endPoint = findClosestPoint({ x: mouseX, y: mouseY });
- Assistant