-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
202 lines (174 loc) · 6.61 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Animate 3D buildings based on ambient sounds</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<!-- Include Mapbox GL JS -->
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.47.0/mapbox-gl.js'></script>
<!-- Include Mapbox GL CSS -->
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.47.0/mapbox-gl.css' rel='stylesheet' />
<script src="https://code.jquery.com/jquery-latest.js"></script>
<!-- Include CARTO VL JS -->
<script src="https://cartodb-libs.global.ssl.fastly.net/carto-vl/v0.3.0/carto-vl.js"></script>
<!-- Include CARTO JS to pull the tiles -->
<script src="https://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.core.js"></script>
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
/* global Promise */
// Use a minimal variant of the Mapbox Dark style, with certain features removed.
var map = new mapboxgl.Map({
style: 'https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json',
center: {
// lng: -74.00649562332922,
// lat: 40.70811328605049
// lng: -97.743022,
// lat: 30.268550
lng: -97.740310,
lat: 30.274668
},
zoom: 15.4,
pitch: 55,
container: 'map'
});
var nav = new mapboxgl.NavigationControl();
map.addControl(nav, 'top-right');
map.addControl(new mapboxgl.FullscreenControl());
map.on('load', function() {
var bins = 16;
var maxHeight = 150;
var binWidth = maxHeight / bins;
// define downtown austin building footprint layer and add it to the map
var layerData = {
user_name: 'tnris-flood',
sublayers: [{
sql: "SELECT * FROM downtown_austin2",
cartocss: '{}'
}],
maps_api_template: 'https://tnris-flood.carto.com'
};
cartodb.Tiles.getTiles(layerData, function (result, error) {
if (result == null) {
console.log("error: ", error.errors.join('\n'));
return;
}
// console.log("url template is ", result.tiles[0]);
var buildingTiles = result.tiles.map(function (tileUrl) {
return tileUrl
.replace('{s}', 'a')
.replace(/\.png/, '.mvt');
});
map.addSource('austin_buildings_source', { type: 'vector', tiles: buildingTiles, attribution: '© Carto' });
// Divide the buildings into 16 bins based on their true height, using a layer filter.
for (var i = 0; i < bins; i++) {
map.addLayer({
'id': 'austin_buildings-' + i,
'source': 'austin_buildings_source',
'source-layer': 'layer0',
'filter': ['all', ['>', 'bldgheight', i * binWidth], ['<=', 'bldgheight', (i + 1) * binWidth]],
'type': 'fill-extrusion',
'minzoom': 15,
'paint': {
'fill-extrusion-color': '#aaa',
'fill-extrusion-height-transition': {
duration: 0,
delay: 0
},
'fill-extrusion-opacity': .6
}
});
}
});
// var bins = 16;
// var maxHeight = 200;
// var binWidth = maxHeight / bins;
//
// // Divide the buildings into 16 bins based on their true height, using a layer filter.
// for (var i = 0; i < bins; i++) {
// map.addLayer({
// 'id': '3d-buildings-' + i,
// // 'source': 'composite',
// 'source': 'carto',
// 'source-layer': 'building',
// // 'filter': ['all', ['==', 'extrude', 'true'], ['>', 'height', i * binWidth], ['<=', 'height', (i + 1) * binWidth]],
// 'filter': ['all', ['>', 'render_height', i * binWidth], ['<=', 'render_height', (i + 1) * binWidth]],
// 'type': 'fill-extrusion',
// 'minzoom': 15,
// 'paint': {
// 'fill-extrusion-color': '#aaa',
// 'fill-extrusion-height-transition': {
// duration: 0,
// delay: 0
// },
// 'fill-extrusion-opacity': .6
// }
// });
// }
// Older browsers might not implement mediaDevices at all, so we set an empty object first
if (navigator.mediaDevices === undefined) {
navigator.mediaDevices = {};
}
// Some browsers partially implement mediaDevices. We can't just assign an object
// with getUserMedia as it would overwrite existing properties.
// Here, we will just add the getUserMedia property if it's missing.
if (navigator.mediaDevices.getUserMedia === undefined) {
navigator.mediaDevices.getUserMedia = function (constraints) {
// First get ahold of the legacy getUserMedia, if present
var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
// Some browsers just don't implement it - return a rejected promise with an error
// to keep a consistent interface
if (!getUserMedia) {
return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
}
// Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
return new Promise(function (resolve, reject) {
getUserMedia.call(navigator, constraints, resolve, reject);
});
};
}
navigator.mediaDevices.getUserMedia({audio: true})
.then(function (stream) {
// Set up a Web Audio AudioContext and AnalyzerNode, configured to return the
// same number of bins of audio frequency data.
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var analyser = audioCtx.createAnalyser();
analyser.minDecibels = -90;
analyser.maxDecibels = -10;
analyser.smoothingTimeConstant = 0.85;
var source = audioCtx.createMediaStreamSource(stream);
source.connect(analyser);
analyser.fftSize = bins * 2;
var dataArray = new Uint8Array(bins);
function draw(now) {
analyser.getByteFrequencyData(dataArray);
// Use that data to drive updates to the fill-extrusion-height property.
var avg = 0;
for (var i = 0; i < bins; i++) {
avg += dataArray[i];
map.setPaintProperty('austin_buildings-' + i, 'fill-extrusion-height', 10 + 4 * i + dataArray[i]);
}
avg /= bins;
// Animate the map bearing and light color over time, and make the light more
// intense when the audio is louder.
map.setBearing(now / 500);
map.setLight({
color: "hsl(" + now / 100 % 360 + "," + Math.min(50 + avg / 4, 100) + "%,50%)",
intensity: Math.min(1, avg / 256 * 10)
});
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
})
.catch(function (err) {
console.log('The following gUM error occured: ' + err);
});
});
</script>
</body>
</html>