odin-etch-a-sketch/script.js

47 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-11-09 18:20:28 -07:00
let numGrid = 16; //will use this later to prompt user
//need to figure out the wrapping issue
const container = document.querySelector(".container");
const conWidth = container.clientWidth;
const conHeight = container.clientHeight
const grid = document.createElement("div");
2023-11-09 20:15:53 -07:00
const gridSlider = document.querySelector("#gridSlider");
2023-11-09 18:20:28 -07:00
function createGrid() {
const gridArea = numGrid ** 2;
const gridWidth = conWidth / numGrid;
const gridHeight = conHeight / numGrid;
2023-11-09 15:14:02 -07:00
2023-11-09 16:31:23 -07:00
grid.classList.add("grid");
grid.style.width = gridWidth + 'px';
grid.style.height = gridHeight + 'px';
2023-11-09 15:14:02 -07:00
2023-11-09 16:31:23 -07:00
for (let i = 0; i < gridArea; i++){
container.appendChild(grid.cloneNode(true));
}
2023-11-09 18:20:28 -07:00
const gridItems = document.querySelectorAll(".grid");
gridItems.forEach((item) => {
2023-11-09 18:26:41 -07:00
item.addEventListener("mouseover", colorGrid);
2023-11-09 18:20:28 -07:00
});
2023-11-09 20:15:53 -07:00
gridSlider.addEventListener("change", getGridDimensions);
2023-11-09 18:20:28 -07:00
2023-11-09 15:14:02 -07:00
}
2023-11-09 16:31:23 -07:00
2023-11-09 20:15:53 -07:00
function getGridDimensions() {
numGrid = gridSlider.value;
while (container.firstChild) {
container.removeChild(container.lastChild);
}
console.log(numGrid);
createGrid();
}
2023-11-09 16:31:23 -07:00
2023-11-09 20:15:53 -07:00
function colorGrid() {
this.style.background = "blue";
}
2023-11-09 16:31:23 -07:00
2023-11-09 20:15:53 -07:00
createGrid();