Skip to content

Commit

Permalink
fix zoom functionality and update Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
modouaicha023 committed Nov 19, 2023
1 parent c1d4664 commit d73bca3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 39 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ The game follows simple rules:

### Zooming

- There's a function `updateZoom` to handle zoom changes.
- Mouse wheel events are used to zoom in and out, and the browser's default zoom is disabled.
- There's a function `updateZoom` to handle zoom changes in function of the inputZoom.

## Functionality

Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h1 id="generation"></h1>
<input type="number" min="10" max="50" name="rateRandomCells" id="rateRandomCells" value="30">
</label>
<label for="zoom"> Zoom
<input type="range" name="zoom" id="zoom" min="5" max="100" step="1">
<input type="range" name="zoom" id="zoom" min="10" max="100" step="10" value="50">
</label>
</form>
<div id="command">
Expand Down
53 changes: 17 additions & 36 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ function createCellsElements(rows, cols, allCells) {
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const cell = document.createElement('div');
cell.style.width = `${inputZoom.value}px`;
cell.style.height = `${inputZoom.value}px`;
cell.className = 'cell';
cell.id = `${i}-${j}`;
gridContainer.appendChild(cell);
Expand All @@ -63,8 +65,8 @@ function newRound() {
createCellsElements(inputRows.value, inputColunms.value, allCells);

// Step3 is update the width and height of the gridContainer element
gridContainer.style.gridTemplateRows = `repeat(${inputRows.value}, 20px)`;
gridContainer.style.gridTemplateColumns = `repeat(${inputColunms.value}, 20px)`;
gridContainer.style.gridTemplateRows = `repeat(${inputRows.value}, ${inputZoom.value}px)`;
gridContainer.style.gridTemplateColumns = `repeat(${inputColunms.value}, ${inputZoom.value}px)`;

//this function set to 1 a random case in the matrix
function setRandomAliveCell(Nrows, Ncolumns, percentageAlive) {
Expand Down Expand Up @@ -177,6 +179,8 @@ function newRound() {
//this function initialize the grid
function handleClearGrid() {
handleStopGame();
initZoom();
updateZoom();
cellsMatrix = initMatrix(inputRows.value, inputColunms.value);
updateCellClasses();
iniGeneration();
Expand All @@ -202,6 +206,8 @@ function newRound() {
//and after run a new round with new Row or/and Col
function updateRowsCols() {
clearInterval(intervalId);
initZoom();
updateZoom();
intervalId = null;
if (inputRows.value > 0 && inputColunms.value > 0)
newRound();
Expand Down Expand Up @@ -245,7 +251,16 @@ function updateZoom() {
});

}
function initZoom() {
inputZoom.value = 50;
// gridContainer.style.gridTemplateRows = `repeat(${inputRows.value}, ${inputZoom.value}px)`;
// gridContainer.style.gridTemplateColumns = `repeat(${inputColunms.value}, ${inputZoom.value}px)`;
// cellElements.forEach((cellElement) => {
// cellElement.style.width = `${inputZoom.value}px`;
// cellElement.style.height = `${inputZoom.value}px`;
// });

}
//
inputZoom.addEventListener('input', updateZoom);

Expand All @@ -255,38 +270,4 @@ window.addEventListener('load', function () {
});


function zoomIn() {
const currentZoom = parseFloat(inputZoom.value);
const newZoom = currentZoom + 5;
inputZoom.value = newZoom;
updateZoom();
gridContainer.style.cursor = 'zoom-in';
}

function zoomOut() {
const currentZoom = parseFloat(inputZoom.value);
const newZoom = Math.max(1, currentZoom - 5);
inputZoom.value = newZoom;
updateZoom();
gridContainer.style.cursor = 'zoom-out';
}

function handleMouseWheel(event) {
if (event.ctrlKey) {
event.preventDefault();
const zoomDirection = event.deltaY < 0 ? "in" : "out";
if (zoomDirection === "in") {
zoomIn();
} else {
zoomOut();
}
}

document.body.style.zoom = 'reset';

}

window.addEventListener("wheel", handleMouseWheel);


/* I really finish this project touti ma abandonné 😭😭 */

0 comments on commit d73bca3

Please sign in to comment.