-
Notifications
You must be signed in to change notification settings - Fork 11
/
script.js
188 lines (164 loc) · 5.75 KB
/
script.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
;(function() {
"use strict";
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Balls = function () {
function Balls(context, buffer) {
_classCallCheck(this, Balls);
this.context = context;
this.buffer = buffer;
}
_createClass(Balls, [{
key: 'setup',
value: function setup() {
this.gainNode = this.context.createGain();
this.source = this.context.createBufferSource();
this.source.buffer = this.buffer;
this.source.connect(this.gainNode);
this.gainNode.connect(this.context.destination);
this.gainNode.gain.setValueAtTime(1, this.context.currentTime);
}
}, {
key: 'play',
value: function play() {
this.setup();
this.source.start(this.context.currentTime);
}
}, {
key: 'stop',
value: function stop() {
var ct = this.context.currentTime + 1;
this.gainNode.gain.exponentialRampToValueAtTime(.1, ct);
this.source.stop(ct);
}
}]);
return Balls;
}();
var Buffer = function () {
function Buffer(context, urls) {
_classCallCheck(this, Buffer);
this.context = context;
this.urls = urls;
this.buffer = [];
}
_createClass(Buffer, [{
key: 'loadSound',
value: function loadSound(url, index) {
var request = new XMLHttpRequest();
request.open('get', url, true);
request.responseType = 'arraybuffer';
var thisBuffer = this;
request.onload = function () {
thisBuffer.context.decodeAudioData(request.response, function (buffer) {
thisBuffer.buffer[index] = buffer;
if (index == thisBuffer.urls.length - 1) {
thisBuffer.loaded();
}
});
};
request.send();
}
}, {
key: 'getBuffer',
value: function getBuffer() {
var _this = this;
this.urls.forEach(function (url, index) {
_this.loadSound(url, index);
});
}
}, {
key: 'loaded',
value: function loaded() {
_loaded = true;
}
}, {
key: 'getSound',
value: function getSound(index) {
return this.buffer[index];
}
}]);
return Buffer;
}();
var balls = null;
var preset = 0;
var _loaded = false;
function playBalls() {
var index = parseInt(this.dataset.note) + preset;
balls = new Balls(context, buffer.getSound(index));
balls.play();
}
function stopBalls() {
balls.stop();
}
var context = new (window.AudioContext || window.webkitAudioContext)();
var path = 'audio/';
var sounds = [path + 'sound1.mp3', path + 'sound2.mp3', path + 'sound3.mp3', path + 'sound4.mp3', path + 'sound5.mp3', path + 'sound6.mp3', path + 'sound7.mp3', path + 'sound8.mp3', path + 'sound9.mp3', path + 'sound10.mp3', path + 'sound11.mp3', path + 'sound12.mp3', path + 'sound13.mp3', path + 'sound14.mp3', path + 'sound15.mp3', path + 'sound16.mp3', path + 'sound17.mp3', path + 'sound18.mp3', path + 'sound19.mp3', path + 'sound20.mp3', path + 'sound21.mp3', path + 'sound22.mp3', path + 'sound23.mp3', path + 'sound24.mp3', path + 'sound25.mp3', path + 'sound26.mp3', path + 'sound27.mp3', path + 'sound28.mp3', path + 'sound29.mp3', path + 'sound30.mp3', path + 'sound31.mp3', path + 'sound32.mp3', path + 'sound33.mp3', path + 'sound34.mp3', path + 'sound35.mp3', path + 'sound36.mp3'];
var buffer = new Buffer(context, sounds);
var ballsSound = buffer.getBuffer();
var buttons = document.querySelectorAll('.b-ball_bounce');
buttons.forEach(function (button) {
button.addEventListener('mouseenter', playBalls.bind(button));
button.addEventListener('mouseleave', stopBalls);
});
function ballBounce(e) {
var i = e;
if (e.className.indexOf(" bounce") > -1) {
return;
}
toggleBounce(i);
}
function toggleBounce(i) {
i.classList.add("bounce");
function n() {
i.classList.remove("bounce")
i.classList.add("bounce1");
function o() {
i.classList.remove("bounce1")
i.classList.add("bounce2");
function p() {
i.classList.remove("bounce2")
i.classList.add("bounce3");
function q() {
i.classList.remove("bounce3");
}
setTimeout(q, 300);
}
setTimeout(p, 300);
}
setTimeout(o, 300);
}
setTimeout(n, 300);
}
var array1 = document.querySelectorAll('.b-ball_bounce')
var array2 = document.querySelectorAll('.b-ball_bounce .b-ball__right')
for(var i=0; i<array1.length; i++){
array1[i].addEventListener('mouseenter', function(){
ballBounce(this)
})
}
for(var i=0; i<array2.length; i++){
array2[i].addEventListener('mouseenter', function(){
ballBounce(this)
})
}
var l = ["49", "50", "51", "52", "53", "54", "55", "56", "57", "48", "189", "187", "81", "87", "69", "82", "84", "89", "85", "73", "79", "80", "219", "221", "65", "83", "68", "70", "71", "72", "74", "75", "76", "186", "222", "220"];
var k = ["90", "88", "67", "86", "66", "78", "77", "188", "190", "191"];
var a = {};
for (var e = 0, c = l.length; e < c; e++) {
a[l[e]] = e;
}
for (var _e = 0, _c = k.length; _e < _c; _e++) {
a[k[_e]] = _e;
}
document.addEventListener('keydown', function(j){
var i = j.target;
console.log(j.keyCode)
if (j.which in a) {
var index = parseInt(a[j.which]);
balls = new Balls(context, buffer.getSound(index));
balls.play();
var ball = document.querySelector('[data-note="' + index + '"]');
toggleBounce(ball);
}
})
})();