2.1.0 • Published 5 years ago

leaflet-customlayer v2.1.0

Weekly downloads
25
License
MIT
Repository
github
Last release
5 years ago

Leaflet.CustomLayer

Build Status npm version npm downloads npm license

Leaflet overlay plugin: L.CustomLayer - fully custom Layer.

Features

  • A custom layer is a layer that develops a defined drawing method. (For example: Canvas\SVG\Image\Video\DIV Layer)

Screenshot canvasLayer

Screenshot svgLayerLayer

Installation

Using npm:

$ npm install leaflet-customlayer

or Yarn:

$ yarn add leaflet-customlayer

Using cdn:

<script type="text/javascript" src="https://unpkg.com/leaflet-customlayer@2.1.0/dist/Leaflet.CustomLayer.js"></script>

Example

var customLayer = new L.customLayer({
  container: document.createElement(element), // The DomElement object to display.
  minZoom: 0, // Minimum zoom level of the layer.
  maxZoom: 18, // Maximum zoom level of the layer.
  opacity: 1, // Opacity of the layer.
  visible: true, // Visible of the layer.
  zIndex: 100 // The explicit zIndex of the layer.
});

customLayer.on("layer-beforemount", function() {
  console.log("layerBeforeMount");
});

customLayer.on("layer-mounted", function() {
  console.log("layerMounted");
})

customLayer.on("layer-render", function() {
  console.log("layerRender");
});

customLayer.on("layer-beforedestroy", function() {
  console.log("layerBeforeDestroy");
});

customLayer.on("layer-destroyed", function() {
  console.log("layerDestroyed");
});

customLayer.addTo(map);

Custom canvas layer example

var canvasLayer = new L.CustomLayer({
  container: document.createElement("canvas"),
  padding: 0.1
});

canvasLayer.on("layer-render", function() {
  var canvas = this.getContainer();
  var dpr = L.Browser.retina ? 2 : 1;
  var size = this._bounds.getSize();
  var padding = this._padding;

  // set Size
  canvas.width = dpr * size.x;
  canvas.height = dpr * size.y;
  canvas.style.width = size.x + "px";
  canvas.style.height = size.y + "px";
    
  var ctx = canvas.getContext("2d");

  // HD adaptation
  if (L.Browser.retina) ctx.scale(dpr, dpr);
  ctx.translate(padding.x, padding.y);
  
  // draw
  var point = this._map.latLngToContainerPoint({
    lat: 39.910088,
    lng: 116.401601
  });
  ctx.fillStyle = "rgb(0, 100, 255)";
  ctx.fillRect(point.x, point.y, 100, 100);
});

canvasLayer.addTo(map);

setFullLayerBounds util usage

Automatically set the full screen container size using the setfulllayerBounds method.

var canvasLayer = new L.CustomLayer({
  container: document.createElement("canvas"),
  padding: 0.1
});

canvasLayer.on("layer-render", function() {
  var { ctx } = this.setFullLayerBounds();

  // draw
  var point = this._map.latLngToContainerPoint({
    lat: 39.910088,
    lng: 116.401601
  });
  ctx.fillStyle = "rgb(0, 100, 255)";
  ctx.fillRect(point.x, point.y, 100, 100);
});

canvasLayer.addTo(map);

The setfulllayerBounds method can automatically set the container size. However, due to conditional restrictions, when it is not satisfied with the usage scenario, manually set the container size.

API

CustomLayer

Leaflet overlay plugin: L.CustomLayer - fully custom Layer. Extends L.Layer.

Options

OptionDescriptionTypeDefault
containerThe DomElement object to display.DomElement-
minZoomMinimum zoom level of the layer.Number*
maxZoomMaximum zoom level of the layer.Number*
opacityOpacity of the layer.Number1
visibleVisible of the layer.Booleantrue
zIndexThe explicit zIndex of the layer.Numbercanvas: 100\ svg: 200\ other: 100
paddingHow much to extend the clip area around the map view (relative to its size) e.g. 0.1 would be 10% of map view in each direction.Number0
toleranceHow much to extend click tolerance round a path/object on the map.Number0

Methods

MethodDescriptionReturn
addTo()layer add to the map
remove()layer remove the map
getContainer()Get the DomElement objectDomElement
setContainer(container)Set the DomElement objectthis
getOpacity()Get the opacity of the layer.Number
setOpacity(opacity)Set the opacity of the layer.this
getZIndex()Get the zIndex of the layer.Number
setZIndex(zIndex)Set the zIndex of the layer.this
show()layer showthis
hide()layer hidethis

Util Methods

MethodDescriptionReturn
setFullLayerBounds()set the full bounds of the layer.{ container, ... }

Events

EventDataDescription
layer-beforemountEventFired before the layer is mounting begins.
layer-mountedEventFired after the layer is has been mounted.
layer-renderEventFired when the layer render its bounds, center and zoom, for example when its map has moved.
layer-beforedestroyEventFired before the layer is destroyed.
layer-destroyedEventFired after the layer is has been destroyed.

License

MIT