-
Notifications
You must be signed in to change notification settings - Fork 0
/
grixel-script.js
118 lines (112 loc) · 2.93 KB
/
grixel-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
109
110
111
112
113
114
115
116
117
118
// declare global variables
let defaultSliderValue = document.getElementById("sliderRange");
defaultSliderValue.value = 50;
let gridContainer = document.getElementById("grid-container");
let gridSize = 50;
let colorScheme = "pastel";
let penUp = true;
// console.log(penUp);
let gridArea = document.getElementById("grid-container");
gridArea.addEventListener("click", function () {
togglePen();
});
// set up touch event listeners for mobile
// gridArea.addEventListener("touchstart", function () {
// penUp = false;
// });
console.log(penUp);
addDivs(gridSize);
var slider = document.getElementById("sliderRange");
var output = document.getElementById("gridSize");
output.innerHTML = slider.value + " x " + slider.value;
// slider output
slider.oninput = function () {
output.innerHTML = this.value + " x " + this.value;
gridSize = this.value;
addDivs(gridSize);
};
// add divs and set class
function addDivs(gridSize) {
gridContainer.innerHTML = "";
setGridSize(gridSize);
// console.log("gridsize = " + gridSize);
for (let i = 1; i <= gridSize * gridSize; i++) {
let node = document.createElement("DIV");
node.id = i;
node.classList.add("gridItem");
node.onmouseover = function () {
color(node);
};
// this is not working correctly. Only the first div touched on touchscreen lights up. After that, nothing
node.ontouchstart = function () {
penup = false;
color(node);
};
gridContainer.appendChild(node);
// console.log(gridContainer);
}
}
// set grid size
function setGridSize(gridSize) {
let gridRatio = (1 / gridSize) * 100 + "%";
gridContainer.style.gridTemplateColumns =
"repeat(" + gridSize + ", " + gridRatio + " )";
gridContainer.style.gridAutoRows = gridRatio;
// console.log(gridContainer);
return;
}
// set background color
function color(node) {
console.log(penUp);
if (penUp === false) {
if (colorScheme === "pastel") {
let rnd = Math.floor(Math.random() * 5);
switch (rnd) {
case 0:
rnd = "#EFA4F1";
break;
case 1:
rnd = "#ffefa1";
break;
case 2:
rnd = "#94ebcd";
break;
case 3:
rnd = "#78c4d4";
break;
case 4:
rnd = "#9a8194";
break;
default:
rnd = "blue";
break;
}
// console.log(rnd);
node.style.backgroundColor = rnd;
} else if (colorScheme === "white") {
node.style.backgroundColor = "white";
} else {
node.style.backgroundColor = document.getElementById("favcolor").value;
}
}
}
function setScheme(scheme) {
switch (scheme) {
case "pastel":
colorScheme = "pastel";
break;
case "erase":
colorScheme = "white";
break;
// custom incomplete
case "custom":
colorScheme = "custom";
console.log(colorScheme);
break;
default:
break;
}
}
function togglePen() {
penUp = !penUp;
}