odin-etch-a-sketch/script.js

211 lines
4.6 KiB
JavaScript
Raw Normal View History

2023-11-09 18:20:28 -07:00
const container = document.querySelector(".container");
const conWidth = container.clientWidth;
const grid = document.createElement("div");
2023-11-09 20:15:53 -07:00
const gridSlider = document.querySelector("#gridSlider");
2023-11-10 13:28:36 -07:00
const buttons = document.querySelectorAll(".pen");
2023-11-10 12:51:07 -07:00
const colorPicker = document.querySelector("#custom-color");
2023-11-10 13:28:36 -07:00
const reset = document.querySelector("#reset");
const backColorPicker = document.querySelector("#back-col-picker")
2023-11-10 17:32:29 -07:00
let colorValue;
let numGrid; //= gridSlider.value;
let sliderLabel = document.querySelector("#sliderLabel");
gridSlider.defaultValue = 16;
2023-11-10 18:39:02 -07:00
//the next few variables are set to be used to test if
//a given button has been clicked
2023-11-10 07:49:31 -07:00
let isRgb;
2023-11-10 08:07:36 -07:00
let isTint;
let isShade;
let isOpaque;
let isTrans;
2023-11-10 12:51:07 -07:00
let isBlack;
let isWhite;
let isErase;
2023-11-09 20:15:53 -07:00
2023-11-10 17:32:29 -07:00
getGridDimensions();
2023-11-10 13:28:36 -07:00
buttons.forEach((button) => {
button.addEventListener ("click", changeColor);
});
colorPicker.addEventListener("input", changeColor);
backColorPicker.addEventListener("input", pickBackground);
reset.addEventListener("click", resetGrid);
gridSlider.addEventListener("input", getGridDimensions);
2023-11-10 13:28:36 -07:00
2023-11-09 18:20:28 -07:00
function pickBackground() {
container.style.backgroundColor = backColorPicker.value;
}
function resetGrid() {
gridSlider.value = gridSlider.defaultValue;
getGridDimensions();
}
function getGridDimensions() {
container.style.backgroundColor = "#ffffff";
backColorPicker.value = "#ffffff";
colorPicker.value = "#000000";
colorValue = "black";
numGrid = gridSlider.value;
while (container.firstChild) {
container.removeChild(container.lastChild);
}
createGrid();
}
2023-11-09 18:20:28 -07:00
function createGrid() {
2023-11-10 18:39:02 -07:00
numGrid = gridSlider.value;
2023-11-09 18:20:28 -07:00
const gridArea = numGrid ** 2;
const gridWidth = 100 / numGrid;
2023-11-09 15:14:02 -07:00
2023-11-10 18:39:02 -07:00
sliderLabel.textContent = numGrid;
2023-11-09 16:31:23 -07:00
grid.classList.add("grid");
grid.style.width = gridWidth + '%';
grid.style.height = gridWidth + '%';
grid.style.opacity = 0.9;
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
}
2023-11-09 16:31:23 -07:00
2023-11-09 20:15:53 -07:00
function colorGrid() {
2023-11-10 07:49:31 -07:00
if (isRgb) {
2023-11-10 12:51:07 -07:00
this.style.backgroundColor = randomizeColor();
2023-11-10 08:26:39 -07:00
} else if (isTint) {
2023-11-10 12:51:07 -07:00
this.style.backgroundColor = tintColor(this.style.backgroundColor);
2023-11-10 09:20:46 -07:00
} else if (isShade) {
2023-11-10 12:51:07 -07:00
this.style.backgroundColor = shadeColor(this.style.backgroundColor);
} else if (isOpaque){
this.style.opacity = makeOpaque(this.style.opacity);
} else if (isTrans) {
this.style.opacity = makeTransparent(this.style.opacity);
2023-11-10 12:51:07 -07:00
} else if (isBlack) {
this.style.backgroundColor = "rgb(0, 0, 0)";
} else if (isWhite) {
this.style.backgroundColor = "rgb(255, 255, 255)";
} else if (isErase) {
this.style.backgroundColor = "";
2023-11-10 07:49:31 -07:00
} else {
this.style.backgroundColor = colorPicker.value;
2023-11-10 07:49:31 -07:00
}
}
function changeColor() {
const buttonID = this.id;
2023-11-10 07:49:31 -07:00
if (buttonID === "rgbColor") {
isRgb = true;
} else {
isRgb = false;
}
2023-11-10 08:26:39 -07:00
if (buttonID === "tint") {
isTint = true;
} else {
isTint = false
}
2023-11-10 09:20:46 -07:00
if (buttonID === "shade") {
isShade = true;
} else {
isShade = false
}
if (buttonID === "opacity") {
isOpaque = true;
} else {
isOpaque = false
}
if (buttonID === "trans") {
isTrans = true;
} else {
isTrans = false
}
2023-11-10 12:51:07 -07:00
if (buttonID === "whiteColor") {
isWhite = true;
} else {
isWhite = false
}
if (buttonID === "blackColor") {
isBlack = true;
} else {
isBlack = false
}
if (buttonID === "erase") {
isErase = true;
} else {
isErase = false
}
2023-11-09 20:15:53 -07:00
}
2023-11-09 16:31:23 -07:00
2023-11-10 08:26:39 -07:00
function randomizeColor() {
2023-11-09 23:17:42 -07:00
let r = Math.floor(Math.random() * 255);
let g = Math.floor(Math.random() * 255);
let b = Math.floor(Math.random() * 255);
return `rgb(${r},${g},${b})`;
2023-11-10 09:20:46 -07:00
}
2023-11-10 09:20:46 -07:00
function tintColor(value) {
let num = value.match(/\d+/g);
2023-11-10 10:22:08 -07:00
r = +num[0] + (0.25 * (255 - +num[0]));
g = +num[1] + (0.25 * (255 - +num[1]));
b = +num[2] + (0.25 * (255 - +num[2]));
2023-11-10 09:20:46 -07:00
return`rgb(${r},${g},${b})`;
}
2023-11-10 09:20:46 -07:00
function shadeColor(value) {
let num = value.match(/\d+/g);
2023-11-10 10:10:41 -07:00
r = +num[0] * .75;
g = +num[1] * .75;
b = +num[2] * .75;
2023-11-09 23:17:42 -07:00
2023-11-10 09:20:46 -07:00
return`rgb(${r},${g},${b})`;
2023-11-09 23:17:42 -07:00
}
function makeOpaque(value) {
let num = +value;
if (num < 1){
num += .1
}
return num;
}
function makeTransparent(value) {
let num = +value;
if (num > 0){
num -= .1
}
return num;
2023-11-10 13:28:36 -07:00
}