-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
170 lines (142 loc) · 5.47 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
const DEFAULT_MODE = 'color';
const DEFAULT_SIZE = 16;
const DEFAULT_COLOR = '#333333'
let currentMode = DEFAULT_MODE;
let currentColor = DEFAULT_COLOR;
function buildGrid(x) {
grid.style.display = "grid";
grid.style.gridTemplateColumns = `repeat(${x}, 1fr)`;
grid.style.gridTemplateRows = `repeat(${x}, 1fr)`;
for (let i = 0; i < x * x; i++) {
let square = document.createElement('div');
square.classList.add('square');
square.addEventListener('mousedown', changeColor);
square.addEventListener('mouseover', changeColor);
square.addEventListener('touchstart', changeColor);
square.addEventListener('touchmove', changeColor);
square.addEventListener('touchend', changeColor);
square.style = 'background-color: rgba(255, 255, 255, 1)';
grid.appendChild(square);
square.dataset.darken = 0;
// let click = 0;
}
}
function changeColor(e) {
// console.log(e.target.style.backgroundColor);
if (e.type === 'mouseover' && !mouseDown) return;
if(currentMode === 'color'){
e.target.style.backgroundColor = currentColor;
}
else if(currentMode === 'erase'){
e.target.style.backgroundColor = '#fefefe';
}
else if(currentMode === 'rainbow'){
// this.classList.remove('black');
let randomR = Math.floor(Math.random()*256);
let randomG = Math.floor(Math.random()*256);
let randomB = Math.floor(Math.random()*256);
e.target.style.backgroundColor = `rgb(${randomR}, ${randomG}, ${randomB})`;
}
else if(currentMode === 'shade'){
let oldColor = e.target.style.backgroundColor;
// console.log(oldColor);
let rgbaString = (oldColor.charAt(3) == 'a') ? oldColor.slice(5, -1) : oldColor.slice(4, -1);
let rgbaArray = rgbaString.split(',');
let red = rgbaArray[0];
let green = rgbaArray[1];
let blue = rgbaArray[2];
let alpha = rgbaArray[3] ? rgbaArray[3] : 1;
let currentOpacity = Number(this.style.backgroundColor.slice(-4, -1));
// console.log(alpha)
// console.log(currentOpacity)
let currentDarkeningStep = e.target.dataset.darken;
if (currentDarkeningStep === 9) {
this.style.backgroundColor = 'rgba(0,0,0,1)';
}
let newRed = getNewColorValue(red, currentDarkeningStep, false);
let newGreen = getNewColorValue(green, currentDarkeningStep, false);
let newBlue = getNewColorValue(blue, currentDarkeningStep, false);
let newAlpha = getNewColorValue(currentOpacity, currentDarkeningStep, true);
currentDarkeningStep ++
e.target.dataset.darken = currentDarkeningStep;
this.style.backgroundColor = `rgba(${newRed}, ${newGreen}, ${newBlue}, ${newAlpha})`;
}
}
// console.log(this.classList);
function getNewColorValue(currentColorValue, step, alpha) {
let increment;
let newValue;
if(!alpha) {
increment = currentColorValue / (10 - step);
newValue = currentColorValue - increment;
}else {
increment = (1 - currentColorValue) / (10 - step);
newValue = +currentColorValue + increment;
}
return (newValue);
}
function reload (){
grid.innerHTML = ''
buildGrid(slider.value);
setCurrentMode ('color');
}
function setCurrentMode (newMode) {
activateButton(newMode);
currentMode = newMode;
}
function setCurrentColor(newColor) {
currentColor = newColor;
}
const grid = document.querySelector('.container');
const square = document.querySelectorAll('.square');
const clearBtn = document.querySelector('.clear');
clearBtn.addEventListener('click', reload);
let mouseDown = false;
document.body.onmousedown = () => (mouseDown = true);
document.body.onmouseup = () => (mouseDown = false);
let click = 0;
const slider = document.getElementById('slider');
const sliderValue = document.getElementById('sliderValue');
sliderValue.innerHTML = `${slider.value} x ${slider.value}`;
slider.oninput = function(){
let inner = this.value;
sliderValue.innerHTML = `${inner} x ${inner}`;
reload();
}
buildGrid(slider.value);
const colorBtn = document.querySelector('.colorBtn');
const eraserBtn = document.querySelector('.eraser');
colorBtn.onclick = () => setCurrentMode('color');
eraserBtn.onclick = () => setCurrentMode('erase');
const rainbowBtn = document.querySelector('.rainbowBtn');
rainbowBtn.onclick = () => setCurrentMode('rainbow');
const colorPicker = document.getElementById ('colorPicker');
colorPicker.oninput = function(){
let colorValue = this.value;
setCurrentColor(colorValue);
}
const shaderBtn = document.querySelector('.shader');
shaderBtn.onclick = () => setCurrentMode('shade');
function activateButton(newMode) {
if (currentMode === 'rainbow') {
rainbowBtn.classList.remove('active');
} else if (currentMode === 'color') {
colorBtn.classList.remove('active');
} else if (currentMode === 'erase') {
eraserBtn.classList.remove('active');
} else if (currentMode === 'shade') {
shaderBtn.classList.remove('active');
}
if (newMode === 'rainbow') {
rainbowBtn.classList.add('active');
} else if (newMode === 'color') {
colorBtn.classList.add('active');
} else if (newMode === 'erase') {
eraserBtn.classList.add('active');
} else if (newMode === 'shade') {
shaderBtn.classList.add('active');
}
}
window.onload = () => {
activateButton(DEFAULT_MODE);
}