-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesigns.js
More file actions
39 lines (36 loc) · 1.45 KB
/
Copy pathdesigns.js
File metadata and controls
39 lines (36 loc) · 1.45 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
// Select color input
const colorSelection = document.querySelector('#colorPicker');
// Adding the grid-color function
function colorSelected(self) {
const color = colorSelection.value;
self.target.style.backgroundColor = color;
}
// Select size input
const canvas = document.querySelector('#pixelCanvas');
var canvasHeight = document.querySelector('#inputHeight');
var canvasWidth = document.querySelector('#inputWidth');
// When size is submitted by the user, call makeGrid()
function makeGrid() {
// I add this line so everytime I call makeGrid(), the older table disappear
canvas.innerHTML = '';
// I saw that using createDocumentFragment makes the process faster
const grid = document.createDocumentFragment();
// First, I create the rows...
for (let y = 0; y < canvasHeight.value; y++) {
const row = document.createElement('tr');
//... and then the columns
for (let x = 0; x < canvasWidth.value; x++) {
const column = document.createElement('td');
row.appendChild(column);
}
// I take advantage to add the EventListener to every square.
row.addEventListener('click', colorSelected);
grid.appendChild(row);
}
canvas.appendChild(grid);
};
document.querySelector('form').addEventListener('submit', function(event) {
// With this line of code, I can prevent the browser to delete my table as soon I click to 'submit'
event.preventDefault();
makeGrid();
});