forked from dataarts/webgl-globe
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebgl-globe.html
178 lines (146 loc) · 5.18 KB
/
webgl-globe.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
<!--
# (c) Copyright Jupyter Development Team
# (c) Copyright IBM Corp. 2015
-->
<link rel='import' href='../polymer/polymer.html'>
<script src="../threejs/build/three.js"></script>
<script src="globe/globe.js"></script>
<!--
Creates a Polymer element that displays a globe using the globe.js library
The globe accepts data via attribute `data` or 'dataUrl'. If both exist, `dataUrl` takes precedence,
and `data` is ignored.
@element webgl-globe
-->
<dom-module id="webgl-globe">
<template>
<style>
:host {
display: block;
height: 100%;
width: 100%;
}
#container canvas {
position: relative !important;
}
</style>
<div id="container"></div>
<content></content>
</template>
</dom-module>
<script>
(function () {
'use strict';
Polymer({
is: 'webgl-globe',
properties: {
/**
* Data for globe. Must be an Array of Arrays
* example: [[lat, long, magnitude, lat, long, magnitude, ...] ] where magnitude is a number between 0 and 1
*
* Will be ignored if `dataUrl` is defined
*
* @attribute data
* @type Array
*/
data: {
type: Array,
observer: '_dataChanged'
},
/**
* Data URL for globe. Must be String
* takes precedence over `data`
*
* @attribute data-url
* @type String
*/
dataUrl: String,
/**
* Texture image URL for globe. Must be String
*
* @attribute texture-url
* @type String
*/
textureUrl: String
},
behaviors: [
Polymer.IronResizableBehavior
],
listeners: {
'iron-resize': '_resizeGlobe'
},
DEFAULT_SIZE: 500,
_readDataFromUrl: function(url) {
if (this.globe) {
var xhr = new XMLHttpRequest();
xhr.open( 'GET', url, true );
xhr.onreadystatechange = function() {
if ( xhr.readyState === 4 && xhr.status === 200 ) {
var data = JSON.parse( xhr.responseText );
this.globe.addData( data, {format: 'magnitude'} );
this.globe.createPoints();
}
}.bind(this);
xhr.send( null );
}
},
_dataChanged: function(newData) {
if (!newData) { return; }
if (this.dataUrl) { return; } // if dataUrl is provided, we ignore this data change
if (this.globe) {
var data = [];
for ( var i = 0; i < newData.length; i ++ ) {
data = data.concat(newData[i]);
}
if (this.globe.points) {
this.globe.scene.remove(this.globe.points);
}
this.globe.addData(data, {format: 'magnitude'});
this.globe.createPoints();
}
},
_resizeGlobe: function() {
if(this.globe) {
var newSize = this.$.container.offsetWidth || this.DEFAULT_SIZE;
this._setContainerHeight(newSize);
this.globe.onWindowResize();
}
},
_getDimension: function() {
return this.$.container.offsetWidth || this.DEFAULT_SIZE;
},
_setContainerHeight: function(height) {
this.$.container.style.height = height + "px";
},
ready: function() {
this.async(function(){
var display = window.getComputedStyle(this).display;
if (display === 'none' || document.readyState !== 'complete') {
this.async(this.ready, 200);
} else {
var container = this.$.container;
this._setContainerHeight(this._getDimension());
// define color function for data
var colorFunction = function(x) {
var c = new THREE.Color();
c.setHSL( ( 0.6 - ( x * 0.5 ) ), 1.0, 0.625 );
return c;
};
// Make the globe
this.globe = new DAT.Globe( container, {
imgUrl: this.textureUrl || this.resolveUrl('globe/world.jpg'),
colorFn: colorFunction
});
// change canvas color to white
this.globe.renderer.setClearColor(0xffffff);
if (this.dataUrl) {
this._readDataFromUrl(this.dataUrl);
} else if (this.data) {
this._dataChanged(this.data);
}
this.globe.animate();
}
});
}
});
})();
</script>