-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
171 lines (159 loc) · 5.46 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>Sound Visualizer</title>
<link rel="stylesheet" href="assets/styles/sanitize.css">
<style media="screen">
#info {
position: absolute;
width: 100%;
text-align: center;
color: #f6f6f6;
}
#info p {
margin: 0;
}
#info a {
color: #2194CE;
text-decoration: none;
}
#progressbar {
margin: 20px;
width: 200px;
height: 200px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
opacity: 1.0;
}
#progressbar.hidden {
transition: all .5s ease-in-out;
opacity: 0;
}
</style>
</head>
<body>
<div id="info">
<p>Sound Visualizer with <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a></p>
<p>music by <a href="http://www.newgrounds.com/audio/listen/358232" target="_blank" rel="noopener noreferrer">bensound-goinghigher</a></p>
</div>
<div id="progressbar"></div>
<div id="container"></div>
<script src="node_modules/progressbar.js/dist/progressbar.min.js"></script>
<script src="assets/js/three.min.js"></script>
<script src="assets/js/OrbitControls.js"></script>
<script type="text/javascript">
var bar = new ProgressBar.Circle('#progressbar', {
color: '#ddd',
strokeWidth: 4,
trailWidth: 1,
easing: 'easeInOut',
duration: 1400,
text: {
autoStyleContainer: false
},
from: { color: '#333', width: 1 },
to: { color: '#fff', width: 4 },
step: function(state, circle) {
circle.path.setAttribute('stroke', state.color);
circle.path.setAttribute('stroke-width', state.width);
var value = circle.value() * 100;
if (value === 0) {
circle.setText('');
} else {
circle.setText(value.toFixed(1) + '%');
}
}
});
bar.text.style.fontFamily = '"Raleway", Helvetica, sans-serif';
bar.text.style.fontSize = '2rem';
bar.animate(1.0, function() {
bar._container.classList.add('hidden');
});
</script>
<script type="text/javascript">
var camera, renderer, analyser, data, fontObj;
var fftSize = 2048;
var textNum = fftSize / 2;
var grid = Math.sqrt(textNum);
var fontGeometries = [];
var fontMaterials = [];
var WIDTH = window.innerWidth;
var HEIGHT = window.innerHeight;
var container = document.getElementById('container');
var scene = new THREE.Scene();
window.onload = function(){
var fontLoader = new THREE.FontLoader();
fontLoader.load('assets/fonts/plstk-min.json', function(font){
fontObj = font;
for(let l = 0; l < 256; l++){
fontGeometries.push(new THREE.ShapeGeometry(fontObj.generateShapes(l, 1.5, 0)));
fontMaterials = new THREE.MeshBasicMaterial({
color: 0x006699,
opacity: 0.4,
side: THREE.DoubleSide
});
}
for(let i = 0; i < grid; i++){
for(let j = 0; j < grid; j++){
mesh = new THREE.Mesh(fontGeometries[0], fontMaterials[0]);
mesh.position.set( -(WIDTH / 2) + i * (WIDTH / grid + grid / 3) + grid * 2, (HEIGHT / 2) - j * (HEIGHT / grid + 2), 0 );
mesh.rotateX(Math.PI / 2.5);
scene.add(mesh);
}
}
init();
});
function init(){
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000);
renderer.setPixelRatio(window.devicePixelRatio);
container.appendChild(renderer.domElement);
window.addEventListener('resize', () => {
WIDTH = window.innerWidth;
HEIGHT = window.innerHeight;
renderer.setSize(WIDTH, HEIGHT);
}, false);
camera = new THREE.OrthographicCamera( WIDTH / - 2, WIDTH / 2, HEIGHT / 2, HEIGHT / - 2, 0.1, 1000 );
controls = new THREE.OrbitControls(camera);
controls.autoRotate = true;
camera.position.set( 0, -500, 500.0 );
camera.lookAt( new THREE.Vector3() );
var audioLoader = new THREE.AudioLoader();
var listener = new THREE.AudioListener();
camera.add(listener);
var audio = new THREE.Audio(listener);
analyser = new THREE.AudioAnalyser(audio, fftSize);
audioLoader.load("assets/audios/bensound-goinghigher-min.mp3", function(buffer){
audio.setBuffer(buffer);
audio.setLoop(true);
audio.play();
animate();
});
};
function animate(){
requestAnimationFrame(animate);
renderNumberMap();
};
function renderNumberMap(){
data = analyser.getFrequencyData();
for(let k = 0; k < data.length; k++){
scene.children[k].geometry = fontGeometries[data[k]];
scene.children[k].scale.setScalar((data[k] + 1) * 0.06);
scene.children[k].position.setZ(data[k] * 0.5);
scene.children[k].rotation.set(Math.PI / 2.5, 0, 0);
scene.children[k].material.color.setRGB(0.0 + (data[k] / 256), 1.0 - (data[k] / 256), 1.0 - (data[k] / 256));
if(data[k] > 200){
scene.children[k].scale.setScalar(Math.pow((data[k] + 1) * 0.06, 1.1));
}
}
renderer.render(scene, camera);
};
};
</script>
</body>
</html>