-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.js
162 lines (139 loc) · 5.4 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
/*jslint browser: true*/
/*global Tangram, gui */
map = (function () {
'use strict';
var map_start_location = [40.70531887544228, -74.00976419448853, 16]; // NYC
/*** URL parsing ***/
// leaflet-style URL hash pattern:
// #[zoom],[lat],[lng]
var url_hash = window.location.hash.slice(1, window.location.hash.length).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);
}
/*** Map ***/
var map = L.map('map',
{"keyboardZoomOffset" : .05}
);
var layer = Tangram.leafletLayer({
scene: 'scene.yaml',
attribution: '<a href="https://mapzen.com/tangram" target="_blank">Tangram</a> | © OSM contributors | <a href="https://mapzen.com/" target="_blank">Mapzen</a>'
});
window.layer = layer;
var scene = layer.scene;
window.scene = scene;
// setView expects format ([lat, long], zoom)
map.setView(map_start_location.slice(0, 3), map_start_location[2]);
var hash = new L.Hash(map);
// GUI options for rendering modes/effects
var style_controls = {
'Default' : function() {
this.setstyle(Object.keys(scene.config.layers), undefined);
},
'Elevator' : function() {
this.setstyle(['buildings'], 'elevator');
},
'Popup' : function() {
this.setstyle(['buildings'], 'popup');
},
'radius': 300,
'height': 3,
'Rainbow' : function() {
this.setstyle(['buildings','landuse'], 'rainbow');
},
'Dots' : function() {
this.setstyle(['buildings','landuse'], 'dots');
},
'Halftone' : function() {
this.setstyle(Object.keys(scene.config.layers), 'halftone');
},
'dot_frequency' : 100,
'dot_scale' : 1.5,
'Colorhalftone' : function() {
this.setstyle(Object.keys(scene.config.layers), 'colorhalftone');
},
'color_dot_frequency' : 50,
'color_dot_scale' : 1.5,
'Windows' : function() {
this.setstyle(['buildings'], 'windows', 'isometric');
},
setstyle: function (layers, newstyle, camera) {
// Restore initial state
for (var i in scene.config.layers) {
try {
scene.config.layers[i].draw.polygons.style = undefined;
}
catch(e) {
scene.config.layers[i].draw.lines.style = undefined;
}
};
// Apply new style
for (var j=0; j < layers.length; j++) {
try {
scene.config.layers[layers[j]].draw.polygons.style = newstyle;
}
catch(e) {
scene.config.layers[layers[j]].draw.lines.style = newstyle;
}
}
// Set camera
if (camera) {
scene.setActiveCamera(camera);
} else {
scene.setActiveCamera("perspective");
}
// Recompile/rebuild
scene.rebuild();
}
};
// Create dat GUI
var gui = new dat.GUI({ autoPlace: true });
function addGUI () {
gui.domElement.parentNode.style.zIndex = 500;
window.gui = gui;
var folder = gui.addFolder("Click a style:");
folder.open(); // this just points the arrow downward
// Styles
gui.add(style_controls, 'Default');
gui.add(style_controls, 'Elevator');
gui.add(style_controls, 'Popup');
gui.add(style_controls, 'radius', 0, 500).name(" radius").onChange(function(value) {
scene.styles.popup.shaders.uniforms.u_popup_radius = value;
scene.requestRedraw();
});
gui.add(style_controls, 'height', 0, 5).name(" amount").onChange(function(value) {
scene.styles.popup.shaders.uniforms.u_popup_height = value;
scene.requestRedraw();
});
gui.add(style_controls, 'Rainbow');
gui.add(style_controls, 'Halftone');
gui.add(style_controls, 'dot_frequency', 1, 200).name(" frequency").onChange(function(value) {
scene.styles.halftone.shaders.uniforms.dot_frequency = value;
scene.requestRedraw();
});
gui.add(style_controls, 'dot_scale', 0, 10).name(" scale").onChange(function(value) {
scene.styles.halftone.shaders.uniforms.dot_scale = value;
scene.requestRedraw();
});
gui.add(style_controls, 'Colorhalftone');
gui.add(style_controls, 'color_dot_frequency', 0, 100).name(" frequency").onChange(function(value) {
scene.styles.colorhalftone.shaders.uniforms.dot_frequency = value;
scene.requestRedraw();
});
gui.add(style_controls, 'color_dot_scale', 0, 3).name(" scale").onChange(function(value) {
scene.styles.colorhalftone.shaders.uniforms.dot_scale = value;
scene.requestRedraw();
});
gui.add(style_controls, 'Windows');
}
/***** Render loop *****/
window.addEventListener('load', function () {
// Scene initialized
layer.on('init', function() {
addGUI();
});
layer.addTo(map);
});
return map;
}());