diff options
author | elioat <elioat@tilde.institute> | 2024-07-06 09:00:24 -0400 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2024-07-06 09:00:24 -0400 |
commit | c8dab2d52ec65d636e23894f7bab66264d1bc9d7 (patch) | |
tree | 5e4ed8045f6b51968430029ca9b51c8c0d526e74 | |
parent | 92dfaae864e3538319d5f8fbe10585d32f502f2c (diff) | |
download | tour-c8dab2d52ec65d636e23894f7bab66264d1bc9d7.tar.gz |
*
-rw-r--r-- | html/life.html | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/html/life.html b/html/life.html index 9d2ecf3..7d93a0b 100644 --- a/html/life.html +++ b/html/life.html @@ -390,11 +390,37 @@ let lastRenderTime = 0; let isPlaying = false; - function drawGrid() { + canvas.addEventListener('mousemove', handleMouseMove); + canvas.addEventListener('mouseout', handleMouseOut); + + let hoveredCell = null; // stores what cell is hovered over + + // Find the cell being hovered hover and and redraw the grid + function handleMouseMove(event) { + const rect = canvas.getBoundingClientRect(); + const x = event.clientX - rect.left; + const y = event.clientY - rect.top; + const gridX = Math.floor(x / cellSize); + const gridY = Math.floor(y / cellSize); + hoveredCell = { x: gridX, y: gridY }; + drawGrid(hoveredCell); // Redraw grid with highlighted cell + } + + // remove highlight when mouse leaves the cell + function handleMouseOut() { + hoveredCell = null; + drawGrid(); + } + + function drawGrid(hoveredCell = null) { ctx.clearRect(0, 0, canvas.width, canvas.height); for (let x = 0; x < game.rows; x++) { for (let y = 0; y < game.cols; y++) { - ctx.fillStyle = game.grid[x][y] ? '#2d2d2d' : 'beige'; + if (hoveredCell && hoveredCell.x === y && hoveredCell.y === x) { + ctx.fillStyle = '#74FFEF'; + } else { + ctx.fillStyle = game.grid[x][y] ? '#2d2d2d' : 'beige'; + } ctx.fillRect(y * cellSize, x * cellSize, cellSize, cellSize); ctx.strokeStyle = 'lightgrey'; ctx.strokeRect(y * cellSize, x * cellSize, cellSize, cellSize); |