-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
216 lines (177 loc) · 7.15 KB
/
script.js
File metadata and controls
216 lines (177 loc) · 7.15 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
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
const sketchContainer = document.querySelector('#sketchContainer');
const sketchPadControls = document.querySelector('#sketchPadControls');
const brushButtonsContainer = document.querySelector('#brushButtons');
// Control sketchpad and brush behaviours
let showGrids = false;
let pixelBackgroundColor = 'rgb(0, 0, 0)';
let rgbPixels = false;
let darkeningMode = false;
let lighteningMode = false;
// Array for managing shading on pixels
const darkenlevel = [];
function generate(pixelsPerEdge) {
const tempContainer = document.createElement('div');
// This helps us have a pixelPerEdge x pixelPerEdge grid
const flexValue = `1 1 calc(100% / ${pixelsPerEdge})`;
for (let i = 0; i < pixelsPerEdge; i++) {
const row = document.createElement('div');
row.classList.add('sketch-row');
row.style.flex = flexValue;
for (let j = 0; j < pixelsPerEdge; j++) {
const pixel = document.createElement('div');
pixel.classList.add('sketch-pixel');
pixel.style.flex = flexValue;
pixel.style.backgroundColor = 'rgb(255, 255, 255)';
if (showGrids === true) {
pixel.style.border = '1px solid rgba(204, 203, 217, 0.5)';
}
// Add a count to the pixel
const pixelNo = parseInt((i + 1) * (j + 1));
pixel.setAttribute('pixelno', pixelNo);
// Set shading values to 0
darkenlevel[pixelNo] = 0;
row.appendChild(pixel);
}
tempContainer.appendChild(row);
}
sketchContainer.innerHTML = tempContainer.innerHTML;
}
generate(24);
document.querySelector('#defaultBrush').classList.toggle('button-toggled-on');
// Keep track of mousedown
let mouseDown = false;
sketchContainer.addEventListener('mousedown', event => {
mouseDown = true;
colorPixel(event);
});
sketchContainer.addEventListener('mouseup', () => {
mouseDown = false;
});
// Color pixel if mouse is pressed and is hovering
sketchContainer.addEventListener('mouseover', colorPixel);
function colorPixel(event) {
const pixel = event.target;
if (pixel.classList.contains('sketch-pixel') && mouseDown) {
if (!darkeningMode && !lighteningMode) {
// Reset shading level on pixel
darkenlevel[pixel.getAttribute('pixelno')] = 0;
// If RGB mode is on the pixels should have random colors
if (rgbPixels === true) {
function generateRandomRGB() {
return Math.floor(Math.random() * 255);
}
pixelBackgroundColor = `rgb(${generateRandomRGB()}, ${generateRandomRGB()}, ${generateRandomRGB()})`;
}
// Shading mode is on
} else if (darkeningMode === true || lighteningMode === true) {
const pixelNo = pixel.getAttribute('pixelno');
const backgroundColor = pixel.style.backgroundColor;
const pixelRGB = [];
const colorsArray = backgroundColor.split(',');
// For darkening mode, we need to increase darkenlevel of the pixel
if (darkeningMode) {
if (darkenlevel[pixelNo] < 0) {
darkenlevel[pixelNo] = 0;
}
if (darkenlevel[pixelNo] < 10) {
darkenlevel[pixelNo]++;
}
// For lightening mode, we need to decrese it
} else if (lighteningMode) {
if (darkenlevel[pixelNo] >= 0) {
darkenlevel[pixelNo] = 0;
}
if (darkenlevel[pixelNo] > -10) {
darkenlevel[pixelNo]--;
}
}
// Red
pixelRGB[0] = Number(colorsArray[0].trim().split('(')[1]);
// Green
pixelRGB[1] = Number(colorsArray[1].trim());
// Blue
pixelRGB[2] = Number(colorsArray[2].trim().split(')')[0]);
for (let i = 0; i < pixelRGB.length; i++) {
let newRGBValue;
// If lightening mode is on and color is black, we need to set the pixel value of 90% black
if (lighteningMode && pixelRGB[i] === 0) {
newRGBValue = Math.floor(255 / 10);
} else {
newRGBValue = Math.floor(pixelRGB[i] * (1 - darkenlevel[pixelNo] / 10));
}
// If new pixel exceeds 255
pixelRGB[i] = newRGBValue > 255 ? 255 : newRGBValue;
};
pixelBackgroundColor = `rgb(${pixelRGB[0]}, ${pixelRGB[1]}, ${pixelRGB[2]})`;
}
// Background color can get changed in event listeners below
pixel.style.backgroundColor = pixelBackgroundColor;
}
}
// Change sketch pad pixels, reset pixels and show grids
sketchPadControls.addEventListener('click', event => {
const target = event.target;
// Pixels per Edge
if (target.id === 'setPixels') {
const pixelBox = document.querySelector('#pixelsPerEdge');
const pixelsPerEdge = pixelBox.value;
if (pixelsPerEdge > 0 && pixelsPerEdge <= 100) {
generate(pixelsPerEdge);
}
return;
// Reset
} else if (target.id === 'resetSketch') {
const pixels = sketchContainer.querySelectorAll('.sketch-pixel');
pixels.forEach(pixel => {
pixel.style.backgroundColor = 'rgb(255, 255, 255)';
darkenlevel[pixel.getAttribute('pixelno')] = 0;
});
// Grids
} else if (target.id === 'showGrids') {
const pixels = sketchContainer.querySelectorAll('.sketch-pixel');
if (showGrids === false) {
pixels.forEach(pixel => pixel.style.border = '1px solid rgba(204, 203, 217, 0.5)');
showGrids = true;
} else {
pixels.forEach(pixel => pixel.style.border = 'none');
showGrids = false;
}
target.classList.toggle('button-toggled-on');
}
});
// Change brush styles (default, rgb, eraser)
brushButtonsContainer.addEventListener('click', event => {
const target = event.target;
if (target.id !== 'rgbBrush') {
rgbPixels = false;
if (target.id !== 'darkenBrush' && target.id !== 'lightenBrush') {
if (target.id === 'defaultBrush') {
pixelBackgroundColor = 'rgb(0, 0, 0)';
} else if (target.id === 'eraser') {
pixelBackgroundColor = 'rgb(255, 255, 255)';
}
darkeningMode = false;
lighteningMode = false;
} else if (target.id === 'darkenBrush') {
darkeningMode = true;
lighteningMode = false;
} else if (target.id === 'lightenBrush') {
lighteningMode = true;
darkeningMode = false;
}
} else if (target.id === 'rgbBrush') {
rgbPixels = true;
darkeningMode = false;
lighteningMode = false;
}
let buttons = target.parentNode.children;
buttons = Array.from(buttons);
buttons.forEach(button => {
if (button.id !== target.id) {
button.classList.remove('button-toggled-on');
}
if (target.tagName === 'BUTTON') {
target.classList.add('button-toggled-on');
}
});
});