A HTML canvas layer to render large amounts of geojson data.
/**
* note unlike most DOM based map event handling which return only
* the top feature, the L.CanvasGeojsonLayer returns all features
* the are moused over or clicked.
*/
var canvasLayer = new L.CanvasGeojsonLayer({
onMouseOver : function(features) {
// handle mouseover events
},
onMouseOut : function(features) {
// handle mouseout events
},
onClick : function(features) {
// handle mouse click events
}
});
// add to map
canvasLayer.addTo(map);
/**
* This is just shorthand for creating a bunch of L.CanvasFeature
* or L.CanvasFeatureCollection objects from your geojson
*/
canvasLayer.addCanvasFeatures(L.CanvasFeatureFactory(geojsonData));
// first render
canvasLayer.render();
There are two ways two the geojson:
- Assign a renderer to the layer
canvasLayer.renderer = function() {}
- Assign a renderer to the L.CanvasFeature
canvasFeature.renderer = function() {}
If a CanvasFeature render is provided, it will be used instead of the layer render.
The renderer function should look like:
/**
* ctx - The canvas elements 2d context
* xyPoints - either object or array
* for Point: {x: number, y: number}
* for Linestring: array of points
* for Polygon: array of points
* for Multipolygon: array of Polygons
* map - canvas layers map
* geojson - actual geojson object passed to L.CanvasFeature
*
* The function will be called in scope of the CanvasFeature. So
* [this] will access the CanvasFeature
*/
function render(ctx, xyPoints, map, geojson) {
// to stuff here
}
See /src/defaultRenderer/index.js for full example. This is also the layer default if no renderer is assigned.
Add array of CanvasFeatures to layer
Add single L.CanvasFeature or L.CanvasFeatureCollection
remove single L.CanvasFeature or L.CanvasFeatureCollection
Remove all canvas layer features
Returns a L.CanvasFeature or L.CanvasFeatureCollection. Useful for when you need to lookup a Feature to remove or update the visibility
Render to GeoJSON
If you want to hide/show a feature, the recommended method is to use the visible flag with the associated L.CanvasFeature. Not only will the Feature not be drawn, but this flag will save the layer from reprojecting the feature.