forked from jieter/leaflet-headless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (81 loc) · 2.85 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
/**
* leaflet-headless
*
* Server side leaflet with fake DOM using jsdom.
*/
var jsdom = require('jsdom');
const { JSDOM } = jsdom;
const { window } = new jsdom.JSDOM('<html><head></head><body></body></html>', {
features: {
FetchExternalResources: ['img']
}
});
const { document } = window.window;
var path = require('path');
global.window = window;
global.document = document;
global.window.navigator.userAgent = 'webkit';
global.navigator = global.window.navigator;
if (!global.L) {
// make some globals to fake browser behaviour.
//global.document = new jsdom.JSDOM('<html><head></head><body></body></html>', {
// features: {
// FetchExternalResources: ['img']
// }
//}).window;
//console.log(global.document);
//global.window = global.document;
//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;