0.0.7 • Published 4 months ago

leaflet-swipe-mode v0.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Leaflet.SwipeMode

A Leaflet control to add a split screen to compare two map overlays.

Requires Leaflet 1.0.0+ branches

Demo

checkout the demo

screencast example

Install

npm install leaflet-swipe-mode

L.Control.SwipeMode(leftLayer, rightLayer, options)

Creates a new Leaflet Control for comparing two layers or collections of layers. It does not add the layers to the map - you need to do that manually.

Parameters

parametertypedescription
leftLayerL.Layer|arrayA Leaflet Layer or array of layers to show on the left side of the map. Any layer added to the map that is in this array will be shown on the left
rightLayerL.Layer|arrayA Leaflet Layer or array of layers to show on the right side of the map. Any layer added to the map that is in this array will be shown on the right. These should not be the same as any layers in leftLayers
optionsObjectOptions

Options

parametertypedescription
paddingNumberPadding between slider min/max and the edge of the screen in pixels. Defaults to 44 - the width of the slider thumb
buttonButton HTML ElementIf you want to make a webapp with custom html elements, you can bind a button to toggle this plugin. This will bind a click event in your button. If you want full control, see noControl option
noControlBooleanIf you want call all methods by yourself, set it to true. You have to call toggle() directly in code. If you use this option, you can let leftLayer and rightLayer null and set it later with setRightLayer and setRightLayer
textStringYou can translate the plugin inner text
text.titleStringControl's Button text
text.leftLayerSelectorStringSelect's left layer label
text.rightLayerSelectorStringSelect's right layer label

Option example

options = {
    position: 'topleft',
    thumbSize: 42,
    padding: 0,
    button: document.getElementById('my-button'), // If you use this option, the control will be disabled
    text: {
      title: 'Enable Swipe Mode',
      leftLayerSelector: 'Left Layer',
      rightLayerSelector: 'Right Layer',
    },
  };

Events

Subscribe to events using these methods

EventDataDescription
swipemode:startVoidFired when plugin starts
swipemode:stopVoidFired when plugin stops
swipemode:newlayerVoidFired when a layer is added to any side
swipemode:leftlayeraddLayerEventFired when a layer is added to the left-hand-side pane
swipemode:leftlayerremoveLayerEventFired when a layer is removed from the left-hand-side pane
swipemode:rightlayeraddLayerEventFired when a layer is added to the right-hand-side pane
swipemode:rightlayerremoveLayerEventYou guessed it... fired when a layer is removed from the right-hand-side pane
swipemode:dividermove{x: Number}Fired when the divider is moved. Returns an event object with the property x = the pixels of the divider from the left side of the map container.

Methods

MethodReturnsDescription
setLeftLayerthisSet the layer for the left side
setRightLayerthisSet the layer for the right side
togglethisToggles the swipe mode on or off
enabledthisGets a true/false of whether the swipe mode is enabled

Usage

const map = L.map('map').setView([51.505, -0.09], 13);

const myLayer1 = L.tileLayer(...).addTo(map);

const myLayer2 = L.tileLayer(...).addTo(map)

const myLayer3 = L.tileLayer(...).addTo(map)

const options = {...}; // optional

// myLayer1 and myLayer2 will be the default when enabled
L.control.sideBySide(myLayer1, myLayer2, options).addTo(map);

Custom html button

If you are developing a web application and you want to use your own html button outside the map container, you can use the following code:

const map = L.map('map').setView([51.505, -0.09], 13);

const myLayer1 = L.tileLayer(...).addTo(map);

const myLayer2 = L.tileLayer(...).addTo(map)

const myLayer3 = L.tileLayer(...).addTo(map)

const options = {
  button: document.getElementById('my-button'),
}; // optional

// myLayer1 and myLayer2 will be the default when enabled
const sm = L.control.sideBySide(myLayer1, myLayer2, options).addTo(map);

// If you want to change the current layers, just use:
sm.setLeftLayer(myLayer3); //or setRightLayer

Future features

  • Update LayerPicker when a new layer is added to the map

Known issues

  • If you set the same layer to both sides, the control will not work properly.

Limitations

  • The divider is not movable with IE.
  • Probably won't work in IE8, but what does?

License

MIT

0.0.7

4 months ago

0.0.6

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1

8 months ago