-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
223 lines (197 loc) · 5.71 KB
/
index.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var MapboxGl = require('mapbox-gl');
var styleSpec = require("mapbox-gl/src/style-spec/reference/v8.json");
var PCancelable = require('p-cancelable');
/**
* Util
*/
function createElement(tagName, style) {
var el = document.createElement('div');
for(let styleProp in style) {
if(style.hasOwnProperty(styleProp)) {
el.style[styleProp] = style[styleProp];
}
}
return el;
}
function removeEl(el) {
if(el.parentNode) {
el.parentNode.removeChild(el);
}
}
function toPixels(length, unit) {
let conversionFactor;
if (unit === "inch") {
// https://www.unitconverters.net/typography/inch-to-pixel-x.htm
conversionFactor = 96;
}
else if (unit == 'mm') {
// https://www.unitconverters.net/typography/inch-to-millimeter.htm
conversionFactor = 96 / 25.4;
}
return (conversionFactor * length) + 'px';
}
/**
* mapbox-gl-js-render
*
* Render a mapbox-gl-js style client side.
*
* See <https://www.mapbox.com/mapbox-gl-js/api/#map> for usage
*
* @params {Array} opts.mapboxGl.center
* @params {Number} opts.mapboxGl.zoom
* @params {Object|String} opts.mapboxGl.style
* @params {Number} opts.mapboxGl.bearing
* @params {Number} opts.mapboxGl.pitch
*
* @params {Number} opts.output.dpi
* @params {String} opts.output.dimensions.unit
* @params {Number} opts.output.dimensions.width
* @params {Number} opts.output.dimensions.height
*
* @returns {Blob}
*/
var toBlob = function fn(opts, mimeType, qualityArgument) {
// HACK: Massive hack to get a unique function name we can grep for in a stack trace.
Object.defineProperty(fn, 'name', { writable: true });
var uniqueKey = "mapboxGlToBlob_"+Math.random().toString(36).substr(2, 9);
fn.name = uniqueKey;
// Copy opts for safety
var outOpts = opts.output;
var glOpts = Object.assign({
dpi: 300,
pitch: 0,
zoom: 0,
center: [0,0],
bearing: 0
}, opts.mapboxGl);
var promise = new PCancelable(function(resolve, reject, onCancel) {
function removeAllTransitions(style) {
// Global transition
style.transition = {
"duration": 0,
"delay": 0
}
// Paint specific transitions
style.layers.forEach(function(layer) {
var layerKey = "paint_"+layer.type;
Object.keys(styleSpec[layerKey]).forEach(function(key) {
if(styleSpec[layerKey][key].transition) {
layer.paint = layer.paint || {};
layer.paint[key+"-transition"] = {
"duration": 0,
"delay": 0
}
}
})
})
return style;
}
function loadStyle(style) {
return new Promise(function(resolve, reject) {
if(typeof(style) === "string") {
fetch(style)
.then(function(response) {
return response.json()
})
.then(function(style) {
resolve(style);
})
}
else {
resolve(style);
}
})
}
loadStyle(opts.mapboxGl.style)
.then(function(style) {
return removeAllTransitions(style)
})
.then(function(style) {
if(promise.isCanceled) {
// Early exit
return;
}
// Calculate pixel ratio
var actualPixelRatio = window.devicePixelRatio;
Object.defineProperty(window, 'devicePixelRatio', {
get: function() {
// HACK: We throw an error so we can see if we (this library)
// is in the stack trace. This means that `window.devicePixelRatio`
// will continue to function as normal in other code running on the
// page.
try {
throw new Error();
}
catch (e) {
if(e.stack.indexOf(uniqueKey)) {
return outOpts.dpi / 96;
}
else {
return actualPixelRatio;
}
}
}
});
// Hidden map container
var hidden = createElement("div", {
overflow: "hidden",
width: "0px",
height: "0px"
})
document.body.appendChild(hidden);
// Actual map container with dimensions set, ready for raster render
var container = createElement("div", {
width: toPixels(outOpts.dimensions.width, outOpts.dimensions.unit),
height: toPixels(outOpts.dimensions.height, outOpts.dimensions.unit)
});
hidden.appendChild(container);
var map = new MapboxGl.Map({
bearing: glOpts.bearing,
center: glOpts.center,
container: container,
pitch: glOpts.pitch,
style: style,
zoom: glOpts.zoom,
attributionControl: false,
interactive: false,
trackResize: false,
preserveDrawingBuffer: true,
fadeDuration: 0,
});
var called = false;
function onMapRendered() {
if(called) {
return;
}
called = true;
try {
map.getCanvas().toBlob(function(blob) {
cleanUp();
resolve(blob);
}, mimeType, qualityArgument);
} catch(err) {
cleanUp();
reject(err);
}
}
function cleanUp() {
map.remove();
removeEl(hidden);
Object.defineProperty(window, 'devicePixelRatio', {
get: function() {
return actualPixelRatio
}
});
}
onCancel(() => {
cleanUp();
})
map.once('idle', onMapRendered);
})
});
return promise;
}
module.exports = {
toBlob: toBlob,
toPixels: toPixels
};