-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.js
170 lines (145 loc) · 3.16 KB
/
grid.js
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
var option = 1;
var lastOption = -1;
$(document).ready(function()
{
//Start out with 16 rows and 16 columns
createSquares(16);
//Clear the squares of any color
$('#clear').click(function(){
clear();
});
//Resize grid
$('#setRows').click(function(){
setNumberOfRows();
});
//Color squares red on hover
$('#defaultColor').click(function(){
lastOption = option;
option = 1;
clear();
});
//Color squares a random color on hover
$('#randomColor').click(function(){
lastOption = option;
option = 2;
clear();
});
//Increase opacity of squares until underlying color is fully visible
$('#opacityColor').click(function(){
if(option !== 3)
{
lastOption = option;
}
option = 3;
clear();
});
});
function setNumberOfRows () {
clear();
var squaresInOneRow = parseInt(prompt("How many tiles in a row?"));
while(isNaN(squaresInOneRow))
{
var squaresInOneRow = parseInt(prompt("Please enter a number for how many tiles in a row."));
}
createSquares(squaresInOneRow);
}
function setRandomColor(item)
{
var r = parseInt(Math.random()*255);
var g = parseInt(Math.random()*255);
var b = parseInt(Math.random()*255);
var colorString = "rgb(" + r + "," + g + "," + b + ")";
$(item).css({"background-color":colorString});
}
function increaseOpacity(item)
{
var opacity = parseFloat($(item).css("opacity"));
if(isNaN(opacity))
{
opacity = 0;
}
opacity += .1;
if(opacity < 1)
{
$(item).css({"opacity":opacity});
}
}
function clear()
{
var opacity = 1;
if(option === 3)
{
opacity = 0;
}
//Set all squares to have the default background color
$('.square').css({"background-color":$('#container').css("background-color"), "opacity":opacity});
setSquareColorPreferences();
}
//Create the squares in a grid of specified size
function createSquares(squaresInOneRow)
{
var html = '';
for(var i=0; i<squaresInOneRow; i++)
{
html = html + '<div class="row">';
for(var j=0; j<squaresInOneRow; j++)
{
html = html + '<div class="square"></div>';
}
html = html + '</div>';
}
$('#container').html(html);
setSquareSize(squaresInOneRow);
setSquareColorPreferences();
}
//Set user chosen preferences for color of squares
function setSquareColorPreferences()
{
$('.square').unbind();
if(option === 1)
{
$('.square').bind({
mouseenter: function(e) {
setDefaultColor(this);
}
});
}
else if(option === 2)
{
$('.square').bind({
mouseenter: function(e) {
setRandomColor(this);
}
});
}
else if(option === 3)
{
$('.square').bind({
mouseenter: function(e) {
//Randomly Colored Squares
if(lastOption === 2)
{
setRandomColor(this);
}
//Squares of default color
else
{
setDefaultColor(this);
}
//Gradually increase the squares visibility
increaseOpacity(this);
}
});
}
}
function setDefaultColor(item)
{
$(item).css({"background-color":"red"});
}
function setSquareSize(squaresInOneRow)
{
var rowWidth = parseFloat($('#container').css("width"));
var squareWidth = rowWidth/squaresInOneRow;
$('.row').css({"width":rowWidth, "height":squareWidth});
$('.square').css({"width":squareWidth, "height":squareWidth, "display":"inline-block"});
}