-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
148 lines (116 loc) · 3.12 KB
/
queue.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
function newHtmlBlock(id) {
return('<div class="block" data-id="' + id + '"><div class="ring"></div></div>');
}
var WIDTH = 12;
var BLOCK_SIZE = 34;
var BLOCK_MARGIN = 2;
var frequencies = [
475,
450,
425,
400,
375,
350,
325,
300,
275,
250,
225,
200
];
iterFrequency = 0.5;
function Queue(n) {
this.n = n;
this.blocks = {};
this.matrix = {};
// Initialize blocks and matrix variables
for (var i = 0; i < n; i++) {
this.matrix[i] = [];
for (var j = 0; j < frequencies.length; j++) {
var block = new Block(i, frequencies[j]);
this.blocks[block.id] = block;
this.matrix[i].push(block.id);
}
}
}
// Take in an id for a block and play it
Queue.prototype.playBlock = function(id) {
block = this.blocks[id];
blockElement = $("div[data-id='" + id + "']");
block.play();
blockElement.addClass('selected');
blockElement.children(".ring").addClass('expand');
}
// Take in a set of ids to play
Queue.prototype.playBlocks = function(ids) {
for (var id in ids) {
this.playBlock(ids[id]);
}
}
// Init function that will generate all of the blocks
Queue.prototype.init = function() {
$('.tracker').css('height', this.n * BLOCK_SIZE);
for (var i in this.matrix) {
var html = "";
for (var j in this.matrix[i] ) {
html += newHtmlBlock(this.matrix[i][j]);
}
$('.container').append("<div class='column'>" + html +"</div>");
}
}
var stop = false;
// Start playing the sounds. This will loop through the matrix and play active blocks
Queue.prototype.start = function() {
var queue = this;
var matrix = queue.matrix;
var i = 0;
window.setInterval(function() {
i = (i + 1) % queue.n;
var activeIds = []
for (var id in matrix[i]) {
var block = queue.blocks[matrix[i][id]];
if (block.active) {
activeIds.push(block.id);
}
}
queue.playBlocks(activeIds);
if (stop) {
return;
}
}, iterFrequency * 1000);
moveTracker(queue);
}
function moveTracker(queue) {
$('.tracker').animate({
left: queue.n * BLOCK_SIZE
}, {
easing: "linear",
duration: queue.n * iterFrequency * 1000,
complete: function(){
$(this).css('left', 20);
moveTracker(queue);
}
});
}
$(document).ready(function() {
var queue = new Queue(WIDTH);
queue.init();
$('.block').click(function(e) {
var block = queue.blocks[this.dataset['id']];
block.toggleActive();
$(this).toggleClass('active');
});
$('#stop').click(function() {
stop = true;
});
$('#start').click(function() {
queue.start();
})
$(".block").bind('animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd', function(e) {
$(this).removeClass('selected');
});
$(".ring").bind('animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd', function(e) {
$(this).removeClass('expand');
});
queue.start();
});