<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport"…
- User
<script> canvas = document.getElementById("canvas"); ctx = canvas.getContext("2d"); let w = 800 let h = 800 const cells = []; class cell { constructor(color, x, y) { this.color = color; this.x = x; this.y = y; } drawRect() { ctx.strokeStyle = "black"; ctx.fillStyle = this.color; ctx.fillRect(this.x, this.y, 50, 50) ctx.strokeRect(this.x, this.y, 50, 50); } } for (i = 0; i < 16; i++) { for (j = 0; j < 16; j++) { cells.push(new cell("green", 0 + 50 * i, 0 + 50 * j)); cells[cells.length - 1].drawRect(); } } canvas.addEventListener('click', function() { const x = event.clientX; // получаем координату X мыши const y = event.clientY; // получаем координату Y мыши console.log(`Координаты мыши: x=${x}, y=${y}`); // выводим координаты мыши в консоль }) </script>add when i click at cell its change color
- Assistant
You can modify your existing JavaScript code to change the color of a clicked cell. To achieve this, you'll need to calculate which cell was clicked based on the mouse coordinates and then update its color accordingly.
Here's how you can do that:
- Calculate the clicked cell's row and column based on the mouse coordinates.
- Update the color of the clicked cell and redraw it.
Below is the updated code that implements this functionality:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Strategy Because Why not?</title> </head> <style> #canvas { border: 3px solid black; } body { background-color: #4a4a4a; } </style> <body> <canvas id="canvas" width="800px" height="800px"></canvas> <script> const canvas = document.getElementById("canvas"); const ctx = canvas.getContext("2d"); let w = 800; let h = 800; const cells = []; class Cell { constructor(color, x, y) { this.color = color; this.x = x; this.y = y; } drawRect() { ctx.strokeStyle = "black"; ctx.fillStyle = this.color; ctx.fillRect(this.x, this.y, 50, 50); ctx.strokeRect(this.x, this.y, 50, 50); } changeColor(newColor) { this.color = newColor; this.drawRect(); // Redraw the rectangle with the new color } } // Initialize cells for (let i = 0; i < 16; i++) { for (let j = 0; j < 16; j++) { cells.push(new Cell("green", 0 + 50 * i, 0 + 50 * j)); cells[cells.length - 1].drawRect(); } } canvas.addEventListener('click', function(event) { const x = event.clientX - canvas.offsetLeft; // get X coordinate relative to canvas const y = event.clientY - canvas.offsetTop; // get Y coordinate relative to canvas // Determine the column and row clicked const col = Math.floor(x / 50); const row = Math.floor(y / 50); // Get the clicked cell const index = col + row * 16; // Change the color of the clicked cell if (cells[index]) { cells[index].changeColor(getRandomColor()); } }); // Function to generate a random color function getRandomColor() { const letters = '0123456789ABCDEF'; let color = '#'; for (let i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } return color; } </script> </body> </html>Changes Made:
- Refactored
celltoCell- Class names should start with an uppercase letter in JavaScript. - Adjusted mouse event coordinates - Corrected the coordinates to be relative to the canvas.
- Added
changeColormethod - This method changes the color of the cell and redraws it. - Redraw on click - When a cell is clicked, its color changes to a randomly generated color.
Now, when you click on any of the cells, it will change to a random color.
- User
dont use rows and collumns because i will use procedural generation later
- Assistant
If you want to avoid using hardcoded rows and columns and plan to implement procedural generation later, we can calculate the index directly based on the mouse coordinates, while still keeping track of the cell's position using its
xandycoordinates.Here’s how you can modify your existing code to achieve that:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Strategy Because Why not?</title> </head> <style> #canvas { border: 3px solid black; } body { background-color: #4a4a4a; } </style> <body> <canvas id="canvas" width="800px" height="800px"></canvas> <script> const canvas = document.getElementById("canvas"); const ctx = canvas.getContext("2d"); let cellSize = 50; const cells = []; class Cell { constructor(color, x, y) { this.color = color; this.x = x; this.y = y; } drawRect() { ctx.strokeStyle = "black"; ctx.fillStyle = this.color; ctx.fillRect(this.x, this.y, cellSize, cellSize); ctx.strokeRect(this.x, this.y, cellSize, cellSize); } changeColor(newColor) { this.color = newColor; this.drawRect(); // Redraw the rectangle with the new color } } // Initialize cells with procedural generation for (let i = 0; i < (800 / cellSize); i++) { for (let j = 0; j < (800 / cellSize); j++) { cells.push(new Cell("green", cellSize * i, cellSize * j)); cells[cells.length - 1].drawRect(); } } canvas.addEventListener('click', function(event) { const rect = canvas.getBoundingClientRect(); const x = event.clientX - rect.left; // get X coordinate relative to canvas const y = event.clientY - rect.top; // get Y coordinate relative to canvas // Determine which cell was clicked based on mouse coordinates const clickedCell = cells.find(cell => x >= cell.x && x < cell.x + cellSize && y >= cell.y && y < cell.y + cellSize ); // Change the color of the clicked cell if (clickedCell) { clickedCell.changeColor(getRandomColor()); } }); // Function to generate a random color function getRandomColor() { const letters = '0123456789ABCDEF'; let color = '#'; for (let i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } return color; } </script> </body> </html>Key Changes and Features:
- Removed hard-coded row/column logic - The cells are generated based on the canvas size and cell size, allowing for flexible procedural generation later.
- Dynamic cell detection - The
findmethod is used to determine which cell was clicked based on the mouse coordinates and the cell's bounds. - Cell size configurable - The size of each cell can easily be changed by modifying the
cellSizevariable.
With this setup, you can easily adjust the cell generation logic to incorporate your procedural generation in the future without being constrained by fixed rows and columns.