Skip to content

Commit

Permalink
Add support for Leaflet v1.0 (mapbox#65)
Browse files Browse the repository at this point in the history
* Ignore NPM files

* Be more defensive about processing overlays

Leaflet 1.0 allows the coexistence of both Canvas and SVG overlays in the same map.  This prevents leaflet-image from throwing when it encounters a non-canvas element at `overlayPane.firstChild`.

* Fix reference to removed Leaflet API

* Fix erroneous offsetting when exporting maps in Retina

Going to be 100% honest here...  I have no idea why this works.

* Rebuild

* Update readme
  • Loading branch information
David Bazile authored and tmcw committed Apr 9, 2016
1 parent ff23494 commit bec29a2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ Canvas and [CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing).
don't, so they aren't supported.
* Your browser must support [CORS](http://caniuse.com/#feat=cors) and [Canvas](http://caniuse.com/#feat=canvas),
so `IE >= 10` with no exceptions.
* You must set `L_PREFER_CANVAS = true;` so that vector layers are drawn in Canvas
rather than SVG or VML.
* __(Leaflet < 1.0.0)__ You must set `L_PREFER_CANVAS = true;` so that vector
layers are drawn in Canvas
* __(Leaflet >= 1.0.0)__ You must set `renderer: L.canvas()` for any layer that
you want included in the generated image.
* This library **does not rasterize HTML** because **browsers cannot rasterize HTML**. Therefore,
L.divIcon and other HTML-based features of a map, like zoom controls or legends, are not
included in the output, because they are HTML.
Expand Down
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ module.exports = function leafletImage(map, callback) {
map.eachLayer(drawTileLayer);
if (map._pathRoot) {
layerQueue.defer(handlePathRoot, map._pathRoot);
} else if (map._panes && map._panes.overlayPane.firstChild) {
layerQueue.defer(handlePathRoot, map._panes.overlayPane.firstChild);
} else if (map._panes) {
var firstCanvas = map._panes.overlayPane.getElementsByTagName('canvas').item(0);
if (firstCanvas) { layerQueue.defer(handlePathRoot, firstCanvas); }
}
map.eachLayer(drawMarkerLayer);
layerQueue.awaitAll(layersDone);
Expand Down Expand Up @@ -57,7 +58,8 @@ module.exports = function leafletImage(map, callback) {
}

function handleTileLayer(layer, callback) {
var isCanvasLayer = (layer instanceof L.TileLayer.Canvas),
// `L.TileLayer.Canvas` was removed in leaflet 1.0
var isCanvasLayer = (L.TileLayer.Canvas && layer instanceof L.TileLayer.Canvas),
canvas = document.createElement('canvas');

canvas.width = dimensions.x;
Expand Down Expand Up @@ -172,7 +174,7 @@ module.exports = function leafletImage(map, callback) {
canvas.height = dimensions.y;
var ctx = canvas.getContext('2d');
var pos = L.DomUtil.getPosition(root).subtract(bounds.min).add(origin);
ctx.drawImage(root, pos.x, pos.y);
ctx.drawImage(root, pos.x, pos.y, canvas.width - (pos.x * 2), canvas.height - (pos.y * 2));
callback(null, {
canvas: canvas
});
Expand Down
10 changes: 6 additions & 4 deletions leaflet-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ module.exports = function leafletImage(map, callback) {
map.eachLayer(drawTileLayer);
if (map._pathRoot) {
layerQueue.defer(handlePathRoot, map._pathRoot);
} else if (map._panes && map._panes.overlayPane.firstChild) {
layerQueue.defer(handlePathRoot, map._panes.overlayPane.firstChild);
} else if (map._panes) {
var firstCanvas = map._panes.overlayPane.getElementsByTagName('canvas').item(0);
if (firstCanvas) { layerQueue.defer(handlePathRoot, firstCanvas); }
}
map.eachLayer(drawMarkerLayer);
layerQueue.awaitAll(layersDone);
Expand Down Expand Up @@ -59,7 +60,8 @@ module.exports = function leafletImage(map, callback) {
}

function handleTileLayer(layer, callback) {
var isCanvasLayer = (layer instanceof L.TileLayer.Canvas),
// `L.TileLayer.Canvas` was removed in leaflet 1.0
var isCanvasLayer = (L.TileLayer.Canvas && layer instanceof L.TileLayer.Canvas),
canvas = document.createElement('canvas');

canvas.width = dimensions.x;
Expand Down Expand Up @@ -174,7 +176,7 @@ module.exports = function leafletImage(map, callback) {
canvas.height = dimensions.y;
var ctx = canvas.getContext('2d');
var pos = L.DomUtil.getPosition(root).subtract(bounds.min).add(origin);
ctx.drawImage(root, pos.x, pos.y);
ctx.drawImage(root, pos.x, pos.y, canvas.width - (pos.x * 2), canvas.height - (pos.y * 2));
callback(null, {
canvas: canvas
});
Expand Down

0 comments on commit bec29a2

Please sign in to comment.