-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
234 lines (196 loc) · 7.79 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
PIXEL ART CREATOR
by Peyton Bechard
Started: 31 Mar 2022
Last Updated: 4 Apr 2022
*/
/*
TO-DO:
- add undo button
- add save functionality
*/
/************************* FIELD DATA *************************/
// Toolbar Items
const penButton = document.getElementById('pen-color');
const penColorChooser = document.getElementById('pen-color-chooser');
const fillButton = document.getElementById('fill-color');
const fillColorChooser = document.getElementById('fill-color-chooser');
const eraserButton = document.getElementById('eraser');
eraserButton.style.backgroundColor = 'rgb(116, 116, 116)';
// Grid
const grid = document.getElementById('grid');
const allGridBoxes = grid.querySelectorAll('div');
// Options Menu Items
const clearCurrentButton = document.getElementById('clear-button');
const toggleGrid = document.getElementById('toggle-grid-button')
const validDimension = /^[\d]+$/;
const gridLabel = document.getElementById('current-grid');
const dimensionInput = document.getElementById('grid-dimension');
const setButton = document.getElementById('set-grid-button');
const resetAllButton = document.getElementById('reset-all-button');
const saveButton = document.getElementById('save-button');
/************************* INITIAL STATES *************************/
// Color States
let gridDimension = 16;
let penColor = '#000000';
let tempPenColor = '#000000';
let fillColor = '#ffffff';
let tempFillColor = '#ffffff';
let eraser = 'transparent';
// Grid Set-Up
let gridOn = true;
toggleGrid.style.backgroundColor = 'rgba(221, 221, 125, 0.8)';
let mouseDown = false;
let currentGridDimension = 16;
let tempGridDimension = 16;
let rightClicked = false;
// Init Functions
createGrid(gridDimension);
/************************* GRID GENERATOR *************************/
function createGrid(dimension) {
currentGridBoxes = grid.querySelectorAll('div');
currentGridBoxes.forEach( (div) => div.remove() );
dimensionInput.value = '';
dimensionInput.style.borderColor = 'rgb(88, 88, 92)';
setButton.setAttribute('disabled', 'true');
for (let i = 0; i < dimension; i++) {
for (let i = 0; i < dimension; i++) {
const newDiv = document.createElement('div');
grid.append(newDiv);
newDiv.classList.add('grid-box');
newDiv.style.width = `${100/dimension}%`;
newDiv.style.height = `${100/dimension}%`;
newDiv.style.borderColor = 'black';
newDiv.style.backgroundColor = 'white';
newDiv.setAttribute('oncontextmenu', 'return rightClick(this);')
let tempColor = newDiv.style.backgroundColor;
newDiv.addEventListener('mousedown', () => mouseDown = true);
newDiv.addEventListener('mouseup', () => mouseDown = false);
newDiv.addEventListener('click', (e) => {
newDiv.style.backgroundColor = penColor;
tempColor = newDiv.style.backgroundColor;
});
newDiv.addEventListener('mouseenter', (e) => {
tempColor = newDiv.style.backgroundColor;
if (mouseDown === false) {
tempColor = newDiv.style.backgroundColor;
newDiv.style.backgroundColor = 'rgba(104, 104, 104, 0.493)';
} else {
newDiv.style.backgroundColor = penColor;
tempColor = newDiv.style.backgroundColor;
}
});
newDiv.addEventListener('mouseleave', (e) => {
if (rightClicked !== true) {
if (mouseDown === false) {
newDiv.style.backgroundColor = tempColor;
} else {
newDiv.style.backgroundColor = penColor;
}
}
rightClicked = false;
});
}
}
gridOn = true;
toggleGrid.style.backgroundColor = 'rgba(221, 221, 125, 0.8)';
};
function rightClick(div) {
div.style.backgroundColor = fillColor;
rightClicked = true;
return false;
}
/************************* TOOLBAR EVENTS *************************/
penColorChooser.addEventListener('change', (e) => {
tempPenColor = e.target.value;
penButton.style.backgroundColor = 'rgba(221, 221, 125, 0.8)';
});
fillColorChooser.addEventListener('change', (e) => {
tempFillColor = e.target.value;
fillButton.style.backgroundColor = 'rgba(221, 221, 125, 0.8)';
});
penButton.addEventListener('click', () => changePenColor(tempPenColor));
function changePenColor(color) {
if (eraserButton.style.backgroundColor == 'rgba(221, 221, 125, 0.8)') {
eraserButton.style.backgroundColor = 'rgb(116, 116, 116)';
}
if (penButton.style.backgroundColor == 'rgba(221, 221, 125, 0.8)') {
penButton.style.backgroundColor = 'rgb(116, 116, 116)';
}
penColor = color;
};
eraserButton.addEventListener('click', (e) => {
if (eraserButton.style.backgroundColor == 'rgba(221, 221, 125, 0.8)') {
eraserButton.style.backgroundColor = 'rgb(116, 116, 116)';
penColor = tempPenColor;
if (eraserButton.style.backgroundColor == 'rgba(221, 221, 125, 0.8)') {
eraserButton.style.backgroundColor = 'rgb(116, 116, 116)';
}
} else {
eraserButton.style.backgroundColor = 'rgba(221, 221, 125, 0.8)';
penColor = 'rgb(198, 204, 211)';
};
});
fillButton.addEventListener('click', (e) => {
fillColor = tempFillColor;
let allGridBoxes = grid.querySelectorAll('div');
allGridBoxes.forEach( (div) => {
div.style.backgroundColor = fillColor;
});
fillButton.style.backgroundColor = 'rgb(116, 116, 116)';
});
/************************* OPTIONS MENU EVENTS *************************/
toggleGrid.addEventListener('click', (e) => {
if (gridOn) {
gridOn = false;
toggleGrid.style.backgroundColor = 'rgb(116, 116, 116)';
} else {
gridOn = true;
toggleGrid.style.backgroundColor = 'rgba(221, 221, 125, 0.8)';
}
let allGridBoxes = grid.querySelectorAll('div');
allGridBoxes.forEach( (div) => {
if (div.style.borderColor === 'black') {
div.style.borderColor = 'transparent';
} else {
div.style.borderColor = 'black';
}
});
});
clearCurrentButton.addEventListener('click', (e) => {
let allGridBoxes = grid.querySelectorAll('div');
allGridBoxes.forEach( (div) => div.style.backgroundColor = '#ffffff' )
});
dimensionInput.addEventListener('keyup', (e) => {
let input = dimensionInput.value;
if (!input) {
dimensionInput.style.borderColor = 'rgb(88, 88, 92)';
setButton.setAttribute('disabled', 'true');
gridLabel.textContent = `${currentGridDimension}x${currentGridDimension}`;
} else if (input.match(validDimension) && 1 <= parseInt(input) && parseInt(input) <= 64) {
dimensionInput.style.borderColor = 'rgb(0, 136, 34)';
setButton.removeAttribute('disabled');
gridLabel.textContent = `${input}x${input}`;
tempGridDimension = input;
} else {
dimensionInput.style.borderColor = 'rgb(168, 0, 0)';
setButton.setAttribute('disabled', 'true');
gridLabel.textContent = `${currentGridDimension}x${currentGridDimension}`;
}
});
setButton.addEventListener('click', () => {
gridDimension = dimensionInput.value;
intDimension = parseInt(gridDimension);
createGrid(intDimension);
currentGridDimension = tempGridDimension;
});
resetAllButton.addEventListener('click', (e) => {
createGrid(16);
gridOn = true;
toggleGrid.style.backgroundColor = 'rgba(221, 221, 125, 0.8)';
gridLabel.textContent = '16x16';
currentGridDimension = 16;
tempGridDimension = 16;
fillColor = 'white';
});
saveButton.addEventListener('click', () => alert("Feature currently unavailable. Please check again soon!") );