-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
81 lines (69 loc) · 2.44 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
/**
* leaflet-headless
*
* Server side leaflet with fake DOM using jsdom.
*/
var jsdom = require('jsdom').jsdom;
var path = require('path');
if (!global.L) {
// make some globals to fake browser behaviour.
global.document = jsdom('<html><head></head><body></body></html>', {
features: {
FetchExternalResources: ['img']
}
});
global.window = global.document.defaultView;
global.window.navigator.userAgent = 'webkit';
global.navigator = global.window.navigator;
global.Image = require('./src/image.js');
global.L_DISABLE_3D = true;
global.L_NO_TOUCH = true;
var leafletPath = require.resolve('leaflet');
var L = require(leafletPath);
global.L = L;
var scriptLength = leafletPath.split(path.sep).slice(-1)[0].length;
L.Icon.Default.imagePath = 'file://' + leafletPath.substring(0, leafletPath.length - scriptLength) + 'images/';
// Monkey patch Leaflet
var originalInit = L.Map.prototype.initialize;
L.Map.prototype.initialize = function (id, options) {
options = L.extend(options || {}, {
fadeAnimation: false,
zoomAnimation: false,
markerZoomAnimation: false,
preferCanvas: true
});
return originalInit.call(this, id, options);
}
// jsdom does not have clientHeight/clientWidth on elements.
// Adjust size with L.Map.setSize()
L.Map.prototype.getSize = function () {
if (!this._size || this._sizeChanged) {
this._size = new L.Point(1024, 1024);
this._sizeChanged = false;
}
return this._size.clone();
};
L.Map.prototype.setSize = function (width, height) {
this._size = new L.Point(width, height);
// reset pixelOrigin
this._resetView(this.getCenter(), this.getZoom());
return this;
};
L.Map.prototype.saveImage = function (outfilename, callback) {
var leafletImage = require('leaflet-image');
var fs = require('fs');
leafletImage(this, function (err, canvas) {
if (err) {
console.error(err);
return;
}
var data = canvas.toDataURL().replace(/^data:image\/\w+;base64,/, '');
fs.writeFile(outfilename, new Buffer(data, 'base64'), function () {
if (callback) {
callback(outfilename);
}
});
});
};
}
module.exports = global.L;