Layer Swipe

This example uses OpenLayers v4.0.1. The latest is v.

#### Layer Swipe

Example of a Layer swipe map.

Example of a Layer swipe map.

Related API documentation:

- ol.Map
,
- ol.View
,
- ol.control
,
- ol.layer.Tile
,
- ol.source.BingMaps
,
- ol.source.OSM

Copy Edit
Layer Swipe Example of a Layer swipe map. var osm = new ol.layer.Tile({ source: new ol.source.OSM() }); var bing = new ol.layer.Tile({ source: new ol.source.BingMaps({ key: 'Your Bing Maps Key from http://www.bingmapsportal.com/ here', imagerySet: 'Aerial' }) }); var map = new ol.Map({ layers: [osm, bing], target: 'map', controls: ol.control.defaults({ attributionOptions: /** @type {olx.control.AttributionOptions} */ ({ collapsible: false }) }), view: new ol.View({ center: [0, 0], zoom: 2 }) }); var swipe = document.getElementById('swipe'); bing.on('precompose', function(event) { var ctx = event.context; var width = ctx.canvas.width * (swipe.value / 100); ctx.save(); ctx.beginPath(); ctx.rect(width, 0, ctx.canvas.width - width, ctx.canvas.height); ctx.clip(); }); bing.on('postcompose', function(event) { var ctx = event.context; ctx.restore(); }); swipe.addEventListener('input', function() { map.render(); }, false); <div id="map" class="map"></div> <input id="swipe" type="range" style="width: 100%">

    <!DOCTYPE html>
    <html>
      <head>
        <title>Layer Swipe</title>
        <link rel="stylesheet" href="https://openlayers.org/en/v4.0.1/css/ol.css" type="text/css">
        <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
        <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
        <script src="https://openlayers.org/en/v4.0.1/build/ol.js"></script>
      </head>
      <body>
        <div id="map" class="map"></div>
        <input id="swipe" type="range" style="width: 100%">
        <script>
          var osm = new ol.layer.Tile({
            source: new ol.source.OSM()
          });
          var bing = new ol.layer.Tile({
            source: new ol.source.BingMaps({
              key: 'Your Bing Maps Key from http://www.bingmapsportal.com/ here',
              imagerySet: 'Aerial'
            })
          });

          var map = new ol.Map({
            layers: [osm, bing],
            target: 'map',
            controls: ol.control.defaults({
              attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
                collapsible: false
              })
            }),
            view: new ol.View({
              center: [0, 0],
              zoom: 2
            })
          });

          var swipe = document.getElementById('swipe');

          bing.on('precompose', function(event) {
            var ctx = event.context;
            var width = ctx.canvas.width * (swipe.value / 100);

            ctx.save();
            ctx.beginPath();
            ctx.rect(width, 0, ctx.canvas.width - width, ctx.canvas.height);
            ctx.clip();
          });

          bing.on('postcompose', function(event) {
            var ctx = event.context;
            ctx.restore();
          });

          swipe.addEventListener('input', function() {
            map.render();
          }, false);
        </script>
      </body>
    </html>