odin-etch-a-sketch/script.js

271 lines
6.3 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-11 15:09:20 -07:00
gridSlider.defaultValue = 16;
const sliderLabel = document.querySelector("#sliderLabel");
const rangeKnob = document.querySelector("#range-knob");
2023-11-11 15:09:20 -07:00
let numGrid;
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 17:32:29 -07:00
let colorValue;
2023-11-11 15:09:20 -07:00
const backColorPicker = document.querySelector("#back-col-picker");
const reset = document.querySelector("#reset");
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 19:21:39 -07:00
resetGrid();
2023-11-10 13:28:36 -07:00
buttons.forEach((button) => {
button.addEventListener ("click", changeColor);
});
colorPicker.addEventListener("input", changeColor);
2023-11-10 19:28:05 -07:00
colorPicker.addEventListener("change", changeColor);
backColorPicker.addEventListener("input", pickBackground);
reset.addEventListener("click", resetGrid);
2023-11-12 10:03:16 -07:00
gridSlider.addEventListener("input", getGridDimensions);
2023-11-11 07:39:44 -07:00
gridSlider.addEventListener("change", getGridDimensions);
2023-11-11 15:09:20 -07:00
rangeKnob.addEventListener("mousedown", (e) => {
document.addEventListener("mousemove", rotateKnob);
2023-11-11 15:09:20 -07:00
document.addEventListener("mouseup", () => {
document.removeEventListener("mousemove", rotateKnob);
});
});
2023-11-10 19:23:03 -07:00
function resetGrid() {
gridSlider.value = gridSlider.defaultValue;
2023-11-11 23:45:04 -07:00
container.style.backgroundColor = "rgb(180, 254, 235)";
backColorPicker.value = "#b4feeb";
colorPicker.value = "#000000";
colorValue = "black";
2023-11-11 18:56:38 -07:00
rangeKnob.style.transform = `rotate(${gridSlider.value}deg)`;
2023-11-10 19:21:39 -07:00
getGridDimensions();
}
2023-11-10 19:23:03 -07:00
2023-11-11 16:26:33 -07:00
function rotateKnob(e) {
const knobBounds = rangeKnob.getBoundingClientRect();
const knobCenter = {
2023-11-12 10:03:16 -07:00
x: knobBounds.left + knobBounds.width / 2,
y: knobBounds.top + knobBounds.height / 2
};
let angle = Math.atan2(e.clientX - knobCenter.x, -(e.clientY - knobCenter.y)) * (180 / Math.PI);
2023-11-11 16:26:33 -07:00
rangeKnob.style.transform = `rotate(${angle}deg)`;
2023-11-11 17:30:17 -07:00
updateGridSlider(angle);
2023-11-11 16:26:33 -07:00
}
2023-11-12 10:03:16 -07:00
2023-11-11 17:30:17 -07:00
function updateGridSlider(angle) {
const maxRotation = 360;
const minSliderVal = gridSlider.min;
const maxSliderVal = gridSlider.max;
const sliderVal = (angle + maxRotation) % maxRotation;
const normalizedValue = (sliderVal / maxRotation) * (maxSliderVal - minSliderVal) + minSliderVal;
2023-11-11 16:26:33 -07:00
2023-11-11 17:30:17 -07:00
gridSlider.value = normalizedValue;
getGridDimensions(normalizedValue);
}
2023-11-11 16:26:33 -07:00
2023-11-11 17:30:17 -07:00
function getGridDimensions(val) {
//numGrid = gridSlider.value;
numGrid = val;
while (container.firstChild) {
container.removeChild(container.lastChild);
}
2023-11-10 19:21:39 -07:00
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-10 20:10:38 -07:00
grid.classList.add("grid", "grid-on");
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");
2023-11-10 20:10:38 -07:00
const gridToggle = document.querySelector("#gridToggle");
2023-11-09 18:20:28 -07:00
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-10 20:10:38 -07:00
gridToggle.addEventListener("click", () => {
gridItems.forEach((item) => {
toggleGridView(item);
});
});
}
function toggleGridView(gridItem) {
if (gridItem.classList.contains("grid-on")){
gridItem.classList.remove("grid-on");
gridItem.classList.add("grid-off");
} else {
gridItem.classList.remove("grid-off");
gridItem.classList.add("grid-on");
}
}
function pickBackground() {
container.style.backgroundColor = backColorPicker.value;
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
}