I don't know. I need to make this grid snapping to function properre, but it intersects…
- User
I don't know. I need to make this grid snapping to function properre, but it intersects between its own cells on each animationframe update, which is not intended. there should be a new wireframe line going from an attachment point or snap point and connected to actual wireframe model, but not the grid itself. just tell what need to replace and where and what to modify with what.:
} .green-dot { position: absolute; width: 5px; height: 5px; background-color: lime; border-radius: 50%; pointer-events: none; z-index:0; }const ctx = canvas.getContext('2d'); const vmcMenu = document.getElementById('vmc-menu');
const vertices = [ [0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0], [0, 0, 1], [0, 1, 1], [1, 1, 1], [1, 0, 1], ];
const edges = [ [0, 1], [1, 2], [2, 3], [3, 0], [0, 4], [1, 5], [2, 6], [3, 7], [4, 5], [5, 6], [6, 7], [7, 4], ];
const scale = 0.025; const zoom = 1; const offsetX = 0.5; const offsetY = 0.5; let edgeStart = null; let angleX = 0; let angleY = 0; let angleZ = 0; let bestIndex = -1; let bestDistance = Infinity; let startNewEdgeIndex = -1; let isMouseDown = false; let prevMousePos = null; const gridSpacing = 50; const gridPoints = []; let displayGrid = true; function roundToGrid(value) { return Math.round(value / gridSpacing) * gridSpacing; }
function snapToGrid(x, y) { return { x: roundToGrid(x - canvas.getBoundingClientRect().left), y: roundToGrid(y - canvas.getBoundingClientRect().top) }; }// Red Dot const redDot = document.getElementById('red-dot'); const greenDotContainer = document.getElementById('green-dot'); document.body.appendChild(greenDotContainer);
function addEdge(event) { if (bestIndex === -1) return; if (startNewEdgeIndex === -1) { const snapped = snapToGrid(window.event.clientX, window.event.clientY); const { x, y } = reverseProject([snapped.x, snapped.y], canvas.height * scale, offsetX, offsetY, zoom); vertices.push([x, y, 0]); startNewEdgeIndex = vertices.length - 1; } else { edges.push([startNewEdgeIndex, bestIndex]); startNewEdgeIndex = -1; } } function reverseProject(coordinates, scale, offsetX, offsetY, zoom) { const [x, y] = coordinates; return [ (x - canvas.width / 2) / (zoom * scale) + offsetX, (y - canvas.height / 2) / (zoom * scale) + offsetY, ]; }// Remove Edge document.getElementById('remove-edge').addEventListener('click', () => { if (bestIndex === -1) return; edges.forEach((edge, index) => { if (edge.includes(bestIndex)) { edges.splice(index, 1); } }); });
function rotateX(angle) { const c = Math.cos(angle); const s = Math.sin(angle); return [ [1, 0, 0], [0, c, -s], [0, s, c], ]; }
function rotateY(angle) { const c = Math.cos(angle); const s = Math.sin(angle); return [ [c, 0, s], [0, 1, 0], [-s, 0, c], ]; }
function rotateZ(angle) { const c = Math.cos(angle); const s = Math.sin(angle); return [ [c, -s, 0], [s, c, 0], [0, 0, 1], ]; }
- Assistant
Here's the modified code that adds a new wireframe line from an attachment point or snap point, connected to the actual wireframe model, and not the grid:
I added a new global variable called
newEdgeEndPoint, which stores the coordinates of the point from which the new edge will be drawn. This point will be updated every time the mouse moves if thestartNewEdgeIndexis not -1. Then, in therenderfunction, after drawing the grid, the new edge will be drawn from thestartNewEdgeIndexvertex to thenewEdgeEndPoint.let newEdgeEndPoint = null; // ... canvas.addEventListener('mousemove', (event) => { // ... if (startNewEdgeIndex !== -1) { newEdgeEndPoint = { x: parseFloat(greenDotContainer.style.transform.split("(")[1].split(",")[0]), y: parseFloat(greenDotContainer.style.transform.split(",")[1]) }; } // ... }); function render() { // ... if (newEdgeEndPoint !== null && startNewEdgeIndex !== -1) { const [startX, startY] = projectedVertices[startNewEdgeIndex]; const cpDist = 0.005 * Math.hypot(startX - newEdgeEndPoint.x, startY - newEdgeEndPoint.y); const angle = Math.atan2(newEdgeEndPoint.y - startY, newEdgeEndPoint.x - startX); const cpX = (startX + newEdgeEndPoint.x) / 2 + cpDist * Math.cos(angle - Math.PI / 2) * getDeviation(0.2); const cpY = (startY + newEdgeEndPoint.y) / 2 + cpDist * Math.sin(angle - Math.PI / 2) * getDeviation(0.2); ctx.beginPath(); ctx.moveTo(startX, startY); ctx.quadraticCurveTo(cpX, cpY, newEdgeEndPoint.x, newEdgeEndPoint.y); ctx.stroke(); } // ... }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.