-
Notifications
You must be signed in to change notification settings - Fork 0
/
solar-installs.html
186 lines (159 loc) · 5.73 KB
/
solar-installs.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
<!DOCTYPE html>
<html>
<head>
<title>Solar Installs</title>
<style>
html, body, #map-div {
margin: 0;
padding: 0;
height: 100%;
}
</style>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false&v=3.20"></script>
<script type="text/javascript" src="CanvasLayer.js"></script>
<script type="text/javascript" src="Glb.js"></script>
<script id="vertex-shader" type="x-shader/x-vertex">
attribute vec4 worldCoord;
uniform mat4 mapMatrix;
uniform float uSize;
void main() {
gl_Position = mapMatrix * worldCoord;
gl_PointSize = uSize;
}
</script>
<script id="fragment-shader" type="x-shader/x-vertex">
precision mediump float;
void main() {
float dist = length(gl_PointCoord.xy - vec2(.5, .5));
dist = 1. - (dist * 2.);
dist = max(0., dist);
gl_FragColor = vec4(.08, .86, .34, 1.) * dist;
}
</script>
<script type="text/javascript">
var map;
var canvasLayer;
var gl;
var solarInstalls = {
url: "solar-installs.bin"
};
var pixelsToWebGLMatrix = new Float32Array(16);
var mapMatrix = new Float32Array(16);
var countryPointSizePixels = 5;
var blockPointSizePixels = 20;
var pointSize;
function init() {
map = initMap();
canvasLayer = initCanvasLayer(map, update, resize);
gl = canvasLayer.canvas.getContext('experimental-webgl');
gl.enable(gl.BLEND);
gl.blendFunc( gl.SRC_ALPHA, gl.ONE );
solarInstalls.glb = new Glb(gl);
solarInstalls.program = solarInstalls.glb.programFromSources(document.getElementById('vertex-shader').text,
document.getElementById('fragment-shader').text);
load(solarInstalls);
}
document.addEventListener('DOMContentLoaded', init, false);
window.addEventListener('resize', function () { google.maps.event.trigger(map, 'resize') }, false);
function initMap() {
// initialize the map
var mapOptions = {
zoom: 2,
center: new google.maps.LatLng(0.0, -20.0)
};
var mapDiv = document.getElementById('map-div');
return new google.maps.Map(mapDiv, mapOptions);
}
function initCanvasLayer(map, update, resize) {
var canvasLayerOptions = {
map: map,
resizeHandler: resize,
animate: false,
updateHandler: update
};
return new CanvasLayer(canvasLayerOptions);
}
function load(obj) {
var xhr = new XMLHttpRequest();
xhr.open('GET', obj.url);
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
var float32Array = new Float32Array(this.response);
obj.buffer = float32Array;
setData(obj);
}
xhr.send();
}
function setData(obj) {
obj.pointCount = obj.buffer.length / 3;
obj.data = obj.buffer;
obj.arrayBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, obj.arrayBuffer);
gl.bufferData(gl.ARRAY_BUFFER, obj.data, gl.STATIC_DRAW);
var attributeLoc = gl.getAttribLocation(obj.program, 'worldCoord');
gl.enableVertexAttribArray(attributeLoc);
gl.vertexAttribPointer(attributeLoc, 2, gl.FLOAT, false, 12, 0);
obj.ready = true;
update();
}
function resize() {
console.log('resizeHandler');
var w = gl.canvas.width;
var h = gl.canvas.height;
gl.viewport(0, 0, w, h);
// matrix which maps pixel coordinates to WebGL coordinates
pixelsToWebGLMatrix.set([2/w, 0, 0, 0,
0, -2/h, 0, 0,
0, 0, 0, 0,
-1, 1, 0, 1]);
}
function update() {
if (solarInstalls.ready) {
gl.clear(gl.COLOR_BUFFER_BIT);
var mapProjection = map.getProjection();
mapMatrix.set(pixelsToWebGLMatrix);
var scale = canvasLayer.getMapScale();
scaleMatrix(mapMatrix, scale, scale);
var translation = canvasLayer.getMapTranslation();
translateMatrix(mapMatrix, translation.x, translation.y);
draw(mapMatrix);
}
}
function scaleMatrix(matrix, scaleX, scaleY) {
matrix[0] *= scaleX;
matrix[1] *= scaleX;
matrix[2] *= scaleX;
matrix[3] *= scaleX;
matrix[4] *= scaleY;
matrix[5] *= scaleY;
matrix[6] *= scaleY;
matrix[7] *= scaleY;
}
function translateMatrix(matrix, tx, ty) {
matrix[12] += matrix[0]*tx + matrix[4]*ty;
matrix[13] += matrix[1]*tx + matrix[5]*ty;
matrix[14] += matrix[2]*tx + matrix[6]*ty;
matrix[15] += matrix[3]*tx + matrix[7]*ty;
}
function draw(transform) {
pointSize = countryPointSizePixels * Math.pow(blockPointSizePixels / countryPointSizePixels, (map.zoom - 1) / (21 - 1));
gl.useProgram(solarInstalls.program);
gl.bindBuffer(gl.ARRAY_BUFFER, solarInstalls.arrayBuffer);
gl.bufferData(gl.ARRAY_BUFFER, solarInstalls.data, gl.STATIC_DRAW);
var attributeLoc = gl.getAttribLocation(solarInstalls.program, 'worldCoord');
gl.enableVertexAttribArray(attributeLoc);
gl.vertexAttribPointer(attributeLoc, 2, gl.FLOAT, false, 12, 0);
var sizeLoc = gl.getUniformLocation(solarInstalls.program, 'uSize');
gl.uniform1f(sizeLoc, pointSize);
var matrixLoc = gl.getUniformLocation(solarInstalls.program, 'mapMatrix');
gl.uniformMatrix4fv(matrixLoc, false, transform);
gl.bindBuffer(gl.ARRAY_BUFFER, solarInstalls.arrayBuffer);
gl.bufferData(gl.ARRAY_BUFFER, solarInstalls.data, gl.STATIC_DRAW);
gl.drawArrays(gl.POINTS, 0, solarInstalls.pointCount);
}
</script>
</head>
<body>
<div id="map-div"></div>
</body>
</html>