-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
170 lines (150 loc) · 5.54 KB
/
main.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
var hours = [];
var time = 0;
var pause = false;
var selected = -1;
var displayContainer = null;
var display = null;
// Init tangram
map = (function () {
'use strict';
var map_start_location = [39.825, -98.170, 5];
// Create a Leaflet Map
var map = L.map('map',{
trackResize: true,
keyboard: false,
dragging: (window.self !== window.top && L.Browser.touch) ? false : true,
tap: (window.self !== window.top && L.Browser.touch) ? false : true,
});
var url_hash = window.location.hash.slice(1).split('/');
if (url_hash.length == 3) {
map_start_location = [url_hash[1], url_hash[2], url_hash[0]];
// convert from strings
map_start_location = map_start_location.map(Number);
}
// Create a Tangram Layer
var layer = Tangram.leafletLayer({
scene: 'scene.yaml',
attribution: '<a href="https://twitter.com/patriciogv" target="_blank">@patriciogv</a> | <a href="https://mapzen.com/tangram" target="_blank">Tangram</a> | <a href="https://mapzen.com/" target="_blank">Mapzen</a> | © OSM contributors | <a href="http://stamen.com/" target="_blank">Stamen</a>'
});
window.layer = layer;
var scene = layer.scene;
window.scene = scene;
map.setView(map_start_location.slice(0, 2), map_start_location[2]);
var hash = new L.Hash(map);
/***** Once the page is loaded is time to initialize the routines that handles the interaction *****/
window.addEventListener('load', function () {
// Add Tangram `layer` to Leaflet `map`
layer.addTo(map);
setTimeout(init, 2000);
});
return map;
}());
function init() {
// Make TimeLine
fetch('data/hours.json')
.then(function (response) {
// If we get a positive response...
if (response.status !== 200) {
console.log('Error getting data. Status code: ' + response.status);
return;
}
// ... parse it to JSON
return response.json();
})
.then(function(json) {
hours = json;
var timeSlider = document.getElementById('time');
var dataLabel = document.getElementById('date');
var slider = noUiSlider.create(timeSlider, {
animate: false,
start: 0,
step: 0.04,
range: {
'min': 0,
'max': hours.length-1
},
pips: {
mode: 'positions',
values: [0,25,50,75,100],
density: 4,
format: wNumb({
decimals: 0,
edit: function ( value ) {
return formatTime(value);
}
})
}
});
window.timeSlider = timeSlider;
timeSlider.noUiSlider.on('update', function( values, handle ) {
var t = parseFloat(values);
scene.styles.wind.shaders.uniforms.u_offset = t;
dataLabel.innerHTML = formatTime(values, true);
if (display) {
display.setUniform('u_hour',t);
}
});
timeSlider.noUiSlider.on('start', function() {
pause = true;
});
timeSlider.noUiSlider.on('end', function() {
time = parseFloat(timeSlider.noUiSlider.get());
pause = false;
});
})
// Load the image with the data
var downloadingImage = new Image();
downloadingImage.onload = function(){
scene.styles.wind.shaders.uniforms.u_param = [this.width,this.height];
};
downloadingImage.src = 'data/data.png'
window.requestAnimationFrame(update);
var dataDisplay = document.getElementById('display_shader');
display = new GlslCanvas(dataDisplay);
// display.setUniform('u_texture','data/data.png',{filtering:'nearest'});
displayContainer = document.getElementById('display');
displayContainer.style.visibility = 'hidden';
layer.setSelectionEvents({
click: function(selection) {
if (selection.feature && selected !== selection.feature.properties.id) {
selected = selection.feature.properties.id;
scene.config.global.hovered = selected;
scene.rebuild();
if (displayContainer.style.visibility === 'hidden') {
displayContainer.style.visibility = 'visible';
}
display.setUniform('u_id',selection.feature.properties.id)
display.canvas.style.height = "50px";
}
else if (!selection.feature && selected !== -1) {
selected = -1;
scene.config.global.hovered = selected;
scene.rebuild();
displayContainer.style.visibility = 'hidden';
}
}
});
}
function update() {
if (!pause) {
if (typeof time === 'string') {
time = parseFloat(time);
}
time += .1;
if (time > hours.length) {
time = 0;
}
if (window.timeSlider) {
window.timeSlider.noUiSlider.set(time);
}
}
window.requestAnimationFrame(update);
}
function formatTime(values, showHour) {
var date = hours[Math.floor(values)].split('-');
var time = date[1]+'/'+date[2]+'/'+date[0]
if (showHour) {
time += ' '+date[3]+'hs';
}
return time;
}