-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic.js
More file actions
82 lines (61 loc) · 2.18 KB
/
magic.js
File metadata and controls
82 lines (61 loc) · 2.18 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
/* The Magic happens below */
/* building a grid for the magic screen */
let gridSize = 64;
let pixelNumber = 0;
let column_num = '';
function addGrid() {
var gridItems = gridSize * gridSize
var pixelNumber = 0;
var column_num = '';
for (let i=0; i<gridSize; i++) {
column_num = column_num.concat(' auto')
}
for (let i=0; i<gridItems; i++) {
grid = document.querySelector('.grid-container');
child = document.createElement('div');
child.classList.add('grid-item');
child.id = "pixel-id-"+ pixelNumber;
grid.appendChild(child);
};
let root =document.documentElement.style.setProperty('--column--number', column_num);
registerGrid();
};
function lightBackgroud(e) {
div = e.currentTarget;
div.classList.add('lightUp');
};
/* reset the magic screen */
function alertButtonLeft() {
var allPixels = document.querySelectorAll('[id^="pixel"]');
allPixels.forEach(pixel => pixel.classList.remove('lightUp'));
}
/* resize the magic screen */
function alertButtonRight(e) {
var allPixels = document.querySelectorAll('[id^="pixel"]');
allPixels.forEach(pixel => pixel.classList.remove('lightUp'));
removeGrid()
while(true){
var newSize = prompt('What size should the screen be?', '128')
if( newSize <= 128 && newSize > 0){
gridSize = newSize;
break;
}else{
alert("Please enter a value less than 128 and greater than zero.");
}
}
addGrid();
}
function removeGrid() {
var allPixels = document.querySelectorAll('[id^="pixel"]');
allPixels.forEach(pixel => pixel.remove());
}
/*listen for mouse over events on the magic screen 'pixels' '[id^="pixel"]' */
function registerGrid() {
const allPixels = document.querySelectorAll('[id^="pixel"]');
allPixels.forEach(pixel => pixel.addEventListener('mouseover', lightBackgroud));
};
/* register buttons */
const buttonLeft = document.querySelector('.button-left');
buttonLeft.addEventListener('click', alertButtonLeft);
const buttonRight = document.querySelector('.button-right');
buttonRight.addEventListener('click', alertButtonRight);