radial grid is invisible. need fix that.: const canvas =…
- User
radial grid is invisible. need fix that.: const canvas = document.createElement('canvas'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; document.body.appendChild(canvas);
const ctx = canvas.getContext('2d');
const sunPos = { x: 0, y: 0, z: 0, size: 20 }; const mercuryPos = { x: 50, y: 0, z: 0, size: 3 }; const venusPos = { x: 70, y: 0, z: -10, size: 4 }; const earthPos = { x: 100, y: 0, z: 0, size: 5 };
let rotation = { x: 0, y: 0, z: 0 };
function drawSolarSystem() { // Clear the canvas ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Milky Way background ctx.globalCompositeOperation = 'lighter'; ctx.fillStyle = 'black'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.globalCompositeOperation = 'lighter'; for (let i = 0; i < 1000; i++) { const size = Math.random() * 2; const x = Math.random() * canvas.width; const y = Math.random() * canvas.height; ctx.beginPath(); ctx.arc(x, y, size, 0, Math.PI * 2); ctx.fillStyle = 'rgba(255, 255, 255, ' + 0.1 + ')'; ctx.fill(); }
// Draw the Sun const sunPos2D = projectTo2D(sunPos.x, sunPos.y, sunPos.z); drawPlanet(sunPos2D.x, sunPos2D.y, sunPos.size, '#ffcc00');
// Draw Mercury const mercPos2D = projectTo2D(mercuryPos.x - sunPos.x, mercuryPos.y - sunPos.y, mercuryPos.z - sunPos.z); drawPlanet(mercPos2D.x, mercPos2D.y, mercuryPos.size, '#ff6666');
// Draw Venus const venusPos2D = projectTo2D(venusPos.x - sunPos.x, venusPos.y - sunPos.y, venusPos.z - sunPos.z); drawPlanet(venusPos2D.x, venusPos2D.y, venusPos.size, '#ffa500');
// Draw Earth const earthPos2D = projectTo2D(earthPos.x - sunPos.x, earthPos.y - sunPos.y, earthPos.z - sunPos.z); drawPlanet(earthPos2D.x, earthPos2D.y, earthPos.size, '#6699ff');
// Update planet positions for the next frame updatePlanetPositions();
// Update rotation angles based on mouse position let isDragging = false; let initialMousePos = { x: 0, y: 0 }; // store initial mouse position when dragging starts let currentRotation = { x: rotation.x, y: rotation.y }; // initialize current rotation angles to the current rotation let prevRotation = { x: 0, y: 0 }; // store previous (last) rotation angles
canvas.addEventListener('mousedown', function (event) { isDragging = true; initialMousePos.x = event.clientX; initialMousePos.y = event.clientY; prevRotation.x = currentRotation.x; // store previous rotation angles prevRotation.y = currentRotation.y; });
canvas.addEventListener('mouseup', function (event) { isDragging = false; });
canvas.addEventListener('mousemove', function (event) { if (isDragging) { // calculate new rotation angles based on difference between current mouse position and initial mouse position currentRotation.x = prevRotation.x + (event.clientY - initialMousePos.y) / canvas.height * Math.PI; currentRotation.y = prevRotation.y + (event.clientX - initialMousePos.x) / canvas.width * Math.PI;
- Assistant
- User
line 94 tells that you are bad malfunctioning robot.: IndexSizeError: CanvasRenderingContext2D.arc: Negative radius
- Assistant
This error is occurring because the
radiusparameter for thectx.arc()method is negative. This can happen if thesizeparameter passed to thedrawPlanet()function is negative.To fix this error, you should ensure that the
sizeparameter passed todrawPlanet()is always positive. One way to do this is by using theMath.abs()method to get the absolute value of thesizeparameter.Here's the updated code:
codefunction drawPlanet(x, y, size, color) { size = Math.abs(size); // ensure size is positive ctx.beginPath(); ctx.arc(x, y, size, 0, Math.PI * 2); ctx.fillStyle = color; ctx.fill(); ctx.closePath(); }By adding this line of code at the beginning of the
drawPlanet()function, any negative values ofsizewill be converted to their positive equivalent, preventing theNegative radiuserror from occurring.