-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (67 loc) · 2.27 KB
/
index.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
const SHADOW_TRAILER = 'shadow';
const RANDOM_TRAILER = 'random';
const DEFAULT_GRID_SIZE = 10;
function clearContainer() {
document.getElementById("container").innerHTML = ""
}
function setSquareTrail(squareElement, trail) {
if(trail === SHADOW_TRAILER) {
squareElement.style['background-color'] = 'black';
let opacity = +squareElement.style.opacity || 0;
opacity += 0.1;
squareElement.style.opacity = opacity;
} else if (trail === RANDOM_TRAILER) {
squareElement.style['background-color'] = getRandomColor();
} else {
squareElement.style['background-color'] = 'rgb(163, 163, 175)';
}
}
function createGrid(size = 50, trail) {
clearContainer();
const SQUARE_BORDER_WIDTH = 1;
let container = document.getElementById("container");
let containerSize = container.clientWidth;
let squareBorderWidth = `${SQUARE_BORDER_WIDTH}px`;
let squareSize = containerSize / size - SQUARE_BORDER_WIDTH * 2;
for(let i = 0; i < size * size; i++) {
let square = document.createElement('div');
let sizePx = squareSize + "px";
square.style['width'] = sizePx;
square.style['border-width'] = squareBorderWidth;
square.classList.add('square');
square.addEventListener('mouseenter', e => setSquareTrail(e.target, trail));
container.appendChild(square);
}
}
function getRandomColorValue() {
return Math.floor(Math.random() * 256);
}
function getRandomColor() {
return `rgb(${getRandomColorValue()}, ${getRandomColorValue()}, ${getRandomColorValue()})`
}
function configureGrid() {
let gridSize = +prompt('Enter a grid size smaller than 100');
if(gridSize > 0 && gridSize <= 100) {
createGrid(gridSize);
} else if(gridSize === 0) {
return;
} else {
configureGrid()
}
}
function configureTrail(trail) {
let squares = document.querySelectorAll('.square');
let gridSize = Math.sqrt(squares.length);
createGrid(gridSize, trail);
}
document.getElementById('grid-size').addEventListener('click', configureGrid);
document.getElementById('setup').addEventListener('click', e => {
if(e.target.id == 'random-trail') {
configureTrail(RANDOM_TRAILER)
} else if(e.target.id == 'shadow-trail') {
configureTrail(SHADOW_TRAILER)
} else if(e.target.id == 'reset-trail') {
configureTrail()
}
})
createGrid(DEFAULT_GRID_SIZE)