-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
107 lines (86 loc) · 3.05 KB
/
script.js
File metadata and controls
107 lines (86 loc) · 3.05 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
//Create 16x16 grid in the drawing box
const drawingBox = document.querySelector('.drawing-box');
function drawDrawingGrid(size) {
drawingBox.style.setProperty('--grid-rows', size);
drawingBox.style.setProperty('--grid-cols', size);
for (i = 0; i < size**2; i++) {
let drawingPixel = document.createElement('div');
drawingBox.appendChild(drawingPixel).className = 'drawing-pixel';
}
}
drawDrawingGrid(16);
function changeSize(size) {
const drawingPixels = document.querySelectorAll('.drawing-pixel');
for (let i = 0; i < drawingPixels.length; i++) {
drawingBox.removeChild(drawingPixels[i]);
}
drawDrawingGrid(size);
}
//Left button dropdown logic
function chooseColor() {
document.getElementById('colors').classList.toggle('show');
}
//Hide the menu if the user clicks outside the dropdown menu
window.onclick = function(event) {
if (!event.target.matches('.left-button')) {
var dropdownLeft = document.getElementsByClassName("left-dropdown-content");
var i;
for (i = 0; i < dropdownLeft.length; i++) {
var openDropdown = dropdownLeft[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
//Painting logic
function paint(color) {
console.log(color)
const drawingPixels = document.querySelectorAll('.drawing-pixel');
if (color === 'rainbow') {
for (let i = 0; i < drawingPixels.length; i++) {
let rgb1 = Math.floor(Math.random()*256);
let rgb2 = Math.floor(Math.random()*256);
let rgb3 = Math.floor(Math.random()*256);
drawingPixels[i].addEventListener('mouseover', () => {
console.log(`rgb(${rgb1}, ${rgb2}, ${rgb3})`);
drawingPixels[i].style.backgroundColor = `rgb(${rgb1}, ${rgb2}, ${rgb3})`;
})
}
} else {
for (let i = 0; i < drawingPixels.length; i++) {
drawingPixels[i].addEventListener('mouseover', () => {
drawingPixels[i].style.backgroundColor = color;
})
}
}
}
paint('black');
//Color picking logic
const blueOption = document.getElementById('blue');
blueOption.onclick = () => paint('blue');
const redOption = document.getElementById('red');
redOption.onclick = () => paint('red');
const blackOption = document.getElementById('black');
blackOption.onclick = () => paint('black');
const rainbowOption = document.getElementById('rainbow');
rainbowOption.onclick = () => paint('rainbow');
//Reset logic
function resetDrawingBox() {
const drawingPixels = document.querySelectorAll('.drawing-pixel');
for (let i = 0; i < drawingPixels.length; i++) {
drawingPixels[i].style.backgroundColor = 'white';
}
}
const resetButton = document.querySelector('.right-button');
resetButton.addEventListener('click', () => resetDrawingBox());
//Slider logic
const slider = document.getElementById('box-size');
const output = document.getElementById('demo');
output.innerHTML = '10x10';
slider.oninput = function() {
output.innerHTML = `${this.value}x${this.value}`;
resetDrawingBox();
changeSize(this.value);
paint('black');
}