-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtriangles.js
116 lines (99 loc) · 2.67 KB
/
triangles.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
/** Populates page with triangles based on the following:
* Chance of a blue = 5%.
* Chance of a grey is between GREYMAXCHANCE and GREYMINCHANCE depending on
* column.
*/
function Triangles(div, width, height) {
this._GREYMAXCHANCE = 85;
this._GREYMINCHANCE = 30;
this._BLUECHANCE = .06;
this._WHITECHANCE = .01;
this._TRIANGLEHEIGHT = 42;
this._div = div;
this._columns = Math.floor(width / this._TRIANGLEHEIGHT);
this._rows = Math.ceil(height / this._TRIANGLEHEIGHT);
for (var i = 0; i < this._columns; i += 1) {
this._createColumn();
}
};
/** Creates column N. */
Triangles.prototype._createColumn = function() {
var greyChance = genRand(this._GREYMINCHANCE, this._GREYMAXCHANCE) / 100;
var col = $('<div></div>').addClass('col');
for (var i = 0; i < this._rows; i += 1) {
var chance = Math.random();
var el = $('<div></div>');
if (chance < greyChance) {
el.addClass('triangle');
if (chance < this._WHITECHANCE) {
el.addClass('white');
} else if (chance < this._BLUECHANCE) {
el.addClass('blue');
}
} else {
el.addClass('empty');
}
col.append(el);
}
this._div.append(col);
};
Triangles.prototype.hide = function() {
var activeRow = $(this._div.children(':first'));
function hideNext() {
if (!activeRow) {
return;
}
row = activeRow;
nextRow = activeRow.next();
activeRow = nextRow.length > 0 ? $(nextRow[0]) : null;
row.animate({ opacity: 0 }, 100, hideNext);
}
hideNext();
};
Triangles.prototype.show = function() {
var activeRow = $(this._div.children(':first'));
function showNext() {
if (!activeRow) {
return;
}
row = activeRow;
nextRow = activeRow.next();
activeRow = nextRow.length > 0 ? $(nextRow[0]) : null;
row.animate({ opacity: 1 }, 100, showNext);
}
showNext();
};
/** Generates a random number between MIN and MAX */
function genRand(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
$(document).ready(function(){
var div = $('#container');
var triangles;
generate = function() {
div.empty();
triangles = new Triangles(div, $(window).width(), $(window).height());
}
generate();
$(window).resize(generate);
$('#sidebar').hover(function() {
if (triangles) {
triangles.hide();
}
}, function() {
if (triangles) {
triangles.show();
}
});
// Lightly mobile
document.getElementById('sidebar').addEventListener('touchstart', function(e) {
if (triangles) {
triangles.hide();
}
});
document.getElementById('sidebar').addEventListener('touchend', function(e) {
if (triangles) {
triangles.show();
}
});
});