-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
108 lines (88 loc) · 3.03 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"use strict";
const defaultColor = "#ff6b6b";
const padWidth = 800;
let gridContainer;
let isDrawing = false;
let cellNum = 64;
let cellColor = defaultColor;
const body = document.body;
const controlsContainer = document.createElement("div");
controlsContainer.classList.toggle("controls-container");
controlsContainer.style.margin = "20px auto";
body.appendChild(controlsContainer);
const button = document.createElement("button");
controlsContainer.appendChild(button);
button.style.width = "160px";
button.style.height = "40px";
button.textContent = "Pad Size";
const colorPickContainer = document.createElement("div");
colorPickContainer.classList.toggle("color-container");
controlsContainer.appendChild(colorPickContainer);
const colorPot = document.createElement("input");
colorPot.type = "color";
colorPot.id = "color";
colorPot.type = "color";
colorPot.value = cellColor;
const colorLabel = document.createElement("label");
colorLabel.for = "color";
colorLabel.textContent = "Color:";
colorPickContainer.appendChild(colorLabel);
colorPickContainer.appendChild(colorPot);
gridContainer = addGridContainer();
let cells = addCells(padWidth, cellNum);
button.addEventListener("click", function () {
gridContainer.remove();
gridContainer = addGridContainer();
cellNum = prompt("How many cells on one side?");
cells = addCells(padWidth, cellNum);
});
colorPot.addEventListener("change", function (e) {
cellColor = e.target.value;
console.log(e.target.value);
cells.forEach((cell) => addClickEvent(cell, cellColor, 100));
});
function addGridContainer() {
const gridContainer = document.createElement("div");
body.appendChild(gridContainer);
gridContainer.style.margin = "0 auto";
gridContainer.style.width = `${padWidth}px`;
gridContainer.style.height = `${padWidth}px`;
gridContainer.style.border = "2px solid #555";
return gridContainer;
}
function addClickEvent(square, color, brightness) {
const addColor = function () {
if (isDrawing) {
brightness = makeDarker(brightness);
square.style.backgroundColor = `color-mix(in srgb, ${color} ${brightness}%, black)`;
}
};
square.addEventListener("mousedown", () => (isDrawing = true));
square.addEventListener("mouseenter", addColor);
square.addEventListener("mouseup", () => (isDrawing = false));
}
function addCells(padWidth, cellNum) {
const cellSize = padWidth / cellNum;
const cellArray = [];
for (let i = 0; i < cellNum; i++) {
let rowContainer = document.createElement("div");
rowContainer.classList.toggle("grid-container");
for (let i = 0; i < cellNum; i++) {
let square = document.createElement("div");
square.classList.toggle("cell");
rowContainer.appendChild(square);
square.style.width = `${cellSize}px`;
square.style.height = `${cellSize}px`;
cellArray.push(square);
let brightness = 100;
addClickEvent(square, cellColor, brightness);
}
gridContainer.appendChild(rowContainer);
}
return cellArray;
}
function makeDarker(br) {
br = br - 10;
if (br < 0) br = 0;
return br;
}