-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.html
187 lines (158 loc) · 7.45 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
<!DOCTYPE html>
<html>
<head>
<title>MercatorGL</title>
<meta charset="utf-8">
<link rel="stylesheet" href="site/css/docco.css">
<link rel="stylesheet" href="site/css/mercator-gl.css">
<script src="site/js/highlight.min.js"></script>
</head>
<body>
<a href="https://github.com/tsherif/mercator-gl"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
<h1>MercatorGL</h1>
<div class="description">
<a class="badge-link" href="https://travis-ci.com/tsherif/mercator-gl">
<img src="https://img.shields.io/travis/com/tsherif/mercator-gl">
</a>
<a href="https://coveralls.io/github/tsherif/mercator-gl?branch=master">
<img src="https://img.shields.io/coveralls/github/tsherif/mercator-gl" alt="Coverage Status" />
</a>
<a class="badge-link" href="https://github.com/tsherif/mercator-gl/blob/master/build/mercator-gl.min.js">
<img src="https://badge-size.herokuapp.com/tsherif/mercator-gl/master/build/mercator-gl.min.js.svg?compression=gzip">
</a>
<a class="badge-link" href="https://github.com/tsherif/mercator-gl/blob/master/LICENSE">
<img src="https://img.shields.io/github/license/tsherif/mercator-gl.svg">
</a>
<a class="badge-link" href="https://www.npmjs.com/package/mercator-gl">
<img src="https://img.shields.io/npm/v/mercator-gl.svg">
</a>
</div>
<div class="description">
<a class="resources-link" href="./docs/">
API Docs
</a>
</div>
<div class="description">
MercatorGL is a minimal library for calculating web mercator projections on a GPU using WebGL. It provides utilities to inject GLSL code for projecting longitude/latitude coordinates into already exisiting vertex shader code and calculate the uniforms it requires. MercatorGL focuses on numerical stability by performing most calculations at 64-bit precision, and switching to an "offset mode" at higher zoom levels (using a technique borrowed from <a href="https://medium.com/vis-gl/how-sometimes-assuming-the-earth-is-flat-helps-speed-up-rendering-in-deck-gl-c43b72fd6db4">deck.gl</a>).
</div>
<div class="description">
Sample usage with <a href="https://docs.mapbox.com/mapbox-gl-js/api/">MapboxGL</a>:
</div>
<pre><code class="javascript">
let map = new mapboxgl.Map({
container: mapboxContainer,
style: "mapbox://styles/mapbox/streets-v9",
center: [-73.982130, 40.762896],
zoom: 15
});
// NOTE: MercatorGL works with both GLSL 1 and 3 shaders
let vs = `
#version 300 es
layout(location=0) in vec2 lngLatPosition;
void main() {
// mercator_gl_lngLatToClip function injected by injectMercatorGLSL().
// mercator_gl_lngLatToMercator and mercator_gl_mercatorToClip also available to do
// projection in multiple steps.
gl_Position = mercator_gl_lngLatToClip(position);
}
`;
let fs = `
#version 300 es
precision highp float;
uniform vec4 color;
out vec4 fragColor;
void main() {
fragColor = color;
}
`;
// Insert projection functions into vertex shader
let vertexShaderSource = injectMercatorGLSL(vs);
let fragmentShaderSource = fs;
// Create WebGL program from vertexShaderSource and fragmentShaderSource
let uniforms = {
// An application uniform, not used by MercatorGL
color: new Float32Array(1.0, 0.0, 0.0, 1.0);
};
// Uniforms used by MercatorGL are added to the map.
allocateMercatorUniforms(uniforms);
map.on("render", (e) => {
let center = map.getCenter().toArray();
let zoom = map.getZoom();
// Update the values of MercatorGL uniforms in the map (including projection matrix provided by Mapbox).
// The application must use the map to update program uniforms used by MercatorGL.
updateMercatorUniforms(uniforms, center, zoom, map.transform.projMatrix);
// Draw to canvas
});
</code></pre>
<div class="description">
MercatorGL can be used directly by downloading the <a href="https://raw.githubusercontent.com/tsherif/mercator-gl/master/build/mercator-gl.min.js">built source</a> and loading it via a script tag:
</div>
<pre><code class="html">
<script src="js/mercator-gl.min.js"></script>
</code></pre>
<div class="description">
or it can be installed via <a href="https://www.npmjs.com/package/mercator-gl">npm</a>:
</div>
<pre><code class="bash">
npm install mercator-gl
</code></pre>
<div class="description">
and loaded via ES6-style <code>import</code>:
</div>
<pre><code class="javascript">
import MercatorGL from "mercator-gl";
</code></pre>
<h2>Examples</h2>
<a href="#" class="example-menu-button" id="triangle">Triangle</a>
<a href="#" class="example-menu-button" id="scatterplot">Scatterplot</a>
<a href="#" class="example-menu-button" id="arc">Arcs</a>
<a href="#" class="example-menu-button" id="columns">Columns</a>
<a href="#" class="example-menu-button" id="geojson">GeoJSON</a>
<div id="example-div">
<div id="example-title"></div>
<iframe id="examples"></iframe>
</div>
<script>
var exampleFrame = document.getElementById("examples");
var exampleTitle = document.getElementById("example-title");
var titles = {
triangle: "Midtown Triangle, New York City",
scatterplot: "Bike Racks, San Francisco",
arc: "Bixi Trips, Montreal",
columns: "Polulation Density, Egypt",
geojson: "Neighborhoods, Baghdad"
}
document.body.addEventListener("click", function(event) {
if (event.target.className !== "example-menu-button") {
return;
}
var link = event.target;
var id = link.id;
var title = titles[id];
var src = "examples/" + id + ".html";
var sourceCodeURL = "https://github.com/tsherif/mercator-gl/blob/master/" + src;
exampleTitle.innerHTML = title + "<div><a href=\"" + sourceCodeURL + "\">Source code</a>";
examples.src = src;
event.stopPropagation();
event.preventDefault();
});
var canvas = document.createElement("canvas");
var gl = canvas.getContext("webgl");
if (gl) {
var buttons = document.getElementsByClassName("example-menu-button");
var exampleButton = buttons[Math.floor(Math.random() * buttons.length)];
exampleButton.click();
function testAll() {
var index = 0;
setInterval(function() {
buttons[index].click();
index = (index + 1) % buttons.length;
}, 2000);
}
} else {
document.getElementById("example-div").innerHTML = "Sorry your browser does not support WebGL.";
}
hljs.initHighlightingOnLoad();
</script>
</body>
</html>