Skip to content

Commit

Permalink
add zoom,genenration, functionality,
Browse files Browse the repository at this point in the history
  • Loading branch information
modouaicha023 committed Nov 19, 2023
1 parent de6964d commit 4f78316
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 10 deletions.
2 changes: 1 addition & 1 deletion headerTextStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,5 @@ function updateCellClassStyleText() {
});
}

let intervalStyleText = 20 * 1000; // 5 second
let intervalStyleText = 10 * 1000; // 1 second
setInterval(UpdateCellStyleText, intervalStyleText);
6 changes: 5 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<body id="body">
<div class="container">
<div id="game-of-life"></div>
<div class="grid"></div>
<h1 id="generation"></h1>
<div class="grid zoom-area"></div>
<form id="formControl">
<label for="rows"> Rows
<input type="number" min="1" max="200" name="rows" id="rows" value="50">
Expand All @@ -26,6 +27,9 @@
<label for="rateRandomCells"> Rate Random Cells (%)
<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">
</label>
</form>
<div id="command">
<button id="reset">
Expand Down
75 changes: 69 additions & 6 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ const inputSpeed = document.getElementById("speed");
const inputRows = document.getElementById("rows");
const inputColunms = document.getElementById("columns");
const inputRateRandomCells = document.getElementById("rateRandomCells");
const inputZoom = document.getElementById('zoom');
let numberOfGeneration = document.getElementById('generation');

let interval = inputSpeed.value * 1000;
let intervalId;
let generation = 0;
numberOfGeneration.innerHTML = generation;
let cellsMatrix = [];
let allCells = [];

Expand All @@ -19,6 +23,11 @@ function initMatrix(initRows, initCols) {
return Array.from({ length: initRows }, () => Array.from({ length: initCols }).fill(0));
}

function iniGeneration() {
generation = 0;
numberOfGeneration.innerHTML = generation;
}

//this function update the state of all the cell depends of their value in the Matrix ---> cellMatrix
function updateCellClasses() {
allCells.forEach((cell) => {
Expand All @@ -45,7 +54,7 @@ function createCellsElements(rows, cols, allCells) {
}

//this function init a new Round of a game depends of the value of the rows and cols ---> inputRows && inputCols
function newRound(r, l) {
function newRound() {

// Step1 is to initialize the matrix with each case the value 0
cellsMatrix = initMatrix(inputRows.value, inputColunms.value);
Expand Down Expand Up @@ -141,6 +150,7 @@ function newRound(r, l) {
cellsMatrix = updatedMatrix;
updateCellClasses();
generation++;
numberOfGeneration.innerHTML = generation;
}


Expand Down Expand Up @@ -169,13 +179,15 @@ function newRound(r, l) {
handleStopGame();
cellsMatrix = initMatrix(inputRows.value, inputColunms.value);
updateCellClasses();
iniGeneration();
}

function handleRandomCell() {
handleStopGame();
cellsMatrix = initMatrix(inputRows.value, inputColunms.value);
setRandomAliveCell(inputRows.value, inputColunms.value, inputRateRandomCells.value)
updateCellClasses();
iniGeneration();
}

//add "click" Event for the three button
Expand All @@ -192,19 +204,20 @@ function updateRowsCols() {
clearInterval(intervalId);
intervalId = null;
if (inputRows.value > 0 && inputColunms.value > 0)
newRound(inputRows.value, inputColunms.value);
newRound();
iniGeneration();
}

//add "change" Event on the Rows input and Columns input
// for each, we call the the function ---> updateRowsCols() <--- for a new round.
//I launch a new round because if the row or the columns change, the matrix will change
// and if the matrix, the the position of all the elements changes, and their neigbors of course
//in this case we should reset the previous game and run a new round with the new row or/and cols.
inputRows.addEventListener("change", () => {
inputRows.addEventListener("input", () => {
updateRowsCols();
});

inputColunms.addEventListener("change", () => {
inputColunms.addEventListener("input", () => {
updateRowsCols()
});

Expand All @@ -213,17 +226,67 @@ inputColunms.addEventListener("change", () => {
//we first stop the game
//after we check if the value is > 0
//if its true we update the value of the speed
inputSpeed.addEventListener("change", () => {
inputSpeed.addEventListener("input", () => {
clearInterval(intervalId);
intervalId = null;
if (inputSpeed.value > 0) {
interval = inputSpeed.value * 1000;
}
});

function updateZoom() {
console.log(inputZoom.value);
const cellElements = document.querySelectorAll(".cell");
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);

//this last lauch a new round when the page is load
window.addEventListener('load', function () {
newRound(inputRows.value, inputColunms.value);
newRound();
});


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é 😭😭 */
43 changes: 43 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,23 @@ body {
}


#generation{
position: fixed;
top: 20px;
right:50px
;
color: black;
background-color: #9dff00a8;
padding: 10px;

font-size: larger;
font-weight: bolder;
font-family: 'Arbutus', serif;

border: 5px solid #000000;
border-radius: 10px;

}
.grid {
padding: 10px;
display: grid;
Expand All @@ -58,6 +74,8 @@ body {
width: 20px;
height: 20px;
border: 1px solid #ccc;
cursor: pointer;

}

.alive {
Expand Down Expand Up @@ -180,6 +198,26 @@ body {
}


#zoom {
background: black !important;
border: solid 2px #82CFD0;
border-radius: 8px;
height: 7px;
width: 100px !important;
outline: none;
transition: 450ms ease-in;
-webkit-appearance: none;
}

#zoom::-webkit-slider-thumb {
width: 15px;
height: 15px;
border-radius: 50%;
-webkit-appearance: none;
cursor: ew-resize;
background: #9EFF00;
}

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
Expand All @@ -188,4 +226,9 @@ input::-webkit-inner-spin-button {
/* Firefox */
input[type=number] {
-moz-appearance: textfield;
}


.zoom-area {
transition: .6s ease;
}
4 changes: 2 additions & 2 deletions style.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ function updateCellClassStyle() {
});
}

// let intervalStyle = .5 * 1000; // 1 second
// setInterval(UpdateCellStyle, intervalStyle);
let intervalStyle = .5 * 1000; // 1 second
setInterval(UpdateCellStyle, intervalStyle);
UpdateCellStyle()

0 comments on commit 4f78316

Please sign in to comment.