-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
163 lines (130 loc) · 5.13 KB
/
scripts.js
File metadata and controls
163 lines (130 loc) · 5.13 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const container = document.querySelector('#container');
const resetBtn = document.querySelector('#resetBtn');
const mouseModeSelect = document.querySelector('#mousemode');
const colorModeSelect = document.querySelector('#colormode');
// variable that knows whether the mouse is clicked or not
let mouseDown = 0;
document.body.onmousedown = function() {
++mouseDown;
}
document.body.onmouseup = function() {
--mouseDown;
}
let userNum = 10;
let blackness = 0;
//function that changes the number of repeating gridBoxes
function changeGridLen(gridnum) {
document.getElementById("container").style.gridTemplateColumns = "repeat(" + gridnum + ", " + ((1 / gridnum)*100) + "%)";
document.getElementById("container").style.gridTemplateRows = "repeat(" + gridnum + ", " + ((1 / gridnum)*100) + "%)";
}
//function that takes a string and style of the grid to increase amount of whiteness
function LightenDarkenColor(col) {
let usePound = false;
if (col[0] == "#") {
col = col.slice(1);
usePound = true;
}
let num = parseInt(col,16);
let r = (num >> 16) + 50;
if (r > 255) r = 255;
else if (r < 0) r = 0;
let b = ((num >> 8) & 0x00FF) + 50;
if (b > 255) b = 255;
else if (b < 0) b = 0;
let g = (num & 0x0000FF) + 50;
if (g > 255) g = 255;
else if (g < 0) g = 0;
return (usePound?"#":"") + (g | (b << 8) | (r << 16)).toString(16);
}
// create a x*x grid of divs styled as boxes. **Mousever to draw**
function sketchGridHover(gridSize) {
for (i=0; i<(gridSize*gridSize); i++) {
const gridBox = document.createElement('div');
let gridBoxColor = '#FF0000';
gridBox.classList.add('gridBox');
// This handler will be executed only once when the cursor
// moves over the div (grid box)
gridBox.addEventListener("mouseover", function( event ) {
if (colorModeSelect.value == 'slowchange') {
// turn the div white slowly
gridBoxColor = LightenDarkenColor(gridBoxColor);
event.target.style.backgroundColor = gridBoxColor;
} else
// turn the div white immediately
event.target.style.backgroundColor = "white";
}, false);
container.appendChild(gridBox);
}
}
// function that takes in a Num and creates a Num*Num grid of divs styled as boxes. **Click to draw**
function sketchGridClick(gridSize) {
for (i=0; i<(gridSize*gridSize); i++) {
const gridBox = document.createElement('div');
let gridBoxColor = '#FF0000';
gridBox.classList.add('gridBox');
// This handler will be executed only once when the cursor
// moves over the div (grid box)
gridBox.addEventListener("mouseover", function( event ) {
if (mouseDown == 1){
if (colorModeSelect.value == 'slowchange') {
// turn the div white slowly
gridBoxColor = LightenDarkenColor(gridBoxColor);
event.target.style.backgroundColor = gridBoxColor;
} else
// turn the div white immediately
event.target.style.backgroundColor = "white";
}
}, false);
container.appendChild(gridBox);
}
}
// function that removes all child nodes
function removeAllChildNodes(parent) {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}
// a button that resets the grid and prompts you to enter a new grid size
resetBtn.addEventListener('click',function(event) {
gridBoxColor = "#FF0000";
let mouseModeSelection = mouseModeSelect.value
userNum = prompt('Please enter a grid size as a number between 1-100');
while(!(userNum >= 1 && userNum <= 100)) {
userNum = prompt('INVALID INPUT, Please enter a grid size as a number between 1-100');
}
if (userNum >= 1 && userNum <= 100){
removeAllChildNodes(container);
changeGridLen(userNum);
if( mouseModeSelection == 'hovermode') {
sketchGridHover(userNum);
} else if( mouseModeSelection == 'clickmode'){
sketchGridClick(userNum);
} else
console.log('something went wrong');
}
});
// a dropdown that allows user to change between click mode and hover mode
mouseModeSelect.addEventListener('input',function(event) {
gridBoxColor = "#FF0000";
removeAllChildNodes(container);
changeGridLen(userNum);
if( event.target.value == 'hovermode') {
sketchGridHover(userNum);
} else if( event.target.value == 'clickmode'){
sketchGridClick(userNum);
} else
console.log('something went wrong');
});
// a dropdown that allows user to change between click mode and hover mode
colorModeSelect.addEventListener('input',function(event) {
let mouseModeSelection = mouseModeSelect.value;
removeAllChildNodes(container);
changeGridLen(userNum);
if( mouseModeSelection == 'hovermode') {
sketchGridHover(userNum);
} else if( mouseModeSelection == 'clickmode'){
sketchGridClick(userNum);
} else
console.log('something went wrong');
});
sketchGridHover(10);