-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (54 loc) · 1.91 KB
/
Copy pathscript.js
File metadata and controls
66 lines (54 loc) · 1.91 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
//define grid height/width and canvas table
const canvas=document.querySelector('.grid-canvas');
const gridSize=document.querySelector('.grid-size');
function makeGrid(){
let height =document.querySelector('.input-height').value;
let width = document.querySelector('.input-width').value;
// after grid is presented, need to remove any cells
//remove all children from canvas so if makeGrid gets called again it cleans the canvas
while(canvas.firstChild){
canvas.removeChild(canvas.firstChild);
}
// to create row & cells
for (let i=1; i<=height; i++){
let gridRow=document.createElement('tr');
canvas.appendChild(gridRow);
for (let j=1; j<=width; j++){
let gridCell=document.createElement('td');
gridRow.appendChild(gridCell);
// fills in cell with selected color
gridCell.addEventListener('mousedown',function(){
// color defined here
const color = document.querySelector('.color-picker').value;
this.style.backgroundColor = color;
})
}
}
}
// when user submitting height and width selections, callback function
gridSize.addEventListener('submit',function(e){
e.preventDefault();
makeGrid();
});
// To make the paint dragging
let down=false;
canvas.addEventListener('mousedown',function(e){
down=true;
canvas.addEventListener('mouseup', function(){
down=false;
});
// cells won't be colored if grid is left while
// pointer is held down
canvas.addEventListener('mouseleave', function(){
down=false;
});
canvas.addEventListener('mouseover', function(e){
const color= document.querySelector('.color-picker').value;
if (down){
if(e.target.tagName=== 'TD'){
e.target.style.backgroundColor=color;
}
}
});
});
makeGrid(18,18);