make this grid array of 3d matrix to center in canvas and zoom.: const canvas =…
- User
make this grid array of 3d matrix to center in canvas and zoom.: const canvas = document.getElementById("canvas"); const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth; canvas.height = window.innerHeight;
const rotationSpeed = 0.001; const gridSize = 50; const gridSpacing = 25; const zoomStep = 5; const minZoom = 100; const maxZoom = 10000; const centerFocus = 180;
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 = 0; let angleY = 0; let zoom = 800; let dragStart = null;
function rotate(point, angleX, angleY) { const { x, y, z } = point;
const offsetX = ((centerFocus - minZoom) / (centerFocus - minZoom)) * gridSize / 2; const offsetZ = ((maxZoom - centerFocus) / (maxZoom - minZoom)) * gridSize / 2;
const xPos = x - offsetX; const yPos = y - offsetX; const zPos = z - offsetZ;
const cosX = Math.cos(angleX); const sinX = Math.sin(angleX); const cosY = Math.cos(angleY); const sinY = Math.sin(angleY);
const newY = yPos * cosX - zPos * sinX; const newZ = yPos * sinX + zPos * cosX; const newX = xPos * cosY - newZ * sinY; const newZ2 = xPos * sinY + newZ * cosY;
return { x: newX + offsetX, y: newY + offsetX, z: newZ2 + offsetZ }; }
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 }; } });
- Assistant
In order to make the grid array of a 3D matrix centered in the canvas and zoomable, you can follow these steps:
- Modify the
projectfunction to accept acenterparameter and apply offsets to the grid points using this center:
function project(point, width, height, center) { const { x, y, z } = point; const offsetX = (x - center.x) * zoom / 1000; const offsetY = (y - center.y) * zoom / 1000; const offsetZ = (z - center.z) * zoom / 1000; const scale = zoom / (zoom / 2 + offsetZ); const newX = offsetX * scale + width / 2; const newY = offsetY * scale + height / 2; return { x: newX, y: newY, scale }; } - Modify the
- User
- Assistant
To fix the centering and zooming issue, update the constants for
gridSizeandgridSpacing. You can also adjust thezoomconstant depending on your preference for the initial zoom level. Additionally, reset theoffsetXandoffsetZvalues to0in therotatefunction.const gridSize = 50; const gridSpacing = 50; let zoom = 800; function rotate(point, angleX, angleY) { const { x, y, z } = point; const offsetX = 0; const offsetZ = 0; const xPos = x - offsetX; const yPos = y - offsetX; const zPos = z - offsetZ; // ... (no changes in the rest of the function) }These changes should center the 3D matrix cube point structure in the canvas and allow you to zoom in and out properly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.