-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
181 lines (131 loc) · 4.48 KB
/
Copy pathscript.js
File metadata and controls
181 lines (131 loc) · 4.48 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
const HEIGHT = 780;
const WIDTH = 780;
const grid = document.querySelector('.grid');
// Draw a num x num grid of divs
function buildGrid(num) {
const size = HEIGHT / num;
// Nested for-loop to create num x num grid of divs
for (let i = 0; i < num; i++) {
const newRow = document.createElement('div');
newRow.classList.add('row');
for (let j = 0; j < num; j++) {
const cell = document.createElement('div');
cell.style.width = size + 'px';
cell.style.height = size +'px';
cell.classList.add('cell');
newRow.appendChild(cell);
}
grid.appendChild(newRow);
}
}
function getSelected() {
let selected;
let i = 0;
while (i < colours.length && colours[i].classList.length < 3)
i++;
selected = colours[i].classList[1];
return selected;
}
// Callback function on 'mouseover' event of "cell" divs
function paintCell(e) {
const colour = getSelected();
e.target.style.backgroundColor = colour;
}
// Modify event listeners on each cell
function modifyPaintListener(add = true, ...colour) {
const rows = grid.querySelectorAll('.row');
rows.forEach( (row) => {
const cells = row.querySelectorAll('.cell');
cells.forEach( cell => {
if (add)
cell.addEventListener('mouseover', paintCell);
else
cell.removeEventListener('mouseover', paintCell);
})
})
}
// Check for mouse click and add 'mouseover' event listener until 'mouseup'
function checkDrag() {
window.addEventListener('mousedown', (e) => {
if (e.target.className == 'cell') {
let selected = getSelected();
// paint AT LEAST the target cell
e.target.style.backgroundColor = selected;
// Add 'mouseover' event listeners on click
modifyPaintListener(true);
console.log(e);
}
// Add event listener for 'mouseup' to remove 'mouseover' event listeners
window.addEventListener('mouseup', (e) => {
modifyPaintListener(false);
})
})
}
function alterGrid() {
let newSize = prompt("Number of squares per side (max: 100): ");
// Check to see that they entered something
if(newSize != null) {
if (newSize <= 100) {
grid.replaceChildren();
buildGrid(newSize);
}
else {
alert("Exceeded maximum input!");
}
}
}
function colourSelect(e) {
colours.forEach ((colour) => {
colour.classList.remove('selected-colour');
})
e.target.classList.add('selected-colour');
const rules = document.styleSheets[0].cssRules;
rules[5].style.backgroundColor = e.target.classList[1];
}
function showConfirmationPopUp() {
const popUp = document.querySelector(".confirm-download");
popUp.style.visibility ="visible";
const cancel = document.querySelector('#cancel');
cancel.addEventListener('click', () => popUp.style.visibility = "hidden");
const save = document.querySelector('#save');
// const form = document.querySelector('.download-form');
save.addEventListener('click', (e) => {
e.preventDefault();
let fileName = document.querySelector('#file-name').value;
let fileType = document.querySelector('#file-type').value;
console.log(fileName + ' ' + fileType);
if (fileName == '') {
alert("Invalid input(s). Please try again.")
console.error("invalid input");
}
else {
downloadURL(fileName, fileType)
}
})
}
function downloadURL(fileName, fileType) {
const toPrint = document.querySelector('.grid');
html2canvas(toPrint).then((canvas) => {
const imageURI = canvas.toDataURL(`image/${fileType}`);
const downloadLink = document.createElement("a");
downloadLink.download = fileName;
downloadLink.href = imageURI;
downloadLink.click();
})
return true;
}
// "main" script
// initialize
buildGrid(16);
checkDrag();
const adjustGrid = document.querySelector(".adjust-grid");
adjustGrid.addEventListener('click', alterGrid);
const colours = document.querySelectorAll(".colour");
colours.forEach( (btn) => {
btn.style.backgroundColor = btn.classList[1];
btn.addEventListener('click', colourSelect);
})
const exportBtn = document.querySelector(".export-img");
exportBtn.addEventListener('click', (e) => {
showConfirmationPopUp();
})