-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
195 lines (164 loc) · 4.61 KB
/
main.js
File metadata and controls
195 lines (164 loc) · 4.61 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//HTML & CSS Layout
//creating main body ,contaner ,and toolbar.
const totwidth = window.innerWidth;
const totheight = window.innerHeight;
const width = totwidth-90;
const height = totheight-10;
var oveWidth = width%40;
var oveheight = (height%40 / height )*100;
var numRows =parseInt( height/40);
var numcolumns = parseInt(width/40);
var drType = "random";
var clicked = false;
const style = document.createElement('style');
style.innerHTML = `
body {
margin:0px;
margin-top:10px;
padding:0px;
}
#mainBody{
margin:0px;
padding:0px;
height:${height-oveheight}px;
width:${totwidth}px;
display:grid;
grid-template-areas:'mainBar mainDiv';
grid-gap: 0;
}
.mainBar{
height:50%;
width: 70%;
margin-top:200%;
border-style: solid;
border-width: 5px 5px 5px 0;
border-radius: 0 25px 25px 0;
background-color: lightgrey;
display:grid;
grid-template-areas:
'icon1'
'icon2'
'icon3'
'icon4';
}
.mainDiv{
margin:auto;
width: ${width-oveWidth}px ;
height:100%;
display: grid;
grid-template-rows: repeat(auto-fill, 40px);
grid-template-columns: repeat(auto-fill, 40px);
}
.mainDiv div{
height: 40px;
width: 40px;
}
.icon1,.icon2,.icon3,.icon4{
padding: auto;
// text-align: center;
align-self: auto;
margin:auto;
cursor: pointer;
}
input[type="color"] {
-webkit-appearance: none;
border: none;
width: 40px;
height: 40px;
border-radius: 50%;
cursor: pointer;
}
input[type="color"]::-webkit-color-swatch-wrapper {
padding: 0;
}
input[type="color"]::-webkit-color-swatch {
border: none;
border-radius: 50%;
}
// h6{
// cursor: pointer;
// }
`
var mainBody = document.querySelector('#mainBody');
document.getElementById("head").appendChild(style);
var mainBar = document.createElement('div');
mainBody.appendChild(mainBar).classList.add("mainBar");
var mainDiv = document.createElement('div');
mainBody.appendChild(mainDiv).classList.add("mainDiv");
//creating the main functionality
const divGenerator = (numRows,numcolumns)=>{
let index = 1;
while (index <= (numRows*numcolumns)) {
index++;
var subDiv = document.createElement('div');
subDiv.addEventListener("mousedown",makeclick);
subDiv.addEventListener("mouseup",unclick);
subDiv.addEventListener("drag",unclick);
subDiv.addEventListener("mousemove",(e) => {
if (clicked) {
if (drType=="select") {
select(e.currentTarget);
}else if (drType=="random") {
pante(e.currentTarget);
}else if (drType=="eraser") {
eraser(e.currentTarget);
}
}
});
document.querySelector('.mainDiv').appendChild(subDiv).classList.add('subdiv');
}
}
//creating the tools
divGenerator(numRows,numcolumns);
var divnum = 1;
while (divnum != 5) {
var icon = document.createElement('div');
if (divnum != 1){
icon.innerHTML=`<img src="img/${divnum-1}.png" width="50" height="50">`
if (divnum == 2) {
icon.addEventListener("click",setTyperandom);
}else if (divnum == 3) {
icon.addEventListener("click",setTypeeraser);
}else if (divnum == 4) {
icon.addEventListener("click",setReset);
}
}else{
icon.innerHTML=`<input type="color" id="sColor" value="#ff0000">`
icon.addEventListener("click",setTypeselect);
}
document.querySelector('.mainBar').appendChild(icon).classList.add("icon"+divnum);
divnum++;
}
//contral functions
function setReset() {
document.querySelector('.mainDiv').innerHTML="";
divGenerator(numRows,numcolumns);
drType="random";
}
function setTypeselect(){
drType = "select"
}
function setTyperandom(){
drType = "random"
}
function setTypeeraser(){
drType = "eraser"
}
function eraser(e){
e.removeAttribute("style");
}
function makeclick() {
if (drType=="eraser"){
this.removeAttribute("style");
}
clicked = true;
}
function unclick() {
clicked = false;
}
function pante(e) {//random color creator.
e.style.backgroundColor=`${"#"+((1<<24)*Math.random()|0).toString(16)}`;
}
function select(e) {//fixed color creator.
e.style.backgroundColor=`${document.querySelector("#sColor").value}`;
}