-
Notifications
You must be signed in to change notification settings - Fork 0
/
etch.js
116 lines (104 loc) · 2.79 KB
/
etch.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
var dimension = 50;
var option = "change"; //change, random, darken, trail
$(document).ready(function() {
drawGrid();
$("#clear").click(function() {
if (option === "change") {
$(".tile").removeClass('passed');
}
else {
$(".tile").css({"background-color":"white", "opacity":1});
}
});
$("#change").on("click", function() {
option = "change";
var newDim = prompt("Please enter the desired width.", 50);
if (newDim != null) {
dimension = newDim;
console.log("new dim " + dimension);
$(".container").empty();
drawGrid();
}
});
$("#greyscale").on("click", function() {
option = "darken";
var newDim = prompt("Please enter the desired width.", 50);
if (newDim != null) {
dimension = newDim;
console.log("new dim " + dimension);
$(".container").empty();
drawGrid();
}
});
$("#random").on("click", function() {
option = "random";
var newDim = prompt("Please enter the desired width.", 50);
if (newDim != null) {
dimension = newDim;
console.log("new dim " + dimension);
$(".container").empty();
drawGrid();
}
});
$("#trail").on("click", function() {
option = "trail";
var newDim = prompt("Please enter the desired width.", 50);
if (newDim != null) {
dimension = newDim;
console.log("new dim " + dimension);
$(".container").empty();
drawGrid();
}
});
});
function drawGrid () {
container = $(".container");
console.log("draw grid " + dimension);
squareWidth = container.width() / dimension;
for (var i = 1; i <= dimension*dimension; i++) {
container.append("<div class='tile'></div>");
};
$(".tile").css({width: squareWidth, height: squareWidth});
if(option === "change") {
$(".tile").mouseenter(function() {
$(this).addClass("passed");
});
}
else if (option === "darken") {
$(".tile").mouseenter(function() {
$(this).css("background-color", darkenColor($(this).css("background-color")));
});
}
else if (option === "random") {
$(".tile").mouseenter(function() {
$(this).css("background-color", randomColor());
});
}
else if (option === "trail") {
$(".tile").hover(function() {
$(this).css("opacity", 0);
}, function() {
$(this).fadeTo('slow', 1);
});
}
}
function darkenColor(rgb){
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
var red=parseInt(rgb[1]);
var green=parseInt(rgb[2]);
var blue=parseInt(rgb[3]);
red = red - 50;
green = green - 50;
blue = blue - 50;
newRgb = "rgb(" + Math.floor(red) + "," + Math.floor(green) + "," + Math.floor(blue) + ")";
// console.log(newRgb);
return newRgb;
};
function randomColor() {
red = Math.floor(Math.random()*255);
green = Math.floor(Math.random()*255);
blue = Math.floor(Math.random()*255);
newRgb = "rgb(" + Math.floor(red) + "," + Math.floor(green) + "," + Math.floor(blue) + ")";
console.log(newRgb);
return newRgb;
}